use of suite.node.Node in project suite by stupidsing.
the class ClonerFactoryTest method test.
private void test(String pattern, String match) {
for (ClonerFactory cf : new ClonerFactory[] { new CompileClonerImpl(), new SewingClonerImpl() }) {
Node node = new Generalizer().generalize(Suite.parse(pattern));
Clone_ p = cf.cloner(node);
assertTrue(Binder.bind(p.apply(cf.mapper().env()), Suite.parse(match), new Trail()));
}
}
use of suite.node.Node in project suite by stupidsing.
the class SymbolicTest method testCubic.
@Test
public void testCubic() {
verifySimplify("(a * x + b) ^ 3", //
"" + //
"(a * a * a) * x * x * x" + //
" + ((3 * a * a) * b) * x * x" + //
" + ((3 * a) * b * b) * x" + " + b * b * b");
verifySimplify("(a * x + neg b) ^ 3", //
"" + //
"(a * a * a) * x * x * x" + //
" + ((neg 3 * a * a) * b) * x * x" + //
" + ((3 * a) * b * b) * x" + " + neg 1 * b * b * b");
Node poly = //
rw.replace(//
x, //
Suite.parse("y + neg (b * inv (3 * a))"), Suite.parse("a * x * x * x + b * x * x + c * x + d"));
verifySimplify(poly, //
"" + //
"a * y * y * y" + //
" + (c + (neg inv 3 * inv a) * b * b) * y" + " + d + ((neg inv 3 * inv a) * b) * c + ((2 * inv 27) * inv (a * a)) * b * b * b");
}
use of suite.node.Node in project suite by stupidsing.
the class Chr method chrThen.
private Streamlet<State> chrThen(Streamlet<State> states, Node then) {
Generalizer generalizer = new Generalizer();
Atom a = atom(".a"), b = atom(".b");
if (Binder.bind(then, generalizer.generalize(Suite.substitute(".0 = .1", a, b)), new Trail())) {
// built-in syntactic equality
Reference from = generalizer.getVariable(a);
Reference to = generalizer.getVariable(b);
states = states.map(new Fun<>() {
public State apply(State state) {
IMap<Prototype, ISet<Node>> factsByPrototype1 = IMap.empty();
for (Pair<Prototype, ISet<Node>> e : state.factsByPrototype) factsByPrototype1 = factsByPrototype1.put(e.t0, replace(e.t1));
return new State(factsByPrototype1);
}
private ISet<Node> replace(ISet<Node> facts) {
ISet<Node> facts1 = ISet.empty();
for (Node node : facts) facts1 = facts1.replace(rw.replace(from, to, node));
return facts1;
}
});
}
return states.map(state -> {
Prototype prototype = Prototype.of(then);
ISet<Node> facts = getFacts(state, prototype);
return setFacts(state, prototype, facts.replace(then));
});
}
use of suite.node.Node in project suite by stupidsing.
the class Chr method chrGiven.
private Streamlet<State> chrGiven(Streamlet<State> states, Trail trail, Node given) {
Prototype prototype = Prototype.of(given);
return states.filter(state -> {
ISet<Node> facts = getFacts(state, prototype);
Predicate<Node> bindFun = bindFun(trail, given);
return facts.streamlet().isAny(bindFun);
});
}
use of suite.node.Node in project suite by stupidsing.
the class Chr method addRule.
public void addRule(Node node) {
Rule rule = new Rule();
while (node != Atom.of("end")) {
Tree t0 = Tree.decompose(node, TermOp.TUPLE_);
Tree t1 = t0 != null ? Tree.decompose(t0.getRight(), TermOp.TUPLE_) : null;
if (t1 != null) {
Node key = t0.getLeft();
Node value = t1.getLeft();
node = t1.getRight();
if (key == Atom.of("given"))
rule.givens = To.list(Tree.iter(value));
else if (key == Atom.of("if"))
rule.ifs = To.list(Tree.iter(value));
else if (key == Atom.of("then"))
rule.thens = To.list(Tree.iter(value));
else if (key == Atom.of("when"))
rule.when = value;
else
Fail.t("invalid key " + key);
} else
Fail.t("invalid rule " + node);
}
rules.add(rule);
}
Aggregations