use of suite.node.Reference in project suite by stupidsing.
the class Prototype method of.
public static Prototype of(Node node) {
Tree t0, t1;
while ((t1 = decompose(node)) != null) {
t0 = t1;
node = t0.getLeft();
}
boolean indexable = !ProverConstant.isVariant(node) && !(node instanceof Reference);
return indexable ? new Prototype(node) : null;
}
use of suite.node.Reference 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.Reference 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.Reference 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.Reference in project suite by stupidsing.
the class Grapher method bind.
public static boolean bind(Node n0, Node n1, Trail trail) {
Map<IdentityKey<Node>, Integer> mapn0 = new HashMap<>();
Map<IdentityKey<Node>, Integer> mapn1 = new HashMap<>();
Grapher g0 = new Grapher();
Grapher g1 = new Grapher();
g0.id = g0.graph_(mapn0, n0);
g1.id = g1.graph_(mapn1, n1);
IntObjMap<IdentityKey<Node>> mapi0 = new IntObjMap<>();
IntObjMap<IdentityKey<Node>> mapi1 = new IntObjMap<>();
for (Entry<IdentityKey<Node>, Integer> e : mapn0.entrySet()) mapi0.put(e.getValue(), e.getKey());
for (Entry<IdentityKey<Node>, Integer> e : mapn1.entrySet()) mapi1.put(e.getValue(), e.getKey());
Set<IntIntPair> set = new HashSet<>();
Deque<IntIntPair> deque = new ArrayDeque<>();
deque.add(IntIntPair.of(g0.id, g1.id));
IntIntPair pair;
while ((pair = deque.pollLast()) != null) if (set.add(pair)) {
GN gn0 = g0.gns.get(pair.t0);
GN gn1 = g1.gns.get(pair.t1);
if (//
gn0.type == ReadType.TERM && //
gn0.terminal instanceof Reference && Binder.bind(gn0.terminal, mapi1.get(pair.t1).key, trail))
;
else if (//
gn1.type == ReadType.TERM && //
gn1.terminal instanceof Reference && Binder.bind(gn1.terminal, mapi0.get(pair.t0).key, trail))
;
else if (gn0.type == gn1.type && Objects.equals(gn0.terminal, gn1.terminal) && gn0.op == gn1.op) {
List<IntIntPair> children0 = gn0.children;
List<IntIntPair> children1 = gn1.children;
int size0 = children0.size();
int size1 = children1.size();
if (size0 == size1)
for (int i = 0; i < size0; i++) {
IntIntPair p0 = children0.get(i);
IntIntPair p1 = children1.get(i);
deque.addLast(IntIntPair.of(p0.t0, p1.t0));
deque.addLast(IntIntPair.of(p0.t1, p1.t1));
}
else
return false;
} else
return false;
}
return true;
}
Aggregations