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

Commit 5512acb

Browse files
committed
翻訳完了
1 parent c2189e1 commit 5512acb

File tree

1 file changed

+75
-6
lines changed

1 file changed

+75
-6
lines changed

Manual/IO/Ref.lean

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,41 +22,66 @@ set_option linter.unusedVariables false
2222
/-
2323
#doc (Manual) "Mutable References" =>
2424
-/
25-
#doc (Manual) "可変な参照(Mutable References)" =>
25+
#doc (Manual) "可変参照(Mutable References)" =>
2626

2727

28+
:::comment
2829
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.
2930
Mutable references have a type {lean}`IO.Ref` that indicates that a cell is mutable, and reads and writes must be explicit.
3031
{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`.
3132

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+
3237
{docstring IO.Ref}
3338

3439
{docstring IO.mkRef}
3540

3641

3742

43+
:::comment
3844
# State Transformers
45+
:::
46+
47+
# 状態変換(State Transformers)
48+
3949
%%%
4050
tag := "mutable-st-references"
4151
%%%
4252

4353

54+
:::comment
4455
Mutable references are often useful in contexts where arbitrary side effects are undesired.
4556
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.
4657
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.
4758

59+
:::
60+
61+
可変参照は恣意的な副作用が望ましくないコンテキストでしばしば有用です。こうした参照は Lean が純粋な演算をミューテーションに最適化できない場合に大幅なスピードアップをもたらすことが可能であり、状態モナドを使うよりも可変参照を使った方が簡単に表現できるアルゴリズムもあります。さらに可変参照には他の副作用にはない特性があります:コードの一部で使用されるすべての可変参照がその実行中に作成され、コードから他のコードに可変参照がエスケープされない場合、評価結果は決定論的です。
62+
63+
:::comment
4864
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}[].]
4965
{lean}`ST` takes a type parameter that is never used to classify any terms.
5066
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.
5167
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.
5268

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

5575
{docstring runST}
5676

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

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

6287
{docstring runEST}
@@ -65,13 +90,21 @@ Here, {lean}`ST` is analogous to {lean}`BaseIO` rather than {lean}`IO`, because
6590

6691
{docstring ST.mkRef}
6792

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

70100
{docstring ST.Ref.get}
71101

72102
{docstring ST.Ref.set}
73103

74-
::::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` のデータ競合"
75108
:::ioExample
76109
```ioLean
77110
def main : IO Unit := do
@@ -110,12 +143,20 @@ Final balance is negative!
110143

111144
{docstring ST.Ref.modify}
112145

113-
::::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` によるデータ競合の回避"
114150

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

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

156197
{docstring ST.Ref.modifyGet}
157198

199+
:::comment
158200
## Comparisons
201+
:::
202+
203+
## 比較(Comparisons)
204+
159205

160206
{docstring ST.Ref.ptrEq}
161207

208+
:::comment
162209
## Concurrency
210+
:::
211+
212+
## 並行性(Concurrency)
213+
163214

215+
:::comment
164216
Mutable references can be used as a locking mechanism.
165217
_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.
166218
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.
167219

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

170226
{docstring ST.Ref.swap}
171227

172228
{docstring ST.Ref.toMonadStateOf}
173229

174230

175-
::::example "Reference Cells as Locks"
231+
:::comment
232+
::example "Reference Cells as Locks"
233+
:::
234+
:::::example "ロックとしての参照セル"
235+
:::comment
176236
This program launches 100 threads.
177237
Each thread simulates a purchase attempt: it generates a random price, and if the account balance is sufficient, it decrements it by the price.
178238
If the balance is not sufficient, then it is not decremented.
179239
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.
180240
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
181241
This program's `main` function is marked {keywordOf Lean.Parser.Command.declaration}`unsafe` because {name ST.Ref.take}`take` itself is unsafe.
182242

183-
:::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
184248
```ioLean
185249
unsafe def main : IO Unit := do
186250
let balance ← IO.mkRef (100 : Int)
@@ -216,11 +280,16 @@ unsafe def main : IO Unit := do
216280
IO.println "Final balance is zero or positive."
217281
```
218282

283+
:::comment
219284
The program's output is:
285+
:::
286+
287+
このプログラムの出力は以下のようになります:
288+
220289
```stdout
221290
Sending out orders...
222291
Validation prevented a negative balance.
223292
Final balance is zero or positive.
224293
```
225-
:::
226294
::::
295+
:::::

0 commit comments

Comments
 (0)