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: functional-programming-lean/src/hello-world/conveniences.md
+106Lines changed: 106 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -1,75 +1,151 @@
1
+
<!--
1
2
# Additional Conveniences
3
+
-->
2
4
5
+
# その他の便利機能
3
6
7
+
<!--
4
8
## Nested Actions
9
+
-->
5
10
11
+
## ネストされたアクション
12
+
13
+
<!--
6
14
Many of the functions in `feline` exhibit a repetitive pattern in which an `IO` action's result is given a name, and then used immediately and only once.
When Lean is compiling a `do` block, expressions that consist of a left arrow immediately under parentheses are lifted to the nearest enclosing `do`, and their results are bound to a unique name.
21
44
This unique name replaces the origin of the expression.
22
45
This means that `dump` can also be written as follows:
`fileStream` can be simplified using the same technique:
62
+
-->
63
+
64
+
`fileStream` も同じテクニックを使って簡略にできます:
65
+
30
66
```lean
31
67
{{#example_decl Examples/Cat.lean fileStream}}
32
68
```
69
+
<!--
33
70
In this case, the local name of `handle` could also have been eliminated using nested actions, but the resulting expression would have been long and complicated.
34
71
Even though it's often good style to use nested actions, it can still sometimes be helpful to name intermediate results.
It is important to remember, however, that nested actions are only a shorter notation for `IO` actions that occur in a surrounding `do` block.
37
78
The side effects that are involved in executing them still occur in the same order, and execution of side effects is not interspersed with the evaluation of expressions.
38
79
For an example of where this might be confusing, consider the following helper definitions that return data after announcing to the world that they have been executed:
This is due to the rule that nested actions are lifted to the _closest enclosing_ `do` block.
61
121
The branches of the `if` were not implicitly wrapped in `do` blocks because the `if` is not itself a statement in the `do` block—the statement is the `let` that defines `a`.
62
122
Indeed, they could not be wrapped this way, because the type of the conditional expression is `Nat`, not `IO Nat`.
In Lean, `do` expressions are whitespace-sensitive.
67
135
Each `IO` action or local binding in the `do` is expected to start on its own line, and they should all have the same indentation.
68
136
Almost all uses of `do` should be written this way.
69
137
In some rare contexts, however, manual control over whitespace and indentation may be necessary, or it may be convenient to have multiple small actions on a single line.
70
138
In these cases, newlines can be replaced with a semicolon and indentation can be replaced with curly braces.
For instance, all of the following programs are equivalent:
145
+
-->
146
+
147
+
例えば、以下のプログラムはすべての等価です:
148
+
73
149
```lean
74
150
{{#example_decl Examples/Cat.lean helloOne}}
75
151
@@ -78,28 +154,58 @@ For instance, all of the following programs are equivalent:
78
154
{{#example_decl Examples/Cat.lean helloThree}}
79
155
```
80
156
157
+
<!--
81
158
Idiomatic Lean code uses curly braces with `do` very rarely.
159
+
-->
160
+
161
+
慣用的なLeanの `do` のコードでは波括弧を使うことはほとんどありません。
82
162
163
+
<!--
83
164
## Running `IO` Actions With `#eval`
165
+
-->
84
166
167
+
## `#eval` による `IO` アクションの実行
168
+
169
+
<!--
85
170
Lean's `#eval` command can be used to execute `IO` actions, rather than just evaluating them.
86
171
Normally, adding a `#eval` command to a Lean file causes Lean to evaluate the provided expression, convert the resulting value to a string, and provide that string as a tooltip and in the info window.
87
172
Rather than failing because `IO` actions can't be converted to strings, `#eval` executes them, carrying out their side effects.
88
173
If the result of execution is the `Unit` value `()`, then no result string is shown, but if it is a type that can be converted to a string, then Lean displays the resulting value.
Quickly testing `IO` actions with `#eval` can be much more convenient that compiling and running whole programs.
102
205
However, there are some limitations.
103
206
For instance, reading from standard input simply returns empty input.
104
207
Additionally, the `IO` action is re-executed whenever Lean needs to update the diagnostic information that it provides to users, and this can happen at unpredictable times.
105
208
An action that reads and writes files, for instance, may do so at inconvenient times.
0 commit comments