Reputation: 2402
Is there any way to make a class instance return a value which is not of the instance's type? An example is wanting to return a value of type Double for the the scalar product of two vectors:
-- data structure to contain a 3D point in space
data Point3D = Point3D !Double !Double !Double
deriving (Eq, Ord)
instance Num Point3D where
-- Multiplication, scalar == Dot product
Point3D x1 y1 z1 * Point3D x2 y2 z2 = x1*x2 + y1*y2 + z1*z2 :: Double
Furthermore, is there any way to define how operators work between functions of different types? For example, I would like to define Point3D x y z + Double a = Point3D (x + a) (y + a) (z + a)
Upvotes: 4
Views: 796
Reputation: 24802
The numeric operations in the Num
typeclass are all defined with the type :: Num n => n -> n -> n
, so both operands and the return value must have the same type. There's no way to alter an existing typeclass, so your options are either to define new operators or hide the existing Num
class and replace it completely with your own implementation.
In order to implement operators that can have different operand types, you are going to need a couple of language extensions.
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}
Instead of a Num
-like class that encompasses +
, -
and *
, it's more flexible to define different typeclasses for different operands, because while Point3D * Double
makes sense, Point3D + Double
usually does not. Let's start with Mul
.
class Mul a b c | a b -> c where
(|*|) :: a -> b -> c
Without extensions, typeclasses only ever contain a single type parameter, but with MultiParamTypeClasses
, we can declare a typeclass like Mul
for the combination of types a
, b
and c
. The part after the parameters, | a b -> c
is a "functional dependecy" which in this case states that the type c
is dependent on a
and b
. This means that if we have an instance like Mul Double Point3D Point3D
the functional dependency states that we can't have any other instances Mul Double Point3D c
, where c
something other than Point3D
, i.e. the return type of the multiplication is always unambiguously determined by the type of the operands.
Here's how we implement instances for Mul
:
instance Mul Double Double Double where
(|*|) = (*)
instance Mul Point3D Double Point3D where
Point3D x y z |*| a = Point3D (x*a) (y*a) (z*a)
instance Mul Double Point3D Point3D where
a |*| Point3D x y z = Point3D (x*a) (y*a) (z*a)
This flexibility does not come without its caveats, though, because it will make type inference a lot more difficult for the compiler. For example, you can't simply write
p = Point3D 1 2 3 |*| 5
Because the literal 5
isn't necessarily of type Double
. It can be any Num n => n
, and it's entirely possible that someone declares new instances like Mul Point3D Int Int
which behaves completely differently. So what this means is that we need to specify the types of numerical literals explicitly.
p = Point3D 1 2 3 |*| (5 :: Double)
Now, if instead of defining new operands we wish to override the default Num
class from Prelude
, we can do it like this
import Prelude hiding (Num(..))
import qualified Prelude as P
class Mul a b c | a b -> c where
(*) :: a -> b -> c
instance Mul Double Double Double where
(*) = (P.*)
instance Mul Point3D Double Point3D where
Point3D x y z * a = Point3D (x*a) (y*a) (z*a)
Upvotes: 6
Reputation: 23014
You can create your custom class with a multiplication that can take different types.
import Prelude hiding ((*))
import qualified Prelude
class Mul a b c | a b -> c where (*) :: a -> b -> c
instance Mul Double Double Double where (*) = (Prelude.*)
instance Mul Double Int Double where a * b = a Prelude.* fromIntegral b
...
You need to turn on multi-parameter type classes and functional dependencies for this to work.
Upvotes: 4
Reputation: 68152
There is no way to get the standard Num
functions (including the operators) to return a different type. *
has the type Num n => n -> n -> n
which means the n
has to be the same type throughout.
There is also no way to have a standard Num
function (like +
) work with arguments of two different types.
The usual solution to this problem is to create a new operator. So you could create a scalar addition operator like |+|
and use that to add doubles to your points.
If you aren't against unicode, you could use · for your dot product :). Haskell supports this, but other programs may have difficulty typing unicode.
Upvotes: 5