Skip to content

Commit 227e963

Browse files
authored
Merge pull request #1342 from lean-ja/Seasawher/issue1335
不要なnamesapceを使用しない
2 parents 6d84e8f + 7eef3da commit 227e963

28 files changed

+123
-201
lines changed

LeanByExample/Attribute/AppUnexpander.lean

-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/- # app_unexpander
22
`[app_unexpander]` 属性を付与すると、関数適用 `f a₁ a₂ ... aₙ` の `#check` コマンドおよび infoview での表示のされ方を変更することができます。
33
-/
4-
namespace AppUnexpander --#
54

65
/-- 人に挨拶をする関数 -/
76
def greet (x : String) := s!"Hello, {x}!"
@@ -69,5 +68,3 @@ def setOf.unexpander : Lean.PrettyPrinter.Unexpander
6968
/-- info: {n | ∃ m, n = 2 * m} : Set Nat -/
7069
#guard_msgs in
7170
#check {n : Nat | ∃ m, n = 2 * m}
72-
73-
end AppUnexpander --#

LeanByExample/Attribute/Csimp.lean

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
`A = B` という形の定理に付与することでコンパイラに `A` の計算を `B` の計算に置き換えさせることができます。非効率な関数を効率的な実装に置き換えるために使用されます。
55
-/
66
import Lean
7-
namespace Csimp --#
87

98
/-- フィボナッチ数列の非効率な実装 -/
109
def fibonacci : Nat → Nat
@@ -27,7 +26,8 @@ elab "#in_second " stx:command : command => do
2726

2827
-- `#eval fibonacci 32` は1秒以上かかる
2928
/-- error: It took more than one second for the command to run. -/
30-
#guard_msgs (error) in #in_second #eval fibonacci 32
29+
#guard_msgs (error) in
30+
#in_second #eval fibonacci 32
3131

3232
/-- フィボナッチ数列のより高速な実装 -/
3333
def fib (n : Nat) : Nat :=
@@ -58,5 +58,3 @@ theorem fib_eq_fibonacci : fibonacci = fib := by
5858
-- `fibonacci` の計算が1秒以内に終わるようになった
5959
#in_second #eval fibonacci 32
6060
#in_second #eval fibonacci 132
61-
62-
end Csimp --#

LeanByExample/Declarative/Attribute.lean

-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
66
次の例では、命題に `[simp]` 属性を付与しています。これは `simp` タクティクで利用される命題を増やすことを意味します。
77
-/
8-
namespace Attribute --#
98

109
theorem foo {P Q : Prop} : (P → Q) ∧ P ↔ Q ∧ P := by
1110
constructor <;> intro h
@@ -102,5 +101,3 @@ example (P Q : Prop) : ((P ∨ Q) ∧ ¬ Q) ↔ (P ∧ ¬ Q) := by
102101
| simp
103102

104103
sorry
105-
106-
end Attribute --#

LeanByExample/Declarative/Axiom.lean

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/- # axiom
22
`axiom` は、公理(axiom)を宣言するためのコマンドです。公理とは、議論の前提のことで、証明を与えることなく正しいと仮定される命題です。
33
-/
4-
namespace Axiom --#
54

65
/-- sorryAx を真似て作った公理 -/
76
axiom mySorryAx {P : Prop} : P
@@ -10,7 +9,7 @@ axiom mySorryAx {P : Prop} : P
109
theorem FLT : ∀ x y z n : Nat, n > 2 → x^n + y^n ≠ z^n := by
1110
apply mySorryAx
1211

13-
/-- info: 'Axiom.FLT' depends on axioms: [Axiom.mySorryAx] -/
12+
/-- info: 'FLT' depends on axioms: [mySorryAx] -/
1413
#guard_msgs in #print axioms FLT
1514

1615
/- ## 組み込みの公理
@@ -30,8 +29,9 @@ theorem ex_prop_ext (a b : Prop) (p : Prop → Prop) (h : a ↔ b) (h₁ : p a)
3029
rw [←this]
3130
assumption
3231

33-
/-- info: 'Axiom.ex_prop_ext' depends on axioms: [propext] -/
34-
#guard_msgs in #print axioms ex_prop_ext
32+
/-- info: 'ex_prop_ext' depends on axioms: [propext] -/
33+
#guard_msgs in
34+
#print axioms ex_prop_ext
3535

3636
/- ### 商の公理 Quot.sound
3737
任意の型 `α : Sort u` と `α` 上の2項関係 `r : α → α → Prop` に対して、その商(quotient)を作ることができます。商の概念は、以下に示す複数の定数から構成されます。
@@ -134,7 +134,7 @@ theorem lambda_eq (f : (x : α) → β x) : f = (fun x => f x) := by rfl
134134

