Search in sources :

Example 1 with Clone_

use of suite.lp.doer.ClonerFactory.Clone_ in project suite by stupidsing.

the class SewingExpressionImpl method evaluator.

public Evaluate_ evaluator(Node node) {
    Tree tree = Tree.decompose(node);
    if (tree != null) {
        Operator op = tree.getOperator();
        Evaluate_ lhs, rhs;
        IntInt_Int fun;
        if (op == TermOp.TUPLE_) {
            Tree rightTree = Tree.decompose(tree.getRight());
            lhs = evaluator(tree.getLeft());
            rhs = evaluator(rightTree.getRight());
            fun = TreeUtil.evaluateOp(rightTree.getLeft());
        } else {
            lhs = evaluator(tree.getLeft());
            rhs = evaluator(tree.getRight());
            fun = TreeUtil.evaluateOp(op);
        }
        return env -> fun.apply(lhs.evaluate(env), rhs.evaluate(env));
    } else if (node instanceof Int) {
        int i = ((Int) node).number;
        return env -> i;
    } else {
        Clone_ f = clonerFactory.cloner(node);
        return env -> TreeUtil.evaluate(f.apply(env));
    }
}
Also used : Operator(suite.node.io.Operator) IntInt_Int(suite.primitive.IntInt_Int) Clone_(suite.lp.doer.ClonerFactory.Clone_) ClonerFactory(suite.lp.doer.ClonerFactory) EvaluatorFactory(suite.lp.doer.EvaluatorFactory) TermOp(suite.node.io.TermOp) Int(suite.node.Int) Operator(suite.node.io.Operator) TreeUtil(suite.node.util.TreeUtil) Tree(suite.node.Tree) Node(suite.node.Node) Clone_(suite.lp.doer.ClonerFactory.Clone_) Tree(suite.node.Tree) IntInt_Int(suite.primitive.IntInt_Int) IntInt_Int(suite.primitive.IntInt_Int) Int(suite.node.Int)

Example 2 with Clone_

use of suite.lp.doer.ClonerFactory.Clone_ in project suite by stupidsing.

the class SewingProverImpl method compileCps.

private Cps compileCps(BinderFactory bf, Node node, Cps cpsx) {
    List<Node> list;
    Tree tree;
    Node[] m;
    Cps cps;
    if (1 < (list = TreeUtil.breakdown(TermOp.AND___, node)).size()) {
        cps = cpsx;
        for (Node n : List_.reverse(list)) cps = compileCps(bf, n, cps);
    } else if (1 < (list = TreeUtil.breakdown(TermOp.OR____, node)).size())
        cps = orCps(Read.from(list).map(n -> compileCps(bf, n, cpsx)));
    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);
        cps = rt -> p.test(rt, f.apply(rt.env)) ? cpsx : null;
    } else if ((m = Suite.pattern(".0 .1").match(node)) != null && m[0] instanceof Atom)
        cps = compileCpsCallPredicate(bf, ((Atom) m[0]).name, m[1], node, cpsx);
    else if (node instanceof Atom) {
        String name = ((Atom) node).name;
        if (String_.equals(name, ""))
            cps = cpsx;
        else if (String_.equals(name, "fail"))
            cps = rt -> null;
        else
            cps = compileCpsCallPredicate(bf, name, Atom.NIL, node, cpsx);
    } else if (node instanceof Reference) {
        Clone_ f = bf.cloner(node);
        cps = rt -> compileCps(passThru, f.apply(rt.env), cpsx);
    } else if ((tree = Tree.decompose(node)) != null)
        cps = compileCpsCallPredicate(bf, tree.getOperator().getName(), node, node, cpsx);
    else if (node instanceof Tuple)
        cps = compileCpsCallPredicate(bf, node, cpsx);
    else
        cps = Fail.t("cannot understand " + node);
    return 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) Reference(suite.node.Reference) Node(suite.node.Node) Atom(suite.node.Atom) Bind_(suite.lp.doer.BinderFactory.Bind_) Clone_(suite.lp.doer.ClonerFactory.Clone_) Tree(suite.node.Tree) Tuple(suite.node.Tuple)

Example 3 with Clone_

use of suite.lp.doer.ClonerFactory.Clone_ 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 4 with Clone_

use of suite.lp.doer.ClonerFactory.Clone_ in project suite by stupidsing.

the class ClonerFactoryTest method test.

