Skip to content

Commit da87f95

Browse files
committed
TreeSet iterators
1 parent a0e9f7c commit da87f95

File tree

7 files changed

+104
-23
lines changed

7 files changed

+104
-23
lines changed

src/Std/Data/DTreeMap/Internal/Zipper.lean

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public def Zipper.prependMapGT [Ord α] (t : Impl α β) (lowerBound : α)
201201
| .eq => prependMapGT r lowerBound it
202202
| .gt => prependMapGT r lowerBound it
203203

204-
theorem Zipper.prependMap_of_pruneLE_eq_prependMapGE [Ord α] (t : Impl α β) (ord_t : t.Ordered)
204+
theorem Zipper.prependMap_pruneLE_eq_prependMapGE [Ord α] (t : Impl α β) (ord_t : t.Ordered)
205205
(z : Zipper α β) (lowerBound : α) :
206206
z.prependMap (t.pruneLE lowerBound) = z.prependMapGE t lowerBound := by
207207
induction t generalizing z
@@ -322,8 +322,8 @@ theorem Zipper.size_prependMap (t : Impl α β) (it : Zipper α β) :
322322

323323
public def Zipper.step : Zipper α β → IterStep (IterM (α := Zipper α β) Id ((a : α) × β a)) ((a : α) × β a)
324324
| .done => .done
325-
| .cons k v t it=>
326-
.yield ⟨it.prependMap t⟩ ⟨k, v⟩
325+
| .cons k v t it =>
326+
.yield ⟨it.prependMap t⟩ ⟨k, v⟩
327327

328328
public instance : Iterator (Zipper α β) Id ((a : α) × β a) where
329329
IsPlausibleStep it step := it.internalState.step = step
@@ -333,7 +333,7 @@ public instance : IteratorCollect (Zipper α β) Id Id := .defaultImplementation
333333

334334
public instance : IteratorCollectPartial (Zipper α β) Id Id := .defaultImplementation
335335

336-
def Zipper.instFinitenessRelation : FinitenessRelation (Zipper α β) Id where
336+
def Zipper.FinitenessRelation : FinitenessRelation (Zipper α β) Id where
337337
rel t' t := t'.internalState.size < t.internalState.size
338338
wf := by
339339
apply InvImage.wf
@@ -362,7 +362,7 @@ def Zipper.instFinitenessRelation : FinitenessRelation (Zipper α β) Id where
362362

363363
@[no_expose]
364364
public instance Zipper.instFinite : Finite (Zipper α β) Id :=
365-
.of_finitenessRelation Zipper.instFinitenessRelation
365+
.of_finitenessRelation Zipper.FinitenessRelation
366366

367367
public def Zipper.iter (t : Zipper α β) : Iter (α := Zipper α β) ((a : α) × β a) := ⟨t⟩
368368

@@ -407,21 +407,17 @@ theorem Zipper.toList_iter {α β} {z : Zipper α β} : z.iter.toList = z.toList
407407
termination_by z.size
408408
decreasing_by simp [size_prependMap]
409409

410-
public theorem Zipper.iterOfTree_toList_eq_toList (t : Impl α β) :
410+
public theorem Zipper.toList.iterOfTree (t : Impl α β) :
411411
(Zipper.iterOfTree t).toList = t.toList := by
412-
rw [iterOfTree, iter]
412+
rw [Zipper.iterOfTree, Zipper.iter]
413413
have := @Zipper.toList_iter α β (prependMap t .done)
414414
simp only [iter] at this
415415
rw [this]
416416
simp only [Zipper.toList_prependMap_eq_append, toList, List.append_nil]
417417

418418
public theorem Zipper.map_iterOfTree_eq_tree_toList_map (t : Impl α β) :
419419
(Iter.map f (Internal.Zipper.iterOfTree t)).toList = List.map f t.toList := by
420-
rw [Iter.toList_map]
421-
congr
422-
rw [iterOfTree]
423-
rw [@toList_iter α β (prependMap t .done)]
424-
simp [Zipper.toList_prependMap_eq_append, toList]
420+
rw [Iter.toList_map, toList.iterOfTree]
425421

426422
end Zipper
427423

@@ -449,7 +445,7 @@ public instance [Ord α] : IteratorCollect (RxcIterator α β) Id Id := .default
449445

450446
public instance [Ord α] : IteratorCollectPartial (RxcIterator α β) Id Id := .defaultImplementation
451447

452-
def instFinitenessRelation [Ord α] : FinitenessRelation (RxcIterator α β) Id where
448+
def RxcIterator.FinitenessRelation [Ord α] : FinitenessRelation (RxcIterator α β) Id where
453449
rel t' t := t'.internalState.iter.size < t.internalState.iter.size
454450
wf := by
455451
apply InvImage.wf
@@ -484,7 +480,7 @@ def instFinitenessRelation [Ord α] : FinitenessRelation (RxcIterator α β) Id
484480