135135
-- 依存関数 `f` がラムダ式 `fun x => f x` に等しいことは、定義から従うので
136136
-- 何の公理も必要としない。
137-
/-- info: 'Axiom.lambda_eq' does not depend on any axioms -/
137+
/-- info: 'lambda_eq' does not depend on any axioms -/
138138
#guard_msgs in
139139
#print axioms lambda_eq
140140

@@ -153,7 +153,7 @@ theorem funApp_eq (f : (x : α) → β x) : funApp (f := f) = f := calc
153153
_ = f := by rw [lambda_eq f]
154154

155155
-- これも何の公理も必要としない
156-
/-- info: 'Axiom.funApp_eq' does not depend on any axioms -/
156+
/-- info: 'funApp_eq' does not depend on any axioms -/
157157
#guard_msgs in
158158
#print axioms funApp_eq
159159

@@ -199,7 +199,7 @@ theorem my_funext {f g : (x : α) → β x} (h : ∀ x, f x = g x) : f = g := by
199199
-- 「`g` を適用する関数」と `g` は等しい
200200
_ = g := by rw [funApp_eq g]
201201

202-
/-- info: 'Axiom.my_funext' depends on axioms: [Quot.sound] -/
202+
/-- info: 'my_funext' depends on axioms: [Quot.sound] -/
203203
#guard_msgs in #print axioms my_funext
204204

205205
/- ### 選択原理 Classical.choice { #ClassicalChoice }
@@ -286,7 +286,7 @@ theorem lemma_em (himp : P → Q) (hor : ¬ Q ∨ P) : P ∨ ¬ P := by
286286
exact h
287287

288288
-- 何の公理も使用していない
289-
/-- info: 'Axiom.lemma_em' does not depend on any axioms -/
289+
/-- info: 'lemma_em' does not depend on any axioms -/
290290
#guard_msgs in #print axioms lemma_em
291291

292292
/- 選択原理を用いると命題 `Q` を構成することができ、関数外延性と命題外延性により、それが所望の性質を持つことを示すことができます。-/
@@ -349,5 +349,3 @@ theorem em (P : Prop) : P ∨ ¬ P := by
349349

350350
-- 残りの1つでは `u ≠ v` が成り立つ。
351351
simp [hu, hv]
352-
353-
end Axiom --#

LeanByExample/Declarative/Class.lean

-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# class
33
`class` は **型クラス(type class)** を定義するためのコマンドです。型クラスを用いると、複数の型に対して定義され、型ごとに異なる実装を持つような関数を定義することができます。例えば「和を取る操作」のような、`Nat` や `Int` や `Rat` など複数の型に対して同じ名前で定義したい関数を作りたいとき、型クラスが適しています。
44
-/
5-
namespace Class --#
65
/-- 証明なしのバージョンのモノイド。
76
ただしモノイドとは、要素同士を「くっつける」操作ができて、
87
くっつけても変わらない要素があるようなもののこと。-/
@@ -197,5 +196,3 @@ export HasCardinal (card)
197196

198197
-- Bool の濃度が計算できた
199198
#guard card Bool = Ordinal.nat 2
200-
201-
end Class --#

LeanByExample/Declarative/DeclareSyntaxCat.lean

-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
55
例として、集合の内包表記 `{x : T | P x}` を定義するコードを示します。
66
-/
7-
namespace DeclareSyntaxCat --#
87

98
universe u
109

@@ -61,5 +60,3 @@ macro_rules
6160
#check
6261
let Even := { x : Nat | x % 2 = 0 }
6362
{ x ∈ Even | x > 0 }
64-
65-
end DeclareSyntaxCat --#

LeanByExample/Declarative/Infix.lean

+4-10
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,31 @@
33
`infix` は、中置記法を定義するコマンドです。
44
-/
55
import Lean --#
6-
namespace Infix --#
76

87
-- 中置記法を定義。中身はただの掛け算
9-
-- 特定の名前空間でだけ有効にするために `scoped` を付けている
10-
scoped infix:60 " ⋄ " => Nat.mul
8+
infix:60 " ⋄ " => Nat.mul
119

1210
#guard 23 = 6
1311

1412
/- `:` の後に付けている数字は **パース優先順位(parsing precedence)** で、高いほど結合するタイミングが早くなります。等号 `=` のパース優先順位は 50 であることを覚えておくと良いかもしれません。-/
1513

1614
-- 等号より微妙にパース優先順位が高い
17-
scoped infix:51 " strong " => Nat.add
15+
infix:51 " strong " => Nat.add
1816

1917
-- きちんと 1 + (2 strong 3) = 6 と解釈される。
2018
-- これは、 等号のパース優先順位が 51 未満であることを意味する
2119
#check 1 + 2 strong 3 = 6
2220

