Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are Clojure's `let` and `for` both monads?

In this discussion Brian Marick makes the point that let and for are monads in Clojure:

That said, the really general-purpose monads tend to get written into the language as special forms. Clojure's let and for are both monads, but you don't need to know that to use them.

This is let

user=> (let [c (+ 1 2)
         [d e] [5 6]]
     (-> (+ d e) (- c)))
8

This is for

user=> (for [x [0 1 2 3 4 5]
             :let [y (* x 3)]
             :when (even? y)]
         y)
(0 6 12)

My question is: Why are Clojure's let and for both monads?

like image 759
hawkeye Avatar asked Feb 13 '14 11:02

hawkeye


1 Answers

Why are Clojure's let and for both monads?

They aren't.

Clojure's let and for are not monads because they do not fully expose their Monadic common structure. They are more like monads in a sugary prison.

What are monads?

In Clojure parlance, a monad could be described as reification of a Monad protocol whose functions are expected to behave with each other and on the reified type in certain well defined ways. This is not to say that monads have to be implemented with defprotocol, reify, and friends, but this gives the idea without having to talk about typeclasses or categories.

(defprotocol Monad 
  (bind [_ mv f]) 
  (unit [_ v]))

(def id-monad
  (reify Monad 
    (bind [_ mv f] (f mv))
    (unit [_ v] v)))

(def seq-monad 
  (reify Monad 
    (bind [_ mv f] (mapcat f mv)) 
    (unit [_ v] [v])))

Sugar

Monads can be messy to use

(bind seq-monad (range 6) (fn [a] 
(bind seq-monad (range a) (fn [b] 
(unit seq-monad (* a b))))))
;=> (0 0 2 0 3 6 0 4 8 12 0 5 10 15 20)

Without some sugar

(defn do-monad-comp 
  [monad body return] 
  (reduce 
    (fn [a [exp sym]] (list 'bind monad exp (list 'fn [sym] a))) 
    (list 'unit monad return) 
    (partition 2 (rseq body))))

(defmacro do-monad [monad body return] 
  (do-monad-comp monad body return))

This is easier to write

(do-monad seq-monad 
  [a (range 6) 
   b (range a)] 
  (* a b))
;=> (0 0 2 0 3 6 0 4 8 12 0 5 10 15 20)

But isn't that just...?

This looks a lot like

(for
  [a (range 6) 
   b (range a)] 
  (* a b))
;=> (0 0 2 0 3 6 0 4 8 12 0 5 10 15 20)

And

(do-monad id-monad 
  [a 6 
   b (inc a)] 
  (* a b))
;=> 42

Looks a lot like

(let
  [a 6 
   b (inc a)] 
  (* a b))
;=> 42

So, yes, for is like the sequence monad and let is like the identity monad, but in the confines of a sugared expression.

But that's not all monads are.

Monads' structure/contract can be exploited in other ways. Many useful monadic functions can be defined in terms of only bind and unit, for example

(defn fmap 
  [monad f mv] 
  (bind monad mv (fn [v] (unit monad (f v)))))

So that they can be used with any monad

(fmap id-monad inc 1)
;=> 2

(fmap seq-monad inc [1 2 3 4])
;=> (2 3 4 5)

This might be a rather trivial example, but more generally/powerfully monads can be composed, transformed, etc. in a uniform way due to their common structure. Clojure's let and for don't fully expose this common structure, and so cannot fully participate (in a generic fashion).

like image 127
A. Webb Avatar answered Oct 07 '22 18:10

A. Webb