TH not compiling when writing the code directly does

I’m stuck getting a TH expansion to compile. When I take the generated code and compile it directly, it is fine. But when expanding and compiling, it fails. The problem seems related to instanceD, where I provide the type as: (appT (appT (conT devName') (varT $ mkName "Word8")) (varT $ mkName "BV”)) which expands to: IDENTIFICATION__MODEL_ID Word8 BV No matter what I put for the second type (BV), it generates the compile error. For example, Word8 or Word16 gives the same error. But if I take the resulting expression and just put it into the file and compile, it compiles fine. So something about expansion is causing the problem. Is there some more correct way to make the expression rather than nesting two appT? Any ideas? Note: the generation of the functions with (map (\f -> returnQ f) (concat funs)) is ugly in my opinion. I could not find the right way to express this. So all ideas welcome. Mike GEN DEC -------------- makeInstance :: String -> [String] -> Q [Dec] makeInstance devName regNames = do let devName' = mkName devName let regNames' = map mkName regNames let ctx = return [] :: Q [Pred] funs <- mapM (\regName -> [d| $(varP regName) = $(varE $ mkName "writeReadField") $(stringE $ nameBase regName)(get fi) (set fi) where fi = $(conE $ mkName "FieldInfo") 3 1 get info = $(varE $ mkName "getValue") info set info = $(varE $ mkName "setValue") info |]) regNames' instance_ <- instanceD ctx (appT (appT (conT devName') (varT $ mkName "Word8")) (varT $ mkName "BV")) (map (\f -> returnQ f) (concat funs)) return [instance_] COMPILE ERROR ------------------------- src/TestFields.hs:175:3: Illegal type variable name: ‘BV’ When splicing a TH declaration: instance IDENTIFICATION__MODEL_ID Word8 BV where id = writeReadField "id" (get_0 fi_1) (set_2 fi_1) where fi_1 = FieldInfo 3 1 get_0 info_3 = getValue info_3 set_2 info_4 = setValue info_4 ids = writeReadField "ids" (get_5 fi_6) (set_7 fi_6) where fi_6 = FieldInfo 3 1 get_5 info_8 = getValue info_8 set_7 info_9 = setValue info_9

Dear Michael, Michael Jones wrote:
I’m stuck getting a TH expansion to compile. When I take the generated code and compile it directly, it is fine. But when expanding and compiling, it fails.
The problem seems related to instanceD, where I provide the type as:
(appT (appT (conT devName') (varT $ mkName "Word8")) (varT $ mkName "BV”))
[...]
Illegal type variable name: ‘BV’
Indeed BV is the name of a type *constructor*, not a type *variable*, so you need to use `conT` instead of `varT`: (appT (appT (conT devName') (conT $ mkName "Word8")) (conT $ mkName "BV”)) HTH, Bertram

Bertram,
Oh I see. The items passed to the devName’ constructor must be constructors too.
I guess I did not think of that way. I thought of them as filling holes in constructor, thus values.
Too much OO in my past :-(
I’m slowly rebuilding those old neurons into something better.
Mike
On Mar 12, 2015, at 5:05 AM, Bertram Felgenhauer
Dear Michael,
Michael Jones wrote:
I’m stuck getting a TH expansion to compile. When I take the generated code and compile it directly, it is fine. But when expanding and compiling, it fails.
The problem seems related to instanceD, where I provide the type as:
(appT (appT (conT devName') (varT $ mkName "Word8")) (varT $ mkName "BV”))
[...]
Illegal type variable name: ‘BV’
Indeed BV is the name of a type *constructor*, not a type *variable*, so you need to use `conT` instead of `varT`:
(appT (appT (conT devName') (conT $ mkName "Word8")) (conT $ mkName "BV”))
HTH,
Bertram _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
participants (2)
-
Bertram Felgenhauer
-
Michael Jones