Skip to content

Commit edc75ce

Browse files
make canonical instances appear as _
1 parent deb1245 commit edc75ce

File tree

3 files changed

+43
-24
lines changed

3 files changed

+43
-24
lines changed

Mathlib/Topology/Defs/Basic.lean

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,11 @@ scoped notation (name := IsClosed_of) "IsClosed[" t "]" => @IsClosed _ t
198198
/-- Notation for `closure` with respect to a non-standard topology. -/
199199
scoped notation (name := closure_of) "closure[" t "]" => @closure _ t
200200

201-
/-- Notation for `Continuous` with respect to a non-standard topologies. -/
201+
/-- Notation for `Continuous` with respect to non-standard topologies. -/
202202
scoped notation (name := Continuous_of) "Continuous[" t₁ ", " t₂ "]" =>
203203
@Continuous _ _ t₁ t₂
204204

205-
open Topology Lean.PrettyPrinter.Delaborator
205+
open Topology Lean.PrettyPrinter.Delaborator Delab.Noncanonical
206206

207207
/-- Delaborator for `IsOpen[_]`. -/
208208
@[scoped app_delab IsOpen] meta def delabIsOpen : Delab := delabUnary 2 1 fun x ↦ `(IsOpen[$x])
@@ -216,7 +216,8 @@ meta def delabIsClosed : Delab := delabUnary 2 1 fun x ↦ `(IsClosed[$x])
216216

217217
/-- Delaborator for `Continuous[_, _]`. -/
218218
@[scoped app_delab Continuous]
219-
meta def delabContinuous : Delab := delabBinary 4 2 3 fun x y ↦ `(Continuous[$x, $y])
219+
meta def delabContinuous : Delab :=
220+
delabBinary 4 2 3 (fun x y ↦ `(Continuous[$x, $y])) (some `(_))
220221

221222
end Topology
222223

Mathlib/Util/DelabNonCanonical.lean

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Authors: Robert Maxton
66
module
77

88
public import Mathlib.Init
9+
public meta import Lean.Meta
910
public meta import Lean.PrettyPrinter.Delaborator.Builtins
1011

1112
/-! Delab checking canonicity.
@@ -18,17 +19,22 @@ Synthesized instances are considered 'canonical' for this purpose.
1819
public meta section
1920
open Lean Meta PrettyPrinter.Delaborator SubExpr
2021

22+
23+
def Lean.Meta.isCanonicalInstance (inst : Expr) : MetaM Bool := do
24+
let type ← inferType inst
25+
let .some synthInst ← trySynthInstance type | return false
26+
isDefEqI inst synthInst
27+
2128
/-- When the delab reader is pointed at an expression for an instance, returns `(true, t)`
22-
**iff** instance synthesis succeeds and produces a defeq instance; otherwise returns `(false, t)`.
29+
iff instance synthesis succeeds and produces a defeq instance; otherwise returns `(false, t)`.
2330
-/
2431
def delabCheckingCanonical : DelabM (Bool × Term) := do
25-
let instD ← delab
2632
let inst ← getExpr
27-
let type ← inferType inst
28-
-- if there is no synthesized instance, still return `false`
29-
-- (because `inst` is still non-canonical)
30-
let .some synthInst ← Meta.trySynthInstance type | return (false, instD)
31-
withReducibleAndInstances <| return (← Meta.isDefEq inst synthInst, instD)
33+
if ← isCanonicalInstance inst then
34+
return (true, ← annotateTermInfo <| ← `(_))
35+
else
36+
return (false, ← delab)
37+
namespace Delab.Noncanonical
3238