private void test(String pattern, String match) {
    for (ClonerFactory cf : new ClonerFactory[] { new CompileClonerImpl(), new SewingClonerImpl() }) {
        Node node = new Generalizer().generalize(Suite.parse(pattern));
        Clone_ p = cf.cloner(node);
        assertTrue(Binder.bind(p.apply(cf.mapper().env()), Suite.parse(match), new Trail()));
    }
}
Also used : Trail(suite.lp.Trail) CompileClonerImpl(suite.lp.compile.impl.CompileClonerImpl) SewingClonerImpl(suite.lp.sewing.impl.SewingClonerImpl) Node(suite.node.Node) Clone_(suite.lp.doer.ClonerFactory.Clone_)

Example 5 with Clone_

use of suite.lp.doer.ClonerFactory.Clone_ in project suite by stupidsing.

the class CompileExpressionImpl method evaluator.

public Evaluate_ evaluator(Node node) {
    FunCreator<Evaluate_> fc = FunCreator.of(Evaluate_.class, false);
    return fc.create(new Iterate<>() {

        private FunExpr env;

        public FunExpr apply(FunExpr env) {
            this.env = env;
            return compile_(node);
        }

        private FunExpr compile_(Node node) {
            return new // 
            SwitchNode<FunExpr>(// 
            node).match2(".0 + .1", (a, b) -> {
                return compileOperator(a, b, "+");
            }).match2(".0 - .1", (a, b) -> {
                return compileOperator(a, b, "-");
            }).match2(".0 * .1", (a, b) -> {
                return compileOperator(a, b, "*");
            }).match2(".0 / .1", (a, b) -> {
                return compileOperator(a, b, "/");
            }).match2(".0 and .1", (a, b) -> {
                return compileOperator(a, b, "&&");
            }).match2(".0 or .1", (a, b) -> {
                return compileOperator(a, b, "||");
            }).match2(".0 shl .1", (a, b) -> {
                return compileOperator(a, b, "<<");
            }).match2(".0 shr .1", (a, b) -> {
                return compileOperator(a, b, ">>");
            }).applyIf(Int.class, i -> {
                return f.int_(i.number);
            }).applyIf(Node.class, i -> {
                Clone_ n_ = clonerFactory.cloner(node);
                Evaluate_ evaluate = env -> TreeUtil.evaluate(n_.apply(env));
                return f.object(evaluate).invoke("evaluate", env);
            }).nonNullResult();
        }

        private FunExpr compileOperator(Node a, Node b, String op) {
            FunExpr fe0 = compile_(a);
            FunExpr fe1 = compile_(b);
            return f.bi(op, fe0, fe1);
        }
    }).apply(Map.ofEntries());
}
Also used : Iterate(suite.util.FunUtil.Iterate) Clone_(suite.lp.doer.ClonerFactory.Clone_) Map(java.util.Map) ClonerFactory(suite.lp.doer.ClonerFactory) EvaluatorFactory(suite.lp.doer.EvaluatorFactory) FunCreator(suite.jdk.gen.FunCreator) SwitchNode(suite.node.io.SwitchNode) FunExpr(suite.jdk.gen.FunExpression.FunExpr) FunFactory(suite.jdk.gen.FunFactory) Int(suite.node.Int) TreeUtil(suite.node.util.TreeUtil) Node(suite.node.Node) Iterate(suite.util.FunUtil.Iterate) SwitchNode(suite.node.io.SwitchNode) Node(suite.node.Node) Clone_(suite.lp.doer.ClonerFactory.Clone_) FunExpr(suite.jdk.gen.FunExpression.FunExpr)

Aggregations

Clone_ (suite.lp.doer.ClonerFactory.Clone_)7 Node (suite.node.Node)7 Int (suite.node.Int)6 TreeUtil (suite.node.util.TreeUtil)6 Map (java.util.Map)5 Tree (suite.node.Tree)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 Iterator (java.util.Iterator)4 List (java.util.List)4 Suite (suite.Suite)4 Mutable (suite.adt.Mutable)4 ListMultimap (suite.adt.map.ListMultimap)4 Pair (suite.adt.pair.Pair)4 IList (suite.immutable.IList)4 ProverConfig (suite.lp.Configuration.ProverConfig)4 CompileExpressionImpl (suite.lp.compile.impl.CompileExpressionImpl)4 Binder (suite.lp.doer.Binder)4 BinderFactory (suite.lp.doer.BinderFactory)4 BindEnv (suite.lp.doer.BinderFactory.BindEnv)4