fantasy-land
Specification for interoperability of common algebraic structures in JavaScript
npm install fantasy-land
Want to see pretty graphs? Log in now!
1 | downloads in the last week |
4 | downloads in the last month |
Last Published By | |
---|---|
Maintainers | |
Version | 0.0.1 last updated a year ago |
License | XXX |
Keywords | algebraic, monad, functor, monoid, semigroup |
Repository | https://github.com/pufuwozu/fantasy-land.git (git) |
Homepage | https://github.com/pufuwozu/fantasy-land |
Dependencies | None |
Fantasy Land Specification
(aka "Algebraic JavaScript Specification")
This project specifies interoperability of common algebraic structures:
- Semigroup
- Monoid
- Functor
- Chain
- Monad
General
An algebra is a set of values, a set of operators that it is closed under and some laws it must obey.
Each Fantasy Land algebra is a separate specification. An algebra may have dependencies on other algebras which must be implemented. An algebra may also state other algebra methods which do not need to be implemented and how they can be derived from new methods.
Terminology
- "value" is any JavaScript value, including any which have the structures defined below.
- "equivalent" is an appropriate definition of equivalence for the given value.
The definition should ensure that the two values can be safely swapped out in a program that respects abstractions. For example:
- Two lists are equivalent if they are equivalent at all indices.
- Two plain old JavaScript objects, interpreted as dictionaries, are equivalent when they are equivalent for all keys.
- Two promises are equivalent when they yield equivalent values.
- Two functions are equivalent if they yield equivalent outputs for equivalent inputs.
Algebras
Semigroup
a.concat(b).concat(c)
is equivalent toa.concat(b.concat(c))
(associativity)
concat
method
A value which has a Semigroup must provide a concat
method. The
concat
method takes one argument:
s.concat(b)
b
must be a value of the same Semigroup- If
b
is not the same semigroup, behaviour ofconcat
is unspecified.
- If
concat
must return a value of the same Semigroup.
Monoid
A value that implements the Monoid specification must also implement the Semigroup specficiation.
m.concat(m.zero())
is equivalent tom
(right identity)m.zero().concat(m)
is equivalent tom
(left identity)
zero
method
A value which has a Monoid must provide an zero
method on itself or
its constructor
object. The zero
method takes no arguments:
m.zero()
m.constructor.zero()
zero
must return a value of the same Monoid
Functor
u.map(function(a) { return a; }))
is equivalent tou
(identity)u.map(function(x) { return f(g(x)); })
is equivalent tou.map(g).map(f)
(composition)
map
method
A value which has a Functor must provide a map
method. The map
method takes one argument:
u.map(f)
f
must be a function,- If
f
is not a function, the behaviour ofmap
is unspecified. f
can return any value.
- If
map
must return a value of the same Functor
Chain
m.chain(f).chain(g)
is equivalent tom.chain(function(x) { return f(x).chain(g); })
(associativity)
chain
method
A value which has a Chain must provide a chain
method. The chain
method takes one argument:
m.chain(f)
f
must be a function which returns a value- If
f
is not a function, the behaviour ofchain
is unspecified. f
must return a value of the same Chain
- If
chain
must return a value of the same Chain
Monad
A value that implements the Monad specification must also implement the Chain specficiation.
A value which satisfies the specification of a Monad does not need to implement:
Functor's
map
; derivable asfunction(f) { var m = this; return m.chain(function(a) { return m.of(f(a)); })}
m.of(a).chain(f)
is equivalent tof(a)
(left identity)m.chain(m.of)
is equivalent tom
(right identity)
of
method
A value which has a Monad must provide an of
method on itself or its
constructor
object. The of
method takes one argument:
m.of(a)
m.constructor.of(a)
of
must provide a value of the same Monad- No parts of
a
should be checked
- No parts of
Notes
- If there's more than a single way to implement the methods and laws, the implementation should choose one and provide wrappers for other uses.
- It's discouraged to overload the specified methods. It can easily result in broken and buggy behaviour.
- It is recommended to throw an exception on unspecified behaviour.
- An
Id
container which implements all methods is provided inid.js
.