11/-
2+ --#--
23# Extra: Options
4+ --#--
5+ # 付録:オプション
6+ --#--
37Options are a way to communicate some special configuration to both
48your meta programs and the Lean compiler itself. Basically it's just
59a [ `KVMap` ] (https://github.com/leanprover/lean4/blob/master/src/Lean/Data/KVMap.lean)
610which is a simple map from `Name` to a `Lean.DataValue`. Right now there
711are 6 kinds of data values:
12+ --#--
13+ オプションはメタプログラムと Lean のコンパイラの両方に特別な設定を伝える機能です。基本的にこれは [ `KVMap` ] (https://github.com/leanprover/lean4/blob/master/src/Lean/Data/KVMap.lean) であり、`Name` から `Lean.DataValue` への単純なマップです。現在、6種類のデータ値を有します:
14+
815- `String`
916- `Bool`
1017- `Name`
1118- `Nat`
1219- `Int`
1320- `Syntax`
1421
22+ --#--
1523Setting an option to tell the Lean compiler to do something different
1624with your program is quite simple with the `set_option` command:
25+ --#--
26+ `set_option` コマンドを使うことで、とても簡単に Lean コンパイラにプログラムに対して何か違うことを行うように指示するオプションを設定することができます:
1727-/
1828
1929import Lean
@@ -28,7 +38,10 @@ set_option pp.explicit true -- No custom syntax in pretty printing
2838set_option pp.explicit false
2939
3040/-!
41+ --#--
3142You can furthermore limit an option value to just the next command or term:
43+ --#--
44+ さらに、オプションの値をすぐ次のコマンドや項だけに限定することもできます:
3245-/
3346
3447set_option pp.explicit true in
@@ -39,24 +52,42 @@ set_option pp.explicit true in
3952#check set_option trace.Meta.synthInstance true in 1 + 1 -- the trace of the type class synthesis for `OfNat` and `HAdd`
4053
4154/-!
55+ --#--
4256If you want to know which options are available out of the Box right now
4357you can simply write out the `set_option` command and move your cursor
4458to where the name is written, it should give you a list of them as auto
4559completion suggestions. The most useful group of options when you are
4660debugging some meta thing is the `trace.` one.
4761
62+ --#--
63+ もし今すぐ利用可能なオプションを取り出したい場合は、`set_option` コマンドを実行し、カーソルをその名前が書かれている場所に移動するだけで、自動補完の候補としてそれらのオプションのリストが表示されるでしょう。メタ関連でデバッグをしているときに最も役に立つオプションは `trace.` から始まるものです。
64+
65+ --#--
4866## Options in meta programming
67+ --#--
68+ ## メタプログラミングにおけるオプション
69+ --#--
4970Now that we know how to set options, let's take a look at how a meta program
5071can get access to them. The most common way to do this is via the `MonadOptions`
5172type class, an extension to `Monad` that provides a function `getOptions : m Options`.
5273As of now, it is implemented by:
74+ --#--
75+ これでオプションを設定する方法がわかったので、メタプログラムがオプションにアクセスする方法を見てみましょう。最も一般的な方法は `MonadOptions` 型クラスを通じたもので、これは `Monad` に関数 `getOptions : m Options` を拡張したものです。現時点でこれは以下に対して実装されています:
76+
5377- `CoreM`
5478- `CommandElabM`
5579- `LevelElabM`
80+ --#--
5681- all monads to which you can lift operations of one of the above (e.g. `MetaM` from `CoreM`)
5782
83+ --#--
84+ - 上記のいずれかの操作を持ち上げることができるすべてのモナド(例えば、`CoreM` から `MetaM`)
85+
86+ --#--
5887Once we have an `Options` object, we can query the information via `Options.get`.
5988To show this, let's write a command that prints the value of `pp.explicit`.
89+ --#--
90+ 一度 `Options` オブジェクトを取得すれば、`Options.get` を使って情報を照会することができます。これを示すために、`pp.explicit` の値を表示するコマンドを書いてみましょう。
6091-/
6192elab "#getPPExplicit" : command => do
6293 let opts ← getOptions
@@ -69,12 +100,22 @@ set_option pp.explicit true in
69100#getPPExplicit -- pp.explicit : true
70101
71102/-!
103+ --#--
72104Note that the real implementation of getting `pp.explicit`, `Lean.getPPExplicit`,
73105uses whether `pp.all` is set as a default value instead.
74106
107+ --#--
108+ `pp.explicit` を取得するた実際の実装である `Lean.getPPExplicit` は代わりに `pp.all` がデフォルト値として設定されているかどうかを使用することに注意してください。
109+
110+ --#--
75111## Making our own
112+ --#--
113+ ## 独自のオプションを作る
114+ --#--
76115Declaring our own option is quite easy as well. The Lean compiler provides
77116a macro `register_option` for this. Let's see it in action:
117+ --#--
118+ 独自のオプションを宣言するのも非常に簡単です。Lean コンパイラはこのために `register_option` というマクロを用意しています。実際に使ってみましょう:
78119-/
79120
80121register_option book.myGreeting : String := {
@@ -84,6 +125,9 @@ register_option book.myGreeting : String := {
84125}
85126
86127/-!
128+ --#--
87129However, we cannot just use an option that we just declared in the same file
88130it was declared in because of initialization restrictions.
131+ --#--
132+ しかし、初期化の制約があるため、宣言したオプションをそのまま同じファイルで使うことはできません。
89133-/
0 commit comments