Why does ghci list the equality type constraint in the type signature for this matchInt function, which I built using pattern matching:
$ ghci GHCi, version 8.2.1: http://www.haskell.org/ghc/ :? for help Prelude> :{ Prelude| matchInt 1 = 3 Prelude| matchInt _ = 4 Prelude| :} Prelude> matchInt 1 3 Prelude> matchInt 22 4 Prelude> :t matchInt matchInt :: (Eq a, Num a, Num p) => a -> p
In contrast, when using a simple data constructor, there is no equality type constraint.
$ ghci GHCi, version 8.2.1: http://www.haskell.org/ghc/ :? for help Prelude> data Z = Y Int Prelude> :{ Prelude| matchDataInt (Y 1) = 3 Prelude| matchDataInt _ = 4 Prelude| :} Prelude> matchDataInt (Y 1) 3 Prelude> matchDataInt (Y 22) 4 Prelude> :t matchDataInt matchDataInt :: Num p => Z -> p
Indeed, instances of Z may not be compared:
Prelude> Y 22 == Y 33 <interactive>:11:1: error: • No instance for (Eq Z) arising from a use of '==' • In the expression: Y 22 == Y 33 In an equation for 'it': it = Y 22 == Y 33
So, why does matchInt represent a list as a type constraint, but not a matchDataInt function?
This question is related. However, if an equality test is needed for matchDataInt , then why is it not needed for matchDataInt ? And here I come to my key point: should not both matchInt and matchDataInt be tested against 1 in order to match the pattern to work?
equality algebraic-data-types haskell typeclass ghci
Rick majpruz
source share