Search in sources :

Example 1 with APPLY

use of suite.fp.match.Matchers.APPLY 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 APPLY

use of suite.fp.match.Matchers.APPLY 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);
}
Also used : IntInt_Bool(suite.node.util.TreeUtil.IntInt_Bool) APPLY(suite.fp.match.Matchers.APPLY) DECONS(suite.fp.match.Matchers.DECONS) Fun(suite.util.FunUtil.Fun) BinOp(suite.util.FunUtil2.BinOp) Node(suite.node.Node) BOOLEAN(suite.fp.match.Matchers.BOOLEAN) VAR(suite.fp.match.Matchers.VAR) Map(java.util.Map) FindUtil(suite.lp.search.FindUtil) RuleSet(suite.lp.kb.RuleSet) NUMBER(suite.fp.match.Matchers.NUMBER) ERROR(suite.fp.match.Matchers.ERROR) IntrinsicCallback(suite.fp.intrinsic.Intrinsics.IntrinsicCallback) TREE(suite.fp.match.Matchers.TREE) IMap(suite.immutable.IMap) FUN(suite.fp.match.Matchers.FUN) To(suite.util.To) Intrinsic(suite.fp.intrinsic.Intrinsics.Intrinsic) 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) SewingProverBuilder2(suite.lp.search.SewingProverBuilder2) 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) Intrinsics(suite.fp.intrinsic.Intrinsics) DEFVARS(suite.fp.match.Matchers.DEFVARS) Suite(suite.Suite) Source(suite.util.FunUtil.Source) Finder(suite.lp.search.ProverBuilder.Finder) Pattern(suite.BindArrayUtil.Pattern) TermOp(suite.node.io.TermOp) ATOM(suite.fp.match.Matchers.ATOM) WRAP(suite.fp.match.Matchers.WRAP) CHARS(suite.fp.match.Matchers.CHARS) Atom(suite.node.Atom) Operator(suite.node.io.Operator) Fail(suite.util.Fail) Str(suite.node.Str) Operator(suite.node.io.Operator) RuleSet(suite.lp.kb.RuleSet) IntrinsicCallback(suite.fp.intrinsic.Intrinsics.IntrinsicCallback) HashMap(java.util.HashMap) Node(suite.node.Node) Finder(suite.lp.search.ProverBuilder.Finder) SewingProverBuilder2(suite.lp.search.SewingProverBuilder2) Data(suite.node.Data) Atom(suite.node.Atom) IntInt_Int(suite.primitive.IntInt_Int) Intrinsic(suite.fp.intrinsic.Intrinsics.Intrinsic) IntInt_Bool(suite.node.util.TreeUtil.IntInt_Bool)

Aggregations

ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Entry (java.util.Map.Entry)2 Collectors (java.util.stream.Collectors)2 Pattern (suite.BindArrayUtil.Pattern)2 Suite (suite.Suite)2 Pair (suite.adt.pair.Pair)2 Matcher (suite.fp.match.Matcher)2 APPLY (suite.fp.match.Matchers.APPLY)2 ATOM (suite.fp.match.Matchers.ATOM)2 BOOLEAN (suite.fp.match.Matchers.BOOLEAN)2 CHARS (suite.fp.match.Matchers.CHARS)2 CONS (suite.fp.match.Matchers.CONS)2 DECONS (suite.fp.match.Matchers.DECONS)2 DEFVARS (suite.fp.match.Matchers.DEFVARS)2 ERROR (suite.fp.match.Matchers.ERROR)2 FUN (suite.fp.match.Matchers.FUN)2 IF (suite.fp.match.Matchers.IF)2