controller_itp.ml 42.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
(********************************************************************)
(*                                                                  *)
(*  The Why3 Verification Platform   /   The Why3 Development Team  *)
(*  Copyright 2010-2017   --   INRIA - CNRS - Paris-Sud University  *)
(*                                                                  *)
(*  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.                           *)
(*                                                                  *)
(********************************************************************)

12
open Format
Clément Fumex's avatar
Clément Fumex committed
13 14
open Session_itp

Sylvain Dailler's avatar
Sylvain Dailler committed
15 16 17 18
let debug_sched = Debug.register_info_flag "scheduler"
  ~desc:"Print@ debugging@ messages@ about@ scheduling@ of@ prover@ calls@ \
         and@ transformation@ applications."

Sylvain Dailler's avatar
Sylvain Dailler committed
19
let debug_call_prover = Debug.lookup_flag "call_prover"
20 21
let default_delay_ms = 100 (* 0.1 seconds *)

Sylvain Dailler's avatar
Sylvain Dailler committed
22

23 24
exception Noprogress

25 26 27
let () = Exn_printer.register
    (fun fmt e ->
      match e with
Sylvain Dailler's avatar
Sylvain Dailler committed
28 29
      | Noprogress ->
          Format.fprintf fmt "The transformation made no progress.\n"
30 31
      | _ -> raise e)

Clément Fumex's avatar
Clément Fumex committed
32 33
(** State of a proof *)
type proof_attempt_status =
34
  | Undone   (** prover was never called *)
35 36 37
  | Scheduled (** external proof attempt is scheduled *)
  | Running (** external proof attempt is in progress *)
  | Done of Call_provers.prover_result (** external proof done *)
38 39
  | Interrupted (** external proof has never completed *)
  | Detached (** parent goal has no task, is detached *)
40 41
  | InternalFailure of exn (** external proof aborted by internal error *)
  | Uninstalled of Whyconf.prover (** prover is uninstalled *)
Clément Fumex's avatar
Clément Fumex committed
42

MARCHE Claude's avatar
MARCHE Claude committed
43 44
let print_status fmt st =
  match st with
45
  | Undone            -> fprintf fmt "Undone"
Sylvain Dailler's avatar
Sylvain Dailler committed
46 47 48 49
  | Scheduled         -> fprintf fmt "Scheduled"
  | Running           -> fprintf fmt "Running"
  | Done r            ->
      fprintf fmt "Done(%a)" Call_provers.print_prover_result r
50 51
  | Interrupted       -> fprintf fmt "Interrupted"
  | Detached          -> fprintf fmt "Detached"
Sylvain Dailler's avatar
Sylvain Dailler committed
52 53 54 55
  | InternalFailure e ->
      fprintf fmt "InternalFailure(%a)" Exn_printer.exn_printer e
  | Uninstalled pr    ->
      fprintf fmt "Prover %a is uninstalled" Whyconf.print_prover pr
MARCHE Claude's avatar
MARCHE Claude committed
56 57

type transformation_status =
58
  | TSscheduled | TSdone of transID | TSfailed of (proofNodeID * exn)
59 60 61 62

let print_trans_status fmt st =
  match st with
  | TSscheduled -> fprintf fmt "TScheduled"
63
  | TSdone _tid -> fprintf fmt "TSdone" (* TODO print tid *)
64
  | TSfailed _e -> fprintf fmt "TSfailed"
Clément Fumex's avatar
Clément Fumex committed
65

66 67 68 69
type strategy_status = STSgoto of proofNodeID * int | STShalt

let print_strategy_status fmt st =
  match st with
Sylvain Dailler's avatar
Sylvain Dailler committed
70 71
  | STSgoto(id,n) ->
      fprintf fmt "goto step %d in proofNode %a" n print_proofNodeID id
72 73 74
  | STShalt -> fprintf fmt "halt"


75
type controller =
Sylvain Dailler's avatar
Sylvain Dailler committed
76
  { mutable controller_session: Session_itp.session;
77
    controller_config : Whyconf.config;
Sylvain Dailler's avatar
Sylvain Dailler committed
78 79 80
    controller_env: Env.env;
    controller_provers:
      (Whyconf.config_prover * Driver.driver) Whyconf.Hprover.t;
81
    controller_strategies : (string * string * Strategy.instruction array) Stdlib.Hstr.t;
82
    controller_running_proof_attempts : unit Hpan.t;
83 84
  }

85 86 87 88 89
let session_max_tasks = ref 1

let set_session_max_tasks n =
  session_max_tasks := n;
  Prove_client.set_max_running_provers n
90

91
let create_controller config env ses =
92 93
  let c =
    {
94
      controller_session = ses;
95 96 97
      controller_config = config;
      controller_env = env;
      controller_provers = Whyconf.Hprover.create 7;
98
      controller_strategies = Stdlib.Hstr.create 7;
99
      controller_running_proof_attempts = Hpan.create 17;
100 101 102 103 104 105
    }
  in
  let provers = Whyconf.get_provers config in
  Whyconf.Mprover.iter
    (fun _ p ->
     try
106
       let d = Whyconf.load_driver (Whyconf.get_main config) env p.Whyconf.driver [] in
107 108
       Whyconf.Hprover.add c.controller_provers p.Whyconf.prover (p,d)
     with e ->
Sylvain Dailler's avatar
Sylvain Dailler committed
109
       Debug.dprintf debug_call_prover
110 111 112 113 114 115
         "[Controller_itp] error while loading driver for prover %a: %a@."
         Whyconf.print_prover p.Whyconf.prover
         Exn_printer.exn_printer e)
    provers;
  c

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

(* Cannot remove a proof_attempt that was scheduled but did not finish yet.
   It can be interrupted though. *)
let removable_proof_attempt c pa =
  try let () = Hpan.find c.controller_running_proof_attempts pa in false
  with Not_found -> true

let any_removable c any =
  match any with
  | APa pa -> removable_proof_attempt c pa
  | _ -> true

(* Check whether the subtree [n] contains an unremovable proof_attempt
   (ie: scheduled or running) *)
let check_removable c (n: any) =
  fold_all_any c.controller_session (fun acc any -> acc && any_removable c any) true n


let remove_subtree ~(notification:notifier) ~(removed:notifier) c (n: any) =
  if check_removable c n then
    Session_itp.remove_subtree ~notification ~removed c.controller_session n
137

138 139 140
(* Get children of any without proofattempts *)
let get_undetached_children_no_pa s any : any list =
  match any with
141
  | AFile f -> List.map (fun th -> ATh th) (file_theories f)
