use of suite.node.Node in project suite by stupidsing.
the class SewingProverImpl method compileTrRules.
private Trampoline compileTrRules(Prototype prototype, List<Rule> rules, TraceLevel traceLevel) {
Streamlet<Trampoline> trs = Read.from(rules).map(rule -> {
Generalizer generalizer = new Generalizer();
Node head = generalizer.generalize(rule.head);
Node tail = generalizer.generalize(rule.tail);
return compileTrRule(head, tail);
});
Trampoline tr0 = orTr(trs);
Trampoline tr1 = cutBegin(tr0);
Trampoline tr2 = saveEnvTr(tr1);
return log(tr2, traceLevel);
}
use of suite.node.Node in project suite by stupidsing.
the class SewingProverImpl method traceLevel.
private TraceLevel traceLevel(Prototype prototype) {
TraceLevel traceLevel;
if (Suite.isProverTrace) {
Node head = prototype.head;
String name = head instanceof Atom ? ((Atom) head).name : null;
traceLevel = //
name != null && //
!name.startsWith("member") && !name.startsWith("rbt-") ? TraceLevel.TRACE : TraceLevel.NONE;
} else
traceLevel = TraceLevel.NONE;
return traceLevel;
}
use of suite.node.Node in project suite by stupidsing.
the class SewingProverImpl method compileCps.
private Cps compileCps(BinderFactory bf, Node node, Cps cpsx) {
List<Node> list;
Tree tree;
Node[] m;
Cps cps;
if (1 < (list = TreeUtil.breakdown(TermOp.AND___, node)).size()) {
cps = cpsx;
for (Node n : List_.reverse(list)) cps = compileCps(bf, n, cps);
} else if (1 < (list = TreeUtil.breakdown(TermOp.OR____, node)).size())
cps = orCps(Read.from(list).map(n -> compileCps(bf, n, cpsx)));
else if ((m = Suite.pattern(".0 = .1").match(node)) != null) {
boolean b = complexity(m[0]) <= complexity(m[1]);
Node n0 = b ? m[0] : m[1];
Node n1 = b ? m[1] : m[0];
Bind_ p = bf.binder(n1);
Clone_ f = bf.cloner(n0);
cps = rt -> p.test(rt, f.apply(rt.env)) ? cpsx : null;
} else if ((m = Suite.pattern(".0 .1").match(node)) != null && m[0] instanceof Atom)
cps = compileCpsCallPredicate(bf, ((Atom) m[0]).name, m[1], node, cpsx);
else if (node instanceof Atom) {
String name = ((Atom) node).name;
if (String_.equals(name, ""))
cps = cpsx;
else if (String_.equals(name, "fail"))
cps = rt -> null;
else
cps = compileCpsCallPredicate(bf, name, Atom.NIL, node, cpsx);
} else if (node instanceof Reference) {
Clone_ f = bf.cloner(node);
cps = rt -> compileCps(passThru, f.apply(rt.env), cpsx);
} else if ((tree = Tree.decompose(node)) != null)
cps = compileCpsCallPredicate(bf, tree.getOperator().getName(), node, node, cpsx);
else if (node instanceof Tuple)
cps = compileCpsCallPredicate(bf, node, cpsx);
else
cps = Fail.t("cannot understand " + node);
return cps;
}
use of suite.node.Node in project suite by stupidsing.
the class SewingProverImpl method compileTrCallPredicate.
private Trampoline compileTrCallPredicate(BinderFactory bf, Node node) {
Prototype prototype = Prototype.of(node);
if (rules.containsKey(prototype)) {
Clone_ f = bf.cloner(node);
Trampoline tr;
if (isHasCutByPrototype.get(prototype)) {
Mutable<Trampoline> mtr = getTrampolineByPrototype(prototype);
tr = rt -> {
rt.query = f.apply(rt.env);
return mtr.get()::prove;
};
} else {
Mutable<Cps> mcps = getCpsByPrototype(prototype);
Cps cpsx = rt -> {
IList<Trampoline> rems = rt.rems;
rt.rems = IList.cons(fail, IList.end());
new Runtime(rt, rt_ -> {
rt_.rems = rems;
return okay;
}).trampoline();
return null;
};
tr = rt -> {
Cps cps0 = rt.cps;
rt.cps = rt_ -> {
rt.cps = cps0;
return cpsx;
};
rt.query = f.apply(rt.env);
rt.cont(mcps.get());
return fail;
};
}
return tr;
} else
return Fail.t("cannot find predicate " + prototype);
}
use of suite.node.Node in project suite by stupidsing.
the class DivisiblePolynomial method format.
public Node format(Poly<N> poly) {
Express ex = new Express();
OpGroup add = ex.add;
OpGroup mul = ex.mul;
Int_Obj<Node> powerFun = p -> {
Node power = mul.identity();
for (int i = 0; i < p; i++) power = mul.apply(x, power);
return power;
};
Node sum = format_.apply(n0);
for (IntObjPair<N> pair : poly.streamlet().sortByKey(Integer::compare)) {
int p = pair.t0;
Node power = p < 0 ? mul.inverse(powerFun.apply(-p)) : powerFun.apply(p);
sum = add.apply(mul.apply(format_.apply(pair.t1), power), sum);
}
return sum;
}
Aggregations