Skip to content

Instantly share code, notes, and snippets.

@khinsen
Created October 1, 2012 09:03
Show Gist options
  • Save khinsen/3810448 to your computer and use it in GitHub Desktop.
Save khinsen/3810448 to your computer and use it in GitHub Desktop.
A challenge for typed Clojure
; A simple example for using the writer monad
(domonad (writer-m "")
[x (m-result 1)
_ (write "first step\n")
y (m-result 2)
_ (write "second step\n")]
(+ x y))
; where write (defined in algo.monads) is:
(defmonadfn write [v]
(let [[_ a] (m-result nil)]
[nil (writer-m-add a v)]))
; Now suppose we move to a monad system based on types/protocols.
; We'd have
(domonad
[x (m-result 1)
_ (write "first step\n")
y (m-result 2)
_ (write "second step\n")]
(+ x y))
; plus type annotations in the various definitions. Can typed Clojure infer
; that (m-result 1) must return the type representing the writer monad with
; a string accumulator from the known facts which are:
; 1) The return type of write is "writer monad type"
; 2) The argument of write is a string.
; 3) The m-bind chain into which the domonad macro expands can only
; be made type-consistent if the return type of m-result is the
; the same as the return type of (write "").
@jduey
Copy link

jduey commented Oct 1, 2012

In protocol monads, there is no single m-result function. Each monad has a different function that serves the purpose of m-result; hash-set, vector, cont, etc. So there shouldn't be any problem typing things.

@khinsen
Copy link
Author

khinsen commented Oct 1, 2012

The point of my example is not to improve typing of protocol monads (which should indeed not present any difficulty), but to see if typed Clojure can combine the full genericity of algo.monads with the performance advantages of protocol monads. Apparently the answer is no.

@jduey
Copy link

jduey commented Oct 1, 2012

Ah yes. I'm pretty sure I agree with that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment