[Haskell-cafe] Foldable for BNFC generated tree

Deniz Dogan deniz.a.m.dogan at gmail.com
Sun May 3 14:47:27 EDT 2009


Hi

I have a bunch of data types which are used to represent a JavaScript
program. The data types and a lexer and a parser have all been
generated using BNFC.  So basically an entire JavaScript program is
represented as a tree using these data types.  Ideally I'd like to be
able to "fold" over this data structure, but I can't instantiate
Foldable for my data types, since the data types all have kind *, if
I'm not completely lost.

Here's an example data type:

data Statement =
   StmtFunDecl JIdent [JIdent] ExprOrBlock
 | StmtVarDecl [VarDecl]
 | StmtLetDecl [VarDecl]
 | StmtWhile Expr Statement
 | ...

So, basically I'd like some sort of folding functionality for these
data types, without having to hack the lexer/parser myself
(parameterising the data types), because as I said they're being
generated by BNFC. I noticed that you can make BNFC generate GADTs
instead of normal ADTs, which would allow me to instantiate Foldable,
but I'm not entirely sure that this is the best way to do this.

Any help is appreciated,
Deniz Dogan


More information about the Haskell-Cafe mailing list