@@ -1529,6 +1529,40 @@ theorem toArray_eq_toArray_map {t : Impl α β} :
15291529
15301530end Const
15311531
1532+ /-!
1533+ ### interSmallerFn
1534+ -/
1535+
1536+ theorem WF.interSmallerFn {_ : Ord α} [TransOrd α] [BEq α] (m₁ : Impl α β) (m₂ : Impl α β)
1537+ (hm₂ :m₂.WF) (k : α) : (m₁.interSmallerFn ⟨m₂,hm₂.balanced⟩ k).1 .WF := by
1538+ rw [Impl.interSmallerFn]
1539+ split
1540+ · exact WF.insert hm₂
1541+ · exact hm₂
1542+
1543+ theorem ordered_inter [Ord α] [TransOrd α] {l₁ l₂ : Impl α β} (hlb : l₁.Balanced)
1544+ (hlo : l₁.Ordered) : (l₁.inter l₂ hlb).Ordered := by
1545+ rw [inter]
1546+ split
1547+ · exact ordered_filter hlo
1548+ · rw [interSmaller]
1549+ rw [foldl_eq_foldl]
1550+ generalize l₂.toListModel = l₂
1551+ suffices ∀ {start}, start.val.Ordered → (List.foldl (fun acc p => interSmallerFn l₁ acc p.fst) start l₂).val.Ordered by
1552+ apply this
1553+ apply ordered_empty
1554+ intro s swf
1555+ induction l₂ generalizing s
1556+ case nil => simp [swf]
1557+ case cons h t ih =>
1558+ simp only [List.foldl_cons]
1559+ apply ih
1560+ simp only [Impl.interSmallerFn]
1561+ split
1562+ · apply ordered_insert
1563+ · exact swf
1564+ · exact swf
1565+
15321566/-!
15331567## Deducing that well-formed trees are ordered
15341568-/
@@ -1549,6 +1583,7 @@ theorem WF.ordered [Ord α] [TransOrd α] {l : Impl α β} (h : WF l) : l.Ordere
15491583 · exact ordered_filter ‹_›
15501584 · exact ordered_mergeWith ‹_› ‹_›
15511585 · exact Const.ordered_mergeWith ‹_› ‹_›
1586+ · exact ordered_inter ‹_› ‹_›
15521587
15531588/-!
15541589## Deducing that additional operations are well-formed
@@ -1995,6 +2030,91 @@ theorem WF.filter! {_ : Ord α} {t : Impl α β} {f : (a : α) → β a → Bool
19952030 rw [← filter_eq_filter! (h := h.balanced)]
19962031 exact h.filter
19972032
2033+ theorem toListModel_interSmallerFn {_ : Ord α} [TransOrd α] [BEq α] [LawfulBEqOrd α] (m sofar : Impl α β) (h₁ : m.WF) (h₂ : sofar.WF)
2034+ (l : List ((a : α) × β a))
2035+ (k : α) (hml : List.Perm (sofar.toListModel) l) :
2036+ List.Perm (toListModel ((interSmallerFn m ⟨sofar,h₂.balanced⟩ k)).1 )
2037+ (List.interSmallerFn (toListModel m) l k) := by
2038+ rw [interSmallerFn, List.interSmallerFn]
2039+ split
2040+ case h_1 _ val heq =>
2041+ simp only
2042+ rw [getEntry?_eq_getEntry?] at heq
2043+ simp only [heq]
2044+ apply List.Perm.trans
2045+ · apply toListModel_insert
2046+ · exact h₂.ordered
2047+ · apply insertEntry_of_perm
2048+ · apply Ordered.distinctKeys h₂.ordered
2049+ · exact hml
2050+ · exact h₁.ordered
2051+ case h_2 heq =>
2052+ simp only
2053+ rw [getEntry?_eq_getEntry?] at heq
2054+ simp only [heq, hml]
2055+ exact h₁.ordered
2056+
2057+ /-!
2058+ ### interSmaller
2059+ -/
2060+
2061+ theorem toListModel_interSmaller {_ : Ord α} [TransOrd α] [BEq α] [LawfulBEqOrd α]
2062+ (m₁ : Impl α β) (m₂ : Impl α β) (hm₁ : m₁.WF) :
2063+ List.Perm (toListModel (m₁.interSmaller m₂))
2064+ (List.interSmaller (toListModel m₁) (toListModel m₂)) := by
2065+ rw [interSmaller, foldl_eq_foldl, List.interSmaller]
2066+ generalize toListModel m₂ = l
2067+ suffices ∀ m l', (hm : m.WF) → List.Perm (toListModel m) l' →
2068+ List.Perm (toListModel (List.foldl (fun a b => interSmallerFn m₁ a b.fst) ⟨m, hm.balanced⟩ l).val)
2069+ (List.foldl (fun sofar kv => List.interSmallerFn (toListModel m₁) sofar kv.fst) l' l) by
2070+ simpa using this empty [] WF.empty (by simp)
2071+ intro m l' hm hml'
2072+ induction l generalizing m l' with
2073+ | nil => simpa
2074+ | cons ht tl ih =>
2075+ rw [List.foldl_cons, List.foldl_cons]
2076+ exact ih _ _ (by apply WF.interSmallerFn _ _ hm) (toListModel_interSmallerFn _ _ hm₁ hm _ _ hml')
2077+
2078+ /-!
2079+ ### inter
2080+ -/
2081+
2082+ theorem toListModel_inter {_ : Ord α} [TransOrd α] [BEq α] [LawfulBEqOrd α]
2083+ (m₁ : Impl α β) (m₂ : Impl α β) (hm₁ : m₁.WF) (hm₂ : m₂.WF) :
2084+ List.Perm (toListModel (m₁.inter m₂ hm₁.balanced)) ((toListModel m₁).filter fun p => containsKey p.1 (toListModel m₂)) := by
2085+ rw [inter]
2086+ split
2087+ · rw [toListModel_filter]
2088+ conv =>
2089+ lhs
2090+ lhs
2091+ ext e
2092+ rw [@contains_eq_containsKey α β _ _ _ _ e.fst m₂ hm₂.ordered]
2093+ · apply List.Perm.trans (toListModel_interSmaller _ _ hm₁) (List.interSmaller_perm_filter _ _ hm₁.ordered.distinctKeys)
2094+
2095+ /-!
2096+ ### inter!
2097+ -/
2098+
2099+ theorem inter_eq_inter ! [Ord α] {l₁ l₂: Impl α β} {h} :
2100+ (inter l₁ l₂ h) = inter! l₁ l₂ := by
2101+ rw [inter, inter!]
2102+ split
2103+ · rw [filter_eq_filter!]
2104+ · rfl
2105+
2106+ theorem toListModel_inter ! {_ : Ord α} [TransOrd α] [BEq α] [LawfulBEqOrd α]
2107+ (m₁ : Impl α β) (m₂ : Impl α β) (hm₁ : m₁.WF) (hm₂ : m₂.WF) :
2108+ List.Perm (toListModel (m₁.inter! m₂)) ((toListModel m₁).filter fun p => containsKey p.1 (toListModel m₂)) := by
2109+ rw [← @inter_eq_inter! _ _ _ _ _ hm₁.balanced]
2110+ exact toListModel_inter _ _ hm₁ hm₂
2111+
2112+ theorem WF.inter ! {_ : Ord α} [TransOrd α]
2113+ {m₁ m₂ : Impl α β} (wh₁ : m₁.WF) :
2114+ (inter! m₁ m₂).WF := by
2115+ rw [← @inter_eq_inter! _ _ _ _ _ wh₁.balanced]
2116+ exact WF.inter wh₁
2117+
19982118/-!
19992119### map
20002120-/
0 commit comments