Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
}
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Step: OneIsNonzero>(n: u8)
requires n == 1u8
ensures n != 0u8
{
Step(n);
}

machine entry() -> u8 {
use_step::<one_is_nonzero>(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:?}");
}
Loading