[Haskell-cafe] Designing an object model in Haskell

Ketil Malde Ketil.Malde at bccs.uib.no
Thu Dec 7 07:30:27 EST 2006


John Ky wrote:
> Hi,
>
> I've got an object model that I have a difficult time conceptualising 
> how it
> might look like in Haskell:
>
> class Element { }
>
> class Inline : Element { }
>
> class ParentInline : Inline {
>   List<Inline> children;
> }
>
> class Bold : ParentInline { }
> class Underline : ParentInline { }
>
> class Link : ParentInline {
>   String link;
> }
>
> class Text : Inline {
>   String text;
> }
>
> class Block : Element { }
>
> class Paragraph : Block {
>   List<Inline> paragraph;
> }
>
> class Heading : Block {
>   List<Inline> heading;
> }
>
> class Document : Element {
>   List<Block> blocks;
> }
>
> How best to represent this OO data model in Haskell?
You could, depending on what you intend to do, model a hierarchical
document type something like this (if I read your model correctly):

  type Document = [Block]
  data Block = Heading [Inline] | Paragraph [Inline]
  data Inline = Text String | ParentInline
  data ParentInline = PIL PILType [Inline]
  data PILType = Bold | Underline | Link String

Does that fit?

-k


More information about the Haskell-Cafe mailing list