rblean 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +130 -0
- data/exe/rblean +6 -0
- data/lib/rblean/batch.rb +104 -0
- data/lib/rblean/checker.rb +80 -0
- data/lib/rblean/cli.rb +168 -0
- data/lib/rblean/document.rb +243 -0
- data/lib/rblean/errors.rb +72 -0
- data/lib/rblean/internal/positions.rb +53 -0
- data/lib/rblean/internal/protocol.rb +127 -0
- data/lib/rblean/internal/transport.rb +129 -0
- data/lib/rblean/server.rb +231 -0
- data/lib/rblean/toolchain.rb +112 -0
- data/lib/rblean/values.rb +126 -0
- data/lib/rblean/version.rb +5 -0
- data/lib/rblean.rb +51 -0
- data/sig/rblean.rbs +266 -0
- metadata +62 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 69c857e1d8e64bf20270cfaaaeb90d22dc86dfb5b654efa95f8c5b43b0c56123
|
|
4
|
+
data.tar.gz: 1b920897930d3d6dea5c431dbfa2c57187504e6d34c138eb8ffc26d483c57535
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: d9f8337492ef86294f3f385217044f52ae2e2363b69381c500b33cb0e649a0220d52e31753b838dbc855f56184272d0b5c1a8de4a7b829928560cbde3a4e822a
|
|
7
|
+
data.tar.gz: bac005e4035f15e44500c659981a0fff55954acccd1c25525c6de54a46f510000915ed3d7c97166bb0657f4740eda7729b20f3ef3e0c21a7f1bbb54a2b26d3f2
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 dak2
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
# rblean
|
|
2
|
+
|
|
3
|
+
Ruby から Lean 4 を動かすための gem です。Lean 本体に入っている言語サーバー(`lean --server`、Lake プロジェクトでは `lake serve`)と LSP で話し、検査結果・証明のゴール・型情報を Ruby のオブジェクトとして返します。
|
|
4
|
+
|
|
5
|
+
- 追加の Lean パッケージは要りません。プロジェクトの toolchain にそのまま合います。
|
|
6
|
+
- 実行時の依存は Ruby の標準ライブラリだけです。
|
|
7
|
+
- Ruby 4.0 以上。Linux と macOS をサポートします(`check` は `lake check` の制約で Linux のみ)。
|
|
8
|
+
|
|
9
|
+
## インストール
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
gem install rblean
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Lean は [elan](https://github.com/leanprover/elan) などで入れておきます。rblean は `lean:` 引数 → `RBLEAN_LEAN` / `RBLEAN_LAKE` → `LEAN` / `LAKE` → `PATH` → `~/.elan/bin` の順で探します。
|
|
16
|
+
|
|
17
|
+
## 使い方
|
|
18
|
+
|
|
19
|
+
```ruby
|
|
20
|
+
require "rblean"
|
|
21
|
+
|
|
22
|
+
# サーバーを立てて何度も精練する。ブロックを抜けると Lean を止める
|
|
23
|
+
RbLean.open do |lean|
|
|
24
|
+
r = lean.elaborate(<<~LEAN)
|
|
25
|
+
theorem t (n : Nat) : n + 0 = n := by
|
|
26
|
+
skip
|
|
27
|
+
simp
|
|
28
|
+
LEAN
|
|
29
|
+
r.success? # => true(Lean がエラーを報告しなかった)
|
|
30
|
+
r.goals(line: 2, column: 2) # => [#<data RbLean::Goal text="n : Nat\n⊢ n + 0 = n">]
|
|
31
|
+
|
|
32
|
+
r = lean.elaborate("example : 1 = 2 := by decide")
|
|
33
|
+
r.errors.first.to_s # => "1:22: error: Tactic `decide` proved that the proposition\n 1 = 2\nis false"
|
|
34
|
+
|
|
35
|
+
# 上級者向け:同じ文書を編集しながら何度も精練する
|
|
36
|
+
source = "theorem g : 2 + 2 = 4 := by\n sorry\n"
|
|
37
|
+
doc = lean.document("Search.lean", source)
|
|
38
|
+
found = %w[simp rfl decide].find do |tactic|
|
|
39
|
+
doc.update(source.sub("sorry", tactic)).elaborate.warnings.empty?
|
|
40
|
+
end
|
|
41
|
+
found # => "simp"
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# サーバーを立てずに一回だけ精練する
|
|
45
|
+
RbLean.elaborate("#eval 6 * 7").infos.map(&:text) # => ["42"]
|
|
46
|
+
|
|
47
|
+
# 証明が本当に正しいかを lake check で検証する(Linux のみ)
|
|
48
|
+
report = RbLean.check(project: "path/to/lake-project")
|
|
49
|
+
report.success? # => true / false
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Lake プロジェクトのモジュールを import するには `project:` を渡します(事前に `lake build` が必要です)。
|
|
53
|
+
|
|
54
|
+
```ruby
|
|
55
|
+
RbLean.open(project: "path/to/lake-project") do |lean|
|
|
56
|
+
lean.elaborate("import MyProject\n#check MyProject.foo")
|
|
57
|
+
end
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### 入口
|
|
61
|
+
|
|
62
|
+
| メソッド | 戻り値 | 説明 |
|
|
63
|
+
| --- | --- | --- |
|
|
64
|
+
| `RbLean.open(project: nil, lean: nil, strict: false, timeout: 60, scratch: 1, logger: nil) { \|server\| }` | ブロックの値 | サーバーを起動し、ブロックの後で必ず close する |
|
|
65
|
+
| `RbLean.open(...)`(ブロックなし) | `RbLean::Server` | 呼び出し側が close する |
|
|
66
|
+
| `RbLean.elaborate(source, project: nil, lean: nil, strict: false)` | `RbLean::Result` | `lean --json` による一回限りの精練 |
|
|
67
|
+
| `RbLean.elaborate_file(path, ...)` | `RbLean::Result` | ファイルを一回限りで精練 |
|
|
68
|
+
| `RbLean.check(project:, paranoid: false, lean: nil)` | `RbLean::CheckReport` | `lake check` を実行し、結果を返す |
|
|
69
|
+
| `RbLean.toolchain(project: nil, lean: nil)` | `RbLean::Toolchain` | 使われる lean / lake とバージョン |
|
|
70
|
+
|
|
71
|
+
- `strict: true` は Lean に `-DwarningAsError=true` を渡し、警告(`sorry` を含む)を Lean 自身にエラーとして報告させます。
|
|
72
|
+
- `scratch:` は名前なしの `elaborate` に使う作業用文書の数で、同時に流せる `elaborate` の数になります。
|
|
73
|
+
- `logger:` には `Logger` 互換のオブジェクトを渡します。Lean のログと stderr を流します。`RBLEAN_DEBUG=1` のときは送受信した JSON-RPC も debug レベルで出します。
|
|
74
|
+
|
|
75
|
+
### 位置
|
|
76
|
+
|
|
77
|
+
位置は行が 1 始まり、列が 0 始まりの文字(コードポイント)単位です。Lean がエラーを表示するときの `line:col` と、Ruby の `String#[]` の添字に合わせています。LSP の UTF-16 単位とは rblean が内部で変換するので、サーバー経由でも `RbLean.elaborate` でも同じ値になります。
|
|
78
|
+
|
|
79
|
+
### `success?` と `check`
|
|
80
|
+
|
|
81
|
+
`Result#success?` は「Lean がエラーを報告しなかった」(`lean` の終了コード 0)という意味です。`sorry` は警告なので `true` になりえます。証明が本当に正しいかは `RbLean.check`(`lake check`)に任せてください。rblean 自身は合否を判定しません。
|
|
82
|
+
|
|
83
|
+
### スレッド
|
|
84
|
+
|
|
85
|
+
1 つの `Server` を複数のスレッドから使えます。異なる文書は並行に精練され、同じ文書への操作は直列になります。`fork` した子プロセスから親の `Server` を使うと `RbLean::ClosedError` になります。
|
|
86
|
+
|
|
87
|
+
### 例外
|
|
88
|
+
|
|
89
|
+
すべて `RbLean::Error` のサブクラスです。Lean のコードの誤りは例外にせず、`Result` のメッセージとして返します。
|
|
90
|
+
|
|
91
|
+
| 例外 | 起きるとき |
|
|
92
|
+
| --- | --- |
|
|
93
|
+
| `ToolchainNotFound` | lean / lake が見つからない |
|
|
94
|
+
| `TimeoutError` | `timeout:` を過ぎた(要求は取り消し済み) |
|
|
95
|
+
| `ServerError` | LSP のエラー応答 |
|
|
96
|
+
| `ServerCrashed` | Lean のプロセスが終了した(終了ステータスと stderr の末尾つき) |
|
|
97
|
+
| `ClosedError` | close した後の呼び出し |
|
|
98
|
+
| `NoServerError` | `RbLean.elaborate` の `Result` に goals / hover を聞いた |
|
|
99
|
+
| `CheckerUnavailable` | `lake check` を実行できない(bwrap がない、Linux 以外、Lake プロジェクトでない、など) |
|
|
100
|
+
|
|
101
|
+
## CLI
|
|
102
|
+
|
|
103
|
+
```
|
|
104
|
+
rblean check lake check で証明が本当に正しいかを検証する
|
|
105
|
+
rblean goals FILE:LINE:COL その位置のゴールを表示する
|
|
106
|
+
rblean version rblean と Lean のバージョンを表示する
|
|
107
|
+
|
|
108
|
+
オプション
|
|
109
|
+
-d, --dir DIR Lake プロジェクトのルート(lake と同じ名前)
|
|
110
|
+
--lean PATH lean の実行ファイル
|
|
111
|
+
--json JSON で出力する(lean / lake と同じ名前)
|
|
112
|
+
--paranoid check で、同梱の外部チェッカーでも検証する
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
終了コードは 0(成功)、1(check が不合格、または goals の対象ファイルにエラーがある)、2(使い方の誤り)、3(toolchain が見つからない、サーバーが落ちた、lake check を実行できない)です。
|
|
116
|
+
|
|
117
|
+
1 ファイルを Lean に通してエラーを見るだけなら、Lean 公式の `lean Foo.lean` / `lake lean Foo.lean` を使ってください。
|
|
118
|
+
|
|
119
|
+
## 開発
|
|
120
|
+
|
|
121
|
+
```sh
|
|
122
|
+
bundle install
|
|
123
|
+
bundle exec rake test # 単体テスト + 結合テスト(Lean がなければ結合テストは skip)
|
|
124
|
+
RBLEAN_TEST_CHECK=1 bundle exec rake test # lake check のテストも回す(Linux + bwrap)
|
|
125
|
+
bundle exec rake steep # 型検査
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## ライセンス
|
|
129
|
+
|
|
130
|
+
MIT
|
data/exe/rblean
ADDED
data/lib/rblean/batch.rb
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "open3"
|
|
5
|
+
require "tempfile"
|
|
6
|
+
require "tmpdir"
|
|
7
|
+
|
|
8
|
+
module RbLean
|
|
9
|
+
# One-shot elaboration without a server: `lean --json FILE`, or
|
|
10
|
+
# `lake env lean --json FILE` at the root of a Lake project.
|
|
11
|
+
module Batch
|
|
12
|
+
SEVERITIES = { "error" => :error, "warning" => :warning, "information" => :information, "info" => :information }.freeze
|
|
13
|
+
private_constant :SEVERITIES
|
|
14
|
+
|
|
15
|
+
module_function
|
|
16
|
+
|
|
17
|
+
def elaborate(source, project: nil, lean: nil, strict: false, timeout: nil)
|
|
18
|
+
Dir.mktmpdir("rblean") do |dir|
|
|
19
|
+
path = File.join(dir, "RbLeanBatch.lean")
|
|
20
|
+
File.write(path, source)
|
|
21
|
+
run(path, source, project:, lean:, strict:, timeout:)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def elaborate_file(path, project: nil, lean: nil, strict: false, timeout: nil)
|
|
26
|
+
full = File.expand_path(path.to_s)
|
|
27
|
+
run(full, File.read(full, encoding: Encoding::UTF_8), project:, lean:, strict:, timeout:)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def run(path, source, project:, lean:, strict:, timeout:)
|
|
31
|
+
toolchain = Toolchain.detect(project:, lean:)
|
|
32
|
+
args = ["--json", *(strict ? Server::STRICT_ARGS : []), path]
|
|
33
|
+
command = project ? [toolchain.lake!, "env", toolchain.lean, *args] : [toolchain.lean, *args]
|
|
34
|
+
stdout, stderr, status = capture(command, chdir: toolchain.chdir, timeout:)
|
|
35
|
+
messages = parse(stdout)
|
|
36
|
+
if !status.success? && messages.none?(&:error?)
|
|
37
|
+
text = [stderr, stdout].map(&:strip).reject(&:empty?).first || "lean exited with #{status.inspect}"
|
|
38
|
+
messages << Message.new(severity: :error, text:, span: origin_span)
|
|
39
|
+
end
|
|
40
|
+
Result.new(messages:, source:)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# One JSON object per line; anything else becomes an :information message.
|
|
44
|
+
def parse(stdout)
|
|
45
|
+
stdout.each_line.filter_map do |line|
|
|
46
|
+
line = line.chomp
|
|
47
|
+
next if line.strip.empty?
|
|
48
|
+
|
|
49
|
+
json = begin
|
|
50
|
+
JSON.parse(line)
|
|
51
|
+
rescue JSON::ParserError
|
|
52
|
+
nil
|
|
53
|
+
end
|
|
54
|
+
if json.is_a?(Hash) && json.key?("severity")
|
|
55
|
+
from_json(json)
|
|
56
|
+
else
|
|
57
|
+
Message.new(severity: :information, text: line, span: origin_span)
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def from_json(json)
|
|
63
|
+
start = position(json["pos"]) || Position.new(line: 1, column: 0)
|
|
64
|
+
stop = position(json["endPos"]) || start
|
|
65
|
+
Message.new(
|
|
66
|
+
severity: SEVERITIES.fetch(json["severity"], :information),
|
|
67
|
+
text: json["data"].to_s,
|
|
68
|
+
span: Span.new(start:, stop:)
|
|
69
|
+
)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def position(pos)
|
|
73
|
+
return nil unless pos.is_a?(Hash)
|
|
74
|
+
|
|
75
|
+
Position.new(line: pos.fetch("line"), column: pos.fetch("column"))
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def origin_span
|
|
79
|
+
origin = Position.new(line: 1, column: 0)
|
|
80
|
+
Span.new(start: origin, stop: origin)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Like Open3.capture3, but KILLs the process and raises TimeoutError after +timeout+ seconds.
|
|
84
|
+
def capture(command, chdir:, timeout:, method_name: "elaborate")
|
|
85
|
+
Open3.popen3(*command, chdir:) do |stdin, stdout, stderr, wait|
|
|
86
|
+
stdin.close
|
|
87
|
+
out_reader = Thread.new { stdout.read }
|
|
88
|
+
err_reader = Thread.new { stderr.read }
|
|
89
|
+
unless wait.join(timeout)
|
|
90
|
+
begin
|
|
91
|
+
Process.kill("KILL", wait.pid)
|
|
92
|
+
rescue Errno::ESRCH
|
|
93
|
+
nil
|
|
94
|
+
end
|
|
95
|
+
wait.join
|
|
96
|
+
raise TimeoutError.new(method_name, timeout)
|
|
97
|
+
end
|
|
98
|
+
[out_reader.value.to_s.force_encoding(Encoding::UTF_8).scrub,
|
|
99
|
+
err_reader.value.to_s.force_encoding(Encoding::UTF_8).scrub,
|
|
100
|
+
wait.value]
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
end
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module RbLean
|
|
6
|
+
# Runs `lake check --json` at the project root. Every judgement is Lake's:
|
|
7
|
+
# rblean only runs it and mirrors the report.
|
|
8
|
+
module Checker
|
|
9
|
+
module_function
|
|
10
|
+
|
|
11
|
+
def check(project:, paranoid: false, lean: nil, timeout: nil)
|
|
12
|
+
root = File.expand_path(project.to_s)
|
|
13
|
+
unless %w[lakefile.lean lakefile.toml].any? { |f| File.file?(File.join(root, f)) }
|
|
14
|
+
raise CheckerUnavailable, "#{root} is not a Lake project (no lakefile.lean or lakefile.toml)"
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
toolchain = Toolchain.detect(project: root, lean:)
|
|
18
|
+
command = [toolchain.lake!, "check", "--json", *("--paranoid" if paranoid)]
|
|
19
|
+
stdout, stderr, status = Batch.capture(command, chdir: root, timeout:, method_name: "check")
|
|
20
|
+
|
|
21
|
+
if status.exitstatus == 2
|
|
22
|
+
raise CheckerUnavailable, (stderr.strip.empty? ? stdout.strip : stderr.strip)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
raw = parse_report(stdout)
|
|
26
|
+
unless raw
|
|
27
|
+
detail = [stderr, stdout].map(&:strip).reject(&:empty?).join("\n")
|
|
28
|
+
raise CheckerUnavailable, "lake check printed no JSON report (exit #{status.exitstatus}): #{detail}"
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
report(raw)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# The report is the last JSON object on stdout.
|
|
35
|
+
def parse_report(stdout)
|
|
36
|
+
text = stdout.strip
|
|
37
|
+
candidates = [text]
|
|
38
|
+
idx = text.rindex(/^\{/)
|
|
39
|
+
candidates << text[idx..] if idx
|
|
40
|
+
candidates.each do |c|
|
|
41
|
+
json = JSON.parse(c)
|
|
42
|
+
return json if json.is_a?(Hash) && json.key?("judgement")
|
|
43
|
+
rescue JSON::ParserError
|
|
44
|
+
next
|
|
45
|
+
end
|
|
46
|
+
nil
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def report(raw)
|
|
50
|
+
steps = (raw["report"] || {}).to_h do |key, value|
|
|
51
|
+
[key, key == "kernel_reports" ? value.to_h { |k, v| [k, step(v)] } : step(value)]
|
|
52
|
+
end
|
|
53
|
+
CheckReport.new(judgement: raw["judgement"].to_s, sandboxed: raw["sandboxed"] == true, steps:, raw:)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def step(value)
|
|
57
|
+
return value unless value.is_a?(Hash) && value.key?("judgement")
|
|
58
|
+
|
|
59
|
+
CheckReport::Step.new(judgement: value["judgement"].to_s, output: output_text(value["output"]))
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Output is {"str": ...} or {"proc": {"stdout": ..., "stderr": ...}} in Lake's JSON.
|
|
63
|
+
def output_text(output)
|
|
64
|
+
case output
|
|
65
|
+
when String then output
|
|
66
|
+
when Hash
|
|
67
|
+
if output.key?("str")
|
|
68
|
+
v = output["str"]
|
|
69
|
+
v.is_a?(Hash) ? v.values.join : v.to_s
|
|
70
|
+
elsif output.key?("proc")
|
|
71
|
+
v = output["proc"]
|
|
72
|
+
v.is_a?(Hash) ? [v["stdout"], v["stderr"]].compact.join : Array(v).join
|
|
73
|
+
else
|
|
74
|
+
output.values.map(&:to_s).join
|
|
75
|
+
end
|
|
76
|
+
else output.to_s
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
data/lib/rblean/cli.rb
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "optparse"
|
|
5
|
+
require_relative "../rblean"
|
|
6
|
+
|
|
7
|
+
module RbLean
|
|
8
|
+
# The rblean command. It only holds what Lean's own commands cannot do; every
|
|
9
|
+
# judgement is left to Lean / Lake, and rblean only calls and prints.
|
|
10
|
+
class CLI
|
|
11
|
+
EXIT_OK = 0
|
|
12
|
+
EXIT_FAILED = 1
|
|
13
|
+
EXIT_USAGE = 2
|
|
14
|
+
EXIT_TOOLCHAIN = 3
|
|
15
|
+
|
|
16
|
+
USAGE = <<~TEXT
|
|
17
|
+
Usage:
|
|
18
|
+
rblean check lake check で証明が本当に正しいかを検証する
|
|
19
|
+
rblean goals FILE:LINE:COL その位置のゴールを表示する
|
|
20
|
+
rblean version rblean と Lean のバージョンを表示する
|
|
21
|
+
|
|
22
|
+
Options:
|
|
23
|
+
-d, --dir DIR Lake プロジェクトのルート(lake と同じ名前)
|
|
24
|
+
--lean PATH lean の実行ファイル
|
|
25
|
+
--json JSON で出力する(lean / lake と同じ名前)
|
|
26
|
+
--paranoid check で、同梱の外部チェッカーでも検証する
|
|
27
|
+
TEXT
|
|
28
|
+
|
|
29
|
+
class UsageError < StandardError; end
|
|
30
|
+
|
|
31
|
+
def self.start(argv = ARGV, out: $stdout, err: $stderr) = new(out:, err:).run(argv)
|
|
32
|
+
|
|
33
|
+
def initialize(out: $stdout, err: $stderr)
|
|
34
|
+
@out = out
|
|
35
|
+
@err = err
|
|
36
|
+
@options = { dir: nil, lean: nil, json: false, paranoid: false }
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def run(argv) = catch(:rblean_exit) { dispatch(argv) }
|
|
40
|
+
|
|
41
|
+
private
|
|
42
|
+
|
|
43
|
+
def dispatch(argv)
|
|
44
|
+
args = parser.parse(argv)
|
|
45
|
+
command = args.shift
|
|
46
|
+
case command
|
|
47
|
+
when "check" then run_check(args)
|
|
48
|
+
when "goals" then run_goals(args)
|
|
49
|
+
when "version" then run_version(args)
|
|
50
|
+
when "axioms" then usage_error("axioms is planned for rblean 0.2")
|
|
51
|
+
when nil, "help" then @out.puts(USAGE).then { command ? EXIT_OK : EXIT_USAGE }
|
|
52
|
+
else usage_error("unknown command: #{command}")
|
|
53
|
+
end
|
|
54
|
+
rescue OptionParser::ParseError, UsageError => e
|
|
55
|
+
usage_error(e.message)
|
|
56
|
+
rescue ToolchainNotFound, ServerCrashed, CheckerUnavailable => e
|
|
57
|
+
@err.puts("rblean: #{e.message}")
|
|
58
|
+
EXIT_TOOLCHAIN
|
|
59
|
+
rescue Error => e
|
|
60
|
+
@err.puts("rblean: #{e.message}")
|
|
61
|
+
EXIT_TOOLCHAIN
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def parser
|
|
65
|
+
OptionParser.new do |o|
|
|
66
|
+
o.banner = USAGE
|
|
67
|
+
o.on("-d", "--dir DIR") { |v| @options[:dir] = v }
|
|
68
|
+
o.on("--lean PATH") { |v| @options[:lean] = v }
|
|
69
|
+
o.on("--json") { @options[:json] = true }
|
|
70
|
+
o.on("--paranoid") { @options[:paranoid] = true }
|
|
71
|
+
o.on("-h", "--help") do
|
|
72
|
+
@out.puts(USAGE)
|
|
73
|
+
throw :rblean_exit, EXIT_OK
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def usage_error(message)
|
|
79
|
+
@err.puts("rblean: #{message}")
|
|
80
|
+
@err.puts(USAGE)
|
|
81
|
+
EXIT_USAGE
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def run_check(args)
|
|
85
|
+
raise UsageError, "check takes no arguments (it checks the whole project)" unless args.empty?
|
|
86
|
+
raise UsageError, "check needs -d DIR (the Lake project root)" unless @options[:dir]
|
|
87
|
+
|
|
88
|
+
report = RbLean.check(project: @options[:dir], paranoid: @options[:paranoid], lean: @options[:lean])
|
|
89
|
+
if @options[:json]
|
|
90
|
+
@out.puts(JSON.pretty_generate(report.raw))
|
|
91
|
+
else
|
|
92
|
+
print_report(report)
|
|
93
|
+
end
|
|
94
|
+
report.success? ? EXIT_OK : EXIT_FAILED
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def print_report(report)
|
|
98
|
+
@out.puts("judgement: #{report.judgement}#{" (not sandboxed)" unless report.sandboxed}")
|
|
99
|
+
report.steps.each do |name, step|
|
|
100
|
+
if step.is_a?(Hash)
|
|
101
|
+
step.each { |kernel, s| print_step("#{name}/#{kernel}", s) }
|
|
102
|
+
else
|
|
103
|
+
print_step(name, step)
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def print_step(name, step)
|
|
109
|
+
return unless step.is_a?(CheckReport::Step)
|
|
110
|
+
|
|
111
|
+
@out.puts(" #{name}: #{step.judgement}")
|
|
112
|
+
return if step.success? || step.output.to_s.strip.empty?
|
|
113
|
+
|
|
114
|
+
step.output.to_s.each_line { |l| @out.puts(" #{l.chomp}") }
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def run_goals(args)
|
|
118
|
+
raise UsageError, "goals takes FILE:LINE:COL" unless args.size == 1
|
|
119
|
+
|
|
120
|
+
file, line, column = parse_location(args.first)
|
|
121
|
+
raise UsageError, "no such file: #{file}" unless File.file?(file)
|
|
122
|
+
|
|
123
|
+
RbLean.open(project: @options[:dir], lean: @options[:lean]) do |lean|
|
|
124
|
+
doc = lean.open_file(file)
|
|
125
|
+
result = doc.elaborate
|
|
126
|
+
goals = doc.goals(line:, column:)
|
|
127
|
+
if @options[:json]
|
|
128
|
+
@out.puts(JSON.generate({
|
|
129
|
+
"goals" => goals.map(&:text),
|
|
130
|
+
"messages" => result.errors.map { |m| message_json(m) }
|
|
131
|
+
}))
|
|
132
|
+
else
|
|
133
|
+
result.errors.each { |m| @err.puts("#{file}:#{m}") }
|
|
134
|
+
@out.puts(goals.empty? ? "no goals" : goals.map(&:text).join("\n\n"))
|
|
135
|
+
end
|
|
136
|
+
result.success? ? EXIT_OK : EXIT_FAILED
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def parse_location(arg)
|
|
141
|
+
m = arg.match(/\A(.+):(\d+):(\d+)\z/)
|
|
142
|
+
raise UsageError, "expected FILE:LINE:COL, got #{arg}" unless m
|
|
143
|
+
|
|
144
|
+
line = Integer(m[2], 10)
|
|
145
|
+
raise UsageError, "LINE starts at 1" if line < 1
|
|
146
|
+
|
|
147
|
+
[m[1], line, Integer(m[3], 10)]
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def message_json(m)
|
|
151
|
+
{
|
|
152
|
+
"severity" => m.severity.to_s,
|
|
153
|
+
"pos" => { "line" => m.span.start.line, "column" => m.span.start.column },
|
|
154
|
+
"endPos" => { "line" => m.span.stop.line, "column" => m.span.stop.column },
|
|
155
|
+
"data" => m.text
|
|
156
|
+
}
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def run_version(args)
|
|
160
|
+
raise UsageError, "version takes no arguments" unless args.empty?
|
|
161
|
+
|
|
162
|
+
@out.puts("rblean #{RbLean::VERSION}")
|
|
163
|
+
tc = RbLean.toolchain(project: @options[:dir], lean: @options[:lean])
|
|
164
|
+
@out.puts("Lean #{tc.version} (#{tc.lean})")
|
|
165
|
+
EXIT_OK
|
|
166
|
+
end
|
|
167
|
+
end
|
|
168
|
+
end
|