blob: 9423a5120e95068cf8c227bb7fdf36bcd80f5161 [file] [edit]
/-
# `ConRon.Arena.DeclCheck` — the declaration-level checks, over handles
The twin of `ConLeche/Kernel/DeclCheck.lean` and of the declaration-level
halves of `ConLeche/Kernel/Checker.lean`, `StdAxioms.lean` and
`TrustAxioms.lean`.
## One twin per PAIR
con-leche carries each of these functions twice — once reading the linear
environment (`Checker.lean`, `StdAxioms.lean`, `TrustAxioms.lean`) and once
the index (`DeclCheck.lean`'s `…F` mirrors, which is what both binaries run).
The arena has ONE environment type, the index (`Arena/Core.lean`'s deviation
1), so each pair collapses into one twin carrying a `con-leche:` line per
collapsed declaration, and the twin keeps the UNSUFFIXED name — as
`Arena/Core.lean`'s thirteen collapsed pairs do.
## No closures
* `divModCertStmts`' eleven local lambdas (`ble2`, `eqB`, `eqN`, `op2`, …) are
named `def`s here, as `Arena/Core.lean`'s `natOpEquations` does with its
three. DESIGN §3.4 forbids a function value in code Aeneas must translate,
and over handles each of them has to intern anyway.
* `divModCertsGuard`'s `(… .zip …).all (fun p => …)` is an explicit
two-list recursion.
-/
module
public import ConRon.Arena.Basis
public import ConRon.Arena.NatOpPinSet
public import ConRon.Arena.TrustAxioms
public import ConRon.Arena.StdAxioms
@[expose] public section
namespace ConRon.Arena
open ConLeche
/-! ## The standard axioms' environment shape -/
/-- con-leche: ConLeche/Kernel/StdAxioms.lean:168-214 stdAxiomOk
con-leche: ConLeche/Kernel/DeclCheck.lean:87-117 stdAxiomOkF
con-leche: ConLeche/Kernel/TrustAxioms.lean:174-184 ofReduceAxOk
con-leche: ConLeche/Kernel/Checker.lean:295-308 divModEnvGuard
The `(fe.find? eqName).isSome` the three guards share: the environment holds
`Eq` only ever as a copy of its pin (at the stream's level-parameter names), so
presence is the whole test. -/
def eqStored (fe : IFEnv) : AM Bool := do
pure (fe.find? (← pinEq)).isSome
/-- con-leche: ConLeche/Kernel/StdAxioms.lean:168-214 stdAxiomOk
con-leche: ConLeche/Kernel/DeclCheck.lean:87-117 stdAxiomOkF
Is this checked axiom one of the two recognized standard axioms, over
standardly-shaped stored `Iff` / `Nonempty` families (and the pinned `Eq`
basis)? All three of each family's constants are pinned (the check must
REALIZE the axiom). The comparands are con-leche's pins `iffA` … `choiceA`,
compared by plain equality (`matchesPin`, con-leche's task #342). -/
def stdAxiomOk (fe : IFEnv) (cv : IConstantVal) : AM Bool := do
let pn ← propextName
if cv.name == pn then do
if !(← eqStored fe) then pure false else do
match fe.find? (← iffName) with
| some (.indInfo cvI _) => do
if !(← cvI.matchesPin (← iffA).toConstantVal) then pure false else do
match fe.find? (← iffIntroName) with
| some (.ctorInfo cvIi 2 2 _) => do
if !(← cvIi.matchesPin (← iffIntroA).toConstantVal) then pure false
else do
match fe.find? (← iffRecName) with
| some (.recInfo cvIr 4 4 _ _) => do
if !(← cvIr.matchesPin (← iffRecA).toConstantVal) then pure false
else cv.matchesPin (← propextA)
| _ => pure false
| _ => pure false
| _ => pure false
else do
-- the `Classical.choice` name is read only past the `propext` test, as the
-- port's `std_axiom_ok` does (task #97-T2-LOCKSTEP lane Checker DeclCheck
-- slice 2: the twin read both names first; a pin read can fail)
let cn ← choiceName
if cv.name == cn then do
match fe.find? (← nonemptyName) with
| some (.indInfo cvN _) => do
if !(← cvN.matchesPin (← nonemptyA).toConstantVal) then pure false else do
match fe.find? (← nonemptyIntroName) with
| some (.ctorInfo cvNi 1 1 _) => do
if !(← cvNi.matchesPin (← nonemptyIntroA).toConstantVal) then pure false
else do
match fe.find? (← nonemptyRecName) with
| some (.recInfo cvNr 3 3 _ _) => do
if !(← cvNr.matchesPin (← nonemptyRecA).toConstantVal) then pure false
else cv.matchesPin (← choiceA)
| _ => pure false
| _ => pure false
| _ => pure false
else pure false
/-! ## The compiler-trust family's environment shape -/
/-- con-leche: ConLeche/Kernel/TrustAxioms.lean:141-152 trustCompilerOk
con-leche: ConLeche/Kernel/DeclCheck.lean:119-127 trustCompilerOkF
Is `Lean.trustCompiler` installable here? The `True` family must be stored
with the pinned shapes, and the checked axiom's type must match the pin. -/
def trustCompilerOk (fe : IFEnv) (cv : IConstantVal) : AM Bool := do
match fe.find? (← trueName) with
| some (.indInfo cvT _) => do
if !(← cvT.matchesPin (← trueCvA)) then pure false else do
match fe.find? (← trueIntroName) with
| some (.ctorInfo cvTi 0 0 _) => do
if !(← cvTi.matchesPin (← trueIntroCvA)) then pure false
else cv.matchesPin (← trustCompilerA)
| _ => pure false
| _ => pure false
/-- con-leche: ConLeche/Kernel/TrustAxioms.lean:154-160 reduceStoredOk
con-leche: ConLeche/Kernel/DeclCheck.lean:129-133 reduceStoredOkF
Is the reduce operation `c` stored as a checked opaque (`axiomInfo`, the
storage kind of every checked `opaque`) of the pinned type? -/
def reduceStoredOk (fe : IFEnv) (c : NIdx) : AM Bool := do
match fe.find? c with
| some (.axiomInfo cvR) => cvR.matchesPin (← reduceOpCvA c)
| _ => pure false
/-- con-leche: ConLeche/Kernel/TrustAxioms.lean:162-172 reduceElemOk
con-leche: ConLeche/Kernel/DeclCheck.lean:135-144 reduceElemOkF
The element-inductive shape an `ofReduce*` axiom needs: a stored `Nat`
inductive at the pin's `ConstantVal` resp. a standardly-shaped stored `Bool`. -/
def reduceElemOk (fe : IFEnv) (c : NIdx) : AM Bool := do
let rn ← reduceNatName
if c == rn then do
let nn ← pinNat
match fe.find? nn with
| some (.indInfo cvN _) => cvN.matchesPin (← natA).toConstantVal
| _ => pure false
else
match fe.find? (← boolName) with
| some (.indInfo cvB _) => cvB.matchesPin (← boolCvA)
| _ => pure false
/-- con-leche: ConLeche/Kernel/TrustAxioms.lean:174-184 ofReduceAxOk
con-leche: ConLeche/Kernel/DeclCheck.lean:146-152 ofReduceAxOkF
Is this checked axiom a pinned `ofReduce*` over a standardly-shaped
environment? -/
def ofReduceAxOk (fe : IFEnv) (cv : IConstantVal) : AM Bool := do
let c ← ofReduceOp cv.name
if !(← eqStored fe) then pure false
else if !(← reduceElemOk fe c) then pure false
else if !(← reduceStoredOk fe c) then pure false
else cv.matchesPin (← ofReducePinA cv.name)
/-- con-leche: ConLeche/Kernel/TrustAxioms.lean:195-199 reducePinGuard
con-leche: ConLeche/Kernel/DeclCheck.lean:154-158 reducePinGuardF
con-leche: ConLeche/Cached/ParsedC.lean:221-225 reducePinGuardC
Syntactic guards on the generated reduce pin (checked once). -/
def reducePinGuard (fe : IFEnv) (c : NIdx) : AM Bool := do
let p ← reduceDeclPin c
if !(← looseBVarsBoundedFast coreWalkFuel 0 p) then pure false
else if ← hasFvarFast coreWalkFuel p then pure false
else if !(← allLevelParamsDefined [] p) then pure false
else constsResolveFFast fe p
/-! `lps.map .param` as an interned universe-argument list — the spelling of
"the constant at its own level parameters", which every model statement below
is written with — is `Arena/Inductives/StructParts.lean`'s `paramLevels`
(P2d-2's half of this phase, which needs the same spelling for the block's
own types). One twin, in the module that sits lower. -/
/-! ## The `Nat.div`/`Nat.mod` pin variants
`Arena/NatOpPinSet.lean` holds the record; these are the checks that read
it. -/
/-- con-leche: ConLeche/Kernel/Checker.lean:113-125 divModDeclPin — the pinned
defining expression of a pin-certified WF-recursive op in one pin variant.
con-leche's `c = natDivName` chain is a handle comparison here. -/
def divModDeclPin (ps : INatOpPinSet) (c : NIdx) : AM EIdx := do
if c == (← natDivName) then pure ps.divPin
else if c == (← natGcdName) then pure ps.gcdPin
else if c == (← natLandName) then pure ps.landPin
else if c == (← natLorName) then pure ps.lorPin
else if c == (← natXorName) then pure ps.xorPin
else if c == (← natShiftLeftName) then pure ps.shiftLeftPin
else if c == (← natShiftRightName) then pure ps.shiftRightPin
else pure ps.modPin
/-- con-leche: ConLeche/Kernel/Checker.lean:127-137 divModCertProofs — the
certificate proof terms of a pin-certified WF-recursive op in one pin
variant, one per statement of `divModCertStmts`. -/
def divModCertProofs (ps : INatOpPinSet) (c : NIdx) : AM (List EIdx) := do
if c == (← natDivName) then pure ps.divProofs
else if c == (← natGcdName) then pure ps.gcdProofs
else if c == (← natLandName) then pure ps.landProofs
else if c == (← natLorName) then pure ps.lorProofs
else if c == (← natXorName) then pure ps.xorProofs
else if c == (← natShiftLeftName) then pure ps.shiftLeftProofs
else if c == (← natShiftRightName) then pure ps.shiftRightProofs
else pure ps.modProofs
/-- con-leche: ConLeche/Kernel/Checker.lean:139-151 divModTypePins — the
type pins of a pin-certified WF-recursive op in one pin variant: the
constants its certificates use whose types differ between supported
toolchains, each with its type in this variant's toolchain. -/
def divModTypePins (ps : INatOpPinSet) (c : NIdx) : AM (List (NIdx × EIdx)) := do
if c == (← natDivName) then pure ps.divTypePins
else if c == (← natGcdName) then pure ps.gcdTypePins
else if c == (← natLandName) then pure ps.landTypePins
else if c == (← natLorName) then pure ps.lorTypePins
else if c == (← natXorName) then pure ps.xorTypePins
else if c == (← natShiftLeftName) then pure ps.shiftLeftTypePins
else if c == (← natShiftRightName) then pure ps.shiftRightTypePins
else pure ps.modTypePins
/-- con-leche: ConLeche/Kernel/Checker.lean:153-160 typePinStored
con-leche: ConLeche/Kernel/DeclCheck.lean:182-186 typePinStoredF
The stored type a type pin is compared against: the constant's type in the
environment. An absent constant gives `Sort 0`, which nothing is compared
against: the guard (`divModPinGuard`) requires every type-pinned constant
present. -/
def typePinStored (fe : IFEnv) (n : NIdx) : AM EIdx := do
match fe.find? n with
| some ci => pure ci.toConstantVal.type
| none => internE (.sort (← zeroLevel))
/-! ### The pinned characterization statements
con-leche writes `divModCertStmts` with eleven local lambdas over `Expr`
constructors. Over handles every one of them interns, and DESIGN §3.4
forbids the function values, so each is a `def` — the treatment
`Arena/Core.lean` gives `natOpEquations`' three. -/
/-- con-leche: ConLeche/Kernel/Checker.lean:162-240 divModCertStmts — the
statements' `Eq.{1} τ a b` former. -/
def eqAt1 (ty a b : EIdx) : AM EIdx := do
let z ← zeroLevel
let one ← internLNode (.succ z)
let us ← internLsNode [one]
let en ← pinEq
let e ← internE (.const en us)
let e1 ← internE (.app e ty)
let e2 ← internE (.app e1 a)
internE (.app e2 b)
/-- con-leche: ConLeche/Kernel/Checker.lean:162-240 divModCertStmts — the
numeral `1` as `Nat.succ Nat.zero`. -/
def natOne : AM EIdx := do
let s ← pinNatSucc
let z ← pinNatZero
natAp1 s (← constE z)
/-- con-leche: ConLeche/Kernel/Checker.lean:162-240 divModCertStmts — the
open statements' variables `x := fvar 0`, `y := fvar 1` at `Nat`. -/
def natVar (i : Nat) : AM EIdx := do
let nt ← pinNat
internE (.fvar i (← constE nt))
/-- con-leche: ConLeche/Kernel/Checker.lean:162-240 divModCertStmts — the
twenty-one pinned handles the statements are built from (the port's
`CertCtx`: a record, because a `let`-bound handle that outlives a `match` arm is
a loan the Aeneas subset will not take; the twin follows the port's factoring,
task #97-T2-LOCKSTEP lane Checker DeclCheck slice 2). -/
structure CertCtxA where
natTy : EIdx
x : EIdx
y : EIdx
one : EIdx
bleN : NIdx
boolTy : EIdx
bT : EIdx
bF : EIdx
z : EIdx
two : EIdx
modN : NIdx
divN : NIdx
addN : NIdx
mulN : NIdx
subN : NIdx
gcdN : NIdx
slN : NIdx
srN : NIdx
landN : NIdx
lorN : NIdx
xorN : NIdx
/-- con-leche: ConLeche/Kernel/Checker.lean:162-240 divModCertStmts — the
context, in the port's order (`cert_ctx`). -/
def certCtx : AM CertCtxA := do
let nt ← pinNat
let natTy ← constE nt
let x ← natVar 0
let y ← natVar 1
let one ← natOne
let bleN ← natBleName
let bn ← boolName
let boolTy ← constE bn
let bT ← constE (← boolTrueName)
let bF ← constE (← boolFalseName)
let z ← constE (← pinNatZero)
let two ← natAp1 (← pinNatSucc) one
let modN ← natModName
let divN ← natDivName
let addN ← natAddName
let mulN ← natMulName
let subN ← natSubName
let gcdN ← natGcdName
let slN ← natShiftLeftName
let srN ← natShiftRightName
let landN ← natLandName
let lorN ← natLorName
let xorN ← natXorName
pure ⟨natTy, x, y, one, bleN, boolTy, bT, bF, z, two, modN, divN, addN, mulN,
subN, gcdN, slN, srN, landN, lorN, xorN⟩
/-- con-leche: ConLeche/Kernel/Checker.lean:162-240 divModCertStmts — the
guard `ble a b = r` (`eqB (ble2 a b) r`). -/
def certGuard (cx : CertCtxA) (a b r : EIdx) : AM EIdx := do
eqAt1 cx.boolTy (← natAp2 cx.bleN a b) r
/-- con-leche: ConLeche/Kernel/Checker.lean:162-240 divModCertStmts — the
characteristic equation `c x y = rhs` (`eqN (op2 x y) rhs`), its left side
interned AFTER the right one, as the port does. -/
def certEq (cx : CertCtxA) (c : NIdx) (rhs : EIdx) : AM EIdx := do
eqAt1 cx.natTy (← natAp2 c cx.x cx.y) rhs
/-- con-leche: ConLeche/Kernel/Checker.lean:162-240 divModCertStmts — the
bitwise branches' `op2 (div2 x two) (div2 y two)`. -/
def certHalves (cx : CertCtxA) (c : NIdx) : AM EIdx := do
let hx ← natAp2 cx.divN cx.x cx.two
let hy ← natAp2 cx.divN cx.y cx.two
natAp2 c hx hy
/-- con-leche: ConLeche/Kernel/Checker.lean:162-240 divModCertStmts — the
six one-hypothesis branches' two certificates. -/
def certTwoEqs (cx : CertCtxA) (c : NIdx) (h1 h2 r1 r2 : EIdx) :
AM (List (List EIdx × EIdx)) := do
let e1 ← certEq cx c r1
let e2 ← certEq cx c r2
pure [([h1], e1), ([h2], e2)]
/-- con-leche: ConLeche/Kernel/Checker.lean:162-240 divModCertStmts —
`gcd`: `1 ≤ x → gcd x y = gcd (y % x) x`, `x = 0 → gcd x y = y`. -/
def certGcd (cx : CertCtxA) (c : NIdx) : AM (List (List EIdx × EIdx)) := do
let h1 ← certGuard cx cx.one cx.x cx.bT
let h2 ← certGuard cx cx.one cx.x cx.bF
let r1 ← natAp2 c (← natAp2 cx.modN cx.y cx.x) cx.x
certTwoEqs cx c h1 h2 r1 cx.y
/-- con-leche: ConLeche/Kernel/Checker.lean:162-240 divModCertStmts —
`1 ≤ y → x <<< y = (2*x) <<< (y-1)`, `y = 0 → x <<< y = x`. -/
def certShiftLeft (cx : CertCtxA) (c : NIdx) : AM (List (List EIdx × EIdx)) := do
let h1 ← certGuard cx cx.one cx.y cx.bT
let h2 ← certGuard cx cx.one cx.y cx.bF
let r1 ← natAp2 c (← natAp2 cx.mulN cx.two cx.x) (← natAp2 cx.subN cx.y cx.one)
certTwoEqs cx c h1 h2 r1 cx.x
/-- con-leche: ConLeche/Kernel/Checker.lean:162-240 divModCertStmts —
`1 ≤ y → x >>> y = (x >>> (y-1)) / 2`, `y = 0 → x >>> y = x`. -/
def certShiftRight (cx : CertCtxA) (c : NIdx) : AM (List (List EIdx × EIdx)) := do
let h1 ← certGuard cx cx.one cx.y cx.bT
let h2 ← certGuard cx cx.one cx.y cx.bF
let r1 ← natAp2 cx.divN (← natAp2 c cx.x (← natAp2 cx.subN cx.y cx.one)) cx.two
certTwoEqs cx c h1 h2 r1 cx.x
/-- con-leche: ConLeche/Kernel/Checker.lean:162-240 divModCertStmts —
`1 ≤ x → x &&& y = 2*((x/2) &&& (y/2)) + (x%2)*(y%2)`, `x = 0 → … = 0`. -/
def certLand (cx : CertCtxA) (c : NIdx) : AM (List (List EIdx × EIdx)) := do
let h1 ← certGuard cx cx.one cx.x cx.bT
let h2 ← certGuard cx cx.one cx.x cx.bF
let rec1 ← certHalves cx c
let r1 ← natAp2 cx.addN (← natAp2 cx.mulN cx.two rec1)
(← natAp2 cx.mulN (← natAp2 cx.modN cx.x cx.two)
(← natAp2 cx.modN cx.y cx.two))
certTwoEqs cx c h1 h2 r1 cx.z
/-- con-leche: ConLeche/Kernel/Checker.lean:162-240 divModCertStmts —
`|||`'s right-hand side `2*((x/2) ||| (y/2)) + (x%2 + y%2 - (x%2)*(y%2))`. -/
def certLorRhs (cx : CertCtxA) (c : NIdx) : AM EIdx := do
let rec1 ← certHalves cx c
let t2 ← natAp2 cx.mulN cx.two rec1
let mx ← natAp2 cx.modN cx.x cx.two
let my ← natAp2 cx.modN cx.y cx.two
natAp2 cx.addN t2 (← natAp2 cx.subN (← natAp2 cx.addN mx my)
(← natAp2 cx.mulN mx my))
/-- con-leche: ConLeche/Kernel/Checker.lean:162-240 divModCertStmts —
`|||`: `x = 0 → x ||| y = y`. -/
def certLor (cx : CertCtxA) (c : NIdx) : AM (List (List EIdx × EIdx)) := do
let h1 ← certGuard cx cx.one cx.x cx.bT
let h2 ← certGuard cx cx.one cx.x cx.bF
let r1 ← certLorRhs cx c
certTwoEqs cx c h1 h2 r1 cx.y
/-- con-leche: ConLeche/Kernel/Checker.lean:162-240 divModCertStmts —
`^^^`'s right-hand side `2*((x/2) ^^^ (y/2)) + (x%2 + y%2) % 2`. -/
def certXorRhs (cx : CertCtxA) (c : NIdx) : AM EIdx := do
let rec1 ← certHalves cx c
let t2 ← natAp2 cx.mulN cx.two rec1
let mx ← natAp2 cx.modN cx.x cx.two
let my ← natAp2 cx.modN cx.y cx.two
natAp2 cx.addN t2 (← natAp2 cx.modN (← natAp2 cx.addN mx my) cx.two)
/-- con-leche: ConLeche/Kernel/Checker.lean:162-240 divModCertStmts —
`^^^`: `x = 0 → x ^^^ y = y`. -/
def certXor (cx : CertCtxA) (c : NIdx) : AM (List (List EIdx × EIdx)) := do
let h1 ← certGuard cx cx.one cx.x cx.bT
let h2 ← certGuard cx cx.one cx.x cx.bF
let r1 ← certXorRhs cx c
certTwoEqs cx c h1 h2 r1 cx.y
/-- con-leche: ConLeche/Kernel/Checker.lean:162-240 divModCertStmts — the
`div`/`mod` branch's recursive right-hand side: `c (x - y) y`, under
`Nat.succ` for `div` (the successor pin read after the two interns, as the
port's `cert_rec_rhs` does). -/
def certRecRhs (cx : CertCtxA) (c : NIdx) : AM EIdx := do
let d ← natAp2 cx.subN cx.x cx.y
let step ← natAp2 c d cx.y
if c == cx.divN then natAp1 (← pinNatSucc) step else pure step
/-- con-leche: ConLeche/Kernel/Checker.lean:162-240 divModCertStmts — the
`div`/`mod` branch's three certificates, at the four guards. -/
def certDivModEqs (cx : CertCtxA) (c : NIdx)
(h1 h2 h3 h4 recRhs baseRhs : EIdx) : AM (List (List EIdx × EIdx)) := do
let e1 ← certEq cx c recRhs
let e2 ← certEq cx c baseRhs
pure [([h1, h2], e1), ([h3], e2), ([h4], e2)]
/-- con-leche: ConLeche/Kernel/Checker.lean:162-240 divModCertStmts — the
`div`/`mod` branch's four guards. -/
def certDivModGuards (cx : CertCtxA) (c : NIdx) (recRhs baseRhs : EIdx) :
AM (List (List EIdx × EIdx)) := do
let h1 ← certGuard cx cx.y cx.x cx.bT
let h2 ← certGuard cx cx.one cx.y cx.bT
let h3 ← certGuard cx cx.y cx.x cx.bF
let h4 ← certGuard cx cx.one cx.y cx.bF
certDivModEqs cx c h1 h2 h3 h4 recRhs baseRhs
/-- con-leche: ConLeche/Kernel/Checker.lean:162-240 divModCertStmts — the
`div`/`mod` branch. -/
def certDivMod (cx : CertCtxA) (c : NIdx) : AM (List (List EIdx × EIdx)) := do
let recRhs ← certRecRhs cx c
let baseRhs := if c == cx.divN then cx.z else cx.x
certDivModGuards cx c recRhs baseRhs
/-- con-leche: ConLeche/Kernel/Checker.lean:162-240 divModCertStmts — the
seven-way dispatch over the operation name. -/
def divModCertStmtsAt (cx : CertCtxA) (c : NIdx) :
AM (List (List EIdx × EIdx)) := do
if c == cx.gcdN then certGcd cx c
else if c == cx.slN then certShiftLeft cx c
else if c == cx.srN then certShiftRight cx c
else if c == cx.landN then certLand cx c
else if c == cx.lorN then certLor cx c
else if c == cx.xorN then certXor cx c
else certDivMod cx c
/-- con-leche: ConLeche/Kernel/Checker.lean:162-240 divModCertStmts — **the
pinned characterization statements** of a pin-certified WF-recursive op, in
*open* form over `x := fvar 0`, `y := fvar 1` (the hypotheses become
`fvar 2, fvar 3`): per certificate, the list of hypothesis types and the
characteristic equation `Eq Nat lhs rhs`. The guards are spelled with the
already-certified `Nat.ble` and the numeral `1` as `Nat.succ Nat.zero`.
In the port's factoring (task #97-T2-LOCKSTEP lane Checker DeclCheck slice 2):
the twin used to intern each equation's left side `c x y` before its right
side, and the port (`cert_eq`) interns it after — different append order,
different handles. -/
def divModCertStmts (c : NIdx) : AM (List (List EIdx × EIdx)) := do
divModCertStmtsAt (← certCtx) c
/-- con-leche: ConLeche/Kernel/Checker.lean:242-255 divModCertApplied — the
vendored proof applied to the statement's free variables (`x`, `y`, then one
`fvar` per hypothesis, carrying the hypothesis *type* as its annotation — the
checker's implicit local context). -/
def divModCertApplied (proofS : EIdx) (hyps : List EIdx) : AM EIdx := do
let x ← natVar 0
let y ← natVar 1
let b1 ← internE (.app proofS x)
let base ← internE (.app b1 y)
match hyps with
| [h1] => do
let f2 ← internE (.fvar 2 h1)
internE (.app base f2)
| [h1, h2] => do
let f2 ← internE (.fvar 2 h1)
let a1 ← internE (.app base f2)
let f3 ← internE (.fvar 3 h2)
internE (.app a1 f3)
| _ => pure base
/-- con-leche: none — `hyps.map (Expr.substConst0 c annVal)`, as an explicit
list recursion (DESIGN §3.4 forbids the closure). -/
def substConst0List (n : NIdx) (r : EIdx) : List EIdx → AM (List EIdx)
| [] => pure []
| h :: hs => do
let h' ← substConst0 n r coreWalkFuel h
let hs' ← substConst0List n r hs
pure (h' :: hs')
/-- con-leche: none — `(hyps.map …).all (fun h => h.constsResolveF fe)`, as an
explicit list recursion. -/
def constsResolveAll (fe : IFEnv) : List EIdx → AM Bool
| [] => pure true
| h :: hs => do
if ← constsResolveFFast fe h then constsResolveAll fe hs else pure false
/-- con-leche: ConLeche/Kernel/Checker.lean:257-268 divModCertGuard
con-leche: ConLeche/Kernel/DeclCheck.lean:171-180 divModCertGuardF
The syntactic guards of one certificate check: the substituted proof is
closed, level-monomorphic and resolving, and the substituted statement
components resolve. -/
def divModCertGuard (fe : IFEnv) (c : NIdx) (annVal : EIdx)
(hyps : List EIdx) (eqE proof : EIdx) : AM Bool := do
let p ← substConstAll c annVal coreWalkFuel proof
if !(← looseBVarsBoundedFast coreWalkFuel 0 p) then pure false
else if ← hasFvarFast coreWalkFuel p then pure false
else if !(← allLevelParamsDefined [] p) then pure false
else if !(← constsResolveFFast fe p) then pure false
else if !(← constsResolveAll fe (← substConst0List c annVal hyps)) then
pure false
else constsResolveFFast fe (← substConst0 c annVal coreWalkFuel eqE)
/-- con-leche: ConLeche/Kernel/Checker.lean:295-308 divModEnvGuard
One `Bool` constructor's clause: is `n` stored with the type `Bool` itself? The
lookup first, then the stored type, then `Bool` interned: the Rust's order
(`bool_ctor_typed`). The twin used to intern `.const Bool []` once, BEFORE
both lookups, which writes the store where the Rust (on a miss) does not —
task #97-T2-LOCKSTEP lane Checker round 2. -/
def boolCtorTyped (fe2 : IFEnv) (n : NIdx) : AM Bool := do
match fe2.find? n with
| some ci => do
let cv := ci.toConstantVal
let bn ← boolName
let boolTy ← constE bn
pure (cv.type == boolTy)
| none => pure false
/-- con-leche: ConLeche/Kernel/Checker.lean:295-308 divModEnvGuard
con-leche: ConLeche/Kernel/DeclCheck.lean:160-169 divModEnvGuardF
Environment prerequisites of a certified `Nat.div`/`Nat.mod`: dependency
guard, the operation's own pinned type, the pinned `Eq` basis, and the `Bool`
constructors stored at the type `Bool` itself. -/
def divModEnvGuard (fe2 : IFEnv) (c : NIdx) : AM Bool := do
if !(← natOpGuard fe2 c) then pure false else do
if !(← natOpStoredOk fe2 c) then pure false else do
if !(← eqStored fe2) then pure false else do
if !(← boolCtorTyped fe2 (← boolTrueName)) then pure false
else boolCtorTyped fe2 (← boolFalseName)
/-- con-leche: ConLeche/Kernel/Checker.lean:310-320 divModPinGuard
con-leche: ConLeche/Kernel/DeclCheck.lean:188-195 divModPinGuardF
con-leche: ConLeche/Cached/ParsedC.lean:151-158 divModPinGuardC
The pin's four ground guards — closed, free-variable-free, level-monomorphic
and resolving — the first four clauses of `divModPinGuard`. -/
def groundGuards (fe : IFEnv) (p : EIdx) : AM Bool := do
if !(← looseBVarsBoundedFast coreWalkFuel 0 p) then pure false
else if ← hasFvarFast coreWalkFuel p then pure false
else if !(← allLevelParamsDefined [] p) then pure false
else constsResolveFFast fe p
/-- con-leche: ConLeche/Kernel/Checker.lean:310-320 divModPinGuard
con-leche: ConLeche/Kernel/DeclCheck.lean:188-195 divModPinGuardF
con-leche: ConLeche/Cached/ParsedC.lean:151-158 divModPinGuardC
One type pin's guard, the body of `divModPinGuard`'s `List.all` closure: the
constant stored, its stored type free of free variables, the pinned type
closed, free of free variables and resolving. -/
def typePinGuard (fe : IFEnv) (n : NIdx) (ty : EIdx) : AM Bool := do
if (fe.find? n).isSome then
if ← hasFvarFast coreWalkFuel (← typePinStored fe n) then pure false
else if !(← looseBVarsBoundedFast coreWalkFuel 0 ty) then pure false
else if ← hasFvarFast coreWalkFuel ty then pure false
else constsResolveFFast fe ty
else pure false
/-- con-leche: ConLeche/Kernel/Checker.lean:310-320 divModPinGuard
con-leche: ConLeche/Kernel/DeclCheck.lean:188-195 divModPinGuardF
con-leche: ConLeche/Cached/ParsedC.lean:151-158 divModPinGuardC
`(divModTypePins ps c).all fun p => …` as a list recursion (DESIGN §3.4
forbids the closure). -/
def typePinsGuard (fe : IFEnv) : List (NIdx × EIdx) → AM Bool
| [] => pure true
| p :: rest => do
if ← typePinGuard fe p.1 p.2 then typePinsGuard fe rest
else pure false
/-- con-leche: ConLeche/Kernel/Checker.lean:310-320 divModPinGuard
con-leche: ConLeche/Kernel/DeclCheck.lean:188-195 divModPinGuardF
con-leche: ConLeche/Cached/ParsedC.lean:151-158 divModPinGuardC
Syntactic guards on one variant's pin (generated; checked once at install
rather than proven about the blob), and on its type pins: each type-pinned
constant stored with a closed type, each pinned type closed over constants
the stream declares. -/
def divModPinGuard (ps : INatOpPinSet) (fe : IFEnv) (c : NIdx) : AM Bool := do
let p ← divModDeclPin ps c
if ← groundGuards fe p then typePinsGuard fe (← divModTypePins ps c)
else pure false
/-- con-leche: ConLeche/Kernel/Checker.lean:322-329 divModCertsGuard
con-leche: ConLeche/Kernel/DeclCheck.lean:197-201 divModCertsGuardF
All of one variant's certificates' syntactic guards at once, `zip`ped with
the statements. Checked *before* the pin comparison; a failure moves on to
the next variant. -/
def divModCertsGuardGo (fe : IFEnv) (c : NIdx) (annVal : EIdx) :
List (List EIdx × EIdx) → List EIdx → AM Bool
| [], _ => pure true
| _, [] => pure true
| (hyps, eqE) :: srest, proof :: prest => do
if ← divModCertGuard fe c annVal hyps eqE proof then
divModCertsGuardGo fe c annVal srest prest
else pure false
/-- con-leche: ConLeche/Kernel/Checker.lean:322-329 divModCertsGuard
con-leche: ConLeche/Kernel/DeclCheck.lean:197-201 divModCertsGuardF
`List.zip` truncates at the shorter list, which is why the helper's two
`[]` clauses are both `true`. -/
def divModCertsGuard (ps : INatOpPinSet) (fe : IFEnv) (c : NIdx)
(annVal : EIdx) : AM Bool := do
divModCertsGuardGo fe c annVal (← divModCertStmts c) (← divModCertProofs ps c)
/-- con-leche: ConLeche/Kernel/Checker.lean:270-293 checkDivModCerts
con-leche: ConLeche/Kernel/DeclCheck.lean:251-265 checkDivModCertsF
Check the pinned certificates of op `c`: per certificate, the vendored proof
(with the op's self-references replaced by the stored value — the
checks run in the *pre-insertion* environment) is applied to free variables
typed by the pinned open statement, its type inferred, and compared against
the pinned characteristic equation. -/
def checkDivModCerts (mode : CheckMode) (fe : IFEnv) (c : NIdx)
(annVal : EIdx) : List (List EIdx × EIdx) → List EIdx → AM Bool
| [], [] => pure true
| (hyps, eqE) :: srest, proof :: prest => do
if ← divModCertGuard fe c annVal hyps eqE proof then do
let p ← substConstAll c annVal coreWalkFuel proof
let hs ← substConst0List c annVal hyps
let appliedA ← divModCertApplied p hs
let tp ← inferTypeCore mode fe checkFuel 4 appliedA
let rhs ← substConst0 c annVal coreWalkFuel eqE
if ← isDefEqCore mode fe checkFuel 4 tp rhs then
checkDivModCerts mode fe c annVal srest prest
else pure false
else pure false
| _, _ => pure false
/-- con-leche: ConLeche/Kernel/Checker.lean:331-339 checkTypePins
con-leche: ConLeche/Kernel/DeclCheck.lean:267-274 checkTypePinsF
con-leche: ConLeche/Cached/ParsedC.lean:160-166 checkTypePinsC
The type pins: each type-pinned constant's stored type against the variant's
pinned one, by definitional equality. -/
def checkTypePins (mode : CheckMode) (fe : IFEnv) : List (NIdx × EIdx) → AM Bool
| [] => pure true
| p :: rest => do
if ← isDefEqCore mode fe checkFuel 0 (← typePinStored fe p.1) p.2 then
checkTypePins mode fe rest
else pure false
/-- con-leche: ConLeche/Kernel/Checker.lean:341-368 checkDivModPinAt
con-leche: ConLeche/Kernel/DeclCheck.lean:276-286 checkDivModPinAtF
The attempt past its type pins: the stored value against the variant's pin
by definitional equality, and on a match the variant's certificates. -/
def checkDivModPinValue (mode : CheckMode) (fe : IFEnv) (c : NIdx)
(value' : EIdx) (ps : INatOpPinSet) : AM Bool := do
let pinA ← divModDeclPin ps c
let okPin ← isDefEqCore mode fe checkFuel 0 value' pinA
if okPin then
checkDivModCerts mode fe c value' (← divModCertStmts c)
(← divModCertProofs ps c)
else pure false
/-- con-leche: ConLeche/Kernel/Checker.lean:341-368 checkDivModPinAt
con-leche: ConLeche/Kernel/DeclCheck.lean:276-286 checkDivModPinAtF
**One pin variant's attempt**: the variant's type pins (`checkTypePins`),
then the value pin and the certificates (`checkDivModPinValue`). `true` =
matched; `false` = a type pin or the pin is not definitionally equal, or a
certificate did not check. Every outcome is a boolean; an error is the
run's. -/
def checkDivModPinAt (mode : CheckMode) (fe : IFEnv) (c : NIdx)
(value' : EIdx) (ps : INatOpPinSet) : AM Bool := do
if ← checkTypePins mode fe (← divModTypePins ps c) then
checkDivModPinValue mode fe c value' ps
else pure false
/-- con-leche: ConLeche/Kernel/Checker.lean:370-374 divModAttemptReason —
what a variant whose guards passed failed on, for the decline message. -/
def divModAttemptReason (ps : INatOpPinSet) : String :=
s!"{ps.toolchain}: a type pin or the pin not definitionally equal, or \
a certificate failed"
/-- con-leche: ConLeche/Kernel/Checker.lean:376-396 checkDivModPinLoop
con-leche: ConLeche/Kernel/DeclCheck.lean:288-303 checkDivModPinLoopF
**The variant loop**: the first variant whose guards pass and whose attempt
succeeds enables the fast path; every other outcome moves on to the next
variant, and when none is left the stream DECLINES with the per-variant
reasons. Every step is a boolean decision: the guards, then the attempt's
`true`/`false` (`checkDivModPinAt`). The two guards short-circuit, as
con-leche's `&&` of two pure `Bool`s does: a variant whose pin guard fails
never walks its certificates. -/
def checkDivModPinLoop (mode : CheckMode) (fe : IFEnv) (c : NIdx)
(value' : EIdx) : List INatOpPinSet → List String → AM Unit
| [], tried => do
fail (.notImplemented s!"unsupported Nat.div/mod spelling (no pin variant \
matched — {String.intercalate "; " tried})")
| ps :: rest, tried => do
if ← divModPinGuard ps fe c then
if ← divModCertsGuard ps fe c value' then
if ← checkDivModPinAt mode fe c value' ps then pure ()
else
checkDivModPinLoop mode fe c value' rest
(tried ++ [divModAttemptReason ps])
else
checkDivModPinLoop mode fe c value' rest
(tried ++ [s!"{ps.toolchain}: pin or certificate ground constants \
absent"])
else
checkDivModPinLoop mode fe c value' rest
(tried ++ [s!"{ps.toolchain}: pin or certificate ground constants \
absent"])
/-- con-leche: ConLeche/Kernel/Checker.lean:398-424 checkDivModPin
con-leche: ConLeche/Kernel/DeclCheck.lean:305-315 checkDivModPinF
The two reads at `fe2`, the environment with the definition:
`divModEnvGuardF fe2 c`, then the stored definition's value. -/
def checkDivModPinAtFe2 (fe2 : IFEnv) (c : NIdx) : AM EIdx := do
if ← divModEnvGuard fe2 c then
match fe2.find? c with
| some (.defnInfo _ value' _) => pure value'
| _ => fail (.internal "Nat.div/mod operation not stored")
else fail (.notImplemented
"unsupported Nat.div/mod environment")
/-- con-leche: ConLeche/Kernel/Checker.lean:398-424 checkDivModPin
con-leche: ConLeche/Kernel/DeclCheck.lean:305-315 checkDivModPinF
The pin-certified operations' gate: the environment guards and the stored
value at `fe2`, then the variant loop at `fe`, the environment without the
definition. The variant list is its parameter (con-leche's task #304).
**Deviation:** the cited `fe2` is `fe.overlay [ci]` (`checkDeclC`'s argument),
passed as `ci`: it is pushed for the reads at `fe2` and popped before the loop
(`IFEnv.popTemp`, the port's overlay), and the index comes back, as the Rust's
`&mut` does. -/
def checkDivModPin (mode : CheckMode) (pins : List INatOpPinSet)
(fe : IFEnv) (ci : IConstantInfo) (c : NIdx) : AM IFEnv := do
let prev := fe.idx[c]?
let fe2 := fe.push ci
let value' ← checkDivModPinAtFe2 fe2 c
let fe := (fe2.popTemp c prev).setFlags fe.natLit fe.strLit
checkDivModPinLoop mode fe c value' pins []
pure fe
/-- con-leche: ConLeche/Kernel/Checker.lean:426-461 checkReducePin
con-leche: ConLeche/Kernel/DeclCheck.lean:317-336 checkReducePinF
con-leche: ConLeche/Cached/ParsedC.lean:227-245 checkReducePinC
The body at `fe`, the environment without the opaque: the element guard, the
pin's own syntactic guards, the definitional comparison of the witness
against the pin, and the identity certificate. -/
def checkReducePinPre (mode : CheckMode) (fe : IFEnv) (c : NIdx)
(value : EIdx) : AM Unit := do
if ← reduceElemOk fe c then
if ← reducePinGuard fe c then do
let valA := value
let pinA ← reduceDeclPin c
let okPin ← isDefEqCore mode fe checkFuel 0 valA pinA
if okPin then do
let x ← reduceCertVar c
let ax ← internE (.app valA x)
let ok ← isDefEqCore mode fe checkFuel 1 ax x
if ok then pure ()
else fail (.internal
"pinned compiler-trust opaque is not the identity")
else fail (.notImplemented
"unsupported compiler-trust opaque spelling")
else fail (.notImplemented
"unsupported compiler-trust opaque spelling (pin ground constants absent)")
else fail (.notImplemented
"unsupported compiler-trust opaque declaration")
/-- con-leche: ConLeche/Kernel/Checker.lean:426-461 checkReducePin
con-leche: ConLeche/Kernel/DeclCheck.lean:317-336 checkReducePinF
con-leche: ConLeche/Cached/ParsedC.lean:227-245 checkReducePinC
The `Lean.reduceNat`/`Lean.reduceBool` gate, after the ordinary opaque check:
the stored constant carries the pinned type (at `fe2`, the environment with
it), then `checkReducePinPre` at `fe`. The two guards short-circuit, as the
Rust's do.
**Deviation:** as `checkDivModPin`, `fe2` is `fe.overlay [ci]`, pushed for its
one read and popped. -/
def checkReducePin (mode : CheckMode) (fe : IFEnv) (ci : IConstantInfo) (c : NIdx)
(value : EIdx) : AM IFEnv := do
let prev := fe.idx[c]?
let fe2 := fe.push ci
let stored ← reduceStoredOk fe2 c
let fe := (fe2.popTemp c prev).setFlags fe.natLit fe.strLit
if stored then do
checkReducePinPre mode fe c value
pure fe
else fail (.notImplemented
"unsupported compiler-trust opaque declaration")
/-! ## The structural-`Nat` recurrence certification -/
/-- con-leche: ConLeche/Kernel/Checker.lean:103-111 certifyNatEqs — certify a
list of recurrence equations by definitional equality (at depth 2: the
equations' variables are `fvar 0`/`fvar 1`). -/
def certifyNatEqs (mode : CheckMode) (fe : IFEnv) :
List (EIdx × EIdx) → AM Bool
| [] => pure true
| eq :: rest => do
if ← isDefEqCore mode fe checkFuel 2 eq.1 eq.2 then
certifyNatEqs mode fe rest
else pure false
/-- con-leche: none — `(natOpEquations 0 c).map fun eq => (substConst0 c v
eq.1, substConst0 c v eq.2)`, as an explicit list recursion. -/
def substConst0Pairs (n : NIdx) (r : EIdx) : List (EIdx × EIdx) → AM (List (EIdx × EIdx))
| [] => pure []
| (a, b) :: rest => do
let a' ← substConst0 n r coreWalkFuel a
let b' ← substConst0 n r coreWalkFuel b
let rest' ← substConst0Pairs n r rest
pure ((a', b') :: rest')
/-! ## The three value kinds' full checks -/
/-- con-leche: ConLeche/Kernel/Checker.lean:30-47 checkDefnVal
con-leche: ConLeche/Kernel/Checker.lean:49-78 checkThmVal
con-leche: ConLeche/Kernel/Checker.lean:80-102 checkOpaqueVal
con-leche: ConLeche/Cached/ParsedC.lean:37-54 checkDefnValCI
The value's syntactic guards the three value checks spell alike (no loose
bound variable, no free variable, declared universe parameters, resolving
constants), spelled once here. -/
def valueGuards (fe : IFEnv) (cv : IConstantVal) (value : EIdx) : AM Unit := do
unless ← looseBVarsBoundedFast coreWalkFuel 0 value do
fail (.invalid "loose bound variable in value")
if ← hasFvarFast coreWalkFuel value then
fail (.invalid "unexpected free variable in value")
unless ← allLevelParamsDefined cv.levelParams value do
fail (.invalid
"undeclared universe parameter in value")
unless ← constsResolveFFast fe value do
fail (← unresolvedConstsErrorL fe.strLit s!"value of {← readName cv.name}" value)
/-- con-leche: ConLeche/Kernel/Checker.lean:30-47 checkDefnVal
con-leche: ConLeche/Cached/ParsedC.lean:37-54 checkDefnValCI
Check a `def` declaration's value against its checked constant, returning the
record to install, unpushed (the cached twin's shape: the caller certifies the
pinned operations against the environment with and without it, then pushes).
The reducibility hint is stored untouched: it steers only the lazy delta
unfolding order, never a verdict. -/
def checkDefnVal (mode : CheckMode) (fe : IFEnv) (cv : IConstantVal)
(value : EIdx) (hint : ReducibilityHint) : AM IConstantInfo := do
valueGuards fe cv value
let vtype ← inferTypeCore mode fe checkFuel 0 value
unless ← isDefEqCore mode fe checkFuel 0 vtype cv.type do
fail (.invalid "type mismatch in definition")
pure (.defnInfo cv value hint)
/-- con-leche: ConLeche/Kernel/Checker.lean:49-78 checkThmVal
con-leche: ConLeche/Cached/ParsedC.lean:56-76 checkThmValCI
Check a `theorem` declaration's value against its checked constant (whose
type must additionally be a proposition), returning the record to install,
unpushed. **A theorem is stored by its statement only**: the checked value is
a realizability witness, checked and then discarded. The Rust splits off the
witness half (`check_thm_val_witness`). -/
def checkThmVal (mode : CheckMode) (fe : IFEnv) (cv : IConstantVal)
(value : EIdx) : AM IConstantInfo := do
let stype ← inferTypeCore mode fe checkFuel 0 cv.type
let u ← ensureSortCore mode fe checkFuel 0 stype
let z ← zeroLevel
unless ← liftFueled "level comparison" (← lvlEq? u z) do
fail (.invalid "type of theorem is not a proposition")
valueGuards fe cv value
let vtype ← inferTypeCore mode fe checkFuel 0 value
unless ← isDefEqCore mode fe checkFuel 0 vtype cv.type do
fail (.invalid "type mismatch in theorem")
pure (.thmInfo cv)
/-- con-leche: ConLeche/Kernel/Checker.lean:80-102 checkOpaqueVal
con-leche: ConLeche/Cached/ParsedC.lean:78-94 checkOpaqueValCI
Check an `opaque` declaration's value against its checked constant: exactly
the theorem check without the is-a-proposition requirement, returning the
record to install, unpushed. It is an `axiomInfo` — the checked value is a
realizability witness, consumed by the model extension and then discarded. -/
def checkOpaqueVal (mode : CheckMode) (fe : IFEnv) (cv : IConstantVal)
(value : EIdx) : AM IConstantInfo := do
valueGuards fe cv value
let vtype ← inferTypeCore mode fe checkFuel 0 value
unless ← isDefEqCore mode fe checkFuel 0 vtype cv.type do
fail (.invalid "type mismatch in opaque")
pure (.axiomInfo cv)
/-! ## The quotient records -/
/-- con-leche: ConLeche/Kernel/Checker.lean:24-28 installBasisDecl
con-leche: ConLeche/Kernel/DeclCheck.lean:245-249 installBasisDeclF
Install one pinned basis declaration (duplicate-checked), returning the
pushed index. -/
def installBasisDecl (fe : IFEnv) (ci : IConstantInfo) : AM IFEnv := do
unless (fe.find? ci.name).isNone do
fail (.invalid "duplicate declaration")
pure (fe.push ci)
/-- con-leche: ConLeche/Kernel/Checker.lean:463-474 checkQuotRecord
con-leche: ConLeche/Cached/ParsedC.lean:96-104 checkQuotRecordC
**A quotient record's check and install**: compared with its slot's pin, the
slots it needs stored, and installed at its own names. A record that does not
match is a quotient this checker positively does not support. -/
def checkQuotRecord (fe : IFEnv) (k : QuotKind) (cv : IConstantVal) : AM IFEnv := do
unless ← quotPinHit k cv do
fail (.notImplemented (match k with
| .sound => "quotient soundness axiom mismatch"
| _ => "quotient declaration mismatch"))
unless ← quotPredsOk fe k do
fail (.notImplemented "quotient declaration before what it needs")
installBasisDecl fe (← quotAt k cv)
end ConRon.Arena