Search in sources :

Example 1 with Mutable

use of suite.adt.Mutable 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 2 with Mutable

use of suite.adt.Mutable in project suite by stupidsing.

the class BackAllocatorUtil method stop.

public default BackAllocator stop(double stopLoss, double stopGain) {
    return (akds, indices) -> {
        OnDateTime onDateTime = allocate(akds, indices);
        Map<String, DataSource> dsBySymbol = akds.dsByKey.toMap();
        Mutable<Map<String, Double>> mutable = Mutable.of(new HashMap<>());
        Map<String, List<DblFltPair>> entriesBySymbol = new HashMap<>();
        return index -> {
            int last = index - 1;
            List<Pair<String, Double>> potentialBySymbol = onDateTime.onDateTime(index);
            Map<String, Double> potentialBySymbol0 = mutable.get();
            Map<String, Double> potentialBySymbol1 = Read.from2(potentialBySymbol).toMap();
            // find out the transactions
            Map<String, Double> diffBySymbol = // 
            Read.from(// 
            Set_.union(potentialBySymbol0.keySet(), potentialBySymbol1.keySet())).map2(symbol -> {
                double potential0 = potentialBySymbol0.getOrDefault(symbol, 0d);
                double potential1 = potentialBySymbol1.getOrDefault(symbol, 0d);
                return potential1 - potential0;
            }).toMap();
            // check on each stock symbol
            for (Entry<String, Double> e : diffBySymbol.entrySet()) {
                String symbol = e.getKey();
                double diff = e.getValue();
                int bs = Quant.sign(diff);
                float price = dsBySymbol.get(symbol).prices[last];
                List<DblFltPair> entries0 = entriesBySymbol.getOrDefault(symbol, new ArrayList<>());
                List<DblFltPair> entries1 = new ArrayList<>();
                Collections.sort(entries0, (pair0, pair1) -> -bs * Float.compare(pair0.t1, pair1.t1));
                for (DblFltPair entry0 : entries0) {
                    double potential0 = entry0.t0;
                    float entryPrice = entry0.t1;
                    double cancellation;
                    // a recent buy would cancel out the lowest price sell
                    if (bs == -1)
                        cancellation = min(0, max(diff, -potential0));
                    else if (bs == 1)
                        cancellation = max(0, min(diff, -potential0));
                    else
                        cancellation = 0d;
                    double potential1 = potential0 + cancellation;
                    diff -= cancellation;
                    double min = entryPrice * (potential1 < 0 ? stopGain : stopLoss);
                    double max = entryPrice * (potential1 < 0 ? stopLoss : stopGain);
                    // drop entries that got past their stopping prices
                    if (min < price && price < max)
                        entries1.add(DblFltPair.of(potential1, entryPrice));
                }
                if (diff != 0d)
                    entries1.add(DblFltPair.of(diff, price));
                entriesBySymbol.put(symbol, entries1);
            }
            mutable.update(potentialBySymbol1);
            // re-assemble the entries into current profile
            return // 
            Read.multimap(// 
            entriesBySymbol).groupBy(// 
            entries -> entries.toDouble(Obj_Dbl.sum(pair -> pair.t0))).toList();
        };
    };
}
Also used : Arrays(java.util.Arrays) Read(suite.streamlet.Read) Trade_(suite.trade.Trade_) AlignKeyDataSource(suite.trade.data.DataSource.AlignKeyDataSource) Mutable(suite.adt.Mutable) DblDbl_Dbl(suite.primitive.DblDbl_Dbl) HashMap(java.util.HashMap) Deque(java.util.Deque) IntPredicate(java.util.function.IntPredicate) Fun(suite.util.FunUtil.Fun) ArrayList(java.util.ArrayList) DblFltPair(suite.primitive.adt.pair.DblFltPair) String_(suite.util.String_) Map(java.util.Map) Set_(suite.util.Set_) Streamlet2(suite.streamlet.Streamlet2) Object_(suite.util.Object_) Friends.min(suite.util.Friends.min) Statistic(suite.math.numeric.Statistic) Predicate(java.util.function.Predicate) ObjObj_Dbl(suite.primitive.DblPrimitives.ObjObj_Dbl) Datum(suite.trade.data.DataSource.Datum) Obj_Dbl(suite.primitive.DblPrimitives.Obj_Dbl) Quant(ts.Quant) Usex(suite.trade.Usex) Objects(java.util.Objects) WalkForwardAllocator(suite.trade.walkforwardalloc.WalkForwardAllocator) Pair(suite.adt.pair.Pair) Friends.max(suite.util.Friends.max) List(java.util.List) Streamlet(suite.streamlet.Streamlet) Time(suite.trade.Time) Configuration(suite.trade.data.Configuration) Entry(java.util.Map.Entry) DataSource(suite.trade.data.DataSource) As(suite.streamlet.As) Asset(suite.trade.Asset) ArrayDeque(java.util.ArrayDeque) Collections(java.util.Collections) Fail(suite.util.Fail) DblFltPair(suite.primitive.adt.pair.DblFltPair) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Mutable(suite.adt.Mutable) HashMap(java.util.HashMap) Map(java.util.Map) DblFltPair(suite.primitive.adt.pair.DblFltPair) Pair(suite.adt.pair.Pair)

