Search in sources :

Example 1 with BinOp

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());
}
Also used : Reference(suite.node.Reference) BinderFactory(suite.lp.doer.BinderFactory) Tree(suite.node.Tree) BinOp(suite.util.FunUtil2.BinOp) Node(suite.node.Node) Atom(suite.node.Atom) Map(java.util.Map) FunCreator(suite.jdk.gen.FunCreator) Binder(suite.lp.doer.Binder) SwitchNode(suite.node.io.SwitchNode) FunExpr(suite.jdk.gen.FunExpression.FunExpr) FunFactory(suite.jdk.gen.FunFactory) Int(suite.node.Int) Tuple(suite.node.Tuple) Str(suite.node.Str) Binder(suite.lp.doer.Binder) Reference(suite.node.Reference) Node(suite.node.Node) SwitchNode(suite.node.io.SwitchNode) Tree(suite.node.Tree) BinOp(suite.util.FunUtil2.BinOp) SwitchNode(suite.node.io.SwitchNode) FunExpr(suite.jdk.gen.FunExpression.FunExpr) Atom(suite.node.Atom)

Example 2 with BinOp

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);
}
Also used : Random(java.util.Random) BinOp(suite.util.FunUtil2.BinOp) Test(org.junit.Test)

Aggregations

BinOp (suite.util.FunUtil2.BinOp)2 Map (java.util.Map)1 Random (java.util.Random)1 Test (org.junit.Test)1 FunCreator (suite.jdk.gen.FunCreator)1 FunExpr (suite.jdk.gen.FunExpression.FunExpr)1 FunFactory (suite.jdk.gen.FunFactory)1 Binder (suite.lp.doer.Binder)1 BinderFactory (suite.lp.doer.BinderFactory)1 Atom (suite.node.Atom)1 Int (suite.node.Int)1 Node (suite.node.Node)1 Reference (suite.node.Reference)1 Str (suite.node.Str)1 Tree (suite.node.Tree)1 Tuple (suite.node.Tuple)1 SwitchNode (suite.node.io.SwitchNode)1