
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@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