aboutsummaryrefslogtreecommitdiffhomepage
path: root/phox/phox-pbrpm.el
blob: fe2021667912e88ec9e7b013d9cbe1d75e1f9e68 (plain)
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
;; $State$ $Date$ $Revision$ 
;;--------------------------------------------------------------------------;;
;;--------------------------------------------------------------------------;;
;; the proof by rules popup menu part
;;
;; Note : program extraction is still experimental This file is very
;; dependant of the actual state of our developments
;;--------------------------------------------------------------------------;;

(require 'pg-pbrpm)

;;--------------------------------------------------------------------------;;
;; Syntactic functions
;;--------------------------------------------------------------------------;;
(setq
 pg-pbrpm-start-goal-regexp "^goal \\([0-9]+\\)/[0-9]+\\( (new)\\)?$"
 pg-pbrpm-start-goal-regexp-par-num 1
 pg-pbrpm-end-goal-regexp "^$"
 pg-pbrpm-start-hyp-regexp "^\\([A-Za-z0-9_.']+\\) := "
 pg-pbrpm-start-hyp-regexp-par-num 1
 pg-pbrpm-start-concl-regexp "^ *|- "
 pg-pbrpm-auto-select-regexp "\\(\\(\\(['a-zA-Z0-9]+\\)\\|\\([][><=/~&+*^%!{}:-]+\\)\\)\\(_+\\(\\(['a-zA-Z0-9]+\\)\\|\\([][><=/~&+*^%!{}:-]+\\)\\)\\)*\\)\\|\\(\\?[0-9]+\\)"
)


; TODO : deal with future invisible parentheses (french guillemots)
(defun phox-pbrpm-left-paren-p (char)
"Retrun t if the character is a left parenthesis : '(' or a french guillemot '<<'"
   (or
   (char-equal char (int-char 40))
   (char-equal char (int-char 171)))
)

(defun phox-pbrpm-right-paren-p (char)
"Retrun t if the character is a right parenthesis ')' or a french guillemot '>>'"
   (or
   (char-equal char (int-char 41))
   (char-equal char (int-char 187)))
)


(defun phox-pbrpm-menu-from-string (order str)
  "build a menu from a string send by phox"
  (setq str (proof-shell-invisible-cmd-get-result str))
  (if (string= str "") nil
    (mapcar 
     (lambda (s) (append (list order) (split-string s "\\\\-") 
			      (list 'phox-pbrpm-rename-in-cmd)))
     (split-string str "\\\\\\\\"))))

(defun phox-pbrpm-rename-in-cmd (cmd spans)
  (let ((modified nil) (prev 0))
    (mapc (lambda (span) 
	    (if (not (string=  (span-property span 'original-text) 
			       (span-string span)))
		(setq modified (cons span modified)))) spans)
    (setq modified (reverse modified))
    (if modified
	(progn
	  (if (= 0 (span-property (car modified) 'goalnum))
	      (progn
		(while (and modified (= 0 (span-property (car modified) 'goalnum)))
		  (let ((span (pop modified)))
		    (setq cmd (concat cmd ";; rename " 
				      (span-property span 'original-text) " " 
				      (span-string span)))))
		(if modified (setq cmd (concat "(" cmd ")")))))
	  (if modified (setq cmd (concat cmd ";; ")))
	  (while modified
	    (let* ((span (pop modified))
		   (goalnum (span-property span 'goalnum)))
	      (while (< (+ prev 1) goalnum)
		(setq cmd (concat cmd "idt @+@ "))
		(setq prev (+ prev 1)))
	      (setq cmd (concat cmd "(rename " (span-property span 'original-text) " " 
				      (span-string span)))
	      (setq prev goalnum)
	      (if (or (not modified) (< goalnum (span-property (car modified) 'goalnum)))
		  (setq cmd (concat cmd ") @+@ "))
		(setq cmd (concat cmd ";; ")))))
	  (if (> prev 0) (setq cmd (concat cmd "idt"))))))
  cmd)
	
(defun phox-pbrpm-get-region-name (region-info)
      (if (= (nth 0 region-info) 1) (nth 1 region-info) (nth 2 region-info)))

(defun  phox-pbrpm-escape-string (str)
  "add escape char '\'"
  (concat "\"" (replace-regexp-in-string "\\\\" "\\\\\\\\" str) "\""))

(defun phox-pbrpm-generate-menu (click-infos region-infos)
"Use informations to build a list of (name , rule)"
   ;click-infos = (goal-number, "whole"/hyp-name/"none", expression, depth-tree-list)
   ;region-infos = idem if in the goal buffer, (0, "none", expression, nil ) if in another buffer, do not display if no region available.

   (let
     ((pbrpm-rules-list nil)
      (goal-prefix
       (if (= (nth 0 click-infos) 1) "" 
	 (concat "["
		 (int-to-string (nth 0 click-infos))
		 "] "))))
 

   ; the first goal is the selected one by default, so we prefer not to display it.

   ; if clicking in a conclusion with no selection
     (if (and (string= (nth 1 click-infos) "none") (not region-infos))
	 (setq pbrpm-rules-list
	       (append pbrpm-rules-list
		       (list
			(list 4 (phox-lang-absurd) (concat goal-prefix "by_absurd;; elim False")))
		       (phox-pbrpm-menu-from-string 0
						    (concat "menu_intro "
							    (int-to-string (nth 0 click-infos))))
		       (phox-pbrpm-menu-from-string 0
						    (concat "menu_evaluate "
							    (int-to-string (nth 0 click-infos))))
		       ); end append
	       );end setq
       );end if

   ; if clicking a conclusion with a selection
   (if (and (string= (nth 1 click-infos) "none") region-infos)
       (setq pbrpm-rules-list
         (append pbrpm-rules-list
		 (phox-pbrpm-menu-from-string 0
			 (concat "menu_intro "
				 (int-to-string (nth 0 click-infos))	
				 (let ((tmp ""))
				   (mapc (lambda (region-info)
					   (setq tmp 
						 (concat tmp " " (phox-pbrpm-escape-string (nth 2 region-info)))))
					 region-infos)
				   tmp)))
	 (phox-pbrpm-menu-from-string 1
		       (concat "menu_rewrite "
			       (int-to-string (nth 0 click-infos)) " "
			       (let ((tmp ""))
				 (mapc (lambda (region-info)
					 (setq tmp 
					       (concat tmp " " (phox-pbrpm-escape-string (phox-pbrpm-get-region-name region-info)))))
				       region-infos)
				 tmp))))))

    ; if clicking in an hypothesis with no selection
     (if (and
	  (not (or (string= (nth 1 click-infos) "none")
		   (string= (nth 1 click-infos) "whole")))
	  (or (string= "" (nth 2 click-infos)) (not (char= (string-to-char (nth 2 click-infos)) ??)))
	  (not region-infos))
	 (let ((r (proof-shell-invisible-cmd-get-result (concat "is_hypothesis "
								(int-to-string (nth 0 click-infos))
								" "
								(phox-pbrpm-escape-string (nth 1 click-infos))))))
	   (setq pbrpm-rules-list
		 (append pbrpm-rules-list
			 (if (char= (string-to-char r) ?t)
			     (list
			      (list 9 (phox-lang-suppress (nth 1 click-infos))
				    (concat "[" (int-to-string (nth 0 click-infos)) "] " 
					    "rmh " (nth 1 click-infos))))
			   nil)
			 (phox-pbrpm-menu-from-string 1
						      (concat "menu_elim "
							      (int-to-string (nth 0 click-infos)) " "
							      (nth 1 click-infos)))
			 (phox-pbrpm-menu-from-string 1
						      (concat "menu_evaluate_hyp "
							      (int-to-string (nth 0 click-infos)) " "
							      (nth 1 click-infos)))
			 (phox-pbrpm-menu-from-string 0
						       (concat "menu_left "
							       (int-to-string (nth 0 click-infos))
							       " "
							       (nth 1 click-infos)))))))

   ; if clicking on an hypothesis with a selection
   (if (and         
	(not (or (string= (nth 1 click-infos) "none")
		 (string= (nth 1 click-infos) "whole")))
	region-infos)
       (setq pbrpm-rules-list
         (append pbrpm-rules-list
		 (phox-pbrpm-menu-from-string 1
		       (concat "menu_apply "
			       (int-to-string (nth 0 click-infos)) " "
			       (nth 1 click-infos)
			       (let ((tmp ""))
				 (mapc (lambda (region-info)
					 (setq tmp 
					       (concat tmp " " (phox-pbrpm-escape-string (nth 2 region-info)))))
				       region-infos)
				 tmp)))
		 (phox-pbrpm-menu-from-string 1
		       (concat "menu_rewrite_hyp "
			       (int-to-string (nth 0 click-infos)) " "
			       (nth 1 click-infos)
			       (let ((tmp ""))
				 (mapc (lambda (region-info)
					 (setq tmp 
					       (concat tmp " " (phox-pbrpm-escape-string (phox-pbrpm-get-region-name region-info)))))
				       region-infos)
				 tmp))))))

   (if (and (not (string= "" (nth 2 click-infos))) (char= (string-to-char (nth 2 click-infos)) ??)
	    region-infos (not (cdr region-infos)))
       (setq pbrpm-rules-list
	     (append pbrpm-rules-list
		     (list (list 0 (concat 
				    (phox-lang-instance (nth 2 click-infos))
				    (nth 2 (car region-infos)))
				 (concat 
				  goal-prefix
				  "instance "
				  (nth 2 click-infos) 
				  " "
				  (nth 2 (car region-infos))
				  ". "))))))

   ; is clicking on a token with no selection
   (if (and (not region-infos) (not (string= (nth 2 click-infos) "")))
       (let ((r (proof-shell-invisible-cmd-get-result (concat "is_definition "
							      (int-to-string (nth 0 click-infos))
							      " " 
                                                              (phox-pbrpm-escape-string (nth 2 click-infos)))))
	     (ri (proof-shell-invisible-cmd-get-result (concat "is_definition "
							      (int-to-string (nth 0 click-infos))
							      " "
							      (phox-pbrpm-escape-string (concat "$" (nth 2 click-infos)))))))
	 (if (or (char= (string-to-char r) ?t) (char= (string-to-char ri) ?t))
	     (setq pbrpm-rules-list
		   (append pbrpm-rules-list
			   (list (list 1 (concat 
					(phox-lang-opendef)
					(nth 2 click-infos))
				       (concat 
					goal-prefix
					(if (or (string= (nth 1 click-infos) "none") 
						(string= (nth 1 click-infos) "whole"))
					    "unfold -ortho " 
					  (concat "unfold_hyp " (nth 1 click-infos) " -ortho "))
					"$"
					(nth 2 click-infos)
					". ")))))
	   (if (and (not (string= "" (nth 2 click-infos))) (char= (string-to-char (nth 2 click-infos)) ??))
	       (let  ((r (proof-shell-invisible-cmd-get-result (concat "is_locked "
							      (nth 2 click-infos)))))
		 (if (char= (string-to-char r) ?t)
		     (setq pbrpm-rules-list
			   (append pbrpm-rules-list
				   (list (list 0 (phox-lang-unlock (nth 2 click-infos))
					 (concat 
					  goal-prefix
					  "unlock "
					  (nth 2 click-infos)
					  ". ")))))
		     (setq pbrpm-rules-list
			   (append pbrpm-rules-list
				   (list (list 0 (phox-lang-lock (nth 2 click-infos))
					 (concat 
					  goal-prefix
					  "lock "
					  (nth 2 click-infos)
					  ". ")))))))))))

   (let ((arg (if (and region-infos (not (cdr region-infos))) (nth 2 (car region-infos)) " ")))
     (setq pbrpm-rules-list
	   (append pbrpm-rules-list
		   (list
		    (list 10 "Trivial ?" (concat goal-prefix "trivial"))
		    (list 10 (phox-lang-prove arg) (concat goal-prefix "prove") 
			  (lambda (cmd spans) 
			    (let ((span (pop spans)))
			      (concat cmd " " (span-string span)))))
		    (list 10 (phox-lang-let arg) (concat goal-prefix "local") 
			  (lambda (cmd spans) 
			    (let ((span1 (pop spans)) (span2 (pop spans)))
			      (concat cmd " " (span-string span1) " = "(span-string span2)))))))))

   pbrpm-rules-list
))


;;--------------------------------------------------------------------------;;
;; phox specific functions
;;--------------------------------------------------------------------------;;

(defalias 'proof-pbrpm-generate-menu 'phox-pbrpm-generate-menu)
(defalias 'proof-pbrpm-left-paren-p 'phox-pbrpm-left-paren-p)
(defalias 'proof-pbrpm-right-paren-p 'phox-pbrpm-right-paren-p)

;;--------------------------------------------------------------------------;;

(require 'phox-lang)
(provide 'phox-pbrpm)
;; phox-pbrpm ends here