monad-egg
monad-egg copied to clipboard
Monads for scheme
- Monads Monads for Scheme
Please file Issues if you find a bug or need help; I will attempt to address them!
- Description ** What is a Monad?
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.
** 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.
** What's this about lazy evaluation?
Yes, this monad implementation is lazily evaluated.
- Functions *** (define-monad name unit-function bind-function [fail-function])
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 |
*** (using monad [body ...])
Within the body the following procedures will be defined:
| Function | Usage | |----------+--------------| | >>= | Maps to bind | | return | Maps to unit | | fail | Maps to fail |
Ie:
: (using
Is the same as:
: (
*** (return monad value)
Allows any function to lift a value into the monad provided. Ie,
: (define (foo bar)
: (if (not bar)
: (fail
Would be the same as:
: (define (foo bar)
: (if (not bar)
: (
Would be the same as:
: (define (foo bar) : (if (not bar) : 'Nothing : `(Just ,bar)))
*** (fail monad [body ...])
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.
*** (do-using monad [body ...])
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 |
**** /m! Keyword
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
Is the same as:
: (do-using
**** /m Keyword
The /m keyword is like /m!, except that it references the procedure without executing it.
For example:
: (do-using
Is the same as:
: (do-using
**** <- Keyword
The <- keyword is used as a shortcut for binding a value within a monad.
For example:
: (do-using
Is the same as:
: (do-using
**** General Example
A simple example:
: (do-using
Or, a more complex example:
: (do-using
*** (do/m monad [body ...])
Alias for (do-using monad [body ...]).
**** /m! Keyword
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
Is the same as:
: (do-using
**** /m Keyword
The /m keyword is like /m!, except that it references the procedure without executing it.
For example:
: (do-using
Is the same as:
: (do-using
**** <- Keyword
The <- keyword is used as a shortcut for binding a value within a monad.
For example:
: (do-using
Is the same as:
: (do-using
**** General Example
A simple example:
: (do-using
Or, a more complex example:
: (do-using
- Basic Monads Simple monads pre-defined by this egg.
** Identity
: (define-monad
:
** Maybe
: (define-monad
:
*** Example
: > (do/m
** List
: (define-monad
:
: (lambda (a) (list a))
: (lambda (a f) (concatenate! (map! f a))))
*** Example
: #;> (do/m
: (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))
** State
: (define-monad
:
*** Extra Methods
****
Retrieves the current state from a given
***** Example
: #;> ((do/m
****
Creates a monad that retrieves a given state after filtering it with the function provided.
***** Example
: #;> ((do/m
****
Creates a monad that modifies the current state with a given function.
***** Example
: #;> ((do/m
****
Creates a monad that forces a value into the current
***** Example
: #;> ((do/m
** Reader
: (define-monad
:
*** Extra Methods
****
Extracts the current value from the current reader monad.
***** Example
: #;> ((do/m
****
Creates a monad that filters the current value in the reader monad with the given function.
***** Example
: #;> ((do/m
****
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.
***** Example
: #;> ((do/m
** Writer
: (define-monad
:
*** Extra Methods
****
Creates a monad where the provided value is wrapped in a
***** Example
: #;> (do/m
****
Creates a monad where the value has been extracted out of the writer.
***** Example
: #;> (let ((other-writer
: (do/m
****
Creates a monad that is the outcome of applying the provided function on the given writer monad.
***** Example
: #;> (let ((other-writer
: (do/m
****
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
***** Example
: #;> (let ((other-writer
: (do/m
****
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
- Version History
| Version | Notes |
|---------+-------|
| 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
- Contribution
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
- Authors
Original Egg By Daniel J. Leslie [email protected]
Additional Contributors: Cameron Swords Peter Bex Alice Maz
- 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:
-
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.