Skip to content

Commit 966d29e

Browse files
committed
Add chain_concat_sf variant for standard form programs
Add Steps.chain_concat_sf that derives pc equality from IsStandardForm, eliminating explicit pc proofs at call sites where the first program is standard form. Refactor call sites in: - Preservation.lean: 3 sites use chain_concat_sf - Halting.lean: Most sites use chain_concat_sf, 2 remain with chain_concat - Correctness.lean: Mixed approach based on downstream pc requirements
1 parent 8884d60 commit 966d29e

4 files changed

Lines changed: 37 additions & 26 deletions

File tree

Urm/Composition/Correctness.lean

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ theorem comp_general_halts_imp_gi_dom
109109
have hn_le_base : n ≤ base + 1 := by have := compositionBase_ge_n_sub_one m n pF pGs; omega
110110
have hsSave_value : sSave.read (base + 1 + r) = inputs ⟨r, hr_lt_n⟩ := by
111111
rw [hsSave_eq]; exact saveInputs_state base n hn_le_base inputs r hr_lt_n
112-
have hSavePrefixChain := Steps.chain_concat hSave_steps (by simp) rfl hPrefix_steps hPrefix_halted
112+
have hSavePrefixChain := Steps.chain_concat_sf hSaveInputs_sf hSave_steps hSave_halted' hPrefix_steps hPrefix_halted
113113
have hPrefix_state_match : cPrefix.state = sSavePrefix := by
114114
simp only [hsSavePrefix_eq hSavePrefix_halts, ← Steps.halts_unique hSavePrefixChain.1 hSavePrefixChain.2 hSavePrefix_halts.choose_spec.1 hSavePrefix_halts.choose_spec.2]
115115
have hPrefix_preserves := allGPhases_prefix_preserves_saved_inputs m n base pGs hGs_sf hpGs_max hn_le_base
@@ -251,23 +251,22 @@ theorem comp_general_dom_imp_halts
251251
have hGPhases_pc : cGPhases.pc = gPhases.length :=
252252
hGPhases_sf.pc_eq_length_of_halted hGPhases_steps (Nat.zero_le _) hGPhases_halted
253253
let sGPhases := cGPhases.state
254+
have hpc_eq : cGPhases.pc + saveInputs.length = (saveInputs.concat gPhases).length := by
255+
simp only [Program.concat_length, hGPhases_pc]; omega
254256
have hResults : ∀ j : ℕ, (hj : j < m) → sGPhases.read (base + n + 1 + j) = results ⟨j, hj⟩ := fun j hj => by
255257
have hSaveGPhases_halted' : ∃ c : Config,
256258
Steps (saveInputs.concat gPhases) ⟨0, State.fromInputs (List.ofFn inputs)⟩ c ∧
257259
c.isHalted (saveInputs.concat gPhases) ∧ c.state = cGPhases.state :=
258-
⟨⟨(saveInputs.concat gPhases).length, sGPhases⟩,
259-
by convert hSaveGPhases_steps using 2; simp [Program.concat_length, hGPhases_pc, add_comm],
260-
by simp, rfl⟩
260+
⟨⟨(saveInputs.concat gPhases).length, sGPhases⟩, hpc_eq ▸ hSaveGPhases_steps, by simp, rfl⟩
261261
exact allGPhases_saves_result (pF := pF) hGs_sf hGs_spec inputs hGs_dom ⟨j, hj⟩ _ hSaveGPhases_halted'
262262
have hpF_max : pF.maxRegister ≤ base := compositionBase_ge_pF_max m n pF pGs
263263
have hm_le_base : m ≤ base + 1 := compositionBase_ge_m m n pF pGs
264264
obtain ⟨cFinal, hFinal_steps, hFinal_halted⟩ :=
265265
finalPhase_halts_from_results hF_sf hpF_max hm_le_base hF_halts sGPhases hResults
266266
have hSaveGPhases_steps' : Steps (saveInputs.concat gPhases) ⟨0, State.fromInputs (List.ofFn inputs)⟩
267-
⟨(saveInputs.concat gPhases).length, sGPhases⟩ := by
268-
convert hSaveGPhases_steps using 2; simp only [Program.concat_length, hGPhases_pc]; omega
269-
have ⟨hTotal_steps, hTotal_halted⟩ := Steps.chain_concat hSaveGPhases_steps' (by simp)
270-
rfl hFinal_steps hFinal_halted
267+
⟨(saveInputs.concat gPhases).length, sGPhases⟩ := hpc_eq ▸ hSaveGPhases_steps
268+
have hSaveGPhases_halted' : (⟨(saveInputs.concat gPhases).length, sGPhases⟩ : Config).isHalted (saveInputs.concat gPhases) := by simp
269+
have ⟨hTotal_steps, hTotal_halted⟩ := Steps.chain_concat hSaveGPhases_steps' hSaveGPhases_halted' rfl hFinal_steps hFinal_halted
271270
rw [hH_eq, ← concat_assoc]; exact ⟨_, hTotal_steps, hTotal_halted⟩
272271

