use of suite.node.Atom in project suite by stupidsing.
the class TypeChecker method getType.
private Node getType(Node data) {
Node type;
Tree tree;
if (data instanceof Reference)
type = variableTypes.computeIfAbsent(IdentityKey.of(data), k -> new Reference()).finalNode();
else if ((tree = Tree.decompose(data)) != null)
if (tree.getOperator() == TermOp.AND___) {
type = Suite.substitute(".0;", getType(tree.getLeft()));
bind(type, getType(tree.getRight()));
} else if (tree.getOperator() == TermOp.TUPLE_) {
Node name = tree.getLeft();
if (name instanceof Atom) {
Node node = tree.getRight();
Node[] ps = TreeUtil.elements(node, TreeUtil.nElements(node));
type = getEnumType(name, Tree.of(TermOp.TUPLE_, Read.from(ps).map(this::getType).toList()));
} else
// free type
return new Reference();
} else {
Atom name = Atom.of(tree.getOperator().getName());
Node lt = getType(tree.getLeft());
Node rt = getType(tree.getRight());
type = getEnumType(name, Tree.of(TermOp.TUPLE_, lt, rt));
}
else if (data == Atom.NIL)
type = Suite.substitute("_;");
else if (data instanceof Atom)
type = getEnumType(data, Atom.NIL);
else
type = Atom.of(data.getClass().getSimpleName());
return type;
}
use of suite.node.Atom in project suite by stupidsing.
the class CompileGeneralizerImpl method generalizer.
@Override
public Generalize_ generalizer(Node node) {
VariableMapper<Reference> mapper = cc.mapper();
Generalizer generalizer = new Generalizer();
Generalize_ generalize = cc.cloner(generalizer.generalize(node))::apply;
Map<Reference, Atom> indices = new IdentityHashMap<>();
for (Atom variableName : generalizer.getVariableNames()) indices.put(generalizer.getVariable(variableName), variableName);
vm = mapper.mapKeys(indices::get);
return generalize;
}
use of suite.node.Atom in project suite by stupidsing.
the class Prover method prove0.
public boolean prove0(Node query) {
rem = OK;
alt = FAIL;
while (true) {
// logUtil.info(Formatter.dump(query));
query = query.finalNode();
if (query instanceof Tree) {
Tree tree = (Tree) query;
Node left = tree.getLeft(), right = tree.getRight();
switch((TermOp) tree.getOperator()) {
case OR____:
int pit = trail.getPointInTime();
Node bt = new Data<Source<Boolean>>(() -> {
trail.unwind(pit);
return Boolean.TRUE;
});
alt = andTree(bt, orTree(andTree(right, rem), alt));
query = left;
continue;
case AND___:
rem = andTree(right, rem);
query = left;
continue;
case EQUAL_:
query = isSuccess(bind(left, right));
break;
default:
}
} else if (query instanceof Data) {
query = isSuccess(Data.<Source<Boolean>>get(query).source());
continue;
}
Boolean b = systemPredicates.call(query);
if (b != null)
query = isSuccess(b);
// not handled above
if (query == OK)
if (rem != OK) {
query = rem;
rem = OK;
} else
return true;
else if (query == FAIL)
if (alt != FAIL) {
query = alt;
alt = FAIL;
rem = OK;
} else
return false;
else {
boolean isTrace = config.isTrace();
if (isTrace) {
Set<String> whites = Suite.tracePredicates;
Set<String> blacks = Suite.noTracePredicates;
Prototype prototype = Prototype.of(query);
Node head = prototype != null ? prototype.head : null;
Atom atom = head instanceof Atom ? (Atom) head : null;
String name = atom != null ? atom.name : null;
isTrace &= whites == null || whites.contains(name);
isTrace &= blacks == null || !blacks.contains(name);
}
if (!isTrace)
query = expand(query);
else
query = tracer.expandWithTrace(query, this, this::expand);
}
}
}
use of suite.node.Atom 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.Atom 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