diff --git a/TASKS.md b/TASKS.md index 94c80d732b6..b382a618ffc 100644 --- a/TASKS.md +++ b/TASKS.md @@ -313,7 +313,7 @@ Find Rust owners in the [compiler map](source-rust/README.md#where-code-belongs) - **PROOF-KERNEL-CORE.** (covers **PROOF-CONTRACT-MIGRATION**, **QUOTIENT-THEOREM-LIFT**, **PSI-CLAIM-ADMISSION**) Complete [source proofs](wiki/spec/proofs/foundation.md) and [Psi claim admission](wiki/drafts/designs/psi_claim_admission.md). Report [coverage code 36](wiki/spec/build/compiler_request.md) with the obligation identity when the prover lacks a rule for its form. - Deliver [unified native inductives](wiki/spec/proofs/inductive_profile.md#native-inductive-declarations) from source through Psi. Check indexed `Vector` and a mutual block. Preserve the source indices, reject negative recursion, wrong indices, invalid universes, strict elimination and forged recursors at the responsible checker. Justify the new kernel rules, compare proof costs with the [prototype measurements](wiki/drafts/designs/recursive_data_encoding_experiments.md), and remove the production W/IW encoding after acceptance. - [Proof kernel core audit](wiki/drafts/designs/proof_kernel_core_audit.md): what #2823 removed, the three general rules still needed, the changes to stage 05's clause readers and the build order. - - Discharge the natural order's well-foundedness once and prove that the fuel a machine with [computed recursive descent](wiki/spec/language/termination.md#computed-recursive-arguments-in-proofs) is defined on suffices for every reachable call, so its laws hold independently of the bound. + - Discharge the natural order's well-foundedness once and prove that the fuel a program's machine with [computed recursive descent](wiki/spec/language/termination.md#computed-recursive-arguments-in-proofs) is defined on suffices for every reachable call, so its recursive equations hold and a law that needs them is provable. Core has no such machine left: `div`, `mod` and `gcd` are structural, and `gcd_within_agrees` is the source-level pattern (every sufficient bound gives the same answer). - Step 7 reads a quotient of mathematical data, proves equal classes for related representatives, checks `MereEquivalence` (including `IsMere` and Hedberg setness for plain data) and effectivity, and judges `Quotient::lift` and `Quotient::define` by their selected theorems. Ordinary programs store a quotient of custody-free copyable data as its representative. A clause may apply a lifted operation that the kernel's `lift` defines. A lift may compute, leave out or repeat its arguments. Still owed, each refused by name today: generic quotients, and laws over quotients of data that has storage (the signature states a record only of mathematical data, never one with a fixed-width field). [Audit, "Step 7 as built so far"](wiki/drafts/designs/proof_kernel_core_audit.md#plan-order) - Put the abstract carriers, trait operations and cited laws of a generic machine's Identity goals in the [mathematical signature](source-rust/subsystems/compiler/source-to-psi/representations/lowered-program/README.md#the-mathematical-signature); `equality::discharge` still builds that table per obligation. - Extend alias-typed theorem parameters to accept evidence projected from bundles, as in `use_step::(7u8)`. Stage 05 refuses every `term.member` proof argument by name. A record field typed `step: NonzeroIsPositive` is a structural callable field, so `const U8_LAWS: Laws = Laws { step: nonzero_is_positive }` needs a callable ceiling and then fails constant evaluation ("Terminal result scalar is outside the compiler-owned vocabulary"). Which field type declares mathematical evidence instead is owner question 5 in [OWNER_QUESTIONS.md](OWNER_QUESTIONS.md#bundle-evidence-field-type). [Theorem proof parameters](wiki/spec/proofs/mathematical_bindings.md#logical-hypotheses-and-machine-use) diff --git a/source-rust/application/tests/kernel_judged_laws.rs b/source-rust/application/tests/kernel_judged_laws.rs index 1854494b800..79665d63a1a 100644 --- a/source-rust/application/tests/kernel_judged_laws.rs +++ b/source-rust/application/tests/kernel_judged_laws.rs @@ -341,6 +341,26 @@ fn core_division_by_zero_is_refused_for_its_premise() { ); } +#[test] +fn core_gcd_computes_and_takes_euclids_step() { + // A zero divisor, a zero dividend, coprime numbers and common factors, + // each checked by computation through `gcd` of `core/nat.omg`, and + // Euclid's step at any positive divisor by citing `gcd_step`. + let (output, report) = check("core_gcd/pass/euclid"); + assert_eq!(output.status.code(), Some(0), "{report}"); +} + +#[test] +fn euclids_step_without_its_law_is_refused() { + let (output, report) = check("core_gcd/fail/euclid_without_its_step"); + assert_eq!(output.status.code(), Some(1), "{report}"); + assert!( + report.contains("cannot prove ensures contract for `euclid_at`") + && report.contains(UNPROVED), + "{report}" + ); +} + #[test] fn a_machine_that_recurses_on_a_computed_value_of_local_data_is_refused() { // `Peano` is the program's own data, so no natural order checks the diff --git a/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/src/tests/fuel_recursion.rs b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/src/tests/fuel_recursion.rs index f3a845c5cda..bcff993f99e 100644 --- a/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/src/tests/fuel_recursion.rs +++ b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/src/tests/fuel_recursion.rs @@ -144,14 +144,16 @@ pub(super) fn judged_library_files(nat: &str, files: &[(&str, &str)]) -> Vec= 2, - "the premise of `mod` at its call and the descent" + assert_eq!(judged("gcd"), 0, "no descent: the bound is structural"); + assert_eq!(judged("gcd_within"), 0, "no premise owed at its call"); + assert_eq!(judged("le_trans"), 1, "transitivity of the order"); + assert_eq!( + judged("gcd_within_agrees"), + 1, + "every sufficient bound agrees" ); + assert_eq!(judged("gcd_step"), 1, "Euclid's step"); for law in &judgment.laws { assert!( matches!(law.decision, MathematicalCoreDecision::Judged(_)), diff --git a/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/src/tests/nat_semiring.rs b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/src/tests/nat_semiring.rs index 3e6c56e3151..ac9faff6184 100644 --- a/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/src/tests/nat_semiring.rs +++ b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/src/tests/nat_semiring.rs @@ -176,9 +176,9 @@ fn an_operation_outside_the_conformance_gets_no_license_from_it() { /// judged through a program that reaches it, and most of these lemmas are /// private, so the file's text is compiled as the program's. Left out are the /// three ranking primitives only the toolchain may declare. `mod`, `div`, -/// `gcd` and the law relating `div` and `mod` are left out here too: -/// `fuel_recursion` judges the file with them, and the tests that build on -/// this text measure what they add without them. +/// `gcd`, Euclid's step and the law relating `div` and `mod` are left out +/// here too: `fuel_recursion` judges the file with them, and the tests that +/// build on this text measure what they add without them. /// Every other declaration is the file's, unchanged, the `<=` and `-` /// operator machines among them. pub(super) fn library_declarations() -> String { @@ -189,7 +189,7 @@ pub(super) fn library_declarations() -> String { let mut skipping = false; for line in NAT[start..].lines() { let declared = line.strip_prefix("public ").unwrap_or(line); - skipping |= ["mod", "div", "gcd", "div_mod_reconstruction"] + skipping |= ["mod", "div", "gcd", "gcd_step", "div_mod_reconstruction"] .iter() .any(|name| declared.starts_with(&format!("machine {name}("))); if !skipping { diff --git a/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/core_gcd/fail/euclid_without_its_step/build.omg b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/core_gcd/fail/euclid_without_its_step/build.omg new file mode 100644 index 00000000000..ccaeb10280f --- /dev/null +++ b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/core_gcd/fail/euclid_without_its_step/build.omg @@ -0,0 +1,12 @@ +// Deployable proof canary: every hosted target selects its exact entry. + +use main.omg as app; + +machine build(builder: @mutable BuildPlan) { + builder.product = BuildProduct::Application { name: "euclid_without_its_step" }; + 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); + Build::bind(builder, windows_arm64::ProgramEntry, |app|::entry_start); +} diff --git a/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/core_gcd/fail/euclid_without_its_step/main.omg b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/core_gcd/fail/euclid_without_its_step/main.omg new file mode 100644 index 00000000000..ebd482a95cb --- /dev/null +++ b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/core_gcd/fail/euclid_without_its_step/main.omg @@ -0,0 +1,61 @@ +// The program of `core_gcd/pass` with one change: `euclid_at` does not +// cite `gcd_step`. Its two sides are Euclid's algorithm at two different +// bounds, and without the law that sufficient bounds agree nothing proves +// them equal, so the compiler refuses its ensures. +use {omega}/language/core/foundation.omg as proof; +use {omega}/language/core/nat.omg; + +// zero divisor: gcd(5, 0) = 5. +machine gcd_5_0() -> |nat|::Nat +ensures + |proof|::Identity::<|nat|::Nat>(result, |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Zero } } } } }) +{ + transition { _ -> (|nat|::gcd(|nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Zero } } } } }, |nat|::Nat::Zero)) } +} + +// zero dividend: gcd(0, 5) = 5. +machine gcd_0_5() -> |nat|::Nat +ensures + |proof|::Identity::<|nat|::Nat>(result, |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Zero } } } } }) +{ + transition { _ -> (|nat|::gcd(|nat|::Nat::Zero, |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Zero } } } } })) } +} + +// coprime: gcd(7, 3) = 1. +machine gcd_7_3() -> |nat|::Nat +ensures + |proof|::Identity::<|nat|::Nat>(result, |nat|::Nat::Succ { prev: |nat|::Nat::Zero }) +{ + transition { _ -> (|nat|::gcd(|nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Zero } } } } } } }, |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Zero } } })) } +} + +// common factor: gcd(6, 4) = 2. +machine gcd_6_4() -> |nat|::Nat +ensures + |proof|::Identity::<|nat|::Nat>(result, |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Zero } }) +{ + transition { _ -> (|nat|::gcd(|nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Zero } } } } } }, |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Zero } } } })) } +} + +// common factor: gcd(9, 6) = 3. +machine gcd_9_6() -> |nat|::Nat +ensures + |proof|::Identity::<|nat|::Nat>(result, |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Zero } } }) +{ + transition { _ -> (|nat|::gcd(|nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Zero } } } } } } } } }, |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Zero } } } } } })) } +} + +// Euclid's step at any positive divisor `b`, from a program. +machine euclid_at(a: |nat|::Nat, b: |nat|::Nat) -> |nat|::Nat +requires + |proof|::Identity::<|nat|::Nat>(|nat|::saturating_sub(|nat|::Nat::Succ { prev: |nat|::Nat::Zero }, b), |nat|::Nat::Zero) +ensures + |proof|::Identity::<|nat|::Nat>(|nat|::gcd(a, b), |nat|::gcd(b, |nat|::mod(a, b))) +{ + transition { _ -> |nat|::Nat::Zero } +} + +data Main { } + +// The entry does nothing: the program is here for its proofs. +machine entry_start() { } diff --git a/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/core_gcd/pass/euclid/build.omg b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/core_gcd/pass/euclid/build.omg new file mode 100644 index 00000000000..dca4909f31d --- /dev/null +++ b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/core_gcd/pass/euclid/build.omg @@ -0,0 +1,12 @@ +// Deployable proof canary: every hosted target selects its exact entry. + +use main.omg as app; + +machine build(builder: @mutable BuildPlan) { + builder.product = BuildProduct::Application { name: "euclid" }; + 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); + Build::bind(builder, windows_arm64::ProgramEntry, |app|::entry_start); +} diff --git a/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/core_gcd/pass/euclid/main.omg b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/core_gcd/pass/euclid/main.omg new file mode 100644 index 00000000000..97fdabfe4f9 --- /dev/null +++ b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/fixtures/core_gcd/pass/euclid/main.omg @@ -0,0 +1,65 @@ +// Core's greatest common divisor: Euclid's algorithm with the divisor as the +// bound on its steps. Each closed machine computes `gcd` of two numbers and +// states the expected result, which the kernel checks by computation: +// a zero divisor, a zero dividend, coprime numbers and a common factor. +// `euclid_at` states Euclid's step, gcd(a, b) = gcd(b, mod(a, b)) for a +// positive `b`, and proves it by citing core's `gcd_step`. The negative +// twin drops that citation. +use {omega}/language/core/foundation.omg as proof; +use {omega}/language/core/nat.omg; + +// zero divisor: gcd(5, 0) = 5. +machine gcd_5_0() -> |nat|::Nat +ensures + |proof|::Identity::<|nat|::Nat>(result, |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Zero } } } } }) +{ + transition { _ -> (|nat|::gcd(|nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Zero } } } } }, |nat|::Nat::Zero)) } +} + +// zero dividend: gcd(0, 5) = 5. +machine gcd_0_5() -> |nat|::Nat +ensures + |proof|::Identity::<|nat|::Nat>(result, |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Zero } } } } }) +{ + transition { _ -> (|nat|::gcd(|nat|::Nat::Zero, |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Zero } } } } })) } +} + +// coprime: gcd(7, 3) = 1. +machine gcd_7_3() -> |nat|::Nat +ensures + |proof|::Identity::<|nat|::Nat>(result, |nat|::Nat::Succ { prev: |nat|::Nat::Zero }) +{ + transition { _ -> (|nat|::gcd(|nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Zero } } } } } } }, |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Zero } } })) } +} + +// common factor: gcd(6, 4) = 2. +machine gcd_6_4() -> |nat|::Nat +ensures + |proof|::Identity::<|nat|::Nat>(result, |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Zero } }) +{ + transition { _ -> (|nat|::gcd(|nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Zero } } } } } }, |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Zero } } } })) } +} + +// common factor: gcd(9, 6) = 3. +machine gcd_9_6() -> |nat|::Nat +ensures + |proof|::Identity::<|nat|::Nat>(result, |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Zero } } }) +{ + transition { _ -> (|nat|::gcd(|nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Zero } } } } } } } } }, |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Succ { prev: |nat|::Nat::Zero } } } } } })) } +} + +// Euclid's step at any positive divisor `b`, from a program. +machine euclid_at(a: |nat|::Nat, b: |nat|::Nat) -> |nat|::Nat +requires + |proof|::Identity::<|nat|::Nat>(|nat|::saturating_sub(|nat|::Nat::Succ { prev: |nat|::Nat::Zero }, b), |nat|::Nat::Zero) +ensures + |proof|::Identity::<|nat|::Nat>(|nat|::gcd(a, b), |nat|::gcd(b, |nat|::mod(a, b))) +{ + _ = |nat|::gcd_step(a, b); + transition { _ -> |nat|::Nat::Zero } +} + +data Main { } + +// The entry does nothing: the program is here for its proofs. +machine entry_start() { } diff --git a/source/library/core/nat.omg b/source/library/core/nat.omg index cef9baffac5..b18960c50fb 100644 --- a/source/library/core/nat.omg +++ b/source/library/core/nat.omg @@ -797,7 +797,7 @@ ensures // One is at most any successor: saturating_sub(Succ Zero, Succ pb) == Zero -- // premise-free, flat (strip the pair, annihilate on the left). The site -// fact gcd's recursive arm needs to discharge mod's positivity premise. +// fact a positive divisor `Succ pb` gives for `mod`'s and `div`'s premise. public machine le_one_succ(pb: Nat) -> Nat ensures |proof|::Identity::( @@ -816,28 +816,6 @@ ensures } } -// EUCLID (slice a3): gcd(a, Zero) = a; gcd(a, Succ pb) = gcd(b, mod(a, b)) -// at b = Succ pb. The recursive edge's measure argument is the COMPUTED -// mod(a, Succ pb); its strict decrease judges through the structural edge -// judge: the incoming payload alias (b == Succ { prev: pb }), le_one_succ's -// cited fact (discharging mod's positivity premise), and mod's own ensures -// at the let-citation (result < divisor) close the obligation. Lean analog: -// Nat.gcd. -machine gcd(a: Nat, b: Nat) -> Nat -terminates by b -{ - transition b { - Nat::Zero -> (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(Nat::Succ { prev: pb }, next)) } - } -} - // TRUNCATED SUBTRACTION BELOW A SUCCESSOR: for r < k, subtracting r leaves // one more than subtracting Succ(r) -- saturating_sub(k, r) == // Succ(saturating_sub(k, Succ r)). Division's countdown takes one step @@ -1050,3 +1028,168 @@ ensures transition { _ -> Nat::Zero } } } + +// Order is transitive: x <= y and y <= z entail x <= z, in core's spelling +// saturating_sub(x, z) == Zero. Induction on `x`; a successor on the left +// forces successors on the right (each premise would otherwise equate a +// successor with zero), and succ_sub_succ strips the three pairs. Lean +// analog: Nat.le_trans. +machine le_trans(x: Nat, y: Nat, z: Nat) -> Nat +terminates by x +requires + |proof|::Identity::(saturating_sub(x, y), Nat::Zero), + |proof|::Identity::(saturating_sub(y, z), Nat::Zero) +ensures + |proof|::Identity::(saturating_sub(x, z), Nat::Zero) +{ + transition x { + Nat::Zero -> lt_zero(z), + Nat::Succ { prev } -> lt_left(prev, y, z) + } + + machine lt_zero(z: Nat) -> Nat { + _ = sub_zero_left(z); + transition { _ -> Nat::Zero } + } + + machine lt_left(prev: Nat, y: Nat, z: Nat) -> Nat { + transition y { + Nat::Zero -> Nat::Zero, + Nat::Succ { prev as py } -> lt_middle(prev, py, z) + } + } + + machine lt_middle(prev: Nat, py: Nat, z: Nat) -> Nat { + transition z { + Nat::Zero -> Nat::Zero, + Nat::Succ { prev as pz } -> lt_all(prev, py, pz) + } + } + + machine lt_all(px: Nat, py: Nat, pz: Nat) -> Nat { + _ = succ_sub_succ(px, py); + _ = succ_sub_succ(py, pz); + _ = succ_sub_succ(px, pz); + transition { _ -> (le_trans(px, py, pz)) } + } +} + +// EUCLID: gcd(a, Zero) == a and gcd(a, Succ pb) == gcd(Succ pb, mod(a, +// Succ pb)). The remainder is strictly below the divisor, so a divisor `b` +// bounds the number of steps. `gcd_within(n, a, b)` takes at most `n` +// steps and recurses on `n` alone, so no fuel is needed; `gcd(a, b)` is +// gcd_within(b, a, b). With no steps left it answers `a`, which is right +// only for b == Zero; `gcd_within_agrees` shows that any bound `n` with +// b <= n gives the same answer, so the bound always suffices. Lean analog: +// Nat.gcd. +machine gcd_within(n: Nat, a: Nat, b: Nat) -> Nat +terminates by n +{ + transition n { + Nat::Zero -> (a), + Nat::Succ { prev } -> gcd_within_step(prev, a, b) + } + + machine gcd_within_step(prev: Nat, a: Nat, b: Nat) -> Nat { + transition b { + Nat::Zero -> (a), + Nat::Succ { prev as pb } -> gcd_within_next(prev, a, pb) + } + } + + machine gcd_within_next(prev: Nat, a: Nat, pb: Nat) -> Nat { + transition { _ -> (gcd_within(prev, Nat::Succ { prev: pb }, divide_step(a, pb, Nat::Zero, pb).remainder)) } + } +} + +public machine gcd(a: Nat, b: Nat) -> Nat { + transition { _ -> (gcd_within(b, a, b)) } +} + +// Every sufficient bound gives the same greatest common divisor: for +// b <= n and b <= m, gcd_within(n, a, b) == gcd_within(m, a, b). Induction +// on `n`. A bound of zero forces b == Zero, where both sides are `a`; with +// two successor bounds and a successor divisor both sides take one step +// to the remainder, which is below the divisor and so within both smaller +// bounds (`divide_bounds`, then `le_trans`). +machine gcd_within_agrees(n: Nat, m: Nat, a: Nat, b: Nat) -> Nat +terminates by n +requires + |proof|::Identity::(saturating_sub(b, n), Nat::Zero), + |proof|::Identity::(saturating_sub(b, m), Nat::Zero) +ensures + |proof|::Identity::(gcd_within(n, a, b), gcd_within(m, a, b)) +{ + transition n { + Nat::Zero -> gwa_none(m, a, b), + Nat::Succ { prev } -> gwa_some(prev, m, a, b) + } + + machine gwa_none(m: Nat, a: Nat, b: Nat) -> Nat { + transition b { + Nat::Zero -> gwa_divisor_zero(m, a), + Nat::Succ { prev as pb } -> Nat::Zero + } + } + + machine gwa_divisor_zero(m: Nat, a: Nat) -> Nat { + transition m { + Nat::Zero -> Nat::Zero, + Nat::Succ { prev as pm } -> Nat::Zero + } + } + + machine gwa_some(pn: Nat, m: Nat, a: Nat, b: Nat) -> Nat { + transition m { + Nat::Zero -> gwa_other_none(pn, a, b), + Nat::Succ { prev as pm } -> gwa_both(pn, pm, a, b) + } + } + + machine gwa_other_none(pn: Nat, a: Nat, b: Nat) -> Nat { + transition b { + Nat::Zero -> Nat::Zero, + Nat::Succ { prev as pb } -> Nat::Zero + } + } + + machine gwa_both(pn: Nat, pm: Nat, a: Nat, b: Nat) -> Nat { + transition b { + Nat::Zero -> Nat::Zero, + Nat::Succ { prev as pb } -> gwa_step(pn, pm, a, pb) + } + } + + machine gwa_step(pn: Nat, pm: Nat, a: Nat, pb: Nat) -> Nat { + _ = sub_self(pb); + _ = divide_bounds(a, pb, Nat::Zero, pb); + _ = succ_sub_succ(pb, pn); + _ = succ_sub_succ(pb, pm); + _ = le_trans(divide_step(a, pb, Nat::Zero, pb).remainder, pb, pn); + _ = le_trans(divide_step(a, pb, Nat::Zero, pb).remainder, pb, pm); + transition { _ -> (gcd_within_agrees(pn, pm, Nat::Succ { prev: pb }, divide_step(a, pb, Nat::Zero, pb).remainder)) } + } +} + +// Euclid's step as a law of `gcd`, on every input with a positive +// divisor: gcd(a, b) == gcd(b, mod(a, b)). Both sides are `gcd_within` at +// different bounds, which agree. +public machine gcd_step(a: Nat, b: Nat) -> Nat +requires + |proof|::Identity::(saturating_sub(Nat::Succ { prev: Nat::Zero }, b), Nat::Zero) +ensures + |proof|::Identity::(gcd(a, b), gcd(b, mod(a, b))) +{ + transition b { + Nat::Zero -> Nat::Zero, + Nat::Succ { prev as pb } -> gs_of(a, pb) + } + + machine gs_of(a: Nat, pb: Nat) -> Nat { + _ = sub_self(pb); + _ = divide_bounds(a, pb, Nat::Zero, pb); + _ = sub_self(divide_step(a, pb, Nat::Zero, pb).remainder); + _ = gcd_within_agrees(pb, divide_step(a, pb, Nat::Zero, pb).remainder, Nat::Succ { prev: pb }, divide_step(a, pb, Nat::Zero, pb).remainder); + transition { _ -> Nat::Zero } + } +} diff --git a/wiki/drafts/designs/proof_kernel_core_audit.md b/wiki/drafts/designs/proof_kernel_core_audit.md index 974a04717b1..97a356336f3 100644 --- a/wiki/drafts/designs/proof_kernel_core_audit.md +++ b/wiki/drafts/designs/proof_kernel_core_audit.md @@ -1299,6 +1299,21 @@ starts with records. strict bound hold on every admitted input. The prover now reduces a projection of a pair, so a law can read a field of a computed record. Only `gcd` still recurses at a computed value, on fuel with its descent checked. +- **Structural gcd (2026-11-27).** `gcd` is no longer defined on fuel + either. It is `gcd_within(b, a, b)`: Euclid's algorithm with an explicit + bound on its steps, recursing on the bound alone. The divisor bounds the + steps because each remainder is strictly below its divisor + (`divide_bounds`). `gcd_within_agrees` proves by induction on the bound + that every bound at least the divisor gives the same answer, the source + form of "the counter suffices" in the computed descent ruling, and + `gcd_step` turns it into Euclid's step, `gcd(a, b) = gcd(b, mod(a, b))` + for a positive `b`. `le_trans` joins the order lemmas. Core now has no + machine defined on fuel. The compiler's fuel definition remains for a + program's own machine with computed descent; for those, the order's + well-foundedness and the sufficiency of the fuel are still not proved, so + their recursive equations are not available to a law. The termination + spec's "core proves once that this order is well founded" is not yet + true of the implementation. - **A choice by a computed value** (`rat_eq`, `mod`) is a definition by cases of its own over its subject and the variables in scope, so the prover computes with it as with any other. In a law, a split on a computed