-
Notifications
You must be signed in to change notification settings - Fork 377
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; |