Skip to content

Commit df214bb

Browse files
authored
Merge pull request #1696 from AdaWorldAPI/lab/math-foundry-3-heritage-study-20261022
integrate(math-foundry): land #1694/#1695 catalogue and heritage stages on main
2 parents f741474 + 68043f2 commit df214bb

6 files changed

Lines changed: 604 additions & 0 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Stage E: mathematical heritage / 27-atom falsifier (lab only)
2+
3+
**Dependency chain:** merged [#1690](https://github.com/AdaWorldAPI/lance-graph/pull/1690) (E1 compiler) → draft Stage C proof capsule (#1693) → draft Stage D catalogue (#1694) → this draft.
4+
5+
## What this implements
6+
7+
The Rust `heritage` module distinguishes:
8+
1. **Source lead**: an author/work/edition candidate. NOT a reviewed facsimile, historical influence proof or mathematical certification.
9+
2. **Normalized mathematical statement**: a modern reconstruction with its assumptions, not necessarily the author's original proof.
10+
3. **Checked computational artifact**: currently only the synthetic E1 exact-integer substitution, compiled by egglog and replayed through R2IL with independent i128 oracle.
11+
12+
The Leibniz 1684 product-rule and Euler-identity entries are **uncertified source candidates**. Neither is executable, because the current E1 language has only `Num/Var/Def/Mul` and does not express differentiation, complex exponentials or the necessary side conditions. A test refuses both, by design.
13+
14+
## 27-atom hypothesis, not a discovery
15+
16+
Candidate vocabulary: six term constructors, eight algebra operators, seven relational operators, six derivation constructs. These are *research-level semantic categories*, not R2IL opcodes or claims that 27 operations suffice for all mathematical domains. No atom is considered "covered" unless an independently certified compiled procedure exercises it. Today the synthetic E1 program witnesses just `Const, Var, Multiply`.
17+
18+
Measure on a future licensed corpus:
19+
- Historical coverage: reviewed editions and unambiguous citations
20+
- Formalization coverage: statements expressed with domain/assumption witnesses
21+
- Proof coverage: mechanically checked lemmas, including independent proof-checker identity
22+
- Lowering coverage: lawful mappings to extant R2IL opcodes/folds
23+
- Minimum description length: vocabulary bytes + compiled corpus bytes; nonsharing control
24+
- Replay cost, proof bytes, binding costs, cache hit rates and provenance retention
25+
26+
**A shorter encoding is not evidence of semantic preservation.** The optimizer must refuse a proposed common template that changes SQL NULL/UNKNOWN, Set/Bag, numeric overflow, effects, source identity or historical attribution.
27+
28+
## Thought-Git learning × audit integration (future gate)
29+
30+
Do not duplicate Thought-Git's store or insert proof events into the nanosecond hot loop. Its existing D-TGB-4 receipts and landed commit witnesses are distinct from egglog proof DAGs. The future link is a reference-only citation from a versioned derivation or historical study row to:
31+
- certified law identity and assumptions
32+
- compiled procedure identity and checker version
33+
- source edition/provenance and any uncertainty
34+
- runtime binding witness (separately invalidatable)
35+
- promotion/demotion decision, counterexamples and cost samples
36+
37+
A replay recipe should be sufficient to recompute the outcome if the source snapshot/operands, codebook and checker are available. A hash alone is not a replay. An unsigned witness fold is not a cryptographic proof. Rubicon authorization and real LanceCycleWriter integration are out of scope.
38+
39+
## Next falsifiable stages (not shipped here)
40+
41+
1. Formalize the Leibniz product rule against an independent differentiation theorem checker. Preserve exact domain conditions, such as differentiability of both functions.
42+
2. Add an explicit mapping from the formal derivative term graph into the low-code hypothesis, measuring needed *new* atoms instead of forcing a 27-atom success.
43+
3. Compare Leibniz- and Newton-era equivalent formulas only after historical source reviews; equivalence does **not** establish intellectual influence.
44+
4. E2: 256 templates × 4,096 focus contexts = 1,048,576 applications. Compare cold certified catalogue vs existing relalg/CSE baseline, share/no-share controls and **total** execution cost. No demonstrated throughput claim yet.
45+
5. Add read-only Thought-Git proof/reference records and replay tests after EXO-98 owner coordination.
46+
47+
## Verification
48+
49+
`cd crates/egglog-r2il-probe && cargo test` (requires sibling OGAR and ndarray checkout).
50+
No runnable historical theorem is claimed. The three source/sample fixtures remain entirely outside production.
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
//! Stage D (lab): pre-certified mathematical patterns with fallback.
2+
//!
3+
//! Egglog is invoked ONLY when a candidate is cold-certified/promoted.
4+
//! The lookup path is a BTreeMap + exact semantic/target/binding guard + an
5+
//! already-lowered R2IL scalar program. Every miss/refusal calls the existing
6+
//! caller-supplied trusted baseline. This is NOT a production cache, data
7+
//! attestation, benchmark framework, or background task scheduler.
8+
9+
use std::collections::BTreeMap;
10+
11+
use crate::artifact::{ReplayCapsule, ReplayRefusal};
12+
use crate::{LowerRefusal, Problem};
13+
14+
/// Full source expression signature. Deliberately preserve premise order,
15+
/// coefficient, and variable names; no hashes or accidentally merged laws.
16+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
17+
pub struct SemanticKey {
18+
goal: String,
19+
target: String,
20+
premises: Vec<(String, i64, String)>,
21+
}
22+
23+
impl From<&Problem> for SemanticKey {
24+
fn from(p: &Problem) -> Self {
25+
Self {
26+
goal: p.goal.to_owned(),
27+
target: p.target_var.to_owned(),
28+
premises: p.premises.iter()
29+
.map(|e| (e.lhs.to_owned(), e.coeff, e.rhs.to_owned()))
30+
.collect(),
31+
}
32+
}
33+
}
34+
35+
/// Caller-supplied lab witness. A production owner must independently attest
36+
/// these values from the sealed source and coordinates before any cache use.
37+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
38+
pub struct BindingWitness {
39+
pub source_generation: u64,
40+
pub scope: u64,
41+
pub coordinate_generation: u64,
42+
}
43+
44+
/// Plan compatibility, independent of mathematical identity and bindings.
45+
#[derive(Debug, Clone, PartialEq, Eq)]
46+
pub struct TargetAbi {
47+
pub revision: u32,
48+
pub target: String,
49+
}
50+
51+
/// All times are supplied from external measurements; none are inferred from
52+
/// AST size or fabricated by the catalogue.
53+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
54+
pub struct CostEvidence {
55+
pub baseline_ns: u64,
56+
pub candidate_ns: u64,
57+
pub compile_and_certify_ns: u64,
58+
pub expected_reuse: u64,
59+
}
60+
61+
impl CostEvidence {
62+
pub fn pays_for_itself(&self) -> bool {
63+
self.candidate_ns < self.baseline_ns
64+
&& self.expected_reuse > 0
65+
&& u128::from(self.baseline_ns - self.candidate_ns)
66+
* u128::from(self.expected_reuse)
67+
> u128::from(self.compile_and_certify_ns)
68+
}
69+
}
70+
71+
#[derive(Debug, Clone, PartialEq, Eq)]
72+
pub enum PromotionRefusal {
73+
Uncertified(ReplayRefusal),
74+
NotAmortized,
75+
}
76+
77+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
78+
pub enum FallbackReason {
79+
CacheMiss,
80+
BindingChanged,
81+
TargetChanged,
82+
CompiledRefused,
83+
}
84+
85+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
86+
pub enum Route {
87+
Certified,
88+
Baseline(FallbackReason),
89+
}
90+
91+
#[derive(Debug, Clone, PartialEq, Eq)]
92+
pub struct Execution {
93+
pub route: Route,
94+
pub result: Result<i64, LowerRefusal>,
95+
}
96+
97+
struct Entry {
98+
capsule: ReplayCapsule,
99+
binding: BindingWitness,
100+
abi: TargetAbi,
101+
_cost: CostEvidence,
102+
}
103+
104+
/// In-memory lab catalogue; never loads an unverified disk blob.
105+
#[derive(Default)]
106+
pub struct Catalogue {
107+
entries: BTreeMap<SemanticKey, Entry>,
108+
}
109+
110+
impl Catalogue {
111+
pub fn new() -> Self {
112+
Self::default()
113+
}
114+
115+
pub fn len(&self) -> usize {
116+
self.entries.len()
117+
}
118+
119+
pub fn is_empty(&self) -> bool {
120+
self.entries.is_empty()
121+
}
122+
123+
/// Cold gate. A proof, replay, independent i128 oracle and supplied
124+
/// break-even evidence are required before inserting executable steps.
125+
/// This retains the existing Stage B exact-integer / E1-only restriction.
126+
pub fn promote(
127+
&mut self,
128+
capsule: ReplayCapsule,
129+
binding: BindingWitness,
130+
abi: TargetAbi,
131+
cost: CostEvidence,
132+
) -> Result<(), PromotionRefusal> {
133+
capsule.recheck().map_err(PromotionRefusal::Uncertified)?;
134+
for x in [0_i64, 1, -1, 7] {
135+
capsule.replay(x).map_err(PromotionRefusal::Uncertified)?;
136+
}
137+
if !cost.pays_for_itself() {
138+
return Err(PromotionRefusal::NotAmortized);
139+
}
140+
self.entries.insert(
141+
SemanticKey::from(capsule.problem()),
142+
Entry {
143+
capsule,
144+
binding,
145+
abi,
146+
_cost: cost,
147+
},
148+
);
149+
Ok(())
150+
}
151+
152+
/// Hot-compatible lookup and execution: neither egglog nor the i128
153+
/// oracle runs here. Source/coordinate/target validation is fail-closed,
154+
/// but caller-supplied witnesses are not source attestation.
155+
///
156+
/// Any miss or compiled execution refusal invokes baseline unconditionally.
157+
pub fn execute_or<F>(
158+
&self,
159+
problem: &Problem,
160+
binding: BindingWitness,
161+
abi: &TargetAbi,
162+
x: i64,
163+
baseline: F,
164+
) -> Execution
165+
where
166+
F: FnOnce(i64) -> Result<i64, LowerRefusal>,
167+
{
168+
let entry = match self.entries.get(&SemanticKey::from(problem)) {
169+
Some(e) => e,
170+
None => {
171+
return Execution {
172+
route: Route::Baseline(FallbackReason::CacheMiss),
173+
result: baseline(x),
174+
};
175+
}
176+
};
177+
let refusal = if entry.binding != binding {
178+
Some(FallbackReason::BindingChanged)
179+
} else if &entry.abi != abi {
180+
Some(FallbackReason::TargetChanged)
181+
} else {
182+
None
183+
};
184+
if let Some(reason) = refusal {
185+
return Execution {
186+
route: Route::Baseline(reason),
187+
result: baseline(x),
188+
};
189+
}
190+
match entry.capsule.execute(x) {
191+
Ok(value) => Execution {
192+
route: Route::Certified,
193+
result: Ok(value),
194+
},
195+
Err(_) => Execution {
196+
route: Route::Baseline(FallbackReason::CompiledRefused),
197+
result: baseline(x),
198+
},
199+
}
200+
}
201+
}

0 commit comments

Comments
 (0)