[Haskell-cafe] Design question

Alexander Solla ajs at 2piix.com
Mon Dec 28 23:56:48 EST 2009

> There are many SVG elements, of which only a few are valid as the  
> content of each other SVG elements.
> SvgDocumentElement defines the allowed subset for the SVG document.
> I want to generate a "DList Char" for all those sub-elements and  
> finally collapse them to one "DList Char" representing the whole SVG  
> document.
> So it's a bit more complicated than your "Either" example

I suggest a monadic combinator approach.  The grammar's the thing.


 > data View a = AtomicView a
 >             | NestViews (View a) (View a) (View a)
 >             | ConcatViews (View a) (View a)
 >             | Etc...

 > instance Monad View where
 >    return = AtomicView
 >    AtomicView v                  >>= f = f v
 >    (NestViews left middle right) >>= f = (ConcatViews (ConcatViews  
left middle) right) >>= f
 >    (ConcatViews left right) 	   >>= f = ConcatVeiws (f left) (f  
 > -- Etc                           >>= f = whatever

These are "structural" nodes.  Notice how >>= normalizes your document  
automagically.  You would put your specific node types "in" Etc.   
Writing a renderer from something with this form is pretty straight  
forward.  Enforcing constraints isn't too hard either. Neither is  
parsing.  Just write a parser for each

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20091228/d569d6ce/attachment.html

More information about the Haskell-Cafe mailing list