Opened 10 years ago

Closed 9 years ago

Last modified 7 years ago

#2221 closed bug (duplicate)

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

Reported by: m4dc4p Owned by:
Priority: normal Milestone:
Component: Template Haskell Version: 6.8.2
Keywords: Cc:
Operating System: Windows Architecture: x86
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:


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 ( 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

Change History (4)

comment:1 Changed 10 years ago by m4dc4p

Summary: Can't use quotationes ([| ... |]) insides declaration splicesCan't use quotations ([| ... |]) insides declaration splices

comment:2 Changed 9 years ago by igloo

difficulty: Unknown
Resolution: duplicate
Status: newclosed

Thanks for the report. The problem you're seeing here is that you can't splice in types; for more info, see #1476.

comment:3 Changed 7 years ago by simonpj

Type of failure: None/Unknown

In GHC 6.12 you can splice into types, so the examples here work.

comment:4 Changed 7 years ago by m4dc4p

Awesome, thanks for taking a look. Great to know!

Note: See TracTickets for help on using tickets.