[Haskell-cafe] Automatic derivation (TemplateHaskell?)

Stefan O'Rear stefanor at cox.net
Thu Apr 5 10:32:07 EDT 2007

On Thu, Apr 05, 2007 at 02:47:21PM +0100, Joel Reymont wrote:
> Here's the output from -ddump-splices (thanks Saizan for the tip).
> It's returning a1 instead of a0.
> ghci -fth -e '$( _derive_print_instance makeFunParser '"''"'Foo )'  
> baz.hs -ddump-splices
> baz.hs:1:0:
>     baz.hs:1:0: Splicing declarations
>         derive makeFunParser 'Foo
>       ======>
>         baz.hs:30:3-28
>         instance {FunParser Main.Foo} where
>             []
>             { parse = choice
>                         [(>>)
>                            (reserved ['F', 'o', 'o'])
>                            ((>>)
>                               (char '(') ((>>=) parse (\ a0 -> (>>)  
> (char ')') (return (Main.Foo a1)))))] }
> baz.hs:30:3: Not in scope: `a1'

Sorry for the late multiple reply, I just spent seven hours sleeping...

I am not the maintainer of Data.Derive, nor did I write the majority
of the nice code; Neil Mitchell did it, you can ask him "why replace
DrIFT".  However, using abstract syntax trees WAS my idea. 

First, _derive_print_instance will never give you a TH splice error,
since it always evaluates to an empty list of declarations.  It uses
the TH 'runIO' facility such that type-checking a file using
_derive_print_instance will emit the instances to standard output as a
side effect.  So the error is coming from the $(derive) in baz.hs, if
you have more errors try commenting it out. (you'll get bogus code on
stdout, but at least it will be completly haskell!)

_derive_print_instance was not intended to be a debugging aid,
although it certainly works well in that capacity.  The intent is that
it will be used when the standalone driver is rewritten to use TH,
which I intend to do not long after I can (Neil is out of
communication for a week with intent to continue hacking Derive; I'm
taking this as a repository lock).

Yes, we do use type classes to implement recursion across types.  This
seems to be a very standard idiom in Haskell, used by Show, Read, Eq,
Ord, NFData, Arbitrary, and doubtless many more. 

More information about the Haskell-Cafe mailing list