LMCAS
Lamina Computer Algebra System
载入中...
搜索中...
未找到
relation_store.hpp
浏览该文件的文档.
1/**
2 * @file relation_store.hpp
3 * @brief RelationStore class for storing relational constraints between symbolic expressions.
4 *
5 * Stores inequality relations (GT, LT, GEQ, LEQ, NEQ) between SymbolicExpr instances.
6 * When a simple "variable op 0" pattern is detected, the corresponding sign property
7 * is automatically propagated to the PropertyStore.
8 */
9#pragma once
10
11#include <vector>
12#include <string>
13#include <memory>
14#include "symbolic.hpp"
15#include "assumption.hpp"
16#include "result.hpp"
17#include <cstdint>
18#include <array>
19#include <cstddef>
20#include <optional>
21
22namespace LMCAS {
23
24// Forward declaration - PropertyStore may not yet be compiled
25class PropertyStore;
26
28
29/**
30 * @brief A stored relational constraint between two symbolic expressions.
31 */
32struct Relation {
33 SymbolicExpr lhs; ///< Left-hand side expression
34 SymbolicExpr rhs; ///< Right-hand side expression
35 RelationOp op; ///< Relational operator (GT, LT, GEQ, LEQ, NEQ, EQ)
36};
37
38/**
39 * @brief Stores relational constraints and derives sign properties for simple patterns.
40 *
41 * When a relation of the form `variable op 0` is added, the RelationStore notifies
42 * the PropertyStore to mark the variable with the corresponding sign:
43 * - GT -> Positive
44 * - GEQ -> NonNegative
45 * - LT -> Negative
46 * - LEQ -> NonPositive
47 * - NEQ -> NonZero
48 *
49 * 复合关系(多变量左端或任意右端)按原结构存储,供 InferenceEngine 后续推导.
50 */
51class LMCAS_API RelationStore {
52public:
53 RelationStore() = default;
54 RelationStore(const RelationStore&) = default;
55 RelationStore(RelationStore&& other) noexcept;
56 RelationStore& operator=(const RelationStore& other);
57 RelationStore& operator=(RelationStore&& other) noexcept;
58
59 /** @brief 成功提交(含赋值)均更新此存储的修订号。 */
60 std::uint64_t revision() const noexcept { return revision_; }
61
62 /**
63 * @brief Store a relation and optionally derive sign properties.
64 *
65 * If the relation compares one variable with exact zero, the corresponding
66 * sign property is declared on the PropertyStore.
67 *
68 * @param lhs Left-hand side expression
69 * @param rhs Right-hand side expression
70 * @param op Relational operator
71 * @param prop_store PropertyStore to notify for simple variable > 0 patterns
72 */
73 RelationStoreResult add_relation(
74 const SymbolicExpr& lhs, const SymbolicExpr& rhs,
75 RelationOp op, PropertyStore& prop_store);
76
77 /**
78 * @brief Checked relation insertion with explicit failure reporting.
79 *
80 * Applies the relation plus derived property declarations transactionally.
81 * On failure, neither this store nor the PropertyStore is modified.
82 */
83 RelationStoreResult add_relation_checked(const SymbolicExpr& lhs,
84 const SymbolicExpr& rhs,
85 RelationOp op,
86 PropertyStore& prop_store);
87
88 /**
89 * @brief Retrieve all stored relations.
90 * @return Const reference to the vector of stored relations
91 */
92 const std::vector<Relation>& get_relations() const;
93
94 /**
95 * @brief Check if a specific relation is stored.
96 *
97 * Compares LHS, RHS, and operator using structural equality of the AST nodes.
98 *
99 * @param lhs Left-hand side expression
100 * @param rhs Right-hand side expression
101 * @param op Relational operator
102 * @return true if the relation is found in the store
103 */
104 bool has_relation(const SymbolicExpr& lhs, const SymbolicExpr& rhs,
105 RelationOp op) const;
106
107 /**
108 * @brief Clear all stored relations (used during scope pop).
109 */
110 void clear();
111
112private:
113 friend class AssumptionContext;
114 using Premises = std::array<std::size_t, 2>;
115 struct RelationProofs {
116 bool declared = false;
117 std::vector<Premises> alternatives;
118 };
119 std::vector<Relation> relations_;
120 std::vector<RelationProofs> proofs_;
121 std::vector<std::size_t> declaration_order_;
122 std::uint64_t revision_ = 0;
123
124 std::optional<std::size_t> find_relation(
125 const SymbolicExpr& lhs, const SymbolicExpr& rhs, RelationOp op) const;
126
127 RelationStoreResult add_relation_unchecked(
128 const SymbolicExpr& lhs,
129 const SymbolicExpr& rhs,
130 RelationOp op,
131 PropertyStore& prop_store);
132
133 /// Maximum number of new relations deduced per add_relation call via transitive closure.
134 static constexpr int MAX_TRANSITIVE_DEDUCTIONS = 64;
135
136 struct TransitiveWork;
137 RelationStoreResult enqueue_transitive_deduction(
138 const Relation& deduced, Premises premises,
139 PropertyStore& prop_store, TransitiveWork& work);
140
141 /**
142 * @brief 新增关系后计算传递闭包。
143 *
144 * 从新关系开始 BFS,仅对 GT、GEQ 进行传递组合:
145 * GT+GT→GT、GT+GEQ→GT、GEQ+GT→GT、GEQ+GEQ→GEQ。
146 * 最多新增 MAX_TRANSITIVE_DEDUCTIONS 个结论,之后仍登记已有结论的替代证明。
147 * @param new_relation 触发本轮闭包计算的关系索引
148 * @param prop_store 用于推导关系符号的属性存储
149 * @return 本轮是否新增结论或替代证明。
150 */
151 Result<bool> compute_transitive_closure(
152 std::size_t new_relation, PropertyStore& prop_store);
153};
154
155} // namespace LMCAS
Core enumerations and forward declarations for the Assumption System.
#define LMCAS_API
RelationOp
Stable relational operator used by public APIs.
Result< void > RelationStoreResult
SymbolicExpr lhs
Left-hand side expression
SymbolicExpr rhs
Right-hand side expression
RelationOp op
Relational operator (GT, LT, GEQ, LEQ, NEQ, EQ)
A stored relational constraint between two symbolic expressions.
符号表达式主类 SymbolicExpr 及其工厂方法,运算接口.