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.
Copy file name to clipboardExpand all lines: Manual/Monads/Zoo/Combined.lean
+39-1Lines changed: 39 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -19,18 +19,26 @@ set_option pp.rawOnError true
19
19
20
20
set_option linter.unusedVariables false
21
21
22
+
/-
22
23
#doc (Manual) "Combined Error and State Monads" =>
24
+
-/
25
+
#doc (Manual) "エラーと状態モナドの結合(Combined Error and State Monads)" =>
23
26
24
27
```lean (show := false)
25
28
variable (ε : Type u) (σ σ' : Type u) (α : Type u)
26
29
```
27
30
31
+
:::comment
28
32
The {name}`EStateM` monad has both exceptions and mutable state.
29
33
{lean}`EStateM ε σ α` is logically equivalent to {lean}`ExceptT ε (StateM σ) α`.
30
34
While {lean}`ExceptT ε (StateM σ)` evaluates to the type {lean}`σ → Except ε α × σ`, the type {lean}`EStateM ε σ α` evaluates to {lean}`σ → EStateM.Result ε σ α`.
31
35
{name}`EStateM.Result` is an inductivetypethat'sverysimilarto {name}`Except`, except both constructors have an additional field for the state.
32
36
In compiled code, this representation removes one level of indirection from each monadic bind.
@@ -53,28 +61,58 @@ In compiled code, this representation removes one level of indirection from each
53
61
54
62
{docstring EStateM.fromStateM}
55
63
64
+
:::comment
56
65
# State Rollback
66
+
:::
57
67
68
+
# 状態のロールバック(State Rollback)
69
+
70
+
71
+
:::comment
58
72
Composing {name}`StateT` and {name}`ExceptT`in different orders causes exceptions to interact differently with state.
59
73
In one ordering, state changes are rolled back when exceptions are caught; in the other, they persist.
60
74
The latter option matches the semantics of most imperative programming languages, but the former is very useful for search-based problems.
61
75
Often, some but not all state should be rolled back; this can be achieved by “sandwiching” {name}`ExceptT` between two separate uses of {name}`StateT`.
62
76
63
-
To avoid yet another layer of indirection via the use of {lean}`StateT σ (EStateM ε σ') α`, {name}`EStateM` offers the {name}`EStateM.Backtrackable` {tech}[型クラス]type class.
To avoid yet another layer of indirection via the use of {lean}`StateT σ (EStateM ε σ') α`, {name}`EStateM` offers the {name}`EStateM.Backtrackable` {tech}[type class].
64
83
This classspecifiessomepartofthestatethatcanbesavedandrestored.
65
84
{name}`EStateM`then arranges for the saving and restoring to take place around error handling.
@@ -52,7 +65,12 @@ The inductive type {name}`Except` captures this pattern, and is itself a monad.
52
65
{docstring Except.toBool}
53
66
54
67
68
+
:::comment
55
69
# Type Class
70
+
:::
71
+
72
+
# 型クラス(Type Class)
73
+
56
74
57
75
{docstring MonadExcept}
58
76
@@ -68,11 +86,21 @@ The inductive type {name}`Except` captures this pattern, and is itself a monad.
68
86
69
87
{docstring tryCatchThe}
70
88
89
+
:::comment
71
90
# “Finally” Computations
91
+
:::
92
+
93
+
# 「finally」の計算(“Finally” Computations)
94
+
72
95
73
96
{docstring MonadFinally}
74
97
98
+
:::comment
75
99
# Transformer
100
+
:::
101
+
102
+
# 変換子(Transformer)
103
+
76
104
77
105
{docstring ExceptT}
78
106
@@ -95,7 +123,12 @@ The inductive type {name}`Except` captures this pattern, and is itself a monad.
95
123
{docstring ExceptT.adapt}
96
124
97
125
126
+
:::comment
98
127
# Exception Monads in Continuation Passing Style
128
+
:::
129
+
130
+
# 継続渡しスタイルの例外モナド(Exception Monads in Continuation Passing Style)
131
+
99
132
100
133
```lean (show := false)
101
134
universe u
@@ -104,12 +137,17 @@ variable (ε : Type u)
104
137
variable {m : Type u → Type v}
105
138
```
106
139
140
+
:::comment
107
141
Continuation-passing-style exception monads represent potentially-failing computations as functions that take success and failure continuations, both of which return the same type, returning that type.
108
142
They must work for _any_ return type.
109
143
An example of such a type is {lean}`(β : Type u) → (α → β) → (ε → β) → β`.
110
144
{lean}`ExceptCpsT` is a transformer that can be applied to any monad, so {lean}`ExceptCpsT ε m α` is actually defined as {lean}`(β : Type u) → (α → m β) → (ε → m β) → m β`.
111
145
Exception monads in continuation passing style have different performance characteristics than {name}`Except`-based state monads; for some applications, it may be worth benchmarking them.
112
146
147
+
:::
148
+
149
+
継続渡しスタイルの例外モナドは、失敗する可能性のある計算を、成功と失敗を取る関数として表します。この継続はどちらも同じ型を返し、この関数はその型を返します。例外モナドは _どのような_ 戻り値の型でも動作しなければなりません。このような型の例は {lean}`(β : Type u) → (α → β) → (ε → β) → β` です。 {lean}`ExceptCpsT` は任意のモナドに適用できる変換子であるため、 {lean}`ExceptCpsT ε m α` は実際には {lean}`(β : Type u) → (α → m β) → (ε → m β) → m β` と定義されています。継続渡しスタイルの例外モナドは、 {name}`Except` ベースの状態モナドとは異なるパフォーマンス特性を持ちます;アプリケーションによってはベンチマークを取る価値があるかもしれません。
150
+
113
151
```lean (show := false)
114
152
/-- info: (β : Type u) → (α → m β) → (ε → m β) → m β -/
Copy file name to clipboardExpand all lines: Manual/Monads/Zoo/Option.lean
+8Lines changed: 8 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -19,16 +19,24 @@ set_option pp.rawOnError true
19
19
20
20
set_option linter.unusedVariables false
21
21
22
+
/-
22
23
#doc (Manual) "Option" =>
24
+
-/
25
+
#doc (Manual) "オプション(Option)" =>
23
26
%%%
24
27
tag := "option-monad"
25
28
%%%
26
29
30
+
:::comment
27
31
Ordinarily, {lean}`Option` is thought of as data, similarly to a nullable type.
28
32
It can also be considered as a monad, and thus a way of performing computations.
29
33
The {lean}`Option` monad and its transformer {lean}`OptionT` can be understood as describing computations that may terminate early, discarding the results.
30
34
Callers can check for early termination and invoke a fallback if desired using {name}`OrElse.orElse` or by treating it as a {lean}`MonadExcept Unit`.
0 commit comments