logic.js
JavaScript implementation of modified version of miniKanren.
Check this presentation.
Install
node.js
npm install logic_js --save
browser
where logic.js
is located at ./lib
folder.
Usages
Core
const lvar run and or eq = // or `logic` in browser const x = // define logic variable with id 'x' // query 'x' => [{x: 1}] // => [{x: 1}] // [{x: 1}, {x: 2}] // [{x: 1}]
Facts
const facts = // FACTconst parent = // Henry is Alice's parentconst x = // who is Alice's parent => ['Henry'] // who are Steve's children => ['Bob', 'Henry'] // RULEconst grandparent = { let z = return // x is z's parent and z is y's parent => x is y's parent} // who is Alice's grandparent => ['Steve']
Array manipulation
const conso firsto resto emptyo membero appendo = const x = y = // [{x: 1}, {x: 2}, {x: 3}] // [{x: 1, y: [2, 3]}] /*[ {x: [], y: [1, 2]}, {x: [1], y: [2]} {x: [1, 2], y: []} ]*/
Arithmetic & Comparison
const add sub mul div lt le gt ge = // [{x: 3}]
Extra
const succeed fail anyo = // [{x: 1}] // [] // [{x: 1}, {x: 2}, {x: 3}] // [{x: 1}, {x: 3}] // [{x: 1}, {x: 2}, {x: 3}, {x: 1}]
References
microKanren
microLogic
cKanren
aKanren
JavaScript stream
core.logic