Last active
May 12, 2021 13:20
-
-
Save Superstar64/996e2a4e62ff3ab4d8c6c6e494655355 to your computer and use it in GitHub Desktop.
Freer monad in javascript
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
/* Copyright Freddy A Cubas "superstar64" | |
Boost Software License - Version 1.0 - August 17th, 2003 | |
Permission is hereby granted, free of charge, to any person or organization | |
obtaining a copy of the software and accompanying documentation covered by | |
this license (the "Software") to use, reproduce, display, distribute, | |
execute, and transmit the Software, and to prepare derivative works of the | |
Software, and to permit third-parties to whom the Software is furnished to | |
do so, all subject to the following: | |
The copyright notices in the Software and this entire statement, including | |
the above license grant, this restriction and the following disclaimer, | |
must be included in all copies of the Software, in whole or in part, and | |
all derivative works of the Software, unless such copies or derivative | |
works are solely in the form of machine-executable object code generated by | |
a source language processor. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT | |
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE | |
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, | |
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
DEALINGS IN THE SOFTWARE. | |
*/ | |
// freer monads (copyible generators) | |
// based off http://okmij.org/ftp/Computation/free-monad.html | |
const pure = a => ({ id : 0, value : a }); | |
const impure = f => m => ({ id : 1, value : {now : m, after : f}}); | |
const match = (pure, impure) => m => [pure, ({now : m, after : f}) => impure(f)(m)][m.id](m.value); | |
const eta = impure(pure); | |
const rbind = k => match(k, k2 => impure(x => rbind(k)(k2(x)))); | |
const bind = m => f => rbind(f)(m); | |
const apply = f => x => bind(f)(f => bind(x)(x => pure(f(x)))); | |
const map = f => (...x) => x.reduce((f,x) => apply(f)(x), pure(f)); | |
const run = ({pure, rbind}) => match(pure, f => rbind(x => run({pure,rbind}) (f(x)))); | |
// dice example, trying out all possible cases | |
const list = { | |
pure : x => [x], | |
rbind : f => m => m.map(f).reduce((a,b) => a.concat(b), []) | |
}; | |
// fair dice roll | |
const dice = eta([1,2,3,4,5,6]); | |
const possible = map(a => b => c => a + b + c)( | |
dice, | |
dice, | |
dice | |
); | |
// sum of all possible triple dice rolls | |
console.log( run(list) (possible) ); | |
// context example, implicitly passing context around | |
const reader = { | |
pure : x => a => x, | |
rbind : f => m => a => f(m(a))(a) | |
}; | |
// get the context | |
const context = eta(a => a); | |
const square = map(x => x * x)(context); | |
const squareSum = map(a => b => a + b)( | |
square, | |
square | |
); | |
console.log ( run(reader) (squareSum) (10) ); | |
// identity, the do nothing monad | |
const identity = { | |
pure : x => ({wrapped : x}), | |
rbind : f => ({wrapped : x}) => f(x) | |
} | |
const wrapped_five = eta ( { wrapped : 5 } ); | |
const wrapped_six = map(x => x + 1)(wrapped_five); | |
console.log( run(identity) (wrapped_six).wrapped ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment