rblean 0.2.0 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d7a4faf3552c96d965a99a51737590fb95c2597714636653665f4c97c25dc03c
4
- data.tar.gz: '0828eb5b4ea602c5c3104e54428998f6c9a55150902b0edd64711c9c18b1ce63'
3
+ metadata.gz: b452c033fd1a1cd7ccb5c44b23c00bf8352f0f498020d689a5daa85d72034ae9
4
+ data.tar.gz: 6d8c0d7b20196a68af536d2696047a7befb5efbe0fe21faad47619a45dd8a4d5
5
5
  SHA512:
6
- metadata.gz: 2e679bebf6befc5c815561f21cb2e183e4ab15be070d7711722b8d47cce388367c41c87e340d3594e4b24805e206f74d4820481d8d6a9179948131d99c38104c
7
- data.tar.gz: bb924cb9ab33c605f45cf2fb36fa67c4c1c8eb369bc90f363f7d40d17f4cc9c5323452d23d5811176544f7db8a8125c071451901795cfb2f6a01f58a0763cb17
6
+ metadata.gz: 619411dc31b54016c35737fb23e58bfcd3eebe4a8fd79ce4e080cbc74f376ca013076168459bc3961ec84a74485d80947993e23bcafed8247ea587cc2c3da114
7
+ data.tar.gz: 05e83dd3981dc9f1bbc4ffcd2eb2503c8d77c30c64bc2f84511ef5ce2763529c4aa0a91838de07f19fc3fa59b6dd3e4675662a3709ec70b007ce68f320df9c9b
data/README.md CHANGED
@@ -105,6 +105,12 @@ To import the modules of a Lake project, pass `project:` (run `lake build` there
105
105
  ```ruby
106
106
  RbLean.start(project: "path/to/lake-project") do |lean|
107
107
  lean.elaborate("import MyProject\n#check MyProject.foo")
108
+
109
+ # A document named after an existing project file elaborates under that file's module,
110
+ # which Lean puts into generated names such as anonymous instances
111
+ lean.document("MyProject/Inst.lean")
112
+ .declaration_axioms("instance : Inhabited Nat := ⟨sorry⟩")
113
+ # => {"instInhabitedNat_myProject" => ["sorryAx"]}
108
114
  end
109
115
  ```
110
116
 
data/lib/rblean/cli.rb CHANGED
@@ -87,7 +87,7 @@ module RbLean
87
87
  raise UsageError, "no such file: #{file}" unless File.file?(file)
88
88
 
89
89
  source = File.read(file, encoding: Encoding::UTF_8)
90
- RbLean.start(project: @options[:dir]) do |lean|
90
+ RbLean.start(project: @options[:dir] || Dir.pwd) do |lean|
91
91
  # Not a scratch name: in a Lake project the file's own path decides its module.
92
92
  doc = lean.document(File.expand_path(file))
93
93
  result = doc.elaborate(source)
@@ -112,21 +112,23 @@ module RbLean
112
112
  raise UsageError, "no such file: #{file}" unless File.file?(file)
113
113
 
114
114
  source = File.read(file, encoding: Encoding::UTF_8)
115
- RbLean.start(project: @options[:dir]) do |lean|
116
- name ? print_axioms(lean, name, source) : print_declaration_axioms(lean, file, source)
115
+ RbLean.start(project: @options[:dir] || Dir.pwd) do |lean|
116
+ # Not a scratch name: the module name, which the file's path decides, goes into generated names.
117
+ doc = lean.document(File.expand_path(file))
118
+ name ? print_axioms(doc, name, source) : print_declaration_axioms(doc, file, source)
117
119
  end
118
120
  end
119
121
 
120
- def print_axioms(lean, name, source)
121
- axioms = lean.axioms(name, source)
122
+ def print_axioms(doc, name, source)
123
+ axioms = doc.axioms(name, source)
122
124
  @out.puts(axioms.empty? ? "no axioms" : axioms)
123
125
  EXIT_OK
124
126
  rescue ArgumentError => e
125
127
  raise UsageError, e.message
126
128
  end
127
129
 
128
- def print_declaration_axioms(lean, file, source)
129
- axioms = lean.declaration_axioms(source)
130
+ def print_declaration_axioms(doc, file, source)
131
+ axioms = doc.declaration_axioms(source)
130
132
  @out.puts("no declarations") if axioms.empty?
131
133
  axioms.each { |decl, list| @out.puts("#{decl}: #{list.empty? ? "no axioms" : list.join(", ")}") }
132
134
  axioms.values.any? { |list| list.include?("sorryAx") } ? EXIT_FAILED : EXIT_OK
@@ -37,6 +37,10 @@ module RbLean
37
37
  end
38
38
  end
39
39
 
40
+ def axioms(name, source, timeout: nil) = @server.__send__(:axioms_in, self, name, source, timeout)
41
+
42
+ def declaration_axioms(source, timeout: nil) = @server.__send__(:declaration_axioms_in, self, source, timeout)
43
+
40
44
  # Waits for a call already running on this document, so its didClose cannot overtake that call's text.
41
45
  def close
42
46
  locked(Internal::Deadline.after(nil)) do
@@ -5,19 +5,19 @@ require "json"
5
5
  module RbLean
6
6
  module Internal
7
7
  class Transport
8
- def initialize(input, output, logger: nil, debug: false)
8
+ def initialize(input, output, logger: nil)
9
9
  @input = input
10
10
  @output = output
11
11
  @input.binmode
12
12
  @output.binmode
13
13
  @write_lock = Mutex.new
14
14
  @logger = logger
15
- @debug = debug
16
15
  end
17
16
 
18
17
  def write(message)
19
18
  body = JSON.generate(message).b
20
- @logger&.debug("rblean --> #{body.dup.force_encoding(Encoding::UTF_8)}") if @debug
19
+ # Not a string built for every message: the block runs only when the logger's level is debug.
20
+ @logger&.debug { "rblean --> #{body.dup.force_encoding(Encoding::UTF_8)}" }
21
21
  @write_lock.synchronize do
22
22
  @output.write("Content-Length: #{body.bytesize}\r\n\r\n", body)
23
23
  @output.flush
@@ -42,7 +42,7 @@ module RbLean
42
42
  return nil if body.nil? || body.bytesize < length
43
43
 
44
44
  body.force_encoding(Encoding::UTF_8)
45
- @logger&.debug("rblean <-- #{body}") if @debug
45
+ @logger&.debug { "rblean <-- #{body}" }
46
46
  JSON.parse(body)
47
47
  end
48
48
  end
data/lib/rblean/server.rb CHANGED
@@ -2,7 +2,6 @@
2
2
 
3
3
  require "json"
4
4
  require "uri"
5
- require "logger"
6
5
 
7
6
  module RbLean
8
7
  class Server
@@ -41,8 +40,7 @@ module RbLean
41
40
  # Not only for project: elan shims read lean-toolchain from the working directory.
42
41
  @root = project ? File.expand_path(project.to_s) : Dir.pwd
43
42
  @timeout = timeout
44
- @debug = ENV["RBLEAN_DEBUG"] == "1"
45
- @logger = logger || (@debug ? Logger.new($stderr) : nil)
43
+ @logger = logger
46
44
  @lock = Mutex.new
47
45
  @closed = false
48
46
  @pid = Process.pid
@@ -60,7 +58,7 @@ module RbLean
60
58
  end
61
59
  @process = Internal::ChildProcess.new(command, chdir: @root, logger: @logger)
62
60
  Server.register(self)
63
- transport = Internal::Transport.new(@process.stdout, @process.stdin, logger: @logger, debug: @debug)
61
+ transport = Internal::Transport.new(@process.stdout, @process.stdin, logger: @logger)
64
62
  @protocol = Internal::Protocol.new(
65
63
  transport,
66
64
  on_notification: method(:handle_notification),
@@ -100,36 +98,9 @@ module RbLean
100
98
  end
101
99
  end
102
100
 
103
- def axioms(name, source, timeout: nil)
104
- text = "#{source}\n"
105
- # Not plain #print axioms: open namespaces shorten the names it prints (`open Classical` gives `choice`).
106
- result = elaborate("#{text}set_option pp.fullNames true in #print axioms #{name}\n", timeout:)
107
- printed = result.messages.select { |m| m.line == text.count("\n") + 1 }
108
- error = printed.find(&:error?)
109
- raise ArgumentError, error.text if error
110
- # Not merged: under `open`, a name can resolve to several constants and Lean prints each.
111
- raise ArgumentError, "#print axioms #{name} gave #{printed.size} results" unless printed.size == 1
112
-
113
- printed.first.text[/: \[(.*)\]\z/m, 1].to_s.split(/,\s+/)
114
- end
115
-
116
- def declaration_axioms(source, timeout: nil)
117
- text = "import Lean\n#{source}\n"
118
- # Not checked in the run with the helper: an unfinished last command would report the helper's first token.
119
- errors = elaborate(text, timeout:).errors
120
- # Not a Hash with the rest: Lean drops a declaration it cannot parse, so its sorry would go unseen.
121
- raise SourceError, errors.map { |m| shift_up(m) } unless errors.empty?
122
-
123
- result = elaborate("#{text}#{AXIOMS}\n#eval show Lean.Elab.Command.CommandElabM Unit from " \
124
- "do IO.println (← RbLeanAxioms.run #{text.count("\n")}).compress\n", timeout:)
125
- last = result.source.count("\n")
126
- error = result.errors.first
127
- output = result.infos.find { |m| m.line == last }
128
- raise Error, "axioms helper failed: #{error ? error.text : "no output"}" if error || output.nil?
101
+ def axioms(name, source, timeout: nil) = axioms_in(self, name, source, timeout)
129
102
 
130
- # Not one entry per name: a macro used twice declares the same name in two scopes.
131
- JSON.parse(output.text).each_with_object({}) { |(name, axioms), h| h[name] = h.fetch(name, []) | axioms }
132
- end
103
+ def declaration_axioms(source, timeout: nil) = declaration_axioms_in(self, source, timeout)
133
104
 
134
105
  def eval(term, source, timeout: nil)
135
106
  # Not plain #eval of the Json: it pretty-prints and wraps long values.
@@ -195,6 +166,41 @@ module RbLean
195
166
 
196
167
  private
197
168
 
169
+ def axioms_in(target, name, source, timeout)
170
+ text = "#{source}\n"
171
+ # Not plain #print axioms: open namespaces shorten the names it prints (`open Classical` gives `choice`).
172
+ result = target.elaborate("#{text}set_option pp.fullNames true in #print axioms #{name}\n", timeout:)
173
+ printed = result.messages.select { |m| m.line == text.count("\n") + 1 }
174
+ error = printed.find(&:error?)
175
+ raise ArgumentError, error.text if error
176
+ # Not merged: under `open`, a name can resolve to several constants and Lean prints each.
177
+ raise ArgumentError, "#print axioms #{name} gave #{printed.size} results" unless printed.size == 1
178
+
179
+ printed.first.text[/: \[(.*)\]\z/m, 1].to_s.split(/,\s+/)
180
+ end
181
+
182
+ def declaration_axioms_in(target, source, timeout)
183
+ text = "import Lean\n#{source}\n"
184
+ # Not checked in the run with the helper: an unfinished last command would report the helper's first token.
185
+ checked = target.elaborate(text, timeout:)
186
+ errors = checked.errors
187
+ # Not a Hash with the rest: Lean drops a declaration it cannot parse, so its sorry would go unseen.
188
+ raise SourceError, errors.map { |m| shift_up(m) } unless errors.empty?
189
+
190
+ result = target.elaborate("#{text}#{AXIOMS}\n#eval show Lean.Elab.Command.CommandElabM Unit from " \
191
+ "do IO.println (← RbLeanAxioms.run #{text.count("\n")}).compress\n", timeout:)
192
+ last = result.source.count("\n")
193
+ error = result.errors.first
194
+ output = result.infos.find { |m| m.line == last }
195
+ raise Error, "axioms helper failed: #{error ? error.text : "no output"}" if error || output.nil?
196
+
197
+ # Not one entry per name: a macro used twice declares the same name in two scopes.
198
+ axioms = JSON.parse(output.text).each_with_object({}) { |(name, list), h| h[name] = h.fetch(name, []) | list }
199
+ # Not from the helper: an example never enters the environment, so only Lean's sorry warning shows it.
200
+ axioms["example"] = ["sorryAx"] if checked.warnings.any? { |m| example_sorry?(m, checked.source) }
201
+ axioms
202
+ end
203
+
198
204
  def evaluate(source, action, timeout:, commands: "")
199
205
  # Not Lean.Data.Json alone, though eval needs no more: a document whose imports change restarts its worker.
200
206
  text = "import Lean\n#{source}\n"
@@ -211,6 +217,14 @@ module RbLean
211
217
  output
212
218
  end
213
219
 
220
+ def example_sorry?(message, source)
221
+ start = message.span.start
222
+ stop = message.span.stop
223
+ # Not any sorry warning: Lean puts an example's on its `example` keyword, a named declaration's on its name.
224
+ message.text.start_with?("declaration uses `") && start.line == stop.line &&
225
+ source.lines[start.line - 1][start.column...stop.column] == "example"
226
+ end
227
+
214
228
  def shift_up(message)
215
229
  # Not line 0: an error in the header lands on the `import Lean` line put before the source.
216
230
  shift = ->(pos) { pos.line > 1 ? pos.with(line: pos.line - 1) : Position.new(1, 0) }
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RbLean
4
- VERSION = "0.2.0"
4
+ VERSION = "0.2.1"
5
5
  end
metadata CHANGED
@@ -1,28 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rblean
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - dak2
8
8
  bindir: exe
9
9
  cert_chain: []
10
10
  date: 1980-01-02 00:00:00.000000000 Z
11
- dependencies:
12
- - !ruby/object:Gem::Dependency
13
- name: logger
14
- requirement: !ruby/object:Gem::Requirement
15
- requirements:
16
- - - ">="
17
- - !ruby/object:Gem::Version
18
- version: '0'
19
- type: :runtime
20
- prerelease: false
21
- version_requirements: !ruby/object:Gem::Requirement
22
- requirements:
23
- - - ">="
24
- - !ruby/object:Gem::Version
25
- version: '0'
11
+ dependencies: []
26
12
  executables:
27
13
  - rblean
28
14
  extensions: []