|
| 1 | +/- |
| 2 | +Copyright (c) 2025 Lean FRO, LLC. All rights reserved. |
| 3 | +Released under Apache 2.0 license as described in the file LICENSE. |
| 4 | +Authors: Paul Reichert |
| 5 | +-/ |
| 6 | +prelude |
| 7 | +import Iterator.Consumers |
| 8 | +import Iterator.Lemmas.Monadic.Consumers |
| 9 | + |
| 10 | +def Iter.induct {α β} [Iterator α Id β] [Finite α Id] |
| 11 | + (motive : Iter (α := α) β → Sort x) |
| 12 | + (step : (it : Iter (α := α) β) → |
| 13 | + (ih_yield : ∀ {it' : Iter (α := α) β} {out : β}, it.plausible_step (.yield it' out) → motive it') → |
| 14 | + (ih_skip : ∀ {it' : Iter (α := α) β}, it.plausible_step (.skip it') → motive it' ) → |
| 15 | + motive it) (it : Iter (α := α) β) : motive it := |
| 16 | + step _ (fun {it' _} _ => Iter.induct motive step it') (fun {it'} _ => Iter.induct motive step it') |
| 17 | +termination_by it.finitelyManySteps |
| 18 | + |
| 19 | +theorem Iter.toArray_eq_toArray_toIterM {α β} [Iterator α Id β] [IteratorToArray α Id] |
| 20 | + [LawfulIteratorToArray α Id] {it : Iter (α := α) β} : |
| 21 | + it.toArray = it.toIterM.toArray := |
| 22 | + rfl |
| 23 | + |
| 24 | +theorem Iter.toList_eq_toList_toIterM {α β} [Iterator α Id β] [IteratorToArray α Id] |
| 25 | + [LawfulIteratorToArray α Id] {it : Iter (α := α) β} : |
| 26 | + it.toList = it.toIterM.toList := |
| 27 | + rfl |
| 28 | + |
| 29 | +theorem Iter.toListRev_eq_toListRev_toIterM {α β} [Iterator α Id β] [Finite α Id] |
| 30 | + {it : Iter (α := α) β} : |
| 31 | + it.toListRev = it.toIterM.toListRev := |
| 32 | + rfl |
| 33 | + |
| 34 | +@[simp] |
| 35 | +theorem IterM.toListRev_toPureIter {α β} [Iterator α Id β] [Finite α Id] |
| 36 | + {it : IterM (α := α) Id β} : |
| 37 | + it.toPureIter.toListRev = it.toListRev := |
| 38 | + rfl |
| 39 | + |
| 40 | +theorem Iter.toList_toArray {α β} [Iterator α Id β] [IteratorToArray α Id] |
| 41 | + [LawfulIteratorToArray α Id] {it : Iter (α := α) β} : |
| 42 | + it.toArray.toList = it.toList := by |
| 43 | + simp only [toArray_eq_toArray_toIterM, toList_eq_toList_toIterM, ← IterM.toList_toArray, |
| 44 | + Id.map_eq] |
| 45 | + |
| 46 | +theorem Iter.toArray_toList {α β} [Iterator α Id β] [IteratorToArray α Id] |
| 47 | + [LawfulIteratorToArray α Id] {it : Iter (α := α) β} : |
| 48 | + it.toList.toArray = it.toArray := by |
| 49 | + simp only [toArray_eq_toArray_toIterM, toList_eq_toList_toIterM, ← IterM.toArray_toList, |
| 50 | + Id.map_eq] |
| 51 | + |
| 52 | +theorem Iter.toListRev_eq {α β} [Iterator α Id β] [Finite α Id] [IteratorToArray α Id] |
| 53 | + [LawfulIteratorToArray α Id] {it : Iter (α := α) β} : |
| 54 | + it.toListRev = it.toList.reverse := by |
| 55 | + simp [Iter.toListRev_eq_toListRev_toIterM, Iter.toList_eq_toList_toIterM, IterM.toListRev_eq] |
| 56 | + |
| 57 | +theorem Iter.toArray_of_step {α β} [Iterator α Id β] [IteratorToArray α Id] |
| 58 | + [LawfulIteratorToArray α Id] {it : Iter (α := α) β} : |
| 59 | + it.toArray = match it.step with |
| 60 | + | .yield it' out _ => #[out] ++ it'.toArray |
| 61 | + | .skip it' _ => it'.toArray |
| 62 | + | .done _ => #[] := by |
| 63 | + rw [Iter.toArray_eq_toArray_toIterM, Iter.step, IterM.toArray_of_step, IterM.step] |
| 64 | + simp only [Id.map_eq, Id.pure_eq, Id.bind_eq, Id.run] |
| 65 | + generalize it.toIterM.stepH.inflate = step |
| 66 | + obtain ⟨step, h⟩ := step |
| 67 | + cases step <;> |
| 68 | + simp [PlausibleIterStep.map, PlausibleIterStep.yield, Iter.toArray_eq_toArray_toIterM] |
| 69 | + |
| 70 | +theorem Iter.toList_of_step {α β} [Iterator α Id β] [IteratorToArray α Id] |
| 71 | + [LawfulIteratorToArray α Id] {it : Iter (α := α) β} : |
| 72 | + it.toList = match it.step with |
| 73 | + | .yield it' out _ => out :: it'.toList |
| 74 | + | .skip it' _ => it'.toList |
| 75 | + | .done _ => [] := by |
| 76 | + rw [← Iter.toList_toArray, Iter.toArray_of_step] |
| 77 | + split <;> simp [Iter.toList_toArray] |
| 78 | + |
| 79 | +theorem Iter.toListRev_of_step {α β} [Iterator α Id β] [Finite α Id] {it : Iter (α := α) β} : |
| 80 | + it.toListRev = match it.step with |
| 81 | + | .yield it' out _ => it'.toListRev ++ [out] |
| 82 | + | .skip it' _ => it'.toListRev |
| 83 | + | .done _ => [] := by |
| 84 | + rw [Iter.toListRev_eq_toListRev_toIterM, IterM.toListRev_of_step, Iter.step, IterM.step] |
| 85 | + simp only [Id.map_eq, Id.pure_eq, Id.bind_eq, Id.run] |
| 86 | + generalize it.toIterM.stepH.inflate = step |
| 87 | + obtain ⟨step, h⟩ := step |
| 88 | + cases step <;> simp [PlausibleIterStep.map, PlausibleIterStep.yield] |
| 89 | + |
| 90 | +-- TODO: congruence lemmas |
0 commit comments