Skip to content

Commit 91e03d3

Browse files
committed
more improvements
1 parent 72d616d commit 91e03d3

File tree

7 files changed

+41
-56
lines changed

7 files changed

+41
-56
lines changed

src/Init/Core.lean

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,7 +1050,7 @@ set_option linter.missingDocs false in
10501050

10511051
/-- Similar to `decide`, but uses an explicit instance -/
10521052
@[inline] def toBoolUsing {p : Prop} (d : Decidable p) : Bool :=
1053-
decide (h := d)
1053+
@decide p d
10541054

10551055
theorem toBoolUsing_eq_true {p : Prop} (d : Decidable p) (h : p) : toBoolUsing d = true :=
10561056
decide_eq_true (inst := d) h
@@ -1114,34 +1114,34 @@ end Decidable
11141114
section
11151115
variable {p q : Prop}
11161116
/-- Transfer a decidability proof across an equivalence of propositions. -/
1117-
@[inline] def decidable_of_decidable_of_iff [Decidable p] (h : p ↔ q) : Decidable q :=
1118-
if hp : p then
1119-
isTrue (Iff.mp h hp)
1120-
else
1121-
isFalse fun hq => absurd (Iff.mpr h hq) hp
1117+
@[inline] def decidable_of_decidable_of_iff [dp : Decidable p] (h : p ↔ q) : Decidable q where
1118+
decide := decide p
1119+
of_decide :=
1120+
match dp with
1121+
| isTrue hp => Iff.mp h hp
1122+
| isFalse hp => fun hq => absurd (Iff.mpr h hq) hp
11221123

11231124
/-- Transfer a decidability proof across an equality of propositions. -/
11241125
@[inline] def decidable_of_decidable_of_eq [Decidable p] (h : p = q) : Decidable q :=
11251126
decidable_of_decidable_of_iff (p := p) (h ▸ Iff.rfl)
11261127
end
11271128

1128-
@[macro_inline] instance {p q} [Decidable p] [Decidable q] : Decidable (p → q) :=
1129-
if hp : p then
1130-
if hq : q then isTrue (fun _ => hq)
1131-
else isFalse (fun h => absurd (h hp) hq)
1132-
else isTrue (fun h => absurd h hp)
1133-
1134-
instance {p q} [Decidable p] [Decidable q] : Decidable (p ↔ q) :=
1135-
if hp : p then
1136-
if hq : q then
1137-
isTrue ⟨fun _ => hq, fun _ => hp⟩
1138-
else
1139-
isFalse fun h => hq (h.1 hp)
1140-
else
1141-
if hq : q then
1142-
isFalse fun h => hp (h.2 hq)
1143-
else
1144-
isTrue ⟨fun h => absurd h hp, fun h => absurd h hq⟩
1129+
@[macro_inline] instance {p q} [dp : Decidable p] [dq : Decidable q] : Decidable (p → q) where
1130+
decide := !p || q
1131+
of_decide :=
1132+
match dp, dq with
1133+
| isTrue _, isTrue hq => fun _ => hq
1134+
| isTrue hp, isFalse hq => fun h => absurd (h hp) hq
1135+
| isFalse hp, _ => fun h => absurd h hp
1136+
1137+
instance {p q} [dp : Decidable p] [dq : Decidable q] : Decidable (p ↔ q) where
1138+
decide := (decide p).beq (decide q)
1139+
of_decide :=
1140+
match dp, dq with
1141+
| isTrue hp, isTrue hq => ⟨fun _ => hq, fun _ => hp⟩
1142+
| isTrue hp, isFalse hq => fun h => hq (h.1 hp)
1143+
| isFalse hp, isTrue hq => fun h => hp (h.2 hq)
1144+
| isFalse hp, isFalse hq => ⟨fun h => absurd h hp, fun h => absurd h hq⟩
11451145

11461146
/-! # if-then-else expression theorems -/
11471147

@@ -1189,18 +1189,16 @@ instance {c : Prop} {t : c → Prop} {e : ¬c → Prop} [dC : Decidable c] [dT :
11891189
| isFalse hc => dE hc
11901190

11911191
/-- Auxiliary definition for generating compact `noConfusion` for enumeration types -/
1192-
abbrev noConfusionTypeEnum {α : Sort u} {β : Sort v} [inst : DecidableEq β] (f : α → β) (P : Sort w) (x y : α) : Sort w :=
1193-
(inst (f x) (f y)).casesOn
1194-
(fun _ => P)
1195-
(fun _ => P → P)
1192+
abbrev noConfusionTypeEnum {α : Sort u} (f : α → Nat) (P : Sort w) (x y : α) : Sort w :=
1193+
((f x).beq (f y)).casesOn P (P → P)
11961194

11971195
/-- Auxiliary definition for generating compact `noConfusion` for enumeration types -/
1198-
abbrev noConfusionEnum {α : Sort u} {β : Sort v} [inst : DecidableEq β] (f : α → β) {P : Sort w} {x y : α} (h : x = y) : noConfusionTypeEnum f P x y :=
1199-
Decidable.casesOn
1200-
(motive := fun (inst : Decidable (f x = f y)) => Decidable.casesOn (motive := fun _ => Sort w) inst (fun _ => P) (fun _ => P → P))
1201-
(inst (f x) (f y))
1202-
(fun h' => False.elim (h' (congrArg f h)))
1203-
(fun _ => fun x => x)
1196+
abbrev noConfusionEnum {α : Sort u} (f : α → Nat) {P : Sort w} {x y : α} (h : x = y) : noConfusionTypeEnum f P x y :=
1197+
((f x).beq (f y)).casesOn
1198+
(motive := fun b => (f x).beq (f y) = b → b.casesOn P (P → P))
1199+
(fun h' => False.elim (Nat.ne_of_beq_eq_false h' (congrArg f h)))
1200+
(fun _ a => a)
1201+
rfl
12041202

