Beta Reduction, undefined vs Type Classes

Brian Boutel boutelbNOSPAM at acm.org
Mon Nov 10 13:16:05 EST 2003


Jared Warren wrote:

>Consider:
>
>  
>
>>class Thing t where
>>  thing :: t
>>
>>instance Thing Int where
>>  thing = 0
>>
>>instance Thing Char where
>>  thing = 'a'
>>    
>>
>
>Can someone please explain why
>
>  
>
>>fst (1,thing)
>>    
>>
>
>...gets an 'ambiguous type variable' error, but
>
>  
>
>>fst (1,undefined)
>>    
>>
>
>...doesn't? 
>
Perhaps because "thing" has an ambiguous type (it's either Int of Char), 
but "undefined" doesn't, (it's for all a.a).

Remember that type inference in the Haskell type system does not assume 
knowledge of the semantics of functions. In order to deduce the type of 
an application of "fst", you need to determine the type of its argument 
- you can't discard one of ithe tuple components just because you know 
you will lose it when you apply a projection function. Type checking is 
done before any evaulation, compile-time or run-time.

You might argue that it would be a good idea to apply some program 
transformations early to avoid this kind of unnecessary ambiguity, but I 
have doubts about its general usefullness.

>And is there anything I can change to make the former work
>as well? Even modifying fst doesn't work:
>
>  
>
>>fst' :: Thing b => (a,b) -> a
>>fst' (~a,~b) = a
>>    
>>
>
>  
>
You should not expect it to work. The problem is with the type 
ambiguity, not with the semantics of fst.

--brian




-- 
Brian Boutel
Wellington New Zealand


Note the NOSPAM




More information about the Haskell-Cafe mailing list