485481
@[no_expose]
486482
public instance instFinite [Ord α] : Finite (RxcIterator α β) Id :=
487-
.of_finitenessRelation instFinitenessRelation
483+
.of_finitenessRelation RxcIterator.FinitenessRelation
488484

489485
@[simp]
490486
theorem RxcIterator.step_done [Ord α] {upper : α} : ({iter := .done, upper := upper } : RxcIterator α β).step = .done := rfl
@@ -836,13 +832,13 @@ theorem toList_rccIter {α β} [Ord α] [TransOrd α]
836832
rw [Bool.and_comm]
837833
rw [← List.filter_filter]
838834
congr 1
839-
rw [← Zipper.prependMap_of_pruneLE_eq_prependMapGE]
835+
rw [← Zipper.prependMap_pruneLE_eq_prependMapGE]
840836
. rw [Zipper.toList_prependMap_eq_append]
841837
rw [Impl.toList_pruneLE]
842838
. simp [Zipper.toList]
843839
. exact t_ord
844840
. exact t_ord
845-
. rw [← Zipper.prependMap_of_pruneLE_eq_prependMapGE]
841+
. rw [← Zipper.prependMap_pruneLE_eq_prependMapGE]
846842
. simp only [Zipper.toList_prependMap_eq_append, Zipper.toList, List.append_nil]
847843
rw [Impl.toList_pruneLE]
848844
. apply List.Pairwise.filter
@@ -929,13 +925,13 @@ theorem toList_rcoIter {α β} [Ord α] [TransOrd α]
929925
rw [Bool.and_comm]
930926
rw [← List.filter_filter]
931927
congr 1
932-
rw [← Zipper.prependMap_of_pruneLE_eq_prependMapGE]
928+
rw [← Zipper.prependMap_pruneLE_eq_prependMapGE]
933929
. rw [Zipper.toList_prependMap_eq_append]
934930
rw [Impl.toList_pruneLE]
935931
. simp only [Zipper.toList, List.append_nil]
936932
. exact t_ord
937933
. exact t_ord
938-
. rw [← Zipper.prependMap_of_pruneLE_eq_prependMapGE]
934+
. rw [← Zipper.prependMap_pruneLE_eq_prependMapGE]
939935
. simp only [Zipper.toList_prependMap_eq_append, Zipper.toList, List.append_nil]
940936
rw [Impl.toList_pruneLE]
941937
. apply List.Pairwise.filter
@@ -1203,7 +1199,7 @@ theorem toList_rciIter {α β} [Ord α] [TransOrd α]
12031199
have := @Zipper.toList_iter _ _ (Zipper.prependMapGE t lowerBound Zipper.done)
12041200
simp only [Zipper.iter] at this
12051201
simp only [this]
1206-
rw [← Zipper.prependMap_of_pruneLE_eq_prependMapGE]
1202+
rw [← Zipper.prependMap_pruneLE_eq_prependMapGE]
12071203
. simp only [Zipper.toList_prependMap_eq_append, Zipper.toList, List.append_nil]
12081204
apply Impl.toList_pruneLE
12091205
exact t_ord
@@ -1258,7 +1254,7 @@ public theorem toList_rci {α : Type u} {β : Type v} [Ord α] [TransOrd α] (t
12581254
have := @Zipper.toList_iter _ _ (Zipper.prependMapGE t lowerBound Zipper.done)
12591255
simp only [Zipper.iter] at this
12601256
simp only [this]
1261-
rw [← Zipper.prependMap_of_pruneLE_eq_prependMapGE]
1257+
rw [← Zipper.prependMap_pruneLE_eq_prependMapGE]
12621258
. simp [Zipper.toList_prependMap_eq_append]
12631259
rw [Impl.toList_pruneLE]
12641260
simp [Impl.toList_eq_toListModel]

src/Std/Data/DTreeMap/Raw/Iterator.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ The key and value types must live in the same universe.
4343
-/
4444
@[inline]
4545
public def keysIter {α : Type u} {β : α → Type u}
46-
(cmp : α → α → Ordering := by exact compare) (m : Raw α β cmp) :=
46+
(cmp : α → α → Ordering := by exact compare) (m : Raw α β cmp) :=
4747
((m.iter cmp).map fun e => e.1 : Iter α)
4848

4949
/--
@@ -66,7 +66,7 @@ public def valuesIter {α : Type u} {β : Type u}
6666
public theorem iter_toList {cmp : α → α → Ordering} (m : Raw α β cmp) :
6767
(m.iter cmp).toList = m.toList := by
6868
rw [iter, toList]
69-
apply Internal.Zipper.iterOfTree_toList_eq_toList
69+
apply Internal.Zipper.toList.iterOfTree
7070

7171
@[simp]
7272
public theorem keysIter_toList {α β} {cmp : α → α → Ordering} (m : Raw α β cmp) :

src/Std/Data/TreeMap/Raw/Iterator.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public def valuesIter {α : Type u} {β : Type u}
6363
(DTreeMap.Raw.valuesIter cmp m.inner : Iter β)
6464

6565
@[simp]
66-
public theorem iter_toList {α : Type u} {β : Type u} {cmp : α → α → Ordering} (m : Raw α β cmp) :
66+
public theorem iter_toList {α : Type u} {β : Type v} {cmp : α → α → Ordering} (m : Raw α β cmp) :
6767
((m.iter cmp).toList) = m.toList := by
6868
simp only [iter, Iter.toList_map, DTreeMap.Raw.iter_toList]
6969
simp only [DTreeMap.Raw.toList, DTreeMap.Internal.Impl.toList_eq_toListModel, toList,

src/Std/Data/TreeSet.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ prelude
99
public import Std.Data.TreeSet.Basic
1010
public import Std.Data.TreeSet.AdditionalOperations
1111
public import Std.Data.TreeSet.Lemmas
12+
public import Std.Data.TreeSet.Iterator

src/Std/Data/TreeSet/Iterator.lean

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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: Wojciech Różowski
5+
-/
6+
module
7+
8+
prelude
9+
public import Std.Data.TreeSet.Basic
10+
public import Std.Data.TreeMap.Iterator
11+
public import Std.Data.DTreeMap.Lemmas
12+
13+
/-!
14+
# Iterators on `DTreeMap`
15+
-/
16+
17+
namespace Std.TreeSet
18+
open Std.Iterators
19+
20+
/--
21+
Returns a finite iterator over the entries of a tree setz.
22+
The iterator yields the elements of the set in order and then terminates.
23+
24+
**Termination properties:**
25+
26+
* `Finite` instance: always
27+
* `Productive` instance: always
28+
-/
29+
@[inline]
30+
public def iter {α : Type u}
31+
(cmp : α → α → Ordering := by exact compare) (m : TreeSet α cmp) :=
32+
((m.inner.iter cmp).map fun e => e.1 : Iter α)
33+
34+
@[simp]
35+
public theorem iter_toList {cmp : α → α → Ordering} (m : TreeSet α cmp) :
36+
(m.iter cmp).toList = m.toList := by
37+
rw [iter, Iter.toList_map, TreeMap.iter_toList, toList, TreeMap.toList, TreeMap.keys, Std.DTreeMap.Const.map_fst_toList_eq_keys]
38+
39+
end Std.TreeSet

src/Std/Data/TreeSet/Raw.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ prelude
99
public import Std.Data.TreeSet.Raw.Basic
1010
public import Std.Data.TreeSet.Raw.Lemmas
1111
public import Std.Data.TreeSet.Raw.WF
12+
public import Std.Data.TreeSet.Raw.Iterator
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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: Wojciech Różowski
5+
-/
6+
module
7+
8+
prelude
9+
public import Std.Data.TreeSet.Raw.Basic
10+
public import Std.Data.TreeMap.Raw.Iterator
11+
public import Std.Data.DTreeMap.Raw.Lemmas
12+
13+
/-!
14+
# Iterators on `DTreeMap`
15+
-/
16+
17+
namespace Std.TreeSet.Raw
18+
open Std.Iterators
19+
20+
/--
21+
Returns a finite iterator over the entries of a tree set.
22+
The iterator yields the elements of the set in order and then terminates.
23+
24+
**Termination properties:**
25+
26+
* `Finite` instance: always
27+
* `Productive` instance: always
28+
-/
29+
@[inline]
30+
public def iter {α : Type u}
31+
(cmp : α → α → Ordering := by exact compare) (m : Raw α cmp) :=
32+
((m.inner.iter cmp).map fun e => e.1 : Iter α)
33+
34+
@[simp]
35+
public theorem iter_toList {cmp : α → α → Ordering} (m : Raw α cmp) :
36+
(m.iter cmp).toList = m.toList := by
37+
rw [iter, Iter.toList_map, TreeMap.Raw.iter_toList, TreeMap.Raw.toList]
38+
rw [TreeSet.Raw.toList]
39+
rw [Std.DTreeMap.Internal.Impl.foldr_eq_foldr_toList]
40+
simp only [DTreeMap.Raw.Const.map_fst_toList_eq_keys, List.empty_eq, List.foldr_cons_eq_append,
41+
List.append_nil]
42+
rw [DTreeMap.Raw.keys, ← Std.DTreeMap.Internal.Impl.map_fst_toList_eq_keys]
43+
44+
end Std.TreeSet.Raw

0 commit comments

Comments
 (0)