Skip to content

Commit 4ebf126

Browse files
authored
Merge pull request #437 from lean-ja/Seasawher/issue226
コマンド紹介: infix を紹介する
2 parents 750ee22 + e373ef1 commit 4ebf126

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

.lycheeignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
https://raw.githubusercontent.com/lean-ja/lean-by-example/.*
22
https://adam.math.hhu.de/#/g/leanprover-community/NNG4
3+
https://live.lean-lang.org/
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/- # infix
2+
`infix` は,中置記法を定義する構文です.
3+
-/
4+
import Lean --#
5+
namespace Infix --#
6+
7+
-- 中置記法を定義. 中身はただの掛け算
8+
-- 特定の名前空間でだけ有効にするために `scoped` を付けている
9+
scoped infix:60 " ⋄ " => Nat.mul
10+
11+
#guard 23 = 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 --#

src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
- [example: 名前をつけずに証明](./Command/Declarative/Example.md)
3131
- [export: 現在の名前空間に追加](./Command/Declarative/Export.md)
3232
- [inductive: 帰納型を定義する](./Command/Declarative/Inductive.md)
33+
- [infix: 中置記法](./Command/Declarative/Infix.md)
3334
- [instance: インスタンスを定義する](./Command/Declarative/Instance.md)
3435
- [local: 有効範囲をセクションで限定](./Command/Declarative/Local.md)
3536
- [namespace: 名前空間を宣言する](./Command/Declarative/Namespace.md)

0 commit comments

Comments
 (0)