use of suite.util.FunUtil2.BinOp in project suite by stupidsing.
the class CompileBinderImpl method binder.
public Bind_ binder(Node node) {
FunCreator<Bind_> fc = FunCreator.of(Bind_.class, false);
return fc.create(new BinOp<>() {
private FunExpr env, trail, b;
public FunExpr apply(FunExpr bindEnv, FunExpr target) {
this.env = bindEnv.field("env");
this.trail = bindEnv.field("trail");
return f.declare(ok, b -> {
this.b = b;
return compile_(node, target);
});
}
private FunExpr compile_(Node node, FunExpr target) {
FunExpr br = bind(f.object_(node, Node.class), target);
FunExpr brc = bindClone(node, target);
return new //
SwitchNode<FunExpr>(//
node).applyIf(Atom.class, n -> {
return f.ifEquals(target, f.object(node), ok, br);
}).applyIf(Int.class, n -> {
return f.ifInstance(Int.class, target, i -> f.ifEquals(i.field("number"), f.int_(n.number), ok, fail), br);
}).applyIf(Reference.class, n -> {
FunExpr ref = env.field("refs").index(f.int_(mapper().computeIndex(n)));
return f.invokeStatic(Binder.class, "bind", target, ref.cast_(Node.class), trail);
}).applyIf(Str.class, n -> {
return f.ifInstance(Str.class, target, s -> f.object(n.value).invoke("equals", s.field("value").cast_(Object.class)), br);
}).applyIf(Tree.class, tree -> {
return f.declare(f.invokeStatic(Tree.class, "decompose", target, f.object(tree.getOperator())), t -> {
Node lt = tree.getLeft();
Node rt = tree.getRight();
return f.ifNonNull(t, compile_(lt, t.invoke("getLeft"), compile_(rt, t.invoke("getRight"), ok)), brc);
});
}).applyIf(Tuple.class, n -> {
return f.ifInstance(Tuple.class, target, tuple -> f.declare(tuple.field("nodes"), targets -> {
Node[] nodes = n.nodes;
FunExpr fe = ok;
for (int i = 0; i < nodes.length; i++) fe = compile_(nodes[i], targets.index(f.int_(i)), fe);
return f.if_(targets.length(), fe, brc);
}), brc);
}).applyIf(Node.class, n -> {
Clone_ cloner = cloner(n);
return f.invokeStatic(Binder.class, "bind", target, f.object(cloner).invoke("apply", env), trail);
}).nonNullResult();
}
private FunExpr compile_(Node node, FunExpr target, FunExpr cps) {
return f.assign(b, compile_(node, target), f.if_(b, cps, fail));
}
private FunExpr bindClone(Node node, FunExpr target) {
if (isBindTrees)
return bind(f.object(cloner(node)).apply(env), target);
else
return fail;
}
private FunExpr bind(FunExpr node, FunExpr target) {
return f.ifInstanceAnd(Reference.class, target, ref -> f.seq(trail.invoke("addBind", ref, node), ok));
}
}).apply(Map.ofEntries());
}
use of suite.util.FunUtil2.BinOp in project suite by stupidsing.
the class NeuralNetworkTest method test.
@Test
public void test() {
Pair<String, BinOp<Boolean>> op0 = Pair.of("and", (b0, b1) -> b0 && b1);
Pair<String, BinOp<Boolean>> op1 = Pair.of("or", (b0, b1) -> b0 || b1);
Pair<String, BinOp<Boolean>> op2 = Pair.of("xor", (b0, b1) -> b0 ^ b1);
boolean[] booleans = new boolean[] { false, true };
Random random = new Random();
boolean result = true;
for (Pair<String, BinOp<Boolean>> pair : List.of(op0, op1, op2)) result &= pair.map((name, oper) -> {
NeuralNetwork nn = new NeuralNetwork();
Layer<float[], float[]> train = nn.ml(new int[] { 2, 4, 1 });
for (int i = 0; i < 16384; i++) {
boolean b0 = random.nextBoolean();
boolean b1 = random.nextBoolean();
float[] in = input(b0, b1);
float[] expect = new float[] { f(oper.apply(b0, b1)) };
Out<float[], float[]> out = train.feed(in);
float[] actual = out.output;
out.backprop.apply(vec.sub(expect, actual));
}
boolean result_ = true;
for (boolean b0 : booleans) for (boolean b1 : booleans) {
float[] in = input(b0, b1);
boolean out = oper.apply(b0, b1);
float f = train.feed(in).output[0];
System.out.println(b0 + " " + name + " " + b1 + " = " + f);
result_ &= out == .5f < f;
}
return result_;
});
assertTrue(result);
}
Aggregations