Title: any working operator overloading example in haskell
Tags: haskell;operator-overloading;overloading
Question: 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
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
(==) 75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0 -&gt; a -&gt; 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
(==) 75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0 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', salazarsean@example.org:4:7
or `Prelude.Eq',
imported from `salazarsean@example.org:1:1
(and originally defined in `GHC.Classes')
Comment: try only the instance parts. The typeclass definition is already made in Prelude. Alternatively, hide the import of the prelude definition.

Comment: 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.

Comment: If you make == mean something other than equality, your code will be hard to understand. Consider using === instead.

Comment: then how to overload == for Trees and return always 5 ?

Here is the accepted answer: 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) =&gt; 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)
Comment for this answer: @amindfv, the "open world assumption" is actually something else—the assumption that new instances can always be added.
Comment for this answer: @user1308990: No; we actually can't control when they're exported -- they're *always* exported. This is called the "open world assumption"
Comment for this answer: 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 ...

Here is another answer: Here's a +++ operator that acts like the (++) operator used to append lists:
(+++) 75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0 [a]-&gt;[a]-&gt;[a]
x +++ [] = x
[] +++ x = x
x  +++ y = (init x) +++ ((last x) : y)
Comment for this answer: What does this answer add to the previous ones?

Here is another answer: 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
Comment for this answer: @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.
Comment for this answer: @user1308990: No, you don't need to list `==` in the exports. Instances are exported automatically.
Comment for this answer: How to specify the types of x and y if x and y is type Tree? Tree a = Node a | Empty
Comment for this answer: 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

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2013:00:04/26/13', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<haskell><operator-overloading><overloading>', 'Title': 'any working operator overloading example in haskell', 'ViewCount': 1655, 'id': 16241556, 'user_AboutMe': '', 'user_AccountId': 1374764.0, 'user_DisplayName': 'oiyio', 'user_Reputation': 4539.0, 'user_Views': 482.0, 'user_id': 1308990.0}}