You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jan 5, 2025. It is now read-only.
{tech key:="lift"}[Lifting] between monads is reflexive and transitive:
39
48
* Any monad can run its own actions.
40
49
* 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`.
41
50
The utility type class {name}`MonadLiftT`constructsliftsviathereflexiveandtransitiveclosureof {name}`MonadLift` instances.
42
51
Users should not define new instances of {name}`MonadLiftT`, but it is useful as an instanceimplicitparametertoapolymorphicfunctionthatneedstorunactionsfrommultiplemonadsinsomeuser-providedmonad.
The function {name}`IO.withStdin` has the following signature:
68
+
:::
69
+
70
+
関数 {name}`IO.withStdin` は以下のシグネチャを持ちます:
71
+
48
72
```signature
49
73
IO.withStdin.{u} {m : Type → Type u} {α : Type}
50
74
[Monad m] [MonadFinally m] [MonadLiftT BaseIO m]
51
75
(h : IO.FS.Stream) (x : m α) :
52
76
m α
53
77
```
78
+
:::comment
54
79
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`.
55
80
The instanceimplicitparameter {lean}`MonadLiftT BaseIO m` allows the reflexive transitive closure of {name}`MonadLift` to be used to assemble the lift.
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.
Lean.Internal.coeM.{u, v} {m : Type u → Type v} {α β : Type u}
66
104
[(a : α) → CoeT α a β] [Monad m]
67
105
(x : m α) :
68
106
m β
69
107
```
108
+
:::comment
70
109
2. If {lean}`α` and {lean}`β` can be unified, then the monads differ.
71
110
In this case, a monad lift is necessary to transform an expression with type {lean}`m α` to {lean}`n α`.
72
111
If {lean}`m` can be lifted to {lean}`n` (that is, there is an instanceof {lean}`MonadLiftT m n`) then a call to {name}`liftM`, which is an alias for {name}`MonadLiftT.monadLift`, is inserted.
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.
80
122
This is done by inserting a call to {name}`Lean.Internal.liftCoeM`:
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.
114
180
For example, state monad actions can be lifted across reader and exception transformers, allowing compatible monads to be intermixed freely:
There are two type classes that support this kind of “reverse lifting”: {name}`MonadFunctor` and {name}`MonadControl`.
153
244
An instanceof {lean}`MonadFunctor m n` explains how to interpret a fully-polymorphic function in {lean}`m` into {lean}`n`.
154
245
This polymorphic function must work for _all_ types {lean}`α`: it has type {lean}`{α : Type u} → m α → m α`.
155
246
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.
156
247
An instanceof {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.
157
248
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` アクションを実行できるようにする「逆インタプリタ」を提供します。
@@ -172,17 +277,30 @@ An instance of {lean}`MonadControl m n` explains how to interpret an arbitrary a
172
277
{docstring controlAt}
173
278
174
279
175
-
::::keepEnv
176
-
:::example"Exceptions and Lifting"
280
+
:::::keepEnv
281
+
:::comment
282
+
::example"Exceptions and Lifting"
283
+
:::
284
+
::::example"例外と持ち上げ"
285
+
:::comment
177
286
One example is {name}`Except.tryCatch`:
287
+
:::
288
+
289
+
一例として {name}`Except.tryCatch` を挙げます:
290
+
178
291
```signature
179
292
Except.tryCatch.{u, v} {ε : Type u} {α : Type v}
180
293
(ma : Except ε α) (handle : ε → Except ε α) :
181
294
Except ε α
182
295
```
296
+
:::comment
183
297
Both of its parameters are in {lean}`Except ε`.
184
298
{name}`MonadLift` can lift the entire application of the handler.
185
299
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 structureexplicit.
Attempting to save bytes and handled exceptions does not work, however, because the arguments to {name}`Except.tryCatch`have type {lean}`Except String Unit`:
defgetBytes' (input : Array Nat) : StateT (Array String) (StateT (Array UInt8) (Except String)) Unit := do
216
344
input.forM fun i =>
@@ -227,10 +355,15 @@ failed to synthesize
227
355
Additional diagnostic information may be available using the `set_option diagnostics true` command.
228
356
```
229
357
358
+
:::comment
230
359
Because {name}`StateT` has a {name}`MonadControl`instance, {name}`control` can be used instead of {name}`liftM`.
231
360
It provides the inner action with an interpreter for the outer monad.
232
361
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.
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.
598
598
2. The instancemetavariablesaresynthesized. {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].
0 commit comments