use of suite.node.Tuple in project suite by stupidsing.
the class CompileBinderImpl method binder.
public Bind_ binder(Node node) {
FunCreator<Bind_> fc = FunCreator.of(Bind_.class, false);
return fc.create(new BinOp<>() {
private FunExpr env, trail, b;
public FunExpr apply(FunExpr bindEnv, FunExpr target) {
this.env = bindEnv.field("env");
this.trail = bindEnv.field("trail");
return f.declare(ok, b -> {
this.b = b;
return compile_(node, target);
});
}
private FunExpr compile_(Node node, FunExpr target) {
FunExpr br = bind(f.object_(node, Node.class), target);
FunExpr brc = bindClone(node, target);
return new //
SwitchNode<FunExpr>(//
node).applyIf(Atom.class, n -> {
return f.ifEquals(target, f.object(node), ok, br);
}).applyIf(Int.class, n -> {
return f.ifInstance(Int.class, target, i -> f.ifEquals(i.field("number"), f.int_(n.number), ok, fail), br);
}).applyIf(Reference.class, n -> {
FunExpr ref = env.field("refs").index(f.int_(mapper().computeIndex(n)));
return f.invokeStatic(Binder.class, "bind", target, ref.cast_(Node.class), trail);
}).applyIf(Str.class, n -> {
return f.ifInstance(Str.class, target, s -> f.object(n.value).invoke("equals", s.field("value").cast_(Object.class)), br);
}).applyIf(Tree.class, tree -> {
return f.declare(f.invokeStatic(Tree.class, "decompose", target, f.object(tree.getOperator())), t -> {
Node lt = tree.getLeft();
Node rt = tree.getRight();
return f.ifNonNull(t, compile_(lt, t.invoke("getLeft"), compile_(rt, t.invoke("getRight"), ok)), brc);
});
}).applyIf(Tuple.class, n -> {
return f.ifInstance(Tuple.class, target, tuple -> f.declare(tuple.field("nodes"), targets -> {
Node[] nodes = n.nodes;
FunExpr fe = ok;
for (int i = 0; i < nodes.length; i++) fe = compile_(nodes[i], targets.index(f.int_(i)), fe);
return f.if_(targets.length(), fe, brc);
}), brc);
}).applyIf(Node.class, n -> {
Clone_ cloner = cloner(n);
return f.invokeStatic(Binder.class, "bind", target, f.object(cloner).invoke("apply", env), trail);
}).nonNullResult();
}
private FunExpr compile_(Node node, FunExpr target, FunExpr cps) {
return f.assign(b, compile_(node, target), f.if_(b, cps, fail));
}
private FunExpr bindClone(Node node, FunExpr target) {
if (isBindTrees)
return bind(f.object(cloner(node)).apply(env), target);
else
return fail;
}
private FunExpr bind(FunExpr node, FunExpr target) {
return f.ifInstanceAnd(Reference.class, target, ref -> f.seq(trail.invoke("addBind", ref, node), ok));
}
}).apply(Map.ofEntries());
}
use of suite.node.Tuple 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.Tuple 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.Tuple in project suite by stupidsing.
the class Formatter method format_.
private void format_(Node node, int parentPrec) {
new //
SwitchNode<Node>(//
node).doIf(Atom.class, n -> {
sb.append(quoteAtomIfRequired(n.name));
}).doIf(Data.class, n -> {
Object data = n.data;
if (data instanceof Chars)
sb.append("Chars<" + quoteStringIfRequired(data.toString()) + ">");
else if (data instanceof Node)
sb.append("Data<" + data.toString() + ">");
else
sb.append("Data<" + data.getClass().getSimpleName() + ">");
}).doIf(Dict.class, n -> {
sb.append("dict<");
for (Entry<Node, Reference> e : n.map.entrySet()) {
format(e.getKey(), TermOp.getLeftPrec(TermOp.AND___));
sb.append(":");
format(e.getValue(), TermOp.getLeftPrec(TermOp.AND___));
sb.append(",");
}
sb.append(">");
}).doIf(Int.class, n -> {
sb.append(n.number);
}).doIf(Reference.class, n -> {
sb.append(n.name());
}).doIf(Str.class, n -> {
sb.append(quoteStringIfRequired(n.value));
}).doIf(Tree.class, n -> {
formatTree(parentPrec, n);
}).doIf(Tuple.class, n -> {
sb.append("tuple<");
for (Node n_ : n.nodes) {
format(n_, TermOp.getLeftPrec(TermOp.AND___));
sb.append(", ");
}
sb.append(">");
}).doIf(Node.class, n -> {
sb.append(n.getClass().getSimpleName() + '@' + Integer.toHexString(System.identityHashCode(n)));
}).nonNullResult();
}
use of suite.node.Tuple in project suite by stupidsing.
the class Comparer method compare.
@Override
public int compare(Node n0, Node n1) {
n0 = n0.finalNode();
n1 = n1.finalNode();
Class<? extends Node> clazz0 = n0.getClass();
Class<? extends Node> clazz1 = n1.getClass();
int c = Integer.compare(order.get(clazz0), order.get(clazz1));
if (c == 0)
if (clazz0 == Atom.class)
return ((Atom) n0).name.compareTo(((Atom) n1).name);
else if (clazz0 == Dict.class) {
Map<Node, Reference> m0 = ((Dict) n0).map;
Map<Node, Reference> m1 = ((Dict) n1).map;
Set<Node> keys = new HashSet<>();
keys.addAll(m0.keySet());
keys.addAll(m1.keySet());
for (Node key : Read.from(keys).sort(this::compare)) c = c != 0 ? c : Object_.compare(m0.get(key), m1.get(key));
return c;
} else if (clazz0 == Int.class)
return Integer.compare(((Int) n0).number, ((Int) n1).number);
else if (clazz0 == Reference.class)
return Integer.compare(((Reference) n0).getId(), ((Reference) n1).getId());
else if (clazz0 == Str.class)
return ((Str) n0).value.compareTo(((Str) n1).value);
else if (Tree.class.isAssignableFrom(clazz0)) {
Tree t0 = (Tree) n0;
Tree t1 = (Tree) n1;
c = t0.getOperator().getPrecedence() - t1.getOperator().getPrecedence();
c = c != 0 ? c : compare(t0.getLeft(), t1.getLeft());
c = c != 0 ? c : compare(t0.getRight(), t1.getRight());
return c;
} else if (clazz0 == Tuple.class) {
Node[] nodes0 = ((Tuple) n0).nodes;
Node[] nodes1 = ((Tuple) n1).nodes;
int i = 0, l = min(nodes0.length, nodes1.length);
while (c == 0 && i < l) c = compare(nodes0[i], nodes1[i]);
if (c == 0)
c = Integer.compare(nodes0.length, nodes1.length);
return c;
} else
return Integer.compare(n0.hashCode(), n1.hashCode());
else
return c;
}
Aggregations