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

Commit 7ae261d

Browse files
authored
2.5章 conveniences (#89)
* 翻訳開始 * 翻訳完了 * 誤訳修正
1 parent 29373c9 commit 7ae261d

File tree

2 files changed

+107
-1
lines changed

2 files changed

+107
-1
lines changed

functional-programming-lean/src/SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
- [ステップ・バイ・ステップ](./hello-world/step-by-step.md)
1919
- [プロジェクトの開始](./hello-world/starting-a-project.md)
2020
- [Worked Example: `cat`](./hello-world/cat.md)
21-
- [Additional Conveniences](./hello-world/conveniences.md)
21+
- [その他の便利機能](./hello-world/conveniences.md)
2222
- [まとめ](./hello-world/summary.md)
2323
- [休憩:命題・証明・リストの添え字アクセス](props-proofs-indexing.md)
2424
- [オーバーロードと型クラス](type-classes.md)

functional-programming-lean/src/hello-world/conveniences.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,151 @@
1+
<!--
12
# Additional Conveniences
3+
-->
24

5+
# その他の便利機能
36

7+
<!--
48
## Nested Actions
9+
-->
510

11+
## ネストされたアクション
12+
13+
<!--
614
Many of the functions in `feline` exhibit a repetitive pattern in which an `IO` action's result is given a name, and then used immediately and only once.
715
For instance, in `dump`:
16+
-->
17+
18+
`feline` の関数の多くは `IO` アクションの結果に名前を付けてすぐ一度だけ使うというパターンを繰り返していました。例えば、`dump` の場合:
19+
820
```lean
921
{{#include ../../../examples/feline/2/Main.lean:dump}}
1022
```
23+
<!--
1124
the pattern occurs for `stdout`:
25+
-->
26+
27+
`stdout` のところでこのパターンが発生しています:
28+
1229
```lean
1330
{{#include ../../../examples/feline/2/Main.lean:stdoutBind}}
1431
```
32+
<!--
1533
Similarly, `fileStream` contains the following snippet:
34+
-->
35+
36+
同様に、`fileStream` も以下のコード片を含みます:
37+
1638
```lean
1739
{{#include ../../../examples/feline/2/Main.lean:fileExistsBind}}
1840
```
1941

42+
<!--
2043
When Lean is compiling a `do` block, expressions that consist of a left arrow immediately under parentheses are lifted to the nearest enclosing `do`, and their results are bound to a unique name.
2144
This unique name replaces the origin of the expression.
2245
This means that `dump` can also be written as follows:
46+
-->
47+
48+
Leanが `do` ブロックをコンパイルする時、括弧のすぐ下にある左矢印からなる式は、それを包含する最も近い `do` に持ち上げられ、その式の結果が一意な名前に束縛されます。この一意名でもともとの式が置き換えられます。つまり、`dump` は次のように書くこともできます:
49+
2350
```lean
2451
{{#example_decl Examples/Cat.lean dump}}
2552
```
53+
<!--
2654
This version of `dump` avoids introducing names that are used only once, which can greatly simplify a program.
2755
`IO` actions that Lean lifts from a nested expression context are called _nested actions_.
56+
-->
2857

58+
このバージョンの `dump` では、一度しか使わない名前の導入を避けることができるため、プログラムを大幅に簡略にすることができます。ネストされた式のコンテキストからLeanが持ち上げた `IO` アクションは **ネストされたアクション** と呼ばれます。
59+
60+
<!--
2961
`fileStream` can be simplified using the same technique:
62+
-->
63+
64+
`fileStream` も同じテクニックを使って簡略にできます:
65+
3066
```lean
3167
{{#example_decl Examples/Cat.lean fileStream}}
3268
```
69+
<!--
3370
In this case, the local name of `handle` could also have been eliminated using nested actions, but the resulting expression would have been long and complicated.
3471
Even though it's often good style to use nested actions, it can still sometimes be helpful to name intermediate results.
72+
-->
73+
74+
ここでは、`handle` というローカル名をネストされたアクションを使って削除することもできますが、その場合は式が長く複雑になってしまいます。ネストされたアクションを使うことが良い場合が多いとはいえ、中間的な結果に名前を付けておくと便利なこともあります。
3575

76+
<!--
3677
It is important to remember, however, that nested actions are only a shorter notation for `IO` actions that occur in a surrounding `do` block.
3778
The side effects that are involved in executing them still occur in the same order, and execution of side effects is not interspersed with the evaluation of expressions.
3879
For an example of where this might be confusing, consider the following helper definitions that return data after announcing to the world that they have been executed:
80+
-->
81+
82+
しかし、ネストされたアクションは `do` ブロックの中で起こる `IO` アクションの短縮表記に過ぎないことを覚えておくことが重要です。アクションの実行に伴う副作用は同じ順序で発生し、副作用の実行が式の評価と混在することはありません。これが混乱を招く可能性がある例として、実行されたことを世界に通知した後にデータを返す以下の補助的な定義を考えてみましょう:
83+
3984
```lean
4085
{{#example_decl Examples/Cat.lean getNumA}}
4186
4287
{{#example_decl Examples/Cat.lean getNumB}}
4388
```
89+
<!--
4490
These definitions are intended to stand in for more complicated `IO` code that might validate user input, read a database, or open a file.
91+
-->
4592

93+
これらの定義はユーザ入力を検証したり、データベースを読み込んだり、ファイルを開いたりするようなより複雑な `IO` コードの代用を意図しています。
94+
95+
<!--
4696
A program that prints `0` when number A is five, or number `B` otherwise, can be written as follows:
97+
-->
98+
99+
数字Aが5の時は `0` を、それ以外の時は `B` の数値を表示するプログラムは次のように書けます:
100+
47101
```lean
48102
{{#example_decl Examples/Cat.lean testEffects}}
49103
```
104+
<!--
50105
However, this program probably has more side effects (such as prompting for user input or reading a database) than was intended.
51106
The definition of `getNumA` makes it clear that it will always return `5`, and thus the program should not read number B.
52107
However, running the program results in the following output:
108+
-->
109+
110+
しかし、このプログラムはおそらく意図した以上の副作用(ユーザ入力のプロンプトやデータベースの読み取りなど)を持ちます。`getNumA` の定義では、常に `5` を返すことが明確になっているので、このプログラムではBの数値を読み込むべきではありません。しかし、プログラムを実行すると次のような出力が得られます:
111+
53112
```output info
54113
{{#example_out Examples/Cat.lean runTest}}
55114
```
56115
`getNumB` was executed because `test` is equivalent to this definition:
57116
```lean
58117
{{#example_decl Examples/Cat.lean testEffectsExpanded}}
59118
```
119+
<!--
60120
This is due to the rule that nested actions are lifted to the _closest enclosing_ `do` block.
61121
The branches of the `if` were not implicitly wrapped in `do` blocks because the `if` is not itself a statement in the `do` block—the statement is the `let` that defines `a`.
62122
Indeed, they could not be wrapped this way, because the type of the conditional expression is `Nat`, not `IO Nat`.
123+
-->
63124

125+
これはネストされたアクションは **最も近い** `do` ブロックに持ち上げられるルールによるものです。なぜならここでの文は `a` を定義している `let` であって `if``do` ブロックの中の文ではないため、この `if` の分岐は暗黙的に `do` ブロックで囲まれないのです。実際、条件式の型は `IO Nat` ではなく `Nat` であるため、このようにラップすることはできません。
126+
127+
<!--
64128
## Flexible Layouts for `do`
129+
-->
130+
131+
## `do` の柔軟なレイアウト
65132

133+
<!--
66134
In Lean, `do` expressions are whitespace-sensitive.
67135
Each `IO` action or local binding in the `do` is expected to start on its own line, and they should all have the same indentation.
68136
Almost all uses of `do` should be written this way.
69137
In some rare contexts, however, manual control over whitespace and indentation may be necessary, or it may be convenient to have multiple small actions on a single line.
70138
In these cases, newlines can be replaced with a semicolon and indentation can be replaced with curly braces.
139+
-->
71140

141+
Leanにおいて、`do` 式は空白に対して敏感です。`do` の各 `IO` アクションや局所的な束縛はそれぞれ独立した行で始まり、インデントも同じでなければなりません。ほとんどすべての `do` はこのように書くべきです。しかしまれに空白やインデントを手動で制御する必要や、複数の小さなアクションを1行に書くと便利な場合があります。このような場合、改行はセミコロンに、インデントは波括弧に置き換えることができます。
142+
143+
<!--
72144
For instance, all of the following programs are equivalent:
145+
-->
146+
147+
例えば、以下のプログラムはすべての等価です:
148+
73149
```lean
74150
{{#example_decl Examples/Cat.lean helloOne}}
75151
@@ -78,28 +154,58 @@ For instance, all of the following programs are equivalent:
78154
{{#example_decl Examples/Cat.lean helloThree}}
79155
```
80156

157+
<!--
81158
Idiomatic Lean code uses curly braces with `do` very rarely.
159+
-->
160+
161+
慣用的なLeanの `do` のコードでは波括弧を使うことはほとんどありません。
82162

163+
<!--
83164
## Running `IO` Actions With `#eval`
165+
-->
84166

167+
## `#eval` による `IO` アクションの実行
168+
169+
<!--
85170
Lean's `#eval` command can be used to execute `IO` actions, rather than just evaluating them.
86171
Normally, adding a `#eval` command to a Lean file causes Lean to evaluate the provided expression, convert the resulting value to a string, and provide that string as a tooltip and in the info window.
87172
Rather than failing because `IO` actions can't be converted to strings, `#eval` executes them, carrying out their side effects.
88173
If the result of execution is the `Unit` value `()`, then no result string is shown, but if it is a type that can be converted to a string, then Lean displays the resulting value.
174+
-->
175+
176+
Leanの `#eval` コマンドは単に式の評価だけでなく、`IO` アクションを実行するために使用することができます。通常、`#eval` コマンドをLeanファイルに追加すると、Leanは指定された式を評価し、結果の値を文字列に変換して、その文字列をツールチップやinfoウィンドウに表示します。`IO` アクションは文字列に変換できないからといって失敗させるのではなく、`#eval` はそのアクションを実行し、その副作用を執り行います。実行結果が `Unit` 型の値 `()` の場合、結果の文字列は表示されませんが、文字列に変換できる型であればLeanは結果の値を表示します。
89177

178+
<!--
90179
This means that, given the prior definitions of `countdown` and `runActions`,
180+
-->
181+
182+
つまり、`countdown``runActions` の定義があらかじめ与えられているとした時:
183+
91184
```lean
92185
{{#example_in Examples/HelloWorld.lean evalDoesIO}}
93186
```
187+
<!--
94188
displays
189+
-->
190+
191+
は以下を出力します。
192+
95193
```output info
96194
{{#example_out Examples/HelloWorld.lean evalDoesIO}}
97195
```
196+
<!--
98197
This is the output produced by running the `IO` action, rather than some opaque representation of the action itself.
99198
In other words, for `IO` actions, `#eval` both _evaluates_ the provided expression and _executes_ the resulting action value.
199+
-->
100200

201+
これは `IO` アクションを実行することによって生成される出力であり、アクション自体の不透明な表現ではありません。言い換えれば、`IO` アクションに対して `#eval` は指定された式の **評価** と、結果のアクションの値の **実行** をどちらも行います。
202+
203+
<!--
101204
Quickly testing `IO` actions with `#eval` can be much more convenient that compiling and running whole programs.
102205
However, there are some limitations.
103206
For instance, reading from standard input simply returns empty input.
104207
Additionally, the `IO` action is re-executed whenever Lean needs to update the diagnostic information that it provides to users, and this can happen at unpredictable times.
105208
An action that reads and writes files, for instance, may do so at inconvenient times.
209+
-->
210+
211+
`#eval` を使って `IO` アクションを素早くテストすることはプログラム全体をコンパイルして実行することよりもはるかに便利です。しかし、いくつかの制限があります。例えば、標準入力からの読み込みは単に空の入力を返すだけになります。さらに、`IO` アクションはLeanがユーザに提供する新参情報を更新する必要があるたびに再実行され、これによって実行タイミングが予測できなくなります。例えば、ファイルを読み書きするアクションは、都合の悪い時に実行される可能性があります。

0 commit comments

Comments
 (0)