chars
int64
40
24k
content_type
stringclasses
9 values
id
stringlengths
13
64
source
stringclasses
5 values
subset
stringclasses
31 values
text
stringlengths
40
24k
267
leanexplore_declaration
leanexplore:386806
leanexplore_sqlite
leanexplore_declarations
/-- capJump is idempotent: capping twice is the same as capping once. -/ @[simp] theorem capJump_idempotent (len : ℕ) (instr : Instr) : (instr.capJump len).capJump len = instr.capJump len := by cases instr with | Z | S | T => rfl | J m n q => simp [capJump]
348
leanexplore_declaration
leanexplore:386807
leanexplore_sqlite
leanexplore_declarations
/-- Any instruction in a program has maxRegister at most the program's maxRegister. -/ theorem mem_maxRegister {p : Program} {instr : Instr} (h : instr ∈ p) : instr.maxRegister ≤ p.maxRegister := by unfold maxRegister rw [List.foldl_map.symm, ←List.foldr_eq_foldl] exact List.le_max_of_le' 0 (List.mem_map.mpr ...
519
leanexplore_declaration
leanexplore:386808
leanexplore_sqlite
leanexplore_declarations
/-- A program `p` computes a partial function `f : (Fin n → ℕ) → Part ℕ` if for any input, `eval p inputs = f inputs` as partial values. This captures both: - The program halts iff the function is defined on that input - When both are defined, the program's output equals the function's value Note: Inputs are provided ...
210
leanexplore_declaration
leanexplore:386809
leanexplore_sqlite
leanexplore_declarations
/-- A partial function `f : (Fin n → ℕ) → Part ℕ` is URM-computable if there exists a URM program that computes it. -/ def Computable (n : ℕ) (f : (Fin n → ℕ) → Part ℕ) : Prop := ∃ p : Program, Computes n p f
443
leanexplore_declaration
leanexplore:386810
leanexplore_sqlite
leanexplore_declarations
/-- URM instructions. - `Z n`: Set register n to zero - `S n`: Increment register n by one - `T m n`: Transfer (copy) the contents of register m to register n - `J m n q`: If registers m and n have equal contents, jump to instruction q; otherwise proceed to the next instruction -/ @[grind] inductive Instr ...
162
leanexplore_declaration
leanexplore:386819
leanexplore_sqlite
leanexplore_declarations
/-- The registers read by an instruction. -/ @[scoped grind =] def readsFrom : Instr → Finset ℕ | Z _ => ∅ | S n => {n} | T m _ => {m} | J m n _ => {m, n}
183
leanexplore_declaration
leanexplore:386820
leanexplore_sqlite
leanexplore_declarations
/-- The register written to by an instruction, if any. -/ @[scoped grind =] def writesTo : Instr → Option ℕ | Z n => some n | S n => some n | T _ n => some n | J _ _ _ => none
179
leanexplore_declaration
leanexplore:386821
leanexplore_sqlite
leanexplore_declarations
/-- The maximum register index referenced by an instruction. -/ @[scoped grind =] def maxRegister : Instr → ℕ | Z n => n | S n => n | T m n => max m n | J m n _ => max m n
277
leanexplore_declaration
leanexplore:386822
leanexplore_sqlite
leanexplore_declarations
/-- Shift all jump targets in an instruction by `offset`. Used when concatenating programs to maintain correct jump destinations. -/ @[scoped grind =] def shiftJumps (offset : ℕ) : Instr → Instr | Z n => Z n | S n => S n | T m n => T m n | J m n q => J m n (q + offset)
327
leanexplore_declaration
leanexplore:386823
leanexplore_sqlite
leanexplore_declarations
/-- Shift all register references in an instruction by `offset`. Used to isolate register usage when composing programs. -/ @[scoped grind =] def shiftRegisters (offset : ℕ) : Instr → Instr | Z n => Z (n + offset) | S n => S (n + offset) | T m n => T (m + offset) (n + offset) | J m n q => J (m + offset) (n + of...
230
leanexplore_declaration
leanexplore:386824
leanexplore_sqlite
leanexplore_declarations
/-- Register contents: maps register indices to natural number contents. Uses the functional representation `ℕ → ℕ` for efficiency with rewrites, following the advice from the `grind` tactic documentation. -/ abbrev Regs := ℕ → ℕ
104
leanexplore_declaration
leanexplore:386825
leanexplore_sqlite
leanexplore_declarations
/-- The zero registers where all registers contain 0. -/ @[scoped grind =] def zero : Regs := fun _ => 0
96
leanexplore_declaration
leanexplore:386826
leanexplore_sqlite
leanexplore_declarations
/-- Read the contents of register n. -/ @[scoped grind =] def read (σ : Regs) (n : ℕ) : ℕ := σ n
122
leanexplore_declaration
leanexplore:386827
leanexplore_sqlite
leanexplore_declarations
/-- Write value v to register n. -/ @[scoped grind =] def write (σ : Regs) (n : ℕ) (v : ℕ) : Regs := Function.update σ n v
208
leanexplore_declaration
leanexplore:386828
leanexplore_sqlite
leanexplore_declarations
/-- Initialize registers with input values in registers 0, 1, ..., k-1. Registers beyond the inputs are initialized to 0. -/ @[scoped grind =] def ofInputs (inputs : List ℕ) : Regs := fun n => inputs.getD n 0
89
leanexplore_declaration
leanexplore:386829
leanexplore_sqlite
leanexplore_declarations
/-- Extract output from register 0. -/ @[scoped grind =] def output (σ : Regs) : ℕ := σ 0
76
leanexplore_declaration
leanexplore:386830
leanexplore_sqlite
leanexplore_declarations
/-- A URM program is a list of instructions. -/ abbrev Program := List Instr
191
leanexplore_declaration
leanexplore:386831
leanexplore_sqlite
leanexplore_declarations
/-- The maximum register index referenced by any instruction in the program. -/ @[scoped grind =] def maxRegister (p : Program) : ℕ := p.foldl (fun acc instr => max acc instr.maxRegister) 0
276
leanexplore_declaration
leanexplore:386832
leanexplore_sqlite
leanexplore_declarations
/-- Shift all jump targets in a program by `offset`. Used when concatenating programs: the second program's jumps must be adjusted by the length of the first program. -/ @[scoped grind =] def shiftJumps (p : Program) (offset : ℕ) : Program := p.map (Instr.shiftJumps offset)
233
leanexplore_declaration
leanexplore:386833
leanexplore_sqlite
leanexplore_declarations
/-- Shift all register references in a program by `offset`. Used to isolate register usage when composing programs. -/ @[scoped grind =] def shiftRegisters (p : Program) (offset : ℕ) : Program := p.map (Instr.shiftRegisters offset)
183
leanexplore_declaration
leanexplore:386834
leanexplore_sqlite
leanexplore_declarations
/-- Machine state: program counter (0-indexed) and register contents. -/ structure State where /-- Program counter (0-indexed). -/ pc : ℕ /-- Register contents. -/ regs : Regs
216
leanexplore_declaration
leanexplore:386835
leanexplore_sqlite
leanexplore_declarations
/-- Initial state for a program with given inputs. The program counter starts at 0, and inputs are loaded into registers 0, 1, .... -/ @[scoped grind =] def init (inputs : List ℕ) : State := ⟨0, Regs.ofInputs inputs⟩
166
leanexplore_declaration
leanexplore:386836
leanexplore_sqlite
leanexplore_declarations
/-- A state is halted if the program counter is at or beyond the program length. -/ @[scoped grind =] def isHalted (s : State) (p : Program) : Prop := p.length ≤ s.pc
112
leanexplore_declaration
leanexplore:386837
leanexplore_sqlite
leanexplore_declarations
instance (s : State) (p : Program) : Decidable (s.isHalted p) := inferInstanceAs (Decidable (p.length ≤ s.pc))
66
leanexplore_declaration
leanexplore:386839
leanexplore_sqlite
leanexplore_declarations
instance : Repr State where reprPrec s _ := s!"State(pc={s.pc})"
1,166
leanexplore_declaration
leanexplore:386840
leanexplore_sqlite
leanexplore_declarations
/-- Single-step execution relation for URMs. Each constructor corresponds to one of the four instruction types: - `zero`: Execute `Z n` (set register n to 0) - `succ`: Execute `S n` (increment register n) - `transfer`: Execute `T m n` (copy register m to register n) - `jump_eq`: Execute `J m n q` when registers m and ...
110
leanexplore_declaration
leanexplore:386841
leanexplore_sqlite
leanexplore_declarations
| zero {s : State} {n : ℕ} (h : p[s.pc]? = some (Instr.Z n)) : Step s ⟨s.pc + 1, s.regs.write n 0⟩
128
leanexplore_declaration
leanexplore:386842
leanexplore_sqlite
leanexplore_declarations
| succ {s : State} {n : ℕ} (h : p[s.pc]? = some (Instr.S n)) : Step s ⟨s.pc + 1, s.regs.write n (s.regs.read n + 1)⟩
132
leanexplore_declaration
leanexplore:386843
leanexplore_sqlite
leanexplore_declarations
| transfer {s : State} {m n : ℕ} (h : p[s.pc]? = some (Instr.T m n)) : Step s ⟨s.pc + 1, s.regs.write n (s.regs.read m)⟩
148
leanexplore_declaration
leanexplore:386844
leanexplore_sqlite
leanexplore_declarations
| jump_eq {s : State} {m n q : ℕ} (h : p[s.pc]? = some (Instr.J m n q)) (heq : s.regs.read m = s.regs.read n) : Step s ⟨q, s.regs⟩
155
leanexplore_declaration
leanexplore:386845
leanexplore_sqlite
leanexplore_declarations
| jump_ne {s : State} {m n q : ℕ} (h : p[s.pc]? = some (Instr.J m n q)) (hne : s.regs.read m ≠ s.regs.read n) : Step s ⟨s.pc + 1, s.regs⟩
142
leanexplore_declaration
leanexplore:386846
leanexplore_sqlite
leanexplore_declarations
/-- Multi-step execution: the reflexive-transitive closure of `Step`. -/ abbrev Steps : State → State → Prop := Relation.ReflTransGen (Step p)
167
leanexplore_declaration
leanexplore:386847
leanexplore_sqlite
leanexplore_declarations
/-- The step relation is deterministic: each state has at most one successor. -/ theorem deterministic : Relator.RightUnique (Step p) := by grind [Relator.RightUnique]
172
leanexplore_declaration
leanexplore:386848
leanexplore_sqlite
leanexplore_declarations
/-- A halted state has no successor in the step relation. -/ theorem no_step_of_halted {s s' : State} (hhalted : s.isHalted p) : ¬Step p s s' := by grind [State.isHalted]
665
leanexplore_declaration
leanexplore:386849
leanexplore_sqlite
leanexplore_declarations
/-- A single step preserves registers not written to by the current instruction. This is a fundamental property of URM execution: each instruction modifies at most one register (Z, S, T write to one register; J writes to none). -/ theorem preserves_register {s s' : State} {r : ℕ} (hstep : Step p s s') (hr : ∀ ...
1,010
leanexplore_declaration
leanexplore:386850
leanexplore_sqlite
leanexplore_declarations
/-- A state is halted iff it is normal (has no successor) in the reduction system. -/ theorem isHalted_iff_normal {p : Program} {s : State} : s.isHalted p ↔ Relation.Normal (Step p) s := by constructor · intro hhalted ⟨s', hstep⟩ exact Step.no_step_of_halted hhalted hstep · intro hnormal -- If not hal...
179
leanexplore_declaration
leanexplore:386851
leanexplore_sqlite
leanexplore_declarations
/-- The step relation is confluent. -/ theorem step_confluent (p : Program) : Relation.Confluent (Step p) := by apply Relation.RightUnique.toConfluent exact Step.deterministic
395
leanexplore_declaration
leanexplore:386852
leanexplore_sqlite
leanexplore_declarations
/-- Multi-step execution preserves registers not written by any executed instruction. -/ theorem preserves_register {s s' : State} {r : ℕ} (hsteps : Steps p s s') (hr : ∀ instr, instr ∈ p → instr.writesTo ≠ some r) : s'.regs.read r = s.regs.read r := by induction hsteps using Relation.ReflTransGen.head_in...
651
leanexplore_declaration
leanexplore:386853
leanexplore_sqlite
leanexplore_declarations
/-- If two halted states are reachable from the same start, they are equal. This follows from confluence: since `Step p` is confluent and both `s₁` and `s₂` are normal forms reachable from `init`, they must be equal. -/ theorem eq_of_halts {init s₁ s₂ : State} (h1 : Steps p init s₁) (hh1 : s₁.isHalted p) (h2 :...
264
leanexplore_declaration
leanexplore:386854
leanexplore_sqlite
leanexplore_declarations
/-- A program halts on given inputs if execution reaches a halted state. This is equivalent to `(Step p).Normalizable (State.init inputs)` — see `halts_iff_normalizable`. -/ def Halts (inputs : List ℕ) : Prop := ∃ s, Steps p (State.init inputs) s ∧ s.isHalted p
252
leanexplore_declaration
leanexplore:386855
leanexplore_sqlite
leanexplore_declarations
/-- Halting is equivalent to normalizability in the reduction system. -/ theorem halts_iff_normalizable {p : Program} {inputs : List ℕ} : Halts p inputs ↔ Relation.Normalizable (Step p) (State.init inputs) := by grind [Halts, isHalted_iff_normal]
119
leanexplore_declaration
leanexplore:386856
leanexplore_sqlite
leanexplore_declarations
/-- A program diverges on given inputs if it does not halt. -/ def Diverges (inputs : List ℕ) : Prop := ¬Halts p inputs
208
leanexplore_declaration
leanexplore:386857
leanexplore_sqlite
leanexplore_declarations
/-- A program halts on given inputs with a specific result in R[0]. -/ def HaltsWithResult (inputs : List ℕ) (result : ℕ) : Prop := ∃ s, Steps p (State.init inputs) s ∧ s.isHalted p ∧ s.regs.output = result
133
leanexplore_declaration
leanexplore:386858
leanexplore_sqlite
leanexplore_declarations
/-- Notation for halting: `p ↓ inputs` means program `p` halts on `inputs`. -/ scoped notation:50 p " ↓ " inputs:51 => Halts p inputs
142
leanexplore_declaration
leanexplore:386859
leanexplore_sqlite
leanexplore_declarations
/-- Notation for divergence: `p ↑ inputs` means program `p` diverges on `inputs`. -/ scoped notation:50 p " ↑ " inputs:51 => Diverges p inputs
209
leanexplore_declaration
leanexplore:386860
leanexplore_sqlite
leanexplore_declarations
/-- Notation for halting with result: `p ↓ inputs ≫ result` means program `p` halts on `inputs` with `result` in R[0]. -/ scoped notation:50 p " ↓ " inputs:51 " ≫ " result:51 => HaltsWithResult p inputs result
201
leanexplore_declaration
leanexplore:386861
leanexplore_sqlite
leanexplore_declarations
/-- If a program halts with a result, it halts. -/ theorem toHalts {inputs : List ℕ} {result : ℕ} (h : p ↓ inputs ≫ result) : p ↓ inputs := let ⟨s, hsteps, hhalted, _⟩ := h ⟨s, hsteps, hhalted⟩
161
leanexplore_declaration
leanexplore:386862
leanexplore_sqlite
leanexplore_declarations
/-- Evaluation returning the full halting state. -/ noncomputable def evalState (inputs : List ℕ) : Part State := ⟨Halts p inputs, fun h => Classical.choose h⟩
268
leanexplore_declaration
leanexplore:386863
leanexplore_sqlite
leanexplore_declarations
/-- Specification: the state from evalState satisfies Steps and isHalted. -/ theorem evalState_spec {inputs : List ℕ} (h : (evalState p inputs).Dom) : let s := (evalState p inputs).get h Steps p (State.init inputs) s ∧ s.isHalted p := Classical.choose_spec h
307
leanexplore_declaration
leanexplore:386864
leanexplore_sqlite
leanexplore_declarations
/-- If a program halts, it halts with the output of the final state. -/ theorem toHaltsWithResult {inputs : List ℕ} (h : p ↓ inputs) : p ↓ inputs ≫ ((evalState p inputs).get h).regs.output := let s := (evalState p inputs).get h let ⟨hsteps, hhalted⟩ := evalState_spec p h ⟨s, hsteps, hhalted, rfl⟩
216
leanexplore_declaration
leanexplore:386865
leanexplore_sqlite
leanexplore_declarations
/-- Evaluation as a partial function using `Part`. Defined when the program halts, returning the value in register 0. -/ noncomputable def eval (inputs : List ℕ) : Part ℕ := (evalState p inputs).map (·.regs.output)
786
leanexplore_declaration
leanexplore:386866
leanexplore_sqlite
leanexplore_declarations
/-- A program halts with result `a` iff evaluation returns `Part.some a`. -/ theorem haltsWithResult_iff_eval {inputs : List ℕ} {result : ℕ} : p ↓ inputs ≫ result ↔ eval p inputs = Part.some result := by rw [Part.eq_some_iff, eval, Part.mem_map_iff] constructor · intro ⟨s, hsteps, hhalted, hresult⟩ have h...
176
leanexplore_declaration
leanexplore:386867
leanexplore_sqlite
leanexplore_declarations
/-- Two programs are equivalent if they produce the same result for all inputs. -/ def ProgramEquiv (p q : Program) : Prop := ∀ inputs : List ℕ, eval p inputs = eval q inputs
248
leanexplore_declaration
leanexplore:386868
leanexplore_sqlite
leanexplore_declarations
/-- Program equivalence is an equivalence relation. -/ theorem ProgramEquiv.equivalence : Equivalence ProgramEquiv where refl := fun _ _ => rfl symm := fun h inputs => (h inputs).symm trans := fun h₁ h₂ inputs => (h₁ inputs).trans (h₂ inputs)
127
leanexplore_declaration
leanexplore:386869
leanexplore_sqlite
leanexplore_declarations
/-- Setoid instance for programs, enabling the ≈ notation. -/ instance : Setoid Program := Setoid.mk _ ProgramEquiv.equivalence
267
leanexplore_declaration
leanexplore:386870
leanexplore_sqlite
leanexplore_declarations
/-- A program is in standard form if all jump targets are bounded by the program length. Jumps can target any instruction (0..length-1) or the "virtual halt" position (length). -/ def IsStandardForm (p : Program) : Prop := ∀ instr ∈ p, instr.JumpsBoundedBy p.length
129
leanexplore_declaration
leanexplore:386871
leanexplore_sqlite
leanexplore_declarations
instance (p : Program) : Decidable p.IsStandardForm := inferInstanceAs (Decidable (∀ instr ∈ p, instr.JumpsBoundedBy p.length))
171
leanexplore_declaration
leanexplore:386872
leanexplore_sqlite
leanexplore_declarations
/-- Convert a program to standard form by capping all jump targets at the program length. -/ def toStandardForm (p : Program) : Program := p.map (Instr.capJump p.length)
182
leanexplore_declaration
leanexplore:386873
leanexplore_sqlite
leanexplore_declarations
/-- toStandardForm preserves program length. -/ @[simp, scoped grind =] theorem toStandardForm_length (p : Program) : p.toStandardForm.length = p.length := by simp [toStandardForm]
335
leanexplore_declaration
leanexplore:386874
leanexplore_sqlite
leanexplore_declarations
/-- toStandardForm produces a standard form program. -/ theorem toStandardForm_isStandardForm (p : Program) : p.toStandardForm.IsStandardForm := by unfold IsStandardForm toStandardForm rw [List.length_map] intro instr hinstr obtain ⟨orig, _, rfl⟩ := List.mem_map.mp hinstr exact Instr.JumpsBoundedBy.capJum...
256
leanexplore_declaration
leanexplore:386875
leanexplore_sqlite
leanexplore_declarations
/-- Accessing an instruction in toStandardForm gives the capJump'd instruction. -/ theorem getElem?_toStandardForm (p : Program) (i : ℕ) : p.toStandardForm[i]? = (p[i]?).map (Instr.capJump p.length) := by simp only [toStandardForm, List.getElem?_map]
331
leanexplore_declaration
leanexplore:386876
leanexplore_sqlite
leanexplore_declarations
/-- toStandardForm is idempotent: applying it twice equals applying it once. -/ @[simp] theorem toStandardForm_idempotent (p : Program) : p.toStandardForm.toStandardForm = p.toStandardForm := by simp only [toStandardForm, List.length_map, List.map_map] congr 1 funext instr exact Instr.capJump_idempotent p.l...
245
leanexplore_declaration
leanexplore:386877
leanexplore_sqlite
leanexplore_declarations
/-- Straight-line programs are in standard form. -/ theorem straight_line_IsStandardForm {p : Program} (hsl : p.IsStraightLine) : p.IsStandardForm := by intro instr hinstr exact Instr.jumpsBoundedBy_of_nonJump (hsl instr hinstr) p.length
1,471
leanexplore_declaration
leanexplore:386878
leanexplore_sqlite
leanexplore_declarations
/-- Forward step correspondence: if p steps from s to s', then either: (1) p.toStandardForm steps from s to s' (same step), or (2) s' is halted in p, and p.toStandardForm steps to a state that is also halted with the same registers (this only happens for jumps with unbounded targets). -/ theorem Step.to...
861
leanexplore_declaration
leanexplore:386879
leanexplore_sqlite
leanexplore_declarations
/-- Forward halting: if p reaches a halted state, p.toStandardForm reaches a halted state with the same registers. -/ theorem Steps.toStandardForm_halts {p : Program} {s s' : State} (hsteps : Steps p s s') (hhalted : s'.isHalted p) : ∃ s₂, Steps p.toStandardForm s s₂ ∧ s₂.isHalted p.toStandardForm ∧ s...
303
leanexplore_declaration
leanexplore:386880
leanexplore_sqlite
leanexplore_declarations
/-- Forward halting theorem. -/ theorem Halts.toStandardForm {p : Program} {inputs : List ℕ} (h : Halts p inputs) : Halts p.toStandardForm inputs := by obtain ⟨s, hsteps, hhalted⟩ := h obtain ⟨s₂, hsteps₂, hhalted₂, _⟩ := Steps.toStandardForm_halts hsteps hhalted exact ⟨s₂, hsteps₂, hhalted₂⟩
810
leanexplore_declaration
leanexplore:386881
leanexplore_sqlite
leanexplore_declarations
/-- Reverse step correspondence: if p.toStandardForm steps from s to s', then either: (1) p steps from s to s' (same step), or (2) s' is halted in p.toStandardForm, and p steps to a state that is also halted with the same registers (this only happens for jumps with unbounded targets). -/ theorem Step.fr...
1,018
leanexplore_declaration
leanexplore:386882
leanexplore_sqlite
leanexplore_declarations
/-- Reverse halting: if p.toStandardForm reaches a halted state, p reaches a halted state with the same registers. -/ theorem Steps.from_toStandardForm_halts {p : Program} {s s' : State} (hsteps : Steps p.toStandardForm s s') (hhalted : s'.isHalted p.toStandardForm) : ∃ s₂, Steps p s s₂ ∧ s₂.isHalted p ∧ s'...
311
leanexplore_declaration
leanexplore:386883
leanexplore_sqlite
leanexplore_declarations
/-- Reverse halting theorem. -/ theorem Halts.of_toStandardForm {p : Program} {inputs : List ℕ} (h : Halts p.toStandardForm inputs) : Halts p inputs := by obtain ⟨s, hsteps, hhalted⟩ := h obtain ⟨s₂, hsteps₂, hhalted₂, _⟩ := Steps.from_toStandardForm_halts hsteps hhalted exact ⟨s₂, hsteps₂, hhalted₂⟩
238
leanexplore_declaration
leanexplore:386884
leanexplore_sqlite
leanexplore_declarations
/-- Halting equivalence: original halts iff standard form halts. -/ theorem Halts.toStandardForm_iff {p : Program} {inputs : List ℕ} : Halts p inputs ↔ Halts p.toStandardForm inputs := ⟨Halts.toStandardForm, Halts.of_toStandardForm⟩
601
leanexplore_declaration
leanexplore:386885
leanexplore_sqlite
leanexplore_declarations
/-- Registers preservation: both reach states with the same registers. -/ theorem evalState_toStandardForm_regs {p : Program} {inputs : List ℕ} (hp : (evalState p inputs).Dom) (hq : (evalState p.toStandardForm inputs).Dom) : ((evalState p inputs).get hp).regs = ((evalState p.toStandardForm inputs).get hq)...
417
leanexplore_declaration
leanexplore:386886
leanexplore_sqlite
leanexplore_declarations
/-- eval equality: both programs produce the same partial result. -/ theorem eval_toStandardForm {p : Program} {inputs : List ℕ} : eval p inputs = eval p.toStandardForm inputs := by simp only [eval] apply Part.ext' · simp only [Part.map_Dom] exact Halts.toStandardForm_iff · intro hp hq simp only [Pa...
157
leanexplore_declaration
leanexplore:386887
leanexplore_sqlite
leanexplore_declarations
/-- A program is equivalent to its standard form. -/ theorem toStandardForm_equiv (p : Program) : p.toStandardForm ≈ p := fun _ => eval_toStandardForm.symm
144
leanexplore_declaration
leanexplore:386888
leanexplore_sqlite
leanexplore_declarations
/-- A program is "straight-line" if it contains no jump instructions. -/ def Program.IsStraightLine (p : Program) : Prop := ∀ i ∈ p, ¬i.IsJump
105
leanexplore_declaration
leanexplore:386889
leanexplore_sqlite
leanexplore_declarations
instance (p : Program) : Decidable p.IsStraightLine := inferInstanceAs (Decidable (∀ i ∈ p, ¬i.IsJump))
298
leanexplore_declaration
leanexplore:386890
leanexplore_sqlite
leanexplore_declarations
/-- Append preserves straight-line property. -/ theorem Program.IsStraightLine.append {p q : Program} (hp : p.IsStraightLine) (hq : q.IsStraightLine) : (p ++ q).IsStraightLine := by intro i hi simp only [List.mem_append] at hi rcases hi with hi | hi <;> [exact hp i hi; exact hq i hi]
337
leanexplore_declaration
leanexplore:386891
leanexplore_sqlite
leanexplore_declarations
/-- Cons of non-jumping instruction preserves straight-line. -/ theorem Program.IsStraightLine.cons {instr : Instr} {p : Program} (hinstr : ¬instr.IsJump) (hp : p.IsStraightLine) : Program.IsStraightLine (instr :: p) := by intro i hi simp only [List.mem_cons] at hi rcases hi with rfl | hi <;> [exact hinst...
245
leanexplore_declaration
leanexplore:386892
leanexplore_sqlite
leanexplore_declarations
/-- Singleton non-jumping instruction is straight-line. -/ theorem Program.IsStraightLine.singleton {instr : Instr} (h : ¬instr.IsJump) : Program.IsStraightLine [instr] := by intro i hi simp only [List.mem_singleton] at hi exact hi ▸ h
557
leanexplore_declaration
leanexplore:386893
leanexplore_sqlite
leanexplore_declarations
/-- A non-jumping instruction produces a step that increments PC by 1. -/ theorem Step.of_nonJump {p : Program} {s : State} (hlt : s.pc < p.length) (hnonjump : ¬(p[s.pc]'hlt).IsJump) : ∃ s', Step p s s' ∧ s'.pc = s.pc + 1 := by cases hp : (p[s.pc]'hlt) with | Z n => use {pc := s.pc + 1, regs := s.regs.w...
820
leanexplore_declaration
leanexplore:386894
leanexplore_sqlite
leanexplore_declarations
/-- Straight-line programs halt from any starting registers, not just State.init. Useful for chaining: after running one program, we can run the next straight-line segment from whatever registers we're in. -/ theorem straight_line_halts_from_regs {p : Program} (hsl : p.IsStraightLine) (r : Regs) : ∃ s, Steps p ⟨0, ...
284
leanexplore_declaration
leanexplore:386895
leanexplore_sqlite
leanexplore_declarations
/-- A straight-line program halts on any input. -/ theorem straight_line_halts {p : Program} (hsl : p.IsStraightLine) (inputs : List ℕ) : Halts p inputs := by obtain ⟨s, hsteps, hhalted, _⟩ := straight_line_halts_from_regs hsl (Regs.ofInputs inputs) exact ⟨s, hsteps, hhalted⟩
291
leanexplore_declaration
leanexplore:386896
leanexplore_sqlite
leanexplore_declarations
/-- The halting state for a straight-line program starting from registers r. Wraps Classical.choose to hide it from the API. -/ noncomputable def straightLine_finalState {p : Program} (hsl : p.IsStraightLine) (r : Regs) : State := Classical.choose (straight_line_halts_from_regs hsl r)
361
leanexplore_declaration
leanexplore:386897
leanexplore_sqlite
leanexplore_declarations
/-- Specification: the state from straightLine_finalState satisfies Steps, isHalted, and has pc = p.length. -/ theorem straightLine_finalState_spec {p : Program} (hsl : p.IsStraightLine) (r : Regs) : let s := straightLine_finalState hsl r Steps p ⟨0, r⟩ s ∧ s.isHalted p ∧ s.pc = p.length := Classical.choose_s...
235
leanexplore_declaration
leanexplore:386898
leanexplore_sqlite
leanexplore_declarations
/-- The final registers after running a straight-line program from given starting registers. -/ noncomputable def straightLine_finalRegs {p : Program} (hsl : p.IsStraightLine) (r : Regs) : Regs := (straightLine_finalState hsl r).regs
425
leanexplore_declaration
leanexplore:386899
leanexplore_sqlite
leanexplore_declarations
/-- For a straight-line program, s.regs equals straightLine_finalRegs if halted from r. -/ theorem straightLine_finalRegs_eq_of_halted {p : Program} (hsl : p.IsStraightLine) (r : Regs) (s : State) (hsteps : Steps p ⟨0, r⟩ s) (hhalted : s.isHalted p) : s.regs = straightLine_finalRegs hsl r := Steps.eq_of_halts...
488
leanexplore_declaration
leanexplore:386900
leanexplore_sqlite
leanexplore_declarations
/-- In a straight-line program, we can characterize the state at any intermediate pc. This gives us the state after executing instructions 0..pc-1. -/ theorem straight_line_state_at_pc {p : Program} (hsl : p.IsStraightLine) (r : Regs) (targetPc : ℕ) (htarget : targetPc ≤ p.length) : ∃ s, Steps p ⟨0, r⟩ s ∧ s.pc...
385
leanexplore_declaration
leanexplore:386901
leanexplore_sqlite
leanexplore_declarations
/-- A scheme is perfectly secret iff the ciphertext distribution is independent of the plaintext ([KatzLindell2020], Lemma 2.5). -/ theorem perfectlySecret_iff_ciphertextIndist (scheme : EncScheme M K C) : scheme.PerfectlySecret ↔ scheme.CiphertextIndist := ⟨PerfectSecrecy.ciphertextIndist_of_perfectlySecret sche...
260
leanexplore_declaration
leanexplore:386902
leanexplore_sqlite
leanexplore_declarations
/-- Perfect secrecy requires `|K| ≥ |M|` ([KatzLindell2020], Theorem 2.12). -/ theorem perfectlySecret_keySpace_ge [Finite K] (scheme : EncScheme M K C) (h : scheme.PerfectlySecret) : Nat.card K ≥ Nat.card M := PerfectSecrecy.shannonKeySpace scheme h
165
leanexplore_declaration
leanexplore:386903
leanexplore_sqlite
leanexplore_declarations
/-- The distribution of `Enc_K(m)` when `K ← Gen`. -/ noncomputable def ciphertextDist (scheme : EncScheme M K C) (m : M) : PMF C := do scheme.enc (← scheme.gen) m
211
leanexplore_declaration
leanexplore:386904
leanexplore_sqlite
leanexplore_declarations
/-- Joint distribution of `(M, C)` given a message prior. -/ noncomputable def jointDist (scheme : EncScheme M K C) (msgDist : PMF M) : PMF (M × C) := do let m ← msgDist return (m, ← scheme.ciphertextDist m)
202
leanexplore_declaration
leanexplore:386905
leanexplore_sqlite
leanexplore_declarations
/-- Marginal ciphertext distribution given a message prior. -/ noncomputable def marginalCiphertextDist (scheme : EncScheme M K C) (msgDist : PMF M) : PMF C := do scheme.ciphertextDist (← msgDist)
402
leanexplore_declaration
leanexplore:386906
leanexplore_sqlite
leanexplore_declarations
/-- The posterior message distribution `Pr[M | C = c]` as a probability distribution, given a message prior and a ciphertext in the support of the marginal distribution. -/ noncomputable def posteriorMsgDist (scheme : EncScheme M K C) (msgDist : PMF M) (c : C) (hc : c ∈ (scheme.marginalCiphertextDist msgDist).s...
302
leanexplore_declaration
leanexplore:386907
leanexplore_sqlite
leanexplore_declarations
@[simp] theorem posteriorMsgDist_apply (scheme : EncScheme M K C) (msgDist : PMF M) (c : C) (hc : c ∈ (scheme.marginalCiphertextDist msgDist).support) (m : M) : scheme.posteriorMsgDist msgDist c hc m = scheme.jointDist msgDist (m, c) / scheme.marginalCiphertextDist msgDist c := rfl
388
leanexplore_declaration
leanexplore:386908
leanexplore_sqlite
leanexplore_declarations
/-- An encryption scheme is perfectly secret if the posterior message distribution equals the prior for every ciphertext with positive probability ([KatzLindell2020], Definition 2.3). -/ def PerfectlySecret (scheme : EncScheme M K C) : Prop := ∀ (msgDist : PMF M) (c : C) (hc : c ∈ (scheme.marginalCiphertextDist m...
253
leanexplore_declaration
leanexplore:386909
leanexplore_sqlite
leanexplore_declarations
/-- Ciphertext indistinguishability: the ciphertext distribution is the same for all messages ([KatzLindell2020], Lemma 2.5). -/ def CiphertextIndist (scheme : EncScheme M K C) : Prop := ∀ m₀ m₁ : M, scheme.ciphertextDist m₀ = scheme.ciphertextDist m₁
652
leanexplore_declaration
leanexplore:386910
leanexplore_sqlite
leanexplore_declarations
/-- A private-key encryption scheme over message space `M`, key space `K`, and ciphertext space `C` ([KatzLindell2020], Definition 2.1). -/ structure EncScheme (Message Key Ciphertext : Type*) where /-- Probabilistic key generation. -/ gen : PMF Key /-- (Possibly randomized) encryption. -/ enc (key : Key) (mess...
589
leanexplore_declaration
leanexplore:386911
leanexplore_sqlite
leanexplore_declarations
/-- Build an encryption scheme from deterministic pure encryption/decryption where decryption is a left inverse of encryption for every key. -/ noncomputable def EncScheme.ofPure.{u} {Message Key Ciphertext : Type u} (gen : PMF Key) (enc : Key → Message → Ciphertext) (dec : Key → Ciphertext → Message) (h : ∀ ke...
155
leanexplore_declaration
leanexplore:386912
leanexplore_sqlite
leanexplore_declarations
instance bitVecFintype (n : ℕ) : Fintype (BitVec n) := Fintype.ofEquiv (Fin (2 ^ n)) ⟨BitVec.ofFin, BitVec.toFin, fun x => by simp, fun x => by simp⟩
183
leanexplore_declaration
leanexplore:386913
leanexplore_sqlite
leanexplore_declarations
/-- XOR by a fixed mask is self-inverse on `BitVec`: `c = k ^^^ m ↔ c ^^^ m = k`. -/ lemma eq_xor_iff_xor_eq {l : ℕ} (c m k : BitVec l) : (c = k ^^^ m) ↔ (c ^^^ m = k) := by grind
308
leanexplore_declaration
leanexplore:386914
leanexplore_sqlite
leanexplore_declarations
/-- The ciphertext distribution of the OTP is uniform, regardless of the message. -/ theorem otp_ciphertextDist_eq_uniform (l : ℕ) (m : BitVec l) : (PMF.uniformOfFintype (BitVec l)).bind (fun k => PMF.pure (k ^^^ m)) = PMF.uniformOfFintype (BitVec l) := by simp [PMF.ext_iff, eq_xor_iff_xor_eq]