273272
/-- The result of the composition equals the composed function value. -/
@@ -314,6 +313,8 @@ theorem comp_general_result
314313
have hn_le_base : n ≤ base + 1 := compositionBase_ge_n m n pF pGs
315314
have hGi_halts : ∀ i, Halts (pGs i) (List.ofFn inputs) := fun i => (hGs_spec i inputs).1.mpr (hGs_dom i)
316315
have hGPhases_sf := allGPhases_isStandardForm (n := n) (base := base) hGs_sf
316+
have hSaveInputs_sf := saveInputs_isStandardForm base n
317+
have hSaveGPhases_sf := hSaveInputs_sf.concat hGPhases_sf
317318
have hSave_sl := copyRegisterRange_isStraightLine 0 (base + 1) n
318319
obtain ⟨cSave, hSave_steps, hSave_halted, hSave_pc'⟩ := straightLine_halts_from_state hSave_sl (State.fromInputs (List.ofFn inputs))
319320
have hNoOverlap' : 0 + n ≤ base + 1 := by simp; exact compositionBase_ge_n m n pF pGs
@@ -332,6 +333,8 @@ theorem comp_general_result
332333
have hGPhases_pc : cGPhases.pc = gPhases.length :=
333334
hGPhases_sf.pc_eq_length_of_halted hGPhases_steps (Nat.zero_le _) hGPhases_halted
334335
let sGPhases := cGPhases.state
336+
have hpc_eq : cGPhases.pc + saveInputs.length = (saveInputs.concat gPhases).length := by
337+
simp only [Program.concat_length, hGPhases_pc]; omega
335338
have hResults : ∀ j : ℕ, (hj : j < m) → sGPhases.read (base + n + 1 + j) = results ⟨j, hj⟩ := fun j hj => by
336339
have hSaveGPhases_halted' : ∃ c : Config,
337340
Steps (saveInputs.concat gPhases) ⟨0, State.fromInputs (List.ofFn inputs)⟩ c ∧
@@ -377,9 +380,9 @@ theorem comp_general_result
377380
have ⟨hTransferF_steps, hTransferF_halted⟩ := Steps.chain_concat hTransfer_steps hTransfer_halted rfl epF.steps epF.halted
378381
have ⟨hClearTransferF_steps, hClearTransferF_halted⟩ := Steps.chain_concat hClear_steps hClear_halted hClear_pc hTransferF_steps hTransferF_halted
379382
have hSaveGPhases_steps' : Steps (saveInputs.concat gPhases) ⟨0, State.fromInputs (List.ofFn inputs)⟩
380-
⟨(saveInputs.concat gPhases).length, sGPhases⟩ := by
381-
convert hSaveGPhases_steps using 2; simp only [Program.concat_length, hGPhases_pc]; omega
382-
have ⟨hTotal_steps, hTotal_halted⟩ := Steps.chain_concat hSaveGPhases_steps' (by simp) rfl hClearTransferF_steps hClearTransferF_halted
383+
⟨(saveInputs.concat gPhases).length, sGPhases⟩ := hpc_eq ▸ hSaveGPhases_steps
384+
have hSaveGPhases_halted' : (⟨(saveInputs.concat gPhases).length, sGPhases⟩ : Config).isHalted (saveInputs.concat gPhases) := by simp
385+
have ⟨hTotal_steps, hTotal_halted⟩ := Steps.chain_concat hSaveGPhases_steps' hSaveGPhases_halted' rfl hClearTransferF_steps hClearTransferF_halted
383386
rw [hH_eq, ← concat_assoc] at hH_steps hH_halted
384387
let cH_built : Config := ⟨(saveInputs.concat gPhases).length + final.length + epF.config.pc - pF.length, epF.config.state⟩
385388
have hcH_built_halted : cH_built.isHalted ((saveInputs.concat gPhases).concat final) := by

