From 9ab592ce3765d07b6dbdb9c1d9b62dc8045e784e Mon Sep 17 00:00:00 2001 From: Richard Qin Date: Fri, 3 Apr 2026 02:46:12 +0800 Subject: feat: implement core term, symbol, and clause storage logic with canonicalization support --- src/core/clause_store.cc | 34 ++++++++++++++++++++++++++++++++++ src/core/symbol_table.cc | 43 +++++++++++++++++++++++++++++++++++++++++++ src/core/term_bank.cc | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+) (limited to 'src/core') diff --git a/src/core/clause_store.cc b/src/core/clause_store.cc index b62ffd0..a0adbe6 100644 --- a/src/core/clause_store.cc +++ b/src/core/clause_store.cc @@ -16,4 +16,38 @@ * along with this program. If not, see . */ +#include "atp/core/clause_store.h" +#include +#include +namespace atp { + +ClauseId ClauseStore::addClause(Clause clause) { + auto new_id = static_cast(clauses_.size()); + clause.id = new_id; + clauses_.push_back(std::move(clause)); + return new_id; +} + +const Clause& ClauseStore::getClause(ClauseId id) const { + assert(id < clauses_.size() && "ClauseId out of bounds!"); + return clauses_[id]; +} + +Clause& ClauseStore::getMutableClause(ClauseId id) { + assert(id < clauses_.size() && "ClauseId out of bounds!"); + return clauses_[id]; +} + +size_t ClauseStore::size() const { + return clauses_.size(); +} + +void ClauseStore::forEach(const std::function& visitor) const { + for (const auto& clause : clauses_) { + // TODO: Tombstone mechanism + visitor(clause); + } +} + +} // namespace atp diff --git a/src/core/symbol_table.cc b/src/core/symbol_table.cc index b62ffd0..261805a 100644 --- a/src/core/symbol_table.cc +++ b/src/core/symbol_table.cc @@ -16,4 +16,47 @@ * along with this program. If not, see . */ +#include "atp/core/symbol_table.h" +#include "atp/core/types.h" +#include +#include +#include + +namespace atp { + +SymbolId SymbolTable::intern(std::string_view name, SymbolKind kind, uint16_t arity, + SortId sort) { + auto it = name_to_id_.find(name); + if (it != name_to_id_.end()) { + SymbolId id = it->second; + SymbolInfo& info = infos_[id]; + if (info.kind != kind || info.arity != arity) { + throw std::runtime_error("Symbol kind or arity mismatch for: " + std::string(name)); + } + return id; + } + auto new_id = static_cast(infos_.size()); + infos_.push_back({.name = std::string(name), .kind = kind, .arity = arity, .sort = sort}); + std::string_view stable_name_view = infos_.back().name; + name_to_id_.emplace(stable_name_view, new_id); + return new_id; +} + +std::string_view SymbolTable::getName(SymbolId id) const { + return infos_[id].name; +} + +const SymbolInfo& SymbolTable::getInfo(SymbolId id) const { + return infos_[id]; +} + +bool SymbolTable::isVariable(SymbolId id) const { + return infos_[id].kind == SymbolKind::kVariable; +} + +size_t SymbolTable::size() const { + return infos_.size(); +} + +} // namespace atp diff --git a/src/core/term_bank.cc b/src/core/term_bank.cc index b62ffd0..53b4f0c 100644 --- a/src/core/term_bank.cc +++ b/src/core/term_bank.cc @@ -16,4 +16,41 @@ * along with this program. If not, see . */ +#include "atp/core/term_bank.h" +#include "atp/core/types.h" +#include +namespace atp { + +TermId TermBank::makeTerm(SymbolId symbol, std::span args) { + TermKey key{.symbol = symbol, .args = {args.begin(), args.end()}}; + auto it = term_to_id_.find(key); + if (it != term_to_id_.end()) { + return it->second; + } + auto new_id = static_cast(terms_.size()); + Term new_term{.symbol_id = symbol, .args = {args.begin(), args.end()}}; + terms_.push_back(std::move(new_term)); + term_to_id_.emplace(std::move(key), new_id); + return new_id; +} + +TermId TermBank::makeVar(SymbolId var_symbol) { + return makeTerm(var_symbol, {}); +} + +const Term& TermBank::getTerm(TermId id) const { + assert(id < terms_.size() && "TermId out of bounds!"); + return terms_[id]; +} + +bool TermBank::isVariable(TermId id) const { + assert(id < terms_.size() && "TermId out of bounds!"); + return symbols_.isVariable(terms_[id].symbol_id); +} + +size_t TermBank::size() const { + return terms_.size(); +} + +} // namespace atp -- cgit v1.2.3