Search in sources :

Example 31 with Pair

use of suite.adt.pair.Pair 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 32 with Pair

use of suite.adt.pair.Pair in project suite by stupidsing.

the class SewingClonerImpl method cloner.

@Override
public Clone_ cloner(Node node) {
    List<Clone_> funs = new ArrayList<>();
    Clone_ fun;
    while (true) {
        Node node0 = node;
        Tree tree;
        if (node0 instanceof Dict) {
            Clone_[][] array = // 
            Read.from2(// 
            ((Dict) node0).map).map(// 
            (key, value) -> new Clone_[] { cloner(key), cloner(value) }).toArray(Clone_[].class);
            int length = array.length;
            return 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____) {
                Clone_ f = cloner(tree.getLeft());
                funs.add(env -> Tree.of(operator, f.apply(env), null));
                node = tree.getRight();
                continue;
            } else {
                // delay generalizing for performance
                Clone_ lf = cloner(tree.getLeft());
                Clone_ rf = cloner(tree.getRight());
                fun = env -> Tree.of(operator, lf.apply(env), new Suspend(() -> rf.apply(env)));
            }
        } else if (node0 instanceof Reference) {
            int index = vm.computeIndex((Reference) node0);
            fun = env -> env.get(index);
        } else if (node0 instanceof Tuple) {
            Clone_[] ps = Read.from(((Tuple) node0).nodes).map(this::cloner).toArray(Clone_.class);
            int size = ps.length;
            fun = env -> {
                Node[] nodes = new Node[size];
                for (int i = 0; i < size; i++) nodes[i] = ps[i].apply(env);
                return Tuple.of(nodes);
            };
        } 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 (Clone_ 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) 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) VariableMapper(suite.lp.sewing.VariableMapper) Suspend(suite.node.Suspend) ClonerFactory(suite.lp.doer.ClonerFactory) 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) Dict(suite.node.Dict) Tree(suite.node.Tree) Tuple(suite.node.Tuple)

Example 33 with Pair

use of suite.adt.pair.Pair in project suite by stupidsing.

the class Grapher method ungraph_.

private Node ungraph_(int id) {
    int size = gns.size();
    List<Node> nodes = // 
    Read.from(// 
    gns).map(gn -> {
        switch(gn.type) {
            case DICT:
                return new Dict();
            case TERM:
                return gn.terminal;
            case TREE:
                return Tree.of(gn.op, null, null);
            case TUPLE:
                return Tuple.of(new Node[gn.children.size()]);
            default:
                return Fail.t();
        }
    }).toList();
    for (int i = 0; i < size; i++) {
        GN gn = gns.get(i);
        Node node = nodes.get(i);
        List<Pair<Node, Node>> children = Read.from(gn.children).map(p -> Pair.of(nodes.get(p.t0), nodes.get(p.t1))).toList();
        switch(gn.type) {
            case DICT:
                ((Dict) node).map.putAll(Read.from2(children).mapValue(Reference::of).collect(As::map));
                break;
            case TERM:
                break;
            case TREE:
                Tree tree = (Tree) node;
                Tree.forceSetLeft(tree, children.get(0).t1);
                Tree.forceSetRight(tree, children.get(1).t1);
                break;
            case TUPLE:
                Node[] list = ((Tuple) node).nodes;
                for (int j = 0; j < children.size(); j++) list[j] = children.get(j).t1;
        }
    }
    return nodes.get(id);
}
Also used : DataInputStream(java.io.DataInputStream) Read(suite.streamlet.Read) HashMap(java.util.HashMap) Deque(java.util.Deque) ArrayList(java.util.ArrayList) Node(suite.node.Node) HashSet(java.util.HashSet) ProverConstant(suite.lp.doer.ProverConstant) DataOutputStream(java.io.DataOutputStream) Map(java.util.Map) IdentityKey(suite.adt.IdentityKey) Binder(suite.lp.doer.Binder) Tuple(suite.node.Tuple) Dict(suite.node.Dict) Reference(suite.node.Reference) Trail(suite.lp.Trail) IntIntPair(suite.primitive.adt.pair.IntIntPair) Set(java.util.Set) IOException(java.io.IOException) NodeRead(suite.node.io.Rewrite_.NodeRead) NodeHead(suite.node.io.Rewrite_.NodeHead) IntObjMap(suite.primitive.adt.map.IntObjMap) Tree(suite.node.Tree) Objects(java.util.Objects) Pair(suite.adt.pair.Pair) List(java.util.List) Atom(suite.node.Atom) Entry(java.util.Map.Entry) As(suite.streamlet.As) ArrayDeque(java.util.ArrayDeque) Int(suite.node.Int) Fail(suite.util.Fail) Str(suite.node.Str) ReadType(suite.node.io.Rewrite_.ReadType) Dict(suite.node.Dict) Reference(suite.node.Reference) Node(suite.node.Node) Tree(suite.node.Tree) Tuple(suite.node.Tuple) IntIntPair(suite.primitive.adt.pair.IntIntPair) Pair(suite.adt.pair.Pair)

