Compare commits

...

21 Commits

Author SHA1 Message Date
Joachim Breitner
bb5b28d3f2 Actually, let's not get this PR entangled with removing the Iff.rfl macro 2024-09-19 16:00:16 +02:00
Joachim Breitner
e351221e07 Merge branch 'nightly' of github.com:leanprover/lean4 into joachim/rfl-errors 2024-09-19 15:58:34 +02:00
Joachim Breitner
fa5b6e29c6 Update test 2024-09-19 15:57:08 +02:00
Joachim Breitner
85f50e8688 Revert "Do not use isDefEqGuarded"
This reverts commit c972bcbc34.
2024-09-18 16:27:39 +02:00
Joachim Breitner
802fa765ed Remove exact Iff.rfl macro 2024-09-18 16:27:32 +02:00
Joachim Breitner
c972bcbc34 Do not use isDefEqGuarded 2024-09-18 15:11:41 +02:00
Joachim Breitner
9f0636cb22 Merge branch 'master' of github.com:leanprover/lean4 into joachim/rfl-errors 2024-09-18 14:43:01 +02:00
Joachim Breitner
449506bd1a Merge branch 'nightly' of github.com:leanprover/lean4 into joachim/rfl-errors 2024-09-18 14:41:29 +02:00
Joachim Breitner
52cdde3e37 Trigger stage0 update after merge 2024-09-16 12:33:52 +02:00
Joachim Breitner
8b6c17eeb9 Merge branch 'nightly-with-mathlib' of github.com:leanprover/lean4 into joachim/rfl-errors 2024-09-15 23:51:01 +02:00
Joachim Breitner
dbc7bc3400 Improve error message 2024-09-11 10:29:17 +02:00
Joachim Breitner
fde07c4994 More tests 2024-09-11 10:16:01 +02:00
Joachim Breitner
7e2870534b Use approxDefEq 2024-09-10 11:40:48 +02:00
Joachim Breitner
3c1e632bf8 Dead code 2024-09-10 10:27:16 +02:00
Joachim Breitner
3f918b5695 Test documentation 2024-09-10 10:03:56 +02:00
Joachim Breitner
e752769bbf Move to tests/run 2024-09-10 10:01:19 +02:00
Joachim Breitner
925b3e5c80 Update test output 2024-09-10 09:53:45 +02:00
Joachim Breitner
61e8b4c26d Merge branch 'nightly-with-mathlib' of github.com:leanprover/lean4 into joachim/rfl-errors 2024-09-10 08:45:04 +02:00
Joachim Breitner
3cbfc4270b Update Rfl.lean 2024-03-19 13:27:44 +01:00
Joachim Breitner
0dd354faa6 Update src/Lean/Meta/Tactic/Rfl.lean 2024-03-19 13:22:36 +01:00
Joachim Breitner
dc519ea130 feat: apply_rfl tactic: Handle Eq, HEq, better error messages
this implements the first half of #3302: It improves the extensible
`rfl` tactic (the one that looks at `refl` attributes) to

* Check itself that the lhs and rhs are defEq, and give a nice
  consistent error message when they don't (instead of just passing on
  the less helpful error message from `apply Foo.refl`)
* Also handle `Eq` and `HEq` (built in) and `Iff` (using the attribute)

Care ist taken that, as before, the transparency setting affects
comparing the lhs and rhs, but not the reduction of the relation

A test file checks the various failure modes and error messages.

I believe this `apply_rfl` can serve as the only implementation of
`rfl`, which would then complete #3302, but that seems to require a
non-trivial bootstrapping dance, so maybe better done separately.
2024-03-19 10:19:57 +01:00
5 changed files with 503 additions and 12 deletions

View File

@@ -12,6 +12,9 @@ namespace Lean.Meta
/--
Close given goal using `Eq.refl`.
See `Lean.MVarId.applyRfl` for the variant that also consults `@[refl]` lemmas, and which
backs the `rfl` tactic.
-/
def _root_.Lean.MVarId.refl (mvarId : MVarId) : MetaM Unit := do
mvarId.withContext do

