Skip to content

Commit 87b9017

Browse files
committed
Add rxo iterator
1 parent 9f131d4 commit 87b9017

File tree

1 file changed

+86
-6
lines changed

1 file changed

+86
-6
lines changed

src/Std/Data/DTreeMap/Slice.lean

Lines changed: 86 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,7 @@ theorem Internal.Impl.prune_LT_filter {α β} [Ord α] [TransOrd α] (t : Intern
105105
specialize l_ih (Internal.Impl.Ordered.left ord_t)
106106
rw [toList_eq_toListModel] at l_ih
107107
simp only [l_ih, toList_eq_toListModel, List.filter, List.append_cancel_left_eq]
108-
rw [OrientedOrd.eq_swap] at heq
109-
rw [Ordering.swap_eq_lt] at heq
108+
rw [OrientedOrd.eq_swap, Ordering.swap_eq_lt] at heq
110109
simp only [heq, Ordering.isGT_gt, List.cons.injEq, true_and]
111110
symm
112111
apply List.filter_eq_self.2
@@ -399,7 +398,6 @@ public instance {z : Zipper α β} : ToIterator z Id ((a : α) × β a) where
399398
State := Zipper α β
400399
iterMInternal := Iter.toIterM <| Zipper.iter z
401400

402-
403401
public theorem step_Zipper_eq_match {it : IterM (α := Zipper α β) Id ((a : α) × β a)} :
404402
it.step = ⟨match it.internalState.iter with
405403
| ⟨Zipper.done⟩ => IterStep.done
@@ -408,8 +406,6 @@ public theorem step_Zipper_eq_match {it : IterM (α := Zipper α β) Id ((a : α
408406
simp only [IterM.IsPlausibleStep, Iterator.IsPlausibleStep, Zipper.step]; split; all_goals (rename_i heq; simp only [Zipper.iter,
409407
heq]))⟩ := by
410408
simp only [IterM.step, Iterator.step, Zipper.step]
411-
ext
412-
congr 1
413409
congr 1
414410
cases it
415411
next =>
@@ -489,6 +485,7 @@ decreasing_by
489485
simp only [Zipper.prependMap_size, Internal.Impl.treeSize, Nat.add_lt_add_iff_right, Nat.lt_add_left_iff_pos,
490486
Nat.lt_add_one]
491487
end ZipperIterator
488+
492489
section Rxc
493490

494491
public structure RxcIterator (α : Type u) (β : α → Type v) (cmp : α → α → Ordering) where
@@ -663,7 +660,6 @@ public theorem toList_eq_takeWhile {α β} [Ord α] [TransOrd α] {z : Zipper α
663660
end Rxc
664661

665662
section Rcx
666-
667663
@[always_inline]
668664
public def Rcx [Ord α] (t : Internal.Impl α β) (lower_bound : α) : Iter (α := Zipper α β) ((a : α) × β a) :=
669665
⟨Zipper.prependMapGE t lower_bound .done⟩
@@ -716,4 +712,88 @@ public theorem toList_rccIter {α β} [Ord α] [TransOrd α]
716712

717713
end Rcc
718714

715+
section Rxo
716+
717+
public structure RxoIterator (α : Type u) (β : α → Type v) (cmp : α → α → Ordering) where
718+
iter : Zipper α β
719+
upper : α
720+
721+
variable {α : Type u} {β : α → Type v}
722+
723+
public def RxoIterator.step {cmp : α → α → Ordering} : RxoIterator α β cmp → IterStep (IterM (α := RxoIterator α β cmp) Id ((a : α) × β a)) ((a : α) × β a)
724+
| ⟨.done, _⟩ => .done
725+
| ⟨.cons k v t it, upper⟩ =>
726+
if (cmp k upper).isLT then
727+
.yield ⟨it.prependMap t, upper⟩ ⟨k, v⟩
728+
else
729+
.done
730+
731+
public instance : Iterator (RxoIterator α β cmp) Id ((a : α) × β a) where
732+
IsPlausibleStep it step := it.internalState.step = step
733+
step it := ⟨it.internalState.step, rfl⟩
734+
735+
def RxoIterator.instFinitenessRelation : FinitenessRelation (RxoIterator α β cmp) Id where
736+
rel t' t := t'.internalState.iter.size < t.internalState.iter.size
737+
wf := by
738+
apply InvImage.wf
739+
exact Nat.lt_wfRel.wf
740+
subrelation {it it'} h := by
741+
obtain ⟨w, h, h'⟩ := h
742+
simp only [IterM.IsPlausibleStep, Iterator.IsPlausibleStep] at h'
743+
cases w
744+
case skip it'' =>
745+
cases h
746+
simp only [RxoIterator.step] at h'
747+
split at h'
748+
any_goals contradiction
749+
. split at h'
750+
all_goals contradiction
751+
case done =>
752+
cases h
753+
case yield it'' out =>
754+
cases h
755+
simp only [RxoIterator.step] at h'
756+
split at h'
757+
case h_1 =>
758+
contradiction
759+
case h_2 h2 =>
760+
split at h'
761+
case isFalse =>
762+
contradiction
763+
case isTrue heq =>
764+
simp at h'
765+
simp only [h2, ← h'.1, Zipper.prependMap_size, Zipper.size, Nat.add_lt_add_iff_right,
766+
Nat.lt_add_left_iff_pos, Nat.lt_add_one]
767+
768+
@[no_expose]
769+
public instance Rxo.instFinite : Finite (RxcIterator α β cmp) Id :=
770+
.of_finitenessRelation instFinitenessRelation
771+
772+
public theorem step_rxoIterator_eq_match {cmp : α → α → Ordering} {it : IterM (α := RxoIterator α β cmp) Id ((a : α) × β a)} :
773+
it.step = ⟨match it.internalState.iter with
774+
| Zipper.done => IterStep.done
775+
| Zipper.cons k v t z =>
776+
if (cmp k it.internalState.upper).isLT = true then
777+
IterStep.yield { internalState := { iter := Zipper.prependMap t z, upper := it.internalState.upper } } ⟨k, v⟩
778+
else IterStep.done,
779+
(by simp only [IterM.IsPlausibleStep, Iterator.IsPlausibleStep, RxoIterator.step]; split; all_goals (rename_i heq; simp only [heq]))⟩ := by
780+
simp only [IterM.step, Iterator.step, RxoIterator.step]
781+
ext
782+
congr 1
783+
congr 1
784+
cases it
785+
next =>
786+
rename_i internalState
787+
simp
788+
cases internalState
789+
case mk iter upper =>
790+
simp
791+
cases iter
792+
case done =>
793+
simp only
794+
case cons k v tree next =>
795+
simp only
796+
797+
end Rxo
798+
719799
end Std.DTreeMap

0 commit comments

Comments
 (0)