Existentials and Scrap your Boilerplate

Posted by finnsson on Stack Overflow See other posts from Stack Overflow or by finnsson
Published on 2010-05-15T10:16:06Z Indexed on 2010/05/23 8:40 UTC
Read the original article Hit count: 264

Filed under:
|
|

I'm writing a XML (de)serializer using Text.XML.Light and Scrap your Boilerplate (at http://github.com/finnsson/Text.XML.Generic) and so far I got working code for "normal" ADTs but I'm stuck at deserializing existentials.

I got the existential data type

data DataBox where
  DataBox :: (Show d, Eq d, Data d) => d -> DataBox

and I'm trying to get this to compile

instance Data DataBox where
  gfoldl k z (DataBox d) = z DataBox `k` d
  gunfold k z c = k (z DataBox)  -- not OK 
  toConstr (DataBox d) = toConstr d
  dataTypeOf (DataBox d) = dataTypeOf d

but I can't figure out how to implement gunfold for DataBox.

The error message is

Text/XML/Generic.hs:274:23:
    Ambiguous type variable `b' in the constraints:
      `Eq b'
        arising from a use of `DataBox' at Text/XML/Generic.hs:274:23-29
      `Show b'
        arising from a use of `DataBox' at Text/XML/Generic.hs:274:23-29
      `Data b' arising from a use of `k' at Text/XML/Generic.hs:274:18-30
    Probable fix: add a type signature that fixes these type variable(s)

It's complaining about not being able to figure out the data type of b.

I'm also trying to implement dataCast1 and dataCast2 but I think I can live without them (i.e. an incorrect implementation).

I guess my questions are:

  1. Is it possible to combine existentials with Scrap your Boilerplate?
  2. If so: how do you implement gunfold for an existential data type?

© Stack Overflow or respective owner

Related posts about generics

Related posts about reflection