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

Commit f3babe0

Browse files
authored
Ref翻訳 (#31)
* 翻訳開始 * 翻訳完了 * 誤字修正
1 parent 63733d2 commit f3babe0

File tree

1 file changed

+77
-5
lines changed

1 file changed

+77
-5
lines changed

Manual/IO/Ref.lean

Lines changed: 77 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,41 +19,69 @@ set_option pp.rawOnError true
1919

2020
set_option linter.unusedVariables false
2121

22+
/-
2223
#doc (Manual) "Mutable References" =>
24+
-/
25+
#doc (Manual) "可変参照(Mutable References)" =>
2326

2427

28+
:::comment
2529
While ordinary {tech}[state monads] encode stateful computations with tuples that track the contents of the state along with the computation's value, Lean's runtime system also provides mutable references that are always backed by mutable memory cells.
2630
Mutable references have a type {lean}`IO.Ref` that indicates that a cell is mutable, and reads and writes must be explicit.
2731
{lean}`IO.Ref` is implemented using {lean}`ST.Ref`, so the entire {ref "mutable-st-references"}[{lean}`ST.Ref` API] may also be used with {lean}`IO.Ref`.
2832

33+
:::
34+
35+
通常の {tech}[state monads] は計算の値とともに状態の内容を追跡するタプルを使用して状態のある計算をエンコードしますが、Lean のランタイムシステムは、常に可変なメモリセルにバックアップされた可変参照も提供しています。可変参照は {lean}`IO.Ref` 型を持ちます。これによってセルが可変であることが示され、読み取りと書き込みは明示的に行う必要があります。 {lean}`IO.Ref` は {lean}`ST.Ref` を使って実装されているため、 {ref "mutable-st-references"}[{lean}`ST.Ref` API] をすべて {lean}`IO.Ref` と一緒に使うことができます。
36+
2937
{docstring IO.Ref}
3038

3139
{docstring IO.mkRef}
3240

3341

3442

43+
:::comment
3544
# State Transformers
45+
:::
46+
47+
# 状態変換(State Transformers)
48+
3649
%%%
3750
tag := "mutable-st-references"
3851
%%%
3952

4053

54+
:::comment
4155
Mutable references are often useful in contexts where arbitrary side effects are undesired.
4256
They can give a significant speedup when Lean is unable to optimize pure operations into mutation, and some algorithms are more easily expressed using mutable references than with state monads.
4357
Additionally, it has a property that other side effects do not have: if all of the mutable references used by a piece of code are created during its execution, and no mutable references from the code escape to other code, then the result of evaluation is deterministic.
4458

59+
:::
60+
61+
可変参照は恣意的な副作用が望ましくないコンテキストでしばしば有用です。こうした参照は Lean が純粋な演算をミューテーションに最適化できない場合に大幅なスピードアップをもたらすことが可能であり、状態モナドを使うよりも可変参照を使った方が簡単に表現できるアルゴリズムもあります。さらに可変参照には他の副作用にはない特性があります:コードの一部で使用されるすべての可変参照がその実行中に作成され、コードから他のコードに可変参照がエスケープされない場合、評価結果は決定論的です。
62+
63+
:::comment
4564
The {lean}`ST` monad is a restricted version of {lean}`IO` in which mutable state is the only side effect, and mutable references cannot escape.{margin}[{lean}`ST` was first described by {citehere launchbury94}[].]
4665
{lean}`ST` takes a type parameter that is never used to classify any terms.
4766
The {lean}`runST` function, which allow escape from {lean}`ST`, requires that the {lean}`ST` action that is passed to it can instantiate this type parameter with _any_ type.
4867
This unknown type does not exist except as a parameter to a function, which means that values whose types are “marked” by it cannot escape its scope.
4968

69+
:::
70+
71+
{lean}`ST` モナドは {lean}`IO` を副作用が可変状態だけにし、可変参照がエスケープしないように制限したバージョンです。 {margin}[{lean}`ST` は {citehere launchbury94}[] によって最初に記述されました。] {lean}`ST` は型パラメータを取りますが、その型パラメータはどの項の分類にも使用されません。 {lean}`ST` からのエスケープを可能にする {lean}`runST` 関数は、渡される {lean}`ST` アクションがこの型パラメータを _任意の_ 型でインスタンス化できることを要求します。この未知の型は関数のパラメータとして以外は存在しないため、この型によって「マーク」された値はそのスコープから逃れることができません。
72+
5073
{docstring ST}
5174

5275
{docstring runST}
5376

77+
:::comment
5478
As with {lean}`IO` and {lean}`EIO`, there is also a variation of {lean}`ST` that takes a custom error type as a parameter.
5579
Here, {lean}`ST` is analogous to {lean}`BaseIO` rather than {lean}`IO`, because {lean}`ST` cannot result in errors being thrown.
5680

81+
:::
82+
83+
{lean}`IO` や {lean}`EIO` と同様に、カスタムエラー型をパラメータとして受け取る {lean}`ST` のバリエーションも存在します。ここでは、 {lean}`ST` はエラーを投げることができないため、 {lean}`IO` より {lean}`BaseIO` に類似しています。
84+
5785
{docstring EST}
5886

5987
{docstring runEST}
@@ -62,13 +90,21 @@ Here, {lean}`ST` is analogous to {lean}`BaseIO` rather than {lean}`IO`, because
6290

6391
{docstring ST.mkRef}
6492

93+
:::comment
6594
## Reading and Writing
95+
:::
96+
97+
## 読み込みと書き込み(Reading and Writing)
98+
6699

67100
{docstring ST.Ref.get}
68101

69102
{docstring ST.Ref.set}
70103

71-
::::example "Data races with {name ST.Ref.get}`get` and {name ST.Ref.set}`set`"
104+
:::comment
105+
::example "Data races with {name ST.Ref.get}`get` and {name ST.Ref.set}`set`"
106+
:::
107+
::::example "{name ST.Ref.get}`get` と {name ST.Ref.set}`set` のデータ競合"
72108
:::ioExample
73109
```ioLean
74110
def main : IO Unit := do
@@ -107,12 +143,20 @@ Final balance is negative!
107143

108144
{docstring ST.Ref.modify}
109145

110-
::::example "Avoiding data races with {name ST.Ref.modify}`modify`"
146+
:::comment
147+
::example "Avoiding data races with {name ST.Ref.modify}`modify`"
148+
:::
149+
::::example "{name ST.Ref.modify}`modify` によるデータ競合の回避"
111150

151+
:::comment
112152
This program launches 100 threads.
113153
Each thread simulates a purchase attempt: it generates a random price, and if the account balance is sufficient, it decrements it by the price.
114154
The balance check and the computation of the new value occur in an atomic call to {name}`ST.Ref.modify`.
115155

156+
:::
157+
158+
このプログラムは100個のスレッドを起動します。各スレッドは購入をシミュレートします:ランダムな価格を生成し、口座の残高が十分であれば、その価格分だけ残高を減少させます。残高チェックと新しい値の計算は {name}`ST.Ref.modify` へのアトミックな呼び出しで行われます。
159+
116160
:::ioExample
117161
```ioLean
118162
def main : IO Unit := do
@@ -152,32 +196,55 @@ Final balance is zero or positive.
152196

153197
{docstring ST.Ref.modifyGet}
154198

199+
:::comment
155200
## Comparisons
201+
:::
202+
203+
## 比較(Comparisons)
204+
156205

157206
{docstring ST.Ref.ptrEq}
158207

208+
:::comment
159209
## Concurrency
210+
:::
211+
212+
## 並行性(Concurrency)
213+
160214

215+
:::comment
161216
Mutable references can be used as a locking mechanism.
162217
_Taking_ the contents of the reference causes attempts to take it or to read from it to block until it is {name ST.Ref.set}`set` again.
163218
This is a low-level feature that can be used to implement other synchronization mechanisms; it's usually better to rely on higher-level abstractions when possible.
164219

220+
:::
221+
222+
可変参照はロックメカニズムとして使用することができます。参照の内容を _取得_ すると、その参照を取得しようとしたり、そのリファレンスから読み込もうとしたりすると {name ST.Ref.set}`set` されるまでブロックされます。これは低レベルの機能であり、他の同期メカニズムを実装するために使用することができます;ただし可能であればより高度な抽象化に頼ったほうが良いです。
223+
165224
{docstring ST.Ref.take}
166225

167226
{docstring ST.Ref.swap}
168227

169228
{docstring ST.Ref.toMonadStateOf}
170229

171230

172-
::::example "Reference Cells as Locks"
231+
:::comment
232+
::example "Reference Cells as Locks"
233+
:::
234+
:::::example "ロックとしての参照セル"
235+
:::comment
173236
This program launches 100 threads.
174237
Each thread simulates a purchase attempt: it generates a random price, and if the account balance is sufficient, it decrements it by the price.
175238
If the balance is not sufficient, then it is not decremented.
176239
Because each thread {name ST.Ref.take}`take`s the balance cell prior to checking it and only returns it when it is finished, the cell acts as a lock.
177240
Unlike using {name}`ST.Ref.modify`, which atomically modifies the contents of the cell using a pure function, other {name}`IO` actions may occur in the critical section
178241
This program's `main` function is marked {keywordOf Lean.Parser.Command.declaration}`unsafe` because {name ST.Ref.take}`take` itself is unsafe.
179242

180-
:::ioExample
243+
:::
244+
245+
このプログラムは100個のスレッドを起動します。各スレッドは購入をシミュレートします:ランダムな価格を生成し、口座の残高が十分であれば、その価格分だけ残高を減少させます。もし残高が十分でなければ、残高を減らしません。各スレッドは残高を確認する前に残高セルを {name ST.Ref.take}`take` し、確認が終わるとそれを返すだけであるため、セルはロックとして機能します。 {name}`ST.Ref.modify` を使用して純粋関数を使用してアトミックにセルの内容を変更するのとは異なり、クリティカルなセクションで他の {name}`IO` アクションが発生する可能性があります。このプログラムの `main` 関数は {name ST.Ref.take}`take` 自体が安全ではないため、 {keywordOf Lean.Parser.Command.declaration}`unsafe` とマークされています。
246+
247+
::::ioExample
181248
```ioLean
182249
unsafe def main : IO Unit := do
183250
let balance ← IO.mkRef (100 : Int)
@@ -213,11 +280,16 @@ unsafe def main : IO Unit := do
213280
IO.println "Final balance is zero or positive."
214281
```
215282

283+
:::comment
216284
The program's output is:
285+
:::
286+
287+
このプログラムの出力は以下のようになります:
288+
217289
```stdout
218290
Sending out orders...
219291
Validation prevented a negative balance.
220292
Final balance is zero or positive.
221293
```
222-
:::
223294
::::
295+
:::::

0 commit comments

Comments
 (0)