From 8130f3f00b04275bf021bad9f647d92d64843eaf Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 27 Nov 2026 02:29:31 +0000 Subject: [PATCH] psi: keep a disjunction's type when an applied implication proof introduces it The kernel infers an unannotated pair as a plain product, so a disjunction introduced inside an implication proof that is applied on the spot lost its tag-indexed family. Ascribe the pair its denoted disjunction type. This lets a theorem proved from an equality, such as n == 1 => n != 0, be cited. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01HqP8t6ks5c3gBLeNZESrbf --- .../proofs/bounded/bounded_denotation.rs | 14 +++- .../bounded/tests/theorem_parameters.rs | 69 +++++++++++++++++++ .../tests/theorem_proof_parameters.rs | 40 +++++++++++ 3 files changed, 122 insertions(+), 1 deletion(-) diff --git a/source-rust/subsystems/compiler/psi/semantics/proofs/bounded/bounded_denotation.rs b/source-rust/subsystems/compiler/psi/semantics/proofs/bounded/bounded_denotation.rs index c2c8b236c747..888be4c50bb9 100644 --- a/source-rust/subsystems/compiler/psi/semantics/proofs/bounded/bounded_denotation.rs +++ b/source-rust/subsystems/compiler/psi/semantics/proofs/bounded/bounded_denotation.rs @@ -993,6 +993,12 @@ impl Denotation { fn pair(&mut self, first: TermHandle, second: TermHandle) -> TermHandle { self.arena.insert(Term::Pair { first, second }) } + /// `(λ (x : ty). x) term`: the kernel checks `term` against `ty` and infers `ty`. + fn ascribe(&mut self, ty: TermHandle, term: TermHandle) -> TermHandle { + let bound = self.var(0); + let identity = self.lam(ty, bound); + self.app(identity, term) + } fn fst(&mut self, pair: TermHandle) -> TermHandle { self.arena.insert(Term::Fst { pair }) } @@ -7045,7 +7051,13 @@ impl<'a> Elaboration<'a> { } self.rules .insert(AcceptedProofRule::DisjunctionIntroduction); - Ok(self.disjunction_intro(ds.len(), *index, ev)) + let pair = self.disjunction_intro(ds.len(), *index, ev); + // The kernel infers an unannotated pair's type as a plain product, which + // loses the tag-indexed family of a disjunction. Where the introduction is + // inferred, as inside an implication proof applied on the spot, that product + // differs from the denoted disjunction, so the pair is ascribed its type. + let ty = self.denotation.denote(&proof.conclusion)?; + Ok(self.denotation.ascribe(ty, pair)) } ProofRule::ImplicationElimination { implication, diff --git a/source-rust/subsystems/compiler/psi/semantics/proofs/bounded/tests/theorem_parameters.rs b/source-rust/subsystems/compiler/psi/semantics/proofs/bounded/tests/theorem_parameters.rs index 82c308939051..880fb30777b0 100644 --- a/source-rust/subsystems/compiler/psi/semantics/proofs/bounded/tests/theorem_parameters.rs +++ b/source-rust/subsystems/compiler/psi/semantics/proofs/bounded/tests/theorem_parameters.rs @@ -225,3 +225,72 @@ fn a_hypothesis_binds_between_the_subjects_and_the_premises() { ) .expect("the hypothesis's declarations arrive with it"); } + +/// `(λ (h : 0 < n). inr h) assumption`: the introduction sits inside an implication proof +/// that is applied on the spot, so the kernel infers the disjunction's pair rather than +/// checking it against the goal. +fn disjunction_under_an_applied_implication(index: usize) -> (Proposition, Proposition, ProofNode) { + let (_, _, subject, zero) = fixture(); + let premise = Proposition::LessThan(zero.clone(), subject.clone()); + let goal = + Proposition::Disjunction(vec![Proposition::LessThan(subject, zero), premise.clone()]); + let node = |conclusion, rule| ProofNode { conclusion, rule }; + let introduced = node( + goal.clone(), + ProofRule::DisjunctionIntroduction { + disjunct: Box::new(node(premise.clone(), ProofRule::Assumption { index: 1 })), + index, + }, + ); + let implication = node( + Proposition::Implication { + premise: Box::new(premise.clone()), + conclusion: Box::new(goal.clone()), + }, + ProofRule::ImplicationIntroduction { + body: Box::new(introduced), + }, + ); + let proof = node( + goal.clone(), + ProofRule::ImplicationElimination { + implication: Box::new(implication), + premise: Box::new(node(premise.clone(), ProofRule::Assumption { index: 0 })), + }, + ); + (premise, goal, proof) +} + +#[test] +fn a_disjunction_introduced_inside_an_applied_implication_keeps_its_type() { + let (integer, id, _, _) = fixture(); + let (premise, goal, proof) = disjunction_under_an_applied_implication(1); + verify_bounded_theorem( + &[(id, ScalarType::Integer(integer))], + &[], + &goal, + std::slice::from_ref(&premise), + &[], + &proof, + &mut Budget::default(), + ) + .expect("the pair is checked against the disjunction it introduces"); +} + +#[test] +fn a_disjunction_introduced_inside_an_applied_implication_still_names_its_disjunct() { + let (integer, id, _, _) = fixture(); + let (premise, goal, proof) = disjunction_under_an_applied_implication(0); + assert!( + verify_bounded_theorem( + &[(id, ScalarType::Integer(integer))], + &[], + &goal, + std::slice::from_ref(&premise), + &[], + &proof, + &mut Budget::default(), + ) + .is_err() + ); +} diff --git a/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/theorem_proof_parameters.rs b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/theorem_proof_parameters.rs index 50d1b528d77e..a3eacb199f99 100644 --- a/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/theorem_proof_parameters.rs +++ b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/theorem_proof_parameters.rs @@ -722,3 +722,43 @@ fn decoding_keeps_the_proof_an_application_gives_its_contract_and_its_check() { terminal_codec::decode_module(&terminal_codec::encode_module(&forged).unwrap()).is_err() ); } +const ONE_IS_NONZERO: &str = "data OneIsNonzero = machine(n: u8) + requires n == 1u8 + ensures n != 0u8; + +machine one_is_nonzero(n: u8) +requires n == 1u8 +ensures n != 0u8 +{ +} + +machine use_step(n: u8) +requires n == 1u8 +ensures n != 0u8 +{ + Step(n); +} + +machine entry() -> u8 { + use_step::(1u8); + 0u8 +} +"; + +#[test] +fn a_theorem_proved_from_an_equality_supplies_the_alias() { + let verdict = Program::of(ONE_IS_NONZERO).judged(|_| {}); + assert!(matches!(verdict, Verdict::Accepted), "{verdict:?}"); +} + +#[test] +fn a_theorem_proved_from_another_equality_cannot_supply_the_alias() { + let source = ONE_IS_NONZERO.replacen( + "machine one_is_nonzero(n: u8)\nrequires n == 1u8", + "machine one_is_nonzero(n: u8)\nrequires n == 2u8", + 1, + ); + assert_ne!(source, ONE_IS_NONZERO); + let verdict = Program::of(&source).judged(|_| {}); + assert!(!matches!(verdict, Verdict::Accepted), "{verdict:?}"); +}