142 143 144
  | ATh th  -> List.map (fun g -> APn g) (theory_goals th)
  | ATn tn  -> List.map (fun pn -> APn pn) (get_sub_tasks s tn)
  | APn pn  -> List.map (fun tn -> ATn tn) (get_transformations s pn)
Sylvain Dailler's avatar
Sylvain Dailler committed
145
  | APa _ -> []
146

147 148


149 150 151 152 153 154 155 156 157 158 159 160
(* printing *)

module PSession = struct

  open Stdlib

  type any =
    | Session
    | File of file
    | Theory of theory
    | Goal of proofNodeID
    | Transf of transID
161
    | ProofAttempt of proof_attempt_node
162 163 164 165 166 167 168 169 170 171

  type 'a t = { tcont : controller;
                tkind : any }

  let decomp x =
    let s = x.tcont.controller_session in
    let n y acc = { x with tkind = y } :: acc in
    match x.tkind with
    | Session -> "", Hstr.fold (fun _ f -> n (File f)) (get_files s) []
    | File f ->
172
       (file_name f),
173
       List.fold_right (fun th -> n (Theory th)) (file_theories f) []
174 175 176
    | Theory th ->
       let id = theory_name th in
       let name = id.Ident.id_string in
177
       let name = if th_proved s th then name^"!" else name^"?" in
178
       name,
Sylvain Dailler's avatar
Sylvain Dailler committed
179 180 181 182
       List.fold_right
         (fun g -> n (Goal g))
         (theory_goals th)
         (List.fold_right (fun g -> n (Goal g)) (theory_detached_goals th) [])
183 184 185
    | Goal id ->
       let gid = get_proof_name s id in
       let name = gid.Ident.id_string in
186
       let name = if pn_proved s id then name^"!" else name^"?" in
187 188 189 190 191 192 193
       let pas = get_proof_attempts s id in
       let trs = get_transformations s id in
       name,
       List.fold_right (fun g -> n (Transf g)) trs
                       (List.fold_right (fun g -> n (ProofAttempt g)) pas [])
    | ProofAttempt pa ->
       Pp.sprintf_wnl "%a%s%s"
Sylvain Dailler's avatar
Sylvain Dailler committed
194 195 196 197 198
          Whyconf.print_prover pa.prover
          (match pa.Session_itp.proof_state with
          | Some { Call_provers.pr_answer = Call_provers.Valid} -> ""
          | _ -> "?")
          (if pa.proof_obsolete then "O" else ""), []
199 200
    | Transf id ->
       let name = get_transf_name s id in
201
       let name = if tn_proved s id then name^"!" else name^"?" in
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
       let sts = get_sub_tasks s id in
       let dsts = get_detached_sub_tasks s id in
       name,
       List.fold_right (fun g -> n (Goal g)) sts
                       (List.fold_right (fun g -> n (Goal g)) dsts [])

end

module P = Print_tree.PMake(PSession)

let print_session fmt c =
  P.print fmt { PSession.tcont = c; PSession.tkind = PSession.Session }

(*********)



219 220 221 222 223 224

(** reload files, associating old proof attempts and transformations
    to the new goals.  old theories and old goals for which we cannot
    find a corresponding new theory resp. old goal are kept, with
    tasks associated to them *)

225
let reload_files (c : controller) ~use_shapes =
226
  let old_ses = c.controller_session in
227
  c.controller_session <- empty_session ~from:old_ses (get_dir old_ses);
228
  try
229
    merge_files ~use_shapes c.controller_env c.controller_session old_ses
230 231 232
  with e ->
    c.controller_session <- old_ses;
    raise e
233 234

let add_file c ?format fname =
235 236 237
  let theories = Session_itp.read_file c.controller_env ?format fname in
  let (_ : file) = add_file_section c.controller_session fname theories format in
  ()
238

239 240

module type Scheduler = sig
241
  val blocking: bool
242
  val multiplier: int
243 244 245 246
  val timeout: ms:int -> (unit -> bool) -> unit
  val idle: prio:int -> (unit -> bool) -> unit
end

247 248
module Make(S : Scheduler) = struct

MARCHE Claude's avatar
MARCHE Claude committed
249 250
let scheduled_proof_attempts = Queue.create ()

251 252 253 254 255 256
let prover_tasks_in_progress = Hashtbl.create 17

