|
| 1 | +import Lean |
| 2 | + |
| 3 | +open Lean Meta Elab PrettyPrinter Delaborator Syntax |
| 4 | + |
| 5 | +class Bar where |
| 6 | + |
| 7 | +syntax boolLit := &"true" <|> &"false" |
| 8 | + |
| 9 | +/-- Syntax to embed our debugging logs in, in order to get them out of `Delab`. -/ |
| 10 | +syntax "⟪" ppLine |
| 11 | +-- Whether `Bar` is synthesized |
| 12 | + &"synthBar" " := " boolLit "," ppLine |
| 13 | + -- Types of terms in the local context |
| 14 | + &"lctx" " := " "[" term,* "]" "," ppLine |
| 15 | + -- List of local instance names |
| 16 | + &"localInstanceNames" " := " "[" ident,* "]" ppLine |
| 17 | + "⟫" : term |
| 18 | + |
| 19 | +/-- Syntax embedding debugging info for synthesizing `Bar`. -/ |
| 20 | + def synthBarStxLog : MetaM Term := do |
| 21 | + let synthBar ← if (← synthInstance? (mkConst ``Bar)).isSome then `(boolLit|true) else `(boolLit|false) |
| 22 | + let lctx : TSepArray `term "," ← (← getLocalHyps).mapM fun e => |
| 23 | + withOptions (·.setBool ``Lean.pp.notation false) do |
| 24 | + PrettyPrinter.delab <|← inferType e |
| 25 | + let localInstanceNames : TSepArray `ident "," := |
| 26 | + (← getLocalInstances).map (mkIdent ·.className) |
| 27 | + `(⟪ synthBar := $synthBar, |
| 28 | + lctx := [$lctx,*], |
| 29 | + localInstanceNames := [$localInstanceNames,*]⟫) |
| 30 | + |
| 31 | +/-- Dummy declaration to attach our `app_delab` to, to mimic circumstances in mathlib4#30266 -/ |
| 32 | +def foo (_ : Unit) : True := True.intro |
| 33 | + |
| 34 | +@[app_delab foo] |
| 35 | +partial def delabFoo : Delab := whenPPOption getPPNotation synthBarStxLog |
| 36 | + |
| 37 | +variable [Bar] |
| 38 | + |
| 39 | +-- Make sure the `[Bar]` instance is present in local instances and synthesis succeeds |
| 40 | +#check foo () |
| 41 | + |
| 42 | +-- As a control for the above code, local instances are, as expected, correctly populated during term elaboration. |
| 43 | +elab "test_elab%" : term => do |
| 44 | + logInfo m!"{← synthBarStxLog}" |
| 45 | + return mkConst ``Unit.unit |
| 46 | + |
| 47 | +#check test_elab% |
0 commit comments