[Haskell-cafe] Automatic derivation (TemplateHaskell?)

Stefan O'Rear stefanor at cox.net
Wed Apr 4 19:48:56 EDT 2007


On Thu, Apr 05, 2007 at 12:14:52AM +0100, Joel Reymont wrote:
> Folks,
> 
> I have very uniform Parsec code like this and I'm wondering if I can  
> derive it using TemplateHaskell or DrIFT or some other tool. Any ideas?
> 
> Note that
> 
> 1) The reserved word matches the constructor
> 
> 2) No arguments equals no parens
> 
> 3) More than one argument is separated with a comma
> 
> 4) For every invocation of numExpr, strExpr or boolExpr, the type of  
> the constructor argument is NumExpr, StrExpr and BoolExpr respectively.
> 
> This is just a handful of functions and I have to tackle about 100  
> more, thus my asking :-).
> 
> 	Thanks, Joel

Data.Derive can do this.  In an attempt to avoid munging the relevent
files they are attached. 


stefan at stefans:/tmp$ ghci -fth -v0 -i/usr/local/src/derive -e '$( _derive_print_instance makeJoelR '"''"'Foo )' Sample.hs
instance JoelR Main.Foo
    where parse = choice [(>>) (reserved ['A']) ((>>) (char '(') ((>>=) parse (\a0 -> (>>) (char ')') (return (Main.A a1))))),
                          (>>) (reserved ['B']) ((>>) (char '(') ((>>=) parse (\a0 -> (>>) (char ',') ((>>=) parse (\a1 -> (>>)
(char ')') (return (Main.B a1 a2))))))),
                          (>>) (reserved ['C']) (return Main.C)]

Not pretty code, but it will work.  (Future plans include adding a
prefix -> infix translator to the optimizer.)

http://www.cs.york.ac.uk/fp/darcs/derive

Stefan
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Sample.hs
Type: text/x-haskell
Size: 283 bytes
Desc: not available
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20070404/52dd32dc/Sample.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: JoelR.hs
Type: text/x-haskell
Size: 800 bytes
Desc: not available
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20070404/52dd32dc/JoelR.bin


More information about the Haskell-Cafe mailing list