Skip to content

Commit edc2afc

Browse files
Dev (#66)
* Macro for `\mapsTo` in introduction (#65) Macro for \mapsTo in introduction * run viper Co-authored-by: Siddhartha Gadgil <[email protected]>
1 parent b36e5f8 commit edc2afc

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

lean/main/intro.lean

+19-3
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,25 @@ Let's see some example use cases of metaprogramming in Lean.
7878
The following examples are meant for mere illustration. Don't worry if you don't
7979
understand the details for now.
8080
81+
### Introducing notation (defining new syntax)
82+
83+
Often one wants to introduce new notation, for example one more suitable for (a branch of) mathematics. For instance, in mathematics one would write the function adding `2` to a natural number as `x : Nat ↦ x + 2` or simply `x ↦ x + 2` if the domain can be inferred to be the natural numbers. The corresponding lean definitions `fun x : Nat => x + 2` and `fun x => x + 2` use `=>` which in mathematics means _implication_, so may be confusing to some.
84+
85+
We can introduce notation using a `macro` which transforms our syntax to lean's own syntax (or syntax we previously defined). Here we introduce the `↦` notation for functions.
86+
-/
87+
import Lean
88+
89+
macro x:ident ":" t:term " ↦ " y:term : term => do
90+
`(fun $x : $t => $y)
91+
92+
#eval (x : Nat ↦ x + 2) 2 -- 4
93+
94+
macro x:ident " ↦ " y:term : term => do
95+
`(fun $x => $y)
96+
97+
#eval (x ↦ x + 2) 2 -- 4
98+
/-!
99+
81100
### Building a command
82101
83102
Suppose we want to build a helper command `#assertType` which tells whether a
@@ -87,9 +106,6 @@ given term is of a certain type. The usage will be:
87106
88107
Let's see the code:
89108
-/
90-
91-
import Lean
92-
93109
elab "#assertType " termStx:term " : " typeStx:term : command =>
94110
open Lean Lean.Elab Command Term in
95111
liftTermElabM `assertTypeCmd

md/main/intro.md

+20-2
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,26 @@ Let's see some example use cases of metaprogramming in Lean.
7777
The following examples are meant for mere illustration. Don't worry if you don't
7878
understand the details for now.
7979

80+
### Introducing notation (defining new syntax)
81+
82+
Often one wants to introduce new notation, for example one more suitable for (a branch of) mathematics. For instance, in mathematics one would write the function adding `2` to a natural number as `x : Nat ↦ x + 2` or simply `x ↦ x + 2` if the domain can be inferred to be the natural numbers. The corresponding lean definitions `fun x : Nat => x + 2` and `fun x => x + 2` use `=>` which in mathematics means _implication_, so may be confusing to some.
83+
84+
We can introduce notation using a `macro` which transforms our syntax to lean's own syntax (or syntax we previously defined). Here we introduce the `` notation for functions.
85+
86+
```lean
87+
import Lean
88+
89+
macro x:ident ":" t:term " ↦ " y:term : term => do
90+
`(fun $x : $t => $y)
91+
92+
#eval (x : Nat ↦ x + 2) 2 -- 4
93+
94+
macro x:ident " ↦ " y:term : term => do
95+
`(fun $x => $y)
96+
97+
#eval (x ↦ x + 2) 2 -- 4
98+
```
99+
80100
### Building a command
81101

82102
Suppose we want to build a helper command `#assertType` which tells whether a
@@ -87,8 +107,6 @@ given term is of a certain type. The usage will be:
87107
Let's see the code:
88108

89109
```lean
90-
import Lean
91-
92110
elab "#assertType " termStx:term " : " typeStx:term : command =>
93111
open Lean Lean.Elab Command Term in
94112
liftTermElabM `assertTypeCmd

0 commit comments

Comments
 (0)