use of suite.primitive.IntInt_Int 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.primitive.IntInt_Int 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.primitive.IntInt_Int 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.primitive.IntInt_Int in project suite by stupidsing.
the class SewingExpressionImpl method evaluator.
public Evaluate_ evaluator(Node node) {
Tree tree = Tree.decompose(node);
if (tree != null) {
Operator op = tree.getOperator();
Evaluate_ lhs, rhs;
IntInt_Int fun;
if (op == TermOp.TUPLE_) {
Tree rightTree = Tree.decompose(tree.getRight());
lhs = evaluator(tree.getLeft());
rhs = evaluator(rightTree.getRight());
fun = TreeUtil.evaluateOp(rightTree.getLeft());
} else {
lhs = evaluator(tree.getLeft());
rhs = evaluator(tree.getRight());
fun = TreeUtil.evaluateOp(op);
}
return env -> fun.apply(lhs.evaluate(env), rhs.evaluate(env));
} else if (node instanceof Int) {
int i = ((Int) node).number;
return env -> i;
} else {
Clone_ f = clonerFactory.cloner(node);
return env -> TreeUtil.evaluate(f.apply(env));
}
}
use of suite.primitive.IntInt_Int in project suite by stupidsing.
the class PlotMain method run.
@Override
protected boolean run(String[] args) {
int size = 1024;
double scale = 1d / (size + 1);
DblDbl_Dbl variety = (x, y) -> {
return y * y - (x + .25f) * (x + .15f) * (x + .05f) * (x - .05f) * (x - .15f) * (x - .25f);
};
IntInt_Int fp = (fx, fy) -> {
double x0 = fx * scale - .5d;
double y0 = fy * scale - .5d;
double value = variety.apply(x0, y0);
if (Double.isNaN(value))
return -2;
else if (value < 0)
return -1;
else
return 1;
};
return //
new Render().renderPixels(size, size, (fx, fy) -> {
int b0 = fp.apply(fx, fy);
int b1 = fp.apply(fx + 1, fy);
int b2 = fp.apply(fx, fy + 1);
int b3 = fp.apply(fx + 1, fy + 1);
double c = b0 != b1 || b1 != b2 || b2 != b3 ? 1d : 0d;
return new R3(c, c, c);
}).view();
}
Aggregations