In Fantasy Land, types are specified by an object containing a catamorphic
method cata. cata has an arity matching the number of constructor functions
belonging to each type. Each argument to cata is either:
- a value
- a function with an arity matching the number of arguments to the constructor
function.
- Each function argument to
catamust return a value of the same type ascataitself.
- Each function argument to
ex.
Identity a = { cata: (a -> b) -> b }
identity :: a -> Identity aConforming libraries may provide custom data constructors corresponding to these
functions. No provided constructors are required to share the names of the
constructor functions defined herein. For example, instead of Identity, the
constructor could be named Id. If custom constructors are provided, they
must return values which contain a cata method. This method must exhibit the
same behaviour as described above.
ex.
var identity = require('fantasy-land/identity');
function Id(x) {
this.x = x;
}
Id.prototype['fantasy-land/cata'] = function cata(f) {
return f(this.x);
}
// Alternatively
Id.prototype['fantasy-land/cata'] = function cata(f) {
return identity(this.x).cata(f);
}
(new Id(42)).cata(x => x) === identity(42).cata(x => x);The Either type encodes the concept of binary possibility (Left a and Right b).
Either a b = { cata :: ((a -> c), (b -> c)) -> c }
left :: a -> Either a b
right :: b -> Either a bA value which conforms to the Either specification must provide an cata method.
The cata method takes two arguments:
e.cata(f, g)
-
fmust be a function which returns a value- If
fis not a function, the behaviour ofcatais unspecified. - No parts of
f's return value should be checked.
- If
-
gmust be a function which returns a value- If
gis not a function, the behaviour ofcatais unspecified. - No parts of
g's return value should be checked.
- If