Skip to content
题叶 edited this page Jan 2, 2014 · 5 revisions

core api (dao.coffee/.js)


####solve

use this utlity to solve a dao expression. usage:

solve(exp, cont=done, failcont=faildone, state)

exp: the expression to be solved. cont: when solver succeed at last, cont is executed.

(v, solver) -> body # in coffeescript
javascript function(v, solver){ body } //in javascript

failcont: when solver fails at last, failcont is executed.

(v, solver) -> body # in coffeescript
function(v, solver){ body } //in javascript

state: the state of solver, mainly used for parsing


####solver

utility for make a new Solver usage:

solver(cont=done, failcont=faildone, state)

To solve an expression with the solver, just write the code like below:

solver.solve(exp, cont)

cont is optional, default to dao.done.


####class Solver

the solver for dao expression usage:

new Solver(failcont, state)
  constructor: (@failcont=faildone, @state) # coffeescript

failcont: fail continuation for backtracking in logic operation state: mainly used in parsing.


####class Var

Var for logic bindings, used in unify, lisp.assign, inc/dec, parser operation, etc.

#####vari

usage:

vari(name)

#####vars usage:

vars("name1 name2 ... ")

####class DummyVar extends Var

DummyVar never fail when it unify. see tests on any/some/times in test_parser for examples

#####dummy usage:

dummy(name)

#####dummies usage:

dummies("name1 name2 ... ")

####class Apply apply to a Command(special, fun, fun2, macro, proc, etc), or some other object which have applyCont method. usage:

new Apply(command, args)

class Command

dao command that can be applied Special, Fun, Macro, Proc is subclass of Command.

commandMaker

generate an instance of a Command definition:

commandMaker = (klass) -> (arity, name, fun) ->  klass(fun, name, arity)

####Speical

Speical knows solver and cont, with them the special function has full control of things. definition:

class Special extends Command

#####special

generate an instance of Special from a function, the first two parameter of which should be solver and cont definition:

special = commandMaker(Special)

usage: usage:

special(arity, name, func)
special(name, func)
special(arity, func)
special(func)

example:

# coffeescript
begin = special(null, 'begin', (solver, cont, exps...) -> solver.expsCont(exps, cont))
//javascript
exports.begin = special(null, 'begin', function() {
  var cont, exps, solver;
  solver = arguments[0], cont = arguments[1], exps = 3 <= arguments.length ? __slice.call(arguments, 2) : []
  return solver.expsCont(exps, cont);
});
exports.fail = special(0, 'fail', (solver, cont) -> (v, solver) -> solver.failcont(v, solver))()
// javascript
exports.fail = special(0, 'fail', function(solver, cont) {
  return function(v, solver) {
    return solver.failcont(v, solver);
  };
})();

####call

the special "call" call a goal with args usage:

call(goal, args...)

####apply

the special "apply" apply a goal with args usage:

apply(goal, args)

####Fun

Fun evaluate its arguments, and return the result to fun(params...) to cont directly. definition:

class Fun extends Command

#####fun

generate an instance of Fun from a function definition:

fun = commandMaker(Fun)

usage:

fun(arity, name, func)
fun(name, func)
fun(arity, func)
fun(func)

example:

add = fun((x, y) -> x+y ) # coffeescript
  add = fun(function(x,y){ return x+y; } // javascript

####Fun2

Fun2 evaluate its arguments, and evaluate the result of fun(params...) again definition:

class Fun2 extends Command

#####fun2

generate a instance of Fun2 from a function definition:

fun2 = commandMaker(Fun2)

usage: usage:

fun2(arity, name, func)
fun2(name, func)
fun2(arity, func)
fun2(func)

####Macro similar to lisp'macro, Macro does NOT evaluate its arguments, but evaluate the result to fun(args). definition: Macro = class Macro extends Command

#####macro generate a instance of Macro from a function definition:

macro = commandMaker(Macro)

usage:

macro(arity, name, func)
macro(name, func)
macro(arity, func)
macro(func)

example:

orpm = fun((x, y) -> orp(x,y ) # coffeescript
orpm = fun(function(x,y){  return orp(x,y ); } // javascript

####Proc

In Proc's function, the dao's expressions can be directly evaluated definition:

class Proc extends Command

#####proc

generate a instance of Proc from a function usage:

proc(arity, name, func)
proc(name, func)
proc(arity, func)
proc(func)

####tofun

evaluate the arguments of a command before execute it . with tofun, Special and Macro can behaviour like a Fun. cmd can be an instance of subclass of Command, especially macro(macro don't eval its arguments) and specials that don't eval their arguments. usage:

tofun(name, cmd)

####class UObject make a object unifiable, by wrapping it in a object which have unify and getvalue method. usage:

new UObject(obj)

####uobject make unifable object definition:

uobject = (x) -> new UObject(x)

usage:

uobject(obj)

####class UArray usage:

new UArray(arr)

####uarray make an array unifable, by wrapping it in a object which have unify and getvalue method. definition:

uarray = (x) -> new UArray(x)

usage:

uarray(arr)

####unifiable

make unifiable array or unifiable object

unifiable(x)

####class Cons

cons behaviours like cons in lisp, and it is a convinient utility in logic rule too. see samples/kleene.coffee for use cases. usage:

new Cons(x, y)

cons

make a new Cons definition:

cons = (x, y)-> new Cons(x, y)

usage:

cons(x, y)

####conslist

conslist behaviours like list in lisp usage:

conslist(args...)

####solver status

solver's status is set to UNKNOWN when start to solve, if solver successfully run to solver'last continuation, status is set SUCCESS, else, solver should run to solver's last failcont(default value is faildone), and the status is set to FAIL.

SUCCESS = 1
UNKNOWN = 0
FAIL = -1

Clone this wiki locally