Skip to content

Commit c0e9549

Browse files
committed
Add Raw iterators
1 parent 87b9017 commit c0e9549

File tree

5 files changed

+349
-198
lines changed

5 files changed

+349
-198
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,11 @@ def toListModel : Impl α β → List ((a : α) × β a)
7878
@[simp] theorem toListModel_inner {sz k v l r} :
7979
(.inner sz k v l r : Impl α β).toListModel = l.toListModel ++ ⟨k, v⟩ :: r.toListModel := rfl
8080

81+
/--
82+
Computes the size of the tree. Used for verification of iterators.
83+
-/
84+
def treeSize : Internal.Impl α β → Nat
85+
| .leaf => 0
86+
| .inner _ _ _ l r => 1 + l.treeSize + treeSize r
87+
8188
end Std.DTreeMap.Internal.Impl

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

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7945,4 +7945,114 @@ end Const
79457945

79467946
end map
79477947

7948+
theorem prune_LE_eq_filter {α β} [Ord α] [TransOrd α] (t : Internal.Impl α β) (ord_t : t.Ordered) (lower_bound : α) :
7949+
(t.prune_LE ord_t lower_bound).toList = t.toList.filter (fun e => (compare e.fst lower_bound).isGE) := by
7950+
induction t
7951+
case leaf =>
7952+
simp only [prune_LE, toList_eq_toListModel, toListModel_leaf, List.filter_nil]
7953+
case inner _ k v l r l_ih r_ih =>
7954+
simp only [prune_LE, toList_eq_toListModel, toListModel_inner, List.filter_append]
7955+
generalize heq : compare lower_bound k = x
7956+
cases x
7957+
case lt =>
7958+
simp only [toListModel_inner]
7959+
specialize l_ih (Internal.Impl.Ordered.left ord_t)
7960+
rw [toList_eq_toListModel] at l_ih
7961+
simp only [l_ih, toList_eq_toListModel, List.filter, List.append_cancel_left_eq]
7962+
rw [OrientedOrd.eq_swap, Ordering.swap_eq_lt] at heq
7963+
simp only [heq, Ordering.isGE_gt, List.cons.injEq, true_and]
7964+
symm
7965+
apply List.filter_eq_self.2
7966+
intro a mem
7967+
apply Ordering.isGE_of_eq_gt
7968+
apply TransCmp.gt_trans ?_ heq
7969+
rw [OrientedOrd.eq_swap, Ordering.swap_eq_gt]
7970+
exact Internal.Impl.Ordered.compare_right ord_t mem
7971+
case eq =>
7972+
simp only [toListModel_inner, toListModel_leaf, List.nil_append, List.filter]
7973+
rw [OrientedCmp.eq_comm] at heq
7974+
simp only [heq, Ordering.isGE_eq]
7975+
suffices new_goal : List.filter (fun e => (compare e.fst lower_bound).isGE) l.toListModel = [] from by
7976+
simp only [new_goal, List.nil_append, List.cons.injEq, true_and]
7977+
symm
7978+
apply List.filter_eq_self.2
7979+
intro a mem
7980+
apply Ordering.isGE_of_eq_gt
7981+
apply TransCmp.gt_of_gt_of_eq ?_ heq
7982+
rw [OrientedOrd.eq_swap, Ordering.swap_eq_gt]
7983+
apply Internal.Impl.Ordered.compare_right ord_t mem
7984+
rw [List.filter_eq_nil_iff]
7985+
intro a mem
7986+
simp only [Bool.not_eq_true, Ordering.isGE_eq_false]
7987+
exact TransCmp.lt_of_lt_of_eq (Internal.Impl.Ordered.compare_left ord_t mem) heq
7988+
case gt =>
7989+
simp only [List.filter]
7990+
rw [OrientedOrd.eq_swap, Ordering.swap_eq_gt] at heq
7991+
rw [heq]
7992+
suffices new_goal : List.filter (fun e => (compare e.fst lower_bound).isGE) l.toListModel = [] from by
7993+
simp only [new_goal, Ordering.isGE_lt, List.nil_append]
7994+
simp only [toList_eq_toListModel] at r_ih
7995+
apply r_ih
7996+
rw [List.filter_eq_nil_iff]
7997+
intro a mem
7998+
simp only [Bool.not_eq_true, Ordering.isGE_eq_false]
7999+
exact TransCmp.lt_trans (Internal.Impl.Ordered.compare_left ord_t mem) heq
8000+
8001+
theorem prune_LT_eq_filter {α β} [Ord α] [TransOrd α] (t : Internal.Impl α β) (ord_t : t.Ordered) (lower_bound : α) :
8002+
(t.prune_LT ord_t lower_bound).toList = t.toList.filter (fun e => (compare e.fst lower_bound).isGT) := by
8003+
induction t
8004+
case leaf =>
8005+
simp only [prune_LT, toList_eq_toListModel, toListModel_leaf, List.filter_nil]
8006+
case inner _ k v l r l_ih r_ih =>
8007+
simp only [prune_LT, toList_eq_toListModel, toListModel_inner, List.filter_append]
8008+
generalize heq : compare lower_bound k = x
8009+
cases x
8010+
case lt =>
8011+
simp
8012+
specialize l_ih (Internal.Impl.Ordered.left ord_t)
8013+
rw [toList_eq_toListModel] at l_ih
8014+
simp only [l_ih, toList_eq_toListModel, List.filter, List.append_cancel_left_eq]
8015+
rw [OrientedOrd.eq_swap, Ordering.swap_eq_lt] at heq
8016+
simp only [heq, Ordering.isGT_gt, List.cons.injEq, true_and]
8017+
symm
8018+
apply List.filter_eq_self.2
8019+
intro a mem
8020+
rw [Ordering.isGT_iff_eq_gt]
8021+
apply TransCmp.gt_trans ?_ heq
8022+
rw [OrientedOrd.eq_swap, Ordering.swap_eq_gt]
8023+
exact Internal.Impl.Ordered.compare_right ord_t mem
8024+
case eq =>
8025+
simp only [List.filter]
8026+
rw [OrientedCmp.eq_comm] at heq
8027+
rw [heq]
8028+
suffices new_goal : List.filter (fun e => (compare e.fst lower_bound).isGT) l.toListModel = [] ∧
8029+
List.filter (fun e => (compare e.fst lower_bound).isGT) r.toListModel = r.toListModel from by
8030+
simp only [new_goal, Ordering.isGT_eq, List.nil_append]
8031+
apply And.intro
8032+
. rw [List.filter_eq_nil_iff]
8033+
intro a mem
8034+
simp only [Ordering.isGT_iff_eq_gt, ← Ordering.isLE_iff_ne_gt]
8035+
apply TransOrd.isLE_trans _ (Ordering.isLE_of_eq_eq heq)
8036+
apply Ordering.isLE_of_eq_lt
8037+
exact Internal.Impl.Ordered.compare_left ord_t mem
8038+
. apply List.filter_eq_self.2
8039+
intro a mem
8040+
rw [Ordering.isGT_iff_eq_gt]
8041+
apply TransCmp.gt_of_gt_of_eq ?_ heq
8042+
rw [OrientedOrd.eq_swap, Ordering.swap_eq_gt]
8043+
exact Internal.Impl.Ordered.compare_right ord_t mem
8044+
case gt =>
8045+
simp only [List.filter]
8046+
rw [OrientedOrd.eq_swap] at heq
8047+
rw [Ordering.swap_eq_gt] at heq
8048+
simp only [heq, Ordering.isGT_lt]
8049+
specialize r_ih (Ordered.right ord_t)
8050+
rw [toList_eq_toListModel] at r_ih
8051+
simp only [r_ih, toList_eq_toListModel, List.self_eq_append_left, List.filter_eq_nil_iff,
8052+
Ordering.isGT_iff_eq_gt]
8053+
intro a mem
8054+
rw [← Ordering.isLE_iff_ne_gt]
8055+
apply Ordering.isLE_of_eq_lt
8056+
exact TransCmp.lt_trans (Internal.Impl.Ordered.compare_left ord_t mem) heq
8057+
79488058
end Std.DTreeMap.Internal.Impl

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,38 @@ def insertMany! [Ord α] {ρ : Type w} [ForIn Id ρ ((a : α) × β a)] (t : Imp
512512
r := ⟨r.val.insert! a b, fun h₀ h₁ => h₁ _ _ _ (r.2 h₀ h₁)⟩
513513
return r
514514

515+
/-!
516+
## Operations for verification of iterators and slices
517+
-/
518+
519+
/--
520+
Removes all elements with key less than or equal to `lower_bound`.
521+
Does not modify size information stored in the tree.
522+
-/
523+
def prune_LE {α β} [Ord α] (t : Internal.Impl α β)
524+
(ord_t : t.Ordered) (lower_bound : α) : Internal.Impl α β :=
525+
match t with
526+
| .leaf => .leaf
527+
| .inner sz k v l r =>
528+
match compare lower_bound k with
529+
| .lt => .inner sz k v (l.prune_LE (Internal.Impl.Ordered.left ord_t) lower_bound) r
530+
| .eq => .inner sz k v .leaf r
531+
| .gt => r.prune_LE (Internal.Impl.Ordered.right ord_t) lower_bound
532+
533+
/--
534+
Removes all elements with key less than to `lower_bound`.
535+
Does not modify size information stored in the tree.
536+
-/
537+
def prune_LT {α β} [Ord α] (t : Internal.Impl α β)
538+
(ord_t : t.Ordered) (lower_bound : α) : Internal.Impl α β :=
539+
match t with
540+
| .leaf => .leaf
541+
| .inner sz k v l r =>
542+
match compare lower_bound k with
543+
| .lt => .inner sz k v (l.prune_LT (Internal.Impl.Ordered.left ord_t) lower_bound) r
544+
| .eq => r
545+
| .gt => r.prune_LT (Internal.Impl.Ordered.right ord_t) lower_bound
546+
515547
namespace Const
516548

517549
variable {β : Type v}

0 commit comments

Comments
 (0)