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 @@ -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
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
})
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Step>(pred, a)))",
"(Step(iterate::<succ>(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 Step>",
"machine succ(a: Peano) -> Peano
{
transition { _ -> Peano::Succ { pred: a } }
}

machine iterate<machine Step>",
/// 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<RouteVerdict> {
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<machine Step>(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::<Step>(Nat::Succ {{ prev: pb }}, next)) }}
}}
}}

machine mod_then<machine Step>(a: Nat, b: Nat) -> Nat
requires machine Step(a: Nat) -> Nat terminates;
terminates by a
requires
|proof|::Identity::<Nat>(saturating_sub(Nat::Succ {{ prev: Nat::Zero }}, b), Nat::Zero)
ensures
|proof|::Identity::<Nat>(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::<Step>(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::<Nat>(
gcd_then::<nat_succ>(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::<succ>(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",
);
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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<machine Step, machine Other>(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::<Other, Step>(pred, a)))
}
}

machine alternate_keeps<machine Step, machine Other>(n: Peano, a: Peano)
requires machine Step(a: Peano) -> Peano terminates;
requires machine Other(a: Peano) -> Peano terminates;
terminates by n
requires
|proof|::Identity::<Peano>(Step(a), a)
ensures
|proof|::Identity::<Peano>(alternate::<Step, Other>(n, a), a)
{
transition n {
Peano::Zero -> base(),
Peano::Succ { pred } -> step(pred, a)
}
machine base() {
}
machine step(pred: Peano, a: Peano) {
alternate_keeps::<Other, Step>(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);
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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<machine Step>(a: Peano) -> Peano
requires machine Step(a: Peano) -> Peano terminates;
requires
|proof|::Identity::<Peano>(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::<succ>(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);
}
Original file line number Diff line number Diff line change
@@ -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);
}
Loading
Loading