Skip to content

Commit e77b528

Browse files
authored
perf: reduce elaboration time and proof size of tree map internals (#7459)
There are several things done here: 1. Use the modified `simp_to_model` which already exists in hash maps. This version of `simp_to_model` allows specifying the query operations to use in addition to the modifying operations. This is mostly to improve elaboration time and actually increases olean size. 2. Instead of proving `toListModel_balance` directly, we write `toListModel_balanceₘ` and use that instead (this saves ~3 MB). 3. Use `fun_cases` and `dsimp` instead of `rw [x.eq_def]` more frequently in `Balancing.olean` (this saves a bit over 2 MB). 4. Mark `updateCell` and other functions dependent on it as `noncomputable`. The main problem with `updateCell` is how other functions, in particular `glue`, get recursively inlined, which blows the size of the IR (this saves ~1 MB). 5. Instead of using `simp_to_model` to prove results on `insert!`, `erase!`, etc., `simpa`s are used now, e.g. `simpa only [insert_eq_insert!] using isEmpty_insert h`. This mainly improves elaboration time although the olean size also goes down by ~0.3 MB.
1 parent 6153474 commit e77b528

File tree

6 files changed

+742
-698
lines changed

6 files changed

+742
-698
lines changed

src/Std/Data/DHashMap/Internal/RawLemmas.lean

+2-2
Original file line numberDiff line numberDiff line change
@@ -2702,7 +2702,7 @@ theorem getKey!_modify_self (h : m.1.WF) [Inhabited α] {k : α} {f : β k →
27022702
theorem getKey_modify (h : m.1.WF) [Inhabited α] {k k' : α} {f : β k → β k}
27032703
(hc : (m.modify k f).contains k') :
27042704
(m.modify k f).getKey k' hc =
2705-
if heq : k == k' then
2705+
if k == k' then
27062706
k
27072707
else
27082708
haveI h' : m.contains k' := by rwa [contains_modify _ h] at hc
@@ -2827,7 +2827,7 @@ theorem getKey!_modify_self (h : m.1.WF) [Inhabited α] {k : α} {f : β → β}
28272827
theorem getKey_modify (h : m.1.WF) [Inhabited α] {k k' : α} {f : β → β}
28282828
(hc : (Const.modify m k f).contains k') :
28292829
(Const.modify m k f).getKey k' hc =
2830-
if heq : k == k' then
2830+
if k == k' then
28312831
k
28322832
else
28332833
haveI h' : m.contains k' := by rwa [contains_modify _ h] at hc

src/Std/Data/DHashMap/RawLemmas.lean

+6-9
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ private def baseNames : Array Name :=
4343
``getKey?_eq, ``getKey_eq, ``getKey!_eq, ``getKeyD_eq,
4444
``insertMany_eq, ``Const.insertMany_eq, ``Const.insertManyIfNewUnit_eq,
4545
``ofList_eq, ``Const.ofList_eq, ``Const.unitOfList_eq,
46-
``alter_eq, ``Const.alter_eq, ``modify_eq, ``Const.modify_eq,
47-
``Subtype.eta]
46+
``alter_eq, ``Const.alter_eq, ``modify_eq, ``Const.modify_eq]
4847

4948
/-- Internal implementation detail of the hash map -/
5049
scoped syntax "simp_to_raw" ("using" term)? : tactic
@@ -55,7 +54,7 @@ macro_rules
5554
| `(tactic| simp_to_raw $[using $using?]?) => do
5655
`(tactic|
5756
(try simp (discharger := with_reducible wf_trivial) only [$[$(Array.map Lean.mkIdent baseNames):term],*]
58-
$[apply $(using?.toArray):term];*)
57+
$[with_reducible apply $(using?.toArray):term];*)
5958
<;> with_reducible try wf_trivial)
6059

6160
end Internal.Raw
@@ -2857,15 +2856,14 @@ theorem getKey!_modify_self [LawfulBEq α] [Inhabited α] {k : α} {f : β k →
28572856
simp_to_raw using Raw₀.getKey!_modify_self
28582857

28592858
theorem getKey_modify [LawfulBEq α] [Inhabited α] {k k' : α} {f : β k → β k}
2860-
(h : m.WF) {hc : k' ∈ m.modify k f} :
2859+
(h : m.WF) : {hc : k' ∈ m.modify k f}
28612860
(m.modify k f).getKey k' hc =
28622861
if k == k' then
28632862
k
28642863
else
28652864
haveI h' : k' ∈ m := mem_modify h |>.mp hc
28662865
m.getKey k' h' := by
2867-
simp only [mem_iff_contains] at hc
2868-
revert hc
2866+
simp only [mem_iff_contains]
28692867
simp_to_raw using Raw₀.getKey_modify
28702868

28712869
@[simp]
@@ -3003,15 +3001,14 @@ theorem getKey!_modify_self [EquivBEq α] [LawfulHashable α] [Inhabited α] {k
30033001
simp_to_raw using Raw₀.Const.getKey!_modify_self
30043002

30053003
theorem getKey_modify [EquivBEq α] [LawfulHashable α] [Inhabited α] {k k' : α} {f : β → β}
3006-
(h : m.WF) {hc : k' ∈ Const.modify m k f} :
3004+
(h : m.WF) : {hc : k' ∈ Const.modify m k f}
30073005
(Const.modify m k f).getKey k' hc =
30083006
if k == k' then
30093007
k
30103008
else
30113009
haveI h' : k' ∈ m := mem_modify h |>.mp hc
30123010
m.getKey k' h' := by
3013-
simp only [mem_iff_contains] at hc
3014-
revert hc
3011+
simp only [mem_iff_contains]
30153012
simp_to_raw using Raw₀.Const.getKey_modify
30163013

30173014
@[simp]

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

+40-51
Original file line numberDiff line numberDiff line change
@@ -530,8 +530,7 @@ theorem balance!_eq_balanceₘ {k v} {l r : Impl α β} (hlb : l.Balanced) (hrb
530530
(hlr : BalanceLErasePrecond l.size r.size ∨ BalanceLErasePrecond r.size l.size) :
531531
balance! k v l r = balanceₘ k v l r := by
532532
cases k, v, l, r using balance!.fun_cases
533-
all_goals
534-
simp only [balance!, balanceₘ]
533+
all_goals dsimp only [balance!, balanceₘ]
535534
· rfl
536535
· split <;> simp_all [Std.Internal.tree_tac]
537536
· split <;> simp_all only [Std.Internal.tree_tac]
@@ -655,7 +654,7 @@ theorem balanced_rotateL (k v l rs rk rv rl rr) (hl : l.Balanced)
655654
(hlr : BalanceLErasePrecond l.size rs ∨ BalanceLErasePrecond rs l.size)
656655
(hh : rs > delta * l.size) :
657656
(rotateL k v l rk rv rl rr : Impl α β).Balanced := by
658-
cases k, v, l, rk, rv, rl, rr using rotateL.fun_cases <;> simp only [rotateL]
657+
fun_cases rotateL k v l rk rv rl rr <;> dsimp only [rotateL]
659658
· split
660659
· next h =>
661660
exact balanced_singleL _ _ _ _ _ _ _ _ hl hr hlr hh h
@@ -669,7 +668,7 @@ theorem balanced_rotateR (k v ls lk lv ll lr r) (hl : (Impl.inner ls lk lv ll lr
669668
(hr : r.Balanced) (hlr : BalanceLErasePrecond ls r.size ∨ BalanceLErasePrecond r.size ls)
670669
(hh : ls > delta * r.size) :
671670
(rotateR k v lk lv ll lr r : Impl α β).Balanced := by
672-
cases k, v, lk, lv, ll, lr, r using rotateR.fun_cases <;> simp only [rotateR]
671+
fun_cases rotateR k v lk lv ll lr r <;> dsimp only [rotateR]
673672
· split
674673
· next h =>
675674
exact balanced_singleR k v _ _ _ _ _ _ hl hr hlr hh h
@@ -681,89 +680,79 @@ theorem balanced_rotateR (k v ls lk lv ll lr r) (hl : (Impl.inner ls lk lv ll lr
681680

682681
theorem balanceL_eq_balanceLErase {k : α} {v : β k} {l r : Impl α β} {hlb hrb hlr} :
683682
balanceL k v l r hlb hrb hlr = balanceLErase k v l r hlb hrb hlr.erase := by
684-
rw [balanceL.eq_def, balanceLErase.eq_def]
683+
fun_cases balanceL k v l r hlb hrb hlr
684+
all_goals dsimp only [balanceL, balanceLErase]
685+
contradiction
685686
split
686-
· dsimp only
687-
split
688-
all_goals dsimp only
689-
contradiction
690-
· dsimp only
691-
split
692-
all_goals dsimp only
693-
split
694-
· split
695-
· dsimp only
696-
· contradiction
697-
· contradiction
698-
· rfl
687+
· split <;> contradiction
688+
· rfl
699689

700690
theorem balanceLErase_eq_balanceL! {k : α} {v : β k} {l r : Impl α β} {hlb hrb hlr} :
701691
balanceLErase k v l r hlb hrb hlr = balanceL! k v l r := by
702-
rw [balanceLErase.eq_def, balanceL!.eq_def]
703-
repeat' (split; dsimp)
704-
all_goals try contradiction
705-
all_goals simp_all [-Nat.not_lt]
692+
fun_cases balanceL! k v l r
693+
all_goals dsimp only [balanceLErase, balanceL!]
694+
all_goals simp only [*]
695+
all_goals dsimp only [dreduceDIte, dreduceIte]
696+
all_goals contradiction
706697

707698
theorem balanceL!_eq_balance! {k : α} {v : β k} {l r : Impl α β} (hlb : l.Balanced)
708699
(hrb : r.Balanced) (hlr : BalanceLErasePrecond l.size r.size) :
709700
balanceL! k v l r = balance! k v l r := by
710701
cases k, v, l, r using balance!.fun_cases
711-
all_goals
712-
simp only [balanceL!, balance!, *, if_true, if_false, true_and, size_inner, size_leaf]
702+
all_goals dsimp only [balanceL!, balance!]
703+
all_goals try simp only [*]
704+
all_goals try dsimp only [dreduceDIte, dreduceIte]
713705
all_goals try rfl
714706
all_goals try contradiction
715707
all_goals try (exfalso; tree_tac; done)
716-
all_goals congr; tree_tac
708+
congr; tree_tac
717709

718710
theorem balanceR_eq_balanceRErase {k : α} {v : β k} {l r : Impl α β} {hlb hrb hlr} :
719711
balanceR k v l r hlb hrb hlr = balanceRErase k v l r hlb hrb hlr.erase := by
720-
rw [balanceR.eq_def, balanceRErase.eq_def]
712+
fun_cases balanceR k v l r hlb hrb hlr
713+
all_goals dsimp only [balanceR, balanceRErase]
714+
contradiction
721715
split
722-
· dsimp only
723-
split
724-
all_goals dsimp only
725-
contradiction
726-
· dsimp only
727-
split
728-
all_goals dsimp only
729-
split
730-
· split
731-
· dsimp only
732-
· contradiction
733-
· contradiction
734-
· rfl
716+
· split <;> contradiction
717+
· rfl
735718

736719
theorem balanceRErase_eq_balanceR! {k : α} {v : β k} {l r : Impl α β} {hlb hrb hlr} :
737720
balanceRErase k v l r hlb hrb hlr = balanceR! k v l r := by
738-
rw [balanceRErase.eq_def, balanceR!.eq_def]
739-
repeat' (split; dsimp)
740-
all_goals try contradiction
741-
all_goals simp_all [-Nat.not_lt]
721+
fun_cases balanceR! k v l r
722+
all_goals dsimp only [balanceRErase, balanceR!]
723+
all_goals simp only [*]
724+
all_goals dsimp only [dreduceDIte, dreduceIte]
725+
all_goals contradiction
742726

743727
theorem balanceR!_eq_balance! {k : α} {v : β k} {l r : Impl α β} (hlb : l.Balanced)
744728
(hrb : r.Balanced) (hlr : BalanceLErasePrecond r.size l.size) :
745729
balanceR! k v l r = balance! k v l r := by
746730
cases k, v, l, r using balance!.fun_cases
747-
all_goals
748-
simp only [balanceR!, balance!, *, if_true, if_false, true_and, size_inner, size_leaf]
731+
all_goals dsimp only [balanceR!, balance!]
732+
all_goals try simp only [*]
733+
all_goals try dsimp only [dreduceDIte, dreduceIte]
749734
all_goals try rfl
750735
all_goals try contradiction
751736
all_goals try (exfalso; tree_tac; done)
752-
all_goals congr; tree_tac
737+
congr; tree_tac
753738

754739
theorem balance_eq_balance! {k : α} {v : β k} {l r : Impl α β} {hlb hrb hlr} :
755740
balance k v l r hlb hrb hlr = balance! k v l r := by
756-
rw [balance.eq_def, balance!.eq_def]
757-
repeat' (split; dsimp)
758-
all_goals try contradiction
759-
all_goals simp_all [-Nat.not_lt]
741+
fun_cases balance! k v l r
742+
all_goals dsimp only [balance, balance!]
743+
all_goals simp only [*]
744+
all_goals dsimp only [dreduceDIte]
745+
all_goals contradiction
760746

761747
theorem balance_eq_inner [Ord α] {sz k v} {l r : Impl α β}
762748
(hl : (inner sz k v l r).Balanced) {h} :
763749
balance k v l r hl.left hl.right h = inner sz k v l r := by
764750
rw [balance_eq_balance!, balance!_eq_balanceₘ hl.left hl.right h, balanceₘ]
765751
have hl' := balanced_inner_iff.mp hl
766-
fun_cases balanceₘ k v l r <;> tree_tac
752+
rw [hl'.2.2.2]
753+
split; rfl
754+
replace hl' := hl'.2.2.1.resolve_left ‹_›
755+
simp only [Nat.not_lt_of_le, hl'.1, hl'.2, reduceDIte, bin]
767756

768757
theorem balance!_desc {k : α} {v : β k} {l r : Impl α β} (hlb : l.Balanced) (hrb : r.Balanced)
769758
(hlr : BalanceLErasePrecond l.size r.size ∨ BalanceLErasePrecond r.size l.size) :

0 commit comments

Comments
 (0)