Example 34 with Pair

use of suite.adt.pair.Pair in project suite by stupidsing.

the class ReversePolish method fromRpn.

public Node fromRpn(Reader reader) throws IOException {
    BufferedReader br = new BufferedReader(reader);
    Map<String, Reference> references = new HashMap<>();
    Deque<Node> deque = new ArrayDeque<>();
    br.lines().filter(elem -> !elem.isEmpty()).forEach(elem -> {
        char type = elem.charAt(0);
        String s = elem.substring(1);
        Node n;
        if (type == '\\')
            n = Atom.of(s);
        else if (type == '^') {
            String[] a = s.split(":");
            int size = Integer.valueOf(a[3]);
            List<Pair<Node, Node>> children = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                Node key = deque.pop();
                Node value = deque.pop();
                children.add(Pair.of(key, value));
            }
            n = new // 
            NodeWrite(// 
            ReadType.valueOf(a[0]), // 
            !String_.equals(a[1], "null") ? Suite.parse(a[1]) : null, // 
            TermOp.valueOf(a[2]), children).node;
        // n = Suite.parse(s);
        } else if (type == 'i')
            n = Int.of(Integer.parseInt(s));
        else if (type == 'r')
            n = references.computeIfAbsent(s, key -> new Reference());
        else if (type == 't') {
            TermOp op = TermOp.valueOf(s);
            Node left = deque.pop();
            Node right = deque.pop();
            n = Tree.of(op, left, right);
        } else
            n = Fail.t("RPN conversion error: " + elem);
        deque.push(n);
    });
    return deque.pop();
}
Also used : Reference(suite.node.Reference) Suite(suite.Suite) NodeWrite(suite.node.io.Rewrite_.NodeWrite) IOException(java.io.IOException) HashMap(java.util.HashMap) NodeRead(suite.node.io.Rewrite_.NodeRead) Reader(java.io.Reader) Deque(java.util.Deque) Tree(suite.node.Tree) ArrayList(java.util.ArrayList) Node(suite.node.Node) Pair(suite.adt.pair.Pair) List(java.util.List) String_(suite.util.String_) StringReader(java.io.StringReader) Rethrow(suite.util.Rethrow) Atom(suite.node.Atom) Map(java.util.Map) BufferedReader(java.io.BufferedReader) ArrayDeque(java.util.ArrayDeque) Int(suite.node.Int) Fail(suite.util.Fail) ReadType(suite.node.io.Rewrite_.ReadType) HashMap(java.util.HashMap) Reference(suite.node.Reference) Node(suite.node.Node) ArrayDeque(java.util.ArrayDeque) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) List(java.util.List)

Example 35 with Pair

use of suite.adt.pair.Pair in project suite by stupidsing.

the class P2InferType method extractPredefine.

