Zulip Chat Archive
Stream: metaprogramming / tactics
Topic: Different behaviour in `example` and `TermElabM`
Leni Aniva (Jan 07 2026 at 08:44):
Why does the first one fail and the second succeed? (Lean v4.25.2) The two targets look identical
example (a b c : Nat) (h : ¬a.le b) : ∀ (s : Decidable (a + c ≤ b + c)), (if s : a + c ≤ b + c then b + c else a + c) = a + c :=
Nat.decLe (a + b) (b + c)
#eval do
let statement ← Elab.Term.elabTerm (← `(term|∀ (a b c : Nat) (h : ¬a.le b) (s : Decidable (a + c ≤ b + c)), (if s : a + c ≤ b + c then b + c else a + c) = a + c)) .none
Meta.forallBoundedTelescope statement (.some 4) λ _fvars target => do
let sol ← Elab.Term.elabTerm (← `(term|Nat.decLe (a + b) (b + c))) (.some target)
let goal := (← Meta.mkFreshExprSyntheticOpaqueMVar target).mvarId!
logInfo s!"{← Meta.ppGoal goal}"
let flag ← goal.checkedAssign sol
Meta.check (.mvar goal)
logInfo s!"{flag}"
Notably it doesn't matter if I write Nat.decLe (a + b) or Nat.decLe (a + c)
Leni Aniva (Jan 07 2026 at 08:47):
Also, is there a programmatical way to solve this? Both rfl and simp can't handle the goal in example
Robin Arnez (Jan 07 2026 at 08:50):
Documentation of checkedAssign:
Returns
trueifmvarId := valwas successfully assigned. This method uses the same assignment validation performed byisDefEq, but it does not check whether the types match.
Leni Aniva (Jan 07 2026 at 08:50):
Robin Arnez said:
Documentation of
checkedAssign:Returns
trueifmvarId := valwas successfully assigned. This method uses the same assignment validation performed byisDefEq, but it does not check whether the types match.
Meta.check succeeds
Maybe thats not enough
Robin Arnez (Jan 07 2026 at 08:51):
See #lean4 > `check` doesn't check bvar/mvar/fvar exists @ 💬
Jovan Gerbscheid (Jan 07 2026 at 10:18):
If you want to verify that the types match, you should do that yourself, by running isDefEq (← inferType sol) target.
Instead, a better solution in this example may be to use elabTermEnsuringType, which checks this for you, and also inserts a coercion if necessary.
Last updated: Feb 28 2026 at 14:05 UTC