Search in sources :

Example 1 with SubUnify

use of nars.term.subst.SubUnify in project narchy by automenta.

the class SubIfUnify method apply.

@Override
public Term apply(/*@NotNull*/
Subterms a) {
    // parse parameters
    boolean strict = false;
    @Nullable Op op = null;
    // TODO compile at function construction time
    boolean force = false;
    int pp = a.subs();
    for (int p = 3; p < pp; p++) {
        Term ai = a.sub(p);
        if (ai.equals(Subst.STRICT))
            strict = true;
        else if (ai.equals(INDEP_VAR)) {
        // ;in this cases also dependent var elimination is fine!
        // 
        // (let [[mode check-var-type] (if (= var-symbol "$")
        // [:ind #(or (= % 'ind-var) (= % 'dep-var))]
        // [:dep #(= % 'dep-var)])
        // op = VAR_INDEP;
        } else if (ai.equals(DEP_VAR)) {
            op = VAR_DEP;
        } else if (ai.equals(Subst.FORCE))
            force = true;
        else
            throw new UnsupportedOperationException("unrecognized parameter: " + ai);
    }
    /**
     * term being transformed if x unifies with y
     */
    Term c = a.sub(0);
    // if (input instanceof Bool)return Null;
    // if (input == Null) return Null;
    Term x = a.sub(1);
    // if (x == Null) return Null;
    Term y = a.sub(2);
    if (x.equalsRoot(y)) {
        // unification would occurr but no changes would result
        return strict ? Null : c;
    }
    Term output;
    if (c.equals(x)) {
        // input equals X so it is entirely replaced by 'y'
        output = y;
    } else {
        boolean tryUnify = (op == null && x.hasAny(Op.VariableBits)) || (op != null && x.hasAny(op));
        if (!tryUnify) /* && mustSubstitute()*/
        {
            // no change
            output = null;
        } else {
            SubUnify su = new MySubUnify(op, strict);
            output = su.tryMatch(c, x, y);
            parent.use(parent.ttl - su.ttl);
        }
        if (output == null) {
            if (!force) {
                return Null;
            } else {
                // force: apply substitution even if un-unifiable
                output = c.replace(x, y);
                if (output == null)
                    return Null;
            }
        }
    }
    return (strict && c.equals(output)) ? Null : output;
}
Also used : Op(nars.Op) SubUnify(nars.term.subst.SubUnify) Term(nars.term.Term) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

Op (nars.Op)1 Term (nars.term.Term)1 SubUnify (nars.term.subst.SubUnify)1 Nullable (org.jetbrains.annotations.Nullable)1