Changes between Version 2 and Version 3 of TemplateHaskell/Annotations


Ignore:
Timestamp:
Oct 9, 2013 5:22:25 PM (17 months ago)
Author:
errge
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • TemplateHaskell/Annotations

    v2 v3  
    11= Problem statement = 
    2  
    32Currently annotations are only usable from the GHC API.  Our goal is to add support for TemplateHaskell too.  Both creation of annotations, reification of annotations and gathering module annotations transitively from our whole import tree should be added. 
    43 
     4 
     5 
    56= Motivation = 
    6  
    77As a main use-case we will consider the http://hackage.haskell.org/package/hflags library. 
    88 
    99The goal of the library is to: 
    1010  - provide easy to use command line flags, 
    11   - that are pure from the programmers point of view (they don't change while running the program once anyways), 
     11  - that are pure from the programmers point of view (they don't change during one program run), 
    1212  - that can be defined anywhere, even in a module, 
    1313  - and the used flags are automatically gathered in the main program for parsing and `--help` generation. 
     
    5353}}} 
    5454 
    55 What's important to note here is that in the main program we didn't have to specify the list of modules that has to be searched for command line flags, the template haskell `$initHFlags` function automagically found them all.  Even if they're not directly, only indirectly imported by our main program.  A motivating example for that: 
    56   - the TCP module has a connect method that accepts a command-line tcp_connect_timeout flag (so the user can change that to 5 seconds from the usual 10 hours :)), 
     55What's important to note here is that in the main program we didn't have to specify the list of modules that has to be searched for command line flags, the template haskell `$initHFlags` function automatically found them all.  Even if they are not imported directly, but only indirectly by our main program.  A motivating example for that: 
     56  - the TCP module has a connect method that accepts a command-line `tcp_connect_timeout` flag (so the user can change that to 5 seconds from the usual 10 hours :)), 
    5757  - the HTTP module of course depends on TCP, 
    5858  - the WGet module depends on HTTP, 
    5959  - the main program uses WGet to download something from the internet. 
    6060 
    61 In this case HFlags automatically makes it so that the tcp_connect_timeout flag is show in --help of the main program and can be changed by the user to any value she sees fits.  This is achieved via TemplateHaskell, but in exchange the programmer doesn't have to explicitly pass around any kind of values or applicative stuff for every imported module that uses command line flags. 
     61In this case HFlags automatically makes it so that the `tcp_connect_timeout` flag is show in `--help` of the main program and can be changed by the user to any value she sees fit.  This is achieved via template haskell, but in exchange the programmer doesn't have to explicitly pass around any kind of values or applicative stuff for every imported module that uses command line flags. 
    6262 
    63 Of course, this whole approach can be debated and maybe we should instead explicitly pass parameters to functions, but let's leave that debate to the getopt authors and focus on TH on this page. 
     63Of course, this whole approach can be debated and maybe we should instead explicitly pass parameters to functions; but let's leave that debate to the getopt authors and focus on TH on this page. 
     64 
     65 
    6466 
    6567= Current implementation with typeclassses = 
     68How is this implemented in HFlags currently?  By using typeclasses. 
    6669 
    67 How the magic works inside HFlags currently?  By using typeclasses. 
    68  
    69 The 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. 
     70The FlagData (https://github.com/errge/hflags/blob/v0.4/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/v0.4/HFlags.hs#L149).  In `initHFlags` we simply call template haskell 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/v0.4/HFlags.hs#L397. 
    7071 
    7172This 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. 
     
    7374The 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. 
    7475 
     76 
     77 
    7578== Aside: the current implementation is not just ugly, but not working with current GHC == 
    76  
    7779Unfortunately, the current idea is not really working all that nice, because of #8426. 
    7880 
    7981Haskell98 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. 
    8082 
    81 TemplateHaskell 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.** 
     83Template haskell 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.** 
    8284 
    8385An 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.** 
     86 
     87 
    8488 
    8589= Implementation status, options, questions = 
     
    8993 
    9094== 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.** 
     95#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.** 
    9296 
    9397== Controversial: #8398, module list reification ==