I want to overload any operator . i want to do such a simple function that for instance think about overloading of == operator .Overload == such that
x==y
returns x .
Or x==y return x+y. It doesn't matter what . Can you show me any simple operator overloading example? I cannot find any example on the web unfortunately.
For example;when i call Tree a == Tree a return 5 (it always return 5. I select it ,it is not related to any thing) or when i call 3==4 return : 7
I tried the below codes(i find it from haskell.org) but it cannot compile.
class Eq a where
(==) ::a -> a -> Int
instance Eq Integer where
x == y = 5
instance Eq Float where
x == y = 5
Neither the below code works:
data Tree a = Node a | Empty
class Tree a where (==) :: Tree a -> Tree a -> Int
instance Tree Integer where x == y = 1
I take the error :
Ambiguous occurrence `Eq'
It could refer to either `Main.Eq', defined at Operations.hs:4:7
or `Prelude.Eq',
imported from `Prelude' at Operations.hs:1:1
(and originally defined in `GHC.Classes')
Eq
class defined in the Prelude requires that the result of==
is aBool
, so to return5
you'd have to hide that and define your own. – hammar