3339
/-- Delaborate an expression with arity `arity` into a unary notation `mkStx` iff the argument
3440
`arg` is a non-canonical instance (is not defeq to what is synthesized for its type, or else
@@ -39,12 +45,19 @@ def delabUnary (arity arg : Nat) (mkStx : Term → DelabM Term) : Delab :=
3945
mkStx instD
4046

4147
/-- Delaborate an expression with arity `arity` into a binary notation `mkStx` iff either
42-
argument `arg₁` or `arg₂` are non-canonical instance (are not defeq to what is synthesized for
43-
its type, or else instance synthesis fails). -/
44-
def delabBinary (arity arg₁ arg₂ : Nat) (mkStx : Term → Term → DelabM Term) : Delab :=
48+
argument `arg₁` or `arg₂` are non-canonical instances (are not defeq to what is synthesized for
49+
its type, or else instance synthesis fails). If `hole` is not `none`, it is used
50+
in place of a canonical instance. -/
51+
def delabBinary (arity arg₁ arg₂ : Nat) (mkStx : Term → Term → DelabM Term)
52+
(hole : Option (DelabM Term) := none) : Delab :=
4553
withOverApp arity <| whenPPOption Lean.getPPNotation do
4654
let (canonα?, instDα) ← withNaryArg arg₁ delabCheckingCanonical
4755
let (canonβ?, instDβ) ← withNaryArg arg₂ delabCheckingCanonical
48-
-- fall through to normal delab if both canonical
4956
if canonα? && canonβ? then failure
5057
mkStx instDα instDβ
58+
-- let hole ← hole.sequence
59+
-- mkStx (replace? hole canonα? instDα) (replace? hole canonβ? instDβ)
60+
-- where
61+
-- replace? h b i := bif b then h.getD i else i
62+
63+
end Delab.Noncanonical

MathlibTest/Delab/TopologicalSpace.lean

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,12 @@ section applied
119119
-- in either or both slots do trigger the delab and that
120120
-- `instTopologicalSpaceNat`, `sierpinskiSpace` does not.
121121

122-
/-- info: [Continuous[τ₁, sierpinskiSpace] p, Continuous[τ₂, sierpinskiSpace] p,
123-
Continuous[induced g inferInstance, inferInstance] g] : List Prop -/
122+
/-- info: [Continuous[τ₁, _] p, Continuous[τ₂, _] p, Continuous[induced g inferInstance, inferInstance] g] : List Prop -/
124123
#guard_msgs(info) in
125124
#check [Continuous[τ₁, sierpinskiSpace] p, Continuous[τ₂, sierpinskiSpace] p,
126125
Continuous[induced g inferInstance, inferInstance] g]
127126

128-
/-- info: [Continuous[instTopologicalSpaceNat, σ₁] p, Continuous[instTopologicalSpaceNat, σ₂] p,
129-
Continuous[inferInstance, coinduced h inferInstance] h] : List Prop -/
127+
/-- info: [Continuous[_, σ₁] p, Continuous[_, σ₂] p, Continuous[inferInstance, coinduced h inferInstance] h] : List Prop -/
130128
#guard_msgs(info) in
131129
#check [Continuous[instTopologicalSpaceNat, σ₁] p, Continuous[instTopologicalSpaceNat, σ₂] p,
132130
Continuous[inferInstance, coinduced h inferInstance] h]
@@ -143,25 +141,32 @@ section unapplied
143141

144142
-- Same as above, except for unapplied operators.
145143

146-
/-- info: [Continuous[τ₁, sierpinskiSpace], Continuous[τ₂, sierpinskiSpace]]
144+
/-- info: [Continuous[τ₁, _], Continuous[τ₂, _]]
147145
: List ((ℕ → Prop) → Prop) -/
148146
#guard_msgs(info) in
149147
#check [Continuous[τ₁, sierpinskiSpace], Continuous[τ₂, sierpinskiSpace]]
150148

149+
-- Note: This is *not* the desired behavior. Treat it instead as a 'bug tracker.'
150+
-- When `(try)SynthInstance` in `$check` is fixed, this should no longer trigger the delab
151+
-- on the right, so we should get `Continuous[induced g inferInstance, _]`.
151152
/-- info:
152-
Continuous[induced g inferInstance, inferInstanceAs (TopologicalSpace α)] : (β → α) → Prop -/
153+
Continuous[induced g inferInstance, inst✝] : (β → α) → Prop -/
153154
#guard_msgs(info) in
154-
#check Continuous[induced g inferInstance, inferInstanceAs (TopologicalSpace α)]
155+
#check Continuous[induced g inferInstance, inferInstanceAs% (TopologicalSpace α)]
155156

156-
/-- info: [Continuous[instTopologicalSpaceNat, σ₁], Continuous[instTopologicalSpaceNat, σ₂]]
157+
/-- info: [Continuous[_, σ₁], Continuous[_, σ₂]]
157158
: List ((ℕ → Prop) → Prop) -/
158159
#guard_msgs(info) in
159160
#check [Continuous[instTopologicalSpaceNat, σ₁], Continuous[instTopologicalSpaceNat, σ₂]]
160161

162+
163+
-- As above, this is not the desired behavior. When `(try)SynthInstance` in `#check` is fixed,
164+
-- this should no longer trigger the delab on the left, so we should get
165+
-- `Continuous[_, coinduced h inferInstance]`.
161166
/-- info:
162-
Continuous[inferInstanceAs (TopologicalSpace α), coinduced h inferInstance] : (α → β) → Prop -/
167+
Continuous[inst✝, coinduced h inferInstance] : (α → β) → Prop -/
163168
#guard_msgs(info) in
164-
#check Continuous[inferInstanceAs (TopologicalSpace α), coinduced h inferInstance]
169+
#check Continuous[inferInstanceAs% (TopologicalSpace α), coinduced h inferInstance]
165170

166171
/-- info: [Continuous[induced p inferInstance, coinduced p inferInstance], Continuous]
167172
: List ((ℕ → Prop) → Prop) -/

0 commit comments

Comments
 (0)