Monads for Scheme
Please file Issues if you find a bug or need help; I will attempt to address them!
Monads are like a burrito.
Ok, well not.
Monads are types that have:
- A binding function of the form: M a -> (a -> M b) -> M b
- A unit function: a -> M a
That’s it.
For instance, the identity monad is:
- Bind: (lambda (a f) (f a))
- 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.
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.
Yes, this monad implementation is lazily evaluated.
Defines a new monad of the given name. Also defines the following procedures:
Providing ‘fail-function’ is optional.
Function | Usage |
---|---|
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-fail | Constructs a ‘failure’ case monad for the given monadic type. Defaults to throwing an assert |
Within the body the following procedures will be defined:
Function | Usage |
---|---|
>>= | Maps to bind |
return | Maps to unit |
fail | Maps to fail |
Ie:
(using <id> (>>= (return 1) (lambda (x) (+ x 1))))
Is the same as:
(<id>-bind (<id>-unit 1) (lambda (x) (+ x 1)))
Allows any function to lift a value into the monad provided. Ie,
(define (foo bar) (if (not bar) (fail <maybe>) (return <maybe> bar)))
Would be the same as:
(define (foo bar) (if (not bar) (<maybe>-fail) (<maybe>-unit bar)))
Would be the same as:
(define (foo bar) (if (not bar) 'Nothing `(Just ,bar)))
Creates the failure case for the given monad, optionally providing a body to the failure state constructor.
Defaults to throwing an assert for most monads.
Similar to the (using) procedure, but allows for even more terseness.
Within do-using the following will be defined:
Function | Usage |
---|---|
>>= | Maps to bind |
return | Maps to unit |
fail | Maps to fail |
/m | Shorthand for calling monad-specific functions, details follow |
/m! | Shorthand for calling monad-specific functions, details follow |
<- | Shorthand for binding a symbol to a monadic value, details follow |
The /m! keyword is used as a shortcut for referencing monad-specific procedures which are prefixed with the current monad name.
For example:
(do-using <writer> (/m! tell 1))
Is the same as:
(do-using <writer> (<writer>-tell 1))
The /m keyword is like /m!, except that it references the procedure without executing it.
For example:
(do-using <state> (x <- (/m get)) (return x))
Is the same as:
(do-using <state> (x <- <state>-get) (return x))
The <- keyword is used as a shortcut for binding a value within a monad.
For example:
(do-using <maybe> (x <- (return 1)) x)
Is the same as:
(do-using <maybe> (>>= (return 1) (lambda (x) (do-using <maybe> x))))
A simple example:
(do-using <maybe> (x <- (return <maybe> 1)) x) ;Returns: (Just 1)
Or, a more complex example:
(do-using <maybe> (x <- (return 1)) (if (eq? 2 x) (return 'Banana) (fail)) (y <- (return 'Apple)) y)
:
;Returns: Nothing
Alias for (do-using monad [body …]).
The /m! keyword is used as a shortcut for referencing monad-specific procedures which are prefixed with the current monad name.
For example:
(do-using <writer> (/m! tell 1))
Is the same as:
(do-using <writer> (<writer>-tell 1))
The /m keyword is like /m!, except that it references the procedure without executing it.
For example:
(do-using <state> (x <- (/m get)) (return x))
Is the same as:
(do-using <writer> (x <- <state>-get) (return x))
The <- keyword is used as a shortcut for binding a value within a monad.
For example:
(do-using <maybe> (x <- (return 1)) x)
Is the same as:
(do-using <maybe> (>>= (return 1) (lambda (x) (do-using <maybe> x))))
A simple example:
(do-using <maybe> (x <- (return <maybe> 1)) x) ;Returns: (Just 1)
Or, a more complex example:
(do-using <maybe> (x <- (return 1)) (if (eq? 2 x) (return 'Banana) (fail)) (y <- (return 'Apple)) y)
:
;Returns: Nothing
Simple monads pre-defined by this egg.
(define-monad <id> (lambda (a) a) (lambda (a f) (f a)))
(define-monad <maybe> (lambda (a) `(Just ,a)) (lambda (a f) (if (not (eq? 'Nothing a)) (f (cadr a)) 'Nothing)) (case-lambda (() 'Nothing) ((_ . _) 'Nothing)))
> (do/m <maybe> (if #t 'Nothing '(Just First)) '(Just Second)) Nothing
(define-monad <either> (lambda (a) `(Right ,a)) (lambda (a f) (if (not (eq? 'Left (car a))) (f (cadr a)) a)) (case-lambda (() '(Left #f)) ((e) `(Left ,e))))
#;3> (do/m <either> (x <- (return 1)) (y <- (fail "...")) (return (+ x y))) (Left "...")
(define-monad <list> (lambda (a) (list a)) (lambda (a f) (concatenate! (map! f a))))
#;> (do/m <list> (x <- '(1 2 3)) (y <- '(a b c)) (return `(,x ,y))) ((1 a) (1 b) (1 c) (2 a) (2 b) (2 c) (3 a) (3 b) (3 c))
(define-monad <state> (lambda (a) (lambda (s) `(,a . ,s))) (lambda (a f) (lambda (s) (let* ((p (a s)) (a^ (car p)) (s^ (cdr p))) ((f a^) s^)))))
<procedure>(<state>-get s)</procedure>
Retrieves the current state from a given <state> monad.
#;> ((do/m <state> (x <- (/m get)) (return x)) "Hi!") ("Hi!" . "Hi!")
<procedure>(<state>-gets f)</procedure>
Creates a monad that retrieves a given state after filtering it with the function provided.
#;> ((do/m <state> (x <- (/m! gets (lambda (s) (+ s 1)))) (return x)) 1) (2 . 1)
<procedure>(<state>-modify f)</procedure>
Creates a monad that modifies the current state with a given function.
#;> ((do/m <state> (/m! modify (lambda (v) (display (format "Received: ~S\n" v)) (+ v 1)))) 1) Received: 1 (() . 2)
<procedure>(<state>-put v)</procedure>
Creates a monad that forces a value into the current <state> monad.
#;> ((do/m <state> (/m! put 1)) 2) (() . 1)
(define-monad <reader> (lambda (a) (lambda (v) a)) (lambda (a f) (lambda (v) ((f (a v)) v))))
<procedure>(<reader>-ask m)</procedure>
Extracts the current value from the current reader monad.
#;> ((do/m <reader> (x <- (/m ask)) (return (+ x 1))) 1) 2
<procedure>(<reader>-asks f)</procedure>
Creates a monad that filters the current value in the reader monad with the given function.
#;> ((do/m <reader> (/m! asks (lambda (v) (+ v 1)))) 1) 2
<procedure>(<reader>-local f m)</procedure>
Creats a monad that first filters the current reader monad value with the provided function, then passes that filtered value to the provided reader monad.
#;> ((do/m <reader> (/m! local (lambda (v) (+ v 1)) (do/m <reader> (x <- (/m ask)) (return x)))) 1) 2
(define-monad <writer> (lambda (a) (cons a '())) (lambda (a f) (let* ((b (f (car a)))) (cons (car b) (append (cdr a) (cdr b))))))
<procedure>(<writer>-tell . v)</procedure>
Creates a monad where the provided value is wrapped in a <writer>.
#;> (do/m <writer> (x <- (return 'hello)) (/m! tell x) (/m! tell 'world)) (() hello world)
<procedure>(<writer>-listen a)</procedure>
Creates a monad where the value has been extracted out of the writer.
#;> (let ((other-writer (do/m <writer> (/m! tell 'hello) (/m! tell 'test) (/m! tell 'world)))) (do/m <writer> (x <- (/m! listen other-writer)) (return x))) ((() hello test world) hello test world)
<procedure>(<writer>-listens f m)</procedure>
Creates a monad that is the outcome of applying the provided function on the given writer monad.
#;> (let ((other-writer (do/m <writer> (/m! tell 'hello) (/m! tell 'test) (/m! tell 'world)))) (do/m <writer> (x <- (/m! listens (lambda (l) (map (lambda (s) 'arf) l)) other-writer)) (return x))) ((() arf arf arf) hello test world)
<procedure>(<writer>-pass m)</procedure>
Creates a monad that is the outcome of mutating the provided writer monad with a given function. Expects to be provided a monad of the form: ((value . function) . rest-of-writer)
Not generally used much except by those that know what they’re doing, you’re likely after <writer>-censor for most cases.
#;> (let ((other-writer (do/m <writer> (/m! tell '(1 2 3)) (return (/m! tell (lambda (l) (map ->string l))))))) (do/m <writer> (/m! pass other-writer))) (() "(1 2 3)")
<procedure>(<writer>-censor f m)</procedure>
Creates a monad resulting from the application of the provided function on the given monad, analyzing the values that were in the given monad.
Think of it as a shortcut to using pass without all the cruft, or flexibility.
#;> (let ((other-writer (do/m <writer> (/m! tell '(1 2 3))))) (do/m <writer> (/m! censor (lambda (l) (map ->string l)) other-writer))) (() "(1 2 3)")
Version | Notes |
---|---|
5.1 | Added Either monad, fixed documentation |
5.0 | Ported to Chicken 5 |
4.1 | Fixed #7, updated readme.org |
4.0 | Dropped Exception and CPS, as they did not work under basic tests; removed do keyword, fixed Writer monad |
3.3 | Total rewrite, no API changes |
3.2 | Minor syntax change |
3.1 | Use let* instead of letrec |
3.0 | Renamed :! to /m! and : to /m due to : already being used for explicit specialization |
2.4 | Added :! keyword, fixed : keyword |
2.3 | Added fail function for all monads which defaults to assert, but is definied appropriately for the <exception> and <maybe> monads. Added : keyword to using and do-using. Added >>=, return and fail bindings to do-using syntax. |
2.2 | Added failure states for monads |
2.1 | Rewrote API to allow for terser execution and a simpler interface. Removed use of promises completely. Removed doto-using, run-chain, and run from the API completely. Added do-using syntax. Maybe monad is now self-defined for it’s value or no-value states. |
2.0 | Internal rewrite that broke the API, immediately followed by 2.1. |
1.1 | Added Cameron Swords’ State, Reader, Writer, CPS and Exception monads |
1.0 | Initial release |
Contributions are welcome provided you accept the license I have chosen for this egg for the contributions themselves.
The github repository is at: https://github.com/dleslie/monad-egg
Original Egg By Daniel J. Leslie [email protected]
Additional Contributors:
- Cameron Swords
- Peter Bex
- Alice Maz
- Kelly Betty
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:
- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
- 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.