[Haskell-cafe] Template Haskell question

Eelco Lempsink eelco at lempsink.nl
Tue Jan 6 18:23:45 EST 2009


On 6 jan 2009, at 18:08, Jeff Heard wrote:
> Alright...  I *think* I'm nearly there, but I can't figure out how to
> derive a class instance using record accessors and updaters...  Can
> anyone help?  There are [| XXXf |] instances at the end of the module
> and they all need replaced, but I can't figure out what to replace
> them with.

...
> -- usage: $(deriveUIState ''MyTypeWithUIState)
> {-
> - Derive an instance of UIState from some type that has had UIState
> fields added to it.
> -}
> deriveUIState tp = do
>    return [InstanceD []
>                      (appUIState $ appType tp [])
>                      [FunD 'mousePosition              [|  
> mousePositionf |]
...
>                     ,FunD 'setMousePosition            [| \b a ->  
> a{ mousePositionf=b } |]
...

Quick guess: this doesn't typecheck?

FunD :: Name -> [Clause] -> Dec

while [| ... |] will return something of type ExpQ (which is the same  
as Q Exp).

You're indeed nearly there, but if you use the quotation brackets you  
need to write monadic code (for the Q monad) and use functions like  
clause and funD.  The tutorials on the wiki (you've probably seen  
them, http://www.haskell.org/haskellwiki/Template_Haskell) or pretty  
good and you could also look at packages at hackage for inspiration/ 
examples, e.g. http://hackage.haskell.org/packages/archive/haxr-th/3000.0.0/doc/html/src/Network-XmlRpc-THDeriveXmlRpcType.html

-- 
Regards,

Eelco Lempsink

-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 194 bytes
Desc: This is a digitally signed message part
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20090107/089b99c4/PGP.bin


More information about the Haskell-Cafe mailing list