Skip to content
This repository was archived by the owner on Jan 5, 2025. It is now read-only.

Commit 4802f6d

Browse files
authored
RecursorExample翻訳 (#34)
* 翻訳開始 * 翻訳完了 * versoの文法をミスっていたため修正
1 parent 3742728 commit 4802f6d

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

Manual/Language/RecursiveDefs/Structural/RecursorExample.lean

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,63 @@ open Verso.Genre Manual
1313
open Lean.Elab.Tactic.GuardMsgs.WhitespaceMode
1414

1515

16+
/-
1617
#doc (Manual) "Recursion Example (for inclusion elsewhere)" =>
18+
-/
19+
#doc (Manual) "再帰の例(Recursion Example (for inclusion elsewhere))" =>
1720

1821

1922
```lean (show := false)
2023
section
2124
variable (n k : Nat) (mot : Nat → Sort u)
2225
```
23-
:::example "Recursion vs Recursors"
26+
:::comment
27+
::example "Recursion vs Recursors"
28+
:::
29+
::::example "再帰 vs 再帰子"
30+
:::comment
2431
Addition of natural numbers can be defined via recursion on the second argument.
2532
This function is straightforwardly structurally recursive.
33+
:::
34+
35+
自然数の足し算は、第2引数に対する再帰によって定義することができます。この関数は単純な構造的再帰です。
36+
2637
```lean
2738
def add (n : Nat) : Nat → Nat
2839
| .zero => n
2940
| .succ k => .succ (add n k)
3041
```
3142

43+
:::comment
3244
Defined using {name}`Nat.rec`, it is much further from the notations that most people are used to.
45+
:::
46+
47+
{name}`Nat.rec` を使って定義すると、多くの人が慣れ親しんでいる表記からかけ離れています。
48+
3349
```lean
3450
def add' (n : Nat) :=
3551
Nat.rec (motive := fun _ => Nat)
3652
n
3753
(fun k soFar => .succ soFar)
3854
```
3955

56+
:::comment
4057
Structural recursive calls made on data that isn't the immediate child of the function parameter requires either creativity or a complex yet systematic encoding.
58+
:::
59+
60+
関数パラメータの直接の子要素ではないデータに対して行われる構造的再帰呼び出しには、創造性か、複雑ですが体系的なエンコーディングのどちらかが必要です。
61+
4162
```lean
4263
def half : Nat → Nat
4364
| 0 | 1 => 0
4465
| n + 2 => half n + 1
4566
```
67+
:::comment
4668
One way to think about this function is as a structural recursion that flips a bit at each call, only incrementing the result when the bit is set.
69+
:::
70+
71+
この関数についての1つの考え方は、呼び出すたびにビットを反転させ、ビットがセットされたときだけ結果をインクリメントする構造的再帰です。
72+
4773
```lean
4874
def helper : Nat → Bool → Nat :=
4975
Nat.rec (motive := fun _ => Bool → Nat)
@@ -61,11 +87,16 @@ def half' (n : Nat) : Nat := helper n false
6187
[0, 0, 1, 1, 2, 2, 3, 3, 4]
6288
```
6389

90+
:::comment
6491
Instead of creativity, a general technique called {deftech}[course-of-values recursion] can be used.
6592
Course-of-values recursion uses helpers that can be systematically derived for every inductive type, defined in terms of the recursor; Lean derives them automatically.
6693
For every {lean}`Nat` {lean}`n`, the type {lean}`n.below (motive := mot)` provides a value of type {lean}`mot k` for all {lean}`k < n`, represented as an iterated {TODO}[xref sigma] dependent pair type.
6794
The course-of-values recursor {name}`Nat.brecOn` allows a function to use the result for any smaller {lean}`Nat`.
6895
Using it to define the function is inconvenient:
96+
:::
97+
98+
創造性の代わりに、 {deftech}[累積再帰] (course-of-values recursion)と呼ばれる一般的なテクニックを使用することができます。累積再帰では、再帰子で定義されたすべての帰納型に対して体系的に導出できる補助関数を使用します;Lean はこれらを自動で導出します。すべての {lean}`Nat` {lean}`n` に対して、 {lean}`n.below (motive := mot)` 型はすべての {lean}`k < n` に対して {lean}`mot k` 型の値を提供し、繰り返された依存ペア型として表現されます。累積再帰子 {name}`Nat.brecOn` は関数が任意の小さい {lean}`Nat` に対して結果を使用することを可能にします。これを使用して関数を定義することは不便です:
99+
69100
```lean
70101
noncomputable def half'' (n : Nat) : Nat :=
71102
Nat.brecOn n (motive := fun _ => Nat)
@@ -74,16 +105,26 @@ noncomputable def half'' (n : Nat) : Nat :=
74105
| 0, _ | 1, _ => 0
75106
| _ + 2, ⟨_, ⟨h, _⟩⟩ => h + 1
76107
```
108+
:::comment
77109
The function is marked {keywordOf Lean.Parser.Command.declaration}`noncomputable` because the compiler doesn't support generating code for course-of-values recursion, which is intended for reasoning rather that efficient code.
78110
The kernel can still be used to test the function, however:
111+
:::
112+
113+
この関数は {keywordOf Lean.Parser.Command.declaration}`noncomputable` とマークされていますが、これはコンパイラが累積再帰のコード生成をサポートしていないためです。というのもこれは効率的なコードではなく推論用を意図されたものだからです。しかし、カーネルを関数のテストのために使用することができます:
114+
79115
```lean (name := halfTest2)
80116
#reduce [0,1,2,3,4,5,6,7,8].map half''
81117
```
82118
```leanOutput halfTest2
83119
[0, 0, 1, 1, 2, 2, 3, 3, 4]
84120
```
85121

122+
:::comment
86123
The dependent pattern matching in the body of {lean}`half''` can also be encoded using recursors (specifically, {name}`Nat.casesOn`), if necessary:
124+
:::
125+
126+
必要に応じて {lean}`half''` の本体で依存パターンマッチを再帰子(具体的には {name}`Nat.casesOn` )を使ってエンコードすることもできます:
127+
87128
```lean
88129
noncomputable def half''' (n : Nat) : Nat :=
89130
n.brecOn (motive := fun _ => Nat)
@@ -104,17 +145,27 @@ noncomputable def half''' (n : Nat) : Nat :=
104145
(fun _ soFar => soFar.2.1.succ))
105146
```
106147

148+
:::comment
107149
This definition still works.
150+
:::
151+
152+
この定義も動作します。
153+
108154
```lean (name := halfTest3)
109155
#reduce [0,1,2,3,4,5,6,7,8].map half''
110156
```
111157
```leanOutput halfTest3
112158
[0, 0, 1, 1, 2, 2, 3, 3, 4]
113159
```
114160

161+
:::comment
115162
However, it is now far from the original definition and it has become difficult for most people to understand.
116163
Recursors are an excellent logical foundation, but not an easy way to write programs or proofs.
117164
:::
165+
166+
しかし、もはや本来の定義からかけ離れ、多くの人にとって理解しにくいものになっています。再帰子は優れた論理的基礎ですが、プログラムや証明を書くには簡単な方法ではありません。
167+
168+
::::
118169
```lean (show := false)
119170
end
120171
```

0 commit comments

Comments
 (0)