@@ -20,6 +20,76 @@ public def Internal.Impl.treeSize : Internal.Impl α β → Nat
2020| .leaf => 0
2121| .inner _ _ _ l r => 1 + l.treeSize + treeSize r
2222
23+ public def Internal.Impl.prune_LE {α β} [Ord α] (t : Internal.Impl α β) (ord_t : t.Ordered) (lower_bound : α) : Internal.Impl α β :=
24+ match t with
25+ | .leaf => .leaf
26+ | .inner sz k v l r =>
27+ match compare lower_bound k with
28+ | .lt => .inner sz k v (l.prune_LE (Internal.Impl.Ordered.left ord_t) lower_bound) r
29+ | .eq => .inner sz k v .leaf r
30+ | .gt => r.prune_LE (Internal.Impl.Ordered.right ord_t) lower_bound
31+
32+ theorem Internal.Impl.prune_LE_filter {α β} [Ord α] [TransOrd α] (t : Internal.Impl α β) (ord_t : t.Ordered) (lower_bound : α) :
33+ (t.prune_LE ord_t lower_bound).toList = t.toList.filter (fun e => (compare e.fst lower_bound).isGE) := by
34+ induction t
35+ case leaf =>
36+ simp [toList_eq_toListModel, prune_LE]
37+ case inner _ k v l r l_ih r_ih =>
38+ simp [toList_eq_toListModel, prune_LE]
39+ generalize heq : compare lower_bound k = x
40+ cases x
41+ case lt =>
42+ simp
43+ specialize l_ih (Internal.Impl.Ordered.left ord_t)
44+ rw [toList_eq_toListModel] at l_ih
45+ simp [l_ih, toList_eq_toListModel]
46+ simp [List.filter]
47+ rw [OrientedOrd.eq_swap] at heq
48+ rw [Ordering.swap_eq_lt] at heq
49+ simp [heq]
50+ symm
51+ apply List.filter_eq_self.2
52+ intro a mem
53+ apply Ordering.isGE_of_eq_gt
54+ apply TransCmp.gt_trans ?_ heq
55+ rw [OrientedOrd.eq_swap]
56+ rw [Ordering.swap_eq_gt]
57+ exact Internal.Impl.Ordered.compare_right ord_t mem
58+ case eq =>
59+ simp [List.filter]
60+ rw [OrientedCmp.eq_comm] at heq
61+ simp [heq]
62+ suffices new_goal : List.filter (fun e => (compare e.fst lower_bound).isGE) l.toListModel = [] from by
63+ simp [new_goal]
64+ symm
65+ apply List.filter_eq_self.2
66+ intro a mem
67+ apply Ordering.isGE_of_eq_gt
68+ apply TransCmp.gt_of_gt_of_eq ?_ heq
69+ rw [OrientedOrd.eq_swap]
70+ rw [Ordering.swap_eq_gt]
71+ apply Internal.Impl.Ordered.compare_right ord_t mem
72+ rw [List.filter_eq_nil_iff]
73+ intro a mem
74+ simp
75+ apply TransCmp.lt_of_lt_of_eq
76+ exact Internal.Impl.Ordered.compare_left ord_t mem
77+ exact heq
78+ case gt =>
79+ simp [List.filter]
80+ rw [OrientedOrd.eq_swap] at heq
81+ rw [Ordering.swap_eq_gt] at heq
82+ simp [heq]
83+ suffices new_goal : List.filter (fun e => (compare e.fst lower_bound).isGE) l.toListModel = [] from by
84+ simp [new_goal]
85+ simp [toList_eq_toListModel] at r_ih
86+ apply r_ih
87+ rw [List.filter_eq_nil_iff]
88+ intro a mem
89+ simp
90+ apply TransCmp.lt_trans ?_ heq
91+ exact Internal.Impl.Ordered.compare_left ord_t mem
92+
2393section MapIterator
2494public inductive Zipper (α : Type u) (β : α → Type v) where
2595 | done
@@ -42,6 +112,36 @@ public def Zipper.prependMap : Internal.Impl α β → Zipper α β → Zipper
42112 | .leaf, it => it
43113 | .inner _ k v l r, it => prependMap l (.cons k v r it)
44114
115+ def Zipper.prependMapGE [Ord α] (t : Internal.Impl α β) (lower_bound : α) (it : Zipper α β) : Zipper α β :=
116+ match t with
117+ | .leaf => it
118+ | .inner _ k v l r =>
119+ match compare lower_bound k with
120+ | .lt => prependMapGE l lower_bound (Zipper.cons k v r it)
121+ | .eq => .cons k v r it
122+ | .gt => prependMapGE r lower_bound it
123+
124+ theorem prepend_eq_prependGE [Ord α] (t : Internal.Impl α β) (ord_t : t.Ordered) (z : Zipper α β) (lower_bound : α) :
125+ z.prependMap (t.prune_LE ord_t lower_bound) = z.prependMapGE t lower_bound := by
126+ induction t generalizing z
127+ case leaf =>
128+ simp [Internal.Impl.prune_LE]
129+ simp [Zipper.prependMap]
130+ simp [Zipper.prependMapGE]
131+ case inner _ k v l r l_ih r_ih =>
132+ simp [Zipper.prependMapGE]
133+ simp [Internal.Impl.prune_LE]
134+ generalize heq : compare lower_bound k = x
135+ cases x
136+ case lt =>
137+ simp
138+ apply l_ih
139+ case eq =>
140+ simp [Zipper.prependMap]
141+ case gt =>
142+ simp
143+ apply r_ih
144+
45145public theorem Zipper.prependMap_to_list (t : Internal.Impl α β) (it : Zipper α β) : (Zipper.prependMap t it).toList = t.toList ++ it.toList := by
46146 induction t generalizing it
47147 case leaf =>
@@ -101,6 +201,14 @@ theorem Zipper.prependMap_invariant [Ord α] [TransOrd α] {t : Internal.Impl α
101201 specialize hyp k₁ in_z k₂ (by simp [mem₂])
102202 exact hyp
103203
204+ theorem Zipper.prependMap_done_invariant [Ord α] [TransOrd α] {t : Internal.Impl α β}
205+ {ord_t : t.Ordered} :
206+ (Zipper.prependMap t .done).Ordered := by
207+ apply Zipper.prependMap_invariant
208+ . exact ord_t
209+ . simp [Ordered, Zipper.toList]
210+ simp [Zipper.toList]
211+
104212public theorem Zipper.ordered_of_cons_ordered [Ord α] [TransOrd α] {t : Internal.Impl α β}
105213 {z : Zipper α β} : (Zipper.cons k v t z).Ordered → z.Ordered := by
106214 intro hyp
@@ -125,7 +233,7 @@ public def Zipper.step : Zipper α β → IterStep (IterM (α := Zipper α β) I
125233 | .cons k v t it=>
126234 .yield ⟨it.prependMap t⟩ ⟨k, v⟩
127235
128- public instance : Iterator (Zipper α β ) Id ((a : α) × β a) where
236+ public instance : Iterator (Zipper α β) Id ((a : α) × β a) where
129237 IsPlausibleStep it step := it.internalState.step = step
130238 step it := ⟨it.internalState.step, rfl⟩
131239
@@ -176,6 +284,10 @@ public instance {z : Zipper α β} : ToIterator z Id ((a : α) × β a) where
176284 State := Zipper α β
177285 iterMInternal := Iter.toIterM <| Zipper.iter z
178286
287+ def test : (DTreeMap.Raw Nat (fun _ => Nat) compare) := .ofList [⟨0 , 0 ⟩, ⟨1 , 1 ⟩, ⟨100 , 3 ⟩, ⟨101 , 4 ⟩]
288+
289+ #eval ! (Zipper.iter_of_tree (test.inner.prune_LE sorry 0 )).toList
290+
179291public theorem step_Zipper_eq_match {it : IterM (α := Zipper α β) Id ((a : α) × β a)} :
180292 it.step = ⟨match it.internalState.iter with
181293 | ⟨Zipper.done⟩ => IterStep.done
@@ -270,9 +382,6 @@ public structure RxcIterator (α : Type u) (β : α → Type v) (cmp : α → α
270382
271383variable {α : Type u} {β : α → Type v}
272384
273- public def RxcIterator.inBounds {cmp : α → α → Ordering} (it : RxcIterator α β cmp) (k : α) : Bool :=
274- (cmp k it.upper).isLE
275-
276385public def RxcIterator.step {cmp : α → α → Ordering} : RxcIterator α β cmp → IterStep (IterM (α := RxcIterator α β cmp) Id ((a : α) × β a)) ((a : α) × β a)
277386 | ⟨.done, _⟩ => .done
278387 | ⟨.cons k v t it, upper⟩ =>
@@ -368,8 +477,6 @@ public instance {s : RicSlice α β cmp} : ToIterator s Id ((a : α) × β a) wh
368477 State := RxcIterator α β cmp
369478 iterMInternal := RicSlice.Internal.iterM s
370479
371- def test : DTreeMap.Raw Nat (fun _ => Nat) compare := .ofList [⟨0 , 0 ⟩, ⟨1 , 1 ⟩, ⟨100 , 3 ⟩, ⟨101 , 4 ⟩]
372-
373480public theorem val_step_rxcIterator_eq_match {α β} [Ord α]
374481 {it : Iter (α := RxcIterator α β compare) (Sigma β)} :
375482 it.step.val =
@@ -387,7 +494,7 @@ public theorem val_step_rxcIterator_eq_match {α β} [Ord α]
387494 · simp [IterM.Step.toPure, IterStep.mapIterator, Id.run]
388495 · split <;> simp [IterM.Step.toPure, IterStep.mapIterator, Id.run, IterM.toIter]
389496
390- public theorem toList_rxcIter {α β} {_ : Ord α}
497+ public theorem toList_rxcIter {α β} [ Ord α]
391498 {z : Zipper α β} {bound : α} :
392499 (⟨RxcIterator.mk (cmp := compare) z bound⟩ : Iter (Sigma β)).toList =
393500 z.toList.takeWhile (fun e => (compare e.fst bound).isLE) := by
@@ -438,6 +545,24 @@ public theorem toList_eq_takeWhile {α β} [Ord α] [TransOrd α] {z : Zipper α
438545 simp [Zipper.Ordered] at z_ord
439546 apply toList_eq_takeWhile_list
440547 exact z_ord
441-
442548end Rxc
549+
550+ section Rcx
551+
552+ @[always_inline]
553+ public def Rcx [Ord α] (t : Internal.Impl α β) (lower_bound : α) : Iter (α := Zipper α β) ((a : α) × β a) :=
554+ ⟨Zipper.prependMapGE t lower_bound .done⟩
555+
556+ public theorem toList_rcxIter {α β} [Ord α] [TransOrd α]
557+ {t : Internal.Impl α β} {t_ord : t.Ordered} {lower_bound : α} :
558+ (Rcx t lower_bound : Iter (Sigma β)).toList =
559+ t.toList.filter (fun e => (compare e.fst lower_bound).isGE) := by
560+ simp [Rcx]
561+ simp [toList_Zipper]
562+ rw [← prepend_eq_prependGE]
563+ simp [Zipper.prependMap_to_list, Zipper.toList]
564+ apply Internal.Impl.prune_LE_filter
565+ exact t_ord
566+
567+ end Rcx
443568end Std.DTreeMap
0 commit comments