use of suite.node.io.Operator 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);
}
use of suite.node.io.Operator 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);
}
use of suite.node.io.Operator in project suite by stupidsing.
the class SystemPredicates method call.
public Boolean call(Node query) {
BuiltinPredicate predicate;
Tree tree;
Operator op;
Node left;
String name = null;
Node pass = null;
if (query instanceof Atom) {
name = ((Atom) query).name;
pass = Atom.NIL;
} else if ((tree = Tree.decompose(query)) != null)
if ((op = tree.getOperator()) != TermOp.TUPLE_) {
name = op.getName();
pass = query;
} else if ((left = tree.getLeft()) instanceof Atom) {
name = ((Atom) left).name;
pass = tree.getRight();
}
predicate = name != null ? get(name) : null;
return predicate != null ? predicate.prove(prover, pass) : null;
}
use of suite.node.io.Operator 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));
}
}
use of suite.node.io.Operator in project suite by stupidsing.
the class SewingGeneralizerImpl method generalizer.
@Override
public Generalize_ generalizer(Node node) {
List<Generalize_> funs = new ArrayList<>();
Generalize_ fun;
while (true) {
Node node0 = node;
Tree tree;
if (node0 instanceof Atom) {
Atom atom = (Atom) node0;
String name = atom.name;
if (ProverConstant.isCut(node0) || ProverConstant.isVariable(name)) {
int index = vm.computeIndex(atom);
fun = env -> env.get(index);
} else if (ProverConstant.isWildcard(name))
fun = env -> new Reference();
else
fun = env -> node0;
} else if (node0 instanceof Dict) {
Generalize_[][] array = //
Read.from2(//
((Dict) node0).map).map(//
(key, value) -> new Generalize_[] { generalizer(key), generalizer(value) }).toArray(Generalize_[].class);
int length = array.length;
fun = env -> {
@SuppressWarnings("unchecked") Pair<Node, Reference>[] pairs = new Pair[length];
for (int i = 0; i < length; i++) pairs[i] = Pair.of(array[i][0].apply(env), Reference.of(array[i][1].apply(env)));
return Dict.of(pairs);
};
} else if ((tree = Tree.decompose(node0)) != null) {
Operator operator = tree.getOperator();
if (operator != TermOp.OR____) {
Generalize_ f = generalizer(tree.getLeft());
funs.add(env -> Tree.of(operator, f.apply(env), null));
node = tree.getRight();
continue;
} else {
// delay generalizing for performance
Generalize_ lf = generalizer(tree.getLeft());
Generalize_ rf = generalizer(tree.getRight());
fun = env -> Tree.of(operator, lf.apply(env), new Suspend(() -> rf.apply(env)));
}
} else if (node0 instanceof Tuple) {
Generalize_[] fs = Read.from(((Tuple) node0).nodes).map(this::generalizer).toArray(Generalize_.class);
int length = fs.length;
fun = env -> {
Node[] array = new Node[length];
for (int i = 0; i < length; i++) array[i] = fs[i].apply(env);
return Tuple.of(array);
};
} else
fun = env -> node0;
funs.add(fun);
break;
}
if (1 < funs.size())
return env -> {
Tree t = Tree.of(null, null, null);
Node node_ = t;
for (Generalize_ fun_ : funs) {
Tree t_ = Tree.decompose(node_);
Tree.forceSetRight(t_, fun_.apply(env));
node_ = t_.getRight();
}
return t.getRight();
};
else
return funs.get(0);
}
Aggregations