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

Commit b83e1ff

Browse files
authored
Monad_Lift翻訳 (#38)
* 翻訳開始 * 翻訳完了 * versoの文法ミス修正 * 用語回避
1 parent 49b3eba commit b83e1ff

File tree

2 files changed

+144
-11
lines changed

2 files changed

+144
-11
lines changed

Manual/Monads/Lift.lean

Lines changed: 143 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,65 +19,109 @@ set_option pp.rawOnError true
1919

2020
set_option linter.unusedVariables false
2121

22+
/-
2223
#doc (Manual) "Lifting Monads" =>
24+
-/
25+
#doc (Manual) "モナドの持ち上げ(Lifting Monads)" =>
2326

24-
::::keepEnv
27+
:::::keepEnv
2528

2629
```lean (show := false)
2730
variable {m m' n : Type u → Type v} [Monad m] [Monad m'] [Monad n] [MonadLift m n]
2831
variable {α β : Type u}
2932
```
3033

34+
:::comment
3135
When one monad is at least as capable as another, then actions from the latter monad can be used in a context that expects actions from the former.
3236
This is called {deftech (key := "lift")}_lifting_ the action from one monad to another.
3337
Lean automatically inserts lifts when they are available; lifts are defined in the {name}`MonadLift` type class.
3438
Automatic monad lifting is attempted before the general {tech}[coercion] mechanism.
3539

40+
:::
41+
42+
あるモナドが別のモナドに対して最低限同等の能力を持つとき、後者のモナドからのアクションは前者のモナドからのアクションを期待するコンテキストで使うことができます。これはあるモナドから別のモナドへのアクションの {deftech}_持ち上げ_ (lift)と呼ばれます。Lean は可能な場合は自動で持ち上げを挿入します;持ち上げは {name}`MonadLift` 型クラスで定義されます。モナドの自動持ち上げは一般的な {tech}[coercion] メカニズムの前に試行されます。
43+
3644
{docstring MonadLift}
3745

46+
:::comment
3847
{tech key:="lift"}[Lifting] between monads is reflexive and transitive:
3948
* Any monad can run its own actions.
4049
* Lifts from {lean}`m` to {lean}`m'` and from {lean}`m'` to {lean}`n` can be composed to yield a lift from {lean}`m` to {lean}`n`.
4150
The utility type class {name}`MonadLiftT` constructs lifts via the reflexive and transitive closure of {name}`MonadLift` instances.
4251
Users should not define new instances of {name}`MonadLiftT`, but it is useful as an instance implicit parameter to a polymorphic function that needs to run actions from multiple monads in some user-provided monad.
4352

53+
:::
54+
55+
モナド間の {tech}[持ち上げ] は反射的かつ推移的です:
56+
* どのモナドもそれ自身のアクションを実行することができます。
57+
* {lean}`m` から {lean}`m'` への持ち上げ、および {lean}`m'` から {lean}`n` への持ち上げは {lean}`m` {lean}`n` への持ち上げを生成するために合成することができます。
58+
ユーティリティの型クラス {name}`MonadLiftT` は {name}`MonadLift` インスタンスの反射的かつ推移的な閉包によって持ち上げを構築します。ユーザは {name}`MonadLiftT` のインスタンスを新たに定義すべきではありませんが、ユーザが提供するモナドの複数のモナドからアクションを実行する必要がある多相関数のインスタンス暗黙パラメータとして便利です。
59+
4460
{docstring MonadLiftT}
4561

46-
:::example "Monad Lifts in Function Signatures"
62+
:::comment
63+
::example "Monad Lifts in Function Signatures"
64+
:::
65+
::::example "関数シグネチャ内のモナドの持ち上げ"
66+
:::comment
4767
The function {name}`IO.withStdin` has the following signature:
68+
:::
69+
70+
関数 {name}`IO.withStdin` は以下のシグネチャを持ちます:
71+
4872
```signature
4973
IO.withStdin.{u} {m : TypeType u} {α : Type}
5074
[Monad m] [MonadFinally m] [MonadLiftT BaseIO m]
5175
(h : IO.FS.Stream) (x : m α) :
5276
m α
5377
```
78+
:::comment
5479
Because it doesn't require its parameter to precisely be in {name}`IO`, it can be used in many monads, and the body does not need to restrict itself to {name}`IO`.
5580
The instance implicit parameter {lean}`MonadLiftT BaseIO m` allows the reflexive transitive closure of {name}`MonadLift` to be used to assemble the lift.
5681
:::
5782

83+
パラメータでは {name}`IO` であることを正確に要求していないため、その他の多くのモナドでも使用することができ、本体は {name}`IO` に制限する必要はありません。インスタンス暗黙パラメータ {lean}`MonadLiftT BaseIO m` により、 {name}`MonadLift` の反射的推移的閉包を使用して持ち上げを組み立てることができます。
84+
85+
::::
86+
5887

88+
:::comment
5989
When a term of type {lean}`n β` is expected, but the provided term has type {lean}`m α`, and the two types are not definitionally equal, Lean attempts to insert lifts and coercions before reporting an error.
6090
There are the following possibilities:
91+
:::
92+
93+
{lean}`n β` 型の項が期待されているが、提供された項が {lean}`m α` 型であり、2つの型が定義上等しくない場合、Lean はエラーを報告する前に持ち上げと強制の挿入を試みます。これには以下の可能性があります:
94+
95+
:::comment
6196
1. If {lean}`m` and {lean}`n` can be unified to the same monad, then {lean}`α` and {lean}`β` are not the same.
6297
In this case, no monad lifts are necessary, but the value in the monad must be {tech key:="coercion"}[coerced].
6398
If the appropriate coercion is found, then a call to {name}`Lean.Internal.coeM` is inserted, which has the following signature:
99+
:::
100+
101+
1. {lean}`m` と {lean}`n` が同じモナドに単一化できる場合、 {lean}`α` と {lean}`β` は同じではありません。この場合、モナドの持ち上げは必要ありませんが、モナド内の値は {tech key:="coercion"}[coerce] されなければなりません。適切な強制が見つかった場合、 {name}`Lean.Internal.coeM` の呼び出しが挿入されます:
64102
```signature
65103
Lean.Internal.coeM.{u, v} {m : Type u → Type v} {α β : Type u}
66104
[(a : α) → CoeT α a β] [Monad m]
67105
(x : m α) :
68106
m β
69107
```
108+
:::comment
70109
2. If {lean}`α` and {lean}`β` can be unified, then the monads differ.
71110
In this case, a monad lift is necessary to transform an expression with type {lean}`m α` to {lean}`n α`.
72111
If {lean}`m` can be lifted to {lean}`n` (that is, there is an instance of {lean}`MonadLiftT m n`) then a call to {name}`liftM`, which is an alias for {name}`MonadLiftT.monadLift`, is inserted.
112+
:::
113+
2. {lean}`α` と {lean}`β` が単一化できる場合、モナドが異なります。この場合、 {lean}`m α` 型の式を {lean}`n α` 型に変換するにはモナドの持ち上げが必要です。 {lean}`m` が {lean}`n` に持ち上げられる場合(つまり {lean}`MonadLiftT m n` のインスタンスがある場合)、 {name}`MonadLiftT.monadLift` のエイリアスである {name}`liftM` の呼び出しが挿入されます。
73114
```signature
74115
liftM.{u, v, w}
75116
{m : Type u → Type v} {n : Type u → Type w}
76117
[self : MonadLiftT m n] {α : Type u} :
77118
m α → n α
78119
```
120+
:::comment
79121
3. If neither {lean}`m` and {lean}`n` nor {lean}`α` and {lean}`β` can be unified, but {lean}`m` can be lifted into {lean}`n` and {lean}`α` can be coerced to {lean}`β`, then a lift and a coercion can be combined.
80122
This is done by inserting a call to {name}`Lean.Internal.liftCoeM`:
123+
:::
124+
3. {lean}`m` と {lean}`n` 、 {lean}`α` と {lean}`β` のどちらも単一化できないが、 {lean}`m` を {lean}`n` に持ち上げることができ、 {lean}`α` を {lean}`β` に強制できる場合、持ち上げと強制を組み合わせることができます。これは {name}`Lean.Internal.liftCoeM` の呼び出しを挿入することで行われます:
81125
```signature
82126
Lean.Internal.liftCoeM.{u, v, w}
83127
{m : Type u → Type v} {n : Type u → Type w}
@@ -87,31 +131,57 @@ There are the following possibilities:
87131
n β
88132
```
89133

134+
:::comment
90135
As their names suggest, {name}`Lean.Internal.coeM` and {name}`Lean.Internal.liftCoeM` are implementation details, not part of the public API.
91136
In the resulting terms, occurrences of {name}`Lean.Internal.coeM`, {name}`Lean.Internal.liftCoeM`, and coercions are unfolded.
92137

93-
::::
138+
:::
139+
140+
その名前が示すように、 {name}`Lean.Internal.coeM` と {name}`Lean.Internal.liftCoeM` は実装の詳細であり、パブリックな API の一部ではありません。これらの結果の項では、 {name}`Lean.Internal.coeM` ・ {name}`Lean.Internal.liftCoeM` ・強制が展開されます。
94141

95-
::::keepEnv
96-
:::example "Lifting `IO` Monads"
142+
:::::
143+
144+
:::::keepEnv
145+
:::comment
146+
::example "Lifting `IO` Monads"
147+
:::
148+
::::example "`IO` モナドの持ち上げ"
149+
:::comment
97150
There is an instance of {lean}`MonadLift BaseIO IO`, so any `BaseIO` action can be run in `IO` as well:
151+
:::
152+
153+
{lean}`MonadLift BaseIO IO` のインスタンスがあるため、`IO` でも `BaseIO` アクションを実行することができます:
154+
98155
```lean
99156
def fromBaseIO (act : BaseIO α) : IO α := act
100157
```
158+
:::comment
101159
Behind the scenes, {name}`liftM` is inserted:
160+
:::
161+
162+
裏側では、 {name}`liftM` が挿入されます:
163+
102164
```lean (name := fromBase)
103165
#check fun {α} (act : BaseIO α) => (act : IO α)
104166
```
105167
```leanOutput fromBase
106168
fun {α} act => liftM act : {α : Type} → BaseIO α → EIO IO.Error α
107169
```
108-
:::
109170
::::
171+
:::::
110172

111173
:::::keepEnv
112-
::::example "Lifting Transformed Monads"
174+
:::comment
175+
::example "Lifting Transformed Monads"
176+
:::
177+
::::example "変換されたモナドの持ち上げ"
178+
:::comment
113179
There are also instances of {name}`MonadLift` for most of the standard library's {tech}[monad transformers], so base monad actions can be used in transformed monads without additional work.
114180
For example, state monad actions can be lifted across reader and exception transformers, allowing compatible monads to be intermixed freely:
181+
:::
182+
183+
また、標準ライブラリの {tech}[monad transformers] のほとんどに {name}`MonadLift` インスタンスがあるため、ベースとなるモナドアクションを追加作業無しに変換されたモナドで使用することができます。例えば、状態モナドアクションは、リーダと例外の変換を横断して持ち上げることができるため、互換性のあるモナドを自由に混在させることができます:
184+
115185
````lean (keep := false)
116186
def incrBy (n : Nat) : StateM Nat Unit := modify (+ n)
117187

@@ -120,7 +190,12 @@ def incrOrFail : ReaderT Nat (ExceptT String (StateM Nat)) Unit := do
120190
incrBy (← read)
121191
````
122192

193+
:::comment
123194
Disabling lifting causes the code to fail to work:
195+
:::
196+
197+
持ち上げを無効にすると、このコードは機能しなくなります:
198+
124199
````lean (name := noLift)
125200
set_option autoLift false
126201

@@ -135,33 +210,63 @@ def incrOrFail : ReaderT Nat (ExceptT String (StateM Nat)) Unit := do
135210
:::::
136211

137212

213+
:::comment
138214
Automatic lifting can be disabled by setting {option}`autoLift` to {lean}`false`.
139215

216+
:::
217+
218+
{option}`autoLift` を {lean}`false` に設定することで自動持ち上げを無効にすることができます。
219+
140220
{optionDocs autoLift}
141221

222+
:::comment
142223
# Reversing Lifts
224+
:::
225+
226+
# 持ち上げの引き下げ(Reversing Lifts)
227+
143228

144229
```lean (show := false)
145230
variable {m n : Type u → Type v} {α ε : Type u}
146231
```
147232

233+
:::comment
148234
Monad lifting is not always sufficient to combine monads.
149235
Many operations provided by monads are higher order, taking an action _in the same monad_ as a parameter.
150236
Even if these operations are lifted to some more powerful monad, their arguments are still restricted to the original monad.
151237

238+
:::
239+
240+
モナドの結合において、モナドの持ち上げだけでは必ずしも十分ではありません。モナドによって提供される多くの操作は高階であり、 _同じモナド内の_ アクションをパラメータとして取ります。これらの操作がより強力なモナドに持ち上げられたとしても、その引数はもとのモナドに制限されます。
241+
242+
:::comment
152243
There are two type classes that support this kind of “reverse lifting”: {name}`MonadFunctor` and {name}`MonadControl`.
153244
An instance of {lean}`MonadFunctor m n` explains how to interpret a fully-polymorphic function in {lean}`m` into {lean}`n`.
154245
This polymorphic function must work for _all_ types {lean}`α`: it has type {lean}`{α : Type u} → m α → m α`.
155246
Such a function can be thought of as one that may have effects, but can't do so based on specific values that are provided.
156247
An instance of {lean}`MonadControl m n` explains how to interpret an arbitrary action from {lean}`m` into {lean}`n`, while at the same time providing a “reverse interpreter” that allows the {lean}`m` action to run {lean}`n` actions.
157248

249+
:::
250+
251+
このような「持ち上げの引き下げ」をサポートする型クラスが2つあります: {name}`MonadFunctor` と {name}`MonadControl` です。 {lean}`MonadFunctor m n` のインスタンスは、 {lean}`m` の完全な多相関数を {lean}`n` として解釈する方法を説明します。この多相関数は {lean}`α` として _すべて_ の型に対して機能しなければなりません:これは {lean}`{α : Type u} → m α → m α` という型を持ちます。このような関数は、作用を持つかもしれないが、与えられた特定の値に基づいて作用を持つことはできない関数として考えることができます。 {lean}`MonadControl m n` のインスタンスは {lean}`m` から {lean}`n` への任意のアクションを解釈する方法を説明すると同時に、 {lean}`m` アクションが {lean}`n` アクションを実行できるようにする「逆インタプリタ」を提供します。
252+
253+
:::comment
158254
## Monad Functors
255+
:::
256+
257+
## モナド関手(Monad Functors)
258+
159259

160260
{docstring MonadFunctor}
161261

162262
{docstring MonadFunctorT}
163263

264+
:::comment
164265
## Reversible Lifting with `MonadControl`
266+
:::
267+
268+
## `MonadControl` による持ち上げの引き下げ(Reversible Lifting with `MonadControl`)
269+
165270

166271
{docstring MonadControl}
167272

@@ -172,17 +277,30 @@ An instance of {lean}`MonadControl m n` explains how to interpret an arbitrary a
172277
{docstring controlAt}
173278

174279

175-
::::keepEnv
176-
:::example "Exceptions and Lifting"
280+
:::::keepEnv
281+
:::comment
282+
::example "Exceptions and Lifting"
283+
:::
284+
::::example "例外と持ち上げ"
285+
:::comment
177286
One example is {name}`Except.tryCatch`:
287+
:::
288+
289+
一例として {name}`Except.tryCatch` を挙げます:
290+
178291
```signature
179292
Except.tryCatch.{u, v} {ε : Type u} {α : Type v}
180293
(ma : Except ε α) (handle : ε → Except ε α) :
181294
Except ε α
182295
```
296+
:::comment
183297
Both of its parameters are in {lean}`Except ε`.
184298
{name}`MonadLift` can lift the entire application of the handler.
185299
The function {lean}`getBytes`, which extracts the single bytes from an array of {lean}`Nat`s using state and exceptions, is written without {keywordOf Lean.Parser.Term.do}`do`-notation or automatic lifting in order to make its structure explicit.
300+
:::
301+
302+
パラメータはどちらも {lean}`Except ε` です。 {name}`MonadLift` はハンドラの適用全体を持ち上げることができます。 {lean}`getBytes` 関数は、状態と例外を使用して {lean}`Nat` の配列からシングルバイトを抽出しますが、その構造を明示的にするために {keywordOf Lean.Parser.Term.do}`do` 記法や自動持ち上げなしで記述されています。
303+
186304
```lean
187305
set_option autoLift false
188306

@@ -205,12 +323,22 @@ def getBytes (input : Array Nat) : StateT (Array UInt8) (Except String) Unit :=
205323
```leanOutput getBytesEval1
206324
Except.ok #[1, 58, 255, 2]
207325
```
326+
:::comment
208327
{name}`getBytes` uses an `Option` returned from the lifted action to signal the desired state updates.
209328
This quickly becomes unwieldy if there are more possible ways to react to the inner action, such as saving handled exceptions.
210329
Ideally, state updates would be performed within the {name}`tryCatch` call directly.
211330

212331

332+
:::
333+
334+
{name}`getBytes` は、持ち上げられたアクションから返された `Option` を使用して必要な状態の更新を通知します。処理された例外を保存するなど、内側のアクションに反応する可能性がある場合、これはすぐに扱いにくくなります。理想的には、状態の更新は {name}`tryCatch` 呼び出しの中で直接実行されてほしいです。
335+
336+
:::comment
213337
Attempting to save bytes and handled exceptions does not work, however, because the arguments to {name}`Except.tryCatch` have type {lean}`Except String Unit`:
338+
:::
339+
340+
しかし、 {name}`Except.tryCatch` の引数の型は {lean}`Except String Unit` を持つため、バイトと処理された例外を保存しようとしてもうまくいきません:
341+
214342
```lean (error := true) (name := getBytesErr) (keep := false)
215343
def getBytes' (input : Array Nat) : StateT (Array String) (StateT (Array UInt8) (Except String)) Unit := do
216344
input.forM fun i =>
@@ -227,10 +355,15 @@ failed to synthesize
227355
Additional diagnostic information may be available using the `set_option diagnostics true` command.
228356
```
229357

358+
:::comment
230359
Because {name}`StateT` has a {name}`MonadControl` instance, {name}`control` can be used instead of {name}`liftM`.
231360
It provides the inner action with an interpreter for the outer monad.
232361
In the case of {name}`StateT`, this interpreter expects that the inner monad returns a tuple that includes the updated state, and takes care of providing the initial state and extracting the updated state from the tuple.
233362

363+
:::
364+
365+
{name}`StateT` は {name}`MonadControl` インスタンスを持つため、 {name}`liftM` の代わりに {name}`control` を使用することができます。これは内側のアクションに外側のモナドのインタプリタを提供します。 {name}`StateT` の場合、このインタプリタは内側のモナドが更新された状態を含むタプルを返すことを期待し、初期状態の提供とタプルからの更新された状態の抽出を行います。
366+
234367
```lean
235368
def getBytes' (input : Array Nat) : StateT (Array String) (StateT (Array UInt8) (Except String)) Unit := do
236369
input.forM fun i =>
@@ -251,5 +384,5 @@ def getBytes' (input : Array Nat) : StateT (Array String) (StateT (Array UInt8)
251384
```leanOutput getBytesEval2
252385
Except.ok (#["Out of range: 300", "Out of range: 1000000"], #[1, 58, 255, 2])
253386
```
254-
:::
255387
::::
388+
:::::

Manual/Terms.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ If any fresh variables were created for missing explicit positional arguments, t
596596
Finally, instance synthesis is invoked and as many metavariables as possible are solved:
597597
1. A type is inferred for the entire function application. This may cause some metavariables to be solved due to unification that occurs during type inference.
598598
2. The instance metavariables are synthesized. {tech}[デフォルトインスタンス]Default instances are only used if the inferred type is a metavariable that is the output parameter of one of the instances.
599-
3. If there is an expected type, it is unified with the inferred type; however, errors resulting from this unification are discarded. If the expected and inferred types can be equal, unification can solve leftover implicit argument metavariables. If they can't be equal, an error is not thrown because a surrounding elaborator may be able to insert {tech}[coercions] or {tech key:="lift"}[monad lifts].
599+
3. If there is an expected type, it is unified with the inferred type; however, errors resulting from this unification are discarded. If the expected and inferred types can be equal, unification can solve leftover implicit argument metavariables. If they can't be equal, an error is not thrown because a surrounding elaborator may be able to insert {tech}[coercions] or lift {tech key:="持ち上げ"}[monad lifts].
600600

601601

602602
::::keepEnv

0 commit comments

Comments
 (0)