Example 3 with Mutable

use of suite.adt.Mutable in project suite by stupidsing.

the class InterpretFunLazy0 method lazy_.

private Fun<IMap<String, Thunk_>, Thunk_> lazy_(Node node) {
    Fun<IMap<String, Thunk_>, Thunk_> result;
    Tree tree;
    Node[] m;
    if ((m = Suite.pattern("define .0 := .1 >> .2").match(node)) != null) {
        String vk = v(m[0]);
        Fun<IMap<String, Thunk_>, Thunk_> value = lazy_(m[1]);
        Fun<IMap<String, Thunk_>, Thunk_> expr = lazy_(m[2]);
        result = env -> {
            Mutable<Thunk_> val = Mutable.nil();
            IMap<String, Thunk_> env1 = env.put(vk, () -> val.get().get());
            val.set(value.apply(env1)::get);
            return expr.apply(env1);
        };
    } else if ((m = Suite.pattern("if .0 then .1 else .2").match(node)) != null) {
        Fun<IMap<String, Thunk_>, Thunk_> if_ = lazy_(m[0]);
        Fun<IMap<String, Thunk_>, Thunk_> then_ = lazy_(m[1]);
        Fun<IMap<String, Thunk_>, Thunk_> else_ = lazy_(m[2]);
        result = env -> (if_.apply(env).get() == Atom.TRUE ? then_ : else_).apply(env);
    } else if ((m = Suite.pattern(".0 => .1").match(node)) != null) {
        String vk = v(m[0]);
        Fun<IMap<String, Thunk_>, Thunk_> value = lazy_(m[1]);
        result = env -> () -> new Fun_(in -> value.apply(env.put(vk, in)));
    } else if ((m = Suite.pattern(".0 {.1}").match(node)) != null) {
        Fun<IMap<String, Thunk_>, Thunk_> fun = lazy_(m[0]);
        Fun<IMap<String, Thunk_>, Thunk_> param = lazy_(m[1]);
        result = env -> fun(fun.apply(env).get()).apply(param.apply(env));
    } else if ((tree = Tree.decompose(node)) != null) {
        Operator operator = tree.getOperator();
        Fun<IMap<String, Thunk_>, Thunk_> p0 = lazy_(tree.getLeft());
        Fun<IMap<String, Thunk_>, Thunk_> p1 = lazy_(tree.getRight());
        result = env -> {
            Thunk_ r0 = env.get(operator.getName());
            Thunk_ r1 = fun(r0.get()).apply(p0.apply(env));
            Thunk_ r2 = fun(r1.get()).apply(p1.apply(env));
            return r2;
        };
    } else if (node instanceof Atom) {
        String vk = v(node);
        result = env -> env.get(vk);
    } else
        result = env -> () -> node;
    return result;
}
Also used : Suite(suite.Suite) IntInt_Bool(suite.node.util.TreeUtil.IntInt_Bool) IMap(suite.immutable.IMap) Mutable(suite.adt.Mutable) TermOp(suite.node.io.TermOp) Fun(suite.util.FunUtil.Fun) Tree(suite.node.Tree) Node(suite.node.Node) IntInt_Int(suite.primitive.IntInt_Int) Iterate(suite.util.FunUtil.Iterate) Atom(suite.node.Atom) Entry(java.util.Map.Entry) Int(suite.node.Int) Operator(suite.node.io.Operator) TreeUtil(suite.node.util.TreeUtil) Fail(suite.util.Fail) Operator(suite.node.io.Operator) Node(suite.node.Node) Atom(suite.node.Atom) IMap(suite.immutable.IMap) Tree(suite.node.Tree) Fun(suite.util.FunUtil.Fun)

