use of suite.node.Tree in project suite by stupidsing.
the class FindPredicates method elaborate.
private Stack<Node> elaborate(Prover prover, Node var, Node goal) {
Stack<Node> stack = new Stack<>();
Tree subGoal = Tree.of(TermOp.AND___, goal, new Data<Source<Boolean>>(() -> {
stack.push(new Cloner().clone(var));
return Boolean.FALSE;
}));
new Prover(prover).elaborate(subGoal);
return stack;
}
use of suite.node.Tree in project suite by stupidsing.
the class SystemPredicates method call.
public Boolean call(Node query) {
BuiltinPredicate predicate;
Tree tree;
Operator op;
Node left;
String name = null;
Node pass = null;
if (query instanceof Atom) {
name = ((Atom) query).name;
pass = Atom.NIL;
} else if ((tree = Tree.decompose(query)) != null)
if ((op = tree.getOperator()) != TermOp.TUPLE_) {
name = op.getName();
pass = query;
} else if ((left = tree.getLeft()) instanceof Atom) {
name = ((Atom) left).name;
pass = tree.getRight();
}
predicate = name != null ? get(name) : null;
return predicate != null ? predicate.prove(prover, pass) : null;
}
use of suite.node.Tree in project suite by stupidsing.
the class SewingExpressionImpl method evaluator.
public Evaluate_ evaluator(Node node) {
Tree tree = Tree.decompose(node);
if (tree != null) {
Operator op = tree.getOperator();
Evaluate_ lhs, rhs;
IntInt_Int fun;
if (op == TermOp.TUPLE_) {
Tree rightTree = Tree.decompose(tree.getRight());
lhs = evaluator(tree.getLeft());
rhs = evaluator(rightTree.getRight());
fun = TreeUtil.evaluateOp(rightTree.getLeft());
} else {
lhs = evaluator(tree.getLeft());
rhs = evaluator(tree.getRight());
fun = TreeUtil.evaluateOp(op);
}
return env -> fun.apply(lhs.evaluate(env), rhs.evaluate(env));
} else if (node instanceof Int) {
int i = ((Int) node).number;
return env -> i;
} else {
Clone_ f = clonerFactory.cloner(node);
return env -> TreeUtil.evaluate(f.apply(env));
}
}
use of suite.node.Tree in project suite by stupidsing.
the class SewingGeneralizerImpl method generalizer.
@Override
public Generalize_ generalizer(Node node) {
List<Generalize_> funs = new ArrayList<>();
Generalize_ fun;
while (true) {
Node node0 = node;
Tree tree;
if (node0 instanceof Atom) {
Atom atom = (Atom) node0;
String name = atom.name;
if (ProverConstant.isCut(node0) || ProverConstant.isVariable(name)) {
int index = vm.computeIndex(atom);
fun = env -> env.get(index);
} else if (ProverConstant.isWildcard(name))
fun = env -> new Reference();
else
fun = env -> node0;
} else if (node0 instanceof Dict) {
Generalize_[][] array = //
Read.from2(//
((Dict) node0).map).map(//
(key, value) -> new Generalize_[] { generalizer(key), generalizer(value) }).toArray(Generalize_[].class);
int length = array.length;
fun = env -> {
@SuppressWarnings("unchecked") Pair<Node, Reference>[] pairs = new Pair[length];
for (int i = 0; i < length; i++) pairs[i] = Pair.of(array[i][0].apply(env), Reference.of(array[i][1].apply(env)));
return Dict.of(pairs);
};
} else if ((tree = Tree.decompose(node0)) != null) {
Operator operator = tree.getOperator();
if (operator != TermOp.OR____) {
Generalize_ f = generalizer(tree.getLeft());
funs.add(env -> Tree.of(operator, f.apply(env), null));
node = tree.getRight();
continue;
} else {
// delay generalizing for performance
Generalize_ lf = generalizer(tree.getLeft());
Generalize_ rf = generalizer(tree.getRight());
fun = env -> Tree.of(operator, lf.apply(env), new Suspend(() -> rf.apply(env)));
}
} else if (node0 instanceof Tuple) {
Generalize_[] fs = Read.from(((Tuple) node0).nodes).map(this::generalizer).toArray(Generalize_.class);
int length = fs.length;
fun = env -> {
Node[] array = new Node[length];
for (int i = 0; i < length; i++) array[i] = fs[i].apply(env);
return Tuple.of(array);
};
} else
fun = env -> node0;
funs.add(fun);
break;
}
if (1 < funs.size())
return env -> {
Tree t = Tree.of(null, null, null);
Node node_ = t;
for (Generalize_ fun_ : funs) {
Tree t_ = Tree.decompose(node_);
Tree.forceSetRight(t_, fun_.apply(env));
node_ = t_.getRight();
}
return t.getRight();
};
else
return funs.get(0);
}
use of suite.node.Tree 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;
}
Aggregations