Opened 9 years ago

Last modified 4 years ago

#5016 new feature request

Make Template Haskell: -ddump-splices generate executable code

Reported by: guest Owned by:
Priority: low Milestone:
Component: Template Haskell Version: 7.0.2
Keywords: dump-splices Cc:
Operating System: Linux Architecture: x86
Type of failure: Other Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description (last modified by simonpj)

http://hpaste.org/44711/testsunpackedhs

I am Aur Saraf, reachable sonoflilit @ don't-be-evil.

I wanted to edit some code that was autogenerated by TH with Michael Snoyman's Persistent library (version 0.3.1.3).

So I ran it through ghc --make -ddump-splices 2>&1 | less, and in a copy of my original file replaced the TH with the splices.

It wouldn't compile, so I googled like crazy and eventually asked for help on #haskell, where the kindly kmc helped me get it to compile.

We identified several inaccuracies in the pretty printer.

I had to do a few things, among them:

  • Delete some instances of the keyword "instance":
    02:12 < kmc> oh, i think the problem is simply that you don't use the kw "instance" for associated types
    02:12 < kmc> only for standalone family instances
    02:12 < kmc> sorry i didn't remember that earlier
    02:13 < kmc> so ghc / TH is being imprecise with how it outputs generated asstype instances
    [..]
    02:14 < kmc> well splices generate abstract syntax trees
    02:15 < kmc> and i guess the AST for a family instance inside or outside a class instance is the same
    02:15 < kmc> but the concrete syntax differs
    02:15 < kmc> and the pretty-printer for that AST is not taking that into account
    
  • Change many illegal identifiers:
    01:51 < kmc> sonoflilit, it looks like TH is generating names of the form x[a2ur]
    01:51 < kmc> which aren't valid Haskell identifiers
    01:52 < kmc> i don't know if that's related to your problem
    01:52 < kmc> but it seems to be a reason why this dumped splice won't work as-is
    
  • Remove {}s:
    02:23 < sonoflilit>     { entityDef _ = Database.Persist.Base.EntityDef
    02:24 < sonoflilit> TestsUnpacked.hs:47:4: parse error on input `{'
    02:25 < kmc> oh i think "entityDef _ = ..." starts off the methods part of the type class instance
    02:25 < kmc> so that brace should move to right after "instance PersistEntity Person where"
    02:25 < kmc> or probably, not exist
    
    (also, in other parts of the file)
  • Fix empty data declarations:
    02:24 < kmc> at line 137-139 you have three "data instance"s with no right-hand side
    02:26 < kmc> does "data Foo =" actually work
    02:26 < kmc> as an alternative to "data Foo"
    02:27 < sonoflilit> hmm, tested, no
    02:27 < sonoflilit> and you need a special extension to allow "data Foo"
    
    (I needed to {-# LANGUAGE EmptyDataDecls #-}, as well as remove the "=" from the lines)
  • Change some wacky syntax:
    02:33 < sonoflilit> kmc: what's wrong with my line 192's ->?
    02:33 < sonoflilit> It gives an error after I solved all the { errors
    02:36 < kmc> sonoflilit, not sure... try rewriting that pattern as ((x_a3pU,_):_)
    02:37 < kmc> that use of qualified infix *should* work
    02:37 < kmc> also it keeps using { } to override layout but then not inserting ;
    
    02:39 < kmc> oh "GHC.Types.[]"
    02:39 < kmc> probably isn't right
    02:40 < kmc> unqualify that mo'fo'
    
  • Add all sorts of imports:
    TestsUnpacked.hs:21:19:
        Not in scope: type constructor or class `GHC.Int.Int64'
    
    TestsUnpacked.hs:31:18:
        Not in scope:
          type constructor or class `Web.Routes.Quasi.Classes.SinglePiece'
    
    [..]
    
    TestsUnpacked.hs:186:20:
        Not in scope: data constructor `Database.Persist.Base.SqlString'
    
    TestsUnpacked.hs:187:26:
        Not in scope:
          data constructor `Database.Persist.Base.PersistString'
    
  • Copy code from a library because it was private:
    TestsUnpacked.hs:67:29: Not in scope: `Database.Persist.TH.apE'
    
  • Add parentheses to type constructor definitions:
    TestsUnpacked.hs:40:24:
        `Maybe' is not applied to enough type arguments
              PersonColorEq Maybe String |
    becomes
              PersonColorEq (Maybe String) |
    
  • Eventually I gave up at:
    TestsUnpacked.hs:61:4:
        The equation(s) for `toPersistFields' have four arguments,
        but its type `Person -> [SomePersistField]' has only one
        In the instance declaration for `PersistEntity Person'
    
    TestsUnpacked.hs:87:4:
        The equation(s) for `persistUpdateToValue' have two arguments,
        but its type `Update Person -> PersistValue' has only one
        In the instance declaration for `PersistEntity Person'
    

What I'd recommend the person who takes this bug is to follow my steps to reproduce and then again and again until the file compiles with as few manual changes as possible (perhaps the added language extension and imports are unavoidable, all else is clearly a bug).

Attachments (1)

Tests.hs (611 bytes) - added by guest 9 years ago.
working file that dump_splices replacing makes not work

Download all attachments as: .zip

Change History (11)

Changed 9 years ago by guest

Attachment: Tests.hs added

working file that dump_splices replacing makes not work

comment:1 Changed 9 years ago by simonpj

Description: modified (diff)

comment:2 Changed 9 years ago by simonpj

Summary: Template Haskell: -ddump-splices generates unexecutable codeMake Template Haskell: -ddump-splices generate executable code
Type: bugfeature request

Thanks for the report. I intended -ddump-splices as a debugging aid; it was never intended to print out compilable Haskell. It's not an unreasonable thing to want, but as you discovered someone would have to do quite a bit of fiddling to make it happen.

So I'll retitle as a feature request. Thanks for the specific list of changes needed.

comment:3 Changed 8 years ago by igloo

Milestone: 7.2.1

Given you can pretty-print splice results yourself if you want, is this really worthwhile? It would also prevent us from adding any extra annotations to the output (without another flag to turn the annotations off).

comment:4 Changed 8 years ago by igloo

Milestone: 7.2.17.4.1

comment:5 Changed 8 years ago by igloo

Milestone: 7.4.17.6.1
Priority: normallow

comment:6 Changed 7 years ago by igloo

Milestone: 7.6.17.6.2

comment:7 Changed 5 years ago by thoughtpolice

Milestone: 7.6.27.10.1

Moving to 7.10.1.

comment:8 Changed 5 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:9 Changed 4 years ago by thoughtpolice

Milestone: 7.12.18.0.1

Milestone renamed

comment:10 Changed 4 years ago by thomie

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