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();
}
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);
}
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);
}
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();
}
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;
}
Aggregations