[Haskell-cafe] Ambiguous types for collection keys

Brandon Moore brandonm at yahoo-inc.com
Wed Apr 12 15:34:16 EDT 2006


Robert Dockins wrote:
> 
> On Apr 12, 2006, at 3:18 PM, Scott Weeks wrote:
> 
>>
>>
>>>
>>> Well, if you get an "ambiguous type variable" error, you probably (I
>>> think) need to add some type annotations. For example:
>>>
>>> class Foo a where
>>>   foo :: a
>>>   bar :: a -> String
>>>
>>> Evaluating bar foo will result in an error, but bar (foo :: Integer)
>>> will work just fine.
>>> ____________________
>>
>>
>> The problem is that I get  an incoming value which is a key of some  
>> sort. There's no way of knowing what type that value is supposed to  
>> be until I compare it with the schema from the above example. where  I 
>> _am_ adding type annotations.
>>
>> coerceIndex f (Schema _ SInt SPtr _) (r,hdl,o,hdr) = f  
>> (r::IdxInt,hdl,o,hdr)
>> coerceIndex f (Schema _ SStr SPtr _) (r,hdl,o,hdr) = f  
>> (r::IdxPS,hdl,o,hdr)
>>
>>
>> When I try to add type annotations I get a complaint from the  
>> typechecker that says (In the case of the above example) Expected  
>> type: Integer, Inferred Type: PackedString.
>>
>> Is the alternative to write different "select" methods for each key  
>> type (selectInt, selectPS, ...)? God I hope not, that would be a  bit 
>> scary.
> 
> 
> 
> I'm not 100% sure I understand your use case, but I think you might  be 
> able to crack this by using Data.Dynamic:
> 
> http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data- 
> Dynamic.html

Or carry an instance in along with a type parameter, using existentials 
or GADT.

Brandon Moore


More information about the Haskell-Cafe mailing list