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)
The numeric operations in the
Numtypeclass 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 existingNumclass 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.
Instead of a
Num-like class that encompasses+,-and*, it’s more flexible to define different typeclasses for different operands, because whilePoint3D * Doublemakes sense,Point3D + Doubleusually does not. Let’s start withMul.Without extensions, typeclasses only ever contain a single type parameter, but with
MultiParamTypeClasses, we can declare a typeclass likeMulfor the combination of typesa,bandc. The part after the parameters,| a b -> cis a “functional dependecy” which in this case states that the typecis dependent onaandb. This means that if we have an instance likeMul Double Point3D Point3Dthe functional dependency states that we can’t have any other instancesMul Double Point3D c, wherecsomething other thanPoint3D, 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: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
Because the literal
5isn’t necessarily of typeDouble. It can be anyNum n => n, and it’s entirely possible that someone declares new instances likeMul Point3D Int Intwhich behaves completely differently. So what this means is that we need to specify the types of numerical literals explicitly.Now, if instead of defining new operands we wish to override the default
Numclass fromPrelude, we can do it like this