Skip to content

Commit e14d54c

Browse files
committed
Add DTreeMap iterators
1 parent e6821e0 commit e14d54c

File tree

3 files changed

+103
-68
lines changed

3 files changed

+103
-68
lines changed

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

Lines changed: 63 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def Zipper.prependMapGT [Ord α] (t : Impl α β) (lower_bound : α)
5858
| .eq => prependMapGT r lower_bound it
5959
| .gt => prependMapGT r lower_bound it
6060

61-
theorem prepend_prune_LE_eq_prependMapGE [Ord α] (t : Impl α β) (ord_t : t.Ordered)
61+
theorem Zipper.prepend_prune_LE_eq_prependMapGE [Ord α] (t : Impl α β) (ord_t : t.Ordered)
6262
(z : Zipper α β) (lower_bound : α) :
6363
z.prependMap (t.prune_LE ord_t lower_bound) = z.prependMapGE t lower_bound := by
6464
induction t generalizing z
@@ -77,7 +77,7 @@ theorem prepend_prune_LE_eq_prependMapGE [Ord α] (t : Impl α β) (ord_t : t.Or
7777
simp only
7878
apply r_ih
7979

80-
theorem prepend_eq_prependMapGT_self [Ord α] [TransOrd α] (r : Impl α β)
80+
theorem Zipper.prepend_eq_prependMapGT_self [Ord α] [TransOrd α] (r : Impl α β)
8181
(z : Zipper α β) (lower_bound : α) (ord_r : r.Ordered)
8282
(hyp : ∀ e ∈ r.toList, compare lower_bound e.fst = .lt) :
8383
Zipper.prependMap r z = Zipper.prependMapGT r lower_bound z := by
@@ -100,7 +100,7 @@ theorem prepend_eq_prependMapGT_self [Ord α] [TransOrd α] (r : Impl α β)
100100
. rw [Impl.toList_eq_toListModel] at mem
101101
exact mem
102102

103-
theorem prepend_prune_LT_eq_prependMapGT [Ord α] [TransOrd α] (t : Impl α β)
103+
theorem Zipper.prepend_prune_LT_eq_prependMapGT [Ord α] [TransOrd α] (t : Impl α β)
104104
(ord_t : t.Ordered) (z : Zipper α β) (lower_bound : α) :
105105
z.prependMap (t.prune_LT ord_t lower_bound) = z.prependMapGT t lower_bound := by
106106
induction t generalizing z
@@ -277,7 +277,7 @@ public instance {z : Zipper α β} : ToIterator z Id ((a : α) × β a) where
277277
State := Zipper α β
278278
iterMInternal := Iter.toIterM <| Zipper.iter z
279279

280-
public theorem step_Zipper_eq_match {it : IterM (α := Zipper α β) Id ((a : α) × β a)} :
280+
public theorem Zipper.step_eq_match {it : IterM (α := Zipper α β) Id ((a : α) × β a)} :
281281
it.step = ⟨match it.internalState.iter with
282282
| ⟨Zipper.done⟩ => IterStep.done
283283
| ⟨Zipper.cons k v t z⟩ => IterStep.yield { internalState := Zipper.prependMap t z } ⟨k, v⟩,
@@ -297,7 +297,7 @@ public theorem step_Zipper_eq_match {it : IterM (α := Zipper α β) Id ((a : α
297297
case cons k v tree next =>
298298
simp only [Zipper.iter]
299299

300-
public theorem val_step_Zipper_eq_match {α β}
300+
public theorem Zipper.val_step_Zipper_eq_match {α β}
301301
{it : Iter (α := Zipper α β) (Sigma β)} :
302302
it.step.val =
303303
match it.internalState.iter with
@@ -307,7 +307,7 @@ public theorem val_step_Zipper_eq_match {α β}
307307
:= by
308308
rcases it with ⟨z, upper⟩
309309
rw [Iter.step]
310-
rw [step_Zipper_eq_match]
310+
rw [Zipper.step_eq_match]
311311
simp only [Iter.toIterM]
312312
split
313313
· simp only [IterM.Step.toPure, IterStep.mapIterator, Id.run, Zipper.iter]
@@ -326,7 +326,7 @@ public theorem val_step_Zipper_eq_match {α β}
326326
Iter.mk.injEq, Sigma.mk.injEq]
327327
simp_all
328328

329-
public theorem toList_Zipper {α β}
329+
public theorem Zipper.toList_Zipper {α β}
330330
{z : Zipper α β}:
331331
(⟨z⟩ : Iter (Sigma β)).toList =
332332
z.toList := by
@@ -364,7 +364,7 @@ decreasing_by
364364
simp only [Zipper.prependMap_size, Impl.treeSize, Nat.add_lt_add_iff_right, Nat.lt_add_left_iff_pos,
365365
Nat.lt_add_one]
366366

367-
public theorem val_step_map_Zipper_eq_match {α β γ} {f : (a : α) × β a → γ}
367+
public theorem Zipper.val_step_map_Zipper_eq_match {α β γ} {f : (a : α) × β a → γ}
368368
{it : Iter (α := Zipper α β) (Sigma β)} :
369369
(it.map f).step.val =
370370
match it.internalState.iter with
@@ -378,7 +378,7 @@ public theorem val_step_map_Zipper_eq_match {α β γ} {f : (a : α) × β a →
378378
simp [heq] at this
379379
split at this <;> (rename_i heq2 ; simp [heq2, this])
380380

381-
public theorem toList_map_Zipper {α β γ} {f : (a : α) × β a → γ}
381+
public theorem Zipper.toList_map_Zipper {α β γ} {f : (a : α) × β a → γ}
382382
{z : Zipper α β}:
383383
((⟨z⟩ : Iter (Sigma β)).map f).toList =
384384
(z.toList).map f := by
@@ -423,8 +423,8 @@ public theorem toList_map_Zipper {α β γ} {f : (a : α) × β a → γ}
423423
simp only [Zipper.prependMap_size, Impl.treeSize, Nat.add_lt_add_iff_right, Nat.lt_add_left_iff_pos,
424424
Nat.lt_add_one]
425425

426-
public theorem iter_of_tree_internal_state_eq :
427-
(Internal.Zipper.iter_of_tree m).internalState = Zipper.prependMap m .done := by
426+
public theorem Zipper.iter_of_tree_internal_state_eq {m : Impl α β} :
427+
(Zipper.iter_of_tree m).internalState = Zipper.prependMap m .done := by
428428
simp [Zipper.iter_of_tree, Zipper.iter]
429429

430430
end Zipper
@@ -622,58 +622,6 @@ public instance instProductive : Productive (RxcIterator α β cmp) Id :=
622622

623623
end Rxc
624624

625-
section Rcx
626-
@[always_inline]
627-
public def Rcx [Ord α] (t : Impl α β) (lower_bound : α) : Iter (α := Zipper α β) ((a : α) × β a) :=
628-
⟨Zipper.prependMapGE t lower_bound .done⟩
629-
630-
public theorem toList_rcxIter {α β} [Ord α] [TransOrd α]
631-
{t : Impl α β} {t_ord : t.Ordered} {lower_bound : α} :
632-
(Rcx t lower_bound : Iter (Sigma β)).toList =
633-
t.toList.filter (fun e => (compare e.fst lower_bound).isGE) := by
634-
simp only [Rcx]
635-
simp only [toList_Zipper]
636-
rw [← prepend_prune_LE_eq_prependMapGE]
637-
simp only [Zipper.prependMap_toList_eq_concat_toList, Zipper.toList, List.append_nil]
638-
apply Impl.prune_LE_eq_filter
639-
exact t_ord
640-
641-
end Rcx
642-
643-
section Rcc
644-
645-
@[always_inline]
646-
public def Rcc [Ord α] (t : Impl α β) (lower_bound : α) (upper_bound : α) : Iter (α := RxcIterator α β compare) ((a : α) × β a) :=
647-
⟨RxcIterator.mk (Zipper.prependMapGE t lower_bound .done) upper_bound⟩
648-
649-
public theorem toList_rccIter {α β} [Ord α] [TransOrd α]
650-
{t : Impl α β} {t_ord : t.Ordered} {lower_bound upper_bound : α} :
651-
(Rcc t lower_bound upper_bound : Iter (Sigma β)).toList =
652-
t.toList.filter (fun e => (compare e.fst lower_bound).isGE ∧ (compare e.fst upper_bound).isLE) := by
653-
simp only [Rcc, Bool.decide_and, Bool.decide_eq_true]
654-
rw [toList_rxcIter, toList_eq_takeWhile_list]
655-
. conv =>
656-
rhs
657-
lhs
658-
ext x
659-
rw [Bool.and_comm]
660-
rw [← List.filter_filter]
661-
congr 1
662-
rw [← prepend_prune_LE_eq_prependMapGE]
663-
. rw [Zipper.prependMap_toList_eq_concat_toList, Impl.prune_LE_eq_filter]
664-
simp only [Zipper.toList, List.append_nil]
665-
. exact t_ord
666-
. rw [← prepend_prune_LE_eq_prependMapGE]
667-
. simp only [Zipper.prependMap_toList_eq_concat_toList, Zipper.toList, List.append_nil]
668-
rw [Impl.prune_LE_eq_filter]
669-
apply List.Pairwise.filter
670-
simp only [Impl.Ordered] at t_ord
671-
rw [Impl.toList_eq_toListModel]
672-
exact t_ord
673-
. exact t_ord
674-
675-
end Rcc
676-
677625
section Rxo
678626

679627
public structure RxoIterator (α : Type u) (β : α → Type v) (cmp : α → α → Ordering) where
@@ -758,4 +706,56 @@ public theorem step_rxoIterator_eq_match {cmp : α → α → Ordering} {it : It
758706

759707
end Rxo
760708

709+
section Rcx
710+
@[always_inline]
711+
public def Rcx [Ord α] (t : Impl α β) (lower_bound : α) : Iter (α := Zipper α β) ((a : α) × β a) :=
712+
⟨Zipper.prependMapGE t lower_bound .done⟩
713+
714+
public theorem toList_rcxIter {α β} [Ord α] [TransOrd α]
715+
{t : Impl α β} {t_ord : t.Ordered} {lower_bound : α} :
716+
(Rcx t lower_bound : Iter (Sigma β)).toList =
717+
t.toList.filter (fun e => (compare e.fst lower_bound).isGE) := by
718+
simp only [Rcx]
719+
simp only [Zipper.toList_Zipper]
720+
rw [← Zipper.prepend_prune_LE_eq_prependMapGE]
721+
simp only [Zipper.prependMap_toList_eq_concat_toList, Zipper.toList, List.append_nil]
722+
apply Impl.prune_LE_eq_filter
723+
exact t_ord
724+
725+
end Rcx
726+
727+
section Rcc
728+
729+
@[always_inline]
730+
public def Rcc [Ord α] (t : Impl α β) (lower_bound : α) (upper_bound : α) : Iter (α := RxcIterator α β compare) ((a : α) × β a) :=
731+
⟨RxcIterator.mk (Zipper.prependMapGE t lower_bound .done) upper_bound⟩
732+
733+
public theorem toList_rccIter {α β} [Ord α] [TransOrd α]
734+
{t : Impl α β} {t_ord : t.Ordered} {lower_bound upper_bound : α} :
735+
(Rcc t lower_bound upper_bound : Iter (Sigma β)).toList =
736+
t.toList.filter (fun e => (compare e.fst lower_bound).isGE ∧ (compare e.fst upper_bound).isLE) := by
737+
simp only [Rcc, Bool.decide_and, Bool.decide_eq_true]
738+
rw [toList_rxcIter, toList_eq_takeWhile_list]
739+
. conv =>
740+
rhs
741+
lhs
742+
ext x
743+
rw [Bool.and_comm]
744+
rw [← List.filter_filter]
745+
congr 1
746+
rw [← Zipper.prepend_prune_LE_eq_prependMapGE]
747+
. rw [Zipper.prependMap_toList_eq_concat_toList, Impl.prune_LE_eq_filter]
748+
simp only [Zipper.toList, List.append_nil]
749+
. exact t_ord
750+
. rw [← Zipper.prepend_prune_LE_eq_prependMapGE]
751+
. simp only [Zipper.prependMap_toList_eq_concat_toList, Zipper.toList, List.append_nil]
752+
rw [Impl.prune_LE_eq_filter]
753+
apply List.Pairwise.filter
754+
simp only [Impl.Ordered] at t_ord
755+
rw [Impl.toList_eq_toListModel]
756+
exact t_ord
757+
. exact t_ord
758+
759+
end Rcc
760+
761761
end Std.DTreeMap.Internal
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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.DTreeMap.Raw.Iterator
10+
11+
namespace Std.DTreeMap
12+
open Std.Iterators
13+
14+
@[inline]
15+
public def iter {α : Type u} {β : α → Type v} [Ord α] (m : DTreeMap α β) :=
16+
Raw.iter ⟨m.inner⟩
17+
18+
@[inline]
19+
public def keysIter {α : Type u} {β : α → Type u} [Ord α] (m : Raw α β) :=
20+
Raw.keysIter ⟨m.inner⟩
21+
22+
@[inline]
23+
public def valuesIter {α : Type u} {β : Type u} [Ord α](m : Raw α (fun _ => β)) :=
24+
Raw.valuesIter ⟨m.inner⟩
25+
26+
public theorem iter_toList [Ord α] (m : Raw α β) :
27+
m.iter.toList = m.toList := Raw.iter_toList ⟨m.inner⟩
28+
29+
public theorem keysIter_toList {α β} [Ord α] (m : Raw α β) :
30+
m.keysIter.toList = m.keys := Raw.keysIter_toList ⟨m.inner⟩
31+
32+
public theorem valuesIter_toList {α β} [Ord α] (m : Raw α (fun _ => β)) :
33+
m.valuesIter.toList = m.values := Raw.valuesIter_toList ⟨m.inner⟩
34+
35+
end Std.DTreeMap

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,27 @@ public def valuesIter {α : Type u} {β : Type u} [Ord α](m : Raw α (fun _ =>
2525

2626
public theorem iter_toList [Ord α] (m : Raw α β) :
2727
m.iter.toList = m.toList := by
28-
rw [Internal.toList_Zipper]
28+
rw [Internal.Zipper.toList_Zipper]
2929
unfold iter
3030
simp [Raw.toList]
3131
rw [Internal.Zipper.iter_of_tree_toList_eq_zipper_prependMap_toList]
3232

3333
public theorem keysIter_toList {α β} [Ord α] (m : Raw α β) :
3434
m.keysIter.toList = m.keys := by
3535
unfold keysIter
36-
rw [Internal.toList_map_Zipper]
36+
rw [Internal.Zipper.toList_map_Zipper]
3737
unfold iter
38-
rw [Internal.iter_of_tree_internal_state_eq]
38+
rw [Internal.Zipper.iter_of_tree_internal_state_eq]
3939
rw [Internal.Zipper.prependMap_done_toList_eq_toList]
4040
unfold keys
4141
simp [Internal.Impl.map_fst_toList_eq_keys]
4242

4343
public theorem valuesIter_toList {α β} [Ord α] (m : Raw α (fun _ => β)) :
4444
m.valuesIter.toList = m.values := by
4545
unfold valuesIter
46-
rw [Internal.toList_map_Zipper]
46+
rw [Internal.Zipper.toList_map_Zipper]
4747
unfold iter
48-
rw [Internal.iter_of_tree_internal_state_eq]
48+
rw [Internal.Zipper.iter_of_tree_internal_state_eq]
4949
rw [Internal.Zipper.prependMap_done_toList_eq_toList]
5050
unfold values
5151
simp [Std.DTreeMap.Internal.Impl.values_eq_map_snd, Internal.Impl.toList_eq_toListModel]

0 commit comments

Comments
 (0)