[Template-haskell] Reification

Simon Peyton-Jones simonpj at microsoft.com
Wed Nov 5 17:16:41 EST 2003


I'm on the TH warpath, and I'm working on reification.  The idea is that
'reify' is not a language construct any more: it's just an ordinary

	reify :: Var -> Q Dec

This is much better that what we have now, because when you walk over
the Dec you may find more Vars, and now you can reify them in turn.
Which you absolutely cannot do today.

To invoke reify in the first place, you need a Var, and  you can get one
using the new quote syntax. e.g.

	reify 'map

will give you the declaration for map.   In general 'map is very like [|
map |], except you get a Var instead of a Q Exp.   So (a) it's not
monadic, which is jolly convenient, and (b) it gives you the Var v
directly rather than a (VarE v), which is also convenient.  But it has
all the same staging rules as [| ... |].

Question 1
You probably want more info than just the declaration.  For functions
you'd like to know the type and fixity... so we could instead have

	reify :: Var -> Q Info

data Info 
  = Class Decl
  | TyCon Decl
  | Var Type Decl Fixity 
  | DataCon Type Var {- parent tycon -} Fixity
  | ClassOp Type Var {- parent class -} Fixity
  | Unknown

Would that be a better plan?  One could have reifyFixity, reifyType,
etc, to get the anciliary bits, but it seems nicer to get the whole lot
in one go.  

It's a bit unclean that given (Class d) it'd be certain that d was of
the form ClassD.  

Any better ideas?

Question 2
The type checker may not know the full type of the thing.  For example:

	f x = $( do { i <- reify 'x; ... } )  .....

The type of 'x' isn't known yet.  It may get refined by the other "..."
parts of f's body, or by f's use.  

So should we give up and refuse to give you f's type?  Or what?

NB This can apply to top-level decls too:
	f = 1+2
	$( ... reify 'f... )
	g = f + (4::Int)

Because of the monomorphism restriction, the type of f is fixed by f's
caller, which may be "after" the reification.

I'm really not sure what to do here. 


PS: I've implemented most of this stuff, but I'm holding off committing
for fear of annoying those of you using current TH.  But I'd like to
commit soon.  Perhaps anyone wanting a stable TH can use the 6.2 branch?
Would that do?

More information about the template-haskell mailing list