blob: b7af81c7ada62271d169ed140917c9ef98482012 [file] [edit]
import Lean
/-!
Tests that `notation` and the mixfix commands register their macro and unexpander as data instead of
generating `macro_rules` and unexpander declarations, and that these behave like the generated ones.
-/
open Lean Elab
def myAdd (a b : Nat) : Nat := a + b
notation:65 a:65 " +' " b:66 => myAdd a b
infixl:70 " *' " => Nat.mul
prefix:max "√'" => Nat.succ
postfix:max "!'" => Nat.pred
notation "ℕ'" => Nat
/-- info: 1 +' 2 +' 3 : ℕ' -/
#guard_msgs in
#check 1 +' 2 +' 3
/-- info: (√'1 *' 2)!' : ℕ' -/
#guard_msgs in
#check (√'1 *' 2)!'
/-- info: 9 -/
#guard_msgs in
#eval 1 +' 2 *' 4
/-! No declarations are generated beyond the parser. -/
/-- info: [term_!', «term_*'_», «term_+'_», termℕ', «term√'_»] -/
#guard_msgs in
run_cmd
let env ← getEnv
let decls := env.constants.map₂.toList.map (·.1) |>.filter fun n =>
n != ``myAdd && !n.isInternalDetail || (`term).isPrefixOf n
logInfo m!"{decls.toArray.qsort Name.lt}"
/-- info: [(«term_+'_», true)] -/
#guard_msgs in
run_cmd
let env ← getEnv
let entries := macroAttribute.getEntries env `«term_+'_»
logInfo m!"{entries.map fun e => (e.declName, (notationRuleExt.find? env e.declName).isSome)}"
/-! Hygiene: binders of the expansion do not capture variables of the arguments. -/
def apply (f : Nat → Nat) (x : Nat) := f x
notation "twice " e => apply (fun x => e + x) 1
/-- info: 11 -/
#guard_msgs in
#eval let x := 10; twice x
/-- info: twice 5 : ℕ' -/
#guard_msgs in
#check twice 5
/-! The right-hand side is resolved at the declaration. -/
namespace Foo
def secret := 42
notation "secret!" => secret
end Foo
/-- info: 42 -/
#guard_msgs in
#eval secret!
/-- error: Unknown identifier `doesNotExist` at quotation precheck
Note: You can use `set_option quotPrecheck false` to disable this check. -/
#guard_msgs in
notation "bad!" => doesNotExist
/-! Unhygienic notations can capture. -/
set_option hygiene false in
notation "captured!" => y
/-- info: 5 -/
#guard_msgs in
#eval let y := 5; captured!
/-! Local notations can refer to section variables, and are gone after the section. -/
section
variable (n : Nat)
local notation "n!" => n
theorem n_eq : n! = n := rfl
end
/-- error: Unknown identifier `n!` -/
#guard_msgs in
#check n!
/-! Scoped notations. -/
namespace Bar
scoped notation "bar!" => 7
end Bar
/-- error: Unknown identifier `bar!` -/
#guard_msgs in
#check bar!
open Bar in
/-- info: 7 -/
#guard_msgs in
#eval bar!
/-! Unexpanders match the structure of the arguments. -/
def setOf' (p : Nat → Prop) : Nat → Prop := p
notation "{' " x " | " p " }" => setOf' (fun x => p)
/-- info: {' x | x = 1 } : ℕ' → Prop -/
#guard_msgs in
#check {' x | x = 1 }
def isOne (x : Nat) : Prop := x = 1
/-- info: setOf' isOne : ℕ' → Prop -/
#guard_msgs in
#check setOf' isOne
notation "one+ " x => myAdd 1 x
/-- info: one+ 2 : ℕ' -/
#guard_msgs in
#check myAdd 1 2
/-- info: 2 +' 2 : ℕ' -/
#guard_msgs in
#check myAdd 2 2
/-! Variables not occurring in the expansion do not prevent the notation. -/
notation "ignore " _x => Nat.zero
/-- info: Nat.zero : ℕ' -/
#guard_msgs in
#check ignore 5
/-! Variables are recorded in the info trees, so they are highlighted and linted like local variables. -/
set_option linter.unusedVariables true
/--
warning: Variable name `y` is not explicitly referenced.
Hint: The binding can be removed (if unused) or named `_` (if used implicitly). Alternatively, prefix the name with `_` to silence this warning:
[apply] _y
Note: This linter can be disabled with `set_option linter.unusedVariables false`
-/
#guard_msgs in
notation "first " x ", " y => Nat.succ x
set_option linter.unusedVariables false
/-! Later `macro_rules` take precedence, and the attribute can be erased. -/
notation "answer" => 1
macro_rules | `(answer) => `(2)
/-- info: 2 -/
#guard_msgs in
#eval answer
attribute [-macro] termAnswer
/-- info: 2 -/
#guard_msgs in
#eval answer
/-! Notations with repeated variables fall back to generated definitions. -/
set_option linter.unusedVariables false in
notation "dup " x ", " x => myAdd x 1
/-- info: 2 -/
#guard_msgs in
#eval dup 1, 2
/-- info: false -/
#guard_msgs in
#eval show CoreM Bool from return (notationRuleExt.find? (← getEnv) `«termDup_,_»).isSome
/-! Syntax defined by `syntax` is pretty-printed without evaluating its parser declaration. -/
syntax (name := mySyntax) "mySyntax(" term,+ ")" : term
/-- info: mySyntax(1, 2) -/
#guard_msgs in
#eval show CoreM Format from do PrettyPrinter.ppTerm (← `(mySyntax(1, 2)))
/-- info: true -/
#guard_msgs in
#eval show CoreM Bool from return (Parser.parserDescrExt.find? (← getEnv) ``mySyntax).isSome
/-! All parser declarations of this file, including those of local notations, have a recorded value. -/
/-- info: [] -/
#guard_msgs in
run_cmd
let env := (← getEnv).setExporting false
let missing := env.constants.map₂.toList.filterMap fun (n, info) =>
if (info.type.isConstOf ``ParserDescr || info.type.isConstOf ``TrailingParserDescr) &&
!Parser.parserDescrExt.contains env n then
some n
else
none
logInfo m!"{missing}"