lean4-htt/tests/lean/run/safeShadowing.lean
Kyle Miller 3f80e530d3
feat: suppress safe shadowing within fun binders (#10376)
This PR modifies pretty printing of `fun` binders, suppressing the safe
shadowing feature among the binders in the same `fun`. For example,
rather than pretty printing as `fun x x => 0`, we now see `fun x x_1 =>
0`. The calculation is done per `fun`, so for example `fun x => id fun x
=> 0` pretty prints as-is, taking advantage of safe shadowing.

The motivation for this change is that many users have reported that
safe shadowing within the same `fun` is confusing.
2025-09-14 15:54:59 +00:00

108 lines
2 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.

/-!
# Tests of delaborator 'safe shadowing' feature
-/
set_option linter.unusedVariables false
def f (x x : Nat) :=
match x with
| 0 => x + 1
| Nat.succ _ => x + 2
def f' :=
fun (x : Nat) => id fun (x : Nat) =>
match x with
| 0 => x + 1
| Nat.succ _ => x + 2
variable {α : Type}
/-- info: fun a => a : αα -/
#guard_msgs in
#check fun (a : α) => a
/-- info: fun {α} a => a : {α : Sort u_1} → αα -/
#guard_msgs in
#check fun {α} (a : α) => a
/-!
This would be safe shadowing, but since it's in the same `fun` it is confusing to have repeated variable names.
-/
/-- info: fun x x_1 => x_1 : Nat → Nat → Nat -/
#guard_msgs in
#check fun (x x : Nat) => x
/-!
Splitting up the `fun`, safe shadowing applies again.
-/
/-- info: fun x => id fun x => x : Nat → Nat → Nat -/
#guard_msgs in
#check fun (x : Nat) => id fun (x : Nat) => x
/-!
Same lambda, no safe shadowing.
-/
/--
info: def f : Nat → Nat → Nat :=
fun x x_1 =>
match x_1 with
| 0 => x_1 + 1
| n.succ => x_1 + 2
-/
#guard_msgs in
#print f
/-!
Split up lambda, uses safe shadowing.
-/
/--
info: def f' : Nat → Nat → Nat :=
fun x =>
id fun x =>
match x with
| 0 => x + 1
| n.succ => x + 2
-/
#guard_msgs in
#print f'
/-!
The within-same-fun safe shadowing suppression does not take outer `let` into account.
-/
/--
info: let x := 1;
fun x x_1 => x_1 : Nat → Nat → Nat
-/
#guard_msgs in
#check let x := 1; fun (x x : Nat) => x
set_option pp.safeShadowing false
/-- info: fun {α_1} a => a : {α_1 : Sort u_1} → α_1 → α_1 -/
#guard_msgs in
#check fun {α} (a : α) => a
/-- info: fun x x_1 => x_1 : Nat → Nat → Nat -/
#guard_msgs in
#check fun (x x : Nat) => x
/--
info: def f : Nat → Nat → Nat :=
fun x x_1 =>
match x_1 with
| 0 => x_1 + 1
| n.succ => x_1 + 2
-/
#guard_msgs in
#print f
/--
info: def f' : Nat → Nat → Nat :=
fun x =>
id fun x_1 =>
match x_1 with
| 0 => x_1 + 1
| n.succ => x_1 + 2
-/
#guard_msgs in
#print f'