diff --git a/source-rust/subsystems/compiler/source-to-psi/pipeline/05_prepared-typed-trees-to-lowered-program/README.md b/source-rust/subsystems/compiler/source-to-psi/pipeline/05_prepared-typed-trees-to-lowered-program/README.md index 93eccad847e..8110b75e6f7 100644 --- a/source-rust/subsystems/compiler/source-to-psi/pipeline/05_prepared-typed-trees-to-lowered-program/README.md +++ b/source-rust/subsystems/compiler/source-to-psi/pipeline/05_prepared-typed-trees-to-lowered-program/README.md @@ -63,8 +63,14 @@ dispatches is lowered instead, with every machine declared inside it: a call of a machine parameter, or of a generic machine at some machines, is an `ExpressionKind::Applied` the signature reader applies to the parameter's variable or to the passed definitions (`read/templates.rs`, -`mathematical_application`). A body that is not generic calls or cites a -generic machine at concrete machines through the same form. A named proposition declared as +`mathematical_application`). So is a generic machine that states nothing, +which a law reads in place, and any other generic body the clause reader +cannot read as terms. A generic machine's machine parameters are arguments +of every definition the signature makes for it: its definition by cases, +its definition over fuel and each dispatch on a computed value, so a call +of itself may pass other machines. A body that is not generic calls or cites +a generic machine at concrete machines through the same form, and a call +owes the callee's premises at the machines and arguments it passes. A named proposition declared as one formula is read where a clause of such a machine applies it: the formula, with each parameter standing for the argument written for it. Its declaration adds nothing to the signature. A quotient of mathematical data diff --git a/source-rust/subsystems/compiler/source-to-psi/pipeline/05_prepared-typed-trees-to-lowered-program/src/read/signature.rs b/source-rust/subsystems/compiler/source-to-psi/pipeline/05_prepared-typed-trees-to-lowered-program/src/read/signature.rs index bc3272fb13a..d3f9f3867f2 100644 --- a/source-rust/subsystems/compiler/source-to-psi/pipeline/05_prepared-typed-trees-to-lowered-program/src/read/signature.rs +++ b/source-rust/subsystems/compiler/source-to-psi/pipeline/05_prepared-typed-trees-to-lowered-program/src/read/signature.rs @@ -193,6 +193,7 @@ impl Program<'_> { fueled: Vec::new(), result_as: None, callable_scope: Vec::new(), + generic_premises: Vec::new(), held: Vec::new(), stating_premises: false, integers: None, @@ -611,6 +612,9 @@ struct Converter<'p, 't> { /// The machine parameters of the generic machine being read, each as /// the variable it is under that machine's formals. callable_scope: Vec<(SymbolHandle, TermHandle)>, + /// The premises of each generic machine read so far, as terms under + /// its machine parameters and formals: what a call of it owes. + generic_premises: Vec<(MachineId, Vec)>, /// The premises of the machine being read that are read so far, as /// terms under its formals: what a clause of it may rest a call on. held: Vec, @@ -3161,6 +3165,9 @@ impl<'p> Converter<'p, '_> { premises.push(premise); self.held.push(premise); } + if template.is_some() { + self.generic_premises.push((id, premises.clone())); + } if let Some(template) = template.filter(|template| !template.lowered) { // A machine generic over machines: its value, read as a term // with its contract, under its machine parameters and formals. @@ -3257,18 +3264,6 @@ impl<'p> Converter<'p, '_> { premises: &premises, }; let structural = by_cases.map(|(subject, arms)| self.by_cases(&defining, subject, arms)); - // A generic machine whose body dispatches is read here, with its - // machine parameters bound outside its formals (`enter`), and only - // as a definition by cases on one formal. - if template.is_some() && !matches!(structural, Some(Ok(_))) { - return match structural { - Some(Err(refusal)) => Err(refusal), - _ => Err(unserved( - id, - "generic mathematical machine that dispatches other than once on a formal", - )), - }; - } let (mut term, cases) = match (structural, ranked) { (Some(Ok((term, cases))), _) => (term, Some(cases)), (Some(Err(refusal)), None) => return Err(refusal), @@ -3276,7 +3271,11 @@ impl<'p> Converter<'p, '_> { // value first: the recursion is on fuel taken from its measure. (_, Some(measure)) => (self.by_fuel(&defining, measure, result_entry)?, None), (None, None) => { - let types: Vec<_> = formals.iter().map(|(formal, _)| *formal).collect(); + // A generic machine's machine parameters are variables of + // the body too, just outside its formals: a dispatch inside + // it is a definition over all of them. + let mut types: Vec<_> = callables.clone(); + types.extend(formals.iter().map(|(formal, _)| *formal)); let valuing = Valuing { owner: id, context: &types, @@ -3504,14 +3503,6 @@ impl<'p> Converter<'p, '_> { result_entry: u32, ) -> Checked { let id = defining.id; - // The definition over fuel is a declaration of its own over the - // formals, which would leave a machine parameter unbound. - if self.template(id).is_some() { - return Err(unserved( - id, - "generic mathematical machine that recurses other than at a field of the case", - )); - } let formals = defining.formals; let count = formals.len(); let body = self @@ -3528,7 +3519,13 @@ impl<'p> Converter<'p, '_> { }; let shape = shape.clone(); let default = self.inhabitant(id, result_entry)?; - let types: Vec<_> = formals.iter().map(|(formal, _)| *formal).collect(); + // The definition over fuel is a declaration of its own: a generic + // machine's machine parameters are its arguments too, after the + // fuel and before the formals. + let callables = defining.callables; + let parameters = callables.len() as u32; + let mut types: Vec<_> = callables.to_vec(); + types.extend(formals.iter().map(|(formal, _)| *formal)); let mut landing = defining.result; for &formal in types.iter().rev() { landing = self.signature.arena.insert(Term::Pi { @@ -3540,7 +3537,7 @@ impl<'p> Converter<'p, '_> { domain: fuel, body: landing, }); - let all = count as u32; + let all = parameters + count as u32; let mut branches = Vec::with_capacity(shape.cases()); for case in 0..shape.cases() { // A unit less of fuel is the first field that holds the data. @@ -3578,7 +3575,23 @@ impl<'p> Converter<'p, '_> { cited: &[], }; let slots = self.formal_slots(count, body.slots.len()); - let mut branch = self.body_value(id, slots, None, &valuing)?; + // Within the branch a machine parameter is the branch's own + // variable, just outside the formals, as it is outside them in + // the machine: the premises read the same here. + let enclosing = std::mem::take(&mut self.callable_scope); + self.callable_scope = enclosing + .iter() + .enumerate() + .map(|(position, &(symbol, _))| { + ( + symbol, + self.variable(count as u32 + parameters - 1 - position as u32), + ) + }) + .collect(); + let read = self.body_value(id, slots, None, &valuing); + self.callable_scope = enclosing; + let mut branch = read?; for &formal in types.iter().rev() { branch = self.signature.arena.insert(Term::Lambda { domain: formal, @@ -3589,8 +3602,8 @@ impl<'p> Converter<'p, '_> { } let tree = self.variable(all); let induction = shape.induct(&mut self.signature.arena, data, motive, &branches, tree); - let own: Vec<_> = (0..count) - .map(|formal| self.variable((count - 1 - formal) as u32)) + let own: Vec<_> = (0..all) + .map(|position| self.variable(all - 1 - position)) .collect(); let mut term = self.signature.arena.apply_arguments(induction, &own); let mut ty = defining.result; @@ -3627,10 +3640,12 @@ impl<'p> Converter<'p, '_> { }, format!("{} over fuel", defining.name), ); - self.fueled.push((id, over_fuel, data, measure as u32)); + // A law counts the machine parameters among its formals, first. + self.fueled + .push((id, over_fuel, data, parameters + measure as u32)); // The machine is that definition with its ranked formal as the fuel. let head = self.constant(over_fuel); - let mut operands = vec![own[measure]]; + let mut operands = vec![own[parameters as usize + measure]]; operands.extend(own); Ok(self.signature.arena.apply_arguments(head, &operands)) } @@ -3949,14 +3964,6 @@ impl<'p> Converter<'p, '_> { scope: &Scope<'_>, valuing: &Valuing<'_>, ) -> Checked { - // The dispatch is a definition of its own over the scope's - // variables, which would leave a machine parameter unbound. - if self.template(valuing.owner).is_some() { - return Err(unserved( - machine, - "generic mathematical machine that dispatches other than once on a formal", - )); - } let ExpressionKind::Slot(slot) = subject.kind else { return Err(unserved( machine, @@ -4173,6 +4180,56 @@ impl<'p> Converter<'p, '_> { Ok(()) } + /// What a call of `callee` at `actuals` owes: each of `premises`, terms + /// under its parameters, instantiated at them and proved from what the + /// body holds there. + fn owed_at( + &mut self, + id: MachineId, + callee: MachineId, + premises: &[TermHandle], + actuals: &[TermHandle], + scope: &Scope<'_>, + ) -> Checked<()> { + if premises.is_empty() { + return Ok(()); + } + let Some(valuing) = scope.value else { + return Err(unserved( + id, + "term outside a definition calls a machine that has a premise", + )); + }; + let program = self.program; + let callee_name = program + .symbols + .get(callee) + .map(|symbol| self.name(*symbol)) + .unwrap_or_default(); + for &premise in premises { + let goal = instantiate(&mut self.signature.arena, premise, actuals); + if valuing + .premises + .iter() + .any(|&held| self.signature.arena.structurally_equal(held, goal)) + { + continue; + } + let source = program + .names + .get(valuing.owner) + .and_then(|names| names.declared); + self.owe_statement( + id, + valuing, + goal, + format!("the premise of `{callee_name}` at its call"), + source, + )?; + } + Ok(()) + } + /// What a recursive call at `operands` of a machine defined on fuel /// owes: its ranked argument strictly below the caller's in natural /// order, instantiated at the call's actual arguments and proved from @@ -4457,45 +4514,86 @@ impl<'p> Converter<'p, '_> { .is_some_and(|valuing| valuing.fuel.is_some_and(|fuel| fuel.machine == head)) || scope.recursion.is_some_and(|(machine, ..)| machine == head); if own { - // The machine calling itself: its machine parameters are - // fixed for the whole definition, so it may only pass - // them on as they are. - if !self.forwards_own(id, machines) { - return Err(unserved( - id, - "generic mathematical machine that calls itself at other machines", - )); + // The machine calling itself, at its own machine + // parameters or others: the recursion's function takes + // the machines first (`by_cases`, `by_fuel`). + let mut operands = Vec::with_capacity(arguments.len()); + for argument in arguments { + operands.push(self.expression(id, argument, scope)?); } - let call = Expression { - kind: ExpressionKind::Call { - callee: CallTarget::Machine(head), - required_contract: None, - static_reach_binding: None, - static_reach_arguments: Vec::new(), - proof_arguments: Vec::new(), - arguments: arguments.clone(), - designated: None, - }, - ..expression.clone() + let passing = self.passed(id, machines, outer)?; + let mut actuals = passing.clone(); + actuals.extend(operands.iter().map(|operand| operand.term)); + let premises = self + .generic_premises + .iter() + .find(|(machine, _)| *machine == head) + .map(|(_, premises)| premises.clone()) + .ok_or_else(|| { + unserved(id, "generic mathematical machine lost its premises") + })?; + self.owed_at(id, head, &premises, &actuals, scope)?; + if let Some(fuel) = scope.value.and_then(|valuing| valuing.fuel) + && fuel.machine == head + { + if let Some(descent) = fuel.descent { + self.descends(id, descent, &operands, expression, scope)?; + } + return Ok(plain(match fuel.step { + Some(step) => self.signature.arena.apply_arguments(step, &actuals), + None => fuel.default, + })); + } + let Some((machine, subject, results)) = scope.recursion else { + return Err(unserved(id, "recursive call outside its recursion")); }; - return self.expression(id, &call, scope); - } - if self - .program - .signatures - .get(head) - .is_none_or(|called| !called.requires.is_empty()) - { - return Err(unserved( - id, - "call of a generic mathematical machine that has a premise", - )); + let field = operands + .get(subject) + .and_then(|operand| operand.field) + .ok_or_else(|| { + unserved( + id, + "recursive call whose ranked argument is not a field of the dispatched case", + ) + })?; + self.ranked_on(machine, subject, true)?; + let mut rest = passing; + rest.extend( + operands + .iter() + .enumerate() + .filter(|(position, _)| *position != subject) + .map(|(_, operand)| operand.term), + ); + let result = + results + .get(field as usize) + .copied() + .flatten() + .ok_or_else(|| { + unserved( + id, + "recursive call at a field that does not hold the data", + ) + })?; + return Ok(plain(self.signature.arena.apply_arguments(result, &rest))); } let mut actuals = self.passed(id, machines, outer)?; for argument in arguments { actuals.push(self.expression(id, argument, scope)?.term); } let function = self.function(head)?; + // Its premises, stated over its machine parameters and + // formals, are owed at the machines and arguments passed. + let premises = self + .generic_premises + .iter() + .find(|(machine, _)| *machine == head) + .map(|(_, premises)| premises.clone()) + .ok_or_else(|| { + unserved(id, "generic mathematical machine lost its premises") + })?; + self.owed_at(id, head, &premises, &actuals, scope)?; let function = self.constant(function); Ok(plain( self.signature.arena.apply_arguments(function, &actuals), @@ -4537,21 +4635,12 @@ impl<'p> Converter<'p, '_> { ) })?; self.ranked_on(machine, subject, true)?; - // A generic machine passes itself its own machine - // parameters first. - let outer = callable_shift(scope); - let mut rest = if self.template(machine).is_some() { - self.callable_actuals(outer) - } else { - Vec::new() - }; - rest.extend( - operands - .iter() - .enumerate() - .filter(|(position, _)| *position != subject) - .map(|(_, operand)| operand.term), - ); + let rest: Vec<_> = operands + .iter() + .enumerate() + .filter(|(position, _)| *position != subject) + .map(|(_, operand)| operand.term) + .collect(); let result = results .get(field as usize) @@ -4788,9 +4877,11 @@ impl<'p> Converter<'p, '_> { .find(|(fueled, ..)| *fueled == id) .expect("`script` found the machine defined on fuel"); // The machine's result with `fuel` units: the definition over - // fuel at a variable bound outside the formals. + // fuel at a variable bound outside the formals and any machine + // parameters, which it takes next. let head = self.constant(definition); - let mut operands = vec![self.variable(count as u32)]; + let mut operands = vec![self.variable((count + callables.len()) as u32)]; + operands.extend(self.callable_actuals(0)); operands.extend(open.iter().copied()); let result = self.signature.arena.apply_arguments(head, &operands); self.result_as = Some((id, result)); @@ -4970,43 +5061,31 @@ impl<'p> Converter<'p, '_> { Ok(types) } - /// The variable of the machine parameter `symbol` of the machine being - /// read, in a scope that binds `outer` variables inside its formals. + /// The machine parameter `symbol` of the machine being read, in a + /// scope that binds `outer` variables inside its formals: its variable, + /// or the machine a law passed it where the law reads it in place. fn callable_term( &mut self, id: MachineId, symbol: SymbolHandle, outer: u32, ) -> Checked { - let variable = self + let term = self .callable_scope .iter() .find(|(callable, _)| *callable == symbol) - .map(|(_, variable)| *variable) + .map(|(_, term)| *term) .ok_or_else(|| unserved(id, "machine parameter outside the machine that binds it"))?; - let Term::Variable(index) = self.signature.arena.get(variable) else { - return Err(unserved( - id, - "machine parameter bound to something other than a variable", - )); - }; - Ok(self.variable(index + outer)) + Ok(shift(&mut self.signature.arena, term, 0, outer)) } /// Every machine parameter of the machine being read, outermost first, /// in a scope that binds `outer` variables inside its formals. fn callable_actuals(&mut self, outer: u32) -> Vec { - let indices: Vec = self - .callable_scope - .iter() - .filter_map(|(_, variable)| match self.signature.arena.get(*variable) { - Term::Variable(index) => Some(index), - _ => None, - }) - .collect(); - indices + let terms: Vec = self.callable_scope.iter().map(|(_, term)| *term).collect(); + terms .into_iter() - .map(|index| self.variable(index + outer)) + .map(|term| shift(&mut self.signature.arena, term, 0, outer)) .collect() } @@ -5035,18 +5114,6 @@ impl<'p> Converter<'p, '_> { Ok(terms) } - /// Whether `machines` passes the machine being read its own machine - /// parameters, in declared order: a call of itself. - fn forwards_own(&self, id: MachineId, machines: &[crate::body::PassedMachine]) -> bool { - let own = self - .template(id) - .map_or(&[][..], |template| template.callables.as_slice()); - own.len() == machines.len() - && own.iter().zip(machines).all(|(callable, passed)| { - *passed == crate::body::PassedMachine::Parameter(callable.symbol) - }) - } - /// What a call of the machine `member` at `operands` in a clause owes: /// each premise of that machine at those arguments must be one of the /// premises the machine being read already has. A clause has no body to @@ -5158,7 +5225,15 @@ impl<'p> Converter<'p, '_> { _ => { let function = self.function(id)?; let function = self.constant(function); - self.signature.arena.apply_arguments(function, formals) + // A generic machine takes its machine parameters + // first. + let mut arguments = if self.template(id).is_some() { + self.callable_actuals(0) + } else { + Vec::new() + }; + arguments.extend(formals.iter().copied()); + self.signature.arena.apply_arguments(function, &arguments) } } } @@ -5891,19 +5966,22 @@ impl<'p> Converter<'p, '_> { for argument in arguments { operands.push(self.expression(machine, argument, scope)?); } - if *callee == law && !self.forwards_own(law, machines) { - return Err(unserved( - machine, - "generic mathematical law that cites itself at other machines", - )); - } + // A generic law cites itself at machines, its own or others, ahead + // of the arguments: the induction is over all of them. + let passing = if self.template(law).is_some() && *callee == law { + self.passed(machine, machines, scope.depth)? + } else { + Vec::new() + }; if *callee == law && self.fueled.iter().any(|(fueled, ..)| *fueled == law) { // The law of a machine defined on fuel is proved by induction on // the fuel, so it may be cited at any arguments: the citation is // the law with one unit less. + let mut arguments = passing; + arguments.extend(operands.iter().map(|operand| operand.term)); script.citations.push(Citation { evidence: Evidence::Fuel, - arguments: operands.iter().map(|operand| operand.term).collect(), + arguments, }); return Ok(()); } @@ -5928,7 +6006,7 @@ impl<'p> Converter<'p, '_> { self.ranked_on(law, subject, true)?; // A generic law's hypothesis is stated over its machine // parameters too, bound outside the formals: they come first. - let mut arguments = self.callable_actuals(scope.depth); + let mut arguments = passing; arguments.extend( operands .iter() @@ -5954,12 +6032,47 @@ impl<'p> Converter<'p, '_> { } return Ok(()); } - if !machines.is_empty() { - return Err(unserved( - machine, - "law body cites a generic mathematical machine that states nothing", - )); + // A generic machine that states nothing is read in place with its + // machine parameters standing for the machines this call passes. A + // machine declared inside the law passes none: it shares the law's. + let generic = match self.template(*callee) { + Some(template) if !machines.is_empty() => { + let symbols: Vec = template + .callables + .iter() + .map(|callable| callable.symbol) + .collect(); + if symbols.len() != machines.len() { + return Err(unserved( + machine, + "citation of a generic mathematical machine without one machine per parameter", + )); + } + let passed = self.passed(machine, machines, 0)?; + Some(symbols.into_iter().zip(passed).collect::>()) + } + _ => None, + }; + let enclosing = generic.map(|scope| std::mem::replace(&mut self.callable_scope, scope)); + let read = self.cite_in_place(law, machine, *callee, &operands, scope, script); + if let Some(enclosing) = enclosing { + self.callable_scope = enclosing; } + read + } + + /// Read the body of `callee`, a machine that states nothing, in place + /// at `operands` inside a case of `law`. + fn cite_in_place( + &mut self, + law: MachineId, + machine: MachineId, + callee: MachineId, + operands: &[Bound], + scope: &Scope<'_>, + script: &mut LawCase, + ) -> Checked<()> { + let callee = &callee; // A machine that states nothing is read in place, with its formals // standing for the arguments of this call. *self.machines.get_mut(*callee).expect("row read by `laws`") = Read::Reading; diff --git a/source-rust/subsystems/compiler/source-to-psi/pipeline/05_prepared-typed-trees-to-lowered-program/src/read/templates.rs b/source-rust/subsystems/compiler/source-to-psi/pipeline/05_prepared-typed-trees-to-lowered-program/src/read/templates.rs index ff8003c9c51..7eab07e871c 100644 --- a/source-rust/subsystems/compiler/source-to-psi/pipeline/05_prepared-typed-trees-to-lowered-program/src/read/templates.rs +++ b/source-rust/subsystems/compiler/source-to-psi/pipeline/05_prepared-typed-trees-to-lowered-program/src/read/templates.rs @@ -17,7 +17,8 @@ machine may so cite another. A body that only binds values, cites laws and returns a value is read here, through the clause reader, with each `let` standing for the expression it is bound to, and is not lowered. A body that dispatches is lowered, with every -machine declared inside it, and read by the signature reader like any other: +machine declared inside it, as is a machine that states nothing and any body +the clause reader cannot read, and read by the signature reader like any other: there a call of a machine parameter, or of a generic machine at some machines, is an `ExpressionKind::Applied` (`mathematical_application`), which applies no operation and only the signature reads. A body that is not @@ -514,7 +515,21 @@ impl<'t> Program<'t> { // A body that dispatches is lowered and read like any other, and so // is each machine declared inside it: those are its cases' helpers, // read in place where it calls them. - if self.dispatches(id, machine)? + // A machine that states nothing and returns nothing is a helper a + // law reads in place where it calls it, so it is lowered too. + let states_nothing = result.is_none() + && !super::span( + |span| typed.signature_contracts.span(span), + declared.contracts, + id, + "contract span", + )? + .iter() + .any(|clause| { + clause.kind == crate::source_state::signature::SignatureContractKind::Ensures + }); + if states_nothing + || self.dispatches(id, machine)? || std::iter::successors(self.enclosing_machine(machine), |&outer| { self.enclosing_machine(outer) }) @@ -593,11 +608,14 @@ impl<'t> Program<'t> { Ok(()) })(); self.applied_propositions.pop(); - read?; - if result.is_some() != body.value.is_some() { - return Err(unserved( - "generic mathematical machine body without its value", - )); + // A body the clause reader cannot read as terms is lowered and read + // like any other body instead, which refuses what it cannot read. + if read.is_err() || result.is_some() != body.value.is_some() { + return Ok(TemplateBody { + callables: body.callables, + lowered: true, + ..TemplateBody::default() + }); } Ok(body) } diff --git a/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/src/tests/generic_machine.rs b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/src/tests/generic_machine.rs index b3a815a9780..139176f1884 100644 --- a/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/src/tests/generic_machine.rs +++ b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/src/tests/generic_machine.rs @@ -334,38 +334,168 @@ fn an_ordinary_call_unfolds_the_concrete_machine() { assert_eq!(law.machine, "plus_one"); } +macro_rules! fixture { + ($path:literal) => { + include_str!(concat!( + "../../tests/fixtures/generic_machine/", + $path, + "/main.omg" + )) + }; +} + +/// A positive fixture's law is judged and its negative twin's stays +/// unproved. +fn twins(pass: &str, judged_machine: &str, fail: &str, unproved_machine: &str) { + let verdicts = judged(&proofs(pass)); + judged_law(&verdicts, judged_machine); + let verdicts = judged(&proofs(fail)); + assert_eq!(unproved(&verdicts).machine, unproved_machine); +} + #[test] -fn a_generic_machine_that_calls_itself_at_another_machine_is_refused() { - // Its machine parameters are fixed for the whole definition, so the - // kernel's recursion has no way to change them. - let source = proofs(ITERATE).replace( - "(Step(iterate::(pred, a)))", - "(Step(iterate::(pred, a)))", +fn a_generic_machine_calls_itself_at_other_machines() { + // The induction's hypothesis is the law at the swapped machines, so it + // owes both premises. + let verdicts = judged(&proofs(fixture!("pass/alternate_keeps_two_fixed_points"))); + judged_law(&verdicts, "alternate_twice"); + twins( + fixture!("pass/alternate_keeps_two_fixed_points"), + "alternate_keeps", + fixture!("fail/alternate_keeps_one_fixed_point"), + "alternate_keeps", ); - let source = source.replace( - "machine iterate", - "machine succ(a: Peano) -> Peano -{ - transition { _ -> Peano::Succ { pred: a } } } -machine iterate", +/// Euclid's `gcd` by remainders, as `core/nat.omg` wrote it before it +/// became structural, with `Step` applied to its result, and a law about +/// it at `succ` claiming `claimed`; with them, the repeated subtraction +/// `mod` that `computed_descent/pass` keeps, generic over a `Step` it +/// does not use, whose done arm cites the bound only when `cite_bound`. +/// All judged with the whole of `core/nat.omg`. +fn gcd_then(claimed: &str, cite_bound: bool) -> Vec { + let nat = include_str!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../../../../../../source/library/core/nat.omg" + )); + let first = nat.find("public data Nat {").expect("the data declaration"); + let nat = format!( + "use {{omega}}/language/core/algebra.omg;\nuse {{omega}}/language/core/foundation.omg as proof;\n{} +machine gcd_then(a: Nat, b: Nat) -> Nat +requires machine Step(a: Nat) -> Nat terminates; +terminates by b +{{ + transition b {{ + Nat::Zero -> (Step(a)), + Nat::Succ {{ prev as pb }} -> gcd_rec(a, b, pb) + }} + + machine gcd_rec(a: Nat, b: Nat, pb: Nat) -> Nat {{ + _ = le_one_succ(pb); + let next: Nat = mod(a, Nat::Succ {{ prev: pb }}); + transition {{ _ -> (gcd_then::(Nat::Succ {{ prev: pb }}, next)) }} + }} +}} + +machine mod_then(a: Nat, b: Nat) -> Nat +requires machine Step(a: Nat) -> Nat terminates; +terminates by a +requires + |proof|::Identity::(saturating_sub(Nat::Succ {{ prev: Nat::Zero }}, b), Nat::Zero) +ensures + |proof|::Identity::(saturating_sub(Nat::Succ {{ prev: result }}, b), Nat::Zero) +{{ + transition (saturating_sub(b, a)) {{ + Nat::Zero -> mod_rec(a, b), + Nat::Succ {{ prev as w }} -> mod_done(a, b, w) + }} + + machine mod_rec(a: Nat, b: Nat) -> Nat {{ + _ = sub_lt(a, b); + let next: Nat = saturating_sub(a, b); + transition {{ _ -> (mod_then::(next, b)) }} + }} + + machine mod_done(a: Nat, b: Nat, w: Nat) -> Nat {{ + {cited} + transition {{ _ -> (a) }} + }} +}} + +machine nat_succ(a: Nat) -> Nat +{{ + transition {{ _ -> Nat::Succ {{ prev: a }} }} +}} + +machine gcd_then_succ() +ensures + |proof|::Identity::( + gcd_then::(Nat::Succ {{ prev: Nat::Succ {{ prev: Nat::Zero }} }}, Nat::Succ {{ prev: Nat::Zero }}), + {claimed} + ) +{{ +}} +", + &nat[first..], + cited = if cite_bound { + "_ = lt_of_sub_pos(a, b, w);" + } else { + "" + }, ); - assert!(source.contains("iterate::(pred, a)")); - let verdicts = judged(&source); - assert!( - verdicts.iter().any(|verdict| matches!( - verdict, - RouteVerdict::FallBack(refusal) if refusal.detail.starts_with( - "machine generic mathematical machine that calls itself at other machines" - ) - )), - "{verdicts:?}" + super::fuel_recursion::judged_library_files(&nat, &[]) +} + +#[test] +fn a_generic_machine_recurses_on_fuel() { + // `gcd_then` calls itself at the computed `mod(a, b)`, so it is defined + // on fuel, with `Step` among the definition's arguments, and owes each + // call's descent. `gcd(2, 1)` is one, so the result at `succ` is two; + // its twin claims one. + let two = "Nat::Succ { prev: Nat::Succ { prev: Nat::Zero } }"; + let verdicts = gcd_then(two, true); + judged_law(&verdicts, "gcd_then_succ"); + // A generic machine on fuel with a law of its own: the law is an + // induction on the fuel, over the machine parameter too. + judged_law(&verdicts, "mod_then"); + for law in &signature(&verdicts).laws { + assert!( + matches!(law.decision, MathematicalCoreDecision::Judged(_)), + "{law:?}" + ); + } + let verdicts = gcd_then("Nat::Succ { prev: Nat::Zero }", true); + assert_eq!(unproved(&verdicts).machine, "gcd_then_succ"); + // Without the bound in its done arm, the law on fuel stays unproved. + let verdicts = gcd_then(two, false); + assert_eq!(unproved(&verdicts).machine, "mod_then"); +} + +#[test] +fn a_generic_machine_dispatches_on_a_computed_value() { + twins( + fixture!("pass/pick_by_a_computed_value"), + "pick_by_succ", + fixture!("fail/pick_by_a_computed_value_wrong_case"), + "pick_by_succ", ); - assert!( - !verdicts - .iter() - .any(|verdict| matches!(verdict, RouteVerdict::Laws(_))), - "{verdicts:?}" +} + +#[test] +fn a_call_of_a_generic_machine_owes_its_premise() { + let verdicts = judged(&proofs(fixture!("pass/call_owes_its_premise"))); + signature(&verdicts); + let verdicts = judged(&proofs(fixture!("fail/call_without_its_premise"))); + let law = unproved(&verdicts); + assert_eq!(law.machine, "fixed_by_succ"); +} + +#[test] +fn a_law_cites_through_a_generic_helper() { + twins( + fixture!("pass/cited_through_a_generic_helper"), + "twice_id_keeps", + fixture!("fail/cited_through_a_generic_helper_at_succ"), + "twice_succ_keeps", ); } diff --git a/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/fail/alternate_keeps_one_fixed_point/build.omg b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/fail/alternate_keeps_one_fixed_point/build.omg new file mode 100644 index 00000000000..84e7d5f6b58 --- /dev/null +++ b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/fail/alternate_keeps_one_fixed_point/build.omg @@ -0,0 +1,13 @@ +// Deployable proof canary: every hosted target selects its exact entry. + +use main.omg as app; +use {omega}/build/dependency.omg; + +machine build(builder: @mutable Build) { + Build::application(builder, "alternate_keeps_one_fixed_point"); + let system_registration: DependencyRegistration = Build::dependency(builder, |dependency|::from_source(Source::Path { location: "../../../../../../../../../../../source/library/system" })); + Build::bind(builder, windows_x86_64::ProgramEntry, |app|::entry_start); + Build::bind(builder, linux_x86_64::ProgramEntry, |app|::entry_start); + Build::bind(builder, linux_arm64::ProgramEntry, |app|::entry_start); + Build::bind(builder, macos_arm64::ProgramEntry, |app|::entry_start); +} diff --git a/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/fail/alternate_keeps_one_fixed_point/main.omg b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/fail/alternate_keeps_one_fixed_point/main.omg new file mode 100644 index 00000000000..d44db76f1cd --- /dev/null +++ b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/fail/alternate_keeps_one_fixed_point/main.omg @@ -0,0 +1,52 @@ +// The twin of `pass/alternate_keeps_two_fixed_points` with only `Step` +// known to fix `a`. The hypothesis at the swapped machines owes `Other(a) = +// a`, which nothing gives, so the law is refused. +use {omega}/language/core/process_exit.omg; +use {omega}/language/core/foundation.omg as proof; + +data Peano { + case Zero; + case Succ(pred: Peano); +} + +machine alternate(n: Peano, a: Peano) -> Peano +requires machine Step(a: Peano) -> Peano terminates; +requires machine Other(a: Peano) -> Peano terminates; +terminates by n +{ + transition n { + Peano::Zero -> (a), + Peano::Succ { pred } -> (Step(alternate::(pred, a))) + } +} + +machine alternate_keeps(n: Peano, a: Peano) +requires machine Step(a: Peano) -> Peano terminates; +requires machine Other(a: Peano) -> Peano terminates; +terminates by n +requires + |proof|::Identity::(Step(a), a) +ensures + |proof|::Identity::(alternate::(n, a), a) +{ + transition n { + Peano::Zero -> base(), + Peano::Succ { pred } -> step(pred, a) + } + machine base() { + } + machine step(pred: Peano, a: Peano) { + alternate_keeps::(pred, a); + } +} + +data Main { } +machine exit_with_status(main: @mutable Main) reaches |process_exit|::ProcessExit { + |process_exit|::ProcessExit::exit_process(0); +} + +// The entry owns ordinary local state; selection supplies no context. +machine entry_start() reaches |process_exit|::ProcessExit { + let mutable entry_storage: Main; + block exit_with_status(@mutable entry_storage); +} diff --git a/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/fail/call_without_its_premise/build.omg b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/fail/call_without_its_premise/build.omg new file mode 100644 index 00000000000..97448106dc2 --- /dev/null +++ b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/fail/call_without_its_premise/build.omg @@ -0,0 +1,13 @@ +// Deployable proof canary: every hosted target selects its exact entry. + +use main.omg as app; +use {omega}/build/dependency.omg; + +machine build(builder: @mutable Build) { + Build::application(builder, "call_without_its_premise"); + let system_registration: DependencyRegistration = Build::dependency(builder, |dependency|::from_source(Source::Path { location: "../../../../../../../../../../../source/library/system" })); + Build::bind(builder, windows_x86_64::ProgramEntry, |app|::entry_start); + Build::bind(builder, linux_x86_64::ProgramEntry, |app|::entry_start); + Build::bind(builder, linux_arm64::ProgramEntry, |app|::entry_start); + Build::bind(builder, macos_arm64::ProgramEntry, |app|::entry_start); +} diff --git a/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/fail/call_without_its_premise/main.omg b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/fail/call_without_its_premise/main.omg new file mode 100644 index 00000000000..58667531949 --- /dev/null +++ b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/fail/call_without_its_premise/main.omg @@ -0,0 +1,38 @@ +// The twin of `pass/call_owes_its_premise` at `succ`. The call owes +// `succ(a) = a`, which does not hold, so the obligation is refused. +use {omega}/language/core/process_exit.omg; +use {omega}/language/core/foundation.omg as proof; + +data Peano { + case Zero; + case Succ(pred: Peano); +} + +machine apply_fixed(a: Peano) -> Peano +requires machine Step(a: Peano) -> Peano terminates; +requires + |proof|::Identity::(Step(a), a) +{ + transition { _ -> (Step(a)) } +} + +machine succ(a: Peano) -> Peano +{ + transition { _ -> Peano::Succ { pred: a } } +} + +machine fixed_by_succ(a: Peano) -> Peano +{ + transition { _ -> (apply_fixed::(a)) } +} + +data Main { } +machine exit_with_status(main: @mutable Main) reaches |process_exit|::ProcessExit { + |process_exit|::ProcessExit::exit_process(0); +} + +// The entry owns ordinary local state; selection supplies no context. +machine entry_start() reaches |process_exit|::ProcessExit { + let mutable entry_storage: Main; + block exit_with_status(@mutable entry_storage); +} diff --git a/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/fail/cited_through_a_generic_helper_at_succ/build.omg b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/fail/cited_through_a_generic_helper_at_succ/build.omg new file mode 100644 index 00000000000..764a6dd4e39 --- /dev/null +++ b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/fail/cited_through_a_generic_helper_at_succ/build.omg @@ -0,0 +1,13 @@ +// Deployable proof canary: every hosted target selects its exact entry. + +use main.omg as app; +use {omega}/build/dependency.omg; + +machine build(builder: @mutable Build) { + Build::application(builder, "cited_through_a_generic_helper_at_succ"); + let system_registration: DependencyRegistration = Build::dependency(builder, |dependency|::from_source(Source::Path { location: "../../../../../../../../../../../source/library/system" })); + Build::bind(builder, windows_x86_64::ProgramEntry, |app|::entry_start); + Build::bind(builder, linux_x86_64::ProgramEntry, |app|::entry_start); + Build::bind(builder, linux_arm64::ProgramEntry, |app|::entry_start); + Build::bind(builder, macos_arm64::ProgramEntry, |app|::entry_start); +} diff --git a/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/fail/cited_through_a_generic_helper_at_succ/main.omg b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/fail/cited_through_a_generic_helper_at_succ/main.omg new file mode 100644 index 00000000000..8255eef8879 --- /dev/null +++ b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/fail/cited_through_a_generic_helper_at_succ/main.omg @@ -0,0 +1,55 @@ +// The twin of `pass/cited_through_a_generic_helper` at `succ`. The helper +// cites the generic law at `succ`, whose premise `succ(a) = a` does not +// hold, so the law is refused. +use {omega}/language/core/process_exit.omg; +use {omega}/language/core/foundation.omg as proof; + +data Peano { + case Zero; + case Succ(pred: Peano); +} + +machine twice(a: Peano) -> Peano +requires machine Step(a: Peano) -> Peano terminates; +{ + let once: Peano = Step(a); + transition { _ -> (Step(once)) } +} + +machine twice_keeps_a_fixed_point(a: Peano) +requires machine Step(a: Peano) -> Peano terminates; +requires + |proof|::Identity::(Step(a), a) +ensures + |proof|::Identity::(twice::(a), a) +{ +} + +machine argue_twice(a: Peano) +requires machine Step(a: Peano) -> Peano terminates; +{ + twice_keeps_a_fixed_point::(a); +} + +machine succ(a: Peano) -> Peano +{ + transition { _ -> Peano::Succ { pred: a } } +} + +machine twice_succ_keeps(a: Peano) +ensures + |proof|::Identity::(twice::(a), a) +{ + argue_twice::(a); +} + +data Main { } +machine exit_with_status(main: @mutable Main) reaches |process_exit|::ProcessExit { + |process_exit|::ProcessExit::exit_process(0); +} + +// The entry owns ordinary local state; selection supplies no context. +machine entry_start() reaches |process_exit|::ProcessExit { + let mutable entry_storage: Main; + block exit_with_status(@mutable entry_storage); +} diff --git a/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/fail/pick_by_a_computed_value_wrong_case/build.omg b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/fail/pick_by_a_computed_value_wrong_case/build.omg new file mode 100644 index 00000000000..b524d0b4240 --- /dev/null +++ b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/fail/pick_by_a_computed_value_wrong_case/build.omg @@ -0,0 +1,13 @@ +// Deployable proof canary: every hosted target selects its exact entry. + +use main.omg as app; +use {omega}/build/dependency.omg; + +machine build(builder: @mutable Build) { + Build::application(builder, "pick_by_a_computed_value_wrong_case"); + let system_registration: DependencyRegistration = Build::dependency(builder, |dependency|::from_source(Source::Path { location: "../../../../../../../../../../../source/library/system" })); + Build::bind(builder, windows_x86_64::ProgramEntry, |app|::entry_start); + Build::bind(builder, linux_x86_64::ProgramEntry, |app|::entry_start); + Build::bind(builder, linux_arm64::ProgramEntry, |app|::entry_start); + Build::bind(builder, macos_arm64::ProgramEntry, |app|::entry_start); +} diff --git a/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/fail/pick_by_a_computed_value_wrong_case/main.omg b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/fail/pick_by_a_computed_value_wrong_case/main.omg new file mode 100644 index 00000000000..1432f63dbe4 --- /dev/null +++ b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/fail/pick_by_a_computed_value_wrong_case/main.omg @@ -0,0 +1,40 @@ +// The twin of `pass/pick_by_a_computed_value` that claims the zero case's +// value. `succ(Zero)` is a successor, so the law is refused. +use {omega}/language/core/process_exit.omg; +use {omega}/language/core/foundation.omg as proof; + +data Peano { + case Zero; + case Succ(pred: Peano); +} + +machine pick_by(n: Peano, a: Peano) -> Peano +requires machine Step(a: Peano) -> Peano terminates; +{ + transition (Step(n)) { + Peano::Zero -> (a), + Peano::Succ { pred } -> (pred) + } +} + +machine succ(a: Peano) -> Peano +{ + transition { _ -> Peano::Succ { pred: a } } +} + +machine pick_by_succ(a: Peano) +ensures + |proof|::Identity::(pick_by::(Peano::Zero, a), a) +{ +} + +data Main { } +machine exit_with_status(main: @mutable Main) reaches |process_exit|::ProcessExit { + |process_exit|::ProcessExit::exit_process(0); +} + +// The entry owns ordinary local state; selection supplies no context. +machine entry_start() reaches |process_exit|::ProcessExit { + let mutable entry_storage: Main; + block exit_with_status(@mutable entry_storage); +} diff --git a/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/pass/alternate_keeps_two_fixed_points/build.omg b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/pass/alternate_keeps_two_fixed_points/build.omg new file mode 100644 index 00000000000..4a53742d6a8 --- /dev/null +++ b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/pass/alternate_keeps_two_fixed_points/build.omg @@ -0,0 +1,13 @@ +// Deployable proof canary: every hosted target selects its exact entry. + +use main.omg as app; +use {omega}/build/dependency.omg; + +machine build(builder: @mutable Build) { + Build::application(builder, "alternate_keeps_two_fixed_points"); + let system_registration: DependencyRegistration = Build::dependency(builder, |dependency|::from_source(Source::Path { location: "../../../../../../../../../../../source/library/system" })); + Build::bind(builder, windows_x86_64::ProgramEntry, |app|::entry_start); + Build::bind(builder, linux_x86_64::ProgramEntry, |app|::entry_start); + Build::bind(builder, linux_arm64::ProgramEntry, |app|::entry_start); + Build::bind(builder, macos_arm64::ProgramEntry, |app|::entry_start); +} diff --git a/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/pass/alternate_keeps_two_fixed_points/main.omg b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/pass/alternate_keeps_two_fixed_points/main.omg new file mode 100644 index 00000000000..638edd441b8 --- /dev/null +++ b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/pass/alternate_keeps_two_fixed_points/main.omg @@ -0,0 +1,70 @@ +// A generic mathematical machine may call itself at other machines. +// `alternate::` applies `Step` and `Other` in turn, by calling +// itself with the two swapped; its law is an induction whose hypothesis is +// the law at the swapped machines, so both must fix `a`. +use {omega}/language/core/process_exit.omg; +use {omega}/language/core/foundation.omg as proof; + +data Peano { + case Zero; + case Succ(pred: Peano); +} + +machine alternate(n: Peano, a: Peano) -> Peano +requires machine Step(a: Peano) -> Peano terminates; +requires machine Other(a: Peano) -> Peano terminates; +terminates by n +{ + transition n { + Peano::Zero -> (a), + Peano::Succ { pred } -> (Step(alternate::(pred, a))) + } +} + +machine alternate_keeps(n: Peano, a: Peano) +requires machine Step(a: Peano) -> Peano terminates; +requires machine Other(a: Peano) -> Peano terminates; +terminates by n +requires + |proof|::Identity::(Step(a), a), + |proof|::Identity::(Other(a), a) +ensures + |proof|::Identity::(alternate::(n, a), a) +{ + transition n { + Peano::Zero -> base(), + Peano::Succ { pred } -> step(pred, a) + } + machine base() { + } + machine step(pred: Peano, a: Peano) { + alternate_keeps::(pred, a); + } +} + +machine succ(a: Peano) -> Peano +{ + transition { _ -> Peano::Succ { pred: a } } +} + +machine id(a: Peano) -> Peano +{ + transition { _ -> (a) } +} + +machine alternate_twice(a: Peano) +ensures + |proof|::Identity::(alternate::(Peano::Succ { pred: Peano::Succ { pred: Peano::Zero } }, a), Peano::Succ { pred: a }) +{ +} + +data Main { } +machine exit_with_status(main: @mutable Main) reaches |process_exit|::ProcessExit { + |process_exit|::ProcessExit::exit_process(0); +} + +// The entry owns ordinary local state; selection supplies no context. +machine entry_start() reaches |process_exit|::ProcessExit { + let mutable entry_storage: Main; + block exit_with_status(@mutable entry_storage); +} diff --git a/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/pass/call_owes_its_premise/build.omg b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/pass/call_owes_its_premise/build.omg new file mode 100644 index 00000000000..d64b0e70913 --- /dev/null +++ b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/pass/call_owes_its_premise/build.omg @@ -0,0 +1,13 @@ +// Deployable proof canary: every hosted target selects its exact entry. + +use main.omg as app; +use {omega}/build/dependency.omg; + +machine build(builder: @mutable Build) { + Build::application(builder, "call_owes_its_premise"); + let system_registration: DependencyRegistration = Build::dependency(builder, |dependency|::from_source(Source::Path { location: "../../../../../../../../../../../source/library/system" })); + Build::bind(builder, windows_x86_64::ProgramEntry, |app|::entry_start); + Build::bind(builder, linux_x86_64::ProgramEntry, |app|::entry_start); + Build::bind(builder, linux_arm64::ProgramEntry, |app|::entry_start); + Build::bind(builder, macos_arm64::ProgramEntry, |app|::entry_start); +} diff --git a/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/pass/call_owes_its_premise/main.omg b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/pass/call_owes_its_premise/main.omg new file mode 100644 index 00000000000..4344ee7f004 --- /dev/null +++ b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/pass/call_owes_its_premise/main.omg @@ -0,0 +1,39 @@ +// A call of a generic mathematical machine that has a premise owes the +// premise at the machines and arguments it passes. `apply_fixed::(a)` +// owes `id(a) = a`, which holds by unfolding `id`. +use {omega}/language/core/process_exit.omg; +use {omega}/language/core/foundation.omg as proof; + +data Peano { + case Zero; + case Succ(pred: Peano); +} + +machine apply_fixed(a: Peano) -> Peano +requires machine Step(a: Peano) -> Peano terminates; +requires + |proof|::Identity::(Step(a), a) +{ + transition { _ -> (Step(a)) } +} + +machine id(a: Peano) -> Peano +{ + transition { _ -> (a) } +} + +machine fixed_by_id(a: Peano) -> Peano +{ + transition { _ -> (apply_fixed::(a)) } +} + +data Main { } +machine exit_with_status(main: @mutable Main) reaches |process_exit|::ProcessExit { + |process_exit|::ProcessExit::exit_process(0); +} + +// The entry owns ordinary local state; selection supplies no context. +machine entry_start() reaches |process_exit|::ProcessExit { + let mutable entry_storage: Main; + block exit_with_status(@mutable entry_storage); +} diff --git a/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/pass/cited_through_a_generic_helper/build.omg b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/pass/cited_through_a_generic_helper/build.omg new file mode 100644 index 00000000000..3d4041bce45 --- /dev/null +++ b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/pass/cited_through_a_generic_helper/build.omg @@ -0,0 +1,13 @@ +// Deployable proof canary: every hosted target selects its exact entry. + +use main.omg as app; +use {omega}/build/dependency.omg; + +machine build(builder: @mutable Build) { + Build::application(builder, "cited_through_a_generic_helper"); + let system_registration: DependencyRegistration = Build::dependency(builder, |dependency|::from_source(Source::Path { location: "../../../../../../../../../../../source/library/system" })); + Build::bind(builder, windows_x86_64::ProgramEntry, |app|::entry_start); + Build::bind(builder, linux_x86_64::ProgramEntry, |app|::entry_start); + Build::bind(builder, linux_arm64::ProgramEntry, |app|::entry_start); + Build::bind(builder, macos_arm64::ProgramEntry, |app|::entry_start); +} diff --git a/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/pass/cited_through_a_generic_helper/main.omg b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/pass/cited_through_a_generic_helper/main.omg new file mode 100644 index 00000000000..98a4e5b5678 --- /dev/null +++ b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/pass/cited_through_a_generic_helper/main.omg @@ -0,0 +1,55 @@ +// A law may cite a generic machine that states nothing: it is read in place +// with its machine parameter standing for the machine the citation passes. +// `argue_twice::` cites the generic law at `id`, whose premise holds. +use {omega}/language/core/process_exit.omg; +use {omega}/language/core/foundation.omg as proof; + +data Peano { + case Zero; + case Succ(pred: Peano); +} + +machine twice(a: Peano) -> Peano +requires machine Step(a: Peano) -> Peano terminates; +{ + let once: Peano = Step(a); + transition { _ -> (Step(once)) } +} + +machine twice_keeps_a_fixed_point(a: Peano) +requires machine Step(a: Peano) -> Peano terminates; +requires + |proof|::Identity::(Step(a), a) +ensures + |proof|::Identity::(twice::(a), a) +{ +} + +machine argue_twice(a: Peano) +requires machine Step(a: Peano) -> Peano terminates; +{ + twice_keeps_a_fixed_point::(a); +} + +machine id(a: Peano) -> Peano +{ + transition { _ -> (a) } +} + +machine twice_id_keeps(a: Peano) +ensures + |proof|::Identity::(twice::(a), a) +{ + argue_twice::(a); +} + +data Main { } +machine exit_with_status(main: @mutable Main) reaches |process_exit|::ProcessExit { + |process_exit|::ProcessExit::exit_process(0); +} + +// The entry owns ordinary local state; selection supplies no context. +machine entry_start() reaches |process_exit|::ProcessExit { + let mutable entry_storage: Main; + block exit_with_status(@mutable entry_storage); +} diff --git a/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/pass/pick_by_a_computed_value/build.omg b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/pass/pick_by_a_computed_value/build.omg new file mode 100644 index 00000000000..e5384a0d5e4 --- /dev/null +++ b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/pass/pick_by_a_computed_value/build.omg @@ -0,0 +1,13 @@ +// Deployable proof canary: every hosted target selects its exact entry. + +use main.omg as app; +use {omega}/build/dependency.omg; + +machine build(builder: @mutable Build) { + Build::application(builder, "pick_by_a_computed_value"); + let system_registration: DependencyRegistration = Build::dependency(builder, |dependency|::from_source(Source::Path { location: "../../../../../../../../../../../source/library/system" })); + Build::bind(builder, windows_x86_64::ProgramEntry, |app|::entry_start); + Build::bind(builder, linux_x86_64::ProgramEntry, |app|::entry_start); + Build::bind(builder, linux_arm64::ProgramEntry, |app|::entry_start); + Build::bind(builder, macos_arm64::ProgramEntry, |app|::entry_start); +} diff --git a/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/pass/pick_by_a_computed_value/main.omg b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/pass/pick_by_a_computed_value/main.omg new file mode 100644 index 00000000000..c9615569e9c --- /dev/null +++ b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/generic_machine/pass/pick_by_a_computed_value/main.omg @@ -0,0 +1,42 @@ +// A generic mathematical machine may dispatch on a computed value, here +// `Step(n)`: the dispatch is a definition of its own over the machine +// parameter and the formals. At `succ`, `Step(Zero)` is a successor, so +// `pick_by::(Zero, a)` is that successor's field, `Zero`. +use {omega}/language/core/process_exit.omg; +use {omega}/language/core/foundation.omg as proof; + +data Peano { + case Zero; + case Succ(pred: Peano); +} + +machine pick_by(n: Peano, a: Peano) -> Peano +requires machine Step(a: Peano) -> Peano terminates; +{ + transition (Step(n)) { + Peano::Zero -> (a), + Peano::Succ { pred } -> (pred) + } +} + +machine succ(a: Peano) -> Peano +{ + transition { _ -> Peano::Succ { pred: a } } +} + +machine pick_by_succ(a: Peano) +ensures + |proof|::Identity::(pick_by::(Peano::Zero, a), Peano::Zero) +{ +} + +data Main { } +machine exit_with_status(main: @mutable Main) reaches |process_exit|::ProcessExit { + |process_exit|::ProcessExit::exit_process(0); +} + +// The entry owns ordinary local state; selection supplies no context. +machine entry_start() reaches |process_exit|::ProcessExit { + let mutable entry_storage: Main; + block exit_with_status(@mutable entry_storage); +} diff --git a/wiki/drafts/designs/proof_kernel_core_audit.md b/wiki/drafts/designs/proof_kernel_core_audit.md index 97a356336f3..695a33690aa 100644 --- a/wiki/drafts/designs/proof_kernel_core_audit.md +++ b/wiki/drafts/designs/proof_kernel_core_audit.md @@ -1374,13 +1374,31 @@ starts with records. `Cases` formal and a split's names count the machine parameters first. A body that is not generic may call or cite a generic machine at concrete machines through the same form: the call no longer names the - typed trees' closed copy. Still refused, by name: a generic machine that - calls itself at other machines, that recurses other than at a field of - the case (on fuel) or dispatches on a computed value, a call of a - generic machine that has a premise, and citing a generic machine that - states nothing. Customers: `generic_machine/pass/iterate_keeps_a_fixed_point`, + typed trees' closed copy. Customers: `generic_machine/pass/iterate_keeps_a_fixed_point`, `pass/cited_by_an_ordinary_law` and `pass/called_by_an_ordinary_definition`, each with its twin. +- **Every generic mathematical body (2026-11-27).** A generic machine may + call itself at other machines: each branch of its definition by cases + binds the machine parameters, so the recursion passes whichever it + names, and a law's induction hypothesis is the law at those machines, + with its premises owed there. One that recurses on fuel takes its machine + parameters after the fuel in the definition over fuel, its laws over fuel + count them among the formals, and a dispatch on a computed value is a + definition over them and the formals alike. A call of a generic machine + that has a premise owes the premise, stated over its machine parameters + and formals, at the machines and arguments it passes. A law reads a + generic machine that states nothing in place, its machine parameters + standing for the machines the citation passes; such a machine is + lowered. Any other body the clause reader cannot read as terms is + lowered and read like any other body. A generic machine's own `result` + in its clauses is its definition at its machine parameters and formals. + Customers: `generic_machine/pass/alternate_keeps_two_fixed_points`, + `pass/pick_by_a_computed_value`, `pass/call_owes_its_premise` and + `pass/cited_through_a_generic_helper`, each with its twin, and + `gcd_then` and `mod_then` over `core/nat.omg` in stage 06's + `generic_machine` tests, a generic `gcd` and repeated-subtraction `mod` + on fuel with their twins. A concrete machine with a premise is still no + machine argument: it is no total function for the parameter to stand for. - **The order and subtraction operators (2026-11-25).** `nat.omg` declares `<=` and `-` as free public operator machines, `machine <= less_or_equal(left: Nat, right: Nat) -> bool` and @@ -1397,9 +1415,6 @@ starts with records. program outside core imports both machines to select them by their tokens (`nat_order` fixtures). - **Not done:** - - A generic mathematical machine defined on fuel, or by a dispatch on a - computed value, and a call of a generic machine that has a premise. - Each is refused by name; nothing in the library does either. - `extent.omg`'s contract over `IntervalSet` (the plan's `extent.omg:219`) is a clause of a runtime boundary trait over generic data. It is read by the carrier clause reader when a program reaches