| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Pp |
| 12 |
|
| 13 |
let stm_pr_err s = Format.eprintf "%s] %s\n%!" (Spawned.process_id ()) s |
| 14 |
let stm_prerr_endline s = if CDebug.(get_flag misc) then begin stm_pr_err s end else () |
| 15 |
|
| 16 |
type response = |
| 17 |
| RespBuiltSubProof of (Constr.constr * UState.t) |
| 18 |
| RespError of bool * Pp.t |
| 19 |
| RespKilled of int |
| 20 |
| RespNoProgress |
| 21 |
|
| 22 |
module TacTask : sig |
| 23 |
|
| 24 |
type nonrec response = response |
| 25 |
|
| 26 |
type task = { |
| 27 |
t_state : Vernacstate.t; |
| 28 |
t_assign : response option ref; |
| 29 |
t_ast : ComTactic.interpretable; |
| 30 |
t_goalno : int; |
| 31 |
t_goal : Evar.t; |
| 32 |
t_kill : unit -> unit; |
| 33 |
t_name : string } |
| 34 |
|
| 35 |
include AsyncTaskQueue.Task with type task := task and type response := response |
| 36 |
|
| 37 |
end = struct |
| 38 |
|
| 39 |
let forward_feedback { Feedback.doc_id = did; span_id = id; route; contents } = |
| 40 |
Feedback.feedback ~did ~id ~route contents |
| 41 |
|
| 42 |
type nonrec response = response |
| 43 |
|
| 44 |
type task = { |
| 45 |
t_state : Vernacstate.t; |
| 46 |
t_assign : response option ref; |
| 47 |
t_ast : ComTactic.interpretable; |
| 48 |
t_goalno : int; |
| 49 |
t_goal : Evar.t; |
| 50 |
t_kill : unit -> unit; |
| 51 |
t_name : string } |
| 52 |
|
| 53 |
type request = { |
| 54 |
r_state : Vernacstate.t option; |
| 55 |
r_ast : ComTactic.interpretable; |
| 56 |
r_goalno : int; |
| 57 |
r_goal : Evar.t; |
| 58 |
r_name : string } |
| 59 |
|
| 60 |
let name = "tactic" |
| 61 |
let extra_env () = [||] |
| 62 |
type competence = unit |
| 63 |
type worker_status = Fresh | Old of competence |
| 64 |
|
| 65 |
let task_match _ _ = true |
| 66 |
|
| 67 |
|
| 68 |
let request_of_task age { t_state; t_ast; t_goalno; t_goal; t_name } = |
| 69 |
Some { |
| 70 |
r_state = if age <> Fresh then None else Some t_state; |
| 71 |
r_ast = t_ast; |
| 72 |
r_goalno = t_goalno; |
| 73 |
r_goal = t_goal; |
| 74 |
r_name = t_name } |
| 75 |
|
| 76 |
let assign r v = |
| 77 |
let () = assert (Option.is_empty !r) in |
| 78 |
r := Some v |
| 79 |
|
| 80 |
let use_response _ { t_assign; t_kill } resp = |
| 81 |
assign t_assign resp; |
| 82 |
let kill = match resp with |
| 83 |
| RespNoProgress | RespBuiltSubProof _ -> false |
| 84 |
| RespError _ -> true |
| 85 |
| RespKilled _ -> assert false |
| 86 |
in |
| 87 |
if kill then t_kill (); |
| 88 |
`Stay ((),[]) |
| 89 |
|
| 90 |
let on_marshal_error err { t_name } = |
| 91 |
stm_prerr_endline ("Fatal marshal error (task " ^ t_name ^ "): " ^ err); |
| 92 |
flush_all () |
| 93 |
|
| 94 |
let on_task_cancellation_or_expiration_or_slave_death = function |
| 95 |
| Some { t_goalno; t_assign; t_kill } -> |
| 96 |
t_kill (); |
| 97 |
assert (Option.is_empty !t_assign); |
| 98 |
t_assign := Some (RespKilled t_goalno) |
| 99 |
| _ -> () |
| 100 |
|
| 101 |
let command_focus = Proof.new_focus_kind "partac_focus" |
| 102 |
let focus_cond = Proof.no_cond command_focus |
| 103 |
|
| 104 |
let state = ref None |
| 105 |
let receive_state = function |
| 106 |
| None -> () |
| 107 |
| Some st -> state := Some st |
| 108 |
|
| 109 |
let perform { r_state = st; r_ast = tactic; r_goal; r_goalno } = |
| 110 |
receive_state st; |
| 111 |
Vernacstate.unfreeze_full_state (Option.get !state); |
| 112 |
try |
| 113 |
Vernacstate.LemmaStack.with_top (Option.get (Option.get !state).Vernacstate.interp.lemmas) ~f:(fun pstate -> |
| 114 |
let pstate = |
| 115 |
Declare.Proof.map pstate ~f:(Proof.focus focus_cond () r_goalno) in |
| 116 |
let pstate = |
| 117 |
ComTactic.solve ~pstate |
| 118 |
Goal_select.SelectAll ~info:None tactic ~with_end_tac:(CAst.make false) in |
| 119 |
let { Proof.sigma } = Declare.Proof.fold pstate ~f:Proof.data in |
| 120 |
let EvarInfo evi = Evd.find sigma r_goal in |
| 121 |
match Evd.(evar_body evi) with |
| 122 |
| Evd.Evar_empty -> RespNoProgress |
| 123 |
| Evd.Evar_defined t -> |
| 124 |
let t = Evarutil.nf_evar sigma t in |
| 125 |
let evars = Evarutil.undefined_evars_of_term sigma t in |
| 126 |
if Evar.Set.is_empty evars then |
| 127 |
let t = EConstr.Unsafe.to_constr t in |
| 128 |
RespBuiltSubProof (t, Evd.ustate sigma) |
| 129 |
else |
| 130 |
CErrors.user_err |
| 131 |
Pp.(str"The par: selector requires a tactic that makes no progress or fully" ++ |
| 132 |
str" solves the goal and leaves no unresolved existential variables. The following" ++ |
| 133 |
str" existentials remain unsolved: " ++ prlist (Termops.pr_existential_key (Global.env ()) sigma) (Evar.Set.elements evars)) |
| 134 |
) |
| 135 |
with e -> |
| 136 |
let noncrit = CErrors.noncritical e in |
| 137 |
RespError (noncrit, CErrors.print e ++ spc() ++ str "(for goal "++int r_goalno ++ str ")") |
| 138 |
|
| 139 |
let perform r : response = |
| 140 |
NewProfile.profile "partac.perform" |
| 141 |
~args:(fun () -> ["goalno", `Intlit (string_of_int r.r_goalno)]) |
| 142 |
(fun () -> perform r) |
| 143 |
() |
| 144 |
|
| 145 |
let name_of_task { t_name } = t_name |
| 146 |
let name_of_request { r_name } = r_name |
| 147 |
|
| 148 |
end |
| 149 |
|
| 150 |
module TaskQueue = AsyncTaskQueue.MakeQueue(TacTask) () |
| 151 |
|
| 152 |
let assign_tac ~abstract res : unit Proofview.tactic = |
| 153 |
Proofview.(Goal.enter begin fun g -> |
| 154 |
let gid = Goal.goal g in |
| 155 |
match List.assoc gid res with |
| 156 |
| exception Not_found -> tclUNIT () |
| 157 |
| (pt, uc) -> |
| 158 |
let open Notations in |
| 159 |
let push_state ctx = |
| 160 |
Proofview.tclEVARMAP >>= fun sigma -> |
| 161 |
Proofview.Unsafe.tclEVARS (Evd.merge_universe_context sigma ctx) |
| 162 |
in |
| 163 |
(if abstract then Abstract.tclABSTRACT None else (fun x -> x)) |
| 164 |
(push_state uc <*> Tactics.exact_no_check (EConstr.of_constr pt)) |
| 165 |
end) |
| 166 |
|
| 167 |
let get_results res = |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
let missing = ref [] in |
| 173 |
let killed = ref [] in |
| 174 |
let res = CList.map_filter_i (fun i (g,v) -> match !v with |
| 175 |
| None -> missing := (succ i) :: !missing; None |
| 176 |
| Some (RespBuiltSubProof v) -> Some (g,v) |
| 177 |
| Some RespNoProgress -> None |
| 178 |
| Some (RespKilled goalno) -> killed := goalno :: !killed; None |
| 179 |
| Some (RespError (noncrt, msg)) -> |
| 180 |
if noncrt then raise (AsyncTaskQueue.RemoteException msg) |
| 181 |
else CErrors.anomaly msg) |
| 182 |
res |
| 183 |
in |
| 184 |
match !killed, !missing with |
| 185 |
| [], [] -> res |
| 186 |
| killed :: _, _ -> CErrors.anomaly Pp.(str "Worker failed (for goal " ++ int killed ++ str")") |
| 187 |
| [], missing -> |
| 188 |
CErrors.anomaly |
| 189 |
(str "Missing results (for " ++ |
| 190 |
str (CString.plural (List.length missing) "goal") ++ |
| 191 |
spc () ++ prlist_with_sep spc int missing ++ str ")") |
| 192 |
|
| 193 |
let enable_par ~spawn_args ~nworkers = ComTactic.set_par_implementation |
| 194 |
(fun ~pstate ~info t_ast ~abstract -> |
| 195 |
let t_state = Vernacstate.freeze_full_state () in |
| 196 |
let t_state = Vernacstate.Stm.make_shallow t_state in |
| 197 |
TaskQueue.with_n_workers ~spawn_args nworkers CoqworkmgrApi.High (fun queue -> |
| 198 |
Declare.Proof.map pstate ~f:(fun p -> |
| 199 |
let open TacTask in |
| 200 |
let results = (Proof.data p).Proof.goals |> CList.map_i (fun i g -> |
| 201 |
let ans = ref None in |
| 202 |
TaskQueue.enqueue_task queue |
| 203 |
~cancel_switch:(ref false) |
| 204 |
{ t_state; t_assign = ans; t_ast; |
| 205 |
t_goalno = i; t_goal = g; t_name = Proof.goal_uid g; |
| 206 |
t_kill = (fun () -> TaskQueue.cancel_all queue) }; |
| 207 |
g, ans) 1 in |
| 208 |
TaskQueue.join queue; |
| 209 |
let results = get_results results in |
| 210 |
let p,_,() = |
| 211 |
NewProfile.profile "partac.assign" (fun () -> |
| 212 |
Proof.run_tactic (Global.env()) |
| 213 |
(assign_tac ~abstract results) p) |
| 214 |
() |
| 215 |
in |
| 216 |
p))) |