Skip to content

Can't use quotations ([| ... |]) insides declaration splices

Normal Template Haskell allows many levels of splicing and quotations:

> runQ [| (const $([|"Int"|])) |]
AppE (VarE GHC.Base.const) (LitE (StringL "Int"))

And TH can parse declarations easily:

> runQ [d| type T = Int|]
[TySynD T [] (ConT GHC.Base.Int)]

But splicing inside a declaration gives a syntax error:

> runQ [d| type T = $([t|Int|])|]
parse error on input `$('

Another example. Bulat Ziganshin defines the cnst function (http://www.haskell.org/bz/th3.htm) as:

cnst 0 str = [| str |]
cnst n str = [| \_ -> $(cnst (n-1) str) |]

Which evaluates to a function that takes n arguments and returns the string given. It would make sense that a function to define a data constructor which takes n arguments could be written similarly:

dataCnst n = [d|data D = D $(dataCnst' n)|]
dataCnst' n = [t|Int|] ++ dataCnst' (n - 1)

Finally, a function that makes a data type which varies the field type based on an argument:

dataVar n = [d|data D = D $(dataVar' n)|]
dataVar' "Int" = ''Int
dataVar' "String" = ''String
etc.
Trac metadata
Trac field Value
Version 6.8.2
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Template Haskell
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
Edited by m4dc4p
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information