[Haskell-cafe] Template Haskell question

Jeff Heard jefferson.r.heard at gmail.com
Wed Jan 7 15:58:30 EST 2009


It doesn't typecheck, no, but it also doesn't check out in scope.  It
complains in

[FunD 'mousePosition [| mousePositionf |] ...

that mousePositionf isn't in scope.

What I believe I need to do is use mkName "mousePositionf", but how do
I bind the record getter "mousePositionf" that is defined by the code
in the function named  "additions" to mousePosition -- a.k.a. how do I
write that template?  Is it a ConE?  And how do I encode

a{ mousePositionf = b }

in template haskell without using the [| |] syntax, so that I can use mkName?

-- Jeff

On Tue, Jan 6, 2009 at 6:23 PM, Eelco Lempsink <eelco at lempsink.nl> wrote:
> 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
>
>


More information about the Haskell-Cafe mailing list