use of suite.util.FunUtil.Fun in project suite by stupidsing.
the class RecursiveFactorizerTest method transform.
private FNode transform(FNode fn0) {
FTerminal from = new FTerminal(To.chars("ic-compile-better-option"));
FTerminal to = new FTerminal(To.chars("ic-new-compile-better-option"));
Iterate<FNode> fun = fn_ -> fn_.equals(from) ? to : null;
return transform(fn0, fun);
}
use of suite.util.FunUtil.Fun in project suite by stupidsing.
the class RecursiveFactorizerTest method rewriteNewArgument.
private String rewriteNewArgument(String pred0, String predx, String newArgument, String s0) {
Source<Node[]> source = () -> {
Reference[] r = new Reference[64];
for (int i = 0; i < r.length; i++) r[i] = new Reference();
Fun<String, Fun<Boolean, Node>> fun = hs -> b -> {
Source<Node> g = To.source(r);
Node head = terminalNode(hs);
Node n0 = !b ? g.source() : operatorNode(TermOp.TUPLE_, List.of(g.source(), terminalNode(" "), terminalNode(newArgument)));
Node n1 = operatorNode(g, TermOp.TUPLE_, g.source(), n0);
return operatorNode(g, TermOp.TUPLE_, head, n1);
};
return new Node[] { fun.apply(pred0).apply(false), fun.apply(predx).apply(true) };
};
FactorizeResult fr0 = recursiveFactorizer.parse(s0);
FNode fn0 = fr0.node;
Node node0 = nodify.nodify(FNode.class, fn0);
Node nodex = rw.rewrite(source, node0);
FNode fnx = nodify.unnodify(FNode.class, nodex);
FactorizeResult frx = new FactorizeResult(fr0.pre, fnx, fr0.post);
String sx = frx.unparse();
return sx;
}
use of suite.util.FunUtil.Fun in project suite by stupidsing.
the class ReduceHeadRecursion method getHeadRecursionForm.
private HeadRecursionForm getHeadRecursionForm(Grammar en0, String entity) {
List<Grammar> empty = List.of();
Grammar en = expand(en0);
HeadRecursionForm hrf;
if (en.type == GrammarType.AND___ && en.children.isEmpty())
hrf = new HeadRecursionForm(empty, empty);
else if (en.type == GrammarType.AND___) {
HeadRecursionForm hrf0 = getHeadRecursionForm(en.children.get(0), entity);
List<Grammar> tail = List_.right(en.children, 1);
Fun<List<Grammar>, List<Grammar>> fun = list -> Read.from(list).map(en_ -> {
List<Grammar> ens1 = new ArrayList<>();
ens1.add(en_);
ens1.addAll(tail);
return new Grammar(GrammarType.AND___, ens1);
}).toList();
hrf = new HeadRecursionForm(fun.apply(hrf0.listb), fun.apply(hrf0.listc));
} else if (en.type == GrammarType.NAMED_ && String_.equals(en.content, entity))
hrf = new HeadRecursionForm(empty, List.of(new Grammar(GrammarType.AND___)));
else if (en.type == GrammarType.OR____) {
List<HeadRecursionForm> hrfs = Read.from(en.children).map(en_ -> getHeadRecursionForm(en_, entity)).toList();
List<Grammar> listb = Read.from(hrfs).flatMap(hrf_ -> hrf_.listb).toList();
List<Grammar> listc = Read.from(hrfs).flatMap(hrf_ -> hrf_.listc).toList();
hrf = new HeadRecursionForm(listb, listc);
} else
hrf = new HeadRecursionForm(List.of(en), empty);
return hrf;
}
use of suite.util.FunUtil.Fun 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);
}
use of suite.util.FunUtil.Fun in project suite by stupidsing.
the class InterpretFunLazy0 method lazy_.
private Fun<IMap<String, Thunk_>, Thunk_> lazy_(Node node) {
Fun<IMap<String, Thunk_>, Thunk_> result;
Tree tree;
Node[] m;
if ((m = Suite.pattern("define .0 := .1 >> .2").match(node)) != null) {
String vk = v(m[0]);
Fun<IMap<String, Thunk_>, Thunk_> value = lazy_(m[1]);
Fun<IMap<String, Thunk_>, Thunk_> expr = lazy_(m[2]);
result = env -> {
Mutable<Thunk_> val = Mutable.nil();
IMap<String, Thunk_> env1 = env.put(vk, () -> val.get().get());
val.set(value.apply(env1)::get);
return expr.apply(env1);
};
} else if ((m = Suite.pattern("if .0 then .1 else .2").match(node)) != null) {
Fun<IMap<String, Thunk_>, Thunk_> if_ = lazy_(m[0]);
Fun<IMap<String, Thunk_>, Thunk_> then_ = lazy_(m[1]);
Fun<IMap<String, Thunk_>, Thunk_> else_ = lazy_(m[2]);
result = env -> (if_.apply(env).get() == Atom.TRUE ? then_ : else_).apply(env);
} else if ((m = Suite.pattern(".0 => .1").match(node)) != null) {
String vk = v(m[0]);
Fun<IMap<String, Thunk_>, Thunk_> value = lazy_(m[1]);
result = env -> () -> new Fun_(in -> value.apply(env.put(vk, in)));
} else if ((m = Suite.pattern(".0 {.1}").match(node)) != null) {
Fun<IMap<String, Thunk_>, Thunk_> fun = lazy_(m[0]);
Fun<IMap<String, Thunk_>, Thunk_> param = lazy_(m[1]);
result = env -> fun(fun.apply(env).get()).apply(param.apply(env));
} else if ((tree = Tree.decompose(node)) != null) {
Operator operator = tree.getOperator();
Fun<IMap<String, Thunk_>, Thunk_> p0 = lazy_(tree.getLeft());
Fun<IMap<String, Thunk_>, Thunk_> p1 = lazy_(tree.getRight());
result = env -> {
Thunk_ r0 = env.get(operator.getName());
Thunk_ r1 = fun(r0.get()).apply(p0.apply(env));
Thunk_ r2 = fun(r1.get()).apply(p1.apply(env));
return r2;
};
} else if (node instanceof Atom) {
String vk = v(node);
result = env -> env.get(vk);
} else
result = env -> () -> node;
return result;
}
Aggregations