(* We need to separate tasks that are edited from proofattempt in progress
   because we are not using why3server for the edited proofs and timeout_handler
   rely on a loop on why3server's results. *)
let prover_tasks_edited = Queue.create ()
MARCHE Claude's avatar
MARCHE Claude committed
257 258 259

let timeout_handler_running = ref false

260

MARCHE Claude's avatar
MARCHE Claude committed
261
let number_of_running_provers = ref 0
262

263 264 265 266
let observer = ref (fun _ _ _ -> ())

let register_observer = (:=) observer

267
module Hprover = Whyconf.Hprover
MARCHE Claude's avatar
MARCHE Claude committed
268

269 270 271 272 273 274 275 276 277 278 279 280 281
(* calling provers, with limits adaptation

 *)


(* 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 ~interactive ~use_steps limits a =
  let t, m, s =
    let timelimit = limits.Call_provers.limit_time in
    let memlimit = limits.Call_provers.limit_mem in
282
    let steplimit = limits.Call_provers.limit_steps in
283 284 285 286 287 288 289 290
    match a.proof_state with
    | Some { Call_provers.pr_answer = r;
             Call_provers.pr_time = t;
             Call_provers.pr_steps = s } ->
       (* 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 t = truncate (1.0 +. 2.0 *. t) in
291
       let increased_time = if interactive then 0
292 293 294 295 296
                            else max timelimit (min t (2 * timelimit)) in
       (* increased mem limit is just 1.5 times the previous mem limit *)
       let increased_mem = if interactive then 0 else 3 * memlimit / 2 in
       begin
         match r with
297 298
         | Call_provers.OutOfMemory -> increased_time, memlimit, steplimit
         | Call_provers.Timeout -> timelimit, increased_mem, steplimit
299 300
         | Call_provers.Valid ->
            let steplimit =
301 302 303 304 305 306 307 308 309
              if use_steps && not a.proof_obsolete then
                (* We need to allow at least one more step than what was used to
                   prove the same statement. Otherwise, the prover run out of
                   steps: this happens with cvc4 on some very fast proofs
                   (steps = 28).
                *)
                s + 1
              else
                0
310 311 312 313
            in
            increased_time, increased_mem, steplimit
         | Call_provers.Unknown _
         | Call_provers.StepLimitExceeded
314
         | Call_provers.Invalid -> increased_time, increased_mem, steplimit
315 316 317
         | Call_provers.Failure _
         | Call_provers.HighFailure ->
            (* correct ? failures are supposed to appear quickly anyway... *)
318
            timelimit, memlimit, steplimit
319 320
       end
    | None when interactive -> 0, 0, 0
321
    | None -> timelimit, memlimit, steplimit
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
  in
  { Call_provers.limit_time = t; limit_mem = m; limit_steps = s }

let group_answer a =
  match a with
  | Call_provers.OutOfMemory
  | Call_provers.Unknown _
  | Call_provers.Timeout -> Call_provers.Timeout
  | _ -> a

let fuzzy_proof_time nres ores =
  let ansold = ores.Call_provers.pr_answer in
  let told = ores.Call_provers.pr_time in
  let ansnew = nres.Call_provers.pr_answer in
  let tnew = nres.Call_provers.pr_time in
  if group_answer ansold = group_answer ansnew &&
       tnew >= told *. 0.9 -. 0.1 && tnew <= told *. 1.1 +. 0.1
  then { nres with Call_provers.pr_time = told }
  else nres


let build_prover_call ?proof_script ~cntexample c id pr limit callback ores =
344
  let (config_pr,driver) = Hprover.find c.controller_provers pr in
345
  let with_steps = Call_provers.(limit.limit_steps <> empty_limit.limit_steps) in
346
  let command =
347
    Whyconf.get_complete_command config_pr ~with_steps in
348 349
  try
    let task = Session_itp.get_raw_task c.controller_session id in
350 351 352
    Debug.dprintf debug_sched "[build_prover_call] Script file = %a@."
                  (Pp.print_option Pp.string) proof_script;
    let inplace = config_pr.Whyconf.in_place in
353 354 355 356 357 358 359 360 361
    try
      let call =
        Driver.prove_task ?old:proof_script ~cntexample:cntexample ~inplace ~command
                          ~limit driver task
      in
      let pa = (c.controller_session,id,pr,callback,false,call,ores) in
      Hashtbl.replace prover_tasks_in_progress call pa
    with Sys_error _ as e ->
      callback (InternalFailure e)
362 363
  with Not_found (* goal has no task, it is detached *) ->
       callback Detached
MARCHE Claude's avatar
MARCHE Claude committed
364

365 366
let update_observer () =
  let scheduled = Queue.length scheduled_proof_attempts in
367
  let waiting_or_running = Hashtbl.length prover_tasks_in_progress in
368 369 370
  let running = !number_of_running_provers in
  !observer scheduled (waiting_or_running - running) running

MARCHE Claude's avatar
MARCHE Claude committed
371 372
let timeout_handler () =
  (* examine all the prover tasks in progress *)
373 374 375
  (* When no tasks are there, probably no tasks were scheduled and the server
     was not launched so getting results could fail. *)
  if Hashtbl.length prover_tasks_in_progress != 0 then begin
376
    let results = Call_provers.forward_results ~blocking:S.blocking in
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
    while not (Queue.is_empty results) do
      let (call, prover_update) = Queue.pop results in
      let c = try Some (Hashtbl.find prover_tasks_in_progress call)
        with Not_found -> None in
      match c with
      | None -> () (* we do nothing. We probably received ProverStarted after
                      ProverFinished because what is sent to and received from
                      the server is not ordered. *)
      | Some c ->
      begin
        let (ses,id,pr,callback,started,call,ores) = c in

        match prover_update with
        | Call_provers.NoUpdates -> ()
        | Call_provers.ProverStarted ->
            assert (not started);
            callback Running;
            incr number_of_running_provers;
            Hashtbl.replace prover_tasks_in_progress call
              (ses,id,pr,callback,true,call,ores)
        | Call_provers.ProverFinished res ->
            Hashtbl.remove prover_tasks_in_progress call;
            if started then decr number_of_running_provers;
            let res = Opt.fold fuzzy_proof_time res ores in
            (* inform the callback *)
            callback (Done res)
        | Call_provers.ProverInterrupted ->
            Hashtbl.remove prover_tasks_in_progress call;
            if started then decr number_of_running_provers;
            (* inform the callback *)
            callback (Interrupted)
        | Call_provers.InternalFailure exn ->
            Hashtbl.remove prover_tasks_in_progress call;
            if started then decr number_of_running_provers;
            (* inform the callback *)
            callback (InternalFailure (exn))
      end
    done
  end;

417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
  (* When blocking is activated, we are in script mode and we don't want editors
     to be launched so we don't need to go in this loop. *)
  if not S.blocking then begin
    (* Check for editor calls which are not finished *)
    let q = Queue.create () in
    while not (Queue.is_empty prover_tasks_edited) do
      (* call is an EditorCall *)
      let (_ses,_id,_pr,callback,_started,call,ores) as c =
        Queue.pop prover_tasks_edited in
      let prover_update = Call_provers.query_call call in
      match prover_update with
      | Call_provers.NoUpdates -> Queue.add c q
      | Call_provers.ProverFinished res ->
          let res = Opt.fold fuzzy_proof_time res ores in
          (* inform the callback *)
          callback (Done res)
      | _ -> assert (false) (* An edition can only return Noupdates or finished *)
    done;
    Queue.transfer q prover_tasks_edited;
  end;
437 438

  (* if the number of prover tasks is less than S.multiplier times the maximum
MARCHE Claude's avatar
MARCHE Claude committed
439 440
     number of running provers, then we heuristically decide to add
     more tasks *)
441 442
  begin
    try
443
      for _i = Hashtbl.length prover_tasks_in_progress
444
          to S.multiplier * !session_max_tasks do
445 446
        let (c,id,pr,limit,proof_script,callback,cntexample,ores) =
          Queue.pop scheduled_proof_attempts in
447
        try
448
          build_prover_call ?proof_script ~cntexample c id pr limit callback ores
449 450 451 452 453
        with e when not (Debug.test_flag Debug.stack_trace) ->
          callback (InternalFailure e)
      done
  with Queue.Empty -> ()
  end;
454
  update_observer ();
455
  true
MARCHE Claude's avatar
MARCHE Claude committed
456

457
let interrupt () =
458 459 460 461 462 463 464
  (* Interrupt provers *)
  Hashtbl.iter (fun _k e ->
    let (_, _, _, callback, _, _, _) = e in callback Interrupted)
    prover_tasks_in_progress;
  Hashtbl.clear prover_tasks_in_progress;
  (* Do not interrupt editors
  while not (Queue.is_empty prover_tasks_edited) do
465
    let (_ses,_id,_pr,callback,_started,_call,_ores) =
466
      Queue.pop prover_tasks_edited in
467 468
    callback Interrupted
  done;
469
  *)
470 471
  number_of_running_provers := 0;
  while not (Queue.is_empty scheduled_proof_attempts) do
472 473
    let (_c,_id,_pr,_limit,_proof_script,callback,_cntexample,_ores) =
      Queue.pop scheduled_proof_attempts in
474 475 476 477
    callback Interrupted
  done;
  !observer 0 0 0

MARCHE Claude's avatar
MARCHE Claude committed
478 479 480 481
let run_timeout_handler () =
  if not !timeout_handler_running then
    begin
      timeout_handler_running := true;
482
      S.timeout ~ms:default_delay_ms timeout_handler;
MARCHE Claude's avatar
MARCHE Claude committed
483 484
    end

485 486 487 488
let schedule_proof_attempt c id pr
                           ~counterexmp ~limit ~callback ~notification =
  let ses = c.controller_session in
  let callback panid s =
489 490 491 492 493 494 495 496 497 498
    begin
      match s with
      | Scheduled ->
         Hpan.add c.controller_running_proof_attempts panid ();
         update_goal_node notification ses id
      | Running -> update_goal_node notification ses id
      | Done res ->
         Hpan.remove c.controller_running_proof_attempts panid;
         update_proof_attempt ~obsolete:false notification ses id pr res;
         update_goal_node notification ses id
499 500
      | Interrupted
      | InternalFailure _ ->
501 502 503 504 505
         Hpan.remove c.controller_running_proof_attempts panid;
         (* what to do ?
         update_proof_attempt ~obsolete:false notification ses id pr res;
          *)
         update_goal_node notification ses id
506 507
      | Detached -> assert false
      | Uninstalled _ -> assert false
508
      | Undone -> assert false
509
    end;
510 511 512
    callback panid s
  in
  let adaptlimit,ores,proof_script =
513
    try
514
      let h = get_proof_attempt_ids ses id in
515
      let pa = Hprover.find h pr in
516
      let a = get_proof_attempt_node ses pa in
517 518 519 520 521
      let old_res = a.proof_state in
      let config_pr,_ = Hprover.find c.controller_provers pr in
      let interactive = config_pr.Whyconf.interactive in
      let use_steps = Call_provers.(limit.limit_steps <> empty_limit.limit_steps) in
      let limit = adapt_limits ~interactive ~use_steps limit a in
522 523 524 525 526 527
      let script = Opt.map (fun s ->
                            Debug.dprintf debug_sched "Script file = %s@." s;
                            Filename.concat (get_dir ses) s) a.proof_script
      in
      limit, old_res, script
    with Not_found | Session_itp.BadID -> limit,None,None
528
  in
529
  let panid = graft_proof_attempt ~limit ses id pr in
530
  Queue.add (c,id,pr,adaptlimit,proof_script,callback panid,counterexmp,ores)
531
            scheduled_proof_attempts;
532
  callback panid Scheduled;
MARCHE Claude's avatar
MARCHE Claude committed
533
  run_timeout_handler ()
Clément Fumex's avatar
Clément Fumex committed
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

(* replay *)


let find_prover notification c goal_id pr =
  if Hprover.mem c.controller_provers pr then Some pr else
   match Whyconf.get_prover_upgrade_policy c.controller_config pr with
   | exception Not_found -> None
   | Whyconf.CPU_keep -> None
   | Whyconf.CPU_upgrade new_pr ->
      (* does a proof using new_pr already exists ? *)
      if Hprover.mem (get_proof_attempt_ids c.controller_session goal_id) new_pr
      then (* yes, then we do nothing *)
        None
      else
        begin
          (* we modify the prover in-place *)
          Session_itp.change_prover notification c.controller_session goal_id pr new_pr;
          Some new_pr
        end
   | Whyconf.CPU_duplicate _new_pr ->
      assert false (* TODO *)
(*
      (* does a proof using new_p already exists ? *)
      if Hprover.mem (goal_external_proofs parid) new_pr
      then (* yes, then we do nothing *)
        None
      else
        begin
          (* we duplicate the proof_attempt *)
          let new_a = copy_external_proof
                        ~notify ~keygen:O.create ~prover:new_p ~env_session:eS a
          in
          Some new_pr
        end
*)

573
let replay_proof_attempt c pr limit (parid: proofNodeID) id ~callback ~notification =
574 575 576 577 578
  (* The replay can be done on a different machine so we need
     to check more things before giving the attempt to the scheduler *)
  match find_prover notification c parid pr with
  | None -> callback id (Uninstalled pr)
  | Some pr ->
579 580
     try
       let _ = get_raw_task c.controller_session parid in
581
       schedule_proof_attempt c parid pr ~counterexmp:false ~limit ~callback ~notification
582 583
     with Not_found ->
       callback id Detached
584 585


586 587
(*** { 2 edition of proof scripts} *)

Sylvain Dailler's avatar
Sylvain Dailler committed
588 589 590
(* create the path to a file for saving the external proof script *)
let create_file_rel_path c pr pn =
  let session = c.controller_session in
591
  let driver = snd (Hprover.find c.controller_provers pr) in
592
  let task = Session_itp.get_raw_task session pn in
Sylvain Dailler's avatar
Sylvain Dailler committed
593 594 595 596
  let session_dir = Session_itp.get_dir session in
  let th = get_encapsulating_theory session (APn pn) in
  let th_name = (Session_itp.theory_name th).Ident.id_string in
  let f = get_encapsulating_file session (ATh th) in
597
  let fn = Filename.chop_extension (Filename.basename (file_name f)) in
Sylvain Dailler's avatar
Sylvain Dailler committed
598 599 600 601 602 603
  let file = Driver.file_of_task driver fn th_name task in
  let file = Filename.concat session_dir file in
  let file = Sysutil.uniquify file in
  let file = Sysutil.relativize_filename session_dir file in
  file

604
let prepare_edition c ?file pn pr ~notification =
Sylvain Dailler's avatar
Sylvain Dailler committed
605
  let session = c.controller_session in
606 607 608
  let proof_attempts_id = get_proof_attempt_ids session pn in
  let panid =
    try
609 610 611 612 613 614 615 616 617 618 619 620 621 622
      let panid = Hprover.find proof_attempts_id pr in
      (* if no proof script yet, we need to add one
         it happens e.g when editing a file for an automatic prover
       *)
      let pa = get_proof_attempt_node session panid in
      match pa.proof_script with
      | None ->
         let file = match file with
           | Some f -> f
           | None -> create_file_rel_path c pr pn
         in
         set_proof_script pa file;
         panid
      | Some _ -> panid
623
    with Not_found ->
624 625 626 627
      let file = match file with
        | Some f -> f
        | None -> create_file_rel_path c pr pn
      in
628 629
      let limit = Call_provers.empty_limit in
      graft_proof_attempt session pn pr ~file ~limit
Sylvain Dailler's avatar
Sylvain Dailler committed
630
  in
631
  update_goal_node notification session pn;
632 633 634
  let pa = get_proof_attempt_node session panid in
  let file = Opt.get pa.proof_script in
  let session_dir = Session_itp.get_dir session in
Sylvain Dailler's avatar
Sylvain Dailler committed
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
  let file = Filename.concat session_dir file in
  let old =
    if Sys.file_exists file
    then
      begin
        let backup = file ^ ".bak" in
        if Sys.file_exists backup
        then Sys.remove backup;
        Sys.rename file backup;
        Some(open_in backup)
      end
    else None
  in
  let ch = open_out file in
  let fmt = formatter_of_out_channel ch in
650 651
  let task = Session_itp.get_raw_task session pn in
  let driver = snd (Hprover.find c.controller_provers pr) in
Sylvain Dailler's avatar
Sylvain Dailler committed
652 653 654
  Driver.print_task ~cntexample:false ?old driver fmt task;
  Opt.iter close_in old;
  close_out ch;
655
  panid,file
Sylvain Dailler's avatar
Sylvain Dailler committed
656 657 658

exception Editor_not_found

659
let schedule_edition c id pr ~callback ~notification =
Sylvain Dailler's avatar
Sylvain Dailler committed
660 661 662 663
  Debug.dprintf debug_sched "[Sched] Scheduling an edition@.";
  let config = c.controller_config in
  let session = c.controller_session in
  let prover_conf = Whyconf.get_prover_config config pr in
664
  (* Make sure editor exists. Fails otherwise *)
Sylvain Dailler's avatar
Sylvain Dailler committed
665 666
  let editor =
    match prover_conf.Whyconf.editor with
667
    | "" -> raise Editor_not_found
Sylvain Dailler's avatar
Sylvain Dailler committed
668
    | s ->
669 670 671 672 673
       try
         let ed = Whyconf.editor_by_id config s in
         String.concat " " (ed.Whyconf.editor_command ::
                              ed.Whyconf.editor_options)
       with Not_found -> raise Editor_not_found
Sylvain Dailler's avatar
Sylvain Dailler committed
674
  in
675
  let panid,file = prepare_edition c id pr ~notification in
676
  (* Notification node *)
677 678 679
  let callback panid s =
    begin
      match s with
680
      | Running -> ()
681 682
      | Done res ->
         (* set obsolete to true since we do not know if the manual
683
            proof was completed or not *)
684 685 686 687 688
         Debug.dprintf debug_sched
                       "Setting status of %a under parent %a to obsolete@."
                       print_proofAttemptID panid print_proofNodeID id;
         update_proof_attempt ~obsolete:true notification session id pr res;
         update_goal_node notification session id
689 690 691 692
      | Interrupted
      | InternalFailure _ ->
         update_goal_node notification session id
      | Undone | Detached | Uninstalled _ | Scheduled -> assert false
693 694
    end;
    callback panid s
695
  in
Sylvain Dailler's avatar
Sylvain Dailler committed
696
  Debug.dprintf debug_sched "[Editing] goal %s with command '%s' on file %s@."
697 698
                (Session_itp.get_proof_name session id).Ident.id_string
                editor file;
699 700
  let call = Call_provers.call_editor ~command:editor file in
  callback panid Running;
701
  Queue.add (c.controller_session,id,pr,callback panid,false,call,None)
702
            prover_tasks_edited;
703
  run_timeout_handler ()
704

705 706
(*** { 2 transformations} *)

707 708 709 710 711 712 713 714 715
let schedule_transformation c id name args ~callback ~notification =
  let callback s =
    begin match s with
          | TSdone tid -> update_trans_node notification c.controller_session tid
          | TSscheduled
          | TSfailed _ -> ()
    end;
    callback s
  in
716
  let apply_trans () =
717 718
    begin
      try
719 720 721
        let subtasks =
          apply_trans_to_goal ~allow_no_effect:false
                              c.controller_session c.controller_env name args id
722
        in
723 724 725 726 727 728
        let tid = graft_transf c.controller_session id name args subtasks in
        callback (TSdone tid)
      with
      | Exit ->
         (* if result is same as input task, consider it as a failure *)
         callback (TSfailed (id, Noprogress))
729
      | e (* when not (Debug.test_flag Debug.stack_trace) *) ->
730
          (* "@[Exception raised in Session_itp.apply_trans_to_goal %s:@ %a@.@]"
731
          name Exn_printer.exn_printer e; TODO *)
732
        callback (TSfailed (id, e))
733 734
    end;
    false
735 736 737
  in
  S.idle ~prio:0 apply_trans;
  callback TSscheduled
Clément Fumex's avatar
Clément Fumex committed
738

739 740 741

open Strategy

Sylvain Dailler's avatar
Sylvain Dailler committed
742
let run_strategy_on_goal
743
    c id strat ~counterexmp ~callback_pa ~callback_tr ~callback ~notification =
744 745 746 747 748 749
  let rec exec_strategy pc strat g =
    if pc < 0 || pc >= Array.length strat then
      callback STShalt
    else
      match Array.get strat pc with
      | Icall_prover(p,timelimit,memlimit) ->
MARCHE Claude's avatar
MARCHE Claude committed
750 751
         let callback panid res =
           callback_pa panid res;
752 753 754 755 756
           match res with
           | Scheduled | Running -> (* nothing to do yet *) ()
           | Done { Call_provers.pr_answer = Call_provers.Valid } ->
              (* proof succeeded, nothing more to do *)
              callback STShalt
757 758
           | Interrupted | InternalFailure _ ->
              callback STShalt
759 760 761 762 763
           | Done _ ->
              (* proof did not succeed, goto to next step *)
              callback (STSgoto (g,pc+1));
              let run_next () = exec_strategy (pc+1) strat g; false in
              S.idle ~prio:0 run_next
764
           | Undone | Detached | Uninstalled _ ->
765 766 767 768 769 770
                         (* should not happen *)
                         assert false
         in
         let limit = { Call_provers.empty_limit with
                       Call_provers.limit_time = timelimit;
                       limit_mem  = memlimit} in
771
         schedule_proof_attempt c g p ~counterexmp ~limit ~callback ~notification
772 773
      | Itransform(trname,pcsuccess) ->
         let callback ntr =
774
           callback_tr trname [] ntr;
775
           match ntr with
776
           | TSfailed _e -> (* transformation failed *)
777 778 779 780 781 782 783 784 785 786 787 788
              callback (STSgoto (g,pc+1));
              let run_next () = exec_strategy (pc+1) strat g; false in
              S.idle ~prio:0 run_next
           | TSscheduled -> ()
           | TSdone tid ->
              List.iter
                (fun g ->
                 callback (STSgoto (g,pcsuccess));
                 let run_next () =
                   exec_strategy pcsuccess strat g; false
                 in
                 S.idle ~prio:0 run_next)
MARCHE Claude's avatar
MARCHE Claude committed
789
                (get_sub_tasks c.controller_session tid)
790
         in
791
         schedule_transformation c g trname [] ~callback ~notification
792 793 794 795 796 797
      | Igoto pc ->
         callback (STSgoto (g,pc));
         exec_strategy pc strat g
  in
  exec_strategy 0 strat id

Sylvain Dailler's avatar
Sylvain Dailler committed
798 799
let schedule_pa_with_same_arguments
    c (pa: proof_attempt_node) (pn: proofNodeID) ~callback ~notification =
Sylvain Dailler's avatar
Sylvain Dailler committed
800 801 802 803
  let prover = pa.prover in
  let limit = pa.limit in
  schedule_proof_attempt c pn prover ~limit ~callback ~notification

Sylvain Dailler's avatar
Sylvain Dailler committed
804 805
let schedule_tr_with_same_arguments
    c (tr: transID) (pn: proofNodeID) ~callback ~notification =
Sylvain Dailler's avatar
Sylvain Dailler committed
806 807 808
  let s = c.controller_session in
  let args = get_transf_args s tr in
  let name = get_transf_name s tr in
809
  let callback = callback name args in
Sylvain Dailler's avatar
Sylvain Dailler committed
810 811
  schedule_transformation c pn name args ~callback ~notification

MARCHE Claude's avatar
MARCHE Claude committed
812 813 814 815 816
let proof_is_complete pa =
  match pa.Session_itp.proof_state with
  | None -> false
  | Some pr ->
     not pa.Session_itp.proof_obsolete &&
817
       Call_provers.(pr.pr_answer = Valid)
MARCHE Claude's avatar
MARCHE Claude committed
818

819 820 821
let clean_session c ~removed =
  (* clean should not change proved status *)
  let notification _ = assert false in
822
  let s = c.controller_session in
823 824 825 826 827
  (* This function is applied on leafs first for the case of removes *)
  Session_itp.fold_all_session s
    (fun () any ->
      (match any with
      | APa pa ->
828 829
        let pa = Session_itp.get_proof_attempt_node s pa in
        if pn_proved s pa.parent then
MARCHE Claude's avatar
MARCHE Claude committed
830
          if not (proof_is_complete pa) then
831
            remove_subtree ~notification ~removed c any
832
      | ATn tn ->
833 834
        let pn = get_trans_parent s tn in
        if pn_proved s pn && not (tn_proved s tn) then
835
          remove_subtree ~notification ~removed c (ATn tn)
836
      | _ -> ())) ()
837

838 839
(* This function folds on any subelements of given node and tries to mark all
   proof attempts it encounters *)
840
let mark_as_obsolete ~notification c any =
841
  let s = c.controller_session in
842
  (* Case for proof attempt only *)
843
  let mark_as_obsolete_pa n =
844 845
    let parent = get_proof_attempt_parent s n in
    Session_itp.mark_obsolete s n;
846
    notification (APa n);
847
    update_goal_node notification s parent
848 849 850
  in
  fold_all_any s
    (fun () any -> match any with
851
    | APa n -> mark_as_obsolete_pa n
852
    | _ -> ()) () any
853

Sylvain Dailler's avatar
Sylvain Dailler committed
854 855 856 857 858 859 860 861 862 863 864 865 866 867
exception BadCopyPaste

(* Reproduce the transformation made on node on an other one *)
let rec copy_paste ~notification ~callback_pa ~callback_tr c from_any to_any =
  let s = c.controller_session in
  if (not (is_below s from_any to_any) &&
      not (is_below s to_any from_any)) then
    match from_any, to_any with
    | AFile _, AFile _ ->
        raise BadCopyPaste
    | ATh _from_th, ATh _to_th ->
        raise BadCopyPaste
    | APn from_pn, APn to_pn ->
      let from_pa_list = get_proof_attempts s from_pn in
868
      List.iter (fun x -> schedule_pa_with_same_arguments c x to_pn ~counterexmp:false
Sylvain Dailler's avatar
Sylvain Dailler committed
869 870
          ~callback:callback_pa ~notification) from_pa_list;
      let from_tr_list = get_transformations s from_pn in
871
      let callback x tr args st = callback_tr tr args st;
Sylvain Dailler's avatar
Sylvain Dailler committed
872
        match st with
Sylvain Dailler's avatar
Sylvain Dailler committed
873 874
        | TSdone tid ->
          copy_paste c (ATn x) (ATn tid) ~notification ~callback_pa ~callback_tr
Sylvain Dailler's avatar
Sylvain Dailler committed
875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
        | _ -> ()
      in
      List.iter (fun x -> schedule_tr_with_same_arguments c x to_pn
          ~callback:(callback x) ~notification) from_tr_list
    | ATn from_tn, ATn to_tn ->
        let from_tn_list = get_sub_tasks s from_tn in
        let to_tn_list = get_sub_tasks s to_tn in
        if (List.length from_tn_list = List.length to_tn_list) then
          List.iter2 (fun x y -> copy_paste c (APn x) (APn y)
              ~notification ~callback_pa ~callback_tr) from_tn_list to_tn_list
    | _ -> raise BadCopyPaste


let copy_detached ~copy c from_any =
  match from_any with
  | APn from_pn ->
    begin
      let pn_id = copy_proof_node_as_detached c.controller_session from_pn in
      let parent = get_any_parent c.controller_session from_any in
      match parent with
895
      | None -> raise (BadCopyDetached "copy_detached no parent")
Sylvain Dailler's avatar
Sylvain Dailler committed
896 897
      | Some parent ->
          copy ~parent (APn pn_id);
Sylvain Dailler's avatar
Sylvain Dailler committed
898 899
          copy_structure
            ~notification:copy c.controller_session (APn from_pn) (APn pn_id)
Sylvain Dailler's avatar
Sylvain Dailler committed
900
    end
901 902
  (* Only goal can be detached *)
  | _ -> raise (BadCopyDetached "copy_detached. Can only copy goal")
Sylvain Dailler's avatar
Sylvain Dailler committed
903

MARCHE Claude's avatar
MARCHE Claude committed
904 905 906 907 908 909 910 911 912 913 914







(***************** {2 replay} ****************)



915 916 917 918
type report =
  | Result of Call_provers.prover_result * Call_provers.prover_result
  (** Result(new_result,old_result) *)
  | CallFailed of exn
919
  | Replay_interrupted
920 921 922 923 924 925 926 927 928 929 930
  | Prover_not_installed
  | Edited_file_absent of string
  | No_former_result of Call_provers.prover_result

(* TODO find a better way to print it *)
let print_report fmt (r: report) =
  match r with
  | Result (new_r, old_r) ->
    Format.fprintf fmt "new_result = %a, old_result = %a@."
      Call_provers.print_prover_result new_r
      Call_provers.print_prover_result old_r
931 932 933 934
  | CallFailed e ->
    Format.fprintf fmt "Callfailed %a@." Exn_printer.exn_printer e
  | Replay_interrupted ->
    Format.fprintf fmt "Interrupted@."
935 936 937 938 939 940 941 942 943
  | Prover_not_installed ->
    Format.fprintf fmt "Prover not installed@."
  | Edited_file_absent _ ->
    Format.fprintf fmt "No edited file@."
  | No_former_result new_r ->
    Format.fprintf fmt "new_result = %a, no former result@."
      Call_provers.print_prover_result new_r

(* TODO to be removed when we have a better way to print *)
944
let replay_print fmt (lr: (proofNodeID * Whyconf.prover * Call_provers.resource_limit * report) list) =
945 946
  let pp_elem fmt (id, pr, rl, report) =
    fprintf fmt "ProofNodeID: %d, Prover: %a, Timelimit?: %d, Result: %a@."
Sylvain Dailler's avatar
Sylvain Dailler committed
947 948
      (Obj.magic id) Whyconf.print_prover pr
      rl.Call_provers.limit_time print_report report
949
  in
950
  Format.fprintf fmt "%a@." (Pp.print_list Pp.newline pp_elem) lr
951

952 953
let replay ?(obsolete_only=true) ?(use_steps=false)
           c ~callback ~notification ~final_callback =
954

955
  let craft_report count s r id pr limits pa =
956
    match s with
957
    | Scheduled | Running -> ()
958
    | Undone | Interrupted ->
959
       decr count;
960
       r := (id, pr, limits, Replay_interrupted ) :: !r
961
    | Done new_r ->
962
       decr count;
963 964 965 966
        (match pa.Session_itp.proof_state with
        | None -> (r := (id, pr, limits, No_former_result new_r) :: !r)
        | Some old_r -> r := (id, pr, limits, Result (new_r, old_r)) :: !r)
    | InternalFailure e ->
967
       decr count;
968
        r := (id, pr, limits, CallFailed (e)) :: !r
969 970 971
    | Uninstalled _ ->
       decr count;
       r := (id, pr, limits, Prover_not_installed) :: !r;
972
    | Detached -> decr count
973
  in
974 975 976 977 978 979 980 981

  let session = c.controller_session in
  let count = ref 0 in
  let report = ref [] in

  (* TODO count the number of node in a more efficient way *)
  (* Counting the number of proof_attempt to print report only once *)
  Session_itp.session_iter_proof_attempt
982
    (fun _ pa -> if pa.proof_obsolete || not obsolete_only then incr count) session;
983 984

  (* Replaying function *)
985
  let replay_pa id pa =
986 987 988 989
    if pa.proof_obsolete || not obsolete_only then
      begin
        let parid = pa.parent in
        let pr = pa.prover in
990 991 992 993
        (* TODO: if pr is not installed, lookup for a replacement policy
         OR: delegate this work to the replay_proof_attempt function *)
        (* If use_steps, we give only steps as a limit
         TODO: steps should not be used if prover was replaced above *)
994 995 996 997 998 999
        let limit =
          if use_steps then
            Call_provers.{empty_limit with limit_steps = pa.limit.limit_steps}
          else
            Call_provers.{ pa.limit with limit_steps = empty_limit.limit_steps }
        in
1000
        replay_proof_attempt c pr limit parid id
1001 1002 1003 1004 1005 1006
                             ~callback:(fun id s ->
                                        craft_report count s report parid pr limit pa;
                                        callback id s;
                                        if !count = 0 then final_callback !report)
                             ~notification
      end in
1007

1008
  if !count = 0 then final_callback !report else
1009
  (* Calling replay on all the proof_attempts of the session *)
1010
  Session_itp.session_iter_proof_attempt replay_pa session
1011

MARCHE Claude's avatar
MARCHE Claude committed
1012 1013 1014 1015 1016 1017 1018 1019 1020



(*************** bisect **********)



let debug = Debug.register_flag ~desc:"Task bisection" "bisect"

1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
let create_rem_list =
  let b = Buffer.create 17 in
  fun rem ->
  Buffer.clear b;
  let add pr id =
    if Buffer.length b > 0 then Buffer.add_char b ',';
    Buffer.add_string b (Pp.string_of pr id)
  in
  let remove_ts ts = add Pretty.print_ts ts in
  let remove_ls ls = add Pretty.print_ls ls in
  let remove_pr pr = add Pretty.print_pr pr in
  Ty.Sts.iter remove_ts rem.Eliminate_definition.rem_ts;
  Term.Sls.iter remove_ls rem.Eliminate_definition.rem_ls;
  Decl.Spr.iter remove_pr rem.Eliminate_definition.rem_pr;
  Buffer.contents b
MARCHE Claude's avatar
MARCHE Claude committed
1036 1037


1038
let bisect_proof_attempt ~callback_tr ~callback_pa ~notification ~removed c pa_id =
MARCHE Claude's avatar
MARCHE Claude committed
1039 1040
  let ses = c.controller_session in
  let pa = get_proof_attempt_node ses pa_id in
MARCHE Claude's avatar
MARCHE Claude committed
1041 1042
  if not (proof_is_complete pa) then
    invalid_arg "bisect: proof attempt should be valid";
MARCHE Claude's avatar
MARCHE Claude committed
1043 1044 1045 1046 1047 1048
  let goal_id = pa.parent in
  let prover = pa.prover in
  let limit = { pa.limit with
                Call_provers.limit_steps =
                  Call_provers.empty_limit.Call_provers.limit_steps }
  in
1049
  let timelimit = ref limit.Call_provers.limit_time in
MARCHE Claude's avatar
MARCHE Claude committed
1050 1051
  let set_timelimit res =
    timelimit := 1 + (int_of_float (floor res.Call_provers.pr_time)) in
MARCHE Claude's avatar
MARCHE Claude committed
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
  let bisect_end rem =
    if Decl.Spr.is_empty rem.Eliminate_definition.rem_pr &&
         Term.Sls.is_empty rem.Eliminate_definition.rem_ls &&
           Ty.Sts.is_empty rem.Eliminate_definition.rem_ts
    then
      Debug.dprintf debug "Bisecting didn't reduce the task.@."
    else
      begin
        Debug.dprintf debug "Bisecting done.@.";
        (* apply again the transformation *)
1062 1063 1064 1065
        let rem = create_rem_list rem in
        let callback st =
          callback_tr "remove" [rem] st;
          begin match st with
MARCHE Claude's avatar
MARCHE Claude committed
1066 1067 1068 1069 1070 1071
          | TSscheduled -> ()
          | TSfailed _ -> assert false
          | TSdone trid ->
             match get_sub_tasks ses trid with
             | [pn] ->
                let limit = { limit with Call_provers.limit_time = !timelimit; } in
1072 1073 1074
                let callback paid st =
                  callback_pa paid st;
                  begin match st with
MARCHE Claude's avatar
MARCHE Claude committed
1075 1076
                  | Scheduled | Running -> ()
                  | Detached | Uninstalled _ -> assert false
1077
                  | Undone | Interrupted -> Debug.dprintf debug "Bisecting interrupted.@."
MARCHE Claude's avatar
MARCHE Claude committed
1078 1079 1080 1081 1082 1083 1084
                  | InternalFailure exn ->
                     (* Perhaps the test can be considered false in this case? *)
                     Debug.dprintf debug "Bisecting interrupted by an error %a.@."
                                   Exn_printer.exn_printer exn
                  | Done res ->
                     assert (res.Call_provers.pr_answer = Call_provers.Valid);
                     Debug.dprintf debug "Bisecting: %a.@."
1085 1086
                                   Call_provers.print_prover_result res
                  end
MARCHE Claude's avatar
MARCHE Claude committed
1087 1088 1089
                in
                schedule_proof_attempt c pn prover ~counterexmp:false ~limit ~callback ~notification
             | _ -> assert false
1090
          end
MARCHE Claude's avatar
MARCHE Claude committed
1091
        in
1092 1093
        Debug.dprintf debug "To remove: %s@." rem;
        schedule_transformation c goal_id "remove" [rem] ~callback ~notification
MARCHE Claude's avatar
MARCHE Claude committed
1094
      end
MARCHE Claude's avatar
MARCHE Claude committed
1095
  in
MARCHE Claude's avatar
MARCHE Claude committed
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
  let rec bisect_step rem kont =
    (* we have to:
       1) apply transformation remove with the given rem
       2) on the generated sub-goal, run the prover with some callback
       3) the callback should :
          compute (next_iter success_value)
          if result is done, do nothing more
          if result is some new rem, remove the previous transformation
           and recursively call bisect_step
     *)
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
    let rem = create_rem_list rem in
    let callback st =
      callback_tr "remove" [rem] st;
      begin match st with
      | TSscheduled ->
         Debug.dprintf
           debug
           "[Bisect] transformation 'remove' scheduled@."
      | TSfailed(_,exn) ->
         (* may happen if removing a type or a lsymbol that is used
later on. We do has if proof fails. *)
MARCHE Claude's avatar
MARCHE Claude committed
1117
         begin
1118 1119 1120
           Debug.dprintf
             debug
             "[Bisect] transformation failed %a@." Exn_printer.exn_printer exn;
MARCHE Claude's avatar
MARCHE Claude committed
1121 1122 1123 1124 1125 1126 1127
           match kont false with
           | Eliminate_definition.BSstep (rem,kont) ->
              bisect_step rem kont
           | Eliminate_definition.BSdone rem ->
              bisect_end rem
         end
      | TSdone trid ->
1128 1129 1130
         Debug.dprintf
           debug
           "[Bisect] transformation 'remove' succeeds@.";
MARCHE Claude's avatar
MARCHE Claude committed
1131 1132 1133
         match get_sub_tasks ses trid with
         | [pn] ->
            let limit = { limit with Call_provers.limit_time = !timelimit; } in
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
            let callback paid st =
              callback_pa paid st;
              begin match st with
              | Scheduled ->
                 Debug.dprintf
                   debug "[Bisect] prover on subtask is scheduled@."
              | Running ->
                 Debug.dprintf
                   debug "[Bisect] prover on subtask is running@.";
              | Detached
              | Uninstalled _ -> assert false
1145
              | Undone | Interrupted -> Debug.dprintf debug "Bisecting interrupted.@."
MARCHE Claude's avatar
MARCHE Claude committed
1146 1147
              | InternalFailure exn ->
                 (* Perhaps the test can be considered false in this case? *)
1148
                 Debug.dprintf debug "[Bisect] prover interrupted by an error: %a.@."
MARCHE Claude's avatar
MARCHE Claude committed
1149 1150
                               Exn_printer.exn_printer exn
              | Done res ->
1151 1152 1153
                 Debug.dprintf
                   debug "[Bisect] prover on subtask returns %a@."
                   Call_provers.print_prover_answer res.Call_provers.pr_answer;
MARCHE Claude's avatar
MARCHE Claude committed
1154 1155 1156 1157
                 let b = res.Call_provers.pr_answer = Call_provers.Valid in
                 if b then set_timelimit res;
                 match kont b with
                 | Eliminate_definition.BSstep (rem,kont) ->
1158
                    Session_itp.remove_subtree ~notification ~removed ses (Session_itp.ATn trid);
MARCHE Claude's avatar
MARCHE Claude committed
1159 1160 1161
                    bisect_step rem kont
                 | Eliminate_definition.BSdone rem ->
                    bisect_end rem
1162
              end
MARCHE Claude's avatar
MARCHE Claude committed
1163
            in
1164 1165
            Debug.dprintf
              debug "[Bisect] running the prover on subtask@.";
MARCHE Claude's avatar
MARCHE Claude committed
1166 1167
            schedule_proof_attempt c pn prover ~counterexmp:false ~limit ~callback ~notification
         | _ -> assert false
1168
      end
MARCHE Claude's avatar
MARCHE Claude committed
1169
    in
1170 1171
    Debug.dprintf debug "To remove: %s@." rem;
    schedule_transformation c goal_id "remove" [rem] ~callback ~notification
MARCHE Claude's avatar
MARCHE Claude committed
1172 1173
  in
  Debug.dprintf debug "Bisecting with %a started.@."
MARCHE Claude's avatar
MARCHE Claude committed
1174 1175 1176 1177 1178
                Whyconf.print_prover prover;
  let t = get_raw_task ses goal_id in
  match Eliminate_definition.bisect_step t with
  | Eliminate_definition.BSdone _ -> assert false
  | Eliminate_definition.BSstep(rem,kont) -> bisect_step rem kont
MARCHE Claude's avatar
MARCHE Claude committed
1179

1180
end