[Haskell-cafe] Automatic derivation (TemplateHaskell?)

Joel Reymont joelr1 at gmail.com
Thu Apr 5 09:32:55 EDT 2007


With derive compiled and installed I thought I would change the code  
a bit and try it...

ghci -fth -v0 -e '$( _derive_print_instance makeFunParser  
'"''"'Foo )' baz.hs

baz.hs:30:3: Not in scope: `a1'

Any help is appreciated!

	Thanks, Joel

---

FunParser.hs:

module FunParser where

import Data.Derive
import Data.Derive.Peephole
import Data.List

import Text.ParserCombinators.Parsec ( CharParser )

makeFunParser = Derivation drv "FunParser"

drv dat@(DataDef name arity ctors) =
     simple_instance "FunParser" dat [funN "parse" [ sclause [] body ] ]
     where
       body = l1 "choice" $ lst [ clause con | con <- ctors ]
       clause con = l1 "reserved" (lit (trim (ctorName con)))
                    >>: args con (ctorArity con)
       trim = reverse . takeWhile (/= '.') . reverse
       args ct 0 = return' (ctp ct 'a')
       args ct k = l1 "char" (lit '(') >>: args' ct k 0
       args'  ct remn seen = l0 "parse"
                             >>=: (('a' : show seen)
                                   ->: args'' ct (remn-1) (seen+1))
       args'' ct 0 seen = l1 "char" (lit ')') >>: return' (ctp ct 'a')
       args'' ct k seen = l1 "char" (lit ',') >>: args' ct k seen

class FunParser a
     where parse :: CharParser s a


baz.hs:

import Text.ParserCombinators.Parsec hiding ( parse )
import qualified Text.ParserCombinators.Parsec.Token as T
import Text.ParserCombinators.Parsec.Language( emptyDef )
import Data.Derive.TH
import FunParser

data NumExpr
     = Int Integer
     | Num Double

instance FunParser NumExpr where
     parse = numExpr

data Foo
     = Foo NumExpr

lexer = T.makeTokenParser emptyDef

identifier = T.identifier lexer
reserved = T.reserved lexer
integer = T.integer lexer
float = T.float lexer

numExpr :: GenParser Char a NumExpr
numExpr =
     choice [ integer >>= return . Int
            , float >>= return . Num
            ]

$( derive makeFunParser ''Foo )


--
http://wagerlabs.com/







More information about the Haskell-Cafe mailing list