Foldable for BNFC generated tree

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Foldable for BNFC generated tree

Deniz Dogan-3
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
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Foldable for BNFC generated tree

Martijn van Steenbergen-2
Hi Deniz,

Deniz Dogan wrote:
> 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.

What exactly do you mean by folding functionality? Folding as in the
Foldable type class applies to containers, which your data type isn't.
Perhaps you're looking for generic programming?

There are several good GP libraries out there:

* EMGM: http://www.cs.uu.nl/wiki/GenericProgramming/EMGM
* Uniplate: http://community.haskell.org/~ndm/uniplate/
* SYB: http://www.cs.vu.nl/boilerplate/

See also Neil Mitchell's blog for some examples:
http://neilmitchell.blogspot.com/2009/03/concise-generic-queries.html

HTH,

Martijn.
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Foldable for BNFC generated tree

Deniz Dogan-3
2009/5/4 Martijn van Steenbergen <[hidden email]>:

> Hi Deniz,
>
> Deniz Dogan wrote:
>>
>> 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.
>
> What exactly do you mean by folding functionality? Folding as in the
> Foldable type class applies to containers, which your data type isn't.
> Perhaps you're looking for generic programming?
>
> There are several good GP libraries out there:
>
> * EMGM: http://www.cs.uu.nl/wiki/GenericProgramming/EMGM
> * Uniplate: http://community.haskell.org/~ndm/uniplate/
> * SYB: http://www.cs.vu.nl/boilerplate/
>
> See also Neil Mitchell's blog for some examples:
> http://neilmitchell.blogspot.com/2009/03/concise-generic-queries.html

You're right, what I was asking for didn't make much sense... I was
really looking for GP.

Thanks,
Deniz
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Foldable for BNFC generated tree

Johan Jeuring
Hi Deniz,

>> Deniz Dogan wrote:
>>>
>>> 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.
>>
>> What exactly do you mean by folding functionality? Folding as in the
>> Foldable type class applies to containers, which your data type  
>> isn't.
>> Perhaps you're looking for generic programming?
>>
>> There are several good GP libraries out there:
>>
>> * EMGM: http://www.cs.uu.nl/wiki/GenericProgramming/EMGM
>> * Uniplate: http://community.haskell.org/~ndm/uniplate/
>> * SYB: http://www.cs.vu.nl/boilerplate/
>>
>> See also Neil Mitchell's blog for some examples:
>> http://neilmitchell.blogspot.com/2009/03/concise-generic-queries.html
>
> You're right, what I was asking for didn't make much sense... I was
> really looking for GP.

If I interpret your question correctly, you want a fold for a set of
mutually recursive datatypes without type parameters. This function is
available in the generic programming library multirec:

http://hackage.haskell.org/cgi-bin/hackage-scripts/package/multirec

Kind reagrds,

Johan Jeuring


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe