File tree 3 files changed +46
-0
lines changed
Examples/Command/Declarative
3 files changed +46
-0
lines changed Original file line number Diff line number Diff line change 1
1
https://raw.githubusercontent.com/lean-ja/lean-by-example/.*
2
2
https://adam.math.hhu.de/#/g/leanprover-community/NNG4
3
+ https://live.lean-lang.org/
Original file line number Diff line number Diff line change
1
+ /- # infix
2
+ `infix` は,中置記法を定義する構文です.
3
+ -/
4
+ import Lean --#
5
+ namespace Infix --#
6
+
7
+ -- 中置記法を定義. 中身はただの掛け算
8
+ -- 特定の名前空間でだけ有効にするために `scoped` を付けている
9
+ scoped infix :60 " ⋄ " => Nat.mul
10
+
11
+ #guard 2 ⋄ 3 = 6
12
+
13
+ /- `:` の後に付けている数字はパースの優先順位で,高いほど結合するタイミングが早くなります.等号 `=` の優先順位は 50 であることを覚えておくと良いかもしれません.-/
14
+
15
+ -- 等号より微妙に優先順位が高い
16
+ scoped infix :51 " strong " => Nat.add
17
+
18
+ -- きちんと 1 + (2 strong 3) = 6 と解釈される.
19
+ -- これは, 等号の優先順位が 51 未満であることを意味する
20
+ #check 1 + 2 strong 3 = 6
21
+
22
+ -- 優先順位を 50 より小さくすると等号より優先順位が低くなる
23
+ -- したがってエラーになる
24
+ scoped infix :49 " weak " => Nat.add
25
+
26
+ #check_failure 1 + 2 weak 3 = 6
27
+
28
+ /- ## 舞台裏
29
+
30
+ `infix` は [ `notation` ] (./Notation.md) コマンドの特別な場合であり,実際内部的にはマクロとして展開されます.以下の例では,`infix:50 " LXOR " => fun l r => (!l && r)` が `notation:50 lhs:51 " LXOR " rhs:51 => (fun l r => (!l && r)) lhs rhs` とマクロ展開されていることがわかります.-/
31
+
32
+ open Lean
33
+
34
+ /-- コマンドをマクロ展開するコマンド -/
35
+ elab "#expand_command " t:command : command => do
36
+ match ← Elab.liftMacroM <| Lean.Macro.expandMacro? t with
37
+ | none => logInfo m!"Not a macro"
38
+ | some t =>
39
+ logInfo m!"{t}"
40
+
41
+ /-- info: notation:50 lhs✝:51 " LXOR " rhs✝:51 => (fun l r => (!l && r)) lhs✝ rhs✝ -/
42
+ #guard_msgs in #expand_command infix :50 " LXOR " => fun l r => (!l && r)
43
+
44
+ end Infix --#
Original file line number Diff line number Diff line change 30
30
- [ example: 名前をつけずに証明] ( ./Command/Declarative/Example.md )
31
31
- [ export: 現在の名前空間に追加] ( ./Command/Declarative/Export.md )
32
32
- [ inductive: 帰納型を定義する] ( ./Command/Declarative/Inductive.md )
33
+ - [ infix: 中置記法] ( ./Command/Declarative/Infix.md )
33
34
- [ instance: インスタンスを定義する] ( ./Command/Declarative/Instance.md )
34
35
- [ local: 有効範囲をセクションで限定] ( ./Command/Declarative/Local.md )
35
36
- [ namespace: 名前空間を宣言する] ( ./Command/Declarative/Namespace.md )
You can’t perform that action at this time.
0 commit comments