Skip to content

Commit 12750d2

Browse files
authored
perf: inline decidable instances (#10934)
This PR adds inline annotations to several `Decidable` instances. Additionally, it removes the `Decidable` instance for `p → q` which is made redundant by `forall_prop_decidable`.
1 parent 334fa47 commit 12750d2

File tree

5 files changed

+94
-17
lines changed

5 files changed

+94
-17
lines changed

src/Init/Core.lean

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,12 +1140,21 @@ variable {p q : Prop}
11401140
decidable_of_decidable_of_iff (p := p) (h ▸ Iff.rfl)
11411141
end
11421142

1143-
@[macro_inline] instance {p q} [Decidable p] [Decidable q] : Decidable (p → q) :=
1144-
if hp : p then
1145-
if hq : q then isTrue (fun _ => hq)
1146-
else isFalse (fun h => absurd (h hp) hq)
1147-
else isTrue (fun h => absurd h hp)
1143+
@[inline]
1144+
instance exists_prop_decidable {p} (P : p → Prop)
1145+
[Decidable p] [∀ h, Decidable (P h)] : Decidable (Exists P) :=
1146+
if h : p then
1147+
decidable_of_decidable_of_iff ⟨fun h2 => ⟨h, h2⟩, fun ⟨_, h2⟩ => h2⟩
1148+
else isFalse fun ⟨h', _⟩ => h h'
1149+
1150+
@[inline]
1151+
instance forall_prop_decidable {p} (P : p → Prop)
1152+
[Decidable p] [∀ h, Decidable (P h)] : Decidable (∀ h, P h) :=
1153+
if h : p then
1154+
decidable_of_decidable_of_iff ⟨fun h2 _ => h2, fun al => al h⟩
1155+
else isTrue fun h2 => absurd h2 h
11481156

1157+
@[inline]
11491158
instance {p q} [Decidable p] [Decidable q] : Decidable (p ↔ q) :=
11501159
if hp : p then
11511160
if hq : q then
@@ -1193,11 +1202,13 @@ theorem dif_eq_if (c : Prop) {h : Decidable c} {α : Sort u} (t : α) (e : α) :
11931202
| isTrue _ => rfl
11941203
| isFalse _ => rfl
11951204

1205+
@[macro_inline]
11961206
instance {c t e : Prop} [dC : Decidable c] [dT : Decidable t] [dE : Decidable e] : Decidable (if c then t else e) :=
11971207
match dC with
11981208
| isTrue _ => dT
11991209
| isFalse _ => dE
12001210

1211+
@[inline]
12011212
instance {c : Prop} {t : c → Prop} {e : ¬c → Prop} [dC : Decidable c] [dT : ∀ h, Decidable (t h)] [dE : ∀ h, Decidable (e h)] : Decidable (if h : c then t h else e h) :=
12021213
match dC with
12031214
| isTrue hc => dT hc

src/Init/Prelude.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,7 @@ until `c` is known.
10991099
| Or.inl h => hp h
11001100
| Or.inr h => hq h
11011101

1102+
@[inline]
11021103
instance [dp : Decidable p] : Decidable (Not p) :=
11031104
match dp with
11041105
| isTrue hp => isFalse (absurd hp)

src/Init/PropLemmas.lean

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -447,18 +447,6 @@ theorem Decidable.by_contra [Decidable p] : (¬p → False) → p := of_not_not
447447
@[expose] protected def Or.by_cases' [Decidable q] {α : Sort u} (h : p ∨ q) (h₁ : p → α) (h₂ : q → α) : α :=
448448
if hq : q then h₂ hq else h₁ (h.resolve_right hq)
449449

450-
instance exists_prop_decidable {p} (P : p → Prop)
451-
[Decidable p] [∀ h, Decidable (P h)] : Decidable (∃ h, P h) :=
452-
if h : p then
453-
decidable_of_decidable_of_iff ⟨fun h2 => ⟨h, h2⟩, fun ⟨_, h2⟩ => h2⟩
454-
else isFalse fun ⟨h', _⟩ => h h'
455-
456-
instance forall_prop_decidable {p} (P : p → Prop)
457-
[Decidable p] [∀ h, Decidable (P h)] : Decidable (∀ h, P h) :=
458-
if h : p then
459-
decidable_of_decidable_of_iff ⟨fun h2 _ => h2, fun al => al h⟩
460-
else isTrue fun h2 => absurd h2 h
461-
462450
@[bool_to_prop] theorem decide_eq_true_iff {p : Prop} [Decidable p] : (decide p = true) ↔ p := by simp
463451

464452
@[simp, bool_to_prop] theorem decide_eq_decide {p q : Prop} {_ : Decidable p} {_ : Decidable q} :

tests/lean/run/10934.lean

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
set_option trace.compiler.ir.result true
2+
3+
/--
4+
trace: [Compiler.IR] [result]
5+
def _example (x_1 : u8) (x_2 : u8) : u8 :=
6+
case x_1 : u8 of
7+
Bool.false →
8+
case x_2 : u8 of
9+
Bool.false →
10+
let x_3 : u8 := 1;
11+
ret x_3
12+
Bool.true →
13+
ret x_1
14+
Bool.true →
15+
ret x_2
16+
def _example._boxed (x_1 : tagged) (x_2 : tagged) : tagged :=
17+
let x_3 : u8 := unbox x_1;
18+
let x_4 : u8 := unbox x_2;
19+
let x_5 : u8 := _example x_3 x_4;
20+
let x_6 : tobj := box x_5;
21+
ret x_6
22+
-/
23+
#guard_msgs in
24+
example (a b : Bool) : Bool := decide (a ↔ b)
25+
26+
/--
27+
trace: [Compiler.IR] [result]
28+
def _example (x_1 : u8) (x_2 : u8) : u8 :=
29+
case x_1 : u8 of
30+
Bool.false →
31+
let x_3 : u8 := 1;
32+
ret x_3
33+
Bool.true →
34+
ret x_2
35+
def _example._boxed (x_1 : tagged) (x_2 : tagged) : tagged :=
36+
let x_3 : u8 := unbox x_1;
37+
let x_4 : u8 := unbox x_2;
38+
let x_5 : u8 := _example x_3 x_4;
39+
let x_6 : tobj := box x_5;
40+
ret x_6
41+
-/
42+
#guard_msgs in
43+
example (a b : Bool) : Bool := decide (a → b)
44+
45+
/--
46+
trace: [Compiler.IR] [result]
47+
def _example (x_1 : u8) (x_2 : u8) : u8 :=
48+
case x_1 : u8 of
49+
Bool.false →
50+
ret x_1
51+
Bool.true →
52+
ret x_2
53+
def _example._boxed (x_1 : tagged) (x_2 : tagged) : tagged :=
54+
let x_3 : u8 := unbox x_1;
55+
let x_4 : u8 := unbox x_2;
56+
let x_5 : u8 := _example x_3 x_4;
57+
let x_6 : tobj := box x_5;
58+
ret x_6
59+
-/
60+
#guard_msgs in
61+
example (a b : Bool) : Bool := decide (∃ _ : a, b)
62+
63+
/--
64+
trace: [Compiler.IR] [result]
65+
def _example (x_1 : u8) : u8 :=
66+
ret x_1
67+
def _example._boxed (x_1 : tagged) : tagged :=
68+
let x_2 : u8 := unbox x_1;
69+
let x_3 : u8 := _example x_2;
70+
let x_4 : tobj := box x_3;
71+
ret x_4
72+
-/
73+
#guard_msgs in
74+
example (a : Bool) : Bool := decide (if _h : a then True else False)

tests/lean/run/grind_cutsat_tests.lean

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ set_option trace.grind.cutsat.model true in
2828
example (a b c d e : Int) : test1 a b c d e := by
2929
(fail_if_success cutsat); sorry
3030

31+
-- TODO: this should not be necessary (compare to 20 before)
32+
set_option synthInstance.maxSize 400 in
33+
3134
/-- info: false -/
3235
#guard_msgs (info) in
3336
#eval test1 101 0 5335 0 0

0 commit comments

Comments
 (0)