[Haskell-cafe] Ambiguous type variable - help!

phil at beadling.co.uk phil at beadling.co.uk
Mon Jul 20 16:26:05 EDT 2009


On 19 Jul 2009, at 21:18, Yitzchak Gale wrote:

> Hi Phil,

>
>
>> I've concocted a very simple example to illustrate this (below) - but
>> it doesn't compile because ghc complains that my type is ambiguous  
>> arising
>> from my use of 'fromSeq'.
>
> Notice that you have given two completely separate sets
> of instructions of what to do depending on whether Int
> or Double is selected. You have not given any indication
> of how to choose between them, even at runtime. Of course,
> the compiler doesn't care that your string constants "Int" and
> "Double" happen also to be the names of types if unquoted.

I see now.  I'm passing fromSeq a SeqType, but it has no way
of knowing if I want to process it as an Int or a Double.
The only thing which is polymorphic is nextSeq as it must handle
the underlying state of Int and Double.

Your result function handles the general case and the typeclass
instances deal with the specialization depending on the state's type.

The printResult function takes in a SeqType and then "parses" (for  
want of
a better word) out the
underlying type of Int or Double.  It then calls results against the  
Int or Double which
in turn will invoke the correct version of nextSeq.


Thank you very much for explaining this!


Phil.

>
> import Control.Monad.State -- Why Strict? Haskell is lazy by default.
>

Ahh, no reason for the Strict - in the large program I'm righting it  
is required
because otherwise I end up with almighty thunks.  But here it serves  
no purpose.



More information about the Haskell-Cafe mailing list