Make Template Haskell: -ddump-splices generate executable code
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 (closed) | 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).