module PTree where import Text.PrettyPrint.HughesPJ data P sym = PSucceed | PToken [sym] | PApply (P sym) | PSeq (P sym) (P sym) | PLabel String (P sym) | PUnit (P sym) | PChain (P sym) deriving (Eq, Show, Read) type PAlgebra s r = (r, [s] -> r, r -> r, r -> r -> r, String -> r -> r, r -> r, r -> r) foldP :: PAlgebra s r -> P s -> r foldP (suc, tok, app, seq, lab, unit, chn) = f where f PSucceed = suc f (PToken ss) = tok ss f (PApply p) = app (f p) f (PSeq p q) = seq (f p) (f q) f (PUnit p) = unit (f p) f (PChain p) = chn (f p) extractSource :: P s -> [s] extractSource = foldP ([], id, id, (++), flip const, id, id) ppp :: P Char -> Doc ppp PSucceed = text "PSucceed" ppp (PToken xs) = text (show xs) ppp (PApply p) = text "<$>" $$ indent (ppp p) ppp (PSeq p q) = text "<*>" $$ indent (ppp p) $$ indent (ppp q) ppp (PLabel n p) = text n $$ indent (ppp p) indent = nest 2