Search in sources :

Example 96 with Node

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

the class SeqIntrinsics method deepSeq.

private Node deepSeq(IntrinsicCallback callback, Node node) {
    Node node1 = callback.yawn(node);
    Tree tree;
    if ((tree = Tree.decompose(node1)) != null)
        node1 = Tree.of(tree.getOperator(), deepSeq(callback, tree.getLeft()), deepSeq(callback, tree.getRight()));
    return callback.enclose(Intrinsics.id_, node1);
}
Also used : Node(suite.node.Node) Tree(suite.node.Tree)

Example 97 with Node

use of suite.node.Node 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 98 with Node

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

Example 99 with Node

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

Example 100 with Node

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

the class Binder method bind.

public static boolean bind(Node n0, Node n1, Trail trail) {
    n0 = n0.finalNode();
    n1 = n1.finalNode();
    if (n0 == n1)
        return true;
    Class<? extends Node> clazz0 = n0.getClass();
    Class<? extends Node> clazz1 = n1.getClass();
    if (clazz0 == Reference.class) {
        trail.addBind((Reference) n0, n1);
        return true;
    } else if (clazz1 == Reference.class) {
        trail.addBind((Reference) n1, n0);
        return true;
    }
    if (clazz0 == Dict.class && clazz1 == Dict.class) {
        Map<Node, Reference> map0 = ((Dict) n0).map;
        Map<Node, Reference> map1 = ((Dict) n1).map;
        boolean b = true;
        for (Node key : List_.concat(map0.keySet(), map1.keySet())) {
            Node v0 = map0.computeIfAbsent(key, k -> new Reference());
            Node v1 = map1.computeIfAbsent(key, k -> new Reference());
            b &= bind(v0, v1, trail);
        }
        return b;
    } else if (clazz0 == Int.class && clazz1 == Int.class)
        return ((Int) n0).number == ((Int) n1).number;
    else if (clazz0 == Str.class && clazz1 == Str.class)
        return Objects.equals(((Str) n0).value, ((Str) n1).value);
    else if (Tree.class.isAssignableFrom(clazz0) && Tree.class.isAssignableFrom(clazz1)) {
        Tree t0 = (Tree) n0;
        Tree t1 = (Tree) n1;
        return // 
        t0.getOperator() == t1.getOperator() && // 
        bind(t0.getLeft(), t1.getLeft(), trail) && bind(t0.getRight(), t1.getRight(), trail);
    } else if (clazz0 == Tuple.class && clazz1 == Tuple.class) {
        Node[] nodes0 = ((Tuple) n0).nodes;
        Node[] nodes1 = ((Tuple) n1).nodes;
        boolean b = nodes0.length == nodes1.length;
        if (b) {
            for (int i = 0; i < nodes0.length; i++) b &= bind(nodes0[i], nodes1[i], trail);
        }
        return b;
    } else
        return false;
}
Also used : Str(suite.node.Str) Reference(suite.node.Reference) Dict(suite.node.Dict) Node(suite.node.Node) Tree(suite.node.Tree) Tuple(suite.node.Tuple)

Aggregations

Node (suite.node.Node)139 Tree (suite.node.Tree)50 Reference (suite.node.Reference)41 Atom (suite.node.Atom)37 Int (suite.node.Int)33 ArrayList (java.util.ArrayList)32 Pair (suite.adt.pair.Pair)25 List (java.util.List)24 TermOp (suite.node.io.TermOp)21 Read (suite.streamlet.Read)21 Test (org.junit.Test)20 Map (java.util.Map)19 Suite (suite.Suite)18 Generalizer (suite.lp.doer.Generalizer)18 Fail (suite.util.Fail)18 Fun (suite.util.FunUtil.Fun)18 Str (suite.node.Str)17 Trail (suite.lp.Trail)16 RuleSet (suite.lp.kb.RuleSet)16 Tuple (suite.node.Tuple)16