12051203
/-! # Inhabited -/
12061204

@@ -1268,7 +1266,7 @@ theorem recSubsingleton
12681266
{h₂ : ¬p → Sort u}
12691267
[h₃ : ∀ (h : p), Subsingleton (h₁ h)]
12701268
[h₄ : ∀ (h : ¬p), Subsingleton (h₂ h)]
1271-
: Subsingleton (h.casesOn h₂ h₁) :=
1269+
: Subsingleton (h.falseTrueCases h₂ h₁) :=
12721270
match h with
12731271
| isTrue h => h₃ h
12741272
| isFalse h => h₄ h

src/Init/Data/List/Basic.lean

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,6 @@ theorem of_concat_eq_concat {as bs : List α} {a b : α} (h : as.concat a = bs.c
117117

118118
/-! ## Equality -/
119119

120-
/--
121-
Checks whether two lists have the same length and their elements are pairwise `BEq`. Normally used
122-
via the `==` operator.
123-
-/
124-
protected def beq [BEq α] : List α → List α → Bool
125-
| [], [] => true
126-
| a::as, b::bs => a == b && List.beq as bs
127-
| _, _ => false
128-
129120
@[simp] theorem beq_nil_nil [BEq α] : List.beq ([] : List α) ([] : List α) = true := rfl
130121
@[simp] theorem beq_cons_nil [BEq α] {a : α} {as : List α} : List.beq (a::as) [] = false := rfl
131122
@[simp] theorem beq_nil_cons [BEq α] {a : α} {as : List α} : List.beq [] (a::as) = false := rfl

src/Init/Prelude.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Copyright (c) 2020 Microsoft Corporation. All rights reserved.
33
Released under Apache 2.0 license as described in the file LICENSE.
44
Authors: Leonardo de Moura, Mario Carneiro
55
-/
6-
--module
6+
module
77

88
prelude -- Don't import Init, because we're in Init itself
99
set_option linter.missingDocs true -- keep it documented
@@ -1012,7 +1012,7 @@ class Decidable (p : Prop) where
10121012
/--
10131013
The default eliminator for `Decidable`, using `isFalse` and `isTrue`.
10141014
-/
1015-
@[cases_eliminator, induction_eliminator]
1015+
@[cases_eliminator, induction_eliminator, elab_as_elim]
10161016
def Decidable.falseTrueCases {p : Prop} {motive : Decidable p → Sort u}
10171017
(isFalse : ∀ h, motive (isFalse h)) (isTrue : ∀ h, motive (isTrue h))
10181018
(t : Decidable p) : motive t :=

src/Lean/Elab/Tactic/ElabTerm.lean

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -453,10 +453,10 @@ where
453453
doElab (expectedType : Expr) : TacticM Expr := do
454454
let dec ← mkDecide expectedType
455455
-- Get instance from `pf`
456-
let inst := dec.appFn!.appArg!
456+
let inst := dec.appArg!
457457
-- reduce `dec`
458458
let r ← withAtLeastTransparency .default <| whnf dec
459-
if r.isAppOf ``Bool.true then
459+
if r.isConstOf ``Bool.true then
460460
-- Success!
461461
return mkApp3 (mkConst ``of_decide_eq_true) expectedType inst reflBoolTrue
462462
else
@@ -482,11 +482,11 @@ where
482482
-- Diagnose the failure, lazily so that there is no performance impact if `decide` isn't being used interactively.
483483
throwError MessageData.ofLazyM (es := #[expectedType]) do
484484
let r ← r?.getDM (withAtLeastTransparency .default <| whnf b)
485-
if r.isAppOf ``Bool.true then
485+
if r.isConstOf ``Bool.true then
486486
return m!"\
487487
tactic '{tacticName}' failed. internal error: the elaborator is able to reduce the \
488488
'{.ofConstName ``Decidable}' instance, but the kernel is not able to"
489-
else if r.isAppOf ``isFalse then
489+
else if r.isConstOf ``Bool.false then
490490
return m!"\
491491
tactic '{tacticName}' proved that the proposition\
492492
{indentExpr expectedType}\n\

src/Lean/Meta/Constructions/NoConfusion.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ where
104104
withLocalDecl `y BinderInfo.implicit enumType fun y => do
105105
withLocalDeclD `h (← mkEq x y) fun h => do
106106
let declType ← mkForallFVars #[P, x, y, h] (mkApp3 noConfusionType P x y)
107-
let declValue ← mkLambdaFVars #[P, x, y, h] (← mkAppOptM ``noConfusionEnum #[none, none, none, toCtorIdx, P, x, y, h])
107+
let declValue ← mkLambdaFVars #[P, x, y, h] (← mkAppOptM ``noConfusionEnum #[none, toCtorIdx, P, x, y, h])
108108
let declName := Name.mkStr enumName "noConfusion"
109109
addAndCompile <| Declaration.defnDecl {
110110
name := declName

stage0/stdlib/Lean/Elab/Tactic/ElabTerm.c

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

stage0/stdlib/Lean/Meta/Constructions/NoConfusion.c

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)