Hello, SYB uses DataRep to represent datatypes: -- | Public representation of datatypes
data DataRep = AlgRep [Constr] | IntRep | FloatRep | StringRep | NoRep
Am I right to believe that StringRep should be CharRep? Note that IntRep is used for the primitives Int and Integer datatypes, FloatRep for Float and Double, and StringRep (apparently) for Char. String, however, is represented as 'AlgRep [[],(:)]': *Main> dataTypeOf 'p'
DataType {tycon = "Prelude.Char", datarep = StringRep} *Main> dataTypeOf "p" DataType {tycon = "Prelude.[]", datarep = AlgRep [[],(:)]}
This makes sense, since String is not a primitive datatype. But it causes the apparently wrong behavior:
*Main> fromConstr (mkStringConstr (dataTypeOf "a") "ab") :: String "*** Exception: mkStringConstr *Main> fromConstr (mkStringConstr (dataTypeOf 'a') "ab") :: String "*** Exception: constrIndex
The correct way of using mkStringConstr is to construct a Char. This, however, only works for strings with a single character: *Main> fromConstr (mkStringConstr (dataTypeOf 'a') "b") :: Char
'b' *Main> fromConstr (mkStringConstr (dataTypeOf 'a') "ab") :: Char *** Exception: gunfold *Main> fromConstr (mkStringConstr (dataTypeOf 'a') "") :: Char *** Exception: gunfold
Wouldn't it be more clear if StringRep would be named CharRep and mkStringConstr named mkCharConstr? Thanks, Pedro
What you say certainly sounds plausible, although I do not have DataRep paged into my brain at the moment. If the generics folk are happy, do make a libraries proposal and, after discussion period, execute! Simon From: generics-bounces@haskell.org [mailto:generics-bounces@haskell.org] On Behalf Of José Pedro Magalhães Sent: 08 December 2008 12:33 To: Generics Mailing List Cc: Americo Vargas Subject: [Hs-Generics] SYB's StringRep behavior Hello, SYB uses DataRep to represent datatypes: -- | Public representation of datatypes data DataRep = AlgRep [Constr] | IntRep | FloatRep | StringRep | NoRep Am I right to believe that StringRep should be CharRep? Note that IntRep is used for the primitives Int and Integer datatypes, FloatRep for Float and Double, and StringRep (apparently) for Char. String, however, is represented as 'AlgRep [[],(:)]': *Main> dataTypeOf 'p' DataType {tycon = "Prelude.Char", datarep = StringRep} *Main> dataTypeOf "p" DataType {tycon = "Prelude.[]", datarep = AlgRep [[],(:)]} This makes sense, since String is not a primitive datatype. But it causes the apparently wrong behavior: *Main> fromConstr (mkStringConstr (dataTypeOf "a") "ab") :: String "*** Exception: mkStringConstr *Main> fromConstr (mkStringConstr (dataTypeOf 'a') "ab") :: String "*** Exception: constrIndex The correct way of using mkStringConstr is to construct a Char. This, however, only works for strings with a single character: *Main> fromConstr (mkStringConstr (dataTypeOf 'a') "b") :: Char 'b' *Main> fromConstr (mkStringConstr (dataTypeOf 'a') "ab") :: Char *** Exception: gunfold *Main> fromConstr (mkStringConstr (dataTypeOf 'a') "") :: Char *** Exception: gunfold Wouldn't it be more clear if StringRep would be named CharRep and mkStringConstr named mkCharConstr? Thanks, Pedro
participants (2)
-
José Pedro Magalhães -
Simon Peyton-Jones