You are looking at historical revision 26484 of this page. It may differ significantly from its current revision.

Monads

Monads for Scheme

By Daniel J. Leslie

dan@ironoxide.ca

Description

What is a Monad?

Monads are like a burrito.

Ok, well not.

Monads are types that have:

1. A binding function of the form: M a -> (a -> b) -> M b

2. A unit function: a -> M a

That's it.

For instance, the identity monad is:

1. Bind: (lambda (a f) (f a))

2. Unit: (lambda (a) a)

Not too bad, eh?

Most of what we write that can be described as a "recipe list" or a "do to" list is a Monad. And, as you may have noticed, we write a lot of boiler plate code to handle the interim wrapping/unwrapping/error checking functionality that occurs between each step.

The Monad egg allows you to write that code once and remain focused on the task at hand.

Why not use miscmacros:doto instead?

Because you may want intermediary control and multiple usage of a unit function.

Monads aren't simply about iterating over a value or set of values and passing them to various functions; they're about removing recurring boilerplate related to constructing the desired values, and any intermediary steps that may be required between processing the values.

Yes, sometimes this isn't strictly a necessary set of functionality. But for when it is, save yourself some time and write a monad.

Why not use F-operator instead?

If you can understand what that is or how it works and it suits your needs better than this egg, then please, go right ahead.

What's this about lazy evaluation?

Yes, this monad implementation is lazily evaluated.

When (run) is called with a given Monad the bind and unit functions are evaluated and their actions are undertaken. This happens once, and once only. Further executions of (run) will simply return the computed value of the first execution.

Furthermore, when running in a REPL or printing to a string a monad will automatically be run! If you don't want this to happen, then capture the value before the REPL would print it.

For example:

#;1> (use monad)
; loading /usr/local/lib/chicken/6/monad.so ...
; loading /usr/local/lib/chicken/6/monad.import.so ...
#;2> (define m (doto-using <id> 1 (lambda (x) (display "Hi!\n") (+ x 1))))
#;3> m
Hi!
#<id> 2
#;4> m
#<id> 2
#;5> (define (m2 y) (doto-using <id> y (lambda (x) (display "Hi!\n") (+ x 1))))
#;6> (m2 1)
Hi!
#<id> 2
#;7> (m2 1)
Hi!
#<id> 2

Functions

[procedure] (define-monad name unit-function bind-function)

Defines a new monad of the given name. Also defines the following procedures:

name-unit
Constructs a new monad from a value using the given unit function
name-bind
Constructs a new monad from an existing monad and a function using the given bind function
name?
Predicate for the new monad
[procedure] (run monad)

Runs a monad and returns the outcome.

[procedure] (run-chain init [monadic-functions ...])

Runs a chain of monadic functions.

Expects that each monad-returning function accepts a single parameter which can be represented by the init value.

Ie,

 #;1> (define (f1 a) (using <id> (return a)))
 #;2> (define (f2 a) (doto-using <id> a (lambda (b) (+ x b))))
 #;3> (run-chain 1 f1 f2)
 2
[procedure] (using monad [body ...])

Within the body the following procedures will be defined:

>>=
Maps to bind
return
Maps to unit

Ie:

(using <id>
  (>>= (return 1) (lambda (x) (+ x 1))))

;Is the same as:

(<id>-bind (<id>-unit 1) (lambda  (x) (+ x 1)))
[procedure] (doto-using monad init [body ...])

Similar to the (using) procedure, but allows for even more terseness.

The init value is turned into a monad using the monad's unit-function, and is then passed to a chain of monads constructed via a cascading chain of bind-function operations performed upon the body.

Ie,

(doto-using <id> (+ 0 1)
  (lambda (x) (+ x 1))
  (lambda (y) (+ y 2)))

;Is the same as:

(using <id>
  (>>= (>>= (return (+ 0 1)) 
            (lambda (x) (+ x 1)))
       (lambda (y) (+ y 2))))

Basic Monads

These monads are all pre-defined by this egg.

Identity

 (define-monad
   <id>
   (lambda (a) a)
   (lambda (a f) (f a)))

Maybe

 (define-monad
   <maybe>
   (lambda (a) a)
   (lambda (a f) (if a (f a) #f)))

List

 (define-monad
   <list>
   (lambda (a) (list a))
   (lambda (a f) (concatenate! (map! f a))))

Example

Everyone expects a prototypical logger example, so here goes:

 ; Wrap the output port in the intermediary value, so that:
 ; (return a) gives as a value ( output-port . a )

 (define-monad <logger>
   (lambda (a) 
     (let ((output-port (car a))
           (value (cdr a)))
       (fprintf output-port "Starting with: ~S\n" value)
       a))
   (lambda (a f)
     (let* ((output-port (car a))
            (value  (cdr a))
            (returned (f v)))
       (fprintf output-port "Calling (~S ~S) returned ~S\n" f value returned)
       (cons output-port returned))))
 
 (define (f1 x) (+ x 1))
 (define (f2 x) (- x 1))
 
 (define m
   (doto-using <logger> 
               (cons (current-output-port) 0) 
               f1 
               f2))
 
 (assert (eq? 0 (cdr (run m)))
         "Did the logger test work?")

Outputs:

 Starting with: 0
 Calling (#<procedure (f1 x)> 0) returned 1
 Calling (#<procedure (f2 x)> 1) returned 0

Contribution

Contributions are welcome provided you accept the license I have chosen for this egg for the contributions themselves.

The repository is at GitHub.

License

Copyright 2012 Daniel J. Leslie. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of
     conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list
     of conditions and the following disclaimer in the documentation and/or other materials
     provided with the distribution.

THIS SOFTWARE IS PROVIDED BY DANIEL J. LESLIE AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DANIEL J. LESLIE OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

The views and conclusions contained in the software and documentation are those of the authors and should not be interpreted as representing official policies, either expressed or implied, of Daniel J. Leslie.