Opened 6 years ago

Closed 6 years ago

Last modified 3 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 Difficulty: Unknown
Test Case: Blocked By:
Blocking: Related Tickets:

Description

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.

Change History (4)

comment:1 Changed 6 years ago by m4dc4p

  • Summary changed from Can't use quotationes ([| ... |]) insides declaration splices to Can't use quotations ([| ... |]) insides declaration splices

comment:2 Changed 6 years ago by igloo

  • Difficulty set to Unknown
  • Resolution set to duplicate
  • Status changed from new to closed

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 3 years ago by simonpj

  • Type of failure set to None/Unknown

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

comment:4 Changed 3 years ago by m4dc4p

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

Note: See TracTickets for help on using tickets.