Skip to content

Commit

Permalink
add laws and update index.js
Browse files Browse the repository at this point in the history
  • Loading branch information
safareli committed Oct 28, 2016
1 parent 83dd3cf commit 876d09e
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 0 deletions.
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
map: 'fantasy-land/map',
ap: 'fantasy-land/ap',
of: 'fantasy-land/of',
alt: 'fantasy-land/alt',
pempty: 'fantasy-land/pempty',
reduce: 'fantasy-land/reduce',
traverse: 'fantasy-land/traverse',
chain: 'fantasy-land/chain',
Expand Down
24 changes: 24 additions & 0 deletions laws/alt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';

const {alt, map} = require('..');

/**
### Alt
1. `a.alt(b).alt(c)` is equivalent to `a.alt(b.alt(c))` (associativity)
2. `a.alt(b).map(f)` is equivalent to `a.map(f).alt(b.map(f))` (distributivity)
**/

const associativity = eq => a => b => c => eq(
a[alt](b)[alt](c),
a[alt](b[alt](c))
);

const distributivity = eq => a => b => f => eq(
a[alt](b)[map](f),
a[map](f)[alt](b[map](f))
);

module.exports = {associativity, distributivity};
24 changes: 24 additions & 0 deletions laws/alternative.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';

const {alt, pempty, ap} = require('..');

/**
### Alternative
1. `x.ap(f.alt(g))` is equivalent to `x.ap(f).alt(x.ap(g))` (distributivity)
1. `x.ap(M.pempty())` is equivalent to `M.pempty()` (annihilation)
**/

const distributivity = eq => x => f => g => eq(
x[ap](f[alt](g)),
x[ap](f)[alt](x[ap(g)])
);

const annihilation = T => eq => x => eq(
x[ap](T[pempty]()),
T[pempty]()
);

module.exports = {distributivity, annihilation};
18 changes: 18 additions & 0 deletions laws/monadOr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

const {of, alt} = require('..');

/**
### MonadOr
1. `M.of(a).alt(b)` is equivalent to `M.of(a)` (left catch)
**/

const leftCatch = T => eq => a => b => eq(
T[of](a)[alt](b),
T[of](a)
);

module.exports = {leftCatch};
18 changes: 18 additions & 0 deletions laws/monadPlus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

const {alt, chain} = require('..');

/**
### MonadPlus
1. `x.alt(y).chain(f)` is equivalent to `x.chain(f).alt(y.chain(f))` (distributivity)
**/

const distributivity = eq => x => y => f => eq(
x[alt](y)[chain](f),
x[chain](f)[alt](y[chain](f))
);

module.exports = {distributivity};
18 changes: 18 additions & 0 deletions laws/monadZero.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

const {pempty, chain} = require('..');

/**
### MonadZero
1. `M.pempty().chain(f)` is equivalent to `M.pempty()` (annihilation)
**/

const annihilation = T => eq => f => eq(
T[pempty]()[chain](f),
T[pempty]()
);

module.exports = {annihilation};
30 changes: 30 additions & 0 deletions laws/plus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

const {pempty, alt, map} = require('..');

/**
### Alt
1. `x.alt(A.pempty())` is equivalent to `x` (right identity)
2. `A.pempty().alt(x)` is equivalent to `x` (left identity)
2. `A.pempty().map(f)` is equivalent to `A.pempty()` (annihilation)
**/

const rightIdentity = T => eq => x => eq(
T[pempty]()[alt](x),
x
);

const leftIdentity = T => eq => x => eq(
x[alt](T[pempty]()),
x
);

const annihilation = T => eq => f => eq(
T[pempty]()[map](f),
T[pempty]()
);

module.exports = {rightIdentity, leftIdentity, annihilation};

0 comments on commit 876d09e

Please sign in to comment.