Search in sources :

Example 1 with Prototype

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

the class Prover method prove0.

public boolean prove0(Node query) {
    rem = OK;
    alt = FAIL;
    while (true) {
        // logUtil.info(Formatter.dump(query));
        query = query.finalNode();
        if (query instanceof Tree) {
            Tree tree = (Tree) query;
            Node left = tree.getLeft(), right = tree.getRight();
            switch((TermOp) tree.getOperator()) {
                case OR____:
                    int pit = trail.getPointInTime();
                    Node bt = new Data<Source<Boolean>>(() -> {
                        trail.unwind(pit);
                        return Boolean.TRUE;
                    });
                    alt = andTree(bt, orTree(andTree(right, rem), alt));
                    query = left;
                    continue;
                case AND___:
                    rem = andTree(right, rem);
                    query = left;
                    continue;
                case EQUAL_:
                    query = isSuccess(bind(left, right));
                    break;
                default:
            }
        } else if (query instanceof Data) {
            query = isSuccess(Data.<Source<Boolean>>get(query).source());
            continue;
        }
        Boolean b = systemPredicates.call(query);
        if (b != null)
            query = isSuccess(b);
        // not handled above
        if (query == OK)
            if (rem != OK) {
                query = rem;
                rem = OK;
            } else
                return true;
        else if (query == FAIL)
            if (alt != FAIL) {
                query = alt;
                alt = FAIL;
                rem = OK;
            } else
                return false;
        else {
            boolean isTrace = config.isTrace();
            if (isTrace) {
                Set<String> whites = Suite.tracePredicates;
                Set<String> blacks = Suite.noTracePredicates;
                Prototype prototype = Prototype.of(query);
                Node head = prototype != null ? prototype.head : null;
                Atom atom = head instanceof Atom ? (Atom) head : null;
                String name = atom != null ? atom.name : null;
                isTrace &= whites == null || whites.contains(name);
                isTrace &= blacks == null || !blacks.contains(name);
            }
            if (!isTrace)
                query = expand(query);
            else
                query = tracer.expandWithTrace(query, this, this::expand);
        }
    }
}
Also used : Set(java.util.Set) RuleSet(suite.lp.kb.RuleSet) Prototype(suite.lp.kb.Prototype) TermOp(suite.node.io.TermOp) Node(suite.node.Node) Tree(suite.node.Tree) Data(suite.node.Data) Source(suite.util.FunUtil.Source) Atom(suite.node.Atom)

Example 2 with Prototype

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

the class QueryRewriter method rewriteQuery.

private Node rewriteQuery(Node node0) {
    Node nodex;
    Prototype prototype = Prototype.of(node0);
    PrototypeInfo pi;
    if ((pi = infoByPrototype.get(prototype)) != null) {
        int length = pi.length;
        if (length <= 0)
            nodex = node0;
        else {
            Node[] ps = TreeUtil.elements(node0, length);
            if (pi.isSkipFirst)
                ps = Arrays.copyOfRange(ps, 1, ps.length, Node[].class);
            nodex = Tuple.of(ps);
        }
    } else
        nodex = node0;
    return nodex;
}
Also used : Prototype(suite.lp.kb.Prototype) Node(suite.node.Node)

Example 3 with Prototype

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

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

the class SewingProverImpl method compileTrCallPredicate.

private Trampoline compileTrCallPredicate(BinderFactory bf, Node node) {
    Prototype prototype = Prototype.of(node);
    if (rules.containsKey(prototype)) {
        Clone_ f = bf.cloner(node);
        Trampoline tr;
        if (isHasCutByPrototype.get(prototype)) {
            Mutable<Trampoline> mtr = getTrampolineByPrototype(prototype);
            tr = rt -> {
                rt.query = f.apply(rt.env);
                return mtr.get()::prove;
            };
        } else {
            Mutable<Cps> mcps = getCpsByPrototype(prototype);
            Cps cpsx = rt -> {
                IList<Trampoline> rems = rt.rems;
                rt.rems = IList.cons(fail, IList.end());
                new Runtime(rt, rt_ -> {
                    rt_.rems = rems;
                    return okay;
                }).trampoline();
                return null;
            };
            tr = rt -> {
                Cps cps0 = rt.cps;
                rt.cps = rt_ -> {
                    rt.cps = cps0;
                    return cpsx;
                };
                rt.query = f.apply(rt.env);
                rt.cont(mcps.get());
                return fail;
            };
        }
        return tr;
    } else
        return Fail.t("cannot find predicate " + prototype);
}
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) Clone_(suite.lp.doer.ClonerFactory.Clone_) IList(suite.immutable.IList)

Example 5 with Prototype

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

the class Chr method chrIf.

private Streamlet<State> chrIf(Streamlet<State> states, Trail trail, Node if_) {
    Prototype prototype = Prototype.of(if_);
    Fun<State, Streamlet<State>> fun = state -> {
        ISet<Node> facts = getFacts(state, prototype);
        Predicate<Node> bindFun = bindFun(trail, if_);
        return facts.streamlet().filter(bindFun).map(node -> setFacts(state, prototype, facts.remove(node)));
    };
    return states.concatMap(fun);
}
Also used : Reference(suite.node.Reference) Suite(suite.Suite) Trail(suite.lp.Trail) Prover(suite.lp.doer.Prover) Read(suite.streamlet.Read) Predicate(java.util.function.Predicate) Collection(java.util.Collection) IMap(suite.immutable.IMap) ISet(suite.immutable.ISet) TermOp(suite.node.io.TermOp) To(suite.util.To) Fun(suite.util.FunUtil.Fun) Tree(suite.node.Tree) ArrayList(java.util.ArrayList) Node(suite.node.Node) Pair(suite.adt.pair.Pair) List(java.util.List) Rewrite(suite.node.util.Rewrite) Streamlet(suite.streamlet.Streamlet) Atom(suite.node.Atom) Prototype(suite.lp.kb.Prototype) Binder(suite.lp.doer.Binder) Generalizer(suite.lp.doer.Generalizer) Fail(suite.util.Fail) Prototype(suite.lp.kb.Prototype) Streamlet(suite.streamlet.Streamlet) ISet(suite.immutable.ISet) Predicate(java.util.function.Predicate)

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