-
Notifications
You must be signed in to change notification settings - Fork 0
/
stm.ml
2837 lines (2490 loc) · 104 KB
/
stm.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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(************************************************************************)
(* * The Coq Proof Assistant / The Coq Development Team *)
(* v * Copyright INRIA, CNRS and contributors *)
(* <O___,, * (see version control and CREDITS file for authors & dates) *)
(* \VV/ **************************************************************)
(* // * This file is distributed under the terms of the *)
(* * GNU Lesser General Public License Version 2.1 *)
(* * (see LICENSE file for the text of the license) *)
(************************************************************************)
(* enable in case of stm problems *)
(* let stm_debug () = CDebug.(get_flag misc) *)
let stm_debug = ref false
let stm_pr_err s = Format.eprintf "%s] %s\n%!" (Spawned.process_id ()) s
let stm_pp_err pp = Format.eprintf "%s] @[%a@]\n%!" (Spawned.process_id ()) Pp.pp_with pp
let stm_prerr_endline s = if !stm_debug then begin stm_pr_err (s ()) end else ()
let stm_pperr_endline s = if !stm_debug then begin stm_pp_err (s ()) end else ()
let stm_prerr_debug s = if CDebug.(get_flag misc) then begin stm_pr_err (s ()) end else ()
open Pp
open CErrors
open Names
open Feedback
open Vernacexpr
open Vernacextend
module PG_compat = Vernacstate.Declare [@@ocaml.warning "-3"]
let is_vtkeep = function VtKeep _ -> true | _ -> false
let get_vtkeep = function VtKeep x -> x | _ -> assert false
module AsyncOpts = struct
type cache = Force
type async_proofs = APoff | APonLazy | APon
type tac_error_filter = FNone | FOnly of string list | FAll
type stm_opt = {
async_proofs_n_workers : int;
async_proofs_n_tacworkers : int;
async_proofs_cache : cache option;
async_proofs_mode : async_proofs;
async_proofs_private_flags : string option;
async_proofs_never_reopen_branch : bool;
async_proofs_tac_error_resilience : tac_error_filter;
async_proofs_cmd_error_resilience : bool;
async_proofs_delegation_threshold : float;
async_proofs_worker_priority : CoqworkmgrApi.priority;
}
let default_opts = {
async_proofs_n_workers = 1;
async_proofs_n_tacworkers = 2;
async_proofs_cache = None;
async_proofs_mode = APoff;
async_proofs_private_flags = None;
async_proofs_never_reopen_branch = false;
async_proofs_tac_error_resilience = FOnly [ "curly" ];
async_proofs_cmd_error_resilience = true;
async_proofs_delegation_threshold = 0.03;
async_proofs_worker_priority = CoqworkmgrApi.Low;
}
let cur_opt : stm_opt option ref = ref None
let set_cur_opt o = assert (Option.is_empty !cur_opt); cur_opt := Some o
let cur_opt () = match !cur_opt with
| Some o -> o
| None ->
anomaly Pp.(str "Incorrect stm init: accessing options before they are set.")
end
open AsyncOpts
let async_proofs_is_master opt =
opt.async_proofs_mode = APon &&
!Flags.async_proofs_worker_id = "master"
let execution_error ?loc state_id msg =
feedback ~id:state_id (Message (Error, loc, [], msg))
module Hooks = struct
let state_computed = ref (fun ~doc:_ state_id ~in_cache ->
feedback ~id:state_id Processed)
let state_ready = ref (fun ~doc:_ state_id -> ())
let forward_feedback =
let m = Mutex.create () in
ref (function
| { doc_id = did; span_id = id; route; contents } ->
CThread.with_lock m ~scope:(fun () -> feedback ~did ~id ~route contents))
let unreachable_state = ref (fun ~doc:_ _ _ -> ())
let document_add = ref (fun _ _ -> ())
let document_edit = ref (fun _ -> ())
let sentence_exec = ref (fun _ -> ())
end
let async_proofs_workers_extra_env = ref [||]
type aast = {
verbose : bool;
indentation : int;
strlen : int;
mutable expr : vernac_control; (* mutable: Proof using hinted by aux file *)
}
let pr_ast { expr; indentation } = Pp.(int indentation ++ str " " ++ Ppvernac.pr_vernac expr)
(* Commands piercing opaque (probably should be using the vernactypes system instead) *)
let may_pierce_opaque = function
| VernacSynPure (VernacPrint _) -> true
| VernacSynterp (VernacExtend ({ ext_plugin = "coq-core.plugins.extraction" }, _)) -> true
| _ -> false
type depth = int
type 'branch branch_type_gen =
| Master
| Proof of depth
| Edit of
Stateid.t * Stateid.t * Vernacextend.vernac_qed_type * 'branch
module Kind =
struct
type 'a t = 'a branch_type_gen
let master = Master
end
module Vcs_ = Vcs.Make(Stateid.Self)(Kind)
type future_proof = Declare.Proof.closed_proof_output option Future.computation
type branch_type = Vcs_.Branch.t Kind.t
(* TODO 8.7 : split commands and tactics, since this type is too messy now *)
type cqueue = MainQueue | SkipQueue
type cmd_t = {
ctac : bool; (* is a tactic *)
ceff : bool; (* is a side-effecting command in the middle of a proof *)
cast : aast;
cids : Names.Id.t list;
cblock : proof_block_name option;
cqueue : cqueue;
cancel_switch : AsyncTaskQueue.cancel_switch; }
type fork_t = aast * Vcs_.Branch.t * opacity_guarantee * Names.Id.t list
type qed_t = {
qast : aast;
keep : vernac_qed_type;
mutable fproof : (future_proof option * AsyncTaskQueue.cancel_switch) option;
brname : Vcs_.Branch.t;
brinfo : Vcs_.branch_info
}
type seff_t = ReplayCommand of aast | CherryPickEnv
type alias_t = Stateid.t * aast
type transaction =
| Cmd of cmd_t
| Fork of fork_t
| Qed of qed_t
| Sideff of seff_t
| Alias of alias_t
| Noop
type step =
| SCmd of cmd_t
| SFork of fork_t * Stateid.t option
| SQed of qed_t * Stateid.t
| SSideff of seff_t * Stateid.t
| SAlias of alias_t
type visit = { step : step; next : Stateid.t }
let mkTransTac cast cblock cqueue =
Cmd { ctac = true; cast; cblock; cqueue; cids = []; ceff = false; cancel_switch = ref false }
let mkTransCmd cast cids ceff cqueue =
Cmd { ctac = false; cast; cblock = None; cqueue; cids; ceff; cancel_switch = ref false }
type cached_state =
| EmptyState
| ParsingState of Pcoq.frozen_t
| FullState of Vernacstate.t
| ErrorState of Pcoq.frozen_t option * Exninfo.iexn
type branch = Vcs_.Branch.t * Vcs_.branch_info
type backup = { mine : branch; others : branch list }
module DynBlockData : Dyn.S = Dyn.Make ()
(* Clusters of nodes implemented as Dag properties. While Dag and Vcs impose
* no constraint on properties, here we impose boxes to be non overlapping.
* Such invariant makes sense for the current kinds of boxes (proof blocks and
* entire proofs) but may make no sense and dropped/refined in the future.
* Such invariant is useful to detect broken proof block detection code *)
type box =
| ProofTask of pt
| ProofBlock of static_block_declaration * proof_block_name
and pt = { (* TODO: inline records in OCaml 4.03 *)
lemma : Stateid.t;
qed : Stateid.t;
}
and static_block_declaration = {
block_start : Stateid.t;
block_stop : Stateid.t;
dynamic_switch : Stateid.t;
carry_on_data : DynBlockData.t;
}
(* Functions that work on a Vcs with a specific branch type *)
module Vcs_aux : sig
val proof_nesting : ('t,'i,'c) Vcs_.t -> int
val find_proof_at_depth :
('t, 'i,'c) Vcs_.t -> int ->
Vcs_.Branch.t * Vcs_.branch_info
exception Expired
val visit : (transaction,'i,'c) Vcs_.t -> Vcs_.Dag.node -> visit
end = struct (* {{{ *)
let proof_nesting vcs =
List.fold_left max 0
(CList.map_filter
(function
| { Vcs_.kind = Proof n } -> Some n
| { Vcs_.kind = Edit _ } -> Some 1
| _ -> None)
(List.map (Vcs_.get_branch vcs) (Vcs_.branches vcs)))
let find_proof_at_depth vcs pl =
try List.find (function
| _, { Vcs_.kind = Proof n } -> Int.equal n pl
| _, { Vcs_.kind = Edit _ } -> anomaly(Pp.str "find_proof_at_depth")
| _ -> false)
(List.map (fun h -> h, Vcs_.get_branch vcs h) (Vcs_.branches vcs))
with Not_found -> failwith "find_proof_at_depth"
exception Expired
let visit vcs id =
if Stateid.equal id Stateid.initial then
anomaly(Pp.str "Visiting the initial state id.")
else if Stateid.equal id Stateid.dummy then
anomaly(Pp.str "Visiting the dummy state id.")
else
try
match Vcs_.Dag.from_node (Vcs_.dag vcs) id with
| [n, Cmd x] -> { step = SCmd x; next = n }
| [n, Alias x] -> { step = SAlias x; next = n }
| [n, Fork x] -> { step = SFork (x,None); next = n }
| [n, Fork x; p, Noop] -> { step = SFork (x,Some p); next = n }
| [p, Noop; n, Fork x] -> { step = SFork (x,Some p); next = n }
| [n, Qed x; p, Noop]
| [p, Noop; n, Qed x] -> { step = SQed (x,p); next = n }
| [n, Sideff CherryPickEnv; p, Noop]
| [p, Noop; n, Sideff CherryPickEnv]-> { step = SSideff (CherryPickEnv, p); next = n }
| [n, Sideff (ReplayCommand x); p, Noop]
| [p, Noop; n, Sideff (ReplayCommand x)]-> { step = SSideff(ReplayCommand x,p); next = n }
| [n, Sideff (ReplayCommand x)]-> {step = SSideff(ReplayCommand x, Stateid.dummy); next=n}
| _ -> anomaly (Pp.str ("Malformed VCS at node "^Stateid.to_string id^"."))
with Not_found -> raise Expired
end (* }}} *)
(*************************** THE DOCUMENT *************************************)
(******************************************************************************)
(* The main document type associated to a VCS *)
type stm_doc_type =
| VoDoc of string
| VosDoc of string
| Interactive of Coqargs.top
(* Dummy until we land the functional interp patch + fixed start_library *)
type doc = int
let dummy_doc : doc = 0
(* Imperative wrap around VCS to obtain _the_ VCS that is the
* representation of the document Coq is currently processing *)
module VCS : sig
exception Expired
module Branch : (module type of Vcs_.Branch with type t = Vcs_.Branch.t)
type id = Stateid.t
type branch_info = Vcs_.branch_info = {
kind : branch_type;
root : id;
pos : id;
}
type vcs = (transaction, state_info, box) Vcs_.t
and state_info = { (* TODO: Make this record private to VCS *)
mutable n_reached : int; (* debug cache: how many times was computed *)
mutable n_goals : int; (* open goals: indentation *)
mutable state : cached_state; (* state value *)
mutable proof_mode : Pvernac.proof_mode option;
mutable vcs_backup : vcs option * backup option;
}
val init : stm_doc_type -> id -> Pcoq.frozen_t -> doc
(* val get_type : unit -> stm_doc_type *)
val set_ldir : Names.DirPath.t -> unit
val get_ldir : unit -> Names.DirPath.t
val is_interactive : unit -> bool
val is_vos_doc : unit -> bool
val current_branch : unit -> Branch.t
val checkout : Branch.t -> unit
val branches : unit -> Branch.t list
val get_branch : Branch.t -> branch_info
val get_branch_pos : Branch.t -> id
val new_node : ?id:Stateid.t -> Pvernac.proof_mode option -> unit -> id
val merge : id -> ours:transaction -> ?into:Branch.t -> Branch.t -> unit
val rewrite_merge : id -> ours:transaction -> at:id -> Branch.t -> unit
val delete_branch : Branch.t -> unit
val commit : id -> transaction -> unit
val mk_branch_name : aast -> Branch.t
val edit_branch : Branch.t
val branch : ?root:id -> ?pos:id -> Branch.t -> branch_type -> unit
val reset_branch : Branch.t -> id -> unit
val reachable : id -> Stateid.Set.t
val cur_tip : unit -> id
val get_info : id -> state_info
val reached : id -> unit
val goals : id -> int -> unit
val set_state : id -> cached_state -> unit
val get_state : id -> cached_state
val set_parsing_state : id -> Pcoq.frozen_t -> unit
val get_parsing_state : id -> Pcoq.frozen_t option
val get_proof_mode : id -> Pvernac.proof_mode option
(* cuts from start -> stop, raising Expired if some nodes are not there *)
val slice : block_start:id -> block_stop:id -> vcs
val nodes_in_slice : block_start:id -> block_stop:id -> Stateid.t list
val create_proof_task_box : id list -> qed:id -> block_start:id -> unit
val create_proof_block : static_block_declaration -> string -> unit
val box_of : id -> box list
val delete_boxes_of : id -> unit
val proof_task_box_of : id -> pt option
val proof_nesting : unit -> int
val checkout_shallowest_proof_branch : unit -> unit
val propagate_sideff : action:seff_t -> Stateid.t list
val propagate_qed : unit -> unit
val gc : unit -> unit
val visit : id -> visit
val print : ?now:bool -> unit -> unit
val backup : unit -> vcs
val restore : vcs -> unit
end = struct (* {{{ *)
include Vcs_
exception Expired = Vcs_aux.Expired
open Printf
type vcs = (transaction, state_info, box) t
and state_info = { (* TODO: Make this record private to VCS *)
mutable n_reached : int; (* debug cache: how many times was computed *)
mutable n_goals : int; (* open goals: indentation *)
mutable state : cached_state; (* state value *)
mutable proof_mode : Pvernac.proof_mode option;
mutable vcs_backup : vcs option * backup option;
}
let default_info proof_mode =
{
n_reached = 0; n_goals = 0;
state = EmptyState;
proof_mode;
vcs_backup = (None,None);
}
let print_dag vcs () =
(* Due to threading, be wary that this will be called from the
toplevel with we_are_parsing set to true, as indeed, the
toplevel is waiting for input . What a race! XD
In case you are hitting the race enable stm_debug.
*)
if !stm_debug then Flags.in_synterp_phase := false;
let fname =
"stm_" ^ Str.global_replace (Str.regexp " ") "_" (Spawned.process_id ()) in
let string_of_transaction = function
| Cmd { cast = t } | Fork (t, _,_,_) ->
(try Pp.string_of_ppcmds (pr_ast t) with e when CErrors.noncritical e -> "ERR")
| Sideff (ReplayCommand t) ->
sprintf "Sideff(%s)"
(try Pp.string_of_ppcmds (pr_ast t) with e when CErrors.noncritical e -> "ERR")
| Sideff CherryPickEnv -> "EnvChange"
| Noop -> " "
| Alias (id,_) -> sprintf "Alias(%s)" (Stateid.to_string id)
| Qed { qast } -> Pp.string_of_ppcmds (pr_ast qast) in
let is_green id =
match get_info vcs id with
| Some { state = FullState _ } -> true
| _ -> false in
let is_red id =
match get_info vcs id with
| Some { state = ErrorState _ } -> true
| _ -> false in
let head = current_branch vcs in
let heads =
List.map (fun x -> x, (get_branch vcs x).pos) (branches vcs) in
let graph = dag vcs in
let quote s =
Str.global_replace (Str.regexp "\n") "<BR/>"
(Str.global_replace (Str.regexp "<") "<"
(Str.global_replace (Str.regexp ">") ">"
(Str.global_replace (Str.regexp "\"") """
(Str.global_replace (Str.regexp "&") "&"
(String.sub s 0 (min (String.length s) 20)))))) in
let fname_dot, fname_ps =
let f = "/tmp/" ^ Filename.basename fname in
f ^ ".dot", f ^ ".pdf" in
let node id = "s" ^ Stateid.to_string id in
let edge tr =
sprintf "<<FONT POINT-SIZE=\"12\" FACE=\"sans\">%s</FONT>>"
(quote (string_of_transaction tr)) in
let node_info id =
match get_info vcs id with
| None -> ""
| Some info ->
sprintf "<<FONT POINT-SIZE=\"12\">%s</FONT>" (Stateid.to_string id) ^
sprintf " <FONT POINT-SIZE=\"11\">r:%d g:%d</FONT>>"
info.n_reached info.n_goals in
let color id =
if is_red id then "red" else if is_green id then "green" else "white" in
let nodefmt oc id =
fprintf oc "%s [label=%s,style=filled,fillcolor=%s];\n"
(node id) (node_info id) (color id) in
let ids = ref Stateid.Set.empty in
let boxes = ref [] in
(* Fill in *)
Dag.iter graph (fun from _ _ l ->
ids := Stateid.Set.add from !ids;
List.iter (fun box -> boxes := box :: !boxes)
(Dag.property_of graph from);
List.iter (fun (dest, _) ->
ids := Stateid.Set.add dest !ids;
List.iter (fun box -> boxes := box :: !boxes)
(Dag.property_of graph dest))
l);
boxes := CList.sort_uniquize Dag.Property.compare !boxes;
let oc = open_out fname_dot in
output_string oc "digraph states {\n";
Dag.iter graph (fun from cf _ l ->
List.iter (fun (dest, trans) ->
fprintf oc "%s -> %s [xlabel=%s,labelfloat=true];\n"
(node from) (node dest) (edge trans)) l
);
let contains b1 b2 =
Stateid.Set.subset
(Dag.Property.having_it b2) (Dag.Property.having_it b1) in
let same_box = Dag.Property.equal in
let outerboxes boxes =
List.filter (fun b ->
not (List.exists (fun b1 ->
not (same_box b1 b) && contains b1 b) boxes)
) boxes in
let rec rec_print b =
boxes := CList.remove same_box b !boxes;
let sub_boxes = List.filter (contains b) (outerboxes !boxes) in
fprintf oc "subgraph cluster_%s {\n" (Dag.Property.to_string b);
List.iter rec_print sub_boxes;
Stateid.Set.iter (fun id ->
if Stateid.Set.mem id !ids then begin
ids := Stateid.Set.remove id !ids;
nodefmt oc id
end)
(Dag.Property.having_it b);
match Dag.Property.data b with
| ProofBlock ({ dynamic_switch = id }, lbl) ->
fprintf oc "label=\"%s (test:%s)\";\n" lbl (Stateid.to_string id);
fprintf oc "color=red; }\n"
| ProofTask _ -> fprintf oc "color=blue; }\n"
in
List.iter rec_print (outerboxes !boxes);
Stateid.Set.iter (nodefmt oc) !ids;
List.iteri (fun i (b,id) ->
let shape = if Branch.equal head b then "box3d" else "box" in
fprintf oc "b%d -> %s;\n" i (node id);
fprintf oc "b%d [shape=%s,label=\"%s\"];\n" i shape
(Branch.to_string b);
) heads;
output_string oc "}\n";
close_out oc;
ignore(Sys.command
("dot -Tpdf -Gcharset=latin1 " ^ fname_dot ^ " -o" ^ fname_ps))
let vcs : vcs ref = ref (empty Stateid.dummy)
let doc_type = ref (Interactive (Coqargs.TopLogical (Names.DirPath.make [])))
let ldir = ref Names.DirPath.empty
let init dt id ps =
doc_type := dt;
vcs := empty id;
let info = { (default_info None) with state = ParsingState ps } in
vcs := set_info !vcs id info;
dummy_doc
let set_ldir ld =
ldir := ld
let get_ldir () = !ldir
(* let get_type () = !doc_type *)
let is_interactive () =
match !doc_type with
| Interactive _ -> true
| _ -> false
let is_vos_doc () =
match !doc_type with
| VosDoc _ -> true
| _ -> false
let current_branch () = current_branch !vcs
let checkout head = vcs := checkout !vcs head
let branches () = branches !vcs
let get_branch head = get_branch !vcs head
let get_branch_pos head = (get_branch head).pos
let new_node ?(id=Stateid.fresh ()) proof_mode () =
assert(Vcs_.get_info !vcs id = None);
vcs := set_info !vcs id (default_info proof_mode);
id
let merge id ~ours ?into branch =
vcs := merge !vcs id ~ours ~theirs:Noop ?into branch
let delete_branch branch = vcs := delete_branch !vcs branch
let reset_branch branch id = vcs := reset_branch !vcs branch id
let commit id t = vcs := commit !vcs id t
let rewrite_merge id ~ours ~at branch =
vcs := rewrite_merge !vcs id ~ours ~theirs:Noop ~at branch
let reachable id = reachable !vcs id
let mk_branch_name { expr = x } = Branch.make
(match x.CAst.v.Vernacexpr.expr with
| VernacSynPure (VernacDefinition (_,({CAst.v=Name i},_),_)) -> Id.to_string i
| VernacSynPure (VernacStartTheoremProof (_,[({CAst.v=i},_),_])) -> Id.to_string i
| VernacSynPure (VernacInstance (({CAst.v=Name i},_),_,_,_,_)) -> Id.to_string i
| _ -> "branch")
let edit_branch = Branch.make "edit"
let branch ?root ?pos name kind = vcs := branch !vcs ?root ?pos name kind
let get_info id =
match get_info !vcs id with
| Some x -> x
| None -> raise Vcs_aux.Expired
let set_state id s =
let info = get_info id in
info.state <- s;
let is_full_state_valid = match s with
| FullState _ -> true
| EmptyState | ErrorState _ | ParsingState _ -> false
in
if async_proofs_is_master (cur_opt()) && is_full_state_valid then
!Hooks.state_ready ~doc:dummy_doc (* XXX should be taken in input *) id
let get_state id = (get_info id).state
let get_parsing_state id =
stm_pperr_endline (fun () -> str "retrieve parsing state state " ++ str (Stateid.to_string id) ++ str " }}}");
match (get_info id).state with
| FullState s -> Some Vernacstate.(Synterp.parsing s.synterp)
| ParsingState s -> Some s
| ErrorState (s,_) -> s
| EmptyState -> None
let set_parsing_state id ps =
let info = get_info id in
let new_state =
match info.state with
| FullState s -> assert false
| ParsingState s -> assert false
| ErrorState _ -> assert false
| EmptyState -> ParsingState ps
in
info.state <- new_state
let get_proof_mode id = (get_info id).proof_mode
let reached id =
let info = get_info id in
info.n_reached <- info.n_reached + 1
let goals id n = (get_info id).n_goals <- n
let cur_tip () = get_branch_pos (current_branch ())
let proof_nesting () = Vcs_aux.proof_nesting !vcs
let checkout_shallowest_proof_branch () =
if List.mem edit_branch (Vcs_.branches !vcs) then begin
checkout edit_branch
end else
let pl = proof_nesting () in
try
let branch = fst @@ Vcs_aux.find_proof_at_depth !vcs pl in
checkout branch
with Failure _ ->
checkout Branch.master
(* copies the transaction on every open branch *)
let propagate_sideff ~action =
List.map (fun b ->
checkout b;
let proof_mode = get_proof_mode @@ get_branch_pos b in
let id = new_node proof_mode () in
merge id ~ours:(Sideff action) ~into:b Branch.master;
id)
(List.filter (fun b -> not (Branch.equal b Branch.master)) (branches ()))
let propagate_qed () =
List.iter (fun b ->
checkout b;
let proof_mode = get_proof_mode @@ get_branch_pos b in
let id = new_node proof_mode () in
let parsing = Option.get @@ get_parsing_state (get_branch_pos b) in
merge id ~ours:(Sideff CherryPickEnv) ~into:b Branch.master;
set_parsing_state id parsing)
(List.filter (fun b -> not (Branch.equal b Branch.master)) (branches ()))
let visit id = Vcs_aux.visit !vcs id
let nodes_in_slice ~block_start ~block_stop =
let rec aux id =
if Stateid.equal id block_start then [] else
match visit id with
| { next = n; step = SCmd x } -> (id,Cmd x) :: aux n
| { next = n; step = SAlias x } -> (id,Alias x) :: aux n
| { next = n; step = SSideff (ReplayCommand x,_) } ->
(id,Sideff (ReplayCommand x)) :: aux n
| _ -> anomaly Pp.(str("Cannot slice from "^ Stateid.to_string block_start ^
" to "^Stateid.to_string block_stop^"."))
in aux block_stop
(* [slice] copies a slice of the DAG, keeping only the last known valid state.
When it copies a state, it drops the libobjects and keeps only the structure. *)
let slice ~block_start ~block_stop =
let l = nodes_in_slice ~block_start ~block_stop in
let copy_info v id =
let info = get_info id in
Vcs_.set_info v id
{ info with state = EmptyState;
vcs_backup = None,None } in
let make_shallow = function
| FullState st -> FullState (Vernacstate.Stm.make_shallow st)
| x -> x
in
let copy_info_w_state v id =
let info = get_info id in
Vcs_.set_info v id { info with state = make_shallow info.state; vcs_backup = None,None } in
let copy_proof_blockes v =
let nodes = Vcs_.Dag.all_nodes (Vcs_.dag v) in
let props =
Stateid.Set.fold (fun n pl -> Vcs_.property_of !vcs n @ pl) nodes [] in
let props = CList.sort_uniquize Vcs_.Dag.Property.compare props in
List.fold_left (fun v p ->
Vcs_.create_property v
(Stateid.Set.elements (Vcs_.Dag.Property.having_it p))
(Vcs_.Dag.Property.data p)) v props
in
let v = Vcs_.empty block_start in
let v = copy_info v block_start in
let v = List.fold_right (fun (id,tr) v ->
let v = Vcs_.commit v id tr in
let v = copy_info v id in
v) l v in
(* Stm should have reached the beginning of proof *)
assert (match get_state block_start
with FullState _ -> true | _ -> false);
(* We put in the new dag the most recent state known to master *)
let rec fill id =
match get_state id with
| EmptyState | ErrorState _ | ParsingState _ -> fill (Vcs_aux.visit v id).next
| FullState _ -> copy_info_w_state v id
in
let v = fill block_stop in
(* We put in the new dag the first state (since Qed shall run on it,
* see check_task_aux) *)
let v = copy_info_w_state v block_start in
copy_proof_blockes v
let nodes_in_slice ~block_start ~block_stop =
List.rev (List.map fst (nodes_in_slice ~block_start ~block_stop))
let topo_invariant l =
let all = List.fold_right Stateid.Set.add l Stateid.Set.empty in
List.for_all
(fun x ->
let props = property_of !vcs x in
let sets = List.map Dag.Property.having_it props in
List.for_all (fun s -> Stateid.Set.(subset s all || subset all s)) sets)
l
let create_proof_task_box l ~qed ~block_start:lemma =
if not (topo_invariant l) then anomaly Pp.(str "overlapping boxes.");
vcs := create_property !vcs l (ProofTask { qed; lemma })
let create_proof_block ({ block_start; block_stop} as decl) name =
let l = nodes_in_slice ~block_start ~block_stop in
if not (topo_invariant l) then anomaly Pp.(str "overlapping boxes.");
vcs := create_property !vcs l (ProofBlock (decl, name))
let box_of id = List.map Dag.Property.data (property_of !vcs id)
let delete_boxes_of id =
List.iter (fun x -> vcs := delete_property !vcs x) (property_of !vcs id)
let proof_task_box_of id =
match
CList.map_filter (function ProofTask x -> Some x | _ -> None) (box_of id)
with
| [] -> None
| [x] -> Some x
| _ -> anomaly Pp.(str "node with more than 1 proof task box.")
let gc () =
let old_vcs = !vcs in
let new_vcs, erased_nodes = gc old_vcs in
Stateid.Set.iter (fun id ->
match (Vcs_aux.visit old_vcs id).step with
| SQed ({ fproof = Some (_, cancel_switch) }, _)
| SCmd { cancel_switch } ->
cancel_switch := true
| _ -> ())
erased_nodes;
vcs := new_vcs
module NB : sig (* Non blocking Sys.command *)
val command : now:bool -> (unit -> unit) -> unit
end = struct
let m = Mutex.create ()
let c = Condition.create ()
let job = ref None
let worker = ref None
let set_last_job j =
CThread.with_lock m ~scope:(fun () ->
job := Some j;
Condition.signal c)
let get_last_job () =
CThread.with_lock m ~scope:(fun () ->
while Option.is_empty !job do Condition.wait c m; done;
match !job with
| None -> assert false
| Some x -> job := None; x)
let run_command () =
try while true do get_last_job () () done
with e -> () (* No failure *)
let command ~now job =
if now then job ()
else begin
set_last_job job;
if Option.is_empty !worker then
worker := Some (CThread.create run_command ())
end
end
let print ?(now=false) () =
if CDebug.(get_flag misc) then NB.command ~now (print_dag !vcs)
let backup () = !vcs
let restore v = vcs := v
end (* }}} *)
type state = Valid of Vernacstate.t option | Expired | Error of exn
let state_of_id ~doc id =
try match VCS.get_state id with
| FullState s -> Valid (Some s)
| ErrorState (_,(e,_)) -> Error e
| EmptyState | ParsingState _ -> Valid None
with VCS.Expired -> Expired
let () =
Stateid.set_is_valid (fun ~doc id -> state_of_id ~doc id <> Expired)
(****** A cache: fills in the nodes of the VCS document with their value ******)
module State : sig
type t
val freeze : unit -> t
val unfreeze : t -> unit
(** The function is from unit, so it uses the current state to define
a new one. I.e. one may been to install the right state before
defining a new one.
Warning: an optimization in installed_cached requires that state
modifying functions are always executed using this wrapper. *)
val define :
doc:doc ->
?safe_id:Stateid.t ->
?redefine:bool -> ?cache:bool ->
?feedback_processed:bool -> (unit -> unit) -> Stateid.t -> unit
val install_cached : Stateid.t -> unit
(* val install_parsing_state : Stateid.t -> unit *)
val is_cached : ?cache:bool -> Stateid.t -> bool
val is_cached_and_valid : ?cache:bool -> Stateid.t -> bool
val exn_on : Stateid.t -> valid:Stateid.t -> Exninfo.iexn -> Exninfo.iexn
(* to send states across worker/master *)
val get_cached : Stateid.t -> Vernacstate.t
type partial_state =
| Full of Vernacstate.t
| ProofOnly of Stateid.t * Vernacstate.Stm.pstate
val assign : Stateid.t -> partial_state -> unit
(* Handlers for initial state, prior to document creation. *)
val register_root_state : unit -> unit
val restore_root_state : unit -> unit
val purify : ('a -> 'b) -> 'a -> 'b
end = struct (* {{{ *)
type t = { id : Stateid.t; vernac_state : Vernacstate.t }
(* cur_id holds Stateid.dummy in case the last attempt to define a state
* failed, so the global state may contain garbage *)
let cur_id = ref Stateid.dummy
let freeze () = { id = !cur_id; vernac_state = Vernacstate.freeze_full_state () }
let unfreeze st =
Vernacstate.unfreeze_full_state st.vernac_state;
cur_id := st.id
let invalidate_cur_state () = cur_id := Stateid.dummy
type partial_state =
| Full of Vernacstate.t
| ProofOnly of Stateid.t * Vernacstate.Stm.pstate
let cache_state id =
VCS.set_state id (FullState (Vernacstate.freeze_full_state ()))
let freeze_invalid id iexn =
let ps = VCS.get_parsing_state id in
VCS.set_state id (ErrorState (ps,iexn))
let is_cached ?(cache=false) id only_valid =
if Stateid.equal id !cur_id then
try match VCS.get_info id with
| ({ state = EmptyState } | { state = ParsingState _ }) when cache -> cache_state id; true
| _ -> true
with VCS.Expired -> false
else
try match VCS.get_state id with
| EmptyState | ParsingState _ -> false
| FullState _ -> true
| ErrorState _ -> not only_valid
with VCS.Expired -> false
let is_cached_and_valid ?cache id = is_cached ?cache id true
let is_cached ?cache id = is_cached ?cache id false
let install_cached id =
match VCS.get_state id with
| FullState s ->
Vernacstate.unfreeze_full_state s;
cur_id := id
| ErrorState (_,ie) ->
Exninfo.iraise ie
| EmptyState | ParsingState _ ->
(* coqc has a 1 slot cache and only for valid states *)
if (VCS.is_interactive ()) || not (Stateid.equal id !cur_id) then
anomaly Pp.(str "installing a non cached state.")
(*
let install_parsing_state id =
if not (Stateid.equal id !cur_id) then begin
Vernacstate.Parser.install @@ VCS.get_parsing_state id
end
*)
let get_cached id =
try match VCS.get_state id with
| FullState s -> s
| _ -> anomaly Pp.(str "not a cached state.")
with VCS.Expired -> anomaly Pp.(str "not a cached state (expired).")
let assign id what =
if VCS.get_state id <> EmptyState then () else
try match what with
| Full s ->
let s =
try
let prev = (VCS.visit id).next in
if is_cached_and_valid prev
then
let open Vernacstate in
{ s with interp = { s.interp with
lemmas = PG_compat.copy_terminators
~src:((get_cached prev).interp.lemmas) ~tgt:s.interp.lemmas }}
else s
with VCS.Expired -> s in
VCS.set_state id (FullState s)
| ProofOnly(ontop,pstate) ->
if is_cached_and_valid ontop then
let s = get_cached ontop in
let s = Vernacstate.Stm.set_pstate s pstate in
VCS.set_state id (FullState s)
with VCS.Expired -> ()
let exn_on id ~valid (e, info) =
match Stateid.get info with
| Some _ -> (e, info)
| None ->
let loc = Loc.get_loc info in
execution_error ?loc id (iprint (e, info));
(e, Stateid.add info ~valid id)
(* [define] puts the system in state [id] calling [f ()] *)
(* [safe_id] is the last known valid state before execution *)
let define ~doc ?safe_id ?(redefine=false) ?(cache=false) ?(feedback_processed=true)
f id
=
feedback ~id:id (ProcessingIn !Flags.async_proofs_worker_id);
let str_id = Stateid.to_string id in
if is_cached id && not redefine then
anomaly Pp.(str"defining state "++str str_id++str" twice.");
try
stm_prerr_endline (fun () -> "defining "^str_id^" (cache="^
if cache then "Y)" else "N)");
f ();
if cache then cache_state id;
stm_prerr_endline (fun () -> "setting cur id to "^str_id);
cur_id := id;
if feedback_processed then
!Hooks.state_computed~doc id ~in_cache:false;
VCS.reached id;
if PG_compat.there_are_pending_proofs () then
VCS.goals id (PG_compat.get_open_goals ())
with e ->
let (e, info) = Exninfo.capture e in
let good_id = !cur_id in
invalidate_cur_state ();
VCS.reached id;
let ie =
match Stateid.get info, safe_id with
| None, None -> (exn_on id ~valid:good_id (e, info))
| None, Some good_id -> (exn_on id ~valid:good_id (e, info))
| Some _, None -> (e, info)
| Some (_,at), Some id -> (e, Stateid.add info ~valid:id at) in
if cache then freeze_invalid id ie;
!Hooks.unreachable_state ~doc id ie;
Exninfo.iraise ie
let init_state = ref None
let register_root_state () =
init_state := Some (Vernacstate.freeze_full_state ())
let restore_root_state () =
cur_id := Stateid.dummy;
Vernacstate.unfreeze_full_state (Option.get !init_state)
(* Protect against state changes *)
let purify f x =
let st = freeze () in
try
let res = f x in
Vernacstate.Interp.invalidate_cache ();
unfreeze st;
res
with e ->