Search in sources :

Example 1 with Atom

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;
}
Also used : Reference(suite.node.Reference) Suite(suite.Suite) Trail(suite.lp.Trail) Read(suite.streamlet.Read) Rule(suite.lp.kb.Rule) TermOp(suite.node.io.TermOp) HashMap(java.util.HashMap) Tree(suite.node.Tree) Node(suite.node.Node) Pair(suite.adt.pair.Pair) List(java.util.List) Atom(suite.node.Atom) Map(java.util.Map) Prototype(suite.lp.kb.Prototype) IdentityKey(suite.adt.IdentityKey) Binder(suite.lp.doer.Binder) TreeUtil(suite.node.util.TreeUtil) Generalizer(suite.lp.doer.Generalizer) Fail(suite.util.Fail) Dict(suite.node.Dict) Reference(suite.node.Reference) Node(suite.node.Node) Tree(suite.node.Tree) Atom(suite.node.Atom)

Example 2 with Atom

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;
}
Also used : Generalizer(suite.lp.doer.Generalizer) Reference(suite.node.Reference) IdentityHashMap(java.util.IdentityHashMap) Atom(suite.node.Atom)

Example 3 with Atom

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);
        }
    }
}
Also used : Set(java.util.Set) RuleSet(suite.lp.kb.RuleSet) Prototype(suite.lp.kb.Prototype) TermOp(suite.node.io.TermOp) Node(suite.node.Node) Tree(suite.node.Tree) Data(suite.node.Data) Source(suite.util.FunUtil.Source) Atom(suite.node.Atom)

Example 4 with Atom

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;
}
Also used : BuiltinPredicate(suite.lp.predicate.PredicateUtil.BuiltinPredicate) Operator(suite.node.io.Operator) Node(suite.node.Node) Tree(suite.node.Tree) Atom(suite.node.Atom)

Example 5 with Atom

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);
}
Also used : Reference(suite.node.Reference) Read(suite.streamlet.Read) GeneralizerFactory(suite.lp.doer.GeneralizerFactory) TermOp(suite.node.io.TermOp) Tree(suite.node.Tree) ArrayList(java.util.ArrayList) Node(suite.node.Node) Pair(suite.adt.pair.Pair) List(java.util.List) ProverConstant(suite.lp.doer.ProverConstant) VariableMapper(suite.lp.sewing.VariableMapper) Atom(suite.node.Atom) Suspend(suite.node.Suspend) Tuple(suite.node.Tuple) Operator(suite.node.io.Operator) Dict(suite.node.Dict) Operator(suite.node.io.Operator) Reference(suite.node.Reference) Node(suite.node.Node) ArrayList(java.util.ArrayList) Suspend(suite.node.Suspend) Atom(suite.node.Atom) Dict(suite.node.Dict) Tree(suite.node.Tree) Tuple(suite.node.Tuple)

Aggregations

Atom (suite.node.Atom)22 Node (suite.node.Node)17 Reference (suite.node.Reference)11 Tree (suite.node.Tree)11 TermOp (suite.node.io.TermOp)9 ArrayList (java.util.ArrayList)7 Pair (suite.adt.pair.Pair)7 Int (suite.node.Int)7 List (java.util.List)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 Suite (suite.Suite)5 Generalizer (suite.lp.doer.Generalizer)5 Prototype (suite.lp.kb.Prototype)5 RuleSet (suite.lp.kb.RuleSet)5 Data (suite.node.Data)5 TreeUtil (suite.node.util.TreeUtil)5 Read (suite.streamlet.Read)5 Fail (suite.util.Fail)5 Operator (suite.node.io.Operator)4