[Haskell-cafe] A generics question

Sterling Clover s.clover at gmail.com
Mon Jun 8 23:18:54 EDT 2009

On Jun 8, 2009, at 7:10 PM, Henry Laxen wrote:
> convert :: (Data a, Data b) =Int -a -b
> convert i x =
>   let c = dataTypeConstrs (dataTypeOf x) !! (i-1)
>   in fromConstr c
> I would like to be able to say: x = convert 1 c and have it
> assign Red to x then I would like to say: y = convert 1 s and
> have it assign Small to y, however, when I try that I get:
>     Ambiguous type variable `b' in the constraint:
>       `Data b' arising from a use of `convert' at <interactive>:1:8-18
>     Probable fix: add a type signature that fixes these type  
> variable(s)
> Of course if I say x :: Color = convert 1 c, it works, but I
> would like to avoid that if possible, as all of the information
> is already contained in the parameter c.  Is there any way to do
> this?  Thanks in advance for your wise counsel.
> Best wishes,
> Henry Laxen

The type signature for 'convert' is throwing away the information you  

Try it with the following type signature and it should work fine:

convert :: (Data a) => Int -> a -> a

Of course, as has been noted, SYB is a rather big sledgehammer for  
the insect in question.


More information about the Haskell-Cafe mailing list