private Funp extractPredefine(Funp node0) {
    List<Pair<String, Funp>> evs = new ArrayList<>();
    Funp node1 = new Object() {

        private Funp extract_(Funp n) {
            return inspect.rewrite(Funp.class, n_ -> {
                return // 
                n_.<// 
                Funp>switch_().applyIf(FunpDefine.class, f -> f.apply((isPolyType, var, value, expr) -> {
                    return FunpDefine.of(isPolyType, var, extractPredefine(value), extract_(expr));
                })).applyIf(FunpDefineRec.class, f -> f.apply((pairs0, expr) -> {
                    List<Pair<String, Funp>> pairs1 = Read.from2(pairs0).mapValue(P2InferType.this::extractPredefine).toList();
                    return FunpDefineRec.of(pairs1, extract_(expr));
                })).applyIf(FunpGlobal.class, f -> f.apply((var, value, expr) -> {
                    return FunpGlobal.of(var, extractPredefine(value), extract_(expr));
                })).applyIf(FunpLambda.class, f -> f.apply((var, expr) -> {
                    return FunpLambda.of(var, extractPredefine(expr));
                })).applyIf(FunpPredefine.class, f -> f.apply(expr -> {
                    String ev = "ev" + Util.temp();
                    evs.add(Pair.of(ev, expr));
                    Funp var = FunpVariable.of(ev);
                    return FunpAssignReference.of(FunpReference.of(var), expr, var);
                })).result();
            }, n);
        }
    }.extract_(node0);
    for (Pair<String, Funp> pair : evs) node1 = FunpDefine.of(false, pair.t0, FunpDontCare.of(), node1);
    return node1;
}
Also used : Funp(suite.funp.Funp_.Funp) FunpAllocGlobal(suite.funp.P2.FunpAllocGlobal) FunpInvokeIo(suite.funp.P2.FunpInvokeIo) Mutable(suite.adt.Mutable) FunpOperand(suite.funp.P2.FunpOperand) FunpDefine(suite.funp.P0.FunpDefine) Fun(suite.util.FunUtil.Fun) FunpLambda(suite.funp.P0.FunpLambda) FunpFramePointer(suite.funp.P2.FunpFramePointer) FunpPredefine(suite.funp.P0.FunpPredefine) Operand(suite.assembler.Amd64.Operand) Map(java.util.Map) FunpWhile(suite.funp.P2.FunpWhile) FunpData(suite.funp.P2.FunpData) FunpInvoke(suite.funp.P2.FunpInvoke) UnNode(suite.fp.Unify.UnNode) FunpCoerce(suite.funp.P0.FunpCoerce) IntIntPair(suite.primitive.adt.pair.IntIntPair) FunpDefineRec(suite.funp.P0.FunpDefineRec) IMap(suite.immutable.IMap) Set(java.util.Set) ISet(suite.immutable.ISet) Unify(suite.fp.Unify) FunpRoutineIo(suite.funp.P2.FunpRoutineIo) Pair(suite.adt.pair.Pair) List(java.util.List) FunpInvoke2(suite.funp.P2.FunpInvoke2) FunpRepeat(suite.funp.P0.FunpRepeat) FunpDeref(suite.funp.P0.FunpDeref) FunpMemory(suite.funp.P2.FunpMemory) FunpSaveRegisters(suite.funp.P2.FunpSaveRegisters) FunpVariable(suite.funp.P0.FunpVariable) FunpIterate(suite.funp.P0.FunpIterate) Read(suite.streamlet.Read) Singleton(suite.node.util.Singleton) AutoObject(suite.util.AutoObject) FunpBoolean(suite.funp.P0.FunpBoolean) Util(suite.util.Util) FunpArray(suite.funp.P0.FunpArray) HashMap(java.util.HashMap) OpReg(suite.assembler.Amd64.OpReg) FunpIndex(suite.funp.P0.FunpIndex) FunpIoCat(suite.funp.P0.FunpIoCat) FunpReference(suite.funp.P0.FunpReference) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) FunpApply(suite.funp.P0.FunpApply) FunpLambdaCapture(suite.funp.P0.FunpLambdaCapture) String_(suite.util.String_) FunpIf(suite.funp.P0.FunpIf) Rethrow(suite.util.Rethrow) FunpGlobal(suite.funp.P0.FunpGlobal) FunpIo(suite.funp.P0.FunpIo) FunpAsm(suite.funp.P0.FunpAsm) Inspect(suite.inspect.Inspect) FunpTree(suite.funp.P0.FunpTree) FunpField(suite.funp.P0.FunpField) TermOp(suite.node.io.TermOp) FunpAssign(suite.funp.P2.FunpAssign) FunpAssignReference(suite.funp.P0.FunpAssignReference) FunpCheckType(suite.funp.P0.FunpCheckType) FunpTree2(suite.funp.P0.FunpTree2) Funp(suite.funp.Funp_.Funp) FunpError(suite.funp.P0.FunpError) FunpRoutine2(suite.funp.P2.FunpRoutine2) Obj_Int(suite.primitive.IntPrimitives.Obj_Int) FunpNumber(suite.funp.P0.FunpNumber) FunpRoutine(suite.funp.P2.FunpRoutine) FixieFun1(suite.adt.pair.Fixie_.FixieFun1) FixieFun2(suite.adt.pair.Fixie_.FixieFun2) Switch(suite.util.Switch) FunpStruct(suite.funp.P0.FunpStruct) FixieFun0(suite.adt.pair.Fixie_.FixieFun0) FunpAllocStack(suite.funp.P2.FunpAllocStack) Fail(suite.util.Fail) FunpDontCare(suite.funp.P0.FunpDontCare) FunpDefineRec(suite.funp.P0.FunpDefineRec) ArrayList(java.util.ArrayList) FunpLambda(suite.funp.P0.FunpLambda) AutoObject(suite.util.AutoObject) IntIntPair(suite.primitive.adt.pair.IntIntPair) Pair(suite.adt.pair.Pair)

Aggregations

Pair (suite.adt.pair.Pair)36 ArrayList (java.util.ArrayList)22 List (java.util.List)22 Read (suite.streamlet.Read)22 HashMap (java.util.HashMap)16 Map (java.util.Map)16 Node (suite.node.Node)14 Fail (suite.util.Fail)13 Fun (suite.util.FunUtil.Fun)12 Reference (suite.node.Reference)11 Tree (suite.node.Tree)10 TermOp (suite.node.io.TermOp)10 Set (java.util.Set)9 Atom (suite.node.Atom)9 Int (suite.node.Int)9 As (suite.streamlet.As)8 IOException (java.io.IOException)7 HashSet (java.util.HashSet)7 Streamlet (suite.streamlet.Streamlet)7 Time (suite.trade.Time)6