[Haskell-cafe] types for parsing a tree

Stephen Tetley stephen.tetley at gmail.com
Fri Sep 10 13:51:12 EDT 2010


On 10 September 2010 17:53, Jared Jennings <jjenning at gmail.com> wrote:

> I've tried
>
>    data OFXThing = OFX { statement :: OFXThing } | StatementResponse
> { ... transactions :: [OFXThing] }
>
> but that would let me make trees of things that make no sense in OFX,
> like a transaction containing a statement.


Using alternative constructors (i.e. sum types) is the right approach
to get different "datatypes" in the tree. However you aren't
respecting the nesting of the tree here - "OFX" is a level higher in
the tree than a statement response and the OFX constructor recurs on
itself which looks suspect.

I suspect OFX is pathologically huge format and isn't a good starting
point for designing syntax trees (the downloadable Spec seemed to be
several megabytes zipped). If the DTD is very large you might want to
use the untyped tree to extract parts of interest and convert after
parsing to a smaller typed tree (with only the syntax you are
interested in).

By the way, HaXML has has a tool called DTD2HS (I think) that will
generate Haskell datatypes from a DTD definition.

Best wishes

Stephen


More information about the Haskell-Cafe mailing list