|
| 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 Init.Data.List.Control |
| 8 | +import Std.Data.Iterators.Lemmas.Basic |
| 9 | +import Std.Data.Iterators.Lemmas.Consumers.Collect |
| 10 | +import Std.Data.Iterators.Lemmas.Consumers.Monadic.Loop |
| 11 | +import Std.Data.Iterators.Consumers.Collect |
| 12 | +import Std.Data.Iterators.Consumers.Loop |
| 13 | + |
| 14 | +namespace Std.Iterators |
| 15 | + |
| 16 | +theorem Iter.forIn_eq {α β : Type w} [Iterator α Id β] [Finite α Id] |
| 17 | + {m : Type w → Type w''} [Monad m] [IteratorLoop α Id m] [hl : LawfulIteratorLoop α Id m] |
| 18 | + {γ : Type w} {it : Iter (α := α) β} {init : γ} |
| 19 | + {f : β → γ → m (ForInStep γ)} : |
| 20 | + ForIn.forIn it init f = |
| 21 | + IterM.DefaultConsumers.forIn (fun _ c => pure c.run) γ (fun _ _ _ => True) |
| 22 | + IteratorLoop.wellFounded_of_finite it.toIterM init ((⟨·, .intro⟩) <$> f · ·) := by |
| 23 | + cases hl.lawful; rfl |
| 24 | + |
| 25 | +theorem Iter.forIn_eq_forIn_toIterM {α β : Type w} [Iterator α Id β] |
| 26 | + [Finite α Id] {m : Type w → Type w''} [Monad m] [LawfulMonad m] |
| 27 | + [IteratorLoop α Id m] [LawfulIteratorLoop α Id m] |
| 28 | + {γ : Type w} {it : Iter (α := α) β} {init : γ} |
| 29 | + {f : β → γ → m (ForInStep γ)} : |
| 30 | + ForIn.forIn it init f = letI : MonadLift Id m := ⟨pure⟩; ForIn.forIn it.toIterM init f := by |
| 31 | + rfl |
| 32 | + |
| 33 | +theorem Iter.forIn_eq_match_step {α β : Type w} [Iterator α Id β] |
| 34 | + [Finite α Id] {m : Type w → Type w''} [Monad m] [LawfulMonad m] |
| 35 | + [IteratorLoop α Id m] [LawfulIteratorLoop α Id m] |
| 36 | + {γ : Type w} {it : Iter (α := α) β} {init : γ} |
| 37 | + {f : β → γ → m (ForInStep γ)} : |
| 38 | + ForIn.forIn it init f = (do |
| 39 | + match it.step with |
| 40 | + | .yield it' out _ => |
| 41 | + match ← f out init with |
| 42 | + | .yield c => ForIn.forIn it' c f |
| 43 | + | .done c => return c |
| 44 | + | .skip it' _ => ForIn.forIn it' init f |
| 45 | + | .done _ => return init) := by |
| 46 | + rw [Iter.forIn_eq_forIn_toIterM, @IterM.forIn_eq_match_step, Iter.step] |
| 47 | + simp only [liftM, monadLift, pure_bind] |
| 48 | + generalize it.toIterM.step = step |
| 49 | + cases step using PlausibleIterStep.casesOn |
| 50 | + · apply bind_congr |
| 51 | + intro forInStep |
| 52 | + rfl |
| 53 | + · rfl |
| 54 | + · rfl |
| 55 | + |
| 56 | +theorem Iter.forIn_toList {α β : Type w} [Iterator α Id β] |
| 57 | + [Finite α Id] {m : Type w → Type w''} [Monad m] [LawfulMonad m] |
| 58 | + [IteratorLoop α Id m] [LawfulIteratorLoop α Id m] |
| 59 | + [IteratorCollect α Id] [LawfulIteratorCollect α Id] |
| 60 | + {γ : Type w} {it : Iter (α := α) β} {init : γ} |
| 61 | + {f : β → γ → m (ForInStep γ)} : |
| 62 | + ForIn.forIn it.toList init f = ForIn.forIn it init f := by |
| 63 | + rw [List.forIn_eq_foldlM] |
| 64 | + induction it using Iter.inductSteps generalizing init with case step it ihy ihs => |
| 65 | + rw [forIn_eq_match_step, Iter.toList_eq_match_step] |
| 66 | + simp only [map_eq_pure_bind] |
| 67 | + generalize it.step = step |
| 68 | + cases step using PlausibleIterStep.casesOn |
| 69 | + · rename_i it' out h |
| 70 | + simp only [List.foldlM_cons, bind_pure_comp, map_bind] |
| 71 | + apply bind_congr |
| 72 | + intro forInStep |
| 73 | + cases forInStep |
| 74 | + · induction it'.toList <;> simp [*] |
| 75 | + · simp only [ForIn.forIn, forIn', List.forIn'] at ihy |
| 76 | + simp [ihy h, forIn_eq_forIn_toIterM] |
| 77 | + · rename_i it' h |
| 78 | + simp only [bind_pure_comp] |
| 79 | + rw [ihs h] |
| 80 | + · simp |
| 81 | + |
| 82 | +theorem Iter.foldM_eq_forIn {α β γ : Type w} [Iterator α Id β] [Finite α Id] {m : Type w → Type w'} |
| 83 | + [Monad m] [IteratorLoop α Id m] {f : γ → β → m γ} |
| 84 | + {init : γ} {it : Iter (α := α) β} : |
| 85 | + it.foldM (init := init) f = ForIn.forIn it init (fun x acc => ForInStep.yield <$> f acc x) := |
| 86 | + rfl |
| 87 | + |
| 88 | +theorem Iter.forIn_yield_eq_foldM {α β γ δ : Type w} [Iterator α Id β] |
| 89 | + [Finite α Id] {m : Type w → Type w''} [Monad m] [LawfulMonad m] [IteratorLoop α Id m] |
| 90 | + [LawfulIteratorLoop α Id m] {f : β → γ → m δ} {g : β → γ → δ → γ} {init : γ} |
| 91 | + {it : Iter (α := α) β} : |
| 92 | + ForIn.forIn it init (fun c b => (fun d => .yield (g c b d)) <$> f c b) = |
| 93 | + it.foldM (fun b c => g c b <$> f c b) init := by |
| 94 | + simp [Iter.foldM_eq_forIn] |
| 95 | + |
| 96 | +theorem Iter.foldM_eq_match_step {α β γ : Type w} [Iterator α Id β] [Finite α Id] |
| 97 | + {m : Type w → Type w'} [Monad m] [LawfulMonad m] [IteratorLoop α Id m] |
| 98 | + [LawfulIteratorLoop α Id m] {f : γ → β → m γ} {init : γ} {it : Iter (α := α) β} : |
| 99 | + it.foldM (init := init) f = (do |
| 100 | + match it.step with |
| 101 | + | .yield it' out _ => it'.foldM (init := ← f init out) f |
| 102 | + | .skip it' _ => it'.foldM (init := init) f |
| 103 | + | .done _ => return init) := by |
| 104 | + rw [Iter.foldM_eq_forIn, Iter.forIn_eq_match_step] |
| 105 | + generalize it.step = step |
| 106 | + cases step using PlausibleIterStep.casesOn <;> simp [foldM_eq_forIn] |
| 107 | + |
| 108 | +theorem Iter.foldlM_toList {α β γ : Type w} [Iterator α Id β] [Finite α Id] {m : Type w → Type w'} |
| 109 | + [Monad m] [LawfulMonad m] [IteratorLoop α Id m] [LawfulIteratorLoop α Id m] |
| 110 | + [IteratorCollect α Id] [LawfulIteratorCollect α Id] |
| 111 | + {f : γ → β → m γ} |
| 112 | + {init : γ} {it : Iter (α := α) β} : |
| 113 | + it.toList.foldlM (init := init) f = it.foldM (init := init) f := by |
| 114 | + rw [Iter.foldM_eq_forIn, ← Iter.forIn_toList] |
| 115 | + simp only [List.forIn_yield_eq_foldlM, id_map'] |
| 116 | + |
| 117 | +theorem IterM.forIn_eq_foldM {α β : Type w} [Iterator α Id β] |
| 118 | + [Finite α Id] {m : Type w → Type w''} [Monad m] [LawfulMonad m] |
| 119 | + [IteratorLoop α Id m] [LawfulIteratorLoop α Id m] |
| 120 | + [IteratorCollect α Id] [LawfulIteratorCollect α Id] |
| 121 | + {γ : Type w} {it : Iter (α := α) β} {init : γ} |
| 122 | + {f : β → γ → m (ForInStep γ)} : |
| 123 | + forIn it init f = ForInStep.value <$> |
| 124 | + it.foldM (fun c b => match c with |
| 125 | + | .yield c => f b c |
| 126 | + | .done c => pure (.done c)) (ForInStep.yield init) := by |
| 127 | + simp only [← Iter.forIn_toList, List.forIn_eq_foldlM, ← Iter.foldlM_toList]; rfl |
| 128 | + |
| 129 | +theorem Iter.fold_eq_forIn {α β γ : Type w} [Iterator α Id β] |
| 130 | + [Finite α Id] [IteratorLoop α Id Id] {f : γ → β → γ} {init : γ} {it : Iter (α := α) β} : |
| 131 | + it.fold (init := init) f = |
| 132 | + (ForIn.forIn (m := Id) it init (fun x acc => pure (ForInStep.yield (f acc x)))).run := by |
| 133 | + rfl |
| 134 | + |
| 135 | +theorem Iter.fold_eq_foldM {α β γ : Type w} [Iterator α Id β] |
| 136 | + [Finite α Id] [IteratorLoop α Id Id] {f : γ → β → γ} {init : γ} |
| 137 | + {it : Iter (α := α) β} : |
| 138 | + it.fold (init := init) f = (it.foldM (m := Id) (init := init) (pure <| f · ·)).run := by |
| 139 | + simp [foldM_eq_forIn, fold_eq_forIn] |
| 140 | + |
| 141 | +@[simp] |
| 142 | +theorem Iter.forIn_pure_yield_eq_fold {α β γ : Type w} [Iterator α Id β] |
| 143 | + [Finite α Id] [IteratorLoop α Id Id] |
| 144 | + [LawfulIteratorLoop α Id Id] {f : β → γ → γ} {init : γ} |
| 145 | + {it : Iter (α := α) β} : |
| 146 | + ForIn.forIn (m := Id) it init (fun c b => pure (.yield (f c b))) = |
| 147 | + pure (it.fold (fun b c => f c b) init) := by |
| 148 | + simp only [fold_eq_forIn] |
| 149 | + rfl |
| 150 | + |
| 151 | +theorem Iter.fold_eq_match_step {α β γ : Type w} [Iterator α Id β] [Finite α Id] |
| 152 | + [IteratorLoop α Id Id] [LawfulIteratorLoop α Id Id] |
| 153 | + {f : γ → β → γ} {init : γ} {it : Iter (α := α) β} : |
| 154 | + it.fold (init := init) f = (match it.step with |
| 155 | + | .yield it' out _ => it'.fold (init := f init out) f |
| 156 | + | .skip it' _ => it'.fold (init := init) f |
| 157 | + | .done _ => init) := by |
| 158 | + rw [fold_eq_foldM, foldM_eq_match_step] |
| 159 | + simp only [fold_eq_foldM] |
| 160 | + generalize it.step = step |
| 161 | + cases step using PlausibleIterStep.casesOn <;> simp |
| 162 | + |
| 163 | +theorem Iter.foldl_toList {α β γ : Type w} [Iterator α Id β] [Finite α Id] |
| 164 | + [IteratorLoop α Id Id] [LawfulIteratorLoop α Id Id] |
| 165 | + [IteratorCollect α Id] [LawfulIteratorCollect α Id] |
| 166 | + {f : γ → β → γ} {init : γ} {it : Iter (α := α) β} : |
| 167 | + it.toList.foldl (init := init) f = it.fold (init := init) f := by |
| 168 | + rw [fold_eq_foldM, List.foldl_eq_foldlM, ← Iter.foldlM_toList] |
| 169 | + |
| 170 | +end Std.Iterators |
0 commit comments