2321
-- パース優先順位を 50 より低くすると等号より低くなる
2422
-- したがってエラーになる
25-
scoped infix:49 " weak " => Nat.add
23+
infix:49 " weak " => Nat.add
2624

2725
#guard_msgs (drop warning) in --#
2826
#check_failure 1 + 2 weak 3 = 6
2927

3028
/- `infix` で定義される記法は左結合でも右結合でもなく、必ず括弧が必要です。-/
31-
section
3229

33-
open Lean Parser
30+
open Lean Parser in
3431

3532
/-- `s : String` をパースして `Syntax` の項を得る。`cat` は構文カテゴリ。-/
3633
def parse (cat : Name) (s : String) : MetaM Syntax := do
@@ -43,7 +40,6 @@ def parse (cat : Name) (s : String) : MetaM Syntax := do
4340
-- 括弧を付ければOK
4441
#eval parse `term "1 ⋄ (2 ⋄ 3)"
4542

46-
end
4743
/- ## 舞台裏
4844
4945
`infix` は [`notation`](./Notation.md) コマンドに展開されるマクロとして実装されています。-/
@@ -68,5 +64,3 @@ elab "#expand " t:macro_stx : command => do
6864
/-- info: notation:50 lhs✝:51 " LXOR " rhs✝:51 => lxor lhs✝ rhs✝ -/
6965
#guard_msgs in
7066
#expand infix:50 " LXOR " => lxor
71-
72-
end Infix --#

LeanByExample/Declarative/Instance.lean

-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# instance
33
`instance` は、型クラスのインスタンスを定義するためのコマンドです。
44
-/
5-
namespace Instance --#
65

76
/-- 平面 -/
87
structure Point (α : Type) where
@@ -94,5 +93,3 @@ attribute [-instance] instListAdd
9493
-- リスト同士を足すことができなくなった
9594
#guard_msgs (drop warning) in --#
9695
#check_failure [1] + [2]
97-
98-
end Instance --#

LeanByExample/Declarative/Postfix.lean

+1-5
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@
33
`postfix` は、後置記法を定義するコマンドです。
44
-/
55
import Lean --#
6-
namespace Postfix --#
76

87
/-- 階乗 -/
98
def factorial : Nat → Nat
109
| 0 => 1
1110
| n + 1 => (n + 1) * factorial n
1211

1312
-- 後置記法を定義する
14-
-- scoped を付けたのは、この後置記法をこの名前空間内でのみ有効にするため
15-
scoped postfix:200 "!" => factorial
13+
postfix:200 "!" => factorial
1614

1715
-- 定義した記法が使える
1816
#guard 5! = 120
@@ -39,5 +37,3 @@ elab "#expand " t:macro_stx : command => do
3937
/-- info: notation:200 arg✝:200 "!" => factorial arg✝ -/
4038
#guard_msgs in
4139
#expand postfix:200 "!" => factorial
42-
43-
end Postfix --#

LeanByExample/Declarative/Prefix.lean

+1-5
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33
`prefix` は、前置記法を定義するためのコマンドです。
44
-/
55
import Lean --#
6-
namespace Prefix --#
76

87
-- 前置記法を定義
98
-- 中身は Nat.succ
10-
-- scoped を付けているのは、この記法をこの名前空間でのみ有効にするため
11-
scoped prefix:90 "⋄" => Nat.succ
9+
prefix:90 "⋄" => Nat.succ
1210

1311
-- 上で定義した記法が使える
1412
#guard ⋄3 = 4
@@ -35,5 +33,3 @@ elab "#expand " t:macro_stx : command => do
3533
/-- info: notation:90 "⋄" arg✝:90 => Nat.succ arg✝ -/
3634
#guard_msgs in
3735
#expand prefix:90 "⋄" => Nat.succ
38-
39-
end Prefix --#

LeanByExample/Declarative/Structure.lean

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# structure
33
`structure` は構造体を定義するためのコマンドです。構造体とは、複数のデータをまとめて一つの型として扱えるようにしたものです。
44
-/
5-
namespace Structure --#
65

76
/-- 2次元空間の点 -/
87
structure Point (α : Type) : Type where
@@ -30,6 +29,7 @@ example : Point.x origin = 0 := by rfl
3029
#check (Point.mk : {α : Type} → α → α → Point α)
3130

3231
/- コンストラクタに `mk` 以外の名前を使いたい場合、`::` を使って次のようにします。-/
32+
namespace Hidden --#
3333

3434
structure Prod (α : Type) (β : Type) where
3535
gen ::
@@ -39,6 +39,7 @@ structure Prod (α : Type) (β : Type) where
3939
-- コンストラクタの名前が gen になっている
4040
#check Prod.gen
4141