Example 4 with Mutable

use of suite.adt.Mutable in project suite by stupidsing.

the class SewingProverImpl method compileTr.

private Trampoline compileTr(BinderFactory bf, Node node) {
    List<Node> list;
    Trampoline tr;
    Tree tree;
    Node[] m;
    if (1 < (list = TreeUtil.breakdown(TermOp.AND___, node)).size())
        tr = andTr(Read.from(list).map(n -> compileTr(bf, n)));
    else if (1 < (list = TreeUtil.breakdown(TermOp.OR____, node)).size())
        tr = orTr(Read.from(list).map(n -> compileTr(bf, n)));
    else if ((m = Suite.pattern(".0 = .1").match(node)) != null) {
        boolean b = complexity(m[0]) <= complexity(m[1]);
        Node n0 = b ? m[0] : m[1];
        Node n1 = b ? m[1] : m[0];
        Bind_ p = bf.binder(n1);
        Clone_ f = bf.cloner(n0);
        tr = rt -> p.test(rt, f.apply(rt.env)) ? okay : fail;
    } else if ((m = Suite.pattern("builtin:.0:.1 .2").match(node)) != null) {
        String className = ((Atom) m[0]).name;
        String fieldName = ((Atom) m[1]).name;
        BuiltinPredicate predicate = Rethrow.ex(() -> {
            Class<?> clazz = Class.forName(className);
            return (BuiltinPredicate) clazz.getField(fieldName).get(Object_.new_(clazz));
        });
        tr = compileTrCallPredicate(bf, predicate, m[2]);
    } else if ((m = Suite.pattern("find.all .0 .1 .2").match(node)) != null) {
        Clone_ f = bf.cloner(m[0]);
        Trampoline tr1 = compileTr(bf, m[1]);
        Bind_ p = bf.binder(m[2]);
        List<Node> vs = new ArrayList<>();
        tr = rt -> {
            Restore restore = save(rt);
            rt.pushRem(rt_ -> {
                vs.add(new Cloner().clone(f.apply(rt_.env)));
                return fail;
            });
            rt.pushAlt(rt_ -> {
                restore.restore(rt);
                return p.test(rt, Tree.of(TermOp.AND___, vs)) ? okay : fail;
            });
            return tr1;
        };
    } else if ((m = Suite.pattern("if .0 .1 .2").match(node)) != null) {
        Trampoline tr0 = compileTr(bf, m[0]);
        Trampoline tr1 = compileTr(bf, m[1]);
        Trampoline tr2 = compileTr(bf, m[2]);
        tr = if_(tr0, tr1, tr2);
    } else if ((m = Suite.pattern("let .0 .1").match(node)) != null) {
        Bind_ p = bf.binder(m[0]);
        Evaluate_ eval = new CompileExpressionImpl(bf).evaluator(m[1]);
        tr = rt -> p.test(rt, Int.of(eval.evaluate(rt.env))) ? okay : fail;
    } else if ((m = Suite.pattern("list.fold .0/.1/.2 .3").match(node)) != null) {
        Clone_ list0_ = bf.cloner(m[0]);
        Clone_ value0_ = bf.cloner(m[1]);
        Bind_ valuex_ = bf.binder(m[2]);
        Clone_ ht_ = bf.cloner(m[3]);
        tr = rt -> {
            Node[] ht = Suite.pattern(".0 .1").match(ht_.apply(rt.env));
            Trampoline tr1 = saveEnvTr(compileTrRule(ht[0], ht[1]));
            Mutable<Node> current = Mutable.of(value0_.apply(rt.env));
            rt.pushRem(rt_ -> valuex_.test(rt_, current.get()) ? okay : fail);
            for (Node elem : Tree.iter(list0_.apply(rt.env))) {
                Reference result = new Reference();
                rt.pushRem(rt_ -> {
                    current.update(result.finalNode());
                    return okay;
                });
                rt.pushRem(rt_ -> {
                    rt_.query = Tree.of(TermOp.ITEM__, Tree.of(TermOp.ITEM__, elem, current.get()), result);
                    return tr1;
                });
            }
            return okay;
        };
    } else if ((m = Suite.pattern("list.fold.clone .0/.1/.2 .3/.4/.5 .6").match(node)) != null) {
        Clone_ list0_ = bf.cloner(m[0]);
        Clone_ value0_ = bf.cloner(m[1]);
        Bind_ valuex_ = bf.binder(m[2]);
        Bind_ elem_ = bf.binder(m[3]);
        Bind_ v0_ = bf.binder(m[4]);
        Clone_ vx_ = bf.cloner(m[5]);
        Trampoline tr1 = compileTr(bf, m[6]);
        tr = rt -> {
            Mutable<Node> current = Mutable.of(value0_.apply(rt.env));
            Env env0 = rt.env;
            rt.pushRem(rt_ -> {
                rt_.env = env0;
                return valuex_.test(rt_, current.get()) ? okay : fail;
            });
            for (Node elem : Tree.iter(list0_.apply(rt.env))) {
                rt.pushRem(rt_ -> {
                    current.update(vx_.apply(rt_.env));
                    return okay;
                });
                rt.pushRem(rt_ -> {
                    rt_.env = env0.clone();
                    return elem_.test(rt_, elem) && v0_.test(rt_, current.get()) ? tr1 : fail;
                });
            }
            return okay;
        };
    } else if ((m = Suite.pattern("list.query .0 .1").match(node)) != null) {
        Clone_ l_ = bf.cloner(m[0]);
        Clone_ ht_ = bf.cloner(m[1]);
        tr = rt -> {
            Node[] ht = Suite.pattern(".0 .1").match(ht_.apply(rt.env));
            Trampoline tr1 = saveEnvTr(compileTrRule(ht[0], ht[1]));
            for (Node n : Tree.iter(l_.apply(rt.env))) rt.pushRem(rt_ -> {
                rt_.query = n;
                return tr1;
            });
            return okay;
        };
    } else if ((m = Suite.pattern("list.query.clone .0 .1 .2").match(node)) != null) {
        Clone_ f = bf.cloner(m[0]);
        Bind_ p = bf.binder(m[1]);
        Trampoline tr1 = compileTr(bf, m[2]);
        tr = rt -> {
            Env env0 = rt.env;
            rt.pushRem(rt_ -> {
                rt_.env = env0;
                return okay;
            });
            for (Node n : Tree.iter(f.apply(rt.env))) rt.pushRem(rt_ -> {
                rt_.env = env0.clone();
                return p.test(rt_, n) ? tr1 : fail;
            });
            return okay;
        };
    } else if ((m = Suite.pattern("member .0 .1").match(node)) != null && TreeUtil.isList(m[0], TermOp.AND___)) {
        List<Bind_> elems_ = Read.from(Tree.iter(m[0])).map(bf::binder).toList();
        Clone_ f = bf.cloner(m[1]);
        tr = rt -> {
            Iterator<Bind_> iter = elems_.iterator();
            Trampoline[] alt = new Trampoline[1];
            Restore restore = save(rt);
            return alt[0] = rt_ -> {
                while (iter.hasNext()) {
                    restore.restore(rt);
                    if (iter.next().test(rt_, f.apply(rt.env))) {
                        rt_.pushAlt(alt[0]);
                        return okay;
                    }
                }
                return fail;
            };
        };
    } else if ((m = Suite.pattern("not .0").match(node)) != null)
        tr = if_(compileTr(bf, m[0]), fail, okay);
    else if ((m = Suite.pattern("once .0").match(node)) != null) {
        Trampoline tr0 = compileTr(bf, m[0]);
        tr = rt -> {
            IList<Trampoline> alts0 = rt.alts;
            rt.pushRem(rt_ -> {
                rt_.alts = alts0;
                return okay;
            });
            return tr0;
        };
    } else if ((m = Suite.pattern("suspend .0 .1 .2").match(node)) != null) {
        Clone_ f0 = bf.cloner(m[0]);
        Clone_ f1 = bf.cloner(m[1]);
        Trampoline tr0 = compileTr(bf, m[2]);
        tr = rt -> {
            List<Node> results = new ArrayList<>();
            Env env = rt.env;
            Trampoline tr_ = andTr(Read.each(tr0, rt_ -> {
                results.add(f1.apply(env));
                return fail;
            }));
            Node n0 = f0.apply(rt.env);
            Suspend suspend = new Suspend(() -> {
                Runtime rt_ = new Runtime(rt, tr_);
                rt_.trampoline();
                return Read.from(results).uniqueResult();
            });
            if (n0 instanceof Reference) {
                rt.trail.addBind((Reference) n0, suspend);
                return okay;
            } else
                return fail;
        };
    } else if ((m = Suite.pattern("throw .0").match(node)) != null) {
        Clone_ f = bf.cloner(m[0]);
        tr = rt -> {
            rt.handler.sink(new Cloner().clone(f.apply(rt.env)));
            return okay;
        };
    } else if ((m = Suite.pattern("try .0 .1 .2").match(node)) != null) {
        Trampoline tr0 = compileTr(bf, m[0]);
        Bind_ p = bf.binder(m[1]);
        Trampoline catch0 = compileTr(bf, m[2]);
        tr = rt -> {
            BindEnv be = rt;
            Restore restore = save(rt);
            IList<Trampoline> alts0 = rt.alts;
            Sink<Node> handler0 = rt.handler;
            rt.handler = node_ -> {
                restore.restore(rt);
                if (p.test(be, node_)) {
                    rt.alts = alts0;
                    rt.pushRem(catch0);
                } else
                    handler0.sink(node_);
            };
            rt.pushRem(rt_ -> {
                rt_.handler = handler0;
                return okay;
            });
            return tr0;
        };
    } else if ((m = Suite.pattern(".0 .1").match(node)) != null && m[0] instanceof Atom)
        tr = compileTrCallPredicate(bf, ((Atom) m[0]).name, m[1], node);
    else if (node instanceof Atom) {
        String name = ((Atom) node).name;
        if (node == ProverConstant.cut)
            tr = cutEnd();
        else if (String_.equals(name, ""))
            tr = okay;
        else if (String_.equals(name, "fail"))
            tr = fail;
        else
            tr = compileTrCallPredicate(bf, name, Atom.NIL, node);
    } else if (node instanceof Data<?>) {
        Object data = ((Data<?>) node).data;
        if (data instanceof Source<?>)
            tr = rt -> ((Source<?>) data).source() != Boolean.TRUE ? okay : fail;
        else
            tr = Fail.t("cannot understand " + node);
    } else if (node instanceof Reference) {
        Clone_ f = bf.cloner(node);
        tr = rt -> compileTr(passThru, f.apply(rt.env));
    } else if ((tree = Tree.decompose(node)) != null)
        tr = compileTrCallPredicate(bf, tree.getOperator().getName(), node, node);
    else if (node instanceof Tuple)
        tr = compileTrCallPredicate(bf, node);
    else
        tr = Fail.t("cannot understand " + node);
    return tr;
}
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) Node(suite.node.Node) ArrayList(java.util.ArrayList) CompileExpressionImpl(suite.lp.compile.impl.CompileExpressionImpl) Suspend(suite.node.Suspend) BindEnv(suite.lp.doer.BinderFactory.BindEnv) Env(suite.lp.sewing.Env) BindEnv(suite.lp.doer.BinderFactory.BindEnv) Iterator(java.util.Iterator) Tree(suite.node.Tree) List(java.util.List) IList(suite.immutable.IList) ArrayList(java.util.ArrayList) BuiltinPredicate(suite.lp.predicate.PredicateUtil.BuiltinPredicate) Evaluate_(suite.lp.doer.EvaluatorFactory.Evaluate_) Reference(suite.node.Reference) Atom(suite.node.Atom) Cloner(suite.lp.doer.Cloner) IList(suite.immutable.IList) Bind_(suite.lp.doer.BinderFactory.Bind_) Mutable(suite.adt.Mutable) Clone_(suite.lp.doer.ClonerFactory.Clone_) Tuple(suite.node.Tuple)

