Search in sources :

Example 6 with Generalizer

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));
}
Also used : RuleSet(suite.lp.kb.RuleSet) Generalizer(suite.lp.doer.Generalizer) ProverConfig(suite.lp.Configuration.ProverConfig) ProverFactory(suite.lp.doer.ProverFactory) SewingProverImpl(suite.lp.sewing.impl.SewingProverImpl) Test(org.junit.Test)

Example 7 with Generalizer

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));
}
Also used : RuleSet(suite.lp.kb.RuleSet) Generalizer(suite.lp.doer.Generalizer) ProverConfig(suite.lp.Configuration.ProverConfig) ProverFactory(suite.lp.doer.ProverFactory) SewingProverImpl(suite.lp.sewing.impl.SewingProverImpl) Test(org.junit.Test)

Example 8 with Generalizer

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));
    });
}
Also used : Generalizer(suite.lp.doer.Generalizer) Trail(suite.lp.Trail) Prototype(suite.lp.kb.Prototype) Reference(suite.node.Reference) Node(suite.node.Node) ISet(suite.immutable.ISet) Atom(suite.node.Atom) Fun(suite.util.FunUtil.Fun)

Example 9 with Generalizer

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));
}
Also used : Generalizer(suite.lp.doer.Generalizer) Trail(suite.lp.Trail) Reference(suite.node.Reference) Node(suite.node.Node) ArrayList(java.util.ArrayList) Tree(suite.node.Tree) Pair(suite.adt.pair.Pair)

Example 10 with Generalizer

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();
}
Also used : Reference(suite.node.Reference) Suite(suite.Suite) Trail(suite.lp.Trail) Read(suite.streamlet.Read) Rule(suite.lp.kb.Rule) TermOp(suite.node.io.TermOp) HashMap(java.util.HashMap) Tree(suite.node.Tree) Node(suite.node.Node) Pair(suite.adt.pair.Pair) List(java.util.List) Atom(suite.node.Atom) Map(java.util.Map) Prototype(suite.lp.kb.Prototype) IdentityKey(suite.adt.IdentityKey) Binder(suite.lp.doer.Binder) TreeUtil(suite.node.util.TreeUtil) Generalizer(suite.lp.doer.Generalizer) Fail(suite.util.Fail) Dict(suite.node.Dict) Prototype(suite.lp.kb.Prototype) Generalizer(suite.lp.doer.Generalizer) HashMap(java.util.HashMap) Reference(suite.node.Reference) Node(suite.node.Node) Pair(suite.adt.pair.Pair)

Aggregations

Generalizer (suite.lp.doer.Generalizer)14 Node (suite.node.Node)11 Reference (suite.node.Reference)7 Trail (suite.lp.Trail)6 ArrayList (java.util.ArrayList)5 Atom (suite.node.Atom)5 Tree (suite.node.Tree)4 List (java.util.List)3 Suite (suite.Suite)3 Pair (suite.adt.pair.Pair)3 RuleSet (suite.lp.kb.RuleSet)3 Fun (suite.util.FunUtil.Fun)3 Test (org.junit.Test)2 ProverConfig (suite.lp.Configuration.ProverConfig)2 Binder (suite.lp.doer.Binder)2 ProverFactory (suite.lp.doer.ProverFactory)2 Prototype (suite.lp.kb.Prototype)2 SewingProverImpl (suite.lp.sewing.impl.SewingProverImpl)2 Dict (suite.node.Dict)2 TermOp (suite.node.io.TermOp)2