[Haskell-beginners] Type of function with constant pattern

Tim Perry tim.v2.0 at gmail.com
Tue Apr 10 23:16:15 CEST 2012


I believe that "f 0 = ..." is a guard and the guard is pattern matching on
the constructor. Despite the fact that you don't have an instance of "f _ =
....", the compiler needs an Eq instance to determine if it should run the
"f 0" version of the function.

Does that make sense? Hopefully someone with a better grasp of the topic
will fill in the details.

--Tim

On Tue, Apr 10, 2012 at 12:57 PM, <j.romildo at gmail.com> wrote:

> Hello.
>
> Given the following function definitions
>
>   f 0 = True
>
>   g False = True
>
> ghc infers the following types for the functions:
>
>   f :: (Eq a, Num a) => a -> Bool
>   g :: Bool -> Bool
>
> Why f has "Eq a" in the context in ts type, and g does not?
>
> As both are defined using a constant pattern, I expected none of them
> should require the type of the argument to be instance of Eq.
>
> Romildo
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20120410/f0186337/attachment.htm>


More information about the Beginners mailing list