0

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')
4
  • 4
    try only the instance parts. The typeclass definition is already made in Prelude. Alternatively, hide the import of the prelude definition.
    – m09
    Apr 26, 2013 at 16:59
  • then how to overload == for Trees and return always 5 ?
    – oiyio
    Apr 26, 2013 at 17:00
  • The Eq class defined in the Prelude requires that the result of == is a Bool, so to return 5 you'd have to hide that and define your own.
    – hammar
    Apr 26, 2013 at 17:22
  • If you make == mean something other than equality, your code will be hard to understand. Consider using === instead.
    – AndrewC
    Apr 27, 2013 at 8:00

3 Answers 3

2

You can't hide instances from an imported module. See for example: Explicitly import instances

It looks like the "overloading" you're trying to do is to allow (==) for other types, like trees. This is easy! Just simply create a new instance:

data Tree a = Leaf a | Branch [Tree a]

 instance (Eq a) => Eq (Tree a) where
    (Leaf a)   == (Leaf b)   = a == b
    (Branch a) == (Branch b) = a == b
    _          == _          = False

(You could also just derive the Eq instance)

3
  • do we have to write anything for overloading of == in module? For example; module x (f) where f a = a . And the above code exists. Do we have to such a thing module x (f,'==') where ...
    – oiyio
    Apr 26, 2013 at 17:33
  • @user1308990: No; we actually can't control when they're exported -- they're always exported. This is called the "open world assumption"
    – amindfv
    Apr 26, 2013 at 21:29
  • @amindfv, the "open world assumption" is actually something else—the assumption that new instances can always be added.
    – dfeuer
    Jun 15, 2015 at 19:12
2

Try hiding the == from the Prelude first. You only need a type class if you want it to work differently for different types.

import Prelude hiding ((==))

x == y = x
4
  • How to specify the types of x and y if x and y is type Tree? Tree a = Node a | Empty
    – oiyio
    Apr 26, 2013 at 17:05
  • @user1308990: I'm not quite sure what you mean, but you can just do the same as you would for any function. Operators only have slightly different syntax.
    – hammar
    Apr 26, 2013 at 17:12
  • OK.I got it . i do it to understand how overloading works. Now it is clear. However , i have one question too and i commented the question below the @amindfv 's answer . Could you clear it? Thanks
    – oiyio
    Apr 26, 2013 at 17:38
  • @user1308990: No, you don't need to list == in the exports. Instances are exported automatically.
    – hammar
    Apr 26, 2013 at 17:42
0

Here's a +++ operator that acts like the (++) operator used to append lists:

(+++) :: [a]->[a]->[a]
x +++ [] = x
[] +++ x = x
x  +++ y = (init x) +++ ((last x) : y)
1
  • What does this answer add to the previous ones?
    – dfeuer
    Jun 15, 2015 at 19:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.