[Haskell-beginners] Ambiguous type variable

Daniel Fischer daniel.is.fischer at googlemail.com
Sat Jul 30 23:23:49 CEST 2011


On Saturday 30 July 2011, 22:57:04, Ovidiu Deac wrote:
> I tried  {-# LANGUAGE ExtendedDefaultRules #-} and it didn't work. I
> tried to put it both in Stack.hs and in TestStack.hs

Aha. I don't have the impression ExtendedDefaultRules will become a much-
used extension.

> 
> Then I tried to be specific about the type in the test.
> 
> Both this code:
>          (pop EmptyStack :: Stack[Char]) ≡ (Nothing, EmptyStack)

This needs parentheses since the '::' has very low fixity.
Without parentheses it's parsed

    ((pop EmptyStack) :: Stack [Char]) == ...

so the type sugnature applies to the result of pop.

I wrote

   ( pop (EmptyStack :: Stack [Char]) == (...,...) )

(extra spaces added to unclutter the outermost parentheses) so that the 
type signature applies only to EmptyStack.

Getting the parentheses for expression type signatures right is notoriously 
tricky (unless you use parens in every case of the slightest doubt) at 
first, I should better have given the signature to Nothing,

   ( pop EmptyStack == (Nothing :: Maybe [Char], EmptyStack) )

that would have been harder to misread.

> and this:
>          (pop EmptyStack :: Stack[Char]) ≡ (Nothing :: Maybe[Char],
> EmptyStack :: Stack[Char])

Note that specifying the type of one subexpression is enough, pop's type 
then determines the others.



More information about the Beginners mailing list