Jim,
you were quicker than me in implementing monad transformers! What I
had in mind is exactly what you did: a monad transformer would be
implemented as a function taking a monad as an argument. That's in
fact why I defined the monad macro in addition to defmonad.
> I did some more work from the paper the parser combinator is based
> on. From that I built what I think is a state monad transformer:
That's exactly what it is. In fact, it looks like a translation of
the Haskell implementation.
There is one aspect of your implementation that I don't like: it
exposes the internal representation of monads as maps. This can be
avoided by using "with-monad m" in the definition of the monad
operations:
(defn stateT [m]
(monad [m-result (with-monad m
(fn m-result-state-t [v]
(fn [s] (m-result (list v s)))))
m-bind (with-monad m
(fn m-bind-state-t [stm f]
(fn [s]
(m-bind (stm s)
(fn [[v ss]]
((f v) ss))))))
m-zero (with-monad m
(when m-zero
(fn [s] m-zero)))
m-plus (with-monad m
(when m-plus
(fn m-plus-state-t [& stms]
(fn [s] (apply m-plus (map #(% s) stms))))))
]))
It should also be possible to write m-bind as a monad comprehension
in the inner monad:
m-bind (with-monad m
(fn m-bind-state-t [stm f]
(fn [s]
(domonad
[[v ss] (stm s)]
((f v) ss)))))
This looks clearer to me, but I didn't test if it actually works.
> I also rewrote the maybe monad as:
...
Why did you do this? Did you just want a more concise implementation,
or is there a difference in behaviour? As far as I can see, your
version of m-bind does exactly the same as mine for all input values
that can occur in the given context. Yours would also do something
useful given a vector of more than one value, but that should never
happen in the maybe monad.
Konrad.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---