Implementation of unification and omissions of variables

I am working on the implementation of the usual unification algorithm in the usual way: recursively descending through the expression trees, adding variable bindings to the hash table along this path, the verification is performed. In Java, as it happens, using the override functions to go with the kernel of the language, so the implementation part related to variables:

@Override
public boolean unify(Term a, Map<Var, Term> map) {
    if (this == a) {
        return true;
    }
    Term x = map.get(this);
    if (x != null) {
        return x.unify(a, map);
    }
    if (a instanceof Var) {
        x = map.get((Var) a);
        if (x != null) {
            return x.unify(this, map);
        }
    }
    if (a.occurs(this)) {
        return false;
    }
    map.put(this, a);
    return true;
}

This version is true for many cases quite quickly, but it has a problem that arises especially when using it for type inference. When combining a large number of variables into the same goal, it ends with a set of bindings, which basically looks like this:

a=b
b=c
c=d
d=e

, , , , , O (N) , O (N ^ 2).

, - , - a, , . , , .

, , , , , , .

, ?

+4
2

, - . :

    return x.unify(a, map);

:

    if (! x.unify(a, map)) {
        return false;
    }
    map.put(this, map.get(x));
    return true;

:

        return x.unify(this, map);

:

        if (! x.unify(this, map)) {
            return false;
        }
        map.put(a, map.get(x));
        return true;

( map.put , , , , , .)

, a b, b c ; , .

+2

: , =, . ,

unify(Term a, Map<VarClass, Term> map) {...

a VarClass - .

x=y, , x VarClass, y ( , ).

Term Var.

- .

+2

Source: https://habr.com/ru/post/1692447/


All Articles