Discussion:
Clojure.contrib: name changes in monads
Konrad Hinsen
2009-02-18 08:24:52 UTC
Permalink
The latest Clojure version broke many of my code by introducing the
function sequence whose name collided with my sequence monad. So I
decided that since now is the time for breaking changes, I should
solve that kind of problem thoroughly. I just renamed all monads in
clojure.contrib.monads and clojure.contrib.probabilities, the names
now have a -m suffix: sequence-m, maybe-m, etc.

Konrad.
jim
2009-02-18 21:40:54 UTC
Permalink
Konrad,

Here's an updated state-m monad transformer. I dropped my CA in the
mail today. I figure if I post a snippet of code to the list, it's
public domain, so do with it as you wish. Or wait till Rich gets my
CA.

(defn state-t [m]
(monad [m-result (with-monad m
(fn [v]
(fn [s]
(m-result (list v s)))))

m-bind (with-monad m
(fn [stm f]
(fn [s]
(m-bind (stm s)
(fn [[v ss]]
((f v) ss))))))

m-zero (with-monad m
(if (not= ::undefined m-zero)
::undefined
(fn [s]
m-zero)))

m-plus (with-monad m
(if (= ::undefined m-plus)
::undefined
(fn [& stms]
(fn [s]
(apply m-plus (map #(% s) stms))))))
]))

Jim
The latest Clojure version broke many of my code by introducing the  
function sequence whose name collided with my sequence monad. So I  
decided that since now is the time for breaking changes, I should  
solve that kind of problem thoroughly. I just renamed all monads in  
clojure.contrib.monads and clojure.contrib.probabilities, the names  
now have a -m suffix: sequence-m, maybe-m, etc.
Konrad.
Konrad Hinsen
2009-02-19 07:45:11 UTC
Permalink
Post by jim
Here's an updated state-m monad transformer. I dropped my CA in the
mail today. I figure if I post a snippet of code to the list, it's
public domain, so do with it as you wish. Or wait till Rich gets my
CA.
Thanks! It's in my working copy of the monads library module
already, and I will check it in when I see your name on the
contributors' list. I'll be absent for a few days anyway, so it
hardly makes a difference.

Konrad.
jim
2009-02-19 15:09:54 UTC
Permalink
Konrad,

As long as your breaking things in monads, what would you think of
changing m-seq to this.

(defmonadfn m-seq
"'Executes' the monadic values in ms and returns a sequence of the
basic values contained in them."
[& ms]
(reduce (fn [q p]
(m-bind p (fn [x]
(m-bind q (fn [y]
(m-result (cons x y)))) )))
(m-result '())
(reverse ms)))

so that it doesn't accept a list of monadic values but instead lets
you call it with any number of mv's.

Instead of:

(m-seq [mv1 mv2 mv3])

you would write

(m-seq mv1 mv2 mv3)

That would make it be the same as the implementations of m-plus you've
already done.

Thanks
Here's an updated state-m monad transformer.  I dropped my CA in the
mail today.  I figure if I post a snippet of code to the list, it's
public domain, so do with it as you wish.  Or wait till Rich gets my
CA.
Thanks!  It's in my working copy of themonadslibrary module  
already, and I will check it in when I see your name on the  
contributors' list. I'll be absent for a few days anyway, so it  
hardly makes a difference.
Konrad.
Konrad Hinsen
2009-02-22 21:10:02 UTC
Permalink
Jim,
Post by jim
As long as your breaking things in monads, what would you think of
changing m-seq to this.
...
Post by jim
so that it doesn't accept a list of monadic values but instead lets
you call it with any number of mv's.
(m-seq [mv1 mv2 mv3])
you would write
(m-seq mv1 mv2 mv3)
That would make it be the same as the implementations of m-plus you've
already done.
Do you have a concrete use case where this would be advantageous? In
my certainly limited experience, m-plus is most frequently called
with two fixed arguments, whereas m-seq is typically called with a
list argument that was constructed using other list operations. In
the latter situation, the change you propose is a significant
disadvantage, all the more since m-seq is a macro and thus cannot be
used directly with apply.

Konrad.
jim
2009-02-22 21:30:20 UTC
Permalink
I converted the code in the paper "Monadic Parsing in Haskell" to
Clojure. Both m-plus and m-seq are used to combine lists of parsers.
m-plus is used to build a parser that executes each parser in the list
until one succeeds. m-seq is used to build a parser that executes all
the parsers in the list sequentially and only succeeds if all the
parsers succeed.

Having m-plus accept a variable number of args is great, in that
case. Having m-seq only accept one arg, which is a list, didn't feel
like it followed the pattern established by m-plus.

In any case, it's a pretty minor thing to adapt m-seq, so if there are
other cases where the current implementation is easier, it's no big
deal. Just felt like little rough edge.

Jim
Do you have a concrete use case where this would be advantageous? In  
my certainly limited experience, m-plus is most frequently called  
with two fixed arguments, whereas m-seq is typically called with a  
list argument that was constructed using other list operations. In  
the latter situation, the change you propose is a significant  
disadvantage, all the more since m-seq is a macro and thus cannot be  
used directly with apply.
Konrad.
Jeffrey Straszheim
2009-02-18 21:55:38 UTC
Permalink
I like that. It makes it clear what is a monad, and what is not.
Post by Konrad Hinsen
The latest Clojure version broke many of my code by introducing the
function sequence whose name collided with my sequence monad. So I
decided that since now is the time for breaking changes, I should
solve that kind of problem thoroughly. I just renamed all monads in
clojure.contrib.monads and clojure.contrib.probabilities, the names
now have a -m suffix: sequence-m, maybe-m, etc.
Konrad.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Clojure" group.
To post to this group, send email to ***@googlegroups.com
To unsubscribe from this group, send email to clojure+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---
Continue reading on narkive:
Loading...