Skip to content

Commit af99105

Browse files
committed
prove toList characterization of Roc and Rcc slices
1 parent 135b94f commit af99105

File tree

1 file changed

+28
-244
lines changed

1 file changed

+28
-244
lines changed

src/Std/Data/DTreeMap/Slice.lean

Lines changed: 28 additions & 244 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ public import Init.Data.Slice
1313
public import Init.Data.Range.Polymorphic.Basic
1414
public import Std.Data.DTreeMap.Lemmas
1515

16-
set_option linter.all true
17-
1816
namespace Std.DTreeMap
1917
open Std.Iterators
2018

@@ -131,33 +129,6 @@ private theorem Option.filter_eq_none_iff_all {α} {o : Option α} {p : α → B
131129
o.filter p = none ↔ o.all (! p ·) := by
132130
simp [Option.all_eq_true]
133131

134-
public theorem step_rxcIterator_eq_match' {α β} {cmp : α → α → Ordering} [TransCmp cmp] {it : IterM (α := RxcIterator α β cmp) Id _} :
135-
it.step = pure (match h : it.internalState.next.filter (fun e => (cmp e.fst it.internalState.bound).isLE) with
136-
| some next =>
137-
.yield ⟨it.internalState.treeMap, it.internalState.treeMap.getEntryGT? next.fst, it.internalState.bound, (by apply getEntryGE?_getEntryGT?_eq_some)⟩ next
138-
(by simpa [IterM.IsPlausibleStep, Iterator.IsPlausibleStep, and_comm (a := (_ = true))] using h)
139-
| none =>
140-
haveI : ∀ e, it.internalState.next = some e → cmp e.fst it.internalState.bound = .gt := by
141-
simpa using h
142-
.done (by simpa only [IterM.IsPlausibleStep, Iterator.IsPlausibleStep, Option.filter_eq_none_iff_all,
143-
Ordering.not_isLE_eq_isGT, ← Ordering.isGT_iff_eq_gt, Bool.decide_eq_true] using h)) := by
144-
rw [step_rxcIterator_eq_match]
145-
sorry
146-
147-
public theorem val_step_rxcIterator_eq_match' {α β} {cmp : α → α → Ordering} [TransCmp cmp] {it : Iter (α := RxcIterator α β cmp) _} :
148-
it.step.val = (match it.internalState.next.filter (fun e => (cmp e.fst it.internalState.bound).isLE) with
149-
| some next =>
150-
.yield ⟨it.internalState.treeMap, it.internalState.treeMap.getEntryGT? next.fst, it.internalState.bound, (by apply getEntryGE?_getEntryGT?_eq_some)⟩ next
151-
| none =>
152-
.done ) := by
153-
sorry
154-
155-
-- public theorem RxcIterator.induct {α β} {cmp : α → α → Ordering} [TransCmp cmp]
156-
-- (motive : Iter (α := RxcIterator α β cmp) _ → Sort x)
157-
-- (step : (it : Iter (α := RxcIterator α β cmp) _) →
158-
-- (ih_yield : ))
159-
-- {it : Iter (α := RxcIterator α β cmp) _}
160-
161132
end Rxc
162133

163134
public structure RocSliceData (α : Type u) (β : α → Type v)
@@ -210,29 +181,15 @@ public theorem step_iter_rocSlice_eq_match {α β} {cmp : α → α → Ordering
210181
split <;> simp_all
211182

212183
public theorem val_step_iter_rocSlice_eq_match {α β} {cmp : α → α → Ordering} [TransCmp cmp] {t : DTreeMap α β cmp} {a b : α} :
213-
t[a<...=b].iter.step.val = (match t.getEntryGT? a with
214-
| some next =>
215-
if (cmp next.fst b).isLE then
216-
.yield ⟨t, t.getEntryGT? next.fst, b, (by apply getEntryGE?_getEntryGT?_eq_some)⟩ next
217-
else
218-
.done
219-
| none =>
220-
.done) := by
221-
rw [step_iter_rocSlice_eq_match]
222-
split <;> split <;> simp_all
223-
224-
public theorem val_step_iter_rocSlice_eq_match' {α β} {cmp : α → α → Ordering} [TransCmp cmp] {t : DTreeMap α β cmp} {a b : α} :
225184
t[a<...=b].iter.step.val = (match (t.getEntryGT? a).filter (fun e => (cmp e.fst b).isLE) with
226185
| some next =>
227186
.yield ⟨t, t.getEntryGT? next.fst, b, (by apply getEntryGE?_getEntryGT?_eq_some)⟩ next
228187
| none =>
229188
.done) := by
230-
rw [val_step_iter_rocSlice_eq_match]
231-
split
232-
· split <;> simp_all [Option.filter_some]
233-
· simp_all
189+
rw [step_iter_rocSlice_eq_match]
190+
split <;> split <;> simp_all [Option.filter_some]
234191

235-
public theorem filter_toList_eq_match_lt_and_isLE {α β} {cmp : α → α → Ordering} [TransCmp cmp] {t : DTreeMap α β cmp} {a b : α} :
192+
public theorem filter_toList_lt_and_isLE {α β} {cmp : α → α → Ordering} [TransCmp cmp] {t : DTreeMap α β cmp} {a b : α} :
236193
t.toList.filter (fun e => cmp a e.fst = .lt ∧ (cmp e.fst b).isLE) = (match (t.getEntryGT? a).filter (fun e => (cmp e.fst b).isLE) with
237194
| some next =>
238195
next :: t.toList.filter (fun e => cmp next.fst e.fst = .lt ∧ (cmp e.fst b).isLE)
@@ -247,8 +204,8 @@ public theorem filter_toList_eq_match_lt_and_isLE {α β} {cmp : α → α → O
247204
public theorem toList_rocSlice_eq_filter_toList {α β} {cmp : α → α → Ordering} [TransCmp cmp]
248205
{t : DTreeMap α β cmp} {a b : α} :
249206
t[a<...=b].toList = t.toList.filter (fun e => cmp a e.fst = .lt ∧ (cmp e.fst b).isLE) := by
250-
rw [filter_toList_eq_match_lt_and_isLE, Slice.toList_eq_toList_iter, Iter.toList_eq_match_step,
251-
val_step_iter_rocSlice_eq_match']
207+
rw [filter_toList_lt_and_isLE, Slice.toList_eq_toList_iter, Iter.toList_eq_match_step,
208+
val_step_iter_rocSlice_eq_match]
252209
cases Option.filter (fun e => (cmp e.fst b).isLE) (t.getEntryGT? a)
253210
· simp
254211
· rename_i next
@@ -259,90 +216,16 @@ public theorem toList_rocSlice_eq_filter_toList {α β} {cmp : α → α → Ord
259216
termination_by (t.toList.filter (fun e => cmp a e.fst = .lt ∧ (cmp e.fst b).isLE)).length
260217
decreasing_by
261218
rename_i next hnext
262-
simp
263219
simp only [getEntryGT?_eq_some_iff, Option.filter_eq_some_iff] at hnext
264220
apply List.length_filter_strict_mono (a := next)
265-
· simp
221+
· simp only [decide_eq_true_eq, and_imp]
266222
intro e hne heb
267223
exact ⟨TransCmp.lt_trans hnext.1.2.1 hne, heb⟩
268224
· exact hnext.1.1
269-
· simp [ReflCmp.compare_self]
270-
· simp
225+
· simp only [ReflCmp.compare_self, reduceCtorEq, false_and, decide_false, not_false_eq_true]
226+
· simp only [decide_eq_true_eq]
271227
exact ⟨hnext.1.2.1, hnext.2
272228

273-
theorem toList_rocSlice_eq_toList_rocSlice {α β} {cmp : α → α → Ordering} [TransCmp cmp] {t t': DTreeMap α β cmp} {a b a' b' : α}
274-
(h : ∀ c, (c ∈ t.toList ∧ cmp a c.fst = .lt ∧ (cmp c.fst b).isLE) ↔ (c ∈ t'.toList ∧ cmp a' c.fst = .lt ∧ (cmp c.fst b').isLE)) :
275-
t[a<...=b].toList = t'[a'<...=b'].toList := by
276-
-- simp only [Slice.toList_eq_toList_iter, Slice.iter_eq_toIteratorIter, ToIterator.iter_eq,
277-
-- RccSlice.Internal.iterM, Rcc.Sliceable.mkSlice, IterM.toIter]
278-
simp only [Slice.toList_eq_toList_iter]
279-
generalize hi : Slice.iter (Roc.Sliceable.mkSlice t a<...=b) = it
280-
change Iter (α := RxcIterator α β cmp) _ at it
281-
induction it using Iter.inductSteps generalizing a b a' b' with | step it ihy ihs
282-
rw [← hi]
283-
simp only [Slice.iter_eq_toIteratorIter, ToIterator.iter_eq, RocSlice.Internal.iterM, Roc.Sliceable.mkSlice, IterM.toIter]
284-
rw [Iter.toList_eq_match_step, Iter.toList_eq_match_step]
285-
rw [val_step_rxcIterator_eq_match', val_step_rxcIterator_eq_match']
286-
simp only
287-
have : (t.getEntryGT? a).filter (fun e => (cmp e.fst b).isLE) =
288-
(t'.getEntryGT? a').filter (fun e => (cmp e.fst b').isLE) := by
289-
ext e
290-
simp [getEntryGT?_eq_some_iff]
291-
specialize h
292-
constructor
293-
· intro h'
294-
have he := (h e).mp ⟨h'.1.1, h'.1.2.1, h'.2
295-
refine ⟨⟨he.1, he.2.1, ?_⟩, he.2.2
296-
intro k hk hk'
297-
by_cases hkb : (cmp k b').isLE
298-
· simp only [← map_fst_toList_eq_keys, List.mem_map] at hk
299-
obtain ⟨f, hfm, rfl⟩ := hk
300-
have hf := (h f).mpr ⟨hfm, hk', hkb⟩
301-
apply h'.1.2.2
302-
· simpa only [← map_fst_toList_eq_keys] using List.mem_map_of_mem hf.1
303-
· exact hf.2.1
304-
· refine TransCmp.isLE_trans he.2.2 ?_
305-
apply Ordering.isLE_of_eq_lt
306-
simpa [OrientedCmp.gt_iff_lt] using hkb
307-
· intro h'
308-
have he := (h e).mpr ⟨h'.1.1, h'.1.2.1, h'.2
309-
refine ⟨⟨he.1, he.2.1, ?_⟩, he.2.2
310-
intro k hk hk'
311-
by_cases hkb : (cmp k b).isLE
312-
· simp only [← map_fst_toList_eq_keys, List.mem_map] at hk
313-
obtain ⟨f, hfm, rfl⟩ := hk
314-
have hf := (h f).mp ⟨hfm, hk', hkb⟩
315-
apply h'.1.2.2
316-
· simpa only [← map_fst_toList_eq_keys] using List.mem_map_of_mem hf.1
317-
· exact hf.2.1
318-
· refine TransCmp.isLE_trans he.2.2 ?_
319-
apply Ordering.isLE_of_eq_lt
320-
simpa [OrientedCmp.gt_iff_lt] using hkb
321-
simp [this]
322-
cases heq : Option.filter (fun e => (cmp e.fst b').isLE) (t'.getEntryGT? a')
323-
· simp
324-
· rename_i out
325-
simp
326-
simp [Slice.iter_eq_toIteratorIter, ToIterator.iter_eq, RocSlice.Internal.iterM, Roc.Sliceable.mkSlice,
327-
← hi] at ihy
328-
apply ihy (a := out.fst) (a' := out.fst) (b := b) (b' := b') (out := out)
329-
· simp [Iter.IsPlausibleStep, IterM.IsPlausibleStep, Iterator.IsPlausibleStep, Iter.toIterM,
330-
IterM.toIter]
331-
rw [← this, Option.filter_eq_some_iff, and_comm] at heq
332-
simpa using heq
333-
· intro e
334-
have heq' := heq
335-
rw [← this, Option.filter_eq_some_iff, getEntryGT?_eq_some_iff] at heq'
336-
rw [Option.filter_eq_some_iff, getEntryGT?_eq_some_iff] at heq
337-
constructor
338-
· intro h'
339-
have he := (h e).mp ⟨h'.1, TransCmp.lt_trans heq'.1.2.1 h'.2.1, h'.2.2
340-
exact ⟨he.1, h'.2.1, he.2.2
341-
· intro h'
342-
have he := (h e).mpr ⟨h'.1, TransCmp.lt_trans heq.1.2.1 h'.2.1, h'.2.2
343-
exact ⟨he.1, h'.2.1, he.2.2
344-
· rfl
345-
346229
public structure RccSliceData (α : Type u) (β : α → Type v)
347230
(cmp : α → α → Ordering := by exact compare) where
348231
treeMap : DTreeMap α β cmp
@@ -393,136 +276,37 @@ public theorem step_iter_rccSlice_eq_match {α β} {cmp : α → α → Ordering
393276
split <;> simp_all
394277

395278
public theorem val_step_iter_rccSlice_eq_match {α β} {cmp : α → α → Ordering} [TransCmp cmp] {t : DTreeMap α β cmp} {a b : α} :
396-
t[a...=b].iter.step.val = (match t.getEntryGE? a with
397-
| some next =>
398-
if (cmp next.fst b).isLE then
399-
.yield ⟨t, t.getEntryGT? next.fst, b, (by apply getEntryGE?_getEntryGT?_eq_some)⟩ next
400-
else
401-
.done
402-
| none =>
403-
.done) := by
404-
rw [step_iter_rccSlice_eq_match]
405-
split <;> split <;> simp_all
406-
407-
public theorem val_step_iter_rccSlice_eq_match' {α β} {cmp : α → α → Ordering} [TransCmp cmp] {t : DTreeMap α β cmp} {a b : α} :
408279
t[a...=b].iter.step.val = (match (t.getEntryGE? a).filter (fun e => (cmp e.fst b).isLE) with
409280
| some next =>
410281
.yield ⟨t, t.getEntryGT? next.fst, b, (by apply getEntryGE?_getEntryGT?_eq_some)⟩ next
411282
| none =>
412283
.done) := by
413-
rw [val_step_iter_rccSlice_eq_match]
414-
split
415-
· split <;> simp_all [Option.filter_some]
416-
· simp_all
284+
rw [step_iter_rccSlice_eq_match]
285+
split <;> split <;> simp_all [Option.filter_some]
417286

418-
public theorem filter_toList_eq_match {α β} {cmp : α → α → Ordering} [TransCmp cmp] {t : DTreeMap α β cmp} {a b : α} :
287+
public theorem filter_toList_isLE_and_isLE {α β} {cmp : α → α → Ordering} [TransCmp cmp] {t : DTreeMap α β cmp} {a b : α} :
419288
t.toList.filter (fun e => (cmp a e.fst).isLE ∧ (cmp e.fst b).isLE) = (match (t.getEntryGE? a).filter (fun e => (cmp e.fst b).isLE) with
420289
| some next =>
421290
next :: t.toList.filter (fun e => cmp next.fst e.fst = .lt ∧ (cmp e.fst b).isLE)
422291
| none =>
423292
[]) := by
424-
sorry
425-
426-
theorem toList_rccSlice_eq_toList_rccSlice {α β} {cmp : α → α → Ordering} [TransCmp cmp] {t t': DTreeMap α β cmp} {a b a' b' : α}
427-
(h : ∀ c, (c ∈ t.toList ∧ (cmp a c.fst).isLE ∧ (cmp c.fst b).isLE) ↔ (c ∈ t'.toList ∧ (cmp a' c.fst).isLE ∧ (cmp c.fst b').isLE)) :
428-
t[a...=b].toList = t'[a'...=b'].toList := by
429-
simp only [Slice.toList_eq_toList_iter]
430-
simp only [Slice.iter_eq_toIteratorIter, ToIterator.iter_eq, RccSlice.Internal.iterM, Rcc.Sliceable.mkSlice, IterM.toIter]
431-
rw [Iter.toList_eq_match_step, Iter.toList_eq_match_step]
432-
rw [val_step_rxcIterator_eq_match', val_step_rxcIterator_eq_match']
433-
simp only
434-
have : (t.getEntryGE? a).filter (fun e => (cmp e.fst b).isLE) =
435-
(t'.getEntryGE? a').filter (fun e => (cmp e.fst b').isLE) := by
436-
ext e
437-
simp [getEntryGE?_eq_some_iff]
438-
specialize h
439-
constructor
440-
· intro h'
441-
have he := (h e).mp ⟨h'.1.1, h'.1.2.1, h'.2
442-
refine ⟨⟨he.1, he.2.1, ?_⟩, he.2.2
443-
intro k hk hk'
444-
by_cases hkb : (cmp k b').isLE
445-
· simp only [← map_fst_toList_eq_keys, List.mem_map] at hk
446-
obtain ⟨f, hfm, rfl⟩ := hk
447-
have hf := (h f).mpr ⟨hfm, hk', hkb⟩
448-
apply h'.1.2.2
449-
· simpa only [← map_fst_toList_eq_keys] using List.mem_map_of_mem hf.1
450-
· exact hf.2.1
451-
· refine TransCmp.isLE_trans he.2.2 ?_
452-
apply Ordering.isLE_of_eq_lt
453-
simpa [OrientedCmp.gt_iff_lt] using hkb
454-
· intro h'
455-
have he := (h e).mpr ⟨h'.1.1, h'.1.2.1, h'.2
456-
refine ⟨⟨he.1, he.2.1, ?_⟩, he.2.2
457-
intro k hk hk'
458-
by_cases hkb : (cmp k b).isLE
459-
· simp only [← map_fst_toList_eq_keys, List.mem_map] at hk
460-
obtain ⟨f, hfm, rfl⟩ := hk
461-
have hf := (h f).mp ⟨hfm, hk', hkb⟩
462-
apply h'.1.2.2
463-
· simpa only [← map_fst_toList_eq_keys] using List.mem_map_of_mem hf.1
464-
· exact hf.2.1
465-
· refine TransCmp.isLE_trans he.2.2 ?_
466-
apply Ordering.isLE_of_eq_lt
467-
simpa [OrientedCmp.gt_iff_lt] using hkb
468-
simp [this]
469-
cases heq : Option.filter (fun e => (cmp e.fst b').isLE) (t'.getEntryGE? a')
470-
· simp
471-
· rename_i out
472-
simp
473-
have hroc := @toList_rocSlice_eq_toList_rocSlice
474-
simp [Slice.toList_eq_toList_iter, Slice.iter_eq_toIteratorIter, ToIterator.iter_eq, RocSlice.Internal.iterM,
475-
Roc.Sliceable.mkSlice] at hroc
476-
apply hroc
477-
· intro e
478-
have heq' := heq
479-
rw [← this, Option.filter_eq_some_iff, getEntryGE?_eq_some_iff] at heq'
480-
rw [Option.filter_eq_some_iff, getEntryGE?_eq_some_iff] at heq
481-
constructor
482-
· intro h'
483-
have he := (h e).mp ⟨h'.1, TransCmp.isLE_trans heq'.1.2.1 (Ordering.isLE_of_eq_lt h'.2.1), h'.2.2
484-
exact ⟨he.1, h'.2.1, he.2.2
485-
· intro h'
486-
have he := (h e).mpr ⟨h'.1, TransCmp.isLE_trans heq.1.2.1 (Ordering.isLE_of_eq_lt h'.2.1), h'.2.2
487-
exact ⟨he.1, h'.2.1, he.2.2
488-
489-
theorem rccSlice_toList_eq_filter_toList {α β} {cmp : α → α → Ordering} [TransCmp cmp] {t : DTreeMap α β cmp} {a b : α} :
490-
t[a...=b].toList = t.toList.filter (fun e => (cmp a e.fst).isLE ∧ (cmp e.fst b).isLE)
491-
:= by
492-
let n := t.size
493-
have hn : t.size ≤ n := by exact Nat.le_refl _
494-
clear_value n
495-
induction n generalizing t
496-
· simp only [length_toList, Nat.le_zero_eq] at hn
497-
rw [← beq_iff_eq, ← isEmpty_eq_size_eq_zero, ← isEmpty_toList] at hn
498-
-- non-confluence: List.isEmpty_iff, DTreeMap.isEmpty_toList applied to t.toList.isEmpty
499-
simp only [List.isEmpty_iff] at hn
500-
simp only [Slice.toList_eq_toList_iter]
501-
rw [Iter.toList_eq_match_step]
502-
simp only [val_step_iter_rccSlice_eq_match]
503-
cases h' : t.getEntryGE? a
504-
· simp [hn]
505-
· simp [getEntryGE?_eq_some_iff, hn] at h'
506-
· rename_i n ih
507-
by_cases hn : t.size = n + 1; rotate_left
508-
· exact ih (by omega)
509-
simp [Slice.toList_eq_toList_iter]
510-
rw [Iter.toList_eq_match_step]
511-
simp [val_step_iter_rccSlice_eq_match]
512-
match h : t.getEntryGE? a with
513-
| some next =>
514-
simp [getEntryGE?_eq_some_iff] at h
515-
let t' := t.erase next.fst
516-
have : t'.size ≤ n := by
517-
simp only [t', size_erase, ← mem_iff_contains, mem_of_mem_toList h.1, ↓reduceIte]
518-
omega
519-
specialize ih this
520-
simp only [Slice.toList_eq_toList_iter, Slice.iter_eq_toIteratorIter, ToIterator.iter_eq,
521-
RccSlice.Internal.iterM, Rcc.Sliceable.mkSlice, IterM.toIter] at ih
522-
by_cases h' : (cmp next.fst b).isLE
523-
· simp [h']
524-
525-
| none => simp
293+
induction hs : t.size
294+
· rw [← beq_iff_eq, ← DTreeMap.isEmpty_eq_size_eq_zero] at hs
295+
have : t.toList = [] := by simpa [← DTreeMap.isEmpty_toList] using hs
296+
simp [getEntryGE?_of_isEmpty hs, this]
297+
· sorry
526298

299+
public theorem toList_rccSlice_eq_filter_toList {α β} {cmp : α → α → Ordering} [TransCmp cmp]
300+
{t : DTreeMap α β cmp} {a b : α} :
301+
t[a...=b].toList = t.toList.filter (fun e => (cmp a e.fst).isLE ∧ (cmp e.fst b).isLE) := by
302+
rw [filter_toList_isLE_and_isLE, Slice.toList_eq_toList_iter, Iter.toList_eq_match_step,
303+
val_step_iter_rccSlice_eq_match]
304+
cases Option.filter (fun e => (cmp e.fst b).isLE) (t.getEntryGE? a)
305+
· simp
306+
· rename_i next
307+
suffices h : t[next.fst<...=b].toList = t.toList.filter (fun e => cmp next.fst e.fst = .lt ∧ (cmp e.fst b).isLE) by
308+
simpa [Slice.toList_eq_toList_iter, Slice.iter_eq_toIteratorIter, ToIterator.iter_eq,
309+
RocSlice.Internal.iterM, Roc.Sliceable.mkSlice] using h
310+
apply toList_rocSlice_eq_filter_toList
527311

528312
end Std.DTreeMap

0 commit comments

Comments
 (0)