Urm/Composition/Halting.lean

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ theorem gPhase_halts_from_saved_inputs {base n : ℕ} {pG : Program} {i : ℕ} {
3434
simp [State.fromInputs, State.read, hr_n]
3535
let epG := Halts.executeFromAgreeingState hpG_halts hpG_sf hagree
3636
let eT := executeSingleTransfer 0 (base + n + 1 + i) epG.config.state
37-
have ⟨hPGT_steps, hPGT_halted⟩ := Steps.chain_concat epG.steps epG.halted epG.pc_eq eT.steps eT.halted
38-
have ⟨hCopyPGT_steps, hCopyPGT_halted⟩ := Steps.chain_concat hCopy_steps hCopy_halted hCopy_pc hPGT_steps hPGT_halted
39-
have ⟨hGPhase_steps, hGPhase_halted⟩ := Steps.chain_concat hClear_steps hClear_halted hClear_pc hCopyPGT_steps hCopyPGT_halted
37+
have ⟨hPGT_steps, hPGT_halted⟩ := Steps.chain_concat_sf hpG_sf epG.steps epG.halted eT.steps eT.halted
38+
have ⟨hCopyPGT_steps, hCopyPGT_halted⟩ := Steps.chain_concat_sf (copyRegisterRange_isStandardForm (base + 1) 0 n) hCopy_steps hCopy_halted hPGT_steps hPGT_halted
39+
have ⟨hGPhase_steps, hGPhase_halted⟩ := Steps.chain_concat_sf (clearRegisters_isStandardForm base) hClear_steps hClear_halted hCopyPGT_steps hCopyPGT_halted
4040
exact ⟨_, hGPhase_steps, hGPhase_halted⟩
4141

4242
theorem gPhase_writes_result {base n : ℕ} {pG : Program} {j : ℕ} {inputs : Fin n → ℕ}
@@ -75,9 +75,9 @@ theorem gPhase_writes_result {base n : ℕ} {pG : Program} {j : ℕ} {inputs : F
7575
have hT_result : cT.state.read (base + n + 1 + j) = sPG.read 0 := by
7676
let tr := executeSingleTransfer 0 (base + n + 1 + j) sPG
7777
simp only [Steps.halts_unique hT_steps hT_halted tr.steps tr.halted, tr.dst_eq]
78-
have hPGT := Steps.chain_concat hPG_steps (by simp) rfl hT_steps hT_halted
79-
have hCopyPGT := Steps.chain_concat hCopy_steps (by simp) rfl hPGT_steps hPGT_halted
80-
have hClearRest := Steps.chain_concat hClear_steps (by simp) rfl hRest_steps hRest_halted
78+
have hPGT := Steps.chain_concat_sf hpG_sf hPG_steps (by simp) hT_steps hT_halted
79+
have hCopyPGT := Steps.chain_concat_sf (copyRegisterRange_isStandardForm (base + 1) 0 n) hCopy_steps (by simp) hPGT_steps hPGT_halted
80+
have hClearRest := Steps.chain_concat_sf (clearRegisters_isStandardForm base) hClear_steps (by simp) hRest_steps hRest_halted
8181
simp only [Steps.halts_unique hsteps hhalted hClearRest.1 hClearRest.2,
8282
Steps.halts_unique hRest_steps hRest_halted hCopyPGT.1 hCopyPGT.2,
8383
Steps.halts_unique hPGT_steps hPGT_halted hPGT.1 hPGT.2, hT_result, hR0_after_pG]
@@ -100,15 +100,14 @@ theorem allGPhases_halts_from_saved_inputs {m n base : ℕ} {pGs : Fin m → Pro
100100
obtain ⟨cPrefix, hPrefix_steps, hPrefix_halted⟩ := ih (fun i => hpGs_sf i.castSucc)
101101
(fun i => hpGs_max i.castSucc) (fun i => hpGs_halts i.castSucc)
102102
have hPrefix_sf := allGPhases_isStandardForm (n := n) (base := base) (fun i => hpGs_sf i.castSucc)
103-
have hPrefix_pc := hPrefix_sf.pc_eq_length_of_halted hPrefix_steps (Nat.zero_le _) hPrefix_halted
104103
have hSaved_after_prefix : ∀ j : ℕ, (hj : j < n) → cPrefix.state.read (base + 1 + j) = inputs ⟨j, hj⟩ := fun j hj =>
105104
allGPhases_prefix_preserves_saved_inputs m' n base pGs' (fun i => hpGs_sf i.castSucc) (fun i => hpGs_max i.castSucc) hn_le_base
106105
m' (Nat.le_refl m') s cPrefix.state cPrefix
107106
(allGPhases_prefix_full m' n base pGs' ▸ hPrefix_steps) (allGPhases_prefix_full m' n base pGs' ▸ hPrefix_halted) rfl
108107
(base + 1 + j) (by omega) (by omega) ▸ hSaved j hj
109108
obtain ⟨cLast, hLast_steps, hLast_halted⟩ := gPhase_halts_from_saved_inputs (i := m')
110109
(hpGs_sf (Fin.last m')) (hpGs_max (Fin.last m')) hn_le_base (hpGs_halts (Fin.last m')) cPrefix.state hSaved_after_prefix
111-
have ⟨hTotal_steps, hTotal_halted⟩ := Steps.chain_concat hPrefix_steps hPrefix_halted hPrefix_pc hLast_steps hLast_halted
110+
have ⟨hTotal_steps, hTotal_halted⟩ := Steps.chain_concat_sf hPrefix_sf hPrefix_steps hPrefix_halted hLast_steps hLast_halted
112111
exact ⟨_, hTotal_steps, hTotal_halted⟩
113112

114113
theorem finalPhase_halts_from_results {m n base : ℕ} {pF : Program} {results : Fin m → ℕ}
@@ -140,8 +139,8 @@ theorem finalPhase_halts_from_results {m n base : ℕ} {pF : Program} {results :
140139
· rw [hTransfer_preserves r (by omega), hClear_zeros r (by omega : r ≤ base)]
141140
simp [State.fromInputs, State.read, hr_m]
142141
let epF := Halts.executeFromAgreeingState hpF_halts hpF_sf hagree
143-
have ⟨hTransferF_steps, hTransferF_halted⟩ := Steps.chain_concat hTransfer_steps hTransfer_halted rfl epF.steps epF.halted
144-
have ⟨hFinal_steps, hFinal_halted⟩ := Steps.chain_concat hClear_steps hClear_halted hClear_pc hTransferF_steps hTransferF_halted
142+
have ⟨hTransferF_steps, hTransferF_halted⟩ := Steps.chain_concat_sf (transferResultsToInputs_isStandardForm (base + n + 1) m) hTransfer_steps hTransfer_halted epF.steps epF.halted
143+
have ⟨hFinal_steps, hFinal_halted⟩ := Steps.chain_concat_sf (clearRegisters_isStandardForm base) hClear_steps hClear_halted hTransferF_steps hTransferF_halted
145144
exact ⟨_, hFinal_steps, hFinal_halted⟩
146145

147146
theorem allGPhases_suffix_preserves_earlier_results {m n base : ℕ} {pGs : Fin m → Program}
@@ -172,7 +171,7 @@ theorem allGPhases_suffix_preserves_earlier_results {m n base : ℕ} {pGs : Fin
172171
(hpGs_sf _) (hpGs_max _) hn_le_base (by omega) s sFirst ⟨_, sFirst⟩ hFirst_steps (by simp) rfl
173172
have hRest_preserves := allGPhases_suffix_preserves_earlier_results hpGs_sf hpGs_max hn_le_base
174173
(start + 1) k (by omega) sFirst cRest hRest_steps hRest_halted
175-
have hChain := Steps.chain_concat hFirst_steps (by simp) rfl hRest_steps hRest_halted
174+
have hChain := Steps.chain_concat_sf (gPhase_isStandardForm (hpGs_sf ⟨start, hEmpty⟩)) hFirst_steps (by simp) hRest_steps hRest_halted
176175
simp only [Steps.halts_unique hsteps hhalted hChain.1 hChain.2, hRest_preserves, hFirst_preserves]
177176
termination_by m - start
178177
decreasing_by simp_wf; omega
@@ -249,7 +248,7 @@ theorem allGPhases_saves_result {m n : ℕ} [NeZero m] {pF : Program} {pGs : Fin
249248
conv at hPrefix_steps => rw [hsSave'_eq]
250249
have hPrefix_steps' : Steps (allGPhases_prefix m n base pGs i.val) ⟨0, cSave.state⟩ cPrefix :=
251250
hSave_state_sSave ▸ hPrefix_steps
252-
have hPrefixChain := Steps.chain_concat hSave_steps' hSave_halted' hSave_pc' hPrefix_steps' hPrefix_halted
251+
have hPrefixChain := Steps.chain_concat_sf (copyRegisterRange_isStandardForm 0 (base + 1) n) hSave_steps' hSave_halted' hPrefix_steps' hPrefix_halted
253252
have hcPrefix_state : cPrefix.state = sSavePrefixI :=
254253
(congrArg Config.state (Steps.halts_unique hSavePrefixI_steps (by simp) hPrefixChain.1 hPrefixChain.2)).symm
255254
rw [← hcPrefix_state, allGPhases_prefix_preserves_saved_inputs m n base pGs hGs_sf hpGs_max hn_le_base

Urm/Composition/Helpers.lean

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ theorem Steps.chain_concat {p1 p2 : Program} {s : State} {c1 c2 : Config}
4545
rw [hstart_eq] at h2'
4646
exact ⟨h1'.trans h2', by simp only [Config.isHalted, Program.concat_length] at h2_halted ⊢; omega⟩
4747

48+
/-- Chain two programs where p1 is standard form: derives pc equality from halted. -/
49+
theorem Steps.chain_concat_sf {p1 p2 : Program} {s : State} {c1 c2 : Config}
50+
(h1_sf : p1.IsStandardForm)
51+
(h1_steps : Steps p1 ⟨0, s⟩ c1) (h1_halted : c1.isHalted p1)
52+
(h2_steps : Steps p2 ⟨0, c1.state⟩ c2) (h2_halted : c2.isHalted p2) :
53+
Steps (p1.concat p2) ⟨0, s⟩ ⟨c2.pc + p1.length, c2.state⟩ ∧
54+
(⟨c2.pc + p1.length, c2.state⟩ : Config).isHalted (p1.concat p2) :=
55+
chain_concat h1_steps h1_halted (h1_sf.pc_eq_length_of_halted h1_steps (Nat.zero_le _) h1_halted) h2_steps h2_halted
56+
4857
/-! ## Agreeing State Execution -/
4958

5059
/-- Bundle for an execution from an agreeing state. -/

Urm/Composition/Preservation.lean

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ private theorem gPhase_preserves_register_aux (base n : ℕ) (pG : Program) (j :
109109
let tr := executeSingleTransfer 0 (base + n + 1 + j) sPG
110110
have hT_preserves : cT.state.read r = sPG.read r := by
111111
simp only [Steps.halts_unique hT_steps hT_halted tr.steps tr.halted]; exact tr.preserved r (by omega)
112-
have hPGT := Steps.chain_concat hPG_steps (by simp) rfl hT_steps hT_halted
113-
have hRest' := Steps.chain_concat hCopy_steps (by simp) rfl hPGT_steps hPGT_halted
114-
have hGPhase := Steps.chain_concat hClear_steps (by simp) rfl hRest_steps hRest_halted
112+
have hPGT := Steps.chain_concat_sf hpG_sf hPG_steps (by simp) hT_steps hT_halted
113+
have hRest' := Steps.chain_concat_sf (copyRegisterRange_isStandardForm (base + 1) 0 n) hCopy_steps (by simp) hPGT_steps hPGT_halted
114+
have hGPhase := Steps.chain_concat_sf (clearRegisters_isStandardForm base) hClear_steps (by simp) hRest_steps hRest_halted
115115
simp only [← hstate_eq, Steps.halts_unique hsteps hhalted hGPhase.1 hGPhase.2,
116116
Steps.halts_unique hRest_steps hRest_halted hRest'.1 hRest'.2,
117117
Steps.halts_unique hPGT_steps hPGT_halted hPGT.1 hPGT.2,

0 commit comments

Comments
 (0)