Search in sources :

Example 11 with Tree

use of suite.node.Tree in project suite by stupidsing.

the class FindPredicates method elaborate.

private Stack<Node> elaborate(Prover prover, Node var, Node goal) {
    Stack<Node> stack = new Stack<>();
    Tree subGoal = Tree.of(TermOp.AND___, goal, new Data<Source<Boolean>>(() -> {
        stack.push(new Cloner().clone(var));
        return Boolean.FALSE;
    }));
    new Prover(prover).elaborate(subGoal);
    return stack;
}
Also used : Node(suite.node.Node) Prover(suite.lp.doer.Prover) Tree(suite.node.Tree) Source(suite.util.FunUtil.Source) Stack(java.util.Stack) Cloner(suite.lp.doer.Cloner)

Example 12 with Tree

use of suite.node.Tree in project suite by stupidsing.

the class SystemPredicates method call.

public Boolean call(Node query) {
    BuiltinPredicate predicate;
    Tree tree;
    Operator op;
    Node left;
    String name = null;
    Node pass = null;
    if (query instanceof Atom) {
        name = ((Atom) query).name;
        pass = Atom.NIL;
    } else if ((tree = Tree.decompose(query)) != null)
        if ((op = tree.getOperator()) != TermOp.TUPLE_) {
            name = op.getName();
            pass = query;
        } else if ((left = tree.getLeft()) instanceof Atom) {
            name = ((Atom) left).name;
            pass = tree.getRight();
        }
    predicate = name != null ? get(name) : null;
    return predicate != null ? predicate.prove(prover, pass) : null;
}
Also used : BuiltinPredicate(suite.lp.predicate.PredicateUtil.BuiltinPredicate) Operator(suite.node.io.Operator) Node(suite.node.Node) Tree(suite.node.Tree) Atom(suite.node.Atom)

Example 13 with Tree

use of suite.node.Tree 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 14 with Tree

use of suite.node.Tree in project suite by stupidsing.

the class SewingGeneralizerImpl method generalizer.

@Override
public Generalize_ generalizer(Node node) {
    List<Generalize_> funs = new ArrayList<>();
    Generalize_ fun;
    while (true) {
        Node node0 = node;
        Tree tree;
        if (node0 instanceof Atom) {
            Atom atom = (Atom) node0;
            String name = atom.name;
            if (ProverConstant.isCut(node0) || ProverConstant.isVariable(name)) {
                int index = vm.computeIndex(atom);
                fun = env -> env.get(index);
            } else if (ProverConstant.isWildcard(name))
                fun = env -> new Reference();
            else
                fun = env -> node0;
        } else if (node0 instanceof Dict) {
            Generalize_[][] array = // 
            Read.from2(// 
            ((Dict) node0).map).map(// 
            (key, value) -> new Generalize_[] { generalizer(key), generalizer(value) }).toArray(Generalize_[].class);
            int length = array.length;
            fun = env -> {
                @SuppressWarnings("unchecked") Pair<Node, Reference>[] pairs = new Pair[length];
                for (int i = 0; i < length; i++) pairs[i] = Pair.of(array[i][0].apply(env), Reference.of(array[i][1].apply(env)));
                return Dict.of(pairs);
            };
        } else if ((tree = Tree.decompose(node0)) != null) {
            Operator operator = tree.getOperator();
            if (operator != TermOp.OR____) {
                Generalize_ f = generalizer(tree.getLeft());
                funs.add(env -> Tree.of(operator, f.apply(env), null));
                node = tree.getRight();
                continue;
            } else {
                // delay generalizing for performance
                Generalize_ lf = generalizer(tree.getLeft());
                Generalize_ rf = generalizer(tree.getRight());
                fun = env -> Tree.of(operator, lf.apply(env), new Suspend(() -> rf.apply(env)));
            }
        } else if (node0 instanceof Tuple) {
            Generalize_[] fs = Read.from(((Tuple) node0).nodes).map(this::generalizer).toArray(Generalize_.class);
            int length = fs.length;
            fun = env -> {
                Node[] array = new Node[length];
                for (int i = 0; i < length; i++) array[i] = fs[i].apply(env);
                return Tuple.of(array);
            };
        } else
            fun = env -> node0;
        funs.add(fun);
        break;
    }
    if (1 < funs.size())
        return env -> {
            Tree t = Tree.of(null, null, null);
            Node node_ = t;
            for (Generalize_ fun_ : funs) {
                Tree t_ = Tree.decompose(node_);
                Tree.forceSetRight(t_, fun_.apply(env));
                node_ = t_.getRight();
            }
            return t.getRight();
        };
    else
        return funs.get(0);
}
Also used : Reference(suite.node.Reference) Read(suite.streamlet.Read) GeneralizerFactory(suite.lp.doer.GeneralizerFactory) TermOp(suite.node.io.TermOp) Tree(suite.node.Tree) ArrayList(java.util.ArrayList) Node(suite.node.Node) Pair(suite.adt.pair.Pair) List(java.util.List) ProverConstant(suite.lp.doer.ProverConstant) VariableMapper(suite.lp.sewing.VariableMapper) Atom(suite.node.Atom) Suspend(suite.node.Suspend) Tuple(suite.node.Tuple) Operator(suite.node.io.Operator) Dict(suite.node.Dict) Operator(suite.node.io.Operator) Reference(suite.node.Reference) Node(suite.node.Node) ArrayList(java.util.ArrayList) Suspend(suite.node.Suspend) Atom(suite.node.Atom) Dict(suite.node.Dict) Tree(suite.node.Tree) Tuple(suite.node.Tuple)

Example 15 with Tree

use of suite.node.Tree 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)

Aggregations

Tree (suite.node.Tree)47 Node (suite.node.Node)36 Reference (suite.node.Reference)19 Atom (suite.node.Atom)14 Int (suite.node.Int)13 TermOp (suite.node.io.TermOp)11 Tuple (suite.node.Tuple)10 ArrayList (java.util.ArrayList)9 Pair (suite.adt.pair.Pair)9 Operator (suite.node.io.Operator)9 Read (suite.streamlet.Read)9 List (java.util.List)8 Dict (suite.node.Dict)8 Map (java.util.Map)7 Str (suite.node.Str)7 Binder (suite.lp.doer.Binder)6 HashMap (java.util.HashMap)5 Generalizer (suite.lp.doer.Generalizer)5 VariableMapper (suite.lp.sewing.VariableMapper)5 TreeUtil (suite.node.util.TreeUtil)5