Generic deriving in GHC
Simon Peyton-Jones
simonpj at microsoft.com
Thu Oct 28 07:26:56 EDT 2010
OK good. I got it built. Then I compiled
data D = D
with -XGenerics -ddump-deriv -dppr-debug
The "-dppr-debug" flag makes the uniques show up.
Then you can see that the two from0 occurrences have different uniques, hence the error.
The problem was that you'd assigned a unique to fromOName, namely from0ClassOpKey, but you hadn't added from0 to the list of basicKnownKeyNames in PrelNames.
Have a look here http://hackage.haskell.org/trac/ghc/wiki/Commentary/Compiler/WiredIn
In fact you don't need to give a Key to from0. Instead, just use a qualifed RdrName Generics.from0. You want an "Orig" RdrName (see http://hackage.haskell.org/trac/ghc/wiki/Commentary/Compiler/RdrNameType). You build one of those with varQual_RDR; there are lots of other examples in PrelNames.
I've made a patch and have pushed it, so you can just pull.
I have started a wiki page in the Commentary http://hackage.haskell.org/trac/ghc/wiki/Commentary/Compiler/GenericDeriving
Can you fill in brief details, summarising where the main code lives? Details belong in the code itself; this overview is just to orient someone, and to help us to work together. Eg you can record open problems, or to-do lists.
Simon
From: José Pedro Magalhães [mailto:jpm at cs.uu.nl]
Sent: 27 October 2010 13:39
To: Simon Peyton-Jones
Cc: cvs-ghc at haskell.org
Subject: Re: Generic deriving in GHC
Ok, I think I now succeeded in pushing my patches to darcs.haskell.org<http://darcs.haskell.org>, along with all the patches pushed so far to the repos we've cloned (ghc, ghc-prim and base).
Cheers,
Pedro
2010/10/25 José Pedro Magalhães <jpm at cs.uu.nl<mailto:jpm at cs.uu.nl>>
Hi,
Currently I have all the generics stuff in GHC.Generics in ghc-prim, but I removed its export from GHC.Base, so no name clashes occur anymore.
For my implementation I'm mostly following what's already there for the current generics stuff. So, previously, types/Generics.lhs took care of generating the binders for generic representations. I'm currently generating the representations there, but I don't want them to be just binders: I want them to be instances.
Previously, a type data D = D triggered the generation of binders
$gfromD = ...
$gtoD = ...
Now, what I actually want is to generate an instance of the form
instance Representable0 D where
type instance Rep D = ...
from0 D = ...
to0 (...) = D
(Whether the final implementation will use associated types or MPTCs is still open for debate.) The stuff generated in types/Generics.lhs is used in typecheck/TcDeriv.lhs. Previously, things like $gfromD were just returned as extra top-level bindings, together with generated instance declarations. Now, we no longer need top-level bindings (I think), but we do need to return extra instances. These are the representable instances, and will be generated even if there are no deriving clauses in the program. (When there are deriving clauses for generic classes, I think this is where a generic instance will be added too, but I'm not at that stage yet.)
So far so good, and I managed to add some form of Representable0 instances (function genGenericRepBinds in TcDeriv.lhs). Problem is, when compiling a file with -XGenerics (to enable the generation of generic representations), I still get complaints of lacking "explicit or default" methods, even though those are generated:
==================== Derived instances ====================
InstInfo: GHC.Generics.Representable0
Main.A
(GHC.Generics.D1
GHC.Generics.NoSelector
(GHC.Generics.C1 GHC.Generics.NoSelector GHC.Generics.V1)
t_a)
{ GHC.Generics.from0 _
= GHC.Generics.M1
(GHC.Err.error
"No generic representation for empty datatype Main.A")
GHC.Generics.to0 (GHC.Generics.M1 _)
= GHC.Err.error "No values for empty datatype Main.A" }
test/Main.hs:1:1:
Warning: No explicit method nor default method for `GHC.Generics.from0'
In the instance declaration for `GHC.Generics.Representable0
A
(GHC.Generics.D1
GHC.Generics.NoSelector
(GHC.Generics.C1 GHC.Generics.NoSelector GHC.Generics.V1)
t_a)'
test/Main.hs:1:1:
Warning: No explicit method nor default method for `GHC.Generics.to0'
In the instance declaration for `GHC.Generics.Representable0
A
(GHC.Generics.D1
GHC.Generics.NoSelector
(GHC.Generics.C1 GHC.Generics.NoSelector GHC.Generics.V1)
t_a)'
Also, if I have more than one datatype, I start getting
/tmp/ghc28844_0/ghc28844_0.s: Assembler messages:
/tmp/ghc28844_0/ghc28844_0.s:405:0:
Error: symbol `ghczmprim_GHCziGenerics_Representable0_closure' is already defined
So I'm clearly doing something wrong when generating my Representable0 instances...
Also, for the meta-information I will need to generate (empty) datatypes, which should not be visible for the user. How can I do this? Currently I see how to generate bindings for values and functions, but now how to declare new types. Would TcDeriv.lhs be the appropriate place for this? Note that these auxiliary types do not need generic representations.
I do not have an account on darcs.haskell.org<http://darcs.haskell.org> yet, so I'm attaching the patches with the changes I've done so far.
Thanks,
Pedro
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/cvs-ghc/attachments/20101028/acad1ea4/attachment-0001.html
More information about the Cvs-ghc
mailing list