Opened 5 years ago

Last modified 19 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:
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:

Description

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: http://www.haskell.org/ghc/  :? 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 )

decSplices.hs:15:15:
    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 (9)

comment:1 Changed 5 years ago by simonpj

See http://hackage.haskell.org/trac/ghc/blog/Template%20Haskell%20Proposal; I think it'll be easy to do thus ticket when we do the changes in the blog post.

comment:2 Changed 5 years ago by igloo

Milestone: 7.8.1
Owner: set to simonpj

comment:3 Changed 4 years ago by monoidal

With

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

Milestone: 7.8.37.10.1

Moving to 7.10.1.

comment:5 Changed 3 years ago by thoughtpolice

Milestone: 7.10.17.12.1

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 2 years ago by ezyang

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

comment:7 Changed 2 years ago by thoughtpolice

Milestone: 7.12.18.0.1

Milestone renamed

comment:8 Changed 21 months ago by jstolarek

comment:9 Changed 19 months ago by thomie

Milestone: 8.0.1
Note: See TracTickets for help on using tickets.