Search in sources :

Example 1 with Rule

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

the class SewingProverImpl method compileAll.

private void compileAll() {
    isHasCutByPrototype = rules.listEntries().mapValue(this::isHasCut).toMap();
    for (Pair<Prototype, List<Rule>> e : rules.listEntries()) {
        Prototype prototype = e.t0;
        List<Rule> rules = new ArrayList<>(e.t1);
        TraceLevel traceLevel = traceLevel(prototype);
        // second-level indexing optimization
        Map<Prototype, List<Rule>> rulesByProto1;
        if (6 <= rules.size()) {
            Map<Prototype, List<Rule>> rulesByProto_ = Read.from(rules).toListMap(rule -> Prototype.of(rule, 1));
            rulesByProto1 = !rulesByProto_.containsKey(null) ? rulesByProto_ : null;
        } else
            rulesByProto1 = null;
        if (isHasCutByPrototype.get(prototype)) {
            Trampoline tr0 = compileTrRules(prototype, rules, traceLevel);
            Trampoline tr;
            if (rulesByProto1 != null) {
                Map<Prototype, Trampoline> trByProto1 = // 
                Read.from2(// 
                rulesByProto1).mapValue(// 
                rules_ -> compileTrRules(prototype, rules_, traceLevel)).toMap();
                tr = rt -> {
                    Prototype proto = Prototype.of(rt.query, 1);
                    if (proto != null) {
                        Trampoline tr_ = trByProto1.get(proto);
                        return tr_ != null ? tr_ : fail;
                    } else
                        return tr0;
                };
            } else
                tr = tr0;
            getTrampolineByPrototype(prototype).set(tr);
        } else {
            Cps cps0 = compileCpsRules(prototype, rules, traceLevel);
            Cps cps;
            if (rulesByProto1 != null) {
                Map<Prototype, Cps> cpsByProto1 = // 
                Read.from2(// 
                rulesByProto1).mapValue(// 
                rules_ -> compileCpsRules(prototype, rules_, traceLevel)).toMap();
                cps = rt -> {
                    Prototype proto = Prototype.of(rt.query, 1);
                    return proto != null ? cpsByProto1.get(proto) : cps0;
                };
            } else
                cps = cps0;
            getCpsByPrototype(prototype).set(cps);
        }
    }
}
Also used : Prover(suite.lp.doer.Prover) BindEnv(suite.lp.doer.BinderFactory.BindEnv) BuiltinPredicate(suite.lp.predicate.PredicateUtil.BuiltinPredicate) LogUtil(suite.os.LogUtil) Mutable(suite.adt.Mutable) BinderFactory(suite.lp.doer.BinderFactory) Node(suite.node.Node) ProverConstant(suite.lp.doer.ProverConstant) VariableMapper(suite.lp.sewing.VariableMapper) Suspend(suite.node.Suspend) Map(java.util.Map) Prototype(suite.lp.kb.Prototype) RuleSet(suite.lp.kb.RuleSet) Generalizer(suite.lp.doer.Generalizer) Bind_(suite.lp.doer.BinderFactory.Bind_) Cloner(suite.lp.doer.Cloner) Object_(suite.util.Object_) SystemPredicates(suite.lp.predicate.SystemPredicates) Tree(suite.node.Tree) Pair(suite.adt.pair.Pair) Friends.max(suite.util.Friends.max) List(java.util.List) Clone_(suite.lp.doer.ClonerFactory.Clone_) ListMultimap(suite.adt.map.ListMultimap) As(suite.streamlet.As) Int(suite.node.Int) TreeUtil(suite.node.util.TreeUtil) ProverConfig(suite.lp.Configuration.ProverConfig) Read(suite.streamlet.Read) Rule(suite.lp.kb.Rule) HashMap(java.util.HashMap) IList(suite.immutable.IList) ArrayList(java.util.ArrayList) Data(suite.node.Data) Formatter(suite.node.io.Formatter) String_(suite.util.String_) Rethrow(suite.util.Rethrow) Binder(suite.lp.doer.Binder) Tuple(suite.node.Tuple) Reference(suite.node.Reference) Suite(suite.Suite) SuiteException(suite.node.util.SuiteException) Iterator(java.util.Iterator) CompileExpressionImpl(suite.lp.compile.impl.CompileExpressionImpl) Source(suite.util.FunUtil.Source) TermOp(suite.node.io.TermOp) List_(suite.util.List_) Evaluate_(suite.lp.doer.EvaluatorFactory.Evaluate_) Rewrite(suite.node.util.Rewrite) Streamlet(suite.streamlet.Streamlet) Atom(suite.node.Atom) Sink(suite.util.FunUtil.Sink) ProverFactory(suite.lp.doer.ProverFactory) Env(suite.lp.sewing.Env) Fail(suite.util.Fail) Prototype(suite.lp.kb.Prototype) ArrayList(java.util.ArrayList) List(java.util.List) IList(suite.immutable.IList) ArrayList(java.util.ArrayList) Rule(suite.lp.kb.Rule)

Example 2 with Rule

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

the class ImportUtil method importFrom.

public synchronized boolean importFrom(RuleSet ruleSet, Node node) {
    List<Rule> rules = new ArrayList<>();
    for (Node elem : Tree.iter(node, TermOp.NEXT__)) rules.add(Rule.of(elem));
    Prover prover = new Prover(ruleSet);
    boolean b = true;
    IList<Node> importing0 = importing.get();
    try {
        importing.set(IList.cons(node, importing0));
        for (Rule rule : rules) if (rule.head != Atom.NIL)
            ruleSet.addRule(rule);
        else {
            Node goal = SewingGeneralizerImpl.generalize(rule.tail);
            b &= prover.prove(goal);
        }
    } finally {
        importing.set(importing0);
    }
    if (// check after all files are imported
    importing0.isEmpty())
        new Checker().check(ruleSet.getRules());
    return b;
}
Also used : Checker(suite.lp.checker.Checker) Node(suite.node.Node) ArrayList(java.util.ArrayList) Prover(suite.lp.doer.Prover) Rule(suite.lp.kb.Rule)

Example 3 with Rule

use of suite.lp.kb.Rule 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 4 with Rule

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

Rule (suite.lp.kb.Rule)4 Prototype (suite.lp.kb.Prototype)3 Node (suite.node.Node)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Suite (suite.Suite)2 Pair (suite.adt.pair.Pair)2 Binder (suite.lp.doer.Binder)2 Generalizer (suite.lp.doer.Generalizer)2 Prover (suite.lp.doer.Prover)2 Atom (suite.node.Atom)2 Reference (suite.node.Reference)2 Tree (suite.node.Tree)2 TermOp (suite.node.io.TermOp)2 Iterator (java.util.Iterator)1 IdentityKey (suite.adt.IdentityKey)1 Mutable (suite.adt.Mutable)1 ListMultimap (suite.adt.map.ListMultimap)1