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 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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With