Nodejs daonode: 一个函数式逻辑求解器
Nodejs daonode: 一个函数式逻辑求解器
我发布了daonode,可通过npm安装,版本库在https://github.com/chaosim/daonode 安装daonode: npm install daonode
Dao是一个函数式逻辑求解器, 具有统一代码和数据,语法和程序,逻辑与函数,编译与执行的能力。 Daonode是原python项目daot用coffescript移植、重写后的升级版本。因为coffeescript可编译到javascript,因此daonode同时也直接就是javascript。 当lisp在javascript遇见prolog,世界将会怎样?
一些例子 (摘录的用coffeescript写的daonode测试代码):
"test builtin function": (test) ->
add = fun(2, (x, y) -> x+y)
test.equal solve(add(1, add(1,1)), 3
“test macro”: (test) ->
orpm = macro((x, y) -> orp(x, y))
test.equal solve(orpm(fail, print_(2))), null
“test macro tofun”: (test) ->
orpm = macro(2, (x, y) -> orp(x, y))
test.equal solve(orpm(print_(1), print_(2))), null
test.equal solve(tofun(orpm)(print_(1), print_(2))), null
test.equal solve(tofun(orpm)(quote(print_(1)), quote(print_(2)))), null
“test proc,aka online function in dao”: (test) ->
a = proc(0, ‘a’, () ->
i = 0
add(1, 2))
test.equal solve(a()), 3
“test eval_ quote”: (test) ->
exp = if_(1, 2, 3)
test.equal solve(quote(exp)), exp
test.equal solve(eval_(quote(exp))), 2
"test lisp style catch/throw ": (test) ->
test.equal solve(catch_(1, 2)), 2
test.equal solve(catch_(1, throw_(1, 2), 3)), 2
test.equal(solve(block(‘foo’, protect(break_(‘foo’, 1), print_(2)))), 1)
test.equal(solve(block(‘foo’, protect(break_(‘foo’, 1), print_(2), print_(3)))), 1)
“test callcc2”: (test) ->
a = null
solve(begin(callcc((k) -> a = k), add(1, 2)))
test.equal a(null), 3
“test callfc”: (test) ->
a = null
x = vari(‘x’)
x.binding = 5
solve(orp(callfc((k) -> a = k), add(x, 2)))
test.equal a(null), 7
“test quasiquote”: (test) ->
test.equal solve(qq(1)), 1
a = add(1, 2)
test.deepEqual solve(qq(a)), a
test.deepEqual solve(qq(uq(a))), 3
test.deepEqual solve(qq(uqs([1,2]))), new UnquoteSliceValue([1,2])
test.deepEqual solve(qq(add(uqs([1,2])))), a
“test block break continue”: (test) ->
test.equal solve(block(‘a’, 1)), 1
test.equal solve(block(‘a’, break_(‘a’, 2), 1)), 2
test.equal solve(block(‘a’, block(‘b’, break_(‘b’, 2), 1), 3)), 3
a = vari(‘a’)
test.equal solve(begin(assign(a, 1), block(‘a’, if_(eq(a, 5), break_(‘a’, a)), inc(a), continue_(‘a’)))), 5
“test logic predicate”: (test) ->
test.equal(solve(andp(unify(a, 1))), true)
test.equal (solve(andp(unify(a, 1), unify(a, 2))), false)
test.equal solve(orp(andp(unify(a, 1), unify(a, 2)), unify(a, 2))), true
test.equal (solve(orp(findall(orp(print_(1), print_(2))),)
print_(3))), null
test.equal (solve(findall(once(orp(print_(1), (print_(2)))))), null)
“test logic rule”: (test) ->
r = rule(2, (x, y) ->
[[x,y], 1, null])
test.equal solve(r(1,1)), 1
“test parser builtins”: (test) ->
test.deepEqual (solve(begin(settext(‘ab’), any(char(), result, ), eoi, result)), [‘a’, ‘b’])
test.equal (solve(parsetext(begin(some(char()), char(‘c’), eoi), ‘abc’)), true)
test.deepEqual (solve(begin(parsetext(seplist(char(), {sep:char(’,’), times:n, result:result, template:}), ‘a,b,c’), result)), [‘a’, ‘b’,‘c’])
test.deepEqual (solve(begin(parsetext(andp(seplist(char(), {sep:char(’,’), times:n, result:result, template:_}), char(’,’), char(‘b’), char(’,’), char(‘c’)),
‘a,b,c’), result)), [‘a’])
Nodejs daonode: 一个函数式逻辑求解器
我发布了daonode,可以通过npm安装。该项目的版本库位于https://github.com/chaosim/daonode。
安装daonode:
npm install daonode
Dao是什么?
Dao 是一个函数式逻辑求解器,它具有统一代码和数据、语法和程序、逻辑与函数、编译与执行的能力。Daonode 是原 Python 项目 daot 用 CoffeeScript 移植、重写后的升级版本。由于 CoffeeScript 可以编译成 JavaScript,因此 Daonode 同时也是纯 JavaScript 实现的。
想象一下,如果 Lisp 在 JavaScript 中遇到了 Prolog,会带来怎样的变革?
示例代码
以下是一些 Daonode 的示例代码,展示了其强大的功能:
const { test } = require('daonode');
// 测试内置函数
test("test builtin function", (test) => {
const add = (x, y) => x + y;
test.equal(solve(add(1, add(1, 1))), 3);
});
// 测试宏
test("test macro", (test) => {
const orpm = macro((x, y) => orp(x, y));
test.equal(solve(orpm(fail, print_(2))), null);
});
// 测试过程(即在线函数)
test("test proc, aka online function in dao", (test) => {
const a = proc(0, 'a', () => {
let i = 0;
return add(1, 2);
});
test.equal(solve(a()), 3);
});
// 测试 eval 和 quote
test("test eval_ quote", (test) => {
const exp = if_(1, 2, 3);
test.equal(solve(quote(exp)), exp);
test.equal(solve(eval_(quote(exp))), 2);
});
// 测试 Lisp 风格的 catch/throw
test("test lisp style catch/throw", (test) => {
test.equal(solve(catch_(1, 2)), 2);
test.equal(solve(catch_(1, throw_(1, 2), 3)), 2);
test.equal(solve(block('foo', protect(break_('foo', 1), print_(2)))), 1);
test.equal(solve(block('foo', protect(break_('foo', 1), print_(2), print_(3)))), 1);
});
这些示例展示了 Daonode 如何处理各种复杂的逻辑操作,包括内置函数、宏定义、过程调用、eval 和 quote 等。通过这些示例,你可以更好地理解 Daonode 的强大功能及其在函数式编程中的应用。
daonode
是一个基于JavaScript实现的函数式逻辑求解器,它将Python项目daot
移植到了Node.js平台上,并使用CoffeeScript进行了重写。这个库使得开发者能够在JavaScript环境中使用类似Lisp和Prolog的编程风格。
下面是一些简单的使用示例:
示例代码
安装
首先需要通过npm安装daonode
:
npm install daonode
使用内置函数
const dao = require('daonode');
let add = dao.fun(2, (x, y) => x + y);
console.log(dao.solve(add(1, add(1, 1)), 3)); // 输出:true
使用宏定义
const dao = require('daonode');
let orpm = dao.macro((x, y) => dao.orp(x, y));
console.log(dao.solve(orpm(dao.fail(), dao.print_(2))) === null); // 输出:true
使用过程(Proc)
const dao = require('daonode');
let a = dao.proc(0, 'a', () => {
let i = 0;
return dao.add(1, 2);
});
console.log(dao.solve(a()) === 3); // 输出:true
使用eval_ 和 quote
const dao = require('daonode');
let exp = dao.if_(1, 2, 3);
console.log(dao.solve(dao.quote(exp)) === exp); // 输出:true
console.log(dao.solve(dao.eval_(dao.quote(exp))) === 2); // 输出:true
使用catch/throw
const dao = require('daonode');
console.log(dao.solve(dao.catch_(1, 2)) === 2); // 输出:true
console.log(dao.solve(dao.catch_(1, dao.throw_(1, 2), 3)) === 2); // 输出:true
解释
dao.fun
定义了一个函数。dao.macro
创建了一个宏,用于在编译时处理代码。dao.proc
定义了一个可以在线执行的函数。dao.eval_
和dao.quote
分别用于评估和引用表达式。dao.catch_
和dao.throw_
实现了异常处理机制。
这些功能展示了daonode
的强大能力,使其成为编写复杂逻辑求解应用的有力工具。