lean4-htt/tests/pkg/module/Module/Imported.lean
Robert J. Simmons 3a309ba4eb
feat: improve error message in the case of type class synthesis failure (#11245)
This PR improves the error message encountered in the case of a type
class instance resolution failure, and adds an error explanation that
discusses the common new-user case of binary operation overloading and
points to the `trace.Meta.synthInstance` option for advanced debugging.

## Example

```lean4
def f (x : String) := x + x
```

Before:
```
failed to synthesize
  HAdd String String ?m.5

Hint: Additional diagnostic information may be available using the `set_option diagnostics true` command.
```

After:
```
failed to synthesize instance of type class
  HAdd String String ?m.5

Hint: Type class instance resolution failures can be inspected with the `set_option trace.Meta.synthInstance true` command.
Error code: lean.failedToSynthesizeTypeclassInstance
[View explanation](https://lean-lang.org/doc/reference/latest/find/?domain=Manual.errorExplanation&name=lean.failedToSynthesizeTypeclassInstance)
```

The error message is changed in three important ways:
* Explains *what* failed to synthesize, using the "type class"
terminology that's more likely to be recognized than the "instance"
terminology
* Points to the `trace.Meta.synthInstance` option which is otherwise
nearly undiscoverable but is quite powerful (see also
leanprover/reference-manual#663 which is adding commentary on this
option)
* Gives an error explanation link (which won't actually work until the
next release after this is merged) which prioritizes the common-case
explanation of using the wrong binary operation
2025-11-21 21:24:27 +00:00

212 lines
4.9 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

module
prelude
public import Module.Basic
import Lean.DocString
meta import Lean.Elab.Command
/-! Definitions should be exported without their bodies by default -/
/--
info: def f : Nat :=
<not imported>
-/
#guard_msgs in
#print f
/--
error: Type mismatch
rfl
has type
?m.5 = ?m.5
but is expected to have type
f = 1
Note: The following definitions were not unfolded because their definition is not exposed:
f ↦ 1
-/
#guard_msgs in
example : f = 1 := rfl
/--
error: Tactic `apply` failed: could not unify the conclusion of `@rfl`
?a = ?a
with the goal
f = 1
Note: The full type of `@rfl` is
∀ {α : Sort ?u.115} {a : α}, a = a
Note: The following definitions were not unfolded because their definition is not exposed:
f ↦ 1
⊢ f = 1
-/
#guard_msgs in
example : f = 1 := by apply rfl
/-! Theorems should be exported without their bodies -/
/--
info: theorem t : f = 1 :=
<not imported>
-/
#guard_msgs in
#print t
/--
info: theorem trfl : f = 1 :=
<not imported>
-/
#guard_msgs in
#print trfl
/-! Metadata of private decls should not be exported. -/
-- Should not fail with 'unknown constant `inst*`
/--
error: failed to synthesize instance of type class
X
Hint: Type class instance resolution failures can be inspected with the `set_option trace.Meta.synthInstance true` command.
-/
#guard_msgs in
def fX : X := inferInstance
/-- error: `dsimp` made no progress -/
#guard_msgs in
example : P f := by dsimp only [t]; exact hP1
example : P f := by simp only [t]; exact hP1
/-- error: `dsimp` made no progress -/
#guard_msgs in
example : P f := by dsimp only [trfl]; exact hP1
/-- error: `dsimp` made no progress -/
#guard_msgs in
example : P f := by dsimp only [trfl']; exact hP1
example : P fexp := by dsimp only [fexp_trfl]; exact hP1
example : P fexp := by dsimp only [fexp_trfl']; exact hP1
example : t = t := by dsimp only [trfl]
/--
error: Invalid field `eq_def`: The environment does not contain `Nat.eq_def`
f
has type
Nat
-/
#guard_msgs in
#check f.eq_def
/--
error: Invalid field `eq_unfold`: The environment does not contain `Nat.eq_unfold`
f
has type
Nat
-/
#guard_msgs in
#check f.eq_unfold
/-- error: Unknown constant `f_struct.eq_1` -/
#guard_msgs in
#check f_struct.eq_1
/-- error: Unknown constant `f_struct.eq_def` -/
#guard_msgs in
#check f_struct.eq_def
/-- error: Unknown constant `f_struct.eq_unfold` -/
#guard_msgs in
#check f_struct.eq_unfold
/-- error: Unknown constant `f_wfrec.eq_1` -/
#guard_msgs in
#check f_wfrec.eq_1
/-- error: Unknown constant `f_wfrec.eq_def` -/
#guard_msgs in
#check f_wfrec.eq_def
/-- error: Unknown constant `f_wfrec.eq_unfold` -/
#guard_msgs in
#check f_wfrec.eq_unfold
/-- error: Unknown constant `f_wfrec.induct_unfolding` -/
#guard_msgs(pass trace, all) in
#check f_wfrec.induct_unfolding
/-- info: f_exp_wfrec.eq_1 (x✝ : Nat) : f_exp_wfrec 0 x✝ = x✝ -/
#guard_msgs in
#check f_exp_wfrec.eq_1
/--
info: f_exp_wfrec.eq_def (x✝ x✝¹ : Nat) :
f_exp_wfrec x✝ x✝¹ =
match x✝, x✝¹ with
| 0, acc => acc
| n.succ, acc => f_exp_wfrec n (acc + 1)
-/
#guard_msgs in
#check f_exp_wfrec.eq_def
/--
info: f_exp_wfrec.eq_unfold :
f_exp_wfrec = fun x x_1 =>
match x, x_1 with
| 0, acc => acc
| n.succ, acc => f_exp_wfrec n (acc + 1)
-/
#guard_msgs in
#check f_exp_wfrec.eq_unfold
/--
info: f_exp_wfrec.induct_unfolding (motive : Nat → Nat → Nat → Prop) (case1 : ∀ (acc : Nat), motive 0 acc acc)
(case2 : ∀ (n acc : Nat), motive n (acc + 1) (f_exp_wfrec n (acc + 1)) → motive n.succ acc (f_exp_wfrec n (acc + 1)))
(a✝ a✝¹ : Nat) : motive a✝ a✝¹ (f_exp_wfrec a✝ a✝¹)
-/
#guard_msgs(pass trace, all) in
#check f_exp_wfrec.induct_unfolding
/-! Basic non-`meta` check. -/
/-- error: Invalid definition `nonMeta`, may not access declaration `pubMeta` marked as `meta` -/
#guard_msgs in
def nonMeta := pubMeta
/-! `simp` should not pick up inaccessible definitional equations. -/
/-- error: `simp` made no progress -/
#guard_msgs in
theorem f_struct_eq : f_struct 0 = 0 := by
simp
/-! `[inherit_doc]` should work independently of visibility. -/
/-- info: some "A private definition. " -/
#guard_msgs in
open Lean in
#eval show CoreM _ from do findDocString? (← getEnv) ``pubInheritDoc
/-! Cross-module `meta` checks, including involving compiler-introduced constants. -/
attribute [local delab Nat] delab
/--
error: Cannot add attribute `[Lean.PrettyPrinter.Delaborator.delabAttribute]`: Declaration `noMetaDelab` must be marked as `meta`
-/
#guard_msgs in
attribute [local delab Nat] noMetaDelab
@[noinline] meta def pap (f : α → β) (a : α) : β := f a
public meta def delab' : Lean.PrettyPrinter.Delaborator.Delab :=
pap delab
-- Used to complain about `_boxed` not being meta
attribute [local delab Nat] delab'
/--
error: Invalid `meta` definition `metaUsingNonMeta`, `f` is not accessible here; consider adding `public meta import Module.Basic`
-/
#guard_msgs in
public meta def metaUsingNonMeta : Nat :=
f