Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Define EQ Instance

I am trying to create an Eq Instance, but I have some issues creating it.

I have the following data definitions:

data Link = G | S | P deriving (Eq, Show)
data Chain = Empty | Join Link Chain

And Now I want to convert Chain into an Eq Instance (True when it is the same).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here is my current instance:

instance Eq Chain where
(Join a b) == (Join c d) = (a == c) && (b == d)
Empty      == Empty      = True
_          == _          = False

it marks (a == c) && (b == d)as mistake (Ambiguous occurrence ‘==’)

I am unsure why this is, and would appreciate it if someone could help me.

Thank you in advance!

>Solution :

You failed to indent the method definitions.

instance Eq Chain where
↓↓
  Join a b == Join c d = a==c && b==d
  Empty    == Empty    = True
  _        == _        = False
↑↑

…Or alternatively, but highly dis-recommended, put them in braces

instance Eq Chain where {
Join a b == Join c d = a==c && b==d;
Empty    == Empty    = True;
_        == _        = False
}

Without the indentation or braces, what your code does is declare an empty Eq instance, and a separate, completely new operator that happens to be also called ==. I.e., you actually wrote this:

module Ch where

instance Eq Chain where
  -- missing definitions of `Prelude.==` here

(Ch.==) :: Chain -> Chain -> Bool
Join a b Ch.== Join c d = a==c && b==d
Empty    Ch.== Empty    = True
_        Ch.== _        = False

The compiler error tells you that the a==c and b==d definitions are now ambiguous: is this referring to Prelude.== or to Ch.==?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading