Skip to content

Commit 925bff5

Browse files
committed
fix(to_fun): fix the @[to_fun] attribute when the proof is not by reflexivity (#32866)
This PR fixes the bug in `@[to_fun]` reported in https://leanprover.zulipchat.com/#narrow/channel/287929-mathlib4/topic/.60to_fun.60.20tactic/with/563623809
1 parent 9fc4084 commit 925bff5

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

Mathlib/Tactic/ToFun.lean

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ initialize registerBuiltinAttribute {
4646
let r ← Push.pullCore .lambda type none
4747
if r.expr == type then
4848
throwError "`@[to_fun]` failed to eta-expand any part of `{.ofConstName src}`."
49-
return (← r.mkCast value, levels)
49+
-- Ensure that the returned `value` has type `r.expr`.
50+
let value ← match r.proof? with
51+
| none => mkExpectedTypeHint value r.expr
52+
| some proof => mkAppOptM ``cast #[type, r.expr, proof, value]
53+
return (value, levels)
5054
| _ => throwUnsupportedSyntax }
5155

5256
end Mathlib.Tactic

MathlibTest/ToFun.lean

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,17 @@ Look I am the doc-string of `foo`.
2424
run_meta
2525
let some doc ← findDocString? (← getEnv) ``fun_foo | throwError "no docstring found"
2626
logInfo doc
27+
28+
29+
-- Test that it also works when the generated proof is not a `rfl` proof:
30+
31+
@[to_additive (attr := push ← high)]
32+
lemma Pi.mul_def' {ι : Type*} {M : ι → Type*} [∀ i, Mul (M i)] (f g : ∀ i, M i) :
33+
f * g = fun i ↦ f i * g i := (rfl)
34+
35+
@[to_fun]
36+
theorem Function.mul_comm' (f g : ℝ → ℝ) : f * g = g * f := _root_.mul_comm f g
37+
38+
/-- info: Function.fun_mul_comm' (f g : ℝ → ℝ) : (fun i => f i * g i) = fun i => g i * f i -/
39+
#guard_msgs in
40+
#check Function.fun_mul_comm'

0 commit comments

Comments
 (0)