Last active
May 5, 2021 01:10
-
-
Save Superstar64/d9a9713d53929b2706720e5cf51ad1a8 to your computer and use it in GitHub Desktop.
lambda calculus interpreter 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. | |
*/ | |
const isAlpha = c => c.toUpperCase() != c.toLowerCase(); // ugly hack | |
const leftAssociative = f => (...xs) => xs.reduce(f); | |
const empty = () => code => null; | |
// infinite backtracing | |
const or = leftAssociative((left, right) => () => code => left()(code) || right()(code)); | |
const pure = item => () => code => ({ just: [item, code] }); | |
const map = (f, ...x) => apply(pure(f), ...x); | |
const apply = leftAssociative((f, x) => () => code => { | |
const monadF = f()(code); | |
if (monadF) { | |
const monadX = x()(monadF.just[1]); | |
if (monadX) { | |
return { just: [monadF.just[0](monadX.just[0]), monadX.just[1]] }; | |
} | |
} | |
return null; | |
}); | |
const cons = x => xs => [x].concat(xs); | |
const many = x => () => or(map(cons, x, many(x)), pure([]))(); | |
const some = x => map(cons, x, many(x)); | |
const satify = check => () => code => { | |
if (code.length > 0 && check(code[0])) { | |
return { just: [code[0], code.slice(1)] }; | |
} else { | |
return null; | |
} | |
}; | |
const letter = satify(isAlpha); | |
const literal = x => satify(a => a == x); | |
const space = many(literal(' ')); | |
const identifier = map(x => x.reduce((a, b) => a + b, ''), some(letter)); | |
const term = () => map(x => x.reduce((f, x) => env => f(env)(x(env))), some(termCore))(); | |
const lambda = map(_ => _ => name => _ => _ => _ => e => env => x => e(augment(env, name, x)), | |
literal('|'), | |
space, | |
identifier, | |
space, | |
literal('|'), | |
space, | |
term | |
); | |
const constiable = map(x => _ => env => env[x], identifier, space); | |
const parens = map(_ => _ => e => _ => _ => _ => e, | |
literal('('), | |
space, | |
term, | |
space, | |
literal(')'), | |
space | |
); | |
const termCore = or(lambda, constiable, parens); | |
const augment = (env, name, x) => { | |
const env2 = Object.create(env); | |
env2[name] = x; | |
return env2; | |
}; | |
const prelude = { | |
two: 2, | |
four: 4, | |
neg: x => -x, | |
add: x => y => x + y, | |
sub: x => y => x - y, | |
mul: x => y => x * y, | |
div: x => y => x / y, | |
sqrt: Math.sqrt | |
}; | |
const run = string => term()(string).just[0](prelude); | |
// quadratic formula | |
const quad = run("|a| |b| |c| div (add (neg b) (sqrt ( sub (mul b b) (mul four (mul a c))))) (mul two a)"); | |
print(quad(2)(4)(-20)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment