|
| 1 | +<!-- |
1 | 2 | # Summary
|
| 3 | +--> |
2 | 4 |
|
| 5 | +# まとめ |
| 6 | + |
| 7 | +<!-- |
3 | 8 | ## Evaluation vs Execution
|
| 9 | +--> |
| 10 | + |
| 11 | +## 評価 vs 実行 |
4 | 12 |
|
| 13 | +<!-- |
5 | 14 | Side effects are aspects of program execution that go beyond the evaluation of mathematical expressions, such as reading files, throwing exceptions, or triggering industrial machinery.
|
6 | 15 | While most languages allow side effects to occur during evaluation, Lean does not.
|
7 | 16 | Instead, Lean has a type called `IO` that represents _descriptions_ of programs that use side effects.
|
8 | 17 | These descriptions are then executed by the language's run-time system, which invokes the Lean expression evaluator to carry out specific computations.
|
9 | 18 | Values of type `IO α` are called _`IO` actions_.
|
10 | 19 | The simplest is `pure`, which returns its argument and has no actual side effects.
|
| 20 | +--> |
| 21 | + |
| 22 | +副作用とはファイルの読み込み・例外の発生・産業機械の起動など、数式の評価を超えたプログラムの実行についての側面です。ほとんどの言語では評価中に副作用が発生することを許可していますが、Leanでは許可していません。その代わりに、Leanには `IO` という型があり、副作用を使用するプログラムの **記述** を表現します。これらの記述は言語のランタイムシステムによって実行されます。このランタイムシステムはLeanの式の評価器を呼び出して特定の計算を実行します。`IO α` 型の値は **`IO` アクション** と呼ばれます。最も単純なものは `pure` で、これは引数をそのまま返し、実際の副作用を持ちません。 |
11 | 23 |
|
| 24 | +<!-- |
12 | 25 | `IO` actions can also be understood as functions that take the whole world as an argument and return a new world in which the side effect has occurred.
|
13 | 26 | Behind the scenes, the `IO` library ensures that the world is never duplicated, created, or destroyed.
|
14 | 27 | While this model of side effects cannot actually be implemented, as the whole universe is too big to fit in memory, the real world can be represented by a token that is passed around through the program.
|
| 28 | +--> |
15 | 29 |
|
| 30 | +`IO` アクションは世界全体を引数として受け取り、副作用が発生した新しい世界を返す関数として理解することもできます。その裏では、`IO` ライブラリは世界が決して複製されたり、新しく作られたり、破壊されたりしないことを保証しています。全宇宙は大きすぎてメモリに収まらないため、この副作用のモデルを実際に実装することはできませんが、現実の世界をプログラム中で受け渡されるトークンとして表現することができます。 |
| 31 | + |
| 32 | +<!-- |
16 | 33 | An `IO` action `main` is executed when the program starts.
|
17 | 34 | `main` can have one of three types:
|
| 35 | +--> |
| 36 | + |
| 37 | +`IO` アクション `main` はプログラムが開始された時に実行されます。`main` は以下3つのうちどれかの型を持ちます: |
| 38 | + |
| 39 | + <!-- |
18 | 40 | * `main : IO Unit` is used for simple programs that cannot read their command-line arguments and always return exit code `0`,
|
19 | 41 | * `main : IO UInt32` is used for programs without arguments that may signal success or failure, and
|
20 | 42 | * `main : List String → IO UInt32` is used for programs that take command-line arguments and signal success or failure.
|
| 43 | +--> |
21 | 44 |
|
| 45 | + * `main : IO Unit` はコマンドライン引数を読むことができず、常に終了コード `0` を返す単純なプログラムに使われます。 |
| 46 | + * `main : IO UInt32` は成功または失敗を知らせる引数のないプログラムに使われます。 |
| 47 | + * `main : List String → IO UInt32` はコマンドライン引数を取り、成功または失敗を知らせるプログラムに使われます。 |
22 | 48 |
|
| 49 | +<!-- |
23 | 50 | ## `do` Notation
|
| 51 | +--> |
| 52 | + |
| 53 | +## `do` 記法 |
24 | 54 |
|
| 55 | +<!-- |
25 | 56 | The Lean standard library provides a number of basic `IO` actions that represent effects such as reading from and writing to files and interacting with standard input and standard output.
|
26 | 57 | These base `IO` actions are composed into larger `IO` actions using `do` notation, which is a built-in domain-specific language for writing descriptions of programs with side effects.
|
27 | 58 | A `do` expression contains a sequence of _statements_, which may be:
|
| 59 | +--> |
| 60 | + |
| 61 | +Leanの標準ライブラリは、ファイルからの読み込みや書き込み、標準入出力とのやり取りなどの作用を表す基本的な `IO` アクションを数多く提供しています。これらの基本的な `IO` アクションは、副作用を持つプログラムを書くための組み込みドメイン固有言語である `do` 記法を使ってより大きな `IO` アクションにまとめることができます。`do` 記法は以下のような一連の **文** を含んでいます: |
| 62 | + |
| 63 | + <!-- |
28 | 64 | * expressions that represent `IO` actions,
|
29 | 65 | * ordinary local definitions with `let` and `:=`, where the defined name refers to the value of the provided expression, or
|
30 | 66 | * local definitions with `let` and `←`, where the defined name refers to the result of executing the value of the provided expression.
|
| 67 | +--> |
31 | 68 |
|
| 69 | + * `IO` アクションを表す式 |
| 70 | + * `let` と `:=` を使った通常のローカル定義、定義された名前は渡された式の値を指します。 |
| 71 | + * `let` と `←` を使ったローカル定義、定義された名前は渡された式の値を実行した結果を指します。 |
| 72 | + |
| 73 | +<!-- |
32 | 74 | `IO` actions that are written with `do` are executed one statement at a time.
|
| 75 | +--> |
| 76 | + |
| 77 | +`do` で記述された `IO` アクションは一度に1文ずつ実行されます。 |
33 | 78 |
|
| 79 | +<!-- |
34 | 80 | Furthermore, `if` and `match` expressions that occur immediately under a `do` are implicitly considered to have their own `do` in each branch.
|
35 | 81 | Inside of a `do` expression, _nested actions_ are expressions with a left arrow immediately under parentheses.
|
36 | 82 | The Lean compiler implicitly lifts them to the nearest enclosing `do`, which may be implicitly part of a branch of a `match` or `if` expression, and gives them a unique name.
|
37 | 83 | This unique name then replaces the origin site of the nested action.
|
| 84 | +--> |
38 | 85 |
|
| 86 | +さらに、`do` の直下にある `if` 式と `match` 式は、暗黙的にそれぞれの分岐に `do` があると見なされます。`do` 式の内部では、**ネストされたアクション** は括弧の直下にある左矢印で記述された式です。Leanコンパイラは、暗黙的にそれらを最も近い `do` (`match` や `if` の分岐に暗黙的にあるものも含みます)に結び付け、一意な名前を付けます。この一意な名前によってネストされたアクションのもともとの位置を置き換えます。 |
39 | 87 |
|
| 88 | +<!-- |
40 | 89 | ## Compiling and Running Programs
|
| 90 | +--> |
41 | 91 |
|
| 92 | +## プログラムのコンパイルと実行 |
| 93 | + |
| 94 | +<!-- |
42 | 95 | A Lean program that consists of a single file with a `main` definition can be run using `lean --run FILE`.
|
43 | 96 | While this can be a nice way to get started with a simple program, most programs will eventually graduate to a multiple-file project that should be compiled before running.
|
| 97 | +--> |
| 98 | + |
| 99 | +`lean --run FILE` というコマンドで `main` 定義を持つ単一ファイルで構成されるLeanプログラムを実行することができます。これは簡単なプログラムの実行には良い方法ですが、ほとんどのプログラムは最終的に複数のファイルを持つプロジェクトへと成長するので、実行する前にコンパイルする必要があります。 |
44 | 100 |
|
| 101 | +<!-- |
45 | 102 | Lean projects are organized into _packages_, which are collections of libraries and executables together with information about dependencies and a build configuration.
|
46 | 103 | Packages are described using Lake, a Lean build tool.
|
47 | 104 | Use `lake new` to create a Lake package in a new directory, or `lake init` to create one in the current directory.
|
48 | 105 | Lake package configuration is another domain-specific language.
|
49 | 106 | Use `lake build` to build a project.
|
| 107 | +--> |
50 | 108 |
|
| 109 | +Leanのプロジェクトは、依存関係やビルド構成に関する情報とともにライブラリや実行可能ファイルのコレクションである **パッケージ** へと編成されます。パッケージはLeanのビルドツールであるLakeを使って記述します。新しいディレクトリにLakeパッケージを作成するには `lake new` を、現在のディレクトリに作成するには `lake init` を使用します。Lakeのパッケージ構成もドメイン固有言語です。プロジェクトをビルドするには `lake build` を使用します。 |
| 110 | + |
| 111 | +<!-- |
51 | 112 | ## Partiality
|
| 113 | +--> |
| 114 | + |
| 115 | +## 部分性 |
52 | 116 |
|
| 117 | +<!-- |
53 | 118 | One consequence of following the mathematical model of expression evaluation is that every expression must have a value.
|
54 | 119 | This rules out both incomplete pattern matches that fail to cover all constructors of a datatype and programs that can fall into an infinite loop.
|
55 | 120 | Lean ensures that all `match` expressions cover all cases, and that all recursive functions are either structurally recursive or have an explicit proof of termination.
|
| 121 | +--> |
56 | 122 |
|
| 123 | +式評価の数学的モデルに従うことから導かれる結論の1つとして、すべての式が値を持たなければならないということがあります。これにより、データ型のすべてのコンストラクタをカバーできない不完全なパターンマッチと無限ループに陥る可能性のあるプログラムの両方が除外されます。Leanはすべての `match` 式がすべてのケースをカバーすること、すべての再帰関数は構造的に再帰的であるか停止の明示的な証明があることを保証します。 |
| 124 | + |
| 125 | +<!-- |
57 | 126 | However, some real programs require the possibility of looping infinitely, because they handle potentially-infinite data, such as POSIX streams.
|
58 | 127 | Lean provides an escape hatch: functions whose definition is marked `partial` are not required to terminate.
|
59 | 128 | This comes at a cost.
|
60 | 129 | Because types are a first-class part of the Lean language, functions can return types.
|
61 | 130 | Partial functions, however, are not evaluated during type checking, because an infinite loop in a function could cause the type checker to enter an infinite loop.
|
62 | 131 | Furthermore, mathematical proofs are unable to inspect the definitions of partial functions, which means that programs that use them are much less amenable to formal proof.
|
| 132 | +--> |
| 133 | + |
| 134 | +しかし、実際のプログラムの中にはPOSIXのストリームのように無限ループする可能性のあるデータを扱うために、無限ループの可能性を必要とするものがあります。これに対してLeanは逃げ道を用意しています:定義が `partial` とマークされた関数は停止する必要がありません。これには代償が伴います。型はLean言語の第一級であるため、関数は型を返すことができます。しかし、関数が無限ループに入ると型チェッカが無限ループも入る可能性があるため、部分関数は型チェック中に評価されません。さらに、数学的な証明では部分関数の定義を検査することができないため、部分関数を使用するプログラムは形式的な証明にあまり適していません。 |
0 commit comments