|
| 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 Std.Data.Iterators.Producers.Monadic.List |
| 8 | +import Std.Data.Iterators.Consumers |
| 9 | +import Std.Data.Iterators.Lemmas.Consumers.Monadic |
| 10 | + |
| 11 | +/-! |
| 12 | +# Lemmas about list iterators |
| 13 | +
|
| 14 | +This module provides lemmas about the interactions of `List.iterM` with `IterM.step` and various |
| 15 | +collectors. |
| 16 | +-/ |
| 17 | + |
| 18 | +namespace Std.Iterators |
| 19 | + |
| 20 | +variable {m : Type w → Type w'} [Monad m] {β : Type w} |
| 21 | + |
| 22 | +@[simp] |
| 23 | +theorem _root_.List.step_iterM_nil : |
| 24 | + (([] : List β).iterM m).step = pure ⟨.done, rfl⟩ := by |
| 25 | + simp only [IterM.step, Iterator.step]; rfl |
| 26 | + |
| 27 | +@[simp] |
| 28 | +theorem _root_.List.step_iterM_cons {x : β} {xs : List β} : |
| 29 | + ((x :: xs).iterM m).step = pure ⟨.yield (xs.iterM m) x, rfl⟩ := by |
| 30 | + simp only [List.iterM, IterM.step, Iterator.step]; rfl |
| 31 | + |
| 32 | +theorem ListIterator.toArrayMapped_toIterM [LawfulMonad m] |
| 33 | + {β : Type w} {γ : Type w} {f : β → m γ} {l : List β} : |
| 34 | + IteratorCollect.toArrayMapped f (l.iterM m) = List.toArray <$> l.mapM f := by |
| 35 | + rw [LawfulIteratorCollect.toArrayMapped_eq] |
| 36 | + induction l with |
| 37 | + | nil => |
| 38 | + rw [IterM.DefaultConsumers.toArrayMapped_eq_match_step] |
| 39 | + simp [List.step_iterM_nil] |
| 40 | + | cons x xs ih => |
| 41 | + rw [IterM.DefaultConsumers.toArrayMapped_eq_match_step] |
| 42 | + simp [List.step_iterM_cons, List.mapM_cons, pure_bind, ih] |
| 43 | + |
| 44 | +@[simp] |
| 45 | +theorem _root_.List.toArray_iterM [LawfulMonad m] {l : List β} : |
| 46 | + (l.iterM m).toArray = pure l.toArray := by |
| 47 | + simp only [IterM.toArray, ListIterator.toArrayMapped_toIterM] |
| 48 | + rw [List.mapM_pure, map_pure, List.map_id'] |
| 49 | + |
| 50 | +@[simp] |
| 51 | +theorem _root_.List.toList_iterM [LawfulMonad m] {l : List β} : |
| 52 | + (l.iterM m).toList = pure l := by |
| 53 | + rw [← IterM.toList_toArray, List.toArray_iterM, map_pure, List.toList_toArray] |
| 54 | + |
| 55 | +@[simp] |
| 56 | +theorem _root_.List.toListRev_iterM [LawfulMonad m] {l : List β} : |
| 57 | + (l.iterM m).toListRev = pure l.reverse := by |
| 58 | + simp [IterM.toListRev_eq, List.toList_iterM] |
| 59 | + |
| 60 | +end Std.Iterators |
0 commit comments