Skip to content

Commit bf86966

Browse files
committed
cleanups
1 parent 0a98a2c commit bf86966

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

src/Std/Data/Iterators/Lemmas/Combinators/Monadic/Zip.lean

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@ namespace Std.Iterators
1111

1212
variable {α₁ α₂ β₁ β₂ : Type w} {m : Type w → Type w'}
1313

14-
def IterM.Intermediate.zip [Iterator α₁ m β₁] (it₁ : IterM (α := α₁) m β₁)
14+
/--
15+
Constructs intermediate states of an iterator created with the combinator `IterM.zip`.
16+
When `left.zip right` has already obtained a value from `left` but not yet from right,
17+
it remembers `left`'s value in a field of its internal state. This intermediate state
18+
cannot be created directly with `IterM.zip`.
19+
20+
`Intermediate.zip` is meant to be used only for verification purposes.
21+
-/
22+
noncomputable def IterM.Intermediate.zip [Iterator α₁ m β₁] (it₁ : IterM (α := α₁) m β₁)
1523
(memo : (Option { out : β₁ //
1624
∃ it : IterM (α := α₁) m β₁, it.IsPlausibleOutput out }))
1725
(it₂ : IterM (α := α₂) m β₂) :
@@ -62,4 +70,19 @@ theorem IterM.step_intermediateZip [Monad m] [Iterator α₁ m β₁] [Iterator
6270
obtain ⟨step, h⟩ := step
6371
cases step <;> rfl
6472

73+
theorem IterM.step_zip [Monad m] [Iterator α₁ m β₁] [Iterator α₂ m β₂]
74+
{it₁ : IterM (α := α₁) m β₁}
75+
{it₂ : IterM (α := α₂) m β₂} :
76+
(it₁.zip it₂).step = (do
77+
match ← it₁.step with
78+
| .yield it₁' out hp =>
79+
pure <| .skip (Intermediate.zip it₁' (some ⟨out, _, _, hp⟩) it₂)
80+
(.yieldLeft rfl hp)
81+
| .skip it₁' hp =>
82+
pure <| .skip (Intermediate.zip it₁' none it₂)
83+
(.skipLeft rfl hp)
84+
| .done hp =>
85+
pure <| .done (.doneLeft rfl hp)) := by
86+
simp [zip_eq_intermediateZip, step_intermediateZip]
87+
6588
end Std.Iterators

src/Std/Data/Iterators/Lemmas/Combinators/Zip.lean

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ theorem Iter.atIdxSlow?_intermediateZip [Iterator α₁ Id β₁] [Iterator α
191191
case case1 it it' out h h' =>
192192
rw [atIdxSlow?]
193193
simp only [Option.pure_def, Option.bind_eq_bind]
194-
simp [step_intermediateZip] at h'
194+
simp only [step_intermediateZip, PlausibleIterStep.skip, PlausibleIterStep.done,
195+
PlausibleIterStep.yield] at h'
195196
split at h'
196197
· split at h' <;> cases h'
197198
· split at h' <;> cases h'
@@ -218,13 +219,13 @@ theorem Iter.atIdxSlow?_intermediateZip [Iterator α₁ Id β₁] [Iterator α
218219
obtain ⟨it₁', memo', it₂', rfl⟩ := Intermediate.zip_surj it'
219220
specialize ih rfl
220221
rw [step_intermediateZip] at h'
221-
simp [PlausibleIterStep.skip] at h'
222+
simp only [PlausibleIterStep.skip, PlausibleIterStep.done, PlausibleIterStep.yield] at h'
222223
rw [Subtype.ext_iff] at h'
223224
split at h'
224225
· split at h' <;> rename_i hs₁
225226
· simp only [IterStep.skip.injEq, Intermediate.zip_inj] at h'
226227
obtain ⟨rfl, rfl, rfl⟩ := h'
227-
simp [ih, atIdxSlow?.eq_def (it := it₁), hs₁]
228+
simp only [ih, Option.pure_def, Option.bind_eq_bind, atIdxSlow?.eq_def (it := it₁), hs₁]
228229
split <;> rfl
229230
· simp only [IterStep.skip.injEq, Intermediate.zip_inj] at h'
230231
obtain ⟨rfl, rfl, rfl⟩ := h'
@@ -240,15 +241,16 @@ theorem Iter.atIdxSlow?_intermediateZip [Iterator α₁ Id β₁] [Iterator α
240241
simp [step_intermediateZip] at h
241242
cases memo
242243
case none =>
243-
simp at h
244+
simp only at h
244245
split at h <;> cases h
245246
rename_i hs₁
246247
simp [atIdxSlow?.eq_def (it := it₁), hs₁]
247248
case some =>
248-
simp at h
249+
simp only at h
249250
split at h <;> cases h
250251
rename_i hs₂
251-
simp [atIdxSlow?.eq_def (it := it₂), hs₂]
252+
simp only [atIdxSlow?.eq_def (it := it₂), hs₂, Option.pure_def, Option.bind_eq_bind,
253+
Option.bind_none, Option.bind_fun_none]
252254
split <;> rfl
253255

254256
theorem Iter.atIdxSlow?_zip {α₁ α₂ β₁ β₂} [Iterator α₁ Id β₁] [Iterator α₂ Id β₂]
@@ -283,11 +285,11 @@ theorem Iter.toList_zip_of_finite_left {α₁ α₂ β₁ β₂} [Iterator α₁
283285
[LawfulIteratorCollect (Zip α₁ Id α₂ β₂) Id Id] :
284286
(it₁.zip it₂).toList = it₁.toList.zip (it₂.take it₁.toList.length).toList := by
285287
ext
286-
simp [List.getElem?_zip_eq_some]
287-
simp [getElem?_toList_eq_atIdxSlow?, atIdxSlow?_take, atIdxSlow?_zip]
288+
simp only [List.getElem?_zip_eq_some, getElem?_toList_eq_atIdxSlow?, atIdxSlow?_zip, Option.pure_def, Option.bind_eq_bind,
289+
atIdxSlow?_take, Option.ite_none_right_eq_some]
288290
constructor
289291
· intro h
290-
simp [Option.bind_eq_some_iff] at h
292+
simp only [Option.bind_eq_some_iff, Option.some.injEq] at h
291293
obtain ⟨b₁, hb₁, b₂, hb₂, rfl⟩ := h
292294
refine ⟨hb₁, ?_, hb₂⟩
293295
false_or_by_contra
@@ -305,11 +307,11 @@ theorem Iter.toList_zip_of_finite_right {α₁ α₂ β₁ β₂} [Iterator α
305307
[LawfulIteratorCollect (Zip α₁ Id α₂ β₂) Id Id] :
306308
(it₁.zip it₂).toList = (it₁.take it₂.toList.length).toList.zip it₂.toList := by
307309
ext
308-
simp [List.getElem?_zip_eq_some]
309-
simp [getElem?_toList_eq_atIdxSlow?, atIdxSlow?_take, atIdxSlow?_zip]
310+
simp only [List.getElem?_zip_eq_some, getElem?_toList_eq_atIdxSlow?, atIdxSlow?_zip, Option.pure_def, Option.bind_eq_bind,
311+
atIdxSlow?_take, Option.ite_none_right_eq_some]
310312
constructor
311313
· intro h
312-
simp [Option.bind_eq_some_iff] at h
314+
simp only [Option.bind_eq_some_iff, Option.some.injEq] at h
313315
obtain ⟨b₁, hb₁, b₂, hb₂, rfl⟩ := h
314316
refine ⟨⟨?_, hb₁⟩, hb₂⟩
315317
false_or_by_contra

0 commit comments

Comments
 (0)