Search in sources :

Example 6 with Prototype

use of suite.lp.kb.Prototype in project suite by stupidsing.

the class Chr method chr.

public Collection<Node> chr(Collection<Node> facts) {
    State state = new State(IMap.empty());
    for (Node fact : facts) {
        Prototype prototype = Prototype.of(fact);
        state = setFacts(state, prototype, getFacts(state, prototype).replace(fact));
    }
    State state1;
    while ((state1 = chr(state)) != null) state = state1;
    List<Node> nodes1 = new ArrayList<>();
    for (Pair<Prototype, ISet<Node>> e : state.factsByPrototype) nodes1.addAll(To.list(e.t1));
    return nodes1;
}
Also used : Prototype(suite.lp.kb.Prototype) Node(suite.node.Node) ArrayList(java.util.ArrayList) ISet(suite.immutable.ISet)

Example 7 with Prototype

use of suite.lp.kb.Prototype 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 8 with Prototype

use of suite.lp.kb.Prototype 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);
    });
}
Also used : Prototype(suite.lp.kb.Prototype) Node(suite.node.Node)

Example 9 with Prototype

use of suite.lp.kb.Prototype in project suite by stupidsing.

the class SingletonVariableChecker method check.

public void check(List<Rule> rules) {
    ListMultimap<Prototype, Rule> rulesByPrototype = Prototype.multimap(rules);
    for (Pair<Prototype, Rule> e : rulesByPrototype.entries()) {
        Prototype prototype = e.t0;
        Rule rule = e.t1;
        Scanner scanner = new Scanner();
        scanner.scan(rule.head);
        scanner.scan(rule.tail);
        scanner.warn(prototype);
    }
}
Also used : Prototype(suite.lp.kb.Prototype) Rule(suite.lp.kb.Rule)

Example 10 with Prototype

use of suite.lp.kb.Prototype 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

Prototype (suite.lp.kb.Prototype)11 Node (suite.node.Node)10 Atom (suite.node.Atom)7 Generalizer (suite.lp.doer.Generalizer)6 Reference (suite.node.Reference)6 Tree (suite.node.Tree)6 TermOp (suite.node.io.TermOp)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Suite (suite.Suite)5 Pair (suite.adt.pair.Pair)5 Binder (suite.lp.doer.Binder)5 Rule (suite.lp.kb.Rule)5 Read (suite.streamlet.Read)5 Fail (suite.util.Fail)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Prover (suite.lp.doer.Prover)4 RuleSet (suite.lp.kb.RuleSet)4 Iterator (java.util.Iterator)3