Representation of unboxed primitives in TH
I am writing some code in Template Haskell which I hope to use to transform code. One thing I want to do is replace integers and Rationals with their unboxed equivalents. However, Template Haskell doesn't support this. The relevant error message, triggered by the following expression: $([| 20# |]) is: DsMeta.repLiteral: trying to represent exotic literal I naively tried to add support for the representation of unboxed types by adding the following clauses to repLiteral HsIntPrim _ -> intPrimLName HsFloatPrim _ -> floatPrimLName HsDoublePrim _ -> doublePrimLName I then added the relevant *IdKey and *Name definitions and added the following constructors to the Lit type in THSyntax.hs IntPrim, FloatPrim, DoublePrim. I had a feeling this wouldn't work. I mean, they're obviously "exotic" and my suspicion was confirmed when I got segmentation faults compiling the code. main = putStrLn (show $([| I# 20# |])) My question then is, why are they hard to represent? What is it that I am missing? Sean Seefried
On Sat, Apr 19, 2003 at 11:38:11PM +1000, Sean Seefried wrote:
I am writing some code in Template Haskell which I hope to use to transform code. One thing I want to do is replace integers and Rationals with their unboxed equivalents. However, Template Haskell doesn't support this.
I've just tried this in my local tree (only done IntPrim) and it was all as you say (assuming you also made the necessary changes to hsSyn/Convert.lhs too) but I also needed to change repLiteral in deSugar/DsMeta.hs as this bit of diff shows: repLiteral :: HsLit -> DsM (Core M.Lit) repLiteral lit - = do { lit_expr <- dsLit lit; rep2 lit_name [lit_expr] } + = do { lit_expr <- dsLit lit'; rep2 lit_name [lit_expr] } where + lit' = case lit of + HsIntPrim i -> HsInteger i + _ -> lit lit_name = case lit of HsInteger _ -> integerLName HsInt _ -> integerLName + HsIntPrim _ -> intPrimLName HsChar _ -> charLName HsString _ -> stringLName HsRat _ _ -> rationalLName I haven't looked into this in detail, but I'm sure Simon will jump up if it is the wrong thing to do. I probably won't be committing until the HEAD builds. Thanks Ian
participants (2)
-
Ian Lynagh -
Sean Seefried