[Haskell-cafe] Datastructure with some business-rules

Mathijs Kwik bluescreen303 at gmail.com
Wed Jun 3 04:03:27 EDT 2009


Hi all,

I'm building a tree-like structure that somewhat resembles a package/
dependency structure as most packagemanagers/ports-systems have them.

It's tree-like (Data.Tree at the moment), but I will probably need to
make some structural changes to allow for more complex stuff like
circular dependencies and stuff like 'alternatives'.

While I'm still on the Tree (nice & simple), I want to explore some
business-rules & validation stuff.
This is a bit of a contrived example:
Let's say that nodes are (NodeType, Int) pairs, where the Nodetype is
just a simple algebraïc type.
Now certain types of nodes are only allowed to 'grow' on top of some
other nodes, but up to 10 levels deep.
So certain nodes are only allowed if one of 10 ancestors is of some
other type.
Or to make this a bit weirder (maybe too contrived): its parent should
have a 'brother' of some type.

Ofcourse I can make a 'validate' function that checks if a tree plays
by the rules, but I would like to put the typesystem to use somehow,
since there aren't that many rules.
So I would like to have an error thrown any time an invalid structure
is used/created instead of having to validate myself every time. I
would like this, because in the future I will need to 'morph' trees
into each other so inbetween steps that exist must still be valid.
First I thought about creating some auto-validate monad that just
validates the result after every step, but before going that way I
would like to make sure this is the way to go.

I understand that Data.Tree (and other functors) just take 1 type
'payload' and they don't mind what's in there, while my 'validation'
rules are about the payload _and_ the place in the tree, so I will
probably have to change a bit here.
Is this at all possible?
Is it ok to do or is a 'external' validation/validation monad clearer?
What would be the way to go?

Thanks for any help!

Mathijs


More information about the Haskell-Cafe mailing list