session_scheduler.ml 35 KB
Newer Older
Andrei Paskevich's avatar
Andrei Paskevich committed
1 2 3
(********************************************************************)
(*                                                                  *)
(*  The Why3 Verification Platform   /   The Why3 Development Team  *)
4
(*  Copyright 2010-2015   --   INRIA - CNRS - Paris-Sud University  *)
Andrei Paskevich's avatar
Andrei Paskevich committed
5 6 7 8 9 10
(*                                                                  *)
(*  This software is distributed under the terms of the GNU Lesser  *)
(*  General Public License version 2.1, with the special exception  *)
(*  on linking described in file LICENSE.                           *)
(*                                                                  *)
(********************************************************************)
François Bobot's avatar
François Bobot committed
11 12 13 14

open Format
open Session

15
let debug = Debug.register_info_flag "scheduler"
Andrei Paskevich's avatar
Andrei Paskevich committed
16
  ~desc:"Print@ debugging@ messages@ about@ scheduling@ of@ prover@ calls@ \
MARCHE Claude's avatar
MARCHE Claude committed
17
         and@ transformation@ applications."
François Bobot's avatar
François Bobot committed
18

19 20 21
let usleep t = ignore (Unix.select [] [] [] t)
let default_delay_ms = 100 (* 0.1 seconds *)

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
module Todo = struct
  type ('a,'b) todo =
      {mutable todo : int;
       mutable report : 'a;
       push_report : 'a -> 'b -> 'a;
       callback : 'a -> unit}

  let create init push callback =
    {todo = 0;  report = init; push_report = push; callback = callback}

  let stop todo =
    todo.todo <- todo.todo - 1;
    if todo.todo=0 then todo.callback todo.report

  let _done todo v =
    todo.report <- todo.push_report todo.report v;
    stop todo

  let start todo =
    todo.todo <- todo.todo + 1

(** dead code
  let print todo =
    dprintf debug "[Sched] todo : %i@." todo.todo
*)
end

François Bobot's avatar
François Bobot committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
(***************************)
(*     main functor        *)
(***************************)

module type OBSERVER = sig
  type key
  val create: ?parent:key -> unit -> key
  val remove: key -> unit
  val reset: unit -> unit

  val timeout: ms:int -> (unit -> bool) -> unit
  val idle: (unit -> bool) -> unit

  val notify_timer_state : int -> int -> int -> unit

  val init : key -> key any -> unit

  val notify : key any -> unit

68 69
  val uninstalled_prover :
    key env_session -> Whyconf.prover -> Whyconf.prover_upgrade_policy
70

François Bobot's avatar
François Bobot committed
71 72 73 74
end



MARCHE Claude's avatar
MARCHE Claude committed
75
module Make(O : OBSERVER) = struct
François Bobot's avatar
François Bobot committed
76 77 78 79 80 81

(*************************)
(*         Scheduler     *)
(*************************)

type action =
David Hauzar's avatar
David Hauzar committed
82
  | Action_proof_attempt of bool * int * int * int * string option * bool * string *
François Bobot's avatar
François Bobot committed
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
      Driver.driver * (proof_attempt_status -> unit) * Task.task
  | Action_delayed of (unit -> unit)

type timeout_action =
  | Check_prover of (proof_attempt_status -> unit) * Call_provers.prover_call
  | Any_timeout of (unit -> bool)

type t =
    { (** Actions that wait some idle time *)
      actions_queue : action Queue.t;
      (** Quota of action slot *)
      mutable maximum_running_proofs : int;
      (** Running actions which take one action slot *)
      mutable running_proofs : timeout_action list;
      (** Running check which doesn't take a running slot.
          Check the end of some computation *)
      mutable running_check : (unit -> bool) list;
      (** proof attempt that wait some available action slot *)
      proof_attempts_queue :
        ((proof_attempt_status -> unit) *
            (unit -> Call_provers.prover_call))
        Queue.t;
      (** timeout handler state *)
      mutable timeout_handler_activated : bool;
      mutable timeout_handler_running : bool;
      (** idle handler state *)
      mutable idle_handler_activated : bool;
    }

let set_maximum_running_proofs max sched =
MARCHE Claude's avatar
MARCHE Claude committed
113
  (* TODO dequeue actions if maximum_running_proofs increase *)
François Bobot's avatar
François Bobot committed
114 115 116
  sched.maximum_running_proofs <- max

let init max =
117
  Debug.dprintf debug "[Sched] init scheduler max=%i@." max;
François Bobot's avatar
François Bobot committed
118 119 120 121 122 123 124 125 126 127
  { actions_queue = Queue.create ();
    maximum_running_proofs = max;
    running_proofs = [];
    running_check = [];
    proof_attempts_queue = Queue.create ();
    timeout_handler_activated = false;
    timeout_handler_running = false;
    idle_handler_activated = false
  }

128 129 130
let notify_timer_state t continue =
  O.notify_timer_state
    (Queue.length t.actions_queue)
131
    (Queue.length t.proof_attempts_queue)
132 133 134
    (List.length t.running_proofs);
  continue

François Bobot's avatar
François Bobot committed
135 136 137
(* timeout handler *)

let timeout_handler t =
138
  Debug.dprintf debug "[Sched] Timeout handler called@.";
François Bobot's avatar
François Bobot committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
  assert (not t.timeout_handler_running);
  t.timeout_handler_running <- true;
  (** Check if some action ended *)
  let l = List.fold_left
    (fun acc c ->
       match c with
         | Check_prover(callback,call)  ->
             (match Call_provers.query_call call with
               | None -> c::acc
               | Some post ->
                   let res = post () in callback (Done res);
                   acc)
         | Any_timeout callback ->
             let b = callback () in
             if b then c::acc else acc)
    [] t.running_proofs
  in
  (** Check if some new actions must be started *)
  let l =
    if List.length l < t.maximum_running_proofs then
      begin try
        let (callback,pre_call) = Queue.pop t.proof_attempts_queue in
161
        callback Running;
162
        Debug.dprintf debug "[Sched] proof attempts started@.";
François Bobot's avatar
François Bobot committed
163 164 165 166 167 168 169 170 171 172 173 174 175 176
        let call = pre_call () in
        (Check_prover(callback,call))::l
      with Queue.Empty -> l
      end
    else l
  in
  t.running_proofs <- l;
  (** Call the running check *)
  t.running_check <- List.fold_left
    (fun acc check -> if check () then check::acc else acc)
    [] t.running_check;
  let continue =
    match l with
      | [] ->
177
          Debug.dprintf debug "[Sched] Timeout handler stopped@.";
François Bobot's avatar
François Bobot committed
178 179 180 181 182
          false
      | _ -> true
  in
  t.timeout_handler_activated <- continue;
  t.timeout_handler_running <- false;
183
  notify_timer_state t continue
François Bobot's avatar
François Bobot committed
184 185 186 187 188

let run_timeout_handler t =
  if t.timeout_handler_activated then () else
    begin
      t.timeout_handler_activated <- true;
189
      Debug.dprintf debug "[Sched] Timeout handler started@.";
190
      O.timeout ~ms:default_delay_ms (fun () -> timeout_handler t)
François Bobot's avatar
François Bobot committed
191 192 193
    end

let schedule_any_timeout t callback =
194
  Debug.dprintf debug "[Sched] schedule a new timeout@.";
François Bobot's avatar
François Bobot committed
195 196 197
  t.running_proofs <- (Any_timeout callback) :: t.running_proofs;
  run_timeout_handler t

MARCHE Claude's avatar
MARCHE Claude committed
198
(* unused
199
let schedule_check t callback =
200
  Debug.dprintf debug "[Sched] add a new check@.";
François Bobot's avatar
François Bobot committed
201 202
  t.running_check <- callback :: t.running_check;
  run_timeout_handler t
MARCHE Claude's avatar
MARCHE Claude committed
203
*)
François Bobot's avatar
François Bobot committed
204 205 206 207 208

(* idle handler *)

let idle_handler t =
  try
MARCHE Claude's avatar
MARCHE Claude committed
209 210
    if Queue.length t.proof_attempts_queue < 3 * t.maximum_running_proofs then
      begin
François Bobot's avatar
François Bobot committed
211
      match Queue.pop t.actions_queue with
David Hauzar's avatar
David Hauzar committed
212
        | Action_proof_attempt(cntexample,timelimit,memlimit,steplimit,
213
              old,inplace,command,driver,callback,goal) ->
François Bobot's avatar
François Bobot committed
214 215 216
            begin
              try
                let pre_call =
David Hauzar's avatar
David Hauzar committed
217
                  Driver.prove_task ?old ~cntexample ~inplace ~command
218
                    ~timelimit ~steplimit ~memlimit driver goal
François Bobot's avatar
François Bobot committed
219 220 221
                in
                Queue.push (callback,pre_call) t.proof_attempts_queue;
                run_timeout_handler t
222
              with e when not (Debug.test_flag Debug.stack_trace) ->
MARCHE Claude's avatar
MARCHE Claude committed
223 224
                Format.eprintf
                  "@[Exception raise in Session.idle_handler:@ %a@.@]"
François Bobot's avatar
François Bobot committed
225 226 227 228
                  Exn_printer.exn_printer e;
                callback (InternalFailure e)
            end
        | Action_delayed callback -> callback ()
MARCHE Claude's avatar
MARCHE Claude committed
229 230
    end
    else
231
      usleep (float default_delay_ms /. 1000.);
232
    notify_timer_state t true
MARCHE Claude's avatar
MARCHE Claude committed
233 234 235 236
  with
    | Queue.Empty ->
      t.idle_handler_activated <- false;
      Debug.dprintf debug "[Sched] idle_handler stopped@.";
237
      notify_timer_state t false
238
    | e when not (Debug.test_flag Debug.stack_trace) ->
François Bobot's avatar
François Bobot committed
239 240 241
      Format.eprintf "@[Exception raise in Session.idle_handler:@ %a@.@]"
        Exn_printer.exn_printer e;
      eprintf "Session.idle_handler stopped@.";
242
      notify_timer_state t false
François Bobot's avatar
François Bobot committed
243 244 245 246 247 248


let run_idle_handler t =
  if t.idle_handler_activated then () else
    begin
      t.idle_handler_activated <- true;
249
      Debug.dprintf debug "[Sched] idle_handler started@.";
François Bobot's avatar
François Bobot committed
250 251 252 253 254 255 256 257 258 259
      O.idle (fun () -> idle_handler t)
    end

(* main scheduling functions *)

let cancel_scheduled_proofs t =
  let new_queue = Queue.create () in
  try
    while true do
      match Queue.pop t.actions_queue with
David Hauzar's avatar
David Hauzar committed
260
        | Action_proof_attempt(_cntexample,_timelimit,_memlimit,_steplimit,
261
              _old,_inplace,_command,_driver,callback,_goal) ->
262
            callback Interrupted
François Bobot's avatar
François Bobot committed
263 264 265 266 267 268 269 270
        | Action_delayed _ as a->
            Queue.push a new_queue
    done
  with Queue.Empty ->
    Queue.transfer new_queue t.actions_queue;
    try
      while true do
        let (callback,_) = Queue.pop t.proof_attempts_queue in
271
        callback Interrupted
François Bobot's avatar
François Bobot committed
272 273
      done
    with
274
      | Queue.Empty ->
François Bobot's avatar
François Bobot committed
275 276 277
          O.notify_timer_state 0 0 (List.length t.running_proofs)


David Hauzar's avatar
David Hauzar committed
278
let schedule_proof_attempt ~cntexample ~timelimit ~memlimit ~steplimit ?old ~inplace
François Bobot's avatar
François Bobot committed
279
    ~command ~driver ~callback t goal =
280
  Debug.dprintf debug "[Sched] Scheduling a new proof attempt (goal : %a)@."
281 282
    (fun fmt g -> Format.pp_print_string fmt
      (Task.task_goal g).Decl.pr_name.Ident.id_string) goal;
283
  callback Scheduled;
François Bobot's avatar
François Bobot committed
284
  Queue.push
David Hauzar's avatar
David Hauzar committed
285
    (Action_proof_attempt(cntexample,timelimit,memlimit,steplimit,
286
      old,inplace,command,driver,callback,goal))
François Bobot's avatar
François Bobot committed
287 288 289 290
    t.actions_queue;
  run_idle_handler t

let schedule_edition t command filename callback =
291
  Debug.dprintf debug "[Sched] Scheduling an edition@.";
292 293 294
  let res_parser =
    { Call_provers.prp_exitcodes = [(0,Call_provers.Unknown "")];
      Call_provers.prp_regexps = [];
295
      Call_provers.prp_timeregexps = [];
296
      Call_provers.prp_stepregexps = [];
297
      Call_provers.prp_model_parser = fun _ _ -> Model_parser.empty_model 
298
    } in
François Bobot's avatar
François Bobot committed
299
  let precall =
300
    Call_provers.call_on_file ~command ~res_parser ~redirect:false filename ~printer_mapping:Printer.get_default_printer_mapping in
301
  callback Running;
François Bobot's avatar
François Bobot committed
302 303 304 305
  t.running_proofs <- (Check_prover(callback, precall ())) :: t.running_proofs;
  run_timeout_handler t

let schedule_delayed_action t callback =
306
  Debug.dprintf debug "[Sched] Scheduling a delayed action@.";
François Bobot's avatar
François Bobot committed
307 308 309 310
  Queue.push (Action_delayed callback) t.actions_queue;
  run_idle_handler t

(**************************)
MARCHE Claude's avatar
MARCHE Claude committed
311
(*  session functions     *)
François Bobot's avatar
François Bobot committed
312 313
(**************************)

MARCHE Claude's avatar
MARCHE Claude committed
314 315 316 317 318 319
let notify = O.notify

let rec init_any any = O.init (key_any any) any; iter init_any any

let init_session session = session_iter init_any session

320 321
let update_session ~allow_obsolete ~release ~use_shapes
    old_session env whyconf  =
François Bobot's avatar
François Bobot committed
322
  O.reset ();
323 324 325 326 327 328 329
  let ctxt = {
    allow_obsolete_goals = allow_obsolete;
    release_tasks = release;
    use_shapes_for_pairing_sub_goals = use_shapes;
    keygen = O.create;
  }
  in
330
  let (env_session,_,_) as res =
331
    update_session ~ctxt old_session env whyconf
MARCHE Claude's avatar
MARCHE Claude committed
332
  in
333
  Debug.dprintf debug "Init_session@\n";
François Bobot's avatar
François Bobot committed
334 335 336
  init_session env_session.session;
  res

337 338
let add_file env_session ?format f =
  let mfile = add_file ~keygen:O.create env_session ?format f in
François Bobot's avatar
François Bobot committed
339 340 341 342 343 344 345 346
  let any_file = (File mfile) in
  init_any any_file;
  O.notify any_file;
  mfile

(*****************************************************)
(* method: run a given prover on each unproved goals *)
(*****************************************************)
347

348 349
let find_prover eS a =
  match load_prover eS a.proof_prover with
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
    | Some p -> Some (a.proof_prover, p,a)
    | None ->
        match O.uninstalled_prover eS a.proof_prover with
          | Whyconf.CPU_keep -> None
          | Whyconf.CPU_upgrade new_p ->
            (* does a proof using new_p already exists ? *)
            let g = a.proof_parent in
            begin
              try
                let _ = PHprover.find g.goal_external_proofs new_p in
                (* yes, then we do nothing *)
                None
              with Not_found ->
                (* we modify the prover in-place *)
                Session.change_prover a new_p;
365
                match load_prover eS new_p with
366
                  | Some p -> Some (new_p,p,a)
Andrei Paskevich's avatar
Andrei Paskevich committed
367
                  | None ->
368 369 370 371 372 373
                    (* should never happen because at loading, config
                       ignores uninstalled prover targets.
                       Nevertheless, we can safely return None.
                    *)
                    None
            end
Andrei Paskevich's avatar
Andrei Paskevich committed
374
          | Whyconf.CPU_duplicate new_p ->
375 376 377 378 379 380 381 382 383 384
            (* does a proof using new_p already exists ? *)
            let g = a.proof_parent in
            begin
              try
                let _ = PHprover.find g.goal_external_proofs new_p in
                (* yes, then we do nothing *)
                None
              with Not_found ->
                (* we duplicate the proof_attempt *)
                let new_a = copy_external_proof
Andrei Paskevich's avatar
Andrei Paskevich committed
385
                  ~notify ~keygen:O.create ~prover:new_p ~env_session:eS a
386 387
                in
                O.init new_a.proof_key (Proof_attempt new_a);
388
                match load_prover eS new_p with
389
                  | Some p -> Some (new_p,p,new_a)
Andrei Paskevich's avatar
Andrei Paskevich committed
390
                  | None ->
391 392 393 394 395 396
                    (* should never happen because at loading, config
                       ignores uninstalled prover targets.
                       Nevertheless, we can safely return None.
                    *)
                    None
            end
397

François Bobot's avatar
François Bobot committed
398

399 400 401 402
(* to avoid corner cases when prover results are obtained very closely
   to the time or mem limits, we adapt these limits when we replay a
   proof *)
let adapt_limits a =
403
  match a.proof_state with
404 405 406 407 408 409
  | Done { Call_provers.pr_answer = r;
           Call_provers.pr_time = t } ->
    (* increased time limit is 1 + twice the previous running time,
       but enforced to remain inside the interval [l,2l] where l is
       the previous time limit *)
    let increased_time =
410 411
      let t = truncate (1.0 +. 2.0 *. t) in
      max a.proof_timelimit (min t (2 * a.proof_timelimit))
412 413 414 415 416 417 418
    in
    (* increased mem limit is just 1.5 times the previous mem limit *)
    let increased_mem = 3 * a.proof_memlimit / 2 in
    begin
      match r with
      | Call_provers.OutOfMemory -> increased_time, a.proof_memlimit
      | Call_provers.Timeout -> a.proof_timelimit, increased_mem
419
      | Call_provers.StepLimitExceeded
420 421 422 423 424 425 426 427 428 429
      | Call_provers.Valid
      | Call_provers.Unknown _
      | Call_provers.Invalid -> increased_time, increased_mem
      | Call_provers.Failure _
      | Call_provers.HighFailure ->
        (* correct ? failures are supposed to appear quickly anyway... *)
        a.proof_timelimit, a.proof_memlimit
    end
  | _ -> a.proof_timelimit, a.proof_memlimit

430

431
type run_external_status =
432 433 434 435
| Starting
| MissingProver
| MissingFile of string
| StatusChange of proof_attempt_status
436 437 438

exception NoFile of string

439 440 441
(* do not modify the proof duration stored in proof sessions if it
   changed by less than 10% or 0.1s, so as to avoid diff noise in
   session files *)
442 443 444 445
let fuzzy_proof_time nres ores =
  match ores, nres with
  | Done { Call_provers.pr_time = told },
    Done ({ Call_provers.pr_time = tnew } as res')
446
  when tnew >= told *. 0.9 -. 0.1 && tnew <= told *. 1.1 +. 0.1 ->
447 448 449
    Done { res' with Call_provers.pr_time = told }
  | _, _ -> nres

450 451
let dummy_limits = (0,0,0)

452 453
(** run_external_proof_v3 doesn't modify existing proof attempt, it can just
    create new one by find_prover *)
David Hauzar's avatar
David Hauzar committed
454
let run_external_proof_v3 eS eT a ?(cntexample=false) callback =
455
  match find_prover eS a with
456
  | None ->
457
    callback a a.proof_prover dummy_limits None Starting;
458
    (* nothing to do *)
459
    callback a a.proof_prover dummy_limits None MissingProver
460
  | Some(ap,npc,a) ->
461
    callback a ap dummy_limits None Starting;
462 463 464
    if a.proof_edited_as = None &&
       npc.prover_config.Whyconf.interactive
    then begin
465
      callback a ap dummy_limits None (MissingFile "unedited")
466
    end else begin
467
      let previous_result = a.proof_state in
468
      let timelimit, memlimit = adapt_limits a in
469
      let steplimit =
470
	match a with
471 472 473
	| { proof_state =
            Done { Call_provers.pr_answer = Call_provers.Valid;
                   Call_provers.pr_steps = s };
474 475
	    proof_obsolete = false } when s >= 0 -> s
	| _ -> -1
476
      in
477
      let inplace = npc.prover_config.Whyconf.in_place in
478
      let command = Whyconf.get_complete_command npc.prover_config steplimit in
479
      let cb result =
480
        let result = fuzzy_proof_time result previous_result in
481
        callback a ap (timelimit,memlimit,steplimit)
482 483 484 485 486 487 488 489 490 491
          (match previous_result with Done res -> Some res | _ -> None)
          (StatusChange result) in
      try
        let old =
          match get_edited_as_abs eS.session a with
          | None -> None
          | Some f ->
            if Sys.file_exists f then Some f
            else raise (NoFile f) in
        schedule_proof_attempt
David Hauzar's avatar
David Hauzar committed
492
          ~cntexample ~timelimit ~memlimit ~steplimit
493 494 495 496
          ?old ~inplace ~command
          ~driver:npc.prover_driver
          ~callback:cb
          eT
497
          (goal_task_or_recover eS a.proof_parent)
498
      with NoFile f ->
499
        callback a ap dummy_limits None (MissingFile f)
500 501
    end

502
(** run_external_proof_v2 modify the session according to the current state *)
David Hauzar's avatar
David Hauzar committed
503
let run_external_proof_v2 eS eT a ~cntexample callback =
504
  let previous_res = ref (a.proof_state,a.proof_obsolete) in
505
  let callback a ap limits previous state =
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
    begin match state with
    | Starting -> previous_res := (a.proof_state,a.proof_obsolete)
    | MissingFile _ ->
      set_proof_state ~notify ~obsolete:false ~archived:false
        Unedited a
    | StatusChange result ->
      begin match result with
      | Interrupted ->
        let previous_result,obsolete = !previous_res in
        set_proof_state ~notify ~obsolete
          ~archived:false previous_result a
      | _ ->
        set_proof_state ~notify ~obsolete:false
          ~archived:false result a
      end
    | _ -> ()
    end;
523
    callback a ap limits previous state
MARCHE Claude's avatar
MARCHE Claude committed
524
  in
David Hauzar's avatar
David Hauzar committed
525
  run_external_proof_v3 eS eT a ~cntexample callback
526

MARCHE Claude's avatar
MARCHE Claude committed
527 528 529 530 531
let running = function
  | Scheduled | Running -> true
  | Unedited | JustEdited | Interrupted
  | Done _ | InternalFailure _ -> false

David Hauzar's avatar
David Hauzar committed
532
let run_external_proof_v2 eS eT a ?(cntexample=false) callback =
533 534
  (* Perhaps the test a.proof_archived should be done somewhere else *)
  if a.proof_archived || running a.proof_state then () else
David Hauzar's avatar
David Hauzar committed
535
  run_external_proof_v2 eS eT a ~cntexample callback
536

David Hauzar's avatar
David Hauzar committed
537
let run_external_proof eS eT ?(cntexample=false) ?callback a =
538 539 540 541 542 543 544 545
  let callback =
    match callback with
    | None -> fun _ _ _ _ _ -> ()
    | Some c -> fun a _ _ _ s ->
      match s with
      | Starting -> ()
      | MissingProver -> c a Interrupted
      | MissingFile _ -> c a a.proof_state
546
      | StatusChange s -> c a s
MARCHE Claude's avatar
MARCHE Claude committed
547
  in
David Hauzar's avatar
David Hauzar committed
548
  run_external_proof_v2 eS eT a ~cntexample callback
François Bobot's avatar
François Bobot committed
549

David Hauzar's avatar
David Hauzar committed
550
let prover_on_goal eS eT ?callback ?(cntexample=false) ~timelimit ~memlimit p g =
François Bobot's avatar
François Bobot committed
551
  let a =
552 553 554
    try
      let a = PHprover.find g.goal_external_proofs p in
      set_timelimit timelimit a;
555
      set_memlimit memlimit a;
556
      a
François Bobot's avatar
François Bobot committed
557
    with Not_found ->
558
      let ep = add_external_proof ~keygen:O.create ~obsolete:false
Andrei Paskevich's avatar
Andrei Paskevich committed
559
        ~archived:false ~timelimit ~memlimit
560
        ~edit:None g p Interrupted in
François Bobot's avatar
François Bobot committed
561 562 563
      O.init ep.proof_key (Proof_attempt ep);
      ep
  in
David Hauzar's avatar
David Hauzar committed
564
  run_external_proof eS eT ~cntexample ?callback a
François Bobot's avatar
François Bobot committed
565

566
let prover_on_goal_or_children eS eT
David Hauzar's avatar
David Hauzar committed
567
    ~context_unproved_goals_only ~cntexample ~timelimit ~memlimit p g =
568
  goal_iter_leaf_goal ~unproved_only:context_unproved_goals_only
David Hauzar's avatar
David Hauzar committed
569
    (prover_on_goal eS eT ~cntexample ~timelimit ~memlimit p) g
François Bobot's avatar
François Bobot committed
570

David Hauzar's avatar
David Hauzar committed
571
let run_prover eS eT ~context_unproved_goals_only ~cntexample ~timelimit ~memlimit pr a =
François Bobot's avatar
François Bobot committed
572
  match a with
573 574
  | Goal g ->
    prover_on_goal_or_children eS eT
David Hauzar's avatar
David Hauzar committed
575
      ~context_unproved_goals_only ~cntexample ~timelimit ~memlimit pr g
576
  | Theory th ->
François Bobot's avatar
François Bobot committed
577 578
        List.iter
          (prover_on_goal_or_children eS eT
David Hauzar's avatar
David Hauzar committed
579
             ~context_unproved_goals_only ~cntexample ~timelimit ~memlimit pr)
François Bobot's avatar
François Bobot committed
580 581 582 583 584 585
          th.theory_goals
    | File file ->
        List.iter
          (fun th ->
             List.iter
               (prover_on_goal_or_children eS eT
David Hauzar's avatar
David Hauzar committed
586
                  ~context_unproved_goals_only ~cntexample ~timelimit ~memlimit pr)
François Bobot's avatar
François Bobot committed
587 588 589 590
               th.theory_goals)
          file.file_theories
    | Proof_attempt a ->
        prover_on_goal_or_children eS eT
David Hauzar's avatar
David Hauzar committed
591
          ~context_unproved_goals_only ~cntexample ~timelimit ~memlimit pr a.proof_parent
François Bobot's avatar
François Bobot committed
592 593 594
    | Transf tr ->
        List.iter
          (prover_on_goal_or_children eS eT
David Hauzar's avatar
David Hauzar committed
595
             ~context_unproved_goals_only ~cntexample ~timelimit ~memlimit pr)
François Bobot's avatar
François Bobot committed
596
          tr.transf_goals
François Bobot's avatar
François Bobot committed
597 598
    | Metas m ->
      prover_on_goal_or_children eS eT
David Hauzar's avatar
David Hauzar committed
599
        ~context_unproved_goals_only ~cntexample ~timelimit ~memlimit pr m.metas_goal
François Bobot's avatar
François Bobot committed
600

François Bobot's avatar
François Bobot committed
601

François Bobot's avatar
François Bobot committed
602 603 604 605 606

(***********************************)
(* method: mark proofs as obsolete *)
(***********************************)

607 608 609
let cancel_proof a =
  if not a.proof_archived then
    set_obsolete ~notify a
François Bobot's avatar
François Bobot committed
610 611 612

let cancel = iter_proof_attempt cancel_proof

613 614
(** Set or unset archive *)

615
let set_archive a b = set_archived a b; notify (Proof_attempt a)
616

François Bobot's avatar
François Bobot committed
617 618 619 620 621 622 623 624
(*********************************)
(* method: check existing proofs *)
(*********************************)

type report =
  | Result of Call_provers.prover_result * Call_provers.prover_result
  | CallFailed of exn
  | Prover_not_installed
625
  | Edited_file_absent of string
François Bobot's avatar
François Bobot committed
626 627
  | No_former_result of Call_provers.prover_result

628 629
let push_report report (g,p,limits,r) =
  (g.goal_name,p,limits,r)::report
François Bobot's avatar
François Bobot committed
630 631

let check_external_proof eS eT todo a =
632
  let callback a ap limits old s =
633 634 635
    let g = a.proof_parent in
    match s with
    | Starting ->
636
      Todo.start todo
637
    | MissingFile f ->
638
      Todo._done todo (g, ap, limits, Edited_file_absent f)
639
    | MissingProver ->
640
      Todo._done todo (g, ap, limits, Prover_not_installed)
641 642 643
    | StatusChange (Scheduled | Running) -> ()
    | StatusChange (Interrupted | Unedited | JustEdited) -> assert false
    | StatusChange (InternalFailure e) ->
644
      Todo._done todo (g, ap, limits, CallFailed e)
645 646 647 648 649
    | StatusChange (Done res) ->
      let r =
        match old with
        | None -> No_former_result res
        | Some old -> Result (res, old) in
650
      Todo._done todo (g, ap, limits, r) in
651
  run_external_proof_v2 eS eT a callback
François Bobot's avatar
François Bobot committed
652

653 654 655 656 657 658 659
let rec goal_iter_proof_attempt_with_release ~release f g =
  let iter g = goal_iter_proof_attempt_with_release ~release f g in
  PHprover.iter (fun _ a -> f a) g.goal_external_proofs;
  PHstr.iter (fun _ t -> List.iter iter t.transf_goals) g.goal_transformations;
  Mmetas_args.iter (fun _ t -> iter t.metas_goal) g.goal_metas;
  if release then release_task g

660
let check_all ?(release=false) ?filter eS eT ~callback =
661
  Debug.dprintf debug "[Sched] check all@.%a@." print_session eS.session;
662
  let todo = Todo.create [] push_report callback in
663
  Todo.start todo;
664
  let check_top_goal g =
665 666 667 668 669
    let check a =
      let c = match filter with
        | None -> true
        | Some f -> f a in
      if c then check_external_proof eS eT todo a in
670
    goal_iter_proof_attempt_with_release ~release check g
671 672
  in
  PHstr.iter (fun _ file ->
673 674
      List.iter (fun t ->
          List.iter check_top_goal t.theory_goals)
675 676
        file.file_theories)
    eS.session.session_files;
677
  Todo.stop todo
François Bobot's avatar
François Bobot committed
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
(**********************************)
(* method: replay obsolete proofs *)
(**********************************)

(* in the default context, a proof should be replayed if
   . it was successful or
   . it was just edited
*)
let proof_should_be_replayed a =
  match a.proof_state with
    | Done { Call_provers.pr_answer = Call_provers.Valid }
    | JustEdited -> true
    | _ -> false

let rec replay_on_goal_or_children eS eT
    ~obsolete_only ~context_unproved_goals_only g =
  iter_goal
    (fun a ->
       if not obsolete_only || a.proof_obsolete then
         if not context_unproved_goals_only || proof_should_be_replayed a
         then run_external_proof eS eT a)
    (iter_transf
       (replay_on_goal_or_children eS eT
          ~obsolete_only ~context_unproved_goals_only)
    )
    (iter_metas
       (replay_on_goal_or_children eS eT
          ~obsolete_only ~context_unproved_goals_only)
    )
    g

let replay eS eT ~obsolete_only ~context_unproved_goals_only a =
  match a with
    | Goal g ->
        replay_on_goal_or_children eS eT
          ~obsolete_only ~context_unproved_goals_only g
    | Theory th ->
        List.iter
          (replay_on_goal_or_children eS eT
             ~obsolete_only ~context_unproved_goals_only)
          th.theory_goals
    | File file ->
        List.iter
          (fun th ->
             List.iter
               (replay_on_goal_or_children eS eT
                  ~obsolete_only ~context_unproved_goals_only)
               th.theory_goals)
          file.file_theories
    | Proof_attempt a ->
        replay_on_goal_or_children eS eT
          ~obsolete_only ~context_unproved_goals_only a.proof_parent
    | Transf tr ->
        List.iter
          (replay_on_goal_or_children eS eT
             ~obsolete_only ~context_unproved_goals_only)
          tr.transf_goals
    | Metas m ->
      replay_on_goal_or_children eS eT
        ~obsolete_only ~context_unproved_goals_only m.metas_goal


742 743 744 745
(***********************************)
(* play all                        *)
(***********************************)

746
let rec play_on_goal_and_children eS eT ~timelimit ~memlimit todo l g =
747 748 749 750 751 752 753 754 755
  let timelimit, memlimit, auto_proved =
    PHprover.fold (fun _ pa (timelimit, memlimit, _ as acc) ->
      match pa.proof_edited_as, pa.proof_state with
        | None, Done { Call_provers.pr_answer = Call_provers.Valid } ->
            max timelimit pa.proof_timelimit,
            max memlimit pa.proof_memlimit,
            true
        | _ -> acc)
      g.goal_external_proofs (timelimit, memlimit, false) in
756
  let callback _key status =
757
    if not (running status) then Todo._done todo () in
758 759 760 761
  if auto_proved then begin
    List.iter
      (fun p ->
        Todo.start todo;
762 763 764
      (* eprintf "todo increased to %d@." todo.Todo.todo; *)
      (* eprintf "prover %a on goal %s@." *)
      (*   Whyconf.print_prover p g.goal_name.Ident.id_string; *)
765 766 767
        prover_on_goal eS eT ~callback ~timelimit ~memlimit p g)
      l
  end;
François Bobot's avatar
François Bobot committed
768 769 770 771 772 773 774 775 776
  iter_goal
    (fun _ -> ())
    (iter_transf
       (play_on_goal_and_children eS eT ~timelimit ~memlimit todo l)
    )
    (iter_metas
       (play_on_goal_and_children eS eT ~timelimit ~memlimit todo l)
    )
    g
777 778


779
let play_all eS eT ~callback ~timelimit ~memlimit l =
780 781
  let todo = Todo.create () (fun () _ -> ()) callback in
  Todo.start todo;
782
  PHstr.iter
783
    (fun _ file ->
784 785 786
      List.iter
        (fun th ->
          List.iter
787
            (play_on_goal_and_children eS eT ~timelimit ~memlimit todo l)
788 789
            th.theory_goals)
        file.file_theories)
790
    eS.session.session_files;
791
  Todo.stop todo
792

793

François Bobot's avatar
François Bobot committed
794 795
(** Transformation *)

796
let transformation_on_goal_aux eS tr keep_dumb_transformation g =
797 798
  let gtask = goal_task_or_recover eS g in
  let subgoals = Trans.apply_transform tr eS.env gtask in
799
  let b = keep_dumb_transformation ||
François Bobot's avatar
François Bobot committed
800
    match subgoals with
801
      | [task] -> not (Task.task_equal task gtask)
François Bobot's avatar
François Bobot committed
802 803 804
      | _ -> true
  in
  if b then
805
    let ntr = add_transformation ~init:init_any ~notify ~keygen:O.create eS tr g subgoals in
806 807
    Some ntr
  else None
François Bobot's avatar
François Bobot committed
808

809 810 811 812 813
let transform_goal eS sched ?(keep_dumb_transformation=false)
    ?callback tr g =
  schedule_delayed_action sched
    (fun () -> let ntr = transformation_on_goal_aux eS tr
                 keep_dumb_transformation g in
814
               Opt.apply () callback ntr)
François Bobot's avatar
François Bobot committed
815 816


817 818 819 820 821
let transform_goal_or_children ~context_unproved_goals_only eS sched ?callback
    tr g =
  goal_iter_leaf_goal ~unproved_only:context_unproved_goals_only
    (transform_goal eS sched ~keep_dumb_transformation:false
       ?callback tr) g
François Bobot's avatar
François Bobot committed
822

823
let rec transform eS sched ~context_unproved_goals_only ?callback tr a =
François Bobot's avatar
François Bobot committed
824 825
  match a with
    | Goal g | Proof_attempt {proof_parent = g} ->
826 827 828
      transform_goal_or_children ~context_unproved_goals_only eS sched
        ?callback tr g
    | _ -> iter (transform ~context_unproved_goals_only eS sched ?callback tr) a
François Bobot's avatar
François Bobot committed
829 830 831 832 833

(*****************************)
(* method: edit current goal *)
(*****************************)

834
let edit_proof_v3 ~cntexample eS sched ~default_editor callback a =
835 836
  match find_prover eS a with
  | None ->
Andrei Paskevich's avatar
Andrei Paskevich committed
837
          (* nothing to do
838 839
             TODO: report an non replayable proof if some option is set
          *)
840 841 842 843 844 845 846 847 848 849 850 851
    ()
  | Some(_,npc,a) ->
    let editor =
      match npc.prover_config.Whyconf.editor with
      | "" -> default_editor
      | s ->
        try
          let ed = Whyconf.editor_by_id eS.whyconf s in
          String.concat " "(ed.Whyconf.editor_command ::
                              ed.Whyconf.editor_options)
        with Not_found -> default_editor
    in
852
    let file = update_edit_external_proof ~cntexample eS a in
853
    Debug.dprintf debug "[Editing] goal %s with command '%s' on file %s@."
854
      a.proof_parent.goal_name.Ident.id_string editor file;
855 856
    schedule_edition sched editor file (fun res -> callback a res)

857
let edit_proof ~cntexample eS sched ~default_editor a =
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
  (* check that the state is not Scheduled or Running *)
  if a.proof_archived || running a.proof_state then ()
(*
    info_window `ERROR "Edition already in progress"
*)
  else
    let callback a res =
      match res with
      | Done {Call_provers.pr_answer = Call_provers.Unknown ""} ->
        set_proof_state ~notify ~obsolete:true ~archived:false
          JustEdited a
      | _ ->
        set_proof_state ~notify ~obsolete:false ~archived:false
          res a
    in
873
    edit_proof_v3 ~cntexample eS sched ~default_editor callback a
874

875
let edit_proof_v3 ~cntexample eS sched ~default_editor ~callback a =
876 877 878 879 880 881
  let callback a res =
    match res with
    | Done {Call_provers.pr_answer = Call_provers.Unknown ""} ->
      callback a
    | _ -> ()
  in
882
  edit_proof_v3 ~cntexample eS sched ~default_editor callback a
883

François Bobot's avatar
François Bobot committed
884 885 886 887 888 889 890 891 892 893 894 895 896 897

(*************)
(* removing  *)
(*************)

let remove_proof_attempt (a:O.key proof_attempt) =
  O.remove a.proof_key;
  let notify = (notify : O.key notify) in
  remove_external_proof ~notify a

let remove_transformation t =
  O.remove t.transf_key;
  remove_transformation ~notify t

François Bobot's avatar
François Bobot committed
898 899 900 901
let remove_metas t =
  O.remove t.metas_key;
  remove_metas ~notify t

902 903 904 905 906 907 908 909 910 911 912
(* a proof is removable if
    . it is not in progress and
    . it is obsolete or not successful
*)
let proof_removable a =
  match a.proof_state with
  | Done pr ->
    a.proof_obsolete || pr.Call_provers.pr_answer <> Call_provers.Valid
  | _ -> false


François Bobot's avatar
François Bobot committed
913
let rec clean = function
914
  | Goal g when Opt.inhabited g.goal_verified ->
François Bobot's avatar
François Bobot committed
915
    iter_goal
916
      (fun a -> if proof_removable a then remove_proof_attempt a)
François Bobot's avatar
François Bobot committed
917
      (fun t ->
918
        if not (Opt.inhabited t.transf_verified) then remove_transformation t
François Bobot's avatar
François Bobot committed
919 920
        else transf_iter clean t)
      (fun m ->
921
        if not (Opt.inhabited m.metas_verified) then remove_metas m
François Bobot's avatar
François Bobot committed
922 923
        else metas_iter clean m)
      g
François Bobot's avatar
François Bobot committed
924
  | Goal g ->
François Bobot's avatar
François Bobot committed
925
    (** don't iter on proof_attempt if the goal is not proved *)
926
    iter_goal (fun _ -> ()) (transf_iter clean) (metas_iter clean) g
François Bobot's avatar
François Bobot committed
927 928 929 930 931 932 933 934
  | Proof_attempt a -> clean (Goal a.proof_parent)
  | any -> iter clean any

(**** convert ***)

let convert_unknown_prover =
  Session_tools.convert_unknown_prover ~keygen:O.create

935
  open Strategy
MARCHE Claude's avatar
MARCHE Claude committed
936

937
  let rec exec_strategy ~todo es sched pc strat g =
MARCHE Claude's avatar
MARCHE Claude committed
938 939
    if pc < 0 || pc >= Array.length strat then
      (* halt the strategy *)
940
      Todo._done todo ()
MARCHE Claude's avatar
MARCHE Claude committed
941 942 943 944 945
    else
      match Array.get strat pc with
        | Icall_prover(p,timelimit,memlimit) ->
          let callback _pa res =
            match res with
946
              | Scheduled | Running ->
MARCHE Claude's avatar
MARCHE Claude committed
947 948 949 950
                (* nothing to do yet *)
                ()
              | Done { Call_provers.pr_answer = Call_provers.Valid } ->
                (* proof succeeded, nothing more to do *)
951
                Todo._done todo ()
MARCHE Claude's avatar
MARCHE Claude committed
952 953
              | Interrupted | InternalFailure _ | Done _ ->
                (* proof did not succeed, goto to next step *)
954
                let callback () = exec_strategy ~todo es sched (pc+1) strat g in
MARCHE Claude's avatar
MARCHE Claude committed
955
                schedule_delayed_action sched callback
956
              | Unedited | JustEdited ->
MARCHE Claude's avatar
MARCHE Claude committed
957 958 959 960
                (* should not happen *)
                assert false
          in
          prover_on_goal es sched ~ca