Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you overload + in haskell?

Tags:

haskell

While I've seen all kinds of weird things in Haskell sample code - I've never seen an operator plus being overloaded. Is there something special about it?

Let's say I have a type like Pair, and I want to have something like

 Pair(2,4) + Pair(1,2) = Pair(3,6) 

Can one do it in haskell?

I am just curious, as I know it's possible in Scala in a rather elegant way.

like image 855
Andriy Drozdyuk Avatar asked Nov 29 '11 08:11

Andriy Drozdyuk


People also ask

Does Haskell support overloading?

Haskell's type class mechanism supports composability in two dimensions. First, it allows users to de- fine overloaded functions from other overloaded functions, and second, it allows compound instances to be defined from simpler instances.

What do you mean by type class in Haskell?

Inbuilt Type Class In Haskell, every statement is considered as a mathematical expression and the category of this expression is called as a Type. You can say that "Type" is the data type of the expression used at compile time. To learn more about the Type, we will use the ":t" command.


1 Answers

Yes

(+) is part of the Num typeclass, and everyone seems to feel you can't define (*) etc for your type, but I strongly disagree.

newtype Pair a b = Pair (a,b)  deriving (Eq,Show)  

I think Pair a b would be nicer, or we could even just use the type (a,b) directly, but...

This is very much like the cartesian product of two Monoids, groups, rings or whatever in maths, and there's a standard way of defining a numeric structure on it, which would be sensible to use.

instance (Num a,Num b) => Num (Pair a b) where    Pair (a,b) + Pair (c,d) = Pair (a+c,b+d)    Pair (a,b) * Pair (c,d) = Pair (a*c,b*d)    Pair (a,b) - Pair (c,d) = Pair (a-c,b-d)    abs    (Pair (a,b)) = Pair (abs a,    abs b)     signum (Pair (a,b)) = Pair (signum a, signum b)     fromInteger i = Pair (fromInteger i, fromInteger i) 

Now we've overloaded (+) in an obvious way, but also gone the whole hog and overloaded (*) and all the other Num functions in the same, obvious, familiar way mathematics does it for a pair. I just don't see the problem with this. In fact I think it's good practice.

*Main> Pair (3,4.0) + Pair (7, 10.5) Pair (10,14.5) *Main> Pair (3,4.0) + 1    -- * Pair (4,5.0) 

* - Notice that fromInteger is applied to numeric literals like 1, so this was interpreted in that context as Pair (1,1.0) :: Pair Integer Double. This is also quite nice and handy.

like image 121
AndrewC Avatar answered Sep 30 '22 05:09

AndrewC