forked from idris-hackers/idris-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
idris-prover.el
468 lines (411 loc) · 19.6 KB
/
idris-prover.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
;;; idris-prover.el --- Prover mode for Idris -*- lexical-binding: t -*-
;; Copyright (C) 2013-2014, Hannes Mehnert and David Raymond Christiansen
;; Author: Hannes Mehnert <hannes@mehnert.org>, David Raymond Christiansen <david@davidchristiansen.dk>
;; License:
;; Inspiration is taken from SLIME/DIME (http://common-lisp.net/project/slime/) (https://github.com/dylan-lang/dylan-mode)
;; Therefore license is GPL
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
(require 'idris-core)
(require 'idris-common-utils)
(require 'idris-warnings)
(require 'idris-settings)
(require 'inferior-idris)
(eval-when-compile (require 'cl-lib))
; consisting of three buffers:
; ------------------------------
; | proof obligations |
; |----------------------------|
; | proof shell | proof script |
; ------------------------------
(defgroup idris-prover nil "Idris Prover." :prefix 'idris :group 'idris)
(defface idris-prover-processed-face
'((t (:background "PaleGreen1")))
"Face for Idris proof script which is already processed."
:group 'idris-faces)
(defface idris-prover-processing-face
'((t (:background "gold")))
"Face for Idris proof script which is currently processing."
:group 'idris-faces)
(defcustom idris-prover-restore-window-configuration t
"When non-nil, restore the window configuration after exiting the prover."
:type 'boolean
:group 'idris-prover)
(defvar idris-prover-obligations-buffer-name (idris-buffer-name :proof-obligations)
"The name of the Idris proof obligation buffer.")
(defvar idris-prover-shell-buffer-name (idris-buffer-name :proof-shell)
"The name of the Idris proof shell buffer.")
(defvar idris-prover-script-buffer-name (idris-buffer-name :proof-script)
"The name of the Idris proof script buffer.")
(defvar idris-prover-currently-proving nil
"The hole that Idris has open in the interactive prover.
Nil if Idris is not proving anything.")
(defconst idris-prover-error-message-prefix "Prover error: "
"A prefix to show on minibuffer error messages that originate in the prover.")
(defun idris-prover-obligations-buffer ()
(or (get-buffer idris-prover-obligations-buffer-name)
(let ((buffer (get-buffer-create idris-prover-obligations-buffer-name)))
(with-current-buffer buffer
(setq buffer-read-only t))
buffer)))
(defun idris-prover-show-obligations ()
(display-buffer (idris-prover-obligations-buffer)
'(display-buffer-pop-up-window . nil)))
(defun idris-prover-write-goals (goals)
"Write GOALS to the goal buffer.
If GOALS is a string, it is treated as undecorated text.
Otherwise, it must be a two-element list whose car is a goal
string and whose cadr is highlighting information."
(with-current-buffer (idris-prover-obligations-buffer)
(let ((buffer-read-only nil))
(erase-buffer)
(when idris-show-help-text
(setq header-line-format
"This is a read-only view of your proof state. Prove the lemma in the script buffer."))
(let ((goals-string (car goals))
(goals-spans (cadr goals)))
(idris-propertize-spans (idris-repl-semantic-text-props goals-spans)
(insert goals-string)))))
(idris-prover-show-obligations))
(defvar idris-prover-saved-window-configuration nil
"The saved window configuration from before running the prover.")
(defvar-local idris-prover-script-processed nil
"Marker for the processed part of proof script.")
(defvar-local idris-prover-script-processed-overlay nil
"Overlay for processed proof script.")
(defvar-local idris-prover-script-processing nil
"Marker for the processing part of proof script.")
(defvar-local idris-prover-script-processing-overlay nil
"Overlay for processing proof script.")
(defvar-local idris-prover-script-warning-overlay nil
"Overlay for warning in proof script.")
; invariant: point-min <= idris-prover-script-processed <= idris-prover-script-processing <= point-max
(defvar-local idris-prover-prove-step 0
"Step counter of the proof.")
(defvar idris-prover-script-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "RET") 'idris-prover-script-ret)
(define-key map (kbd "M-n") 'idris-prover-script-forward)
(define-key map (kbd "M-p") 'idris-prover-script-backward)
(define-key map (kbd "C-c C-q") 'idris-prover-script-qed)
(define-key map (kbd "C-c C-k") 'idris-prover-abandon)
;; Using (kbd "<TAB>") in place of "\t" makes emacs angry, and suggests
;; using the latter form.
(define-key map "\t" 'completion-at-point)
map)
"Keymap used in Idris proof script mode.")
(defun idris-prover-complete ()
"Completion of the current input."
(let* ((start (line-beginning-position))
(input (buffer-substring-no-properties
start
(point)))
(result (idris-eval `(:repl-completions ,input))))
(cl-destructuring-bind (completions _partial) (car result)
(if (null completions)
nil
(list start (point) completions)))))
(defun idris-prover-find-tactic (start-pos)
"Use layout heuristics to find the tactic beginning at START-POS.
Return a pair consisting of the start and end positions of the tactic.
Tactics are required to begin at the left margin."
(let (tactic-start tactic-end)
(save-excursion
(goto-char start-pos)
;; Ensure that we're at the next line beginning
(beginning-of-line)
(unless (= (point) start-pos)
(forward-line))
;; Go forward until the current line begins a tactic
(while (and (not (eobp)) (not (looking-at-p "[a-zA-Z]")))
(forward-line))
(if (eobp) ;; if at end of buffer, no tactic to be found!
nil
(setq tactic-start (point))
;; Go forward until end of buffer or non-blank line at left margin
(forward-line)
(while (and (not (eobp)) (not (looking-at-p "[a-zA-Z]")))
(forward-line))
;; Go backward until a non-whitespace char is found - it is the end of
;; the tactic
(backward-char)
(while (looking-at-p "\\s-\\|$") (backward-char))
(forward-char)
(setq tactic-end (point))
(cons tactic-start tactic-end)))))
(defun idris-prover-script-backward ()
"Backward one piece of proof script."
(interactive)
(idris-eval-async (list :interpret (if idris-enable-elab-prover ":undo" "undo"))
#'(lambda (_result) t)
#'(lambda (condition)
(message (concat idris-prover-error-message-prefix condition)))))
(defun idris-prover-script-forward ()
"Forward one piece of proof script."
(interactive)
(when (eobp) (newline)) ;; There must be a newline at the end
(when idris-prover-script-warning-overlay
(delete-overlay idris-prover-script-warning-overlay)
(setq idris-prover-script-warning-overlay nil))
(goto-char (+ 1 idris-prover-script-processed))
(let ((prior-processed-position (marker-position idris-prover-script-processed)) ; to restore on error
(next-tactic (idris-prover-find-tactic
idris-prover-script-processed)))
(if (null next-tactic)
(error "At the end of the proof script")
(let* ((tactic-start (car next-tactic))
(tactic-end (cdr next-tactic))
(tactic-text (buffer-substring-no-properties tactic-start
tactic-end)))
(set-marker idris-prover-script-processed tactic-start)
(set-marker idris-prover-script-processing tactic-end)
(let ((overlay (make-overlay idris-prover-script-processed
idris-prover-script-processing)))
(overlay-put overlay 'face 'idris-prover-processing-face)
(setq idris-prover-script-processing-overlay overlay))
(with-no-warnings
(let ((tactic-cmd (replace-regexp-in-string
"\\`[ \t\n]*" ""
;; replace Windows newlines with a space
(replace-regexp-in-string "" " " tactic-text))))
(idris-rex () (list :interpret tactic-cmd) nil
((:ok _result)
(with-current-buffer (idris-prover-script-buffer)
(when idris-prover-script-processing-overlay
(delete-overlay idris-prover-script-processing-overlay)
(setq idris-prover-script-processing-overlay nil))
;; Delete the region because it will be or has been
;; written with the proof state.
(delete-region idris-prover-script-processed
idris-prover-script-processing)
;; Put point at a useful spot for the next tactic
(when (eql (marker-position idris-prover-script-processed) (point-max))
(goto-char idris-prover-script-processed)
(let ((inhibit-read-only t)) (insert "\n")))
(goto-char (1+ (marker-position idris-prover-script-processed)))
(recenter)))
((:error condition &optional _spans)
(with-current-buffer (idris-prover-script-buffer)
(when idris-prover-script-processing-overlay
(delete-overlay idris-prover-script-processing-overlay)
(setq idris-prover-script-processing-overlay nil))
(setq idris-prover-script-warning-overlay
(idris-warning-create-overlay idris-prover-script-processed
idris-prover-script-processing
condition))
;; Restore the original position of the marker for
;; the processed region to prevent Emacs and Idris
;; from getting out of sync RE proof script contents
(set-marker idris-prover-script-processed prior-processed-position))
(message (concat idris-prover-error-message-prefix condition))
t))))))))
(defun idris-prover-script-ret ()
"Insert a newline at the end of buffer, even if it's read-only."
(interactive)
(if (equal (point) (marker-position idris-prover-script-processed))
(let ((inhibit-read-only t)) (insert "\n"))
(newline)))
(defun idris-prover-script-qed ()
"Send a QED command to Idris."
(interactive)
(if idris-prover-currently-proving
(idris-eval-async (list :interpret (if idris-enable-elab-prover ":qed" "qed"))
#'(lambda (_result) t)
#'(lambda (condition)
(message (concat idris-prover-error-message-prefix condition))))
(error "No proof in progress")))
(easy-menu-define idris-prover-script-mode-menu idris-prover-script-mode-map
"Menu for Idris prover scripts."
`("Idris Proof"
["Advance" idris-prover-script-forward t]
["Retract" idris-prover-script-backward t]
["QED" idris-prover-script-qed t]
["Abandon" idris-prover-abandon t])
)
(define-derived-mode idris-prover-script-mode prog-mode "Idris-Proof-Script"
"Major mode for interacting with Idris proof script.
\\{idris-prover-script-mode-map}
Invokes `idris-prover-script-mode-hook'."
:group 'idris-prover
(set (make-local-variable 'completion-at-point-functions)
'(idris-prover-complete))
(set (make-local-variable 'indent-tabs-mode) nil))
(defun idris-prover-script-buffer ()
(or (get-buffer idris-prover-script-buffer-name)
(let ((buffer (get-buffer-create idris-prover-script-buffer-name)))
(with-current-buffer buffer
(idris-prover-script-mode)
(setq idris-prover-script-processing (make-marker))
(setq idris-prover-script-processed (make-marker))
(set-marker idris-prover-script-processing (point))
(set-marker idris-prover-script-processed (point)))
buffer)))
(defun idris-prover-reset-prover-script-buffer ()
"Erase or initialize a proof script buffer, resetting all the prover state."
(with-current-buffer (idris-prover-script-buffer)
(when idris-prover-script-processed-overlay
(delete-overlay idris-prover-script-processed-overlay)
(setq idris-prover-script-processed-overlay nil))
(when idris-prover-script-processing-overlay
(delete-overlay idris-prover-script-processing-overlay)
(setq idris-prover-script-processing-overlay nil))
(setq idris-prover-prove-step 0)
(erase-buffer)
(when idris-show-help-text
(setq header-line-format
(let ((fwd (where-is-internal 'idris-prover-script-forward))
(bak (where-is-internal 'idris-prover-script-backward))
(qed (where-is-internal 'idris-prover-script-qed)))
(concat " Write your proof script here."
(if (and fwd bak qed)
(format "Use %s to advance and %s to retract. %s saves a completed proof."
(key-description (car fwd))
(key-description (car bak))
(key-description (car qed)))
"")))))
(unless idris-prover-script-processing
(setq idris-prover-script-processing (make-marker)))
(unless idris-prover-script-processed
(setq idris-prover-script-processed (make-marker)))
(set-marker idris-prover-script-processing (point))
(set-marker idris-prover-script-processed (point))))
(defun idris-prover-write-script (script count)
"Put the proof state recieved from Idris into the proof script buffer.
SCRIPT is the list of tactics in the proof state, and COUNT is
the length reported by Idris."
(interactive)
(with-current-buffer (idris-prover-script-buffer)
(let ((inhibit-read-only t))
(put-text-property (point-min) (point-max) 'read-only nil))
(cond ((< count idris-prover-prove-step)
;; this is actually the (count - 1) == Idris-prover-prove-step case!
;; in other words, we are undoing the final step.
;; can the other case(s) happen??
(goto-char idris-prover-script-processed)
(if (= (forward-line -1) 0)
;; we went back
(end-of-line)
;; we did not go back - are thus at top of buffer and must
;; retract whole script
(goto-char (point-min)))
(set-marker idris-prover-script-processed (point)))
((> count idris-prover-prove-step)
;; Here we are inserting a newly-checked proof step.
(goto-char idris-prover-script-processed)
(while (< idris-prover-prove-step count)
(let ((lelem (nth idris-prover-prove-step script)))
(insert-before-markers lelem))
(newline)
(setq idris-prover-prove-step (1+ idris-prover-prove-step))))
(t nil))
(setq idris-prover-prove-step count)
(unless (null idris-prover-script-processed-overlay)
(delete-overlay idris-prover-script-processed-overlay))
(let ((overlay (make-overlay 0 idris-prover-script-processed)))
(overlay-put overlay 'face 'idris-prover-processed-face)
(setq idris-prover-script-processed-overlay overlay))
(let ((inhibit-read-only t))
(put-text-property (point-min) idris-prover-script-processed 'read-only t))))
(defun idris-prover-abandon ()
"Abandon an in-progress proof."
(interactive)
;; Ask for confirmation when called interactively
(when (or (not (called-interactively-p 'interactive))
(yes-or-no-p "Abandon proof and discard script? "))
(if idris-prover-currently-proving
(idris-eval (list :interpret (if idris-enable-elab-prover ":abandon" "abandon")) t)
(error "No proof in progress"))))
(defun idris-prover-end ()
"Remove buffers from proof mode and unset global state related to the prover."
(interactive)
(setq idris-prover-currently-proving nil)
(let ((obligations (idris-prover-obligations-buffer))
(script (idris-prover-script-buffer)))
(when obligations
(delete-windows-on obligations)
(kill-buffer obligations))
(when script (kill-buffer script)))
(when (and idris-prover-restore-window-configuration
(window-configuration-p
idris-prover-saved-window-configuration))
(set-window-configuration idris-prover-saved-window-configuration))
(setq idris-prover-saved-window-configuration nil))
(autoload 'idris-repl-write-string "idris-repl.el")
(defun idris-prover-event-hook-function (event)
"Process an EVENT returned from Idris when the prover is running."
(pcase event
(`(:start-proof-mode ,name ,_target)
(setq idris-prover-currently-proving name)
(setq idris-prover-saved-window-configuration
(current-window-configuration))
(idris-prover-reset-prover-script-buffer)
(idris-repl-write-string (format "Start proof of %s" name))
(let* ((obligations-window (idris-prover-show-obligations))
(script-window (split-window obligations-window)))
(set-window-buffer script-window (idris-prover-script-buffer)))
t)
(`(:end-proof-mode ,msg ,_target)
(let ((name (car msg))
(proof (cadr msg)))
(idris-perhaps-insert-proof-script proof)
(idris-prover-end)
(idris-repl-write-string (concat "End proof of " name))
(run-hooks 'idris-prover-success-hook))
t)
(`(:write-proof-state ,msg ,_target)
(cl-destructuring-bind (script count) msg
(idris-prover-write-script script count))
t)
(`(:write-goal ,goal ,_target)
(idris-prover-write-goals goal)
t)
(`(:abandon-proof ,_msg ,_target)
(when (get-buffer idris-prover-script-buffer-name)
(with-current-buffer idris-prover-script-buffer-name
(copy-region-as-kill (point-min) (point-max))
(message "Proof saved to kill ring")))
(idris-prover-end)
(idris-repl-write-string "Abandoned proof")
t)
(_ nil)))
(defun idris-perhaps-insert-proof-script (proof)
"Prompt the user as to whether PROOF should be inserted into the buffer."
(save-window-excursion
(pop-to-buffer idris-currently-loaded-buffer)
(delete-other-windows)
(let ((proof-buffer (get-buffer-create "*idris-finished-proof*")))
(unwind-protect
(progn
(pop-to-buffer proof-buffer)
(insert proof)
(if (y-or-n-p "Keep this proof script?")
(idris-insert-proof-script idris-currently-loaded-buffer proof)
(kill-new proof)
(message "Proof saved to kill ring")))
(kill-buffer proof-buffer)))))
(defconst idris-proof-script-insertion-marker "---------- Proofs ----------"
"Look for this marker to insert proofs.
Should agree with the one in the Idris compiler source.")
(defun idris-insert-proof-script (buffer proof)
"Insert PROOF into BUFFER."
(with-current-buffer buffer
(save-excursion
(goto-char (point-min))
(unless (re-search-forward idris-proof-script-insertion-marker nil t)
(when (re-search-forward "\\(\\s-*\n\\)*\\'")
(replace-match (concat "\n\n" idris-proof-script-insertion-marker "\n") nil nil)))
(newline)
(insert proof)
(newline))))
(provide 'idris-prover)