Skip to content

Commit c479f37

Browse files
committed
fix: use withMainContext in borelize tactic (#5237)
Also make it use `withoutRecover` when elaborating arguments (like in `apply`), and in addition use `exprToSyntax` to make sure each argument is elaborated only once. Adding `withMainContext` fixes the bug reported by @urkud [on Zulip](https://leanprover.zulipchat.com/#narrow/stream/287929-mathlib4/topic/bug.20in.20.60borelize.60/near/367468010).
1 parent 697ac1d commit c479f37

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

Mathlib/MeasureTheory/Constructions/BorelSpace/Basic.lean

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -220,17 +220,19 @@ Finally, `borelize α β γ` runs `borelize α; borelize β; borelize γ`.
220220
-/
221221
syntax "borelize" (ppSpace colGt term:max)* : tactic
222222

223-
/-- Add instances `borel $t : MeasurableSpace $t` and `⟨rfl⟩ : BorelSpace $t`. -/
224-
def addBorelInstance (t : Term) : TacticM Unit := do
223+
/-- Add instances `borel e : MeasurableSpace e` and `⟨rfl⟩ : BorelSpace e`. -/
224+
def addBorelInstance (e : Expr) : TacticM Unit := do
225+
let t ← Lean.Elab.Term.exprToSyntax e
225226
evalTactic <| ← `(tactic|
226227
refine_lift
227228
letI : MeasurableSpace $t := borel $t
228229
haveI : BorelSpace $t := ⟨rfl⟩
229230
?_)
230231

231-
/-- Given a type `$t`, an assumption `i : MeasurableSpace $t`, and an instance `[BorelSpace $t]`,
232-
replace `i` with `borel $t`. -/
233-
def borelToRefl (t : Term) (i : FVarId) : TacticM Unit := do
232+
/-- Given a type `e`, an assumption `i : MeasurableSpace e`, and an instance `[BorelSpace e]`,
233+
replace `i` with `borel e`. -/
234+
def borelToRefl (e : Expr) (i : FVarId) : TacticM Unit := do
235+
let t ← Lean.Elab.Term.exprToSyntax e
234236
evalTactic <| ← `(tactic|
235237
have := @BorelSpace.measurable_eq $t _ _ _)
236238
liftMetaTactic fun m => return [← subst m i]
@@ -242,11 +244,11 @@ def borelToRefl (t : Term) (i : FVarId) : TacticM Unit := do
242244
/-- Given a type `$t`, if there is an assumption `[i : MeasurableSpace $t]`, then try to prove
243245
`[BorelSpace $t]` and replace `i` with `borel $t`. Otherwise, add instances
244246
`borel $t : MeasurableSpace $t` and `⟨rfl⟩ : BorelSpace $t`. -/
245-
def borelize (t : Term) : TacticM Unit := do
247+
def borelize (t : Term) : TacticM Unit := withMainContext <| do
246248
let u ← mkFreshLevelMVar
247-
let e ← Tactic.elabTermEnsuringType t (mkSort (mkLevelSucc u))
249+
let e ← withoutRecover <| Tactic.elabTermEnsuringType t (mkSort (mkLevelSucc u))
248250
let i? ← findLocalDeclWithType? (← mkAppOptM ``MeasurableSpace #[e])
249-
i?.elim (addBorelInstance t) (borelToRefl t)
251+
i?.elim (addBorelInstance e) (borelToRefl e)
250252

251253
elab_rules : tactic
252254
| `(tactic| borelize $[$t:term]*) => t.forM borelize

test/borelize.lean

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import Mathlib.MeasureTheory.Constructions.BorelSpace.Basic
2+
3+
example [TopologicalSpace α] [inst : MeasurableSpace α] [BorelSpace α] : MeasurableSet (∅ : Set α) := by
4+
guard_target = @MeasurableSet α inst ∅
5+
borelize α
6+
guard_target = @MeasurableSet α (borel α) ∅
7+
apply MeasurableSet.empty
8+
9+
example [TopologicalSpace α] : True := by
10+
borelize α
11+
have h : MeasurableSet (∅ : Set α) := MeasurableSet.empty
12+
guard_hyp h : @MeasurableSet α (borel α) ∅
13+
trivial
14+
15+
example : True := by
16+
obtain ⟨α, ⟨hα⟩⟩ : ∃ α : Type, Nonempty (TopologicalSpace α) := ⟨ℕ, ⟨inferInstance⟩⟩
17+
borelize α
18+
have h : MeasurableSet (∅ : Set α) := MeasurableSet.empty
19+
guard_hyp h : @MeasurableSet α (borel α) ∅
20+
trivial
21+
22+
example : True := by
23+
set α := ℕ
24+
borelize α
25+
have h : MeasurableSet (∅ : Set α) := MeasurableSet.empty
26+
guard_hyp h : @MeasurableSet α (borel α) ∅
27+
trivial
28+
29+
example : True := by
30+
have h1 : MeasurableSet (∅ : Set ℕ) := MeasurableSet.empty
31+
guard_hyp h1 : @MeasurableSet ℕ Nat.instMeasurableSpace ∅
32+
borelize ℕ
33+
have h2 : MeasurableSet (∅ : Set ℕ) := MeasurableSet.empty
34+
guard_hyp h2 : @MeasurableSet ℕ (borel ℕ) ∅
35+
trivial

0 commit comments

Comments
 (0)