use of suite.node.util.TreeUtil.IntInt_Bool in project suite by stupidsing.
the class InterpretFunLazy method lazy.
public Thunk_ lazy(Node node) {
Node parsed = parse(node);
Map<String, Thunk_> df = new HashMap<>();
df.put(TermOp.AND___.name, binary((a, b) -> new Pair_(a, b)));
df.put("fst", () -> new Fun_(in -> ((Pair_) in.get()).first_));
df.put("if", () -> new Fun_(a -> () -> new Fun_(b -> () -> new Fun_(c -> a.get() == Atom.TRUE ? b : c))));
df.put("snd", () -> new Fun_(in -> ((Pair_) in.get()).second));
for (Entry<Operator, IntInt_Bool> e : TreeUtil.boolOperations.entrySet()) {
IntInt_Bool fun = e.getValue();
df.put(e.getKey().getName(), binary((a, b) -> b(fun.apply(compare(a.get(), b.get()), 0))));
}
for (Entry<Operator, IntInt_Int> e : TreeUtil.intOperations.entrySet()) {
IntInt_Int fun = e.getValue();
df.put(e.getKey().getName(), binary((a, b) -> Int.of(fun.apply(i(a), i(b)))));
}
List<String> keys = df.keySet().stream().sorted().collect(Collectors.toList());
Lazy_ lazy0 = new Lazy_(0, IMap.empty());
Frame frame = new Frame(null);
for (String key : keys) {
lazy0 = lazy0.put(Atom.of(key));
frame.add(df.get(key));
}
return lazy0.lazy_(parsed).apply(frame);
}
use of suite.node.util.TreeUtil.IntInt_Bool in project suite by stupidsing.
the class InterpretFunLazy0 method lazy.
public Thunk_ lazy(Node node) {
Thunk_ error = () -> Fail.t("error termination");
IMap<String, Thunk_> env = IMap.empty();
env = env.put(Atom.TRUE.name, () -> Atom.TRUE);
env = env.put(Atom.FALSE.name, () -> Atom.FALSE);
env = env.put(TermOp.AND___.name, () -> new Fun_(a -> () -> new Fun_(b -> () -> new Pair_(a, b))));
env = env.put(ERROR.name, error);
env = env.put(FST__.name, () -> new Fun_(in -> ((Pair_) in.get()).first));
env = env.put(SND__.name, () -> new Fun_(in -> ((Pair_) in.get()).second));
for (Entry<Operator, IntInt_Bool> e : TreeUtil.boolOperations.entrySet()) {
IntInt_Bool fun = e.getValue();
env = env.put(e.getKey().getName(), () -> new Fun_(a -> () -> new Fun_(b -> () -> b(fun.apply(i(a), i(b))))));
}
for (Entry<Operator, IntInt_Int> e : TreeUtil.intOperations.entrySet()) {
IntInt_Int fun = e.getValue();
env = env.put(e.getKey().getName(), () -> new Fun_(a -> () -> new Fun_(b -> () -> Int.of(fun.apply(i(a), i(b))))));
}
return lazy_(node).apply(env);
}
use of suite.node.util.TreeUtil.IntInt_Bool in project suite by stupidsing.
the class P3Optimize method optimize_.
private Funp optimize_(Funp n) {
return //
n.<//
Funp>switch_().applyIf(FunpCoerce.class, f -> f.apply((coerce, expr) -> {
return !(expr instanceof FunpDontCare) ? n : optimize(expr);
})).applyIf(FunpData.class, f -> f.apply(pairs -> {
return FunpData.of(Read.from2(pairs).concatMap((expr, range) -> {
Funp expr1 = optimize(expr);
int start = range.t0;
Streamlet<Pair<Funp, IntIntPair>> pairsx = new //
Switch<Streamlet<Pair<Funp, IntIntPair>>>(//
expr1).applyIf(FunpData.class, g -> g.apply(pairs1 -> {
return //
Read.from2(//
pairs1).map((exprc, range1) -> Pair.of(optimize(exprc), IntIntPair.of(start + range1.t0, start + range1.t1)));
})).result();
return pairsx != null ? pairsx : Read.each(Pair.of(expr1, range));
}).toList());
})).applyIf(FunpDeref.class, f -> f.apply(pointer -> {
return optimize(pointer).<Funp>switch_().applyIf(FunpReference.class, g -> g.expr).result();
})).applyIf(FunpIf.class, f -> f.apply((if_, then, else_) -> {
return //
optimize(if_).<Funp>switch_().applyIf(FunpBoolean.class, g -> g.apply(b -> {
return b ? then : else_;
})).result();
})).applyIf(FunpMemory.class, f -> f.apply((pointer, start, end) -> {
return //
optimize(pointer).<Funp>switch_().applyIf(FunpData.class, g -> g.apply(pairs -> {
for (Pair<Funp, IntIntPair> pair : pairs) {
IntIntPair range = pair.t1;
if (start == range.t0 && end == range.t1)
return pair.t0;
}
return null;
})).applyIf(FunpReference.class, g -> {
return FunpTree.of(TermOp.PLUS__, g.expr, FunpNumber.ofNumber(start));
}).result();
})).applyIf(FunpReference.class, f -> f.apply(expr -> {
return optimize(expr).<Funp>switch_().applyIf(FunpMemory.class, g -> g.pointer).result();
})).applyIf(FunpTree.class, f -> f.apply((operator, lhs, rhs) -> {
IntInt_Bool iib = TreeUtil.boolOperations.get(operator);
IntInt_Int iii = TreeUtil.intOperations.get(operator);
if (iib != null)
return evaluate(iib, lhs, rhs);
else if (iii != null)
return evaluate(iii, lhs, rhs);
else
return null;
})).applyIf(FunpTree2.class, f -> f.apply((operator, lhs, rhs) -> {
return evaluate(TreeUtil.tupleOperations.get(operator), lhs, rhs);
})).applyIf(FunpWhile.class, f -> f.apply((while_, do_, expr) -> {
return //
optimize(while_).<Funp>switch_().applyIf(FunpBoolean.class, g -> g.apply(b -> {
return b ? null : expr;
})).result();
})).result();
}
use of suite.node.util.TreeUtil.IntInt_Bool in project suite by stupidsing.
the class InterpretFunEager method eager.
public Node eager(Node node) {
Node mode = isLazyify ? Atom.of("LAZY") : Atom.of("EAGER");
Node query = Suite.substitute("source .in, fc-process-function .0 .in .out, sink .out", mode);
RuleSet rs = Suite.newRuleSet(List.of("auto.sl", "fc/fc.sl"));
Finder finder = new SewingProverBuilder2().build(rs).apply(query);
Node parsed = FindUtil.collectSingle(finder, node);
IntrinsicCallback ic = isLazyify ? lazyIntrinsicCallback() : Intrinsics.eagerIntrinsicCallback;
Map<String, Node> df = new HashMap<>();
df.put(TermOp.AND___.name, f2((a, b) -> Tree.of(TermOp.AND___, a, b)));
df.put("+call%i-t1", f1(i -> fn(1, l -> Data.<Intrinsic>get(i).invoke(ic, l))));
df.put("+call%i-t2", f1(i -> fn(2, l -> Data.<Intrinsic>get(i).invoke(ic, l))));
df.put("+call%i-t3", f1(i -> fn(3, l -> Data.<Intrinsic>get(i).invoke(ic, l))));
df.put("+call%i-v1", f1(i -> fn(1, l -> Data.<Intrinsic>get(i).invoke(ic, l))));
df.put("+call%i-v2", f1(i -> fn(2, l -> Data.<Intrinsic>get(i).invoke(ic, l))));
df.put("+call%i-v3", f1(i -> fn(3, l -> Data.<Intrinsic>get(i).invoke(ic, l))));
df.put("+compare", f2((a, b) -> Int.of(Comparer.comparer.compare(a, b))));
df.put("+get%i", f1(a -> new Data<>(Intrinsics.intrinsics.get(((Atom) a).name.split("!")[1]))));
df.put("+is-list", f1(a -> b(Tree.decompose(a) != null)));
df.put("+is-pair", f1(a -> b(Tree.decompose(a) != null)));
df.put("+lcons", f2((a, b) -> Tree.of(TermOp.OR____, a, b)));
df.put("+lhead", f1(a -> Tree.decompose(a).getLeft()));
df.put("+ltail", f1(a -> Tree.decompose(a).getRight()));
df.put("+pcons", f2((a, b) -> Tree.of(TermOp.AND___, a, b)));
df.put("+pleft", f1(a -> Tree.decompose(a).getLeft()));
df.put("+pright", f1(a -> Tree.decompose(a).getRight()));
for (Entry<Operator, IntInt_Bool> e : TreeUtil.boolOperations.entrySet()) {
IntInt_Bool fun = e.getValue();
df.put(e.getKey().getName(), f2((a, b) -> b(fun.apply(compare(a, b), 0))));
}
for (Entry<Operator, IntInt_Int> e : TreeUtil.intOperations.entrySet()) {
IntInt_Int fun = e.getValue();
df.put(e.getKey().getName(), f2((a, b) -> Int.of(fun.apply(i(a), i(b)))));
}
List<String> keys = df.keySet().stream().sorted().collect(Collectors.toList());
Eager_ eager0 = new Eager_(0, IMap.empty());
Frame frame = new Frame(null);
for (String key : keys) {
eager0 = eager0.put(Atom.of(key));
frame.add(df.get(key));
}
return eager0.eager_(parsed).apply(frame);
}
Aggregations