Example 5 with Mutable

use of suite.adt.Mutable in project suite by stupidsing.

the class SewingProverImpl method compileCpsCallPredicate.

private Cps compileCpsCallPredicate(BinderFactory bf, Node node, Cps cpsx) {
    Prototype prototype = Prototype.of(node);
    if (rules.containsKey(prototype)) {
        Clone_ f = bf.cloner(node);
        Cps cps;
        if (isHasCutByPrototype.get(prototype)) {
            Mutable<Trampoline> mtr = getTrampolineByPrototype(prototype);
            Trampoline rem = rt -> {
                rt.cont(cpsx);
                return fail;
            };
            cps = rt -> {
                IList<Trampoline> rems = rt.rems;
                rt.rems = IList.cons(fail, IList.end());
                new Runtime(rt, rt_ -> {
                    rt_.query = f.apply(rt.env);
                    rt_.rems = rems;
                    rt_.pushRem(rem);
                    return mtr.get();
                }).trampoline();
                return null;
            };
        } else {
            Mutable<Cps> mcps = getCpsByPrototype(prototype);
            cps = rt -> {
                Cps cps0 = rt.cps;
                rt.cps = rt_ -> {
                    rt.cps = cps0;
                    return cpsx;
                };
                rt.query = f.apply(rt.env);
                return mcps.get();
            };
        }
        return cps;
    } 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_)

Aggregations

Mutable (suite.adt.Mutable)6 Fail (suite.util.Fail)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 List (java.util.List)4 Map (java.util.Map)4 Suite (suite.Suite)4 Pair (suite.adt.pair.Pair)4 Iterator (java.util.Iterator)3 ListMultimap (suite.adt.map.ListMultimap)3 IList (suite.immutable.IList)3 ProverConfig (suite.lp.Configuration.ProverConfig)3 CompileExpressionImpl (suite.lp.compile.impl.CompileExpressionImpl)3 Binder (suite.lp.doer.Binder)3 BinderFactory (suite.lp.doer.BinderFactory)3 BindEnv (suite.lp.doer.BinderFactory.BindEnv)3 Bind_ (suite.lp.doer.BinderFactory.Bind_)3 Cloner (suite.lp.doer.Cloner)3 Clone_ (suite.lp.doer.ClonerFactory.Clone_)3 Evaluate_ (suite.lp.doer.EvaluatorFactory.Evaluate_)3