Search in sources :

Example 1 with IntInt_Int

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);
}
Also used : IntInt_Bool(suite.node.util.TreeUtil.IntInt_Bool) APPLY(suite.fp.match.Matchers.APPLY) Prover(suite.lp.doer.Prover) DECONS(suite.fp.match.Matchers.DECONS) Mutable(suite.adt.Mutable) Fun(suite.util.FunUtil.Fun) Node(suite.node.Node) BOOLEAN(suite.fp.match.Matchers.BOOLEAN) VAR(suite.fp.match.Matchers.VAR) Map(java.util.Map) NUMBER(suite.fp.match.Matchers.NUMBER) ERROR(suite.fp.match.Matchers.ERROR) TREE(suite.fp.match.Matchers.TREE) IMap(suite.immutable.IMap) FUN(suite.fp.match.Matchers.FUN) To(suite.util.To) Collectors(java.util.stream.Collectors) Matcher(suite.fp.match.Matcher) PRAGMA(suite.fp.match.Matchers.PRAGMA) Tree(suite.node.Tree) UNWRAP(suite.fp.match.Matchers.UNWRAP) IntInt_Int(suite.primitive.IntInt_Int) Iterate(suite.util.FunUtil.Iterate) Pair(suite.adt.pair.Pair) List(java.util.List) Entry(java.util.Map.Entry) Int(suite.node.Int) TreeUtil(suite.node.util.TreeUtil) IF(suite.fp.match.Matchers.IF) Comparer(suite.node.util.Comparer) HashMap(java.util.HashMap) TCO(suite.fp.match.Matchers.TCO) ArrayList(java.util.ArrayList) CONS(suite.fp.match.Matchers.CONS) Data(suite.node.Data) Formatter(suite.node.io.Formatter) DEFVARS(suite.fp.match.Matchers.DEFVARS) Reference(suite.node.Reference) Suite(suite.Suite) Pattern(suite.BindArrayUtil.Pattern) TermOp(suite.node.io.TermOp) ATOM(suite.fp.match.Matchers.ATOM) WRAP(suite.fp.match.Matchers.WRAP) Streamlet(suite.streamlet.Streamlet) CHARS(suite.fp.match.Matchers.CHARS) Atom(suite.node.Atom) Operator(suite.node.io.Operator) Fail(suite.util.Fail) Str(suite.node.Str) BiFun(suite.util.FunUtil2.BiFun) Operator(suite.node.io.Operator) HashMap(java.util.HashMap) Node(suite.node.Node) IntInt_Int(suite.primitive.IntInt_Int) IntInt_Bool(suite.node.util.TreeUtil.IntInt_Bool)

Example 2 with IntInt_Int

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);
}
Also used : Suite(suite.Suite) IntInt_Bool(suite.node.util.TreeUtil.IntInt_Bool) IMap(suite.immutable.IMap) Mutable(suite.adt.Mutable) TermOp(suite.node.io.TermOp) Fun(suite.util.FunUtil.Fun) Tree(suite.node.Tree) Node(suite.node.Node) IntInt_Int(suite.primitive.IntInt_Int) Iterate(suite.util.FunUtil.Iterate) Atom(suite.node.Atom) Entry(java.util.Map.Entry) Int(suite.node.Int) Operator(suite.node.io.Operator) TreeUtil(suite.node.util.TreeUtil) Fail(suite.util.Fail) Operator(suite.node.io.Operator) IntInt_Int(suite.primitive.IntInt_Int) IntInt_Bool(suite.node.util.TreeUtil.IntInt_Bool)

Example 3 with IntInt_Int

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();
}
Also used : IntInt_Bool(suite.node.util.TreeUtil.IntInt_Bool) Read(suite.streamlet.Read) Singleton(suite.node.util.Singleton) FunpCoerce(suite.funp.P0.FunpCoerce) IntIntPair(suite.primitive.adt.pair.IntIntPair) FunpBoolean(suite.funp.P0.FunpBoolean) Inspect(suite.inspect.Inspect) FunpTree(suite.funp.P0.FunpTree) TermOp(suite.node.io.TermOp) FunpReference(suite.funp.P0.FunpReference) FunpTree2(suite.funp.P0.FunpTree2) Funp(suite.funp.Funp_.Funp) IntInt_Int(suite.primitive.IntInt_Int) Pair(suite.adt.pair.Pair) Streamlet(suite.streamlet.Streamlet) FunpIf(suite.funp.P0.FunpIf) FunpNumber(suite.funp.P0.FunpNumber) FunpDeref(suite.funp.P0.FunpDeref) FunpMemory(suite.funp.P2.FunpMemory) FunpWhile(suite.funp.P2.FunpWhile) Switch(suite.util.Switch) FunpData(suite.funp.P2.FunpData) TreeUtil(suite.node.util.TreeUtil) FunpDontCare(suite.funp.P0.FunpDontCare) FunpDontCare(suite.funp.P0.FunpDontCare) FunpReference(suite.funp.P0.FunpReference) FunpWhile(suite.funp.P2.FunpWhile) FunpMemory(suite.funp.P2.FunpMemory) FunpBoolean(suite.funp.P0.FunpBoolean) Funp(suite.funp.Funp_.Funp) FunpDeref(suite.funp.P0.FunpDeref) Streamlet(suite.streamlet.Streamlet) FunpCoerce(suite.funp.P0.FunpCoerce) IntInt_Int(suite.primitive.IntInt_Int) FunpTree(suite.funp.P0.FunpTree) IntInt_Bool(suite.node.util.TreeUtil.IntInt_Bool) IntIntPair(suite.primitive.adt.pair.IntIntPair) IntIntPair(suite.primitive.adt.pair.IntIntPair) Pair(suite.adt.pair.Pair)

Example 4 with IntInt_Int

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));
    }
}
Also used : Operator(suite.node.io.Operator) IntInt_Int(suite.primitive.IntInt_Int) Clone_(suite.lp.doer.ClonerFactory.Clone_) ClonerFactory(suite.lp.doer.ClonerFactory) EvaluatorFactory(suite.lp.doer.EvaluatorFactory) TermOp(suite.node.io.TermOp) Int(suite.node.Int) Operator(suite.node.io.Operator) TreeUtil(suite.node.util.TreeUtil) Tree(suite.node.Tree) Node(suite.node.Node) Clone_(suite.lp.doer.ClonerFactory.Clone_) Tree(suite.node.Tree) IntInt_Int(suite.primitive.IntInt_Int) IntInt_Int(suite.primitive.IntInt_Int) Int(suite.node.Int)

Example 5 with IntInt_Int

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();
}
Also used : IntInt_Int(suite.primitive.IntInt_Int) ExecutableProgram(suite.util.RunUtil.ExecutableProgram) DblDbl_Dbl(suite.primitive.DblDbl_Dbl) Render(suite.image.Render) RunUtil(suite.util.RunUtil) Render(suite.image.Render) DblDbl_Dbl(suite.primitive.DblDbl_Dbl) IntInt_Int(suite.primitive.IntInt_Int)

Aggregations

IntInt_Int (suite.primitive.IntInt_Int)7 Int (suite.node.Int)5 Tree (suite.node.Tree)5 Operator (suite.node.io.Operator)5 TermOp (suite.node.io.TermOp)4 TreeUtil (suite.node.util.TreeUtil)4 Entry (java.util.Map.Entry)3 Suite (suite.Suite)3 Pair (suite.adt.pair.Pair)3 IMap (suite.immutable.IMap)3 Atom (suite.node.Atom)3 Node (suite.node.Node)3 IntInt_Bool (suite.node.util.TreeUtil.IntInt_Bool)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 Pattern (suite.BindArrayUtil.Pattern)2 Mutable (suite.adt.Mutable)2