use of suite.node.Str in project suite by stupidsing.
the class RecursiveFactorizerTest method operatorNode.
private Node operatorNode(Operator op, List<Node> nodes) {
Str s = new Str("");
Str name = new Str(op.toString());
return treeNode(() -> s, name, nodes);
}
use of suite.node.Str in project suite by stupidsing.
the class RecursiveFactorizerTest method terminalNode.
private Node terminalNode(String s) {
Dict dict = new Dict();
dict.map.put(Atom.of("chars"), Reference.of(new Str(s)));
return Tree.of(TermOp.COLON_, Atom.of(FTerminal.class.getName()), dict);
}
use of suite.node.Str in project suite by stupidsing.
the class Binder method bind.
public static boolean bind(Node n0, Node n1, Trail trail) {
n0 = n0.finalNode();
n1 = n1.finalNode();
if (n0 == n1)
return true;
Class<? extends Node> clazz0 = n0.getClass();
Class<? extends Node> clazz1 = n1.getClass();
if (clazz0 == Reference.class) {
trail.addBind((Reference) n0, n1);
return true;
} else if (clazz1 == Reference.class) {
trail.addBind((Reference) n1, n0);
return true;
}
if (clazz0 == Dict.class && clazz1 == Dict.class) {
Map<Node, Reference> map0 = ((Dict) n0).map;
Map<Node, Reference> map1 = ((Dict) n1).map;
boolean b = true;
for (Node key : List_.concat(map0.keySet(), map1.keySet())) {
Node v0 = map0.computeIfAbsent(key, k -> new Reference());
Node v1 = map1.computeIfAbsent(key, k -> new Reference());
b &= bind(v0, v1, trail);
}
return b;
} else if (clazz0 == Int.class && clazz1 == Int.class)
return ((Int) n0).number == ((Int) n1).number;
else if (clazz0 == Str.class && clazz1 == Str.class)
return Objects.equals(((Str) n0).value, ((Str) n1).value);
else if (Tree.class.isAssignableFrom(clazz0) && Tree.class.isAssignableFrom(clazz1)) {
Tree t0 = (Tree) n0;
Tree t1 = (Tree) n1;
return //
t0.getOperator() == t1.getOperator() && //
bind(t0.getLeft(), t1.getLeft(), trail) && bind(t0.getRight(), t1.getRight(), trail);
} else if (clazz0 == Tuple.class && clazz1 == Tuple.class) {
Node[] nodes0 = ((Tuple) n0).nodes;
Node[] nodes1 = ((Tuple) n1).nodes;
boolean b = nodes0.length == nodes1.length;
if (b) {
for (int i = 0; i < nodes0.length; i++) b &= bind(nodes0[i], nodes1[i], trail);
}
return b;
} else
return false;
}
use of suite.node.Str in project suite by stupidsing.
the class Grapher method load.
public void load(DataInputStream dis) throws IOException {
int size = dis.readInt();
id = dis.readInt();
for (int index = 0; index < size; index++) {
ReadType type = ReadType.of(dis.readByte());
Node terminal;
Operator op;
List<IntIntPair> children = new ArrayList<>();
if (type == ReadType.TERM) {
char ch = (char) dis.readByte();
switch(ch) {
case 'a':
terminal = Atom.of(dis.readUTF());
break;
case 'i':
terminal = Int.of(dis.readInt());
break;
case 'r':
terminal = new Reference();
break;
case 's':
terminal = new Str(dis.readUTF());
break;
default:
terminal = Fail.t("unknown type " + ch);
}
} else
terminal = null;
if (type == ReadType.TREE) {
op = TermOp.find(dis.readUTF());
children.add(IntIntPair.of(0, dis.readInt() + index));
children.add(IntIntPair.of(0, dis.readInt() + index));
} else
op = null;
if (type == ReadType.DICT || type == ReadType.TUPLE) {
int size1 = dis.readInt();
for (int i = 0; i < size1; i++) {
int i0 = type != ReadType.DICT ? 0 : dis.readInt() + index;
int i1 = dis.readInt() + index;
children.add(IntIntPair.of(i0, i1));
}
}
gns.add(new GN(type, terminal, op, children));
}
}
use of suite.node.Str in project suite by stupidsing.
the class FactorizeResult method rewrite.
public static FactorizeResult rewrite(FactorizeResult frfrom, FactorizeResult frto, FactorizeResult fr0) {
Generalizer generalizer = new Generalizer();
Iterate<Node> rewrite = n0 -> {
Node[] m = Suite.pattern(FTerminal.class.getName() + ":.0").match(n0);
Node n1 = m != null ? m[0] : null;
Node n2 = n1 instanceof Dict ? ((Dict) n1).map.get(Atom.of("chars")) : null;
Node n3 = n2 != null ? n2.finalNode() : null;
String s = n3 instanceof Str ? ((Str) n3).value : null;
boolean b = s != null && s.startsWith(ProverConstant.variablePrefix) && s.substring(1).matches("[0-9]*");
return b ? generalizer.generalize(Atom.of(s)) : n0;
};
Fun<FactorizeResult, Node> parse = fr -> rw.rewrite(rewrite, nodify.nodify(FNode.class, fr.node));
Node nodeFrom = parse.apply(frfrom);
Node nodeTo = parse.apply(frto);
FNode fn0 = fr0.node;
Node node0 = nodify.nodify(FNode.class, fn0);
Node nodex = rw.rewrite(nodeFrom, nodeTo, node0);
FNode fnx = nodify.unnodify(FNode.class, nodex);
return new FactorizeResult(fr0.pre, fnx, fr0.post);
}
Aggregations