Search in sources :

Example 1 with Termlike

use of nars.term.Termlike in project narchy by automenta.

the class PrologToNAL method N.

private static nars.term.Term N(alice.tuprolog.Term t) {
    if (t instanceof alice.tuprolog.Term) {
        Struct s = (Struct) t;
        String name = s.name();
        switch(name) {
            case ":-":
                assert (s.subs() == 2);
                // reverse, prolog is backwards
                nars.term.Term pre = N(s.sub(1));
                nars.term.Term post = N(s.sub(0));
                // convert to implication first, then promote variables on the resulting pre/post
                Term impl = $.impl(pre, post);
                pre = impl.sub(0);
                post = impl.sub(1);
                if (pre.varQuery() > 0 && post.varQuery() > 0) {
                    MutableSet<nars.term.var.Variable> prev = new UnifiedSet();
                    pre.recurseTerms(Termlike::hasVarQuery, (a) -> {
                        if (a.op() == Op.VAR_QUERY)
                            prev.add((Variable) a);
                        return true;
                    }, null);
                    MutableSet<nars.term.var.Variable> posv = new UnifiedSet();
                    post.recurseTerms(Termlike::hasVarQuery, (a) -> {
                        if (a.op() == Op.VAR_QUERY)
                            posv.add((Variable) a);
                        return true;
                    }, null);
                    MutableSet<nars.term.var.Variable> common = prev.intersect(posv);
                    int cs = common.size();
                    if (cs > 0) {
                        Map<nars.term.Term, nars.term.Term> x = new UnifiedMap(cs);
                        for (nars.term.var.Variable c : common) {
                            x.put(c, $.varIndep(c.toString().substring(1)));
                        }
                        impl = impl.replace(x);
                    }
                }
                return impl;
            case ",":
                return CONJ.the(N(s.sub(0)), N(s.sub(1)));
            default:
                nars.term.Term atom = $.the(name);
                int arity = s.subs();
                if (arity == 0) {
                    return atom;
                } else {
                    return $.inh($.p((nars.term.Term[]) Util.map(0, arity, i -> N(s.sub(i)), nars.term.Term[]::new)), atom);
                }
        }
    } else if (t instanceof Var) {
        return $.varQuery(((Var) t).name());
    // throw new RuntimeException(t + " untranslated");
    } else if (t instanceof NumberTerm.Int) {
        return $.the(((NumberTerm.Int) t).intValue());
    } else {
        throw new TODO(t + " (" + t.getClass() + ") untranslatable");
    }
}
Also used : Struct(alice.tuprolog.Struct) NumberTerm(alice.tuprolog.NumberTerm) Iterables(com.google.common.collect.Iterables) UnifiedMap(org.eclipse.collections.impl.map.mutable.UnifiedMap) CONJ(nars.Op.CONJ) Var(alice.tuprolog.Var) nars.$(nars.$) Theory(alice.tuprolog.Theory) Util(jcog.Util) Variable(nars.term.var.Variable) MutableSet(org.eclipse.collections.api.set.MutableSet) TODO(jcog.TODO) Op(nars.Op) Termlike(nars.term.Termlike) Map(java.util.Map) Term(nars.term.Term) UnifiedSet(org.eclipse.collections.impl.set.mutable.UnifiedSet) TODO(jcog.TODO) Variable(nars.term.var.Variable) NumberTerm(alice.tuprolog.NumberTerm) UnifiedMap(org.eclipse.collections.impl.map.mutable.UnifiedMap) Var(alice.tuprolog.Var) NumberTerm(alice.tuprolog.NumberTerm) Term(nars.term.Term) Struct(alice.tuprolog.Struct) Termlike(nars.term.Termlike) Variable(nars.term.var.Variable) UnifiedSet(org.eclipse.collections.impl.set.mutable.UnifiedSet) Term(nars.term.Term)

Aggregations

NumberTerm (alice.tuprolog.NumberTerm)1 Struct (alice.tuprolog.Struct)1 Theory (alice.tuprolog.Theory)1 Var (alice.tuprolog.Var)1 Iterables (com.google.common.collect.Iterables)1 Map (java.util.Map)1 TODO (jcog.TODO)1 Util (jcog.Util)1 nars.$ (nars.$)1 Op (nars.Op)1 CONJ (nars.Op.CONJ)1 Term (nars.term.Term)1 Termlike (nars.term.Termlike)1 Variable (nars.term.var.Variable)1 MutableSet (org.eclipse.collections.api.set.MutableSet)1 UnifiedMap (org.eclipse.collections.impl.map.mutable.UnifiedMap)1 UnifiedSet (org.eclipse.collections.impl.set.mutable.UnifiedSet)1