[GHC] #14296: Add `Lift Exp` instance to `Language.Haskell.TH.Syntax`

GHC ghc-devs at haskell.org
Fri Sep 29 13:12:01 UTC 2017


#14296: Add `Lift Exp` instance to `Language.Haskell.TH.Syntax`
-------------------------------------+-------------------------------------
        Reporter:  heisenbug         |                Owner:  (none)
            Type:  feature request   |               Status:  new
        Priority:  normal            |            Milestone:
       Component:  Template Haskell  |              Version:  8.2.1
      Resolution:                    |             Keywords:
Operating System:  Unknown/Multiple  |         Architecture:
 Type of failure:  Poor/confusing    |  Unknown/Multiple
  error message                      |            Test Case:
      Blocked By:                    |             Blocking:
 Related Tickets:  #14030            |  Differential Rev(s):
       Wiki Page:                    |
-------------------------------------+-------------------------------------

Comment (by RyanGlScott):

 Let me try and articulate more precisely what this invariant is capturing.
 Here are some examples of the invariant at work:

 {{{#!hs
 {-# LANGUAGE TemplateHaskell #-}

 import Language.Haskell.TH.Syntax

 test :: IO ()
 test = mapM_ print
   [ $(lift True)     == True
   , $(lift "hello")  == "hello"
   ]
 }}}

 These should all print `True`, and more generally, you should be able to
 stick in any other `Bool` or `String` and also have it print all `True`s.
 But it's easy to subvert this with your proposed `Lift Exp` instance,
 since you can have, e.g.:

 {{{#!hs
 uhOh :: Exp
 uhOh = lift "uhOh"
 }}}

 Now `$(lift uhOh)` won't be an `Exp`, but rather a `String`. This is not
 at all what we want.

 Replying to [comment:3 heisenbug]:
 > What about the derivation mechanism for `Lift` instances? Will it do
 ''the right thing'' in this case too?

 Right. Here is an except of the `-ddump-deriv` output from deriving a
 `Lift` instance for `Exp`:

 {{{#!hs
 deriving instance Lift Exp

 ===>

 instance Lift Exp where
   lift (VarE a) = conE 'VarE `appE` lift a
   lift (ConE a) = conE 'ConE `appE` lift a
   lift (AppE f x) = conE 'AppE `appE` lift f `appE` lift x
   ...
 }}}

 Now calling `lift` on an `Exp` will produce an `ExpQ` that represents an
 `Exp` value. It's quite meta, granted, but that's how it should be :)

 In any case, I've been meaning to resolve #14030 for a while now, but
 haven't had much time to look into it recently. One obstacle is that
 deriving `Lift` instances for data types in `template-haskell` during
 stage-1 compilation is harder than it sounds because GHC HEAD moved around
 the locations of functions that are used when deriving `Lift` instances
 themselves. Therefore, I think I'll have to disable generating `Lift`
 instances on stage-1 compilers for now to make it work, but I've yet to
 figure out how to accomplish that.

-- 
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/14296#comment:4>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler


More information about the ghc-tickets mailing list