View File

@@ -50,33 +50,59 @@ open Elab Tactic
/-- `MetaM` version of the `rfl` tactic.
This tactic applies to a goal whose target has the form `x ~ x`,
where `~` is a reflexive relation other than `=`,
that is, a relation which has a reflexive lemma tagged with the attribute @[refl].
This tactic applies to a goal whose target has the form `x ~ x`, where `~` is a reflexive
relation, that is, equality or another relation which has a reflexive lemma tagged with the
attribute [refl].
-/
def _root_.Lean.MVarId.applyRfl (goal : MVarId) : MetaM Unit := do
let .app (.app rel _) _ whnfR <| instantiateMVars <| goal.getType
| throwError "reflexivity lemmas only apply to binary relations, not{
indentExpr (← goal.getType)}"
if let .app (.const ``Eq [_]) _ := rel then
throwError "MVarId.applyRfl does not solve `=` goals. Use `MVarId.refl` instead."
def _root_.Lean.MVarId.applyRfl (goal : MVarId) : MetaM Unit := goal.withContext do
-- NB: uses whnfR, we do not want to unfold the relation itself
let t whnfR <| instantiateMVars <| goal.getType
if t.getAppNumArgs < 2 then
throwError "rfl can only be used on binary relations, not{indentExpr (← goal.getType)}"
-- Special case HEq here as it has a different argument order.
if t.isAppOfArity ``HEq 4 then
let gs goal.applyConst ``HEq.refl
unless gs.isEmpty do
throwError MessageData.tagged `Tactic.unsolvedGoals <| m!"unsolved goals\n{
goalsToMessageData gs}"
return
let rel := t.appFn!.appFn!
let lhs := t.appFn!.appArg!
let rhs := t.appArg!
let success approxDefEq <| isDefEqGuarded lhs rhs
unless success do
let explanation := MessageData.ofLazyM (es := #[lhs, rhs]) do
let (lhs, rhs) addPPExplicitToExposeDiff lhs rhs
return m!"The lhs{indentExpr lhs}\nis not definitionally equal to rhs{indentExpr rhs}"
throwTacticEx `apply_rfl goal explanation
if rel.isAppOfArity `Eq 1 then
-- The common case is equality: just use `Eq.refl`
let us := rel.appFn!.constLevels!
let α := rel.appArg!
goal.assign (mkApp2 (mkConst ``Eq.refl us) α lhs)
else
-- Else search through `@refl` keyed by the relation
let s saveState
let mut ex? := none
for lem in (reflExt.getState ( getEnv)).getMatch rel reflExt.config do
try
let gs goal.apply ( mkConstWithFreshMVarLevels lem)
if gs.isEmpty then return () else
logError <| MessageData.tagged `Tactic.unsolvedGoals <| m!"unsolved goals\n{
throwError MessageData.tagged `Tactic.unsolvedGoals <| m!"unsolved goals\n{
goalsToMessageData gs}"
catch e =>
ex? := ex? <|> (some ( saveState, e)) -- stash the first failure of `apply`
unless ex?.isSome do
ex? := some ( saveState, e) -- stash the first failure of `apply`
s.restore
if let some (sErr, e) := ex? then
sErr.restore
throw e
else
throwError "rfl failed, no lemma with @[refl] applies"
throwError "rfl failed, no @[refl] lemma registered for relation{indentExpr rel}"
/-- Helper theorem for `Lean.MVarId.liftReflToEq`. -/
private theorem rel_of_eq_and_refl {α : Sort _} {R : α α Prop}

View File

@@ -18,3 +18,5 @@ options get_default_options() {
return opts;
}
}
// please update stage0

View File

@@ -0,0 +1,15 @@
opaque f : Nat Nat
-- A rewrite with a free variable on the RHS
opaque P : Nat (Nat Nat) Prop
opaque Q : Nat Prop
opaque foo (g : Nat Nat) (x : Nat) : P x f Q (g x) := sorry
example : P x f Q (x + 10) := by
rewrite [foo]
-- we have an mvar now
with_reducible rfl -- should should instantiate it with the lambda on the RHS and close the goal
-- same as
-- with_reducible (apply (Iff.refl _))
-- NB: apply, not exact! Different defEq flags.

View File

@@ -0,0 +1,445 @@
/-!
This file tests the `rfl` tactic:
* Extensibility
* Error messages
* Effect of `with_reducible`
-/
-- First, let's see what `rfl` does:
/--
error: The rfl tactic failed. Possible reasons:
- The goal is not a reflexive relation (neither `=` nor a relation with a @[refl] lemma).
- The arguments of the relation are not equal.
Try using the reflexivity lemma for your relation explicitly, e.g. `exact Eq.refl _` or
`exact HEq.rfl` etc.
⊢ false = true
-/
#guard_msgs in
example : false = true := by rfl
-- Now to `apply_rfl`.
-- A plain reflexive predicate
inductive P : α α Prop where | refl : P a a
attribute [refl] P.refl
-- Plain error
/--
error: tactic 'apply_rfl' failed, The lhs
42
is not definitionally equal to rhs
23
⊢ P 42 23
-/
#guard_msgs in
example : P 42 23 := by apply_rfl
-- Revealing implicit arguments
opaque withImplicitNat {n : Nat} : Nat
/--
error: tactic 'apply_rfl' failed, The lhs
@withImplicitNat 42
is not definitionally equal to rhs
@withImplicitNat 23
⊢ P withImplicitNat withImplicitNat
-/
#guard_msgs in
example : P (@withImplicitNat 42) (@withImplicitNat 23) := by apply_rfl
-- Exhaustive testing of various combinations:
-- In addition to Eq, HEq and Iff we test four relations:
-- Defeq to relation `P` at reducible transparency
abbrev Q : α α Prop := P
-- Defeq to relation `P` at default transparency
def Q' : α α Prop := P
-- No refl attribute
inductive R : α α Prop where | refl : R a a
/-
Now we systematically test all relations with
* syntactic equal arguments
* reducibly equal arguments
* semireducibly equal arguments
* nonequal arguments
and all using `apply_rfl` and `with_reducible apply_rfl`
-/
-- Syntactic equal
example : true = true := by apply_rfl
example : HEq true true := by apply_rfl
example : True True := by apply_rfl
example : P true true := by apply_rfl
example : Q true true := by apply_rfl
/--
error: rfl failed, no @[refl] lemma registered for relation
Q'
-/
#guard_msgs in example : Q' true true := by apply_rfl -- Error
/--
error: rfl failed, no @[refl] lemma registered for relation
R
-/
#guard_msgs in example : R true true := by apply_rfl -- Error
example : true = true := by with_reducible apply_rfl
example : HEq true true := by with_reducible apply_rfl
example : True True := by with_reducible apply_rfl
example : P true true := by with_reducible apply_rfl
example : Q true true := by with_reducible apply_rfl
/--
error: rfl failed, no @[refl] lemma registered for relation
Q'
-/
#guard_msgs in
example : Q' true true := by with_reducible apply_rfl -- Error
/--
error: rfl failed, no @[refl] lemma registered for relation
R
-/
#guard_msgs in
example : R true true := by with_reducible apply_rfl -- Error
-- Reducibly equal
abbrev true' := true
abbrev True' := True
example : true' = true := by apply_rfl
example : HEq true' true := by apply_rfl
example : True' True := by apply_rfl
example : P true' true := by apply_rfl
example : Q true' true := by apply_rfl
/--
error: rfl failed, no @[refl] lemma registered for relation
Q'
-/
#guard_msgs in
example : Q' true' true := by apply_rfl -- Error
/--
error: rfl failed, no @[refl] lemma registered for relation
R
-/
#guard_msgs in
example : R true' true := by apply_rfl -- Error
example : true' = true := by with_reducible apply_rfl
example : HEq true' true := by with_reducible apply_rfl
example : True' True := by with_reducible apply_rfl
example : P true' true := by with_reducible apply_rfl
example : Q true' true := by with_reducible apply_rfl -- NB: No error, Q and true' reducible
/--
error: rfl failed, no @[refl] lemma registered for relation
Q'
-/
#guard_msgs in
example : Q' true' true := by with_reducible apply_rfl -- Error
/--
error: rfl failed, no @[refl] lemma registered for relation
R
-/
#guard_msgs in
example : R true' true := by with_reducible apply_rfl -- Error
-- Equal at default transparency only
def true'' := true
def True'' := True
example : true'' = true := by apply_rfl
example : HEq true'' true := by apply_rfl
example : True'' True := by apply_rfl
example : P true'' true := by apply_rfl
example : Q true'' true := by apply_rfl
/--
error: rfl failed, no @[refl] lemma registered for relation
Q'
-/
#guard_msgs in
example : Q' true'' true := by apply_rfl -- Error
/--
error: rfl failed, no @[refl] lemma registered for relation
R
-/
#guard_msgs in
example : R true'' true := by apply_rfl -- Error
/--
error: tactic 'apply_rfl' failed, The lhs
true''
is not definitionally equal to rhs
true
⊢ true'' = true
-/
#guard_msgs in
example : true'' = true := by with_reducible apply_rfl -- Error
/--
error: tactic 'apply' failed, failed to unify
@HEq ?α ?a ?α ?a
with
@HEq Bool true'' Bool true
⊢ HEq true'' true
-/
#guard_msgs in
example : HEq true'' true := by with_reducible apply_rfl -- Error
/--
error: tactic 'apply_rfl' failed, The lhs
True''
is not definitionally equal to rhs
True
⊢ True'' ↔ True
-/
#guard_msgs in
example : True'' True := by with_reducible apply_rfl -- Error
/--
error: tactic 'apply_rfl' failed, The lhs
true''
is not definitionally equal to rhs
true
⊢ P true'' true
-/
#guard_msgs in
example : P true'' true := by with_reducible apply_rfl -- Error
/--
error: tactic 'apply_rfl' failed, The lhs
true''
is not definitionally equal to rhs
true
⊢ Q true'' true
-/
#guard_msgs in
example : Q true'' true := by with_reducible apply_rfl -- Error
/--
error: tactic 'apply_rfl' failed, The lhs
true''
is not definitionally equal to rhs
true
⊢ Q' true'' true
-/
#guard_msgs in
example : Q' true'' true := by with_reducible apply_rfl -- Error
/--
error: tactic 'apply_rfl' failed, The lhs
true''
is not definitionally equal to rhs
true
⊢ R true'' true
-/
#guard_msgs in
example : R true'' true := by with_reducible apply_rfl -- Error
-- Unequal
/--
error: tactic 'apply_rfl' failed, The lhs
false
is not definitionally equal to rhs
true
⊢ false = true
-/
#guard_msgs in
example : false = true := by apply_rfl -- Error
/--
error: tactic 'apply' failed, failed to unify
HEq ?a ?a
with
HEq false true
⊢ HEq false true
-/
#guard_msgs in
example : HEq false true := by apply_rfl -- Error
/--
error: tactic 'apply_rfl' failed, The lhs
False
is not definitionally equal to rhs
True
⊢ False ↔ True
-/
#guard_msgs in
example : False True := by apply_rfl -- Error
/--
error: tactic 'apply_rfl' failed, The lhs
false
is not definitionally equal to rhs
true
⊢ P false true
-/
#guard_msgs in
example : P false true := by apply_rfl -- Error
/--
error: tactic 'apply_rfl' failed, The lhs
false
is not definitionally equal to rhs
true
⊢ Q false true
-/
#guard_msgs in
example : Q false true := by apply_rfl -- Error
/--
error: tactic 'apply_rfl' failed, The lhs
false
is not definitionally equal to rhs
true
⊢ Q' false true
-/
#guard_msgs in
example : Q' false true := by apply_rfl -- Error
/--
error: tactic 'apply_rfl' failed, The lhs
false
is not definitionally equal to rhs
true
⊢ R false true
-/
#guard_msgs in
example : R false true := by apply_rfl -- Error
/--
error: tactic 'apply_rfl' failed, The lhs
false
is not definitionally equal to rhs
true
⊢ false = true
-/
#guard_msgs in
example : false = true := by with_reducible apply_rfl -- Error
/--
error: tactic 'apply' failed, failed to unify
HEq ?a ?a
with
HEq false true
⊢ HEq false true
-/
#guard_msgs in
example : HEq false true := by with_reducible apply_rfl -- Error
/--
error: tactic 'apply_rfl' failed, The lhs
False
is not definitionally equal to rhs
True
⊢ False ↔ True
-/
#guard_msgs in
example : False True := by with_reducible apply_rfl -- Error
/--
error: tactic 'apply_rfl' failed, The lhs
false
is not definitionally equal to rhs
true
⊢ P false true
-/
#guard_msgs in
example : P false true := by with_reducible apply_rfl -- Error
/--
error: tactic 'apply_rfl' failed, The lhs
false
is not definitionally equal to rhs
true
⊢ Q false true
-/
#guard_msgs in
example : Q false true := by with_reducible apply_rfl -- Error
/--
error: tactic 'apply_rfl' failed, The lhs
false
is not definitionally equal to rhs
true
⊢ Q' false true
-/
#guard_msgs in
example : Q' false true := by with_reducible apply_rfl -- Error
/--
error: tactic 'apply_rfl' failed, The lhs
false
is not definitionally equal to rhs
true
⊢ R false true
-/
#guard_msgs in
example : R false true := by with_reducible apply_rfl -- Error
-- Inheterogeneous unequal
/--
error: tactic 'apply' failed, failed to unify
HEq ?a ?a
with
HEq true 1
⊢ HEq true 1
-/
#guard_msgs in
example : HEq true 1 := by apply_rfl -- Error
/--
error: tactic 'apply' failed, failed to unify
HEq ?a ?a
with
HEq true 1
⊢ HEq true 1
-/
#guard_msgs in
example : HEq true 1 := by with_reducible apply_rfl -- Error
-- Rfl lemma with side condition:
-- Error message should show left-over goal
inductive S : Bool Bool Prop where | refl : a = true S a a
attribute [refl] S.refl
/--
error: tactic 'apply_rfl' failed, The lhs
true
is not definitionally equal to rhs
false
⊢ S true false
-/
#guard_msgs in
example : S true false := by apply_rfl -- Error
/--
error: tactic 'apply_rfl' failed, The lhs
true
is not definitionally equal to rhs
false
⊢ S true false
-/
#guard_msgs in
example : S true false := by with_reducible apply_rfl -- Error
/--
error: unsolved goals
case a
⊢ true = true
-/
#guard_msgs in
example : S true true := by apply_rfl -- Error (left-over goal)
/--
error: unsolved goals
case a
⊢ true = true
-/
#guard_msgs in
example : S true true := by with_reducible apply_rfl -- Error (left-over goal)
/--
error: unsolved goals
case a
⊢ false = true
-/
#guard_msgs in
example : S false false := by apply_rfl -- Error (left-over goal)
/--
error: unsolved goals
case a
⊢ false = true
-/
#guard_msgs in
example : S false false := by with_reducible apply_rfl -- Error (left-over goal)