[Template-haskell] splicing types

Robert Greayer robgreayer at yahoo.com
Tue Jul 1 12:03:10 EDT 2008


--- On Tue, 7/1/08, Simon Peyton-Jones <simonpj at microsoft.com> wrote:

> 
> You mention a special code generator -- who writes that?

Anyone writing a library which generates code by reifying/analysing user-supplied code (e.g. Neil Mitchell's derive, http://www-users.cs.york.ac.uk/~ndm/derive/) could potentially make use of programmer supplied meta-data associated with the (library user's) code.  So there's any number of special code generators which could make use of some annotation facility.  Essentially, the author of the code generator specifies the annotations it understands (e.g. in my formulation, 'type Omitted defval = ()') and is otherwise a client of the annotation facility (which, in my formulation, means simply using 'decT' to decode values associated with annotations).  So you have (in my formulation of annotations) an annotation support library which contains encT and decT and probably 'type Ann a b = a', and everything else is just a use case for annotations.  I hope this answers your question.

(My formulation, aside from not working, has the disadvantage of obfuscating declarations; a 'proper' annotations feature, such as the GHC plugins/annotation project might not have this problem.  Also the burden of type checking falls squarely on the author of the code generator.  'Omitted $(encT "hello")' would be perfectly valid at the splice point.  Also not a problem for a 'real' annotations feature, one would think.)

> Could you instead write a declaration splice?
>         $(sigEncode "val2" 100)
> where sigEncode generates a suitable declaration?

Not quite; I elided the surrounding context for brevity:
> data Bar {
>         val1 :: String,
>         val2 :: Ann Int (Omitted $(encT 1000))
>     }

I could generate the entire declaration for Bar (this, I think, is the approach Marc Weber is taking, as he points out in his response to my original message) with a declaration splice, but not the just portion of the declaration that I wanted to annotate, the declaration of the field 'val2', i.e.

> data Bar {
>     val1 :: String,
>     $(sigEncode "val2" 1000) -- still an invalid splice position
> }
----

> 2)  I don't think this is insoluble at all, but someone
> needs to work it all out and implement it.  I have postponed
> doing this until sufficient user pressure arises!  It's
> just a bandwidth problem -- I am totally snowed under at
> the moment.  Would anyone care to help?

Well, I'll certainly spelunk into the GHC to see how this might work.  Whether this will eventually translate into anything resembling 'help' is another question.

Thanks to you, and Claus, for the pointers!

Regards,
rcg


      


More information about the template-haskell mailing list