[Haskell-beginners] A few really short beginners questions

Brent Yorgey byorgey at seas.upenn.edu
Sun Oct 3 15:06:29 EDT 2010


On Sun, Oct 03, 2010 at 11:11:47AM -0700, Andrew Sackville-West wrote:
> > 
> > Why is the following example not valid?
> > 
> >   f :: a -> a
> >   f '0' = 0
> >   f x = 1
> > 
> > I think the reason lies in the type system but I can't locate it exactly.
> > 
> 
> in the type declaration, you have claimed that f takes an object of
> one type and returns another object of that same type. That is, every
> instance of 'a' in the type declaration has to refer to the *same*
> type. 'a' gets bound to a specific type and must match that type
> throughout. 
> 
> now, f '0' = 0 has type f::Num t => Char -> t. This means that f takes
> a char and returns some numeric type. But char is not an instance of
> Num, and so this definition of f does not match its type signature.

This is a good point, I hadn't even noticed the type mismatch!
However, note that this isn't the whole story:

  f :: a -> a
  f '0' = 'a'
  f x   = x

does not work either, for the reasons I mentioned in my other email.

-Brent


More information about the Beginners mailing list