use of suite.lp.doer.Generalizer in project suite by stupidsing.
the class SewingProverTest method testBacktrack.
@Test
public void testBacktrack() {
RuleSet rs = Suite.newRuleSet();
Suite.addRule(rs, "mem (.e, _) .e");
Suite.addRule(rs, "mem (_, .tail) .e :- mem .tail .e");
Suite.addRule(rs, "q .c .v :- once (mem (0,) .v), .a/.b/.c = 0/0/0; mem (1,) .v, .a/.b/.c = 1/1/1");
Suite.addRule(rs, "r .c :- q .c .v, .v = 1");
ProverFactory sp = new SewingProverImpl(rs);
ProverConfig pc = new ProverConfig(rs);
assertTrue(sp.prover(new Generalizer().generalize(Suite.parse("r .c"))).test(pc));
}
use of suite.lp.doer.Generalizer in project suite by stupidsing.
the class SewingProverTest method test.
@Test
public void test() {
RuleSet rs = Suite.newRuleSet();
Suite.addRule(rs, "yes");
ProverFactory sp = new SewingProverImpl(rs);
ProverConfig pc = new ProverConfig(rs);
assertTrue(sp.prover(Suite.parse("yes")).test(pc));
assertTrue(sp.prover(Suite.parse("fail; yes")).test(pc));
assertTrue(sp.prover(Suite.parse("yes; yes")).test(pc));
assertFalse(sp.prover(Suite.parse("yes, fail")).test(pc));
assertFalse(sp.prover(Suite.parse("!, fail; yes")).test(pc));
assertTrue(sp.prover(new Generalizer().generalize(Suite.parse("(.v = 1; .v = 2), .v = 2"))).test(pc));
assertFalse(sp.prover(new Generalizer().generalize(Suite.parse("once (.v = 1; .v = 2), .v = 2"))).test(pc));
assertFalse(sp.prover(Suite.parse("not yes")).test(pc));
assertTrue(sp.prover(Suite.parse("not fail")).test(pc));
assertTrue(sp.prover(new Generalizer().generalize(Suite.parse(".p = yes, .p"))).test(pc));
}
use of suite.lp.doer.Generalizer 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.lp.doer.Generalizer in project suite by stupidsing.
the class Assembler method assemble.
public Bytes assemble(Node input) {
Generalizer generalizer = new Generalizer();
Trail trail = new Trail();
List<Pair<Reference, Node>> lnis = new ArrayList<>();
for (Node node0 : Tree.iter(input)) {
Node node = generalizer.generalize(node0);
Tree tree;
if ((tree = Tree.decompose(node, TermOp.EQUAL_)) != null)
if (!Binder.bind(tree.getLeft(), tree.getRight(), trail))
Fail.t("bind failed");
else
;
else if ((tree = Tree.decompose(node, TermOp.TUPLE_)) != null)
lnis.add(Pair.of((Reference) tree.getLeft(), tree.getRight()));
else
Fail.t("cannot assemble " + node);
}
return assemble(generalizer, preassemble.apply(lnis));
}
use of suite.lp.doer.Generalizer in project suite by stupidsing.
the class TypeChecker method check.
public void check(List<Rule> rules) {
Map<Prototype, Integer> nElementsByPrototype = checkerUtil.getNumberOfElements(rules);
Map<Pair<Prototype, Integer>, Reference> types = new HashMap<>();
Read.from(rules).concatMap(rule -> {
Generalizer generalizer = new Generalizer();
Node head = generalizer.generalize(rule.head);
Node tail = generalizer.generalize(rule.tail);
return checkerUtil.scan(tail).cons(head);
}).forEach(pred -> {
Prototype prototype = Prototype.of(pred);
Integer nElements = prototype != null ? nElementsByPrototype.get(prototype) : null;
Node[] ps = nElements != null ? TreeUtil.elements(pred, nElements) : new Node[0];
try {
if (nElements != null)
for (int i = 1; i < nElements; i++) {
Pair<Prototype, Integer> key = Pair.of(prototype, i);
Node p = ps[i];
Node type0 = types.computeIfAbsent(key, k -> new Reference());
Node type1 = getType(p);
bind(type0, type1);
}
} catch (Exception ex) {
Fail.t("in predicate " + prototype, ex);
}
});
trail.unwindAll();
}
Aggregations