[Template-haskell] types.

Sean Seefried sseefried at cse.unsw.edu.au
Sun Feb 20 08:16:31 EST 2005


On 20/02/2005, at 1:49 AM, Keean Schupke wrote:

> Ahh, I am doing a form of source to source translation for Haskell. I 
> don't want to maintain the parser for this so TH seemed to be ideal.
>
> I would be happy with the restriction of no splices within the 
> meta-quotes.
>
> If TH is already type checking (it is, as it will catch type errors in 
> the meta-quoted code) then surely this type info is available, it just 
> needs to be reified?
>

Check out this code from fptools/ghc/compiler/typecheck/TcSplice.lhs 
The comment mid-way is quite interesting.

tcBracket :: HsBracket Name -> Expected TcType -> TcM (LHsExpr Id)
tcBracket brack res_ty
   = getStage 				`thenM` \ level ->
     case bracketOK level of {
	Nothing         -> failWithTc (illegalBracket level) ;
	Just next_level ->

    	-- Typecheck expr to make sure it is valid,
	-- but throw away the results.  We'll type check
	-- it again when we actually use it.
     recordThUse				`thenM_`
     newMutVar []	 		`thenM` \ pending_splices ->
     getLIEVar				`thenM` \ lie_var ->

     setStage (Brack next_level pending_splices lie_var) (
	getLIE (tc_bracket brack)
     )					`thenM` \ (meta_ty, lie) ->
     tcSimplifyBracket lie 		`thenM_`

	-- Make the expected type have the right shape
     zapExpectedTo res_ty meta_ty	`thenM_`

	-- Return the original expression, not the type-decorated one
     readMutVar pending_splices		`thenM` \ pendings ->
     returnM (noLoc (HsBracketOut brack pendings))
     }


I found that reifying from another module was almost what I wanted but 
I wanted to be able to get type information for *all* variables at the 
very least, and preferably everything. But I remember that I couldn't. 
I think it might have been the case that you can only get the type of 
things actually present in .hi files, which I think means just top 
level functions.  Anyone else know about this?


> Who maintains that part of the TH code in GHC?
>

Ian Lynagh would probably have some familiarity with it. As for 
maintaining it - it's probably Simon Peyton Jones.

Cheers,

Sean



More information about the template-haskell mailing list