Created
March 7, 2017 11:17
-
-
Save anonymous/f8154f2dd1a7099e7092fcc761dc0797 to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/jekebidewo
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
/* jshint esnext: true */ | |
'use strict'; | |
function LVar(id) { | |
return Symbol['for']('LVar' + id); | |
} | |
function isLVar(a) { | |
return typeof a === 'symbol' && /Symbol\(LVar[0-9]+\)/.test(a.toString()); | |
} | |
function addSubstitution(subMap, lVar, value) { | |
if (subMap instanceof Map) { | |
subMap.set(lVar, value); | |
return subMap; | |
} else { | |
return null; | |
} | |
} | |
var x = LVar(1); | |
var y = LVar(1); | |
console.log(x === y, y.valueOf(), 'i', isLVar(y)); | |
function walk(u, subMap) { | |
if (isLVar(u)) { | |
var value = subMap.get(u); | |
while (subMap.has(value)) value = subMap.get(value); | |
return value; | |
} else { | |
return u; | |
} | |
} | |
function unify(_u, _v, subMap) { | |
var u = walk(_u, subMap), | |
v = walk(_v, subMap); | |
//console.log('var', u, v) | |
if (u === v) return subMap;else if (isLVar(_u)) return addSubstitution(subMap, _u, v);else if (isLVar(_v)) return addSubstitution(subMap, _v, u);else return null; | |
} | |
var subMap = new Map([[LVar(0), LVar(1)], [LVar(1), 'banana']]); | |
console.log('SUBMAP:', walk("mango", subMap)); | |
console.log('UNIFY', Array.from(unify(LVar(0), "banana", new Map()).entries())[0][0].toString(), Array.from(unify('banana', LVar(0), new Map()).entries())[0][0].toString()); | |
console.log('UNI', Array.from(unify(LVar(0), "banana", new Map([[LVar(0), "mango"]])).entries())); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">/* jshint esnext: true */ | |
function LVar(id) { | |
return Symbol.for('LVar' + id) | |
} | |
function isLVar(a) { | |
return typeof a === 'symbol' | |
&& /Symbol\(LVar[0-9]+\)/.test(a.toString()) | |
} | |
function addSubstitution(subMap, lVar, value) { | |
if (subMap instanceof Map) { | |
subMap.set(lVar, value) | |
return subMap | |
} | |
else { | |
return null | |
} | |
} | |
var x = LVar(1) | |
var y = LVar(1) | |
console.log(x === y, y.valueOf(), 'i', isLVar(y)) | |
function walk(u, subMap) { | |
if (isLVar(u)) { | |
var value = subMap.get(u) | |
while (subMap.has(value)) | |
value = subMap.get(value) | |
return value | |
} | |
else { | |
return u | |
} | |
} | |
function unify(_u, _v, subMap) { | |
let u = walk(_u, subMap) | |
, v = walk(_v, subMap) | |
//console.log('var', u, v) | |
if (u === v) | |
return subMap | |
else if (isLVar(_u)) | |
return addSubstitution(subMap, _u, v) | |
else if (isLVar(_v)) | |
return addSubstitution(subMap, _v, u) | |
else | |
return null | |
} | |
var subMap = new Map([ [LVar(0), LVar(1)] | |
, [LVar(1), 'banana'] | |
]) | |
console.log('SUBMAP:', walk("mango", subMap)) | |
console.log('UNIFY', Array.from( unify(LVar(0), "banana", new Map()).entries() )[0][0].toString(), | |
Array.from( unify('banana', LVar(0), new Map()).entries() )[0][0].toString() | |
) | |
console.log('UNI', Array.from( unify(LVar(0), "banana", new Map([[LVar(0), "mango"]])).entries() ) )</script></body> | |
</html> |
This file contains hidden or 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
/* jshint esnext: true */ | |
'use strict'; | |
function LVar(id) { | |
return Symbol['for']('LVar' + id); | |
} | |
function isLVar(a) { | |
return typeof a === 'symbol' && /Symbol\(LVar[0-9]+\)/.test(a.toString()); | |
} | |
function addSubstitution(subMap, lVar, value) { | |
if (subMap instanceof Map) { | |
subMap.set(lVar, value); | |
return subMap; | |
} else { | |
return null; | |
} | |
} | |
var x = LVar(1); | |
var y = LVar(1); | |
console.log(x === y, y.valueOf(), 'i', isLVar(y)); | |
function walk(u, subMap) { | |
if (isLVar(u)) { | |
var value = subMap.get(u); | |
while (subMap.has(value)) value = subMap.get(value); | |
return value; | |
} else { | |
return u; | |
} | |
} | |
function unify(_u, _v, subMap) { | |
var u = walk(_u, subMap), | |
v = walk(_v, subMap); | |
//console.log('var', u, v) | |
if (u === v) return subMap;else if (isLVar(_u)) return addSubstitution(subMap, _u, v);else if (isLVar(_v)) return addSubstitution(subMap, _v, u);else return null; | |
} | |
var subMap = new Map([[LVar(0), LVar(1)], [LVar(1), 'banana']]); | |
console.log('SUBMAP:', walk("mango", subMap)); | |
console.log('UNIFY', Array.from(unify(LVar(0), "banana", new Map()).entries())[0][0].toString(), Array.from(unify('banana', LVar(0), new Map()).entries())[0][0].toString()); | |
console.log('UNI', Array.from(unify(LVar(0), "banana", new Map([[LVar(0), "mango"]])).entries())); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment