aboutsummaryrefslogtreecommitdiff
path: root/src/infer/resolution.cc
blob: 89be520568afc6737013c206b28eca4869fc34bf (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * 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/resolution.h"
#include "atp/core/clause.h"
#include "atp/core/symbol_table.h"
#include "atp/core/term.h"
#include "atp/core/term_bank.h"
#include "atp/core/types.h"
#include "atp/infer/substitution.h"
#include "atp/infer/unification.h"

#include <string>
#include <unordered_map>
#include <vector>

namespace atp {

namespace {

/// Recursively collect all variable TermIds appearing in a term.
void collectVariables(TermId term, const TermBank& bank,
                      std::unordered_map<TermId, bool>& seen) {
    if (bank.isVariable(term)) {
        seen[term] = true;
        return;
    }
    // Copy args to avoid dangling ref (bank is const here, but be safe)
    const Term& t = bank.getTerm(term);
    for (TermId arg : t.args) {
        collectVariables(arg, bank, seen);
    }
}

}  // namespace

Clause renameVariables(const Clause& c, TermBank& bank, SymbolTable& symbols,
                       uint32_t& next_var_id) {
    // 1. Collect all variables in the clause
    std::unordered_map<TermId, bool> var_set;
    for (const auto& lit : c.literals) {
        collectVariables(lit.atom, bank, var_set);
    }

    if (var_set.empty()) {
        return c;  // Ground clause — nothing to rename
    }

    // 2. Create fresh variables and build a substitution
    Substitution rename_subst;
    for (auto& [old_var_id, _] : var_set) {
        std::string fresh_name = "_R" + std::to_string(next_var_id++);
        SymbolId fresh_sym = symbols.intern(fresh_name, SymbolKind::kVariable);
        TermId fresh_var = bank.makeVar(fresh_sym);
        rename_subst.bind(old_var_id, fresh_var);
    }

    // 3. Apply the renaming substitution to all literals
    Clause renamed;
    renamed.id = c.id;
    renamed.rule = c.rule;
    renamed.parent1 = c.parent1;
    renamed.parent2 = c.parent2;
    renamed.depth = c.depth;
    for (const auto& lit : c.literals) {
        TermId new_atom = applySubstitution(rename_subst, lit.atom, bank);
        renamed.literals.push_back({.atom = new_atom, .is_positive = lit.is_positive});
    }
    return renamed;
}

std::optional<Clause> resolve(TermBank& bank, const Clause& c1, size_t lit_idx1,
                              const Clause& c2, size_t lit_idx2) {
    // Must have opposite polarity
    if (c1.literals[lit_idx1].is_positive == c2.literals[lit_idx2].is_positive) {
        return std::nullopt;
    }

    // Rename c2's variables to avoid clashes with c1
    // Use a static counter — safe for single-threaded prover
    static uint32_t var_counter = 0;
    auto& symbols = const_cast<SymbolTable&>(bank.symbols());
    Clause c2r = renameVariables(c2, bank, symbols, var_counter);

    // Unify the complementary atoms
    Substitution subst;
    if (!unify(bank, c1.literals[lit_idx1].atom, c2r.literals[lit_idx2].atom, subst)) {
        return std::nullopt;
    }

    // Build the resolvent: all literals except the resolved pair, with subst applied
    Clause resolvent;
    resolvent.rule = InferenceRule::kResolution;
    resolvent.parent1 = c1.id;
    resolvent.parent2 = c2.id;

    for (size_t i = 0; i < c1.literals.size(); ++i) {
        if (i != lit_idx1) {
            TermId new_atom = applySubstitution(subst, c1.literals[i].atom, bank);
            resolvent.literals.push_back(
                {.atom = new_atom, .is_positive = c1.literals[i].is_positive});
        }
    }
    for (size_t i = 0; i < c2r.literals.size(); ++i) {
        if (i != lit_idx2) {
            TermId new_atom = applySubstitution(subst, c2r.literals[i].atom, bank);
            resolvent.literals.push_back(
                {.atom = new_atom, .is_positive = c2r.literals[i].is_positive});
        }
    }
    return resolvent;
}

std::vector<Clause> allResolvents(TermBank& bank, const Clause& c1, const Clause& c2) {
    std::vector<Clause> results;
    for (size_t i = 0; i < c1.literals.size(); ++i) {
        for (size_t j = 0; j < c2.literals.size(); ++j) {
            if (c1.literals[i].is_positive != c2.literals[j].is_positive) {
                if (auto r = resolve(bank, c1, i, c2, j)) {
                    results.push_back(std::move(*r));
                }
            }
        }
    }
    return results;
}

std::vector<Clause> factor(TermBank& bank, const Clause& c) {
    std::vector<Clause> results;
    for (size_t i = 0; i < c.literals.size(); ++i) {
        for (size_t j = i + 1; j < c.literals.size(); ++j) {
            if (c.literals[i].is_positive != c.literals[j].is_positive) {
                continue;  // Must have same polarity
            }
            Substitution subst;
            if (!unify(bank, c.literals[i].atom, c.literals[j].atom, subst)) {
                continue;
            }
            // Build factored clause: drop literal j, apply subst to all
            Clause factored;
            factored.rule = InferenceRule::kFactoring;
            factored.parent1 = c.id;
            for (size_t k = 0; k < c.literals.size(); ++k) {
                if (k != j) {
                    TermId new_atom = applySubstitution(subst, c.literals[k].atom, bank);
                    factored.literals.push_back(
                        {.atom = new_atom, .is_positive = c.literals[k].is_positive});
                }
            }
            results.push_back(std::move(factored));
        }
    }
    return results;
}

}  // namespace atp