42+
end Hidden --#
4243
/- ## フィールド記法 { #FieldNotation }
4344
構造体 `S` の項 `s : S` に `x` というフィールドがあるとき、`S.x s` の代わりに `s.x` と書くことができます。これにより、`s : S` におけるフィールド `x` の値を取得することができます。この関数適用を省略して「あたかもフィールドのように」値にアクセスする記法のことを **フィールド記法(field notation)** といいます。
4445
@@ -178,5 +179,3 @@ warning: invalid {...} notation, structure type expected
178179
-/
179180
#guard_msgs in
180181
#check_failure ({ x := 1, y := 2 } : Point' Int)
181-
182-
end Structure --#

LeanByExample/Declarative/Theorem.lean

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# theorem
33
`theorem` は名前付きで命題を証明するためのコマンドです。より正確には、`theorem` は証明項を定義するためのコマンドだといえます。
44
-/
5-
namespace Theorem --#
65

76
/-- 自然数に右から0を足しても変わらない -/
87
theorem add_zero {n : Nat} : n + 0 = n := by simp
@@ -16,13 +15,11 @@ theorem add_zero {n : Nat} : n + 0 = n := by simp
1615
def add_zero' {n : Nat} : n + 0 = n := by simp
1716

1817
/--
19-
error: type of theorem 'Theorem.frac' is not a proposition
18+
error: type of theorem 'frac' is not a proposition
2019
Nat → Nat
2120
-/
2221
#guard_msgs (whitespace := lax) in --#
2322
theorem frac (n : Nat) : Nat :=
2423
match n with
2524
| 0 => 1
2625
| n + 1 => (n + 1) * frac n
27-
28-
end Theorem --#

LeanByExample/Diagnostic/Guard.lean

+3-6
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
-/
44
import Batteries.Data.List.Lemmas -- リストに対して `⊆` が使えるようにする
55

6-
namespace guard --#
7-
86
-- 階乗関数
97
def fac : Nat → Nat
108
| 0 => 1
@@ -32,7 +30,7 @@ but is expected to have type
3230
error: cannot evaluate code because 'sorryAx' uses 'sorry' and/or contains errors
3331
-/
3432
#guard_msgs (whitespace := lax) in
35-
#guard ((α : Type) → ∀ (l : List α), [] ⊆ l : Prop)
33+
#guard ((α : Type) → ∀ (l : List α), [] ⊆ l : Prop)
3634

3735
/- しかし、 `1 + 1 = 2` 等も `#check` で確かめてみると型は `Prop` です。にも関わらず `#guard` に渡してもエラーになりません。これは不思議に思えますが、理由は `1 + 1 = 2` が [`Decidable`](../TypeClass/Decidable.md) 型クラスのインスタンスであり、決定可能だからです。-/
3836

@@ -54,6 +52,5 @@ error: cannot evaluate code because 'sorryAx' uses 'sorry' and/or contains error
5452

5553
-- Bool 型になっている
5654
/-- info: decide (1 + 1 = 2) : Bool -/
57-
#guard_msgs in #check decide (1 + 1 = 2)
58-
59-
end guard --#
55+
#guard_msgs in
56+
#check decide (1 + 1 = 2)

LeanByExample/Diagnostic/Reduce.lean

-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/- # \#reduce
22
`#reduce` は、与えられた式をこれ以上簡約できなくなるまで簡約します。
33
-/
4-
namespace Reduce --#
54

65
/-- info: 4 -/
76
#guard_msgs in #reduce 1 + 3
@@ -20,5 +19,3 @@ def addOne (x : Nat) := x + 1
2019
-- 合成が計算できる
2120
/-- info: fun x => x.succ.succ -/
2221
#guard_msgs in #reduce addOne ∘ addOne
23-
24-
end Reduce --#

LeanByExample/Diagnostic/Time.lean

-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
`#time` は、コマンドの実行時間を計測するためのコマンドです。ミリ秒単位で結果を出してくれます。
33
-/
44
import Lean
5-
namespace Time --#
65

76
-- フィボナッチ数列の遅い実装
87
-- `n` に関して指数関数的な時間がかかる
@@ -78,5 +77,3 @@ elab "#in_second " stx:command : command => do
7877
/-- error: It took more than one second for the command to run. -/
7978
#guard_msgs (error) in
8079
#in_second #eval fibonacci 32
81-
82-
end Time --#

LeanByExample/Modifier/PrivateLib.lean

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ structure Point where
66

77
namespace Point
88

9-
protected def sub (p q : Point) : Point :=
10-
{ x := p.x - q.x, y := p.y - q.y }
9+
protected def sub (p q : Point) : Point :=
10+
{ x := p.x - q.x, y := p.y - q.y }
1111

12-
private def private_sub := Point.sub
12+
private def private_sub := Point.sub
1313

1414
end Point

0 commit comments

Comments
 (0)