Mon Feb 12 00:53:53 GMT 2007
about a year ago i made a decision to use a functional dynamic store
to solve the problem of state, because i didn't understand the idea
behind monads. this was a mistake, but i guess a necessary one. i
probably wasn't ready for the ideas at that time.
now i think i sort of get it. monads (haskell style) are about
dragging along state implicitly.
the irony is, i implemented that!
what i did was to have an implicit state objected being dragged along
as a top of stack element, invisible to some computations. this is the
the mistake is: this too general. it's better to use a smaller gun to
solve the problem at hand on a more local scale, instead of basicly
using a state machine model (albeit one without destructive mutation).
the small gun is mostly related to the 'Writer Monad'. the operation
that's made implicit is 'append'. i call this 'lift-stateful'. this,
together with some other state dragging (if the data stack is not
used, it can be dragged: some operations, like the pattern matching
peephole optimizer, work on the produced code as a stack.)
the thing that's really interesting though is this: if you start to
think about forth as a compositional language, then this whole monad
thing is nothing more than a way to 'lift' words so they can be
composed in linear code.
basicly. if the things you want to compose are operations A x B -> A x
B, but what you have is operations like
A -> A
B -> B
A -> B
A -> A x B
B -> A x B
A x B -> A
together with a higher order function (hof) that will correctly lift
them to A x B -> A x B, then what you're doing is abstracting away the
trivial parts of such a map in this hof.
for the writer monad, the trivial part is 'append'. replace 'trivial
work' with 'hard work' and you get this:
"By using a monad, a simplified interface to the necessary
functionality can be provided, while the hard work of maintaining and
passing the context is handled behind the scenes."
so, what i need to do is to work out some abstractions so i can
perform this kind of magic in straight cat without having to resort to