Source file coqloop.ml
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
open Pp
let print_emacs = ref false
let top_stderr x =
Format.fprintf !Topfmt.err_ft "@[%a@]%!" pp_with x
type input_buffer = {
mutable prompt : Stm.doc -> string;
mutable str : Bytes.t;
mutable len : int;
mutable bols : int list;
mutable tokens : Pcoq.Parsable.t;
mutable start : int }
let resize_buffer ibuf = let open Bytes in
let nstr = create (2 * length ibuf.str + 1) in
blit ibuf.str 0 nstr 0 (length ibuf.str);
ibuf.str <- nstr
let resynch_buffer ibuf =
match ibuf.bols with
| ll::_ ->
let new_len = ibuf.len - ll in
Bytes.blit ibuf.str ll ibuf.str 0 new_len;
ibuf.len <- new_len;
ibuf.bols <- [];
ibuf.start <- ibuf.start + ll
| _ -> ()
let emacs_prompt_startstring () = if !print_emacs then "<prompt>" else ""
let emacs_prompt_endstring () = if !print_emacs then "</prompt>" else ""
let prompt_char doc ic ibuf count =
let bol = match ibuf.bols with
| ll::_ -> Int.equal ibuf.len ll
| [] -> Int.equal ibuf.len 0
in
if bol && not !print_emacs then top_stderr (str (ibuf.prompt doc));
try
let c = input_char ic in
if c == '\n' then ibuf.bols <- (ibuf.len+1) :: ibuf.bols;
if ibuf.len == Bytes.length ibuf.str then resize_buffer ibuf;
Bytes.set ibuf.str ibuf.len c;
ibuf.len <- ibuf.len + 1;
Some c
with End_of_file ->
None
let reset_input_buffer doc ic ibuf =
ibuf.str <- Bytes.empty;
ibuf.len <- 0;
ibuf.bols <- [];
ibuf.tokens <- Pcoq.Parsable.make (Gramlib.Stream.from (prompt_char doc ic ibuf));
ibuf.start <- 0
module TopErr = struct
let get_bols_of_loc ibuf (bp,ep) =
let add_line (b,e) lines =
if b < 0 || e < b then CErrors.anomaly (Pp.str "Bad location.");
match lines with
| ([],None) -> ([], Some (b,e))
| (fl,oe) -> ((b,e)::fl, oe)
in
let rec lines_rec ba after = function
| [] -> add_line (0,ba) after
| ll::_ when ll <= bp -> add_line (ll,ba) after
| ll::fl ->
let nafter = if ll < ep then add_line (ll,ba) after else after in
lines_rec ll nafter fl
in
let (fl,ll) = lines_rec ibuf.len ([],None) ibuf.bols in
(fl,Option.get ll)
let dotted_location (b,e) =
if e-b < 3 then
("", String.make (e-b) ' ')
else
(String.make (e-b-1) '.', " ")
let blanch_utf8_string s bp ep = let open Bytes in
let s' = make (ep-bp) ' ' in
let j = ref 0 in
for i = bp to ep - 1 do
let n = Char.code (get s i) in
if get s i == '\t' then set s' !j '\t';
if n < 0x80 || 0xC0 <= n then incr j
done;
Bytes.sub_string s' 0 !j
let adjust_loc_buf ib loc = let open Loc in
{ loc with ep = loc.ep - ib.start; bp = loc.bp - ib.start }
let print_highlight_location ib loc =
let (bp,ep) = Loc.unloc loc in
let highlight_lines =
match get_bols_of_loc ib (bp,ep) with
| ([],(bl,el)) ->
let shift = blanch_utf8_string ib.str bl bp in
let span = String.length (blanch_utf8_string ib.str bp ep) in
(str"> " ++ str(Bytes.sub_string ib.str bl (el-bl-1)) ++ fnl () ++
str"> " ++ str(shift) ++ str(String.make span '^'))
| ((b1,e1)::ml,(bn,en)) ->
let (d1,s1) = dotted_location (b1,bp) in
let (dn,sn) = dotted_location (ep,en) in
let l1 = (str"> " ++ str d1 ++ str s1 ++
str(Bytes.sub_string ib.str bp (e1-bp))) in
let li =
prlist (fun (bi,ei) ->
(str"> " ++ str(Bytes.sub_string ib.str bi (ei-bi)))) ml in
let ln = (str"> " ++ str(Bytes.sub_string ib.str bn (ep-bn)) ++
str sn ++ str dn) in
(l1 ++ li ++ ln)
in
highlight_lines
let valid_buffer_loc ib loc =
let (b,e) = Loc.unloc loc in b-ib.start >= 0 && e-ib.start < ib.len && b<=e
let error_info_for_buffer ?loc buf =
match loc with
| None -> Topfmt.pr_phase ?loc ()
| Some loc ->
let fname = loc.Loc.fname in
match fname with
| Loc.ToplevelInput ->
let nloc = adjust_loc_buf buf loc in
if valid_buffer_loc buf loc then
match Topfmt.pr_phase ~loc:nloc () with
| None -> None
| Some hd -> Some (hd ++ fnl () ++ print_highlight_location buf nloc)
else Topfmt.pr_phase ~loc ()
| Loc.InFile _ -> Topfmt.pr_phase ~loc ()
let print_error_for_buffer ?loc lvl msg buf =
let pre_hdr = error_info_for_buffer ?loc buf in
if !print_emacs
then Topfmt.emacs_logger ?pre_hdr lvl msg
else Topfmt.std_logger ?pre_hdr lvl msg
end
let make_prompt () =
try
(Names.Id.to_string (Vernacstate.Declare.get_current_proof_name ())) ^ " < "
with Vernacstate.Declare.NoCurrentProof ->
"Coq < "
[@@ocaml.warning "-3"]
let make_emacs_prompt doc =
let statnum = Stateid.to_string (Stm.get_current_state ~doc) in
let dpth = Stm.current_proof_depth ~doc in
let pending = Stm.get_all_proof_names ~doc in
let pendingprompt =
List.fold_left
(fun acc x -> acc ^ (if CString.is_empty acc then "" else "|") ^ Names.Id.to_string x)
"" pending in
let proof_info = if dpth >= 0 then string_of_int dpth else "0" in
if !print_emacs then statnum ^ " |" ^ pendingprompt ^ "| " ^ proof_info ^ " < "
else ""
let top_buffer =
let pr doc =
emacs_prompt_startstring()
^ make_prompt()
^ make_emacs_prompt doc
^ emacs_prompt_endstring()
in
{ prompt = pr;
str = Bytes.empty;
len = 0;
bols = [];
tokens = Pcoq.Parsable.make (Gramlib.Stream.empty ());
start = 0 }
let set_prompt prompt =
top_buffer.prompt
<- (fun doc ->
emacs_prompt_startstring()
^ prompt ()
^ emacs_prompt_endstring())
let parse_to_dot =
let rec dot kwstate st = match Gramlib.LStream.next kwstate st with
| Tok.KEYWORD ("."|"...") -> ()
| Tok.EOI -> ()
| _ -> dot kwstate st
in
Pcoq.Entry.(of_parser "Coqtoplevel.dot" { parser_fun = dot })
let rec discard_to_dot () =
try
Pcoq.Entry.parse parse_to_dot top_buffer.tokens
with
| CLexer.Error.E _ -> discard_to_dot ()
| e when CErrors.noncritical e -> ()
let read_sentence ~state input =
let open Vernac.State in
try Stm.parse_sentence ~doc:state.doc state.sid ~entry:G_toplevel.vernac_toplevel input
with reraise ->
let reraise = Exninfo.capture reraise in
(match fst reraise with
| Sys.Break -> Pp.pp_with !Topfmt.err_ft (Pp.fnl ())
| _ ->
try discard_to_dot ()
with Sys.Break ->
Pp.pp_with !Topfmt.err_ft (Pp.fnl ());
raise Sys.Break);
Exninfo.iraise reraise
let loc doc_id sid : Loc.t option =
match loc with
| Some _ -> loc
| None ->
try
let doc = Stm.get_doc doc_id in
Option.cata (fun {CAst.loc} -> loc) None Stm.(get_ast ~doc sid)
with _ -> loc
(** Coqloop Console feedback handler *)
let coqloop_feed (fb : Feedback.feedback) = let open Feedback in
match fb.contents with
| Processed -> ()
| Incomplete -> ()
| Complete -> ()
| ProcessingIn _ -> ()
| InProgress _ -> ()
| WorkerStatus (_,_) -> ()
| AddedAxiom -> ()
| GlobRef (_,_,_,_,_) -> ()
| GlobDef (_,_,_,_) -> ()
| FileDependency (_,_) -> ()
| FileLoaded (_,_) -> ()
| Custom (_,_,_) -> ()
| Message (Error,loc,msg) -> ()
| Message (Warning,loc,msg) ->
let loc = extract_default_loc loc fb.doc_id fb.span_id in
TopErr.print_error_for_buffer ?loc Warning msg top_buffer
| Message (lvl,loc,msg) ->
TopErr.print_error_for_buffer ?loc lvl msg top_buffer
(** Main coq loop : read vernacular expressions until Drop is entered.
Ctrl-C is handled internally as Sys.Break instead of aborting Coq.
Normally, the only exceptions that can come out of [do_vernac] and
exit the loop are Drop and Quit. Any other exception there indicates
an issue with [print_toplevel_error] above. *)
let loop_flush_all () =
flush stderr;
flush stdout;
Format.pp_print_flush !Topfmt.std_ft ();
Format.pp_print_flush !Topfmt.err_ft ()
let pequal cmp1 cmp2 (a1,a2) (b1,b2) = cmp1 a1 b1 && cmp2 a2 b2
let evleq e1 e2 = CList.equal Evar.equal e1 e2
let cproof p1 p2 =
let Proof.{goals=a1;stack=a2;sigma=sigma1} = Proof.data p1 in
let Proof.{goals=b1;stack=b2;sigma=sigma2} = Proof.data p2 in
evleq a1 b1 &&
CList.equal (pequal evleq evleq) a2 b2 &&
CList.equal Evar.equal (Evd.shelf sigma1) (Evd.shelf sigma2) &&
Evar.Set.equal (Evd.given_up sigma1) (Evd.given_up sigma2)
let drop_last_doc = ref None
let print_anyway_opts = [
[ "Diffs" ];
]
let print_anyway c =
let open Vernacexpr in
match c.expr with
| VernacSynterp (VernacSetOption (_, opt, _)) -> List.mem opt print_anyway_opts
| _ -> false
let print_and_diff oldp proof =
let output =
if Proof_diffs.show_diffs () then
try Printer.pr_open_subgoals ~diffs:oldp proof
with Pp_diff.Diff_Failure msg -> begin
Feedback.msg_warning Pp.(str ("Diff failure: " ^ msg) ++ cut()
++ str "Showing results without diff highlighting" );
Printer.pr_open_subgoals proof
end
else
Printer.pr_open_subgoals proof
in
Feedback.msg_notice output
let top_goal_print ~doc c oldp newp =
try
let proof_changed = not (Option.equal cproof oldp (Some newp)) in
let print_goals = proof_changed && Vernacstate.Declare.there_are_pending_proofs () ||
print_anyway c in
if not !Flags.quiet && print_goals then begin
let dproof = Stm.get_prev_proof ~doc (Stm.get_current_state ~doc) in
print_and_diff dproof newp
end
with
| exn ->
let (e, info) = Exninfo.capture exn in
let loc = Loc.get_loc info in
let msg = CErrors.iprint (e, info) in
TopErr.print_error_for_buffer ?loc Feedback.Error msg top_buffer
[@@ocaml.warning "-3"]
let { Goptions.get = exit_on_error } =
let open Goptions in
declare_bool_option_and_ref
~key:["Coqtop";"Exit";"On";"Error"]
~value:false
()
let show_proof_diff_cmd ~state diff_opt =
let open Vernac.State in
match state.proof with
| None -> CErrors.user_err (str "No proofs to diff.")
| Some proof ->
let old = Stm.get_prev_proof ~doc:state.doc state.sid in
Proof_diffs.diff_proofs ~diff_opt ?old proof
let process_toplevel_command ~state stm =
let open Vernac.State in
let open G_toplevel in
match stm with
| VernacDrop ->
state
| VernacBackTo bid ->
let bid = Stateid.of_int bid in
let doc, res = Stm.edit_at ~doc:state.doc bid in
assert (res = Stm.NewTip);
{ state with doc; sid = bid }
| VernacQuit ->
exit 0
| VernacControl { CAst.loc; v=c } ->
let nstate = Vernac.process_expr ~state (CAst.make ?loc c) in
let () = match nstate.proof with
| None -> ()
| Some proof -> top_goal_print ~doc:state.doc c state.proof proof
in
nstate
| VernacShowGoal { gid; sid } ->
let proof = Stm.get_proof ~doc:state.doc (Stateid.of_int sid) in
let goal = Printer.pr_goal_emacs ~proof gid sid in
let () = Feedback.msg_notice goal in
state
| VernacShowProofDiffs diff_opt ->
if not (Proof_diffs.color_enabled ()) then
CErrors.user_err Pp.(str "Show Proof Diffs requires setting the \"-color\" command line argument to \"on\" or \"auto\".")
else
let out = show_proof_diff_cmd ~state diff_opt in
Feedback.msg_notice out;
state
let read_and_execute_base ~state =
let input = top_buffer.tokens in
match read_sentence ~state input with
| Some G_toplevel.VernacDrop ->
if Mltop.is_ocaml_top()
then (drop_last_doc := Some state; state, true)
else (Feedback.msg_warning (str "There is no ML toplevel."); state, false)
| Some stm ->
process_toplevel_command ~state stm, false
| None ->
top_stderr (fnl ()); exit 0
let read_and_execute ~state =
try read_and_execute_base ~state
with
| Sys_blocked_io ->
let msg =
Pp.(strbrk "Coqtop needs the standard input to be in blocking mode." ++ spc()
++ str "One way of clearing the non-blocking flag is through Python:" ++ fnl()
++ str " import os" ++ fnl()
++ str " os.set_blocking(0, True)")
in
TopErr.print_error_for_buffer Feedback.Error msg top_buffer;
exit 1
| any ->
let (e, info) = Exninfo.capture any in
let loc = Loc.get_loc info in
let msg = CErrors.iprint (e, info) in
TopErr.print_error_for_buffer ?loc Feedback.Error msg top_buffer;
if exit_on_error () then exit 1;
state, false
let rec vernac_loop ~state =
let open Vernac.State in
loop_flush_all ();
top_stderr (fnl());
if !print_emacs then top_stderr (str (top_buffer.prompt state.doc));
resynch_buffer top_buffer;
let state, drop = read_and_execute ~state in
if drop then state else (vernac_loop [@ocaml.tailcall]) ~state
let drop_args = ref None
let init_ocaml_path () =
let env = Boot.Env.init () in
let corelib = Boot.Env.corelib env |> Boot.Path.to_string in
let add_subdir dl = Mltop.add_ml_dir (Filename.concat corelib dl) in
List.iter add_subdir ("dev" :: Coq_config.all_src_dirs)
let loop ~opts ~state =
drop_args := Some opts;
let open Coqargs in
print_emacs := opts.config.print_emacs;
let tl_feed = Feedback.add_feeder coqloop_feed in
Sys.catch_break true;
reset_input_buffer state.Vernac.State.doc stdin top_buffer;
let _ : Vernac.State.t = vernac_loop ~state in
init_ocaml_path ();
Mltop.ocaml_toploop();
Feedback.del_feeder tl_feed