Search in sources :

Example 21 with Reference

use of suite.node.Reference 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 22 with Reference

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

Example 23 with Reference

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

the class Cloner method cloneRight.

private void cloneRight(Tree tree) {
    while (tree != null) {
        Tree nextTree = null;
        Node right = tree.getRight();
        IdentityKey<Node> key = IdentityKey.of(right);
        Node right1 = clonedNodes.get(key);
        Tree rt;
        if (right1 == null) {
            if (right instanceof Reference)
                right1 = new Reference();
            else if ((rt = Tree.decompose(right)) != null)
                right1 = nextTree = Tree.of(rt.getOperator(), clone(rt.getLeft()), rt.getRight());
            else
                right1 = Rewrite_.map(right, this::clone);
            clonedNodes.put(key, right1);
        }
        Tree.forceSetRight(tree, right1);
        tree = nextTree;
    }
}
Also used : Reference(suite.node.Reference) Node(suite.node.Node) Tree(suite.node.Tree)

Example 24 with Reference

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

the class Generalizer method generalizeRight.

private void generalizeRight(Tree tree) {
    while (tree != null) {
        Tree nextTree = null;
        Node right = tree.getRight();
        Tree rt;
        if (right instanceof Atom) {
            Atom atom = (Atom) right;
            String name = atom.name;
            if (name.startsWith(ProverConstant.wildcardPrefix))
                right = new Reference();
            if (name.startsWith(ProverConstant.variablePrefix))
                right = getVariable(atom);
        } else if ((rt = Tree.decompose(right)) != null)
            right = nextTree = Tree.of(rt.getOperator(), generalize(rt.getLeft()), rt.getRight());
        else
            right = Rewrite_.map(right, this::generalize);
        Tree.forceSetRight(tree, right);
        tree = nextTree;
    }
}
Also used : Reference(suite.node.Reference) Node(suite.node.Node) Tree(suite.node.Tree) Atom(suite.node.Atom)

Example 25 with Reference

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

the class Env method clone.

public Env clone() {
    Reference[] refs1 = new Reference[refs.length];
    Cloner cloner = new Cloner();
    for (int i = 0; i < refs.length; i++) refs1[i] = Reference.of(cloner.clone(refs[i]));
    return new Env(refs1);
}
Also used : Reference(suite.node.Reference) Cloner(suite.lp.doer.Cloner)

Aggregations

Reference (suite.node.Reference)38 Node (suite.node.Node)31 Tree (suite.node.Tree)21 Atom (suite.node.Atom)17 ArrayList (java.util.ArrayList)15 Pair (suite.adt.pair.Pair)13 Int (suite.node.Int)13 List (java.util.List)11 Generalizer (suite.lp.doer.Generalizer)10 TermOp (suite.node.io.TermOp)10 Read (suite.streamlet.Read)10 Trail (suite.lp.Trail)9 Dict (suite.node.Dict)9 Tuple (suite.node.Tuple)9 HashMap (java.util.HashMap)8 Map (java.util.Map)7 Binder (suite.lp.doer.Binder)7 Str (suite.node.Str)7 Fail (suite.util.Fail)7 Suite (suite.Suite)6