Opened 7 years ago

Last modified 13 months ago

#6089 new feature request

Allow declaration splices inside declaration brackets

Reported by: igloo Owned by: simonpj
Priority: normal Milestone:
Component: Template Haskell Version: 7.4.1
Keywords: Cc: Wizek
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: GHC rejects valid program Test Case:
Blocked By: Blocking:
Related Tickets: #11129 Differential Rev(s):
Wiki Page:


I would like to be able to write code like:

{-# LANGUAGE TemplateHaskell #-}

import Language.Haskell.TH

decs :: Q [Dec]
decs = [d| data MyType = MyCon | MyOtherCon

           val1 :: MyType
           val1 = MyCon

           val2 :: MyType
           val2 = MyOtherCon

           $( do let mkFun v i = [| if $v == i then val1 else val2 |]
                 [d| fun3 x = $(mkFun [| x |] 3)
                     fun4 x = $(mkFun [| x |] 4) |] ) |]

but GHC says:

$ ghci decSplices.hs
GHCi, version 7.5.20120420:  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
[1 of 1] Compiling Main             ( decSplices.hs, interpreted )

    Declaration splices are not permitted inside declaration brackets
Failed, modules loaded: none.

As far as I can see there is no technical reason why this couldn't work, with a splice within a declaration bracket splitting the declarations into 3. The declarations before the splice would be renamed, typechecked etc first, then those in the splice, and finally those after the splice.

Change History (12)

comment:1 Changed 7 years ago by simonpj

See; I think it'll be easy to do thus ticket when we do the changes in the blog post.

comment:2 Changed 7 years ago by igloo

Milestone: 7.8.1
Owner: set to simonpj

comment:3 Changed 6 years ago by monoidal


decs :: Q [Dec]
decs = [d|$( [d| fun3 = undefined |] ) |]

GHC 7.6 complains, but GHC 7.7 apparently ignores the splice - $decs does not bring fun3 into scope.

comment:4 Changed 5 years ago by thoughtpolice


Moving to 7.10.1.

comment:5 Changed 5 years ago by thoughtpolice


Moving to 7.12.1 milestone; if you feel this is an error and should be addressed sooner, please move it back to the 7.10.1 milestone.

comment:6 Changed 5 years ago by ezyang

The renamer splices and then typechecks now, so we should be able to do this.

comment:7 Changed 4 years ago by thoughtpolice


Milestone renamed

comment:8 Changed 4 years ago by jstolarek

comment:9 Changed 4 years ago by thomie

Milestone: 8.0.1

comment:10 Changed 17 months ago by gbaz

Cc: gbaz added

comment:11 Changed 17 months ago by gbaz

Cc: gbaz removed

comment:12 Changed 13 months ago by Wizek

Cc: Wizek added
Note: See TracTickets for help on using tickets.