aboutsummaryrefslogtreecommitdiff
path: root/src/infer/unification.cc
blob: be8a0253e6a916d43a982da3e07ef9a230e98439 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 * richard-atp-engine: A saturation-based automated theorem prover for first-order logic.
 * Copyright (C) 2026 Richard Qin
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */


#include "atp/infer/unification.h"
#include "atp/core/types.h"
#include "atp/infer/substitution.h"

namespace atp {

    bool unify(const TermBank& bank, TermId t1, TermId t2, Substitution& subst,
           const UnificationConfig& config) {
        t1 = subst.resolve(t1, bank);
        t2 = subst.resolve(t2, bank);

        if (t1 == t2) {
            return true;
        }

        if (bank.isVariable(t1)) {
            if (config.enable_occurs_check && subst.occursIn(t1, t2, bank)) {
                return false;
            }
            subst.bind(t1, t2);
            return true;
        }

        if (bank.isVariable(t2)) {
            if (config.enable_occurs_check && subst.occursIn(t2, t1, bank)) {
                return false;
            }
            subst.bind(t2, t1);
            return true;
        }

        const Term& term1 = bank.getTerm(t1);
        const Term& term2 = bank.getTerm(t2);

        if (term1.symbol_id != term2.symbol_id || term1.arity() != term2.arity()) {
            return false;
        }

        for (size_t i = 0; i < term1.arity(); ++i) {
            if (!unify(bank, term1.args[i], term2.args[i], subst, config)) {
                return false;
            }
        }

        return true;
    }

    TermId applySubstitution(const Substitution& subst, TermId term, TermBank& bank) {
        term = subst.resolve(term, bank);
        if (bank.isVariable(term)) {
            return term;
        }

        // IMPORTANT: Copy args and symbol_id BEFORE recursing.
        // Recursive calls may call bank.makeTerm() → realloc terms_ → invalidate refs.
        const Term& t = bank.getTerm(term);
        SymbolId sym = t.symbol_id;
        std::vector<TermId> old_args(t.args);  // copy!

        std::vector<TermId> new_args;
        new_args.reserve(old_args.size());
        for (TermId arg : old_args) {
            new_args.push_back(applySubstitution(subst, arg, bank));
        }
        return bank.makeTerm(sym, new_args);
    }

}  // namespace atp