Changes between Version 1 and Version 2 of TemplateHaskell/Annotations


Ignore:
Timestamp:
Oct 9, 2013 5:13:15 PM (7 months ago)
Author:
errge
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • TemplateHaskell/Annotations

    v1 v2  
    6969The FlagData (https://github.com/errge/hflags/blob/master/HFlags.hs#L129) datatype contains all the information we need to know about a flag.  Then for every flag we create a new fake datatype that implements the Flag class (https://github.com/errge/hflags/blob/master/HFlags.hs#L149).  In `initHFlags` we simply call TemplateHaskell reify on the Flag class.  This gives us our "fake" instances and their `getFlagData` method can be used to query the needed flag data for `--help` generation, parsing, etc.  This can be seen in at https://github.com/errge/hflags/blob/master/HFlags.hs#L397. 
    7070 
     71This is obviously not nice, we are abusing the reification of types and instances to send messages to ourselves between modules.  There should be an explicit way to do that.  This is requested in #7867. 
    7172 
     73The proposal is to make it possible to generate annotations from template haskell (when defining a flag) and read them all back via template haskell (in initHFlags).  These module level annotations (in HFlags case) will then contain the info that is needed for flag parsing and `--help` generation. 
     74 
     75== Aside: the current implementation is not just ugly, but not working with current GHC == 
     76 
     77Unfortunately, the current idea is not really working all that nice, because of #8426. 
     78 
     79Haskell98 and Haskell prime says that all the instances should be visible that are under the current module in the dependency tree, but this is not the case currently in GHC when using one-shot compilation (`ghc -c`, not `ghc --make`).  This is a optimization, so we can save reading of all the module dependencies transitively.  GHC tries to cleverly figure out where to find so called orphan instances. 
     80 
     81TemplateHaskell is a corner-case, where this orphan logic is not clever enough and therefore reify doesn't see some of the instances that are under the current module in the dependency tree.  Therefore HFlags can't gather all the flags in `$initHFlags`.  I propose to fix this by loading all the imported interfaces transitively when reifying classes or type families.  **If you have any comments or questions regarding this, please comment on #8426.** 
     82 
     83An alternative approach would be to mark all the modules that define command line flags as orphan modules.  Then they are automatically read up with the current one-shot and batch compiler and seen in reification.  To do this it has to be possible to mark modules as orphans, as proposed by #8337 or agree on a new orphan rule: e.g. every module that contains module level annotations are orphan.  If we agree on the latter, then the patch in #8337 has to be sightly modified to work like that.  **Please comment here or on #8337 if you prefer either of the two solutions or you object both.** 
     84 
     85= Implementation status, options, questions = 
     86 
     87== Already done: a bugfix, and annotation generation == 
     88The already merged #3725 and #8340 makes it possible to generate Annotations from TH.  We support all three kind of annotations: annotations on types, values and whole modules. 
     89 
     90== In the works, but seems easy: annotation reification == 
     91#8397 contains the code for reification of annotations.  The patch is quite straightforward and quite separated from all of the other TODOs, it just adds a new Q monad statement for handling the reification request and the necessary handling of this Q monad statement in TcSplice.  **I consider this patch ready to review, so please comment.** 
     92 
     93== Controversial: #8398, module list reification == 
     94#8397 alone is very useful, but unfortunately not enough.  Because it only makes it possible to get annotations for modules, values or types when you already know your target.  But in the case of `$initHFlags` we want to get all the flags that has been defined somewhere below us in our import tree.  To do this, #8398 implements module listing.  Once we get back the module list, we can use the returned names to get the module annotations for all the modules and extract the flag data. 
     95 
     96The current patch is a bit controversial, it simply uses the currently loaded list of modules as a substitute for "module list of our dependencies".  This approximation is heavily used currently everywhere in GHC, but it can cause problems, as showed in #8426.  SPJ already stated (http://ghc.haskell.org/trac/ghc/ticket/7867#comment:12) that he doesn't agree to this approximation and we should work on something that is well defined.  I agree. 
     97 
     98I propose to change this patch to properly construct the list of dependent modules and return that.  We would return (pkgid, moduleid) pairs for every module that is imported by us or imported by our imports transitively. 
     99 
     100**Here for sure we need design discussion, so please think about this and share your thoughts.**  This is not to say that design discussion is not welcome for the other patches, of course it is!