itp_server.ml 20.8 KB
Newer Older
Clément Fumex's avatar
Clément Fumex committed
1
open Call_provers
2
open Format
Clément Fumex's avatar
Clément Fumex committed
3

Sylvain Dailler's avatar
Sylvain Dailler committed
4
(* Information that the IDE may want to have *)
Clément Fumex's avatar
Clément Fumex committed
5 6 7 8
type prover = string
type transformation = string
type strategy = string

Sylvain Dailler's avatar
Sylvain Dailler committed
9

10 11
type node_ID = int
let root_node : node_ID = 0
Sylvain Dailler's avatar
Sylvain Dailler committed
12

Clément Fumex's avatar
Clément Fumex committed
13 14 15 16 17 18 19
type node_type =
  | NRoot
  | NFile
  | NTheory
  | NTransformation
  | NGoal
  | NProofAttempt of Call_provers.prover_answer option * bool
Clément Fumex's avatar
Clément Fumex committed
20

21 22 23 24 25
type node_info =
    {
     proved : bool;
     name   : string;
    }
Clément Fumex's avatar
Clément Fumex committed
26

27 28
type global_information =
    {
Clément Fumex's avatar
Clément Fumex committed
29 30 31 32
     provers         : prover list;
     transformations : transformation list;
     strategies      : strategy list;
     commands        : string list;
33 34 35 36 37 38 39
     (* hidden_provers       : string list; *)
     (* session_time_limit   : int; *)
     (* session_mem_limit    : int; *)
     (* session_nb_processes : int; *)
     (* session_cntexample   : bool; *)
     (* main_dir             : string *)
    }
Clément Fumex's avatar
Clément Fumex committed
40

41
type message_notification =
Clément Fumex's avatar
Clément Fumex committed
42 43
  | Proof_error  of node_ID * string
  | Transf_error of node_ID * string
Clément Fumex's avatar
Clément Fumex committed
44
  | Strat_error  of node_ID * string
45 46 47 48 49
  | Replay_Info  of string
  | Query_Info   of node_ID * string
  | Query_Error  of node_ID * string
  | Help         of string
  | Information  of string
Clément Fumex's avatar
Clément Fumex committed
50
  | Task_Monitor of int * int * int
51
  | Error        of string
Clément Fumex's avatar
Clément Fumex committed
52 53

type notification =
Clément Fumex's avatar
Clément Fumex committed
54 55 56 57
  | Node_change  of node_ID * node_info
  | New_node     of node_ID * node_ID * node_type * node_info
  | Remove       of node_ID
  | Initialized  of global_information
Sylvain Dailler's avatar
Sylvain Dailler committed
58
  | Saved
Clément Fumex's avatar
Clément Fumex committed
59 60 61 62
  | Message      of message_notification
  | Dead         of string
  | Proof_update of node_ID * Controller_itp.proof_attempt_status
  | Task         of node_ID * string
Clément Fumex's avatar
Clément Fumex committed
63 64

type request_type =
Clément Fumex's avatar
Clément Fumex committed
65 66 67 68 69 70 71
  | Command_req       of string
  | Prove_req         of prover * resource_limit
  | Transform_req     of transformation * string list
  | Strategy_req      of strategy
  | Open_req          of string
  | Set_max_tasks_req of int
  | Get_task
72 73 74 75 76
  | Get_Session_Tree_req
  | Save_req
  | Reload_req
  | Replay_req
  | Exit_req
Clément Fumex's avatar
Clément Fumex committed
77

Sylvain Dailler's avatar
Sylvain Dailler committed
78
(* Debugging functions *)
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
let print_request fmt r =
  match r with
  | Command_req s             -> fprintf fmt "command \"%s\"" s
  | Prove_req (prover, _rl)   -> fprintf fmt "prove with %s" prover
  | Transform_req (tr, _args) -> fprintf fmt "transformation :%s" tr
  | Strategy_req st           -> fprintf fmt "strategy %s" st
  | Open_req f                -> fprintf fmt "open file %s" f
  | Set_max_tasks_req i       -> fprintf fmt "set max tasks %i" i
  | Get_task                  -> fprintf fmt "get task"
  | Get_Session_Tree_req      -> fprintf fmt "get session tree"
  | Save_req                  -> fprintf fmt "save"
  | Reload_req                -> fprintf fmt "reload"
  | Replay_req                -> fprintf fmt "replay"
  | Exit_req                  -> fprintf fmt "exit"

Sylvain Dailler's avatar
Sylvain Dailler committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
let print_msg fmt m =
  match m with
  | Proof_error (_ids, s)  -> fprintf fmt "proof error %s" s
  | Transf_error (_ids, s) -> fprintf fmt "transf error %s" s
  | Strat_error (_ids, s)  -> fprintf fmt "start error %s" s
  | Replay_Info s          -> fprintf fmt "replay info %s" s
  | Query_Info (_ids, s)   -> fprintf fmt "query info %s" s
  | Query_Error (_ids, s)  -> fprintf fmt "query error %s" s
  | Help _s                -> fprintf fmt "help"
  | Information s          -> fprintf fmt "info %s" s
  | Task_Monitor _         -> fprintf fmt "task montor"
  | Error s                -> fprintf fmt "%s" s

let print_notify fmt n =
  match n with
  | Node_change (_ni, _nf)          -> fprintf fmt "node change"
  | New_node (_ni, _pni, _nt,  _nf) -> fprintf fmt "new node"
  | Remove _ni                      -> fprintf fmt "remove"
  | Initialized _gi                 -> fprintf fmt "initialized"
  | Saved                           -> fprintf fmt "saved"
  | Message msg                     ->
      print_msg fmt msg
  | Dead s                          -> fprintf fmt "dead :%s" s
  | Proof_update (_ni, _pas)        -> fprintf fmt "proof update"
  | Task (_ni, _s)                  -> fprintf fmt "task"

Clément Fumex's avatar
Clément Fumex committed
120 121 122 123
type ide_request = request_type * node_ID

open Session_itp
open Controller_itp
Clément Fumex's avatar
Clément Fumex committed
124 125
open Session_user_interface
open Stdlib
Clément Fumex's avatar
Clément Fumex committed
126 127

module type Protocol = sig
Clément Fumex's avatar
Clément Fumex committed
128 129
  val get_requests : unit -> ide_request list
  val notify : notification -> unit
Clément Fumex's avatar
Clément Fumex committed
130 131
end

132 133 134
module Make (S:Controller_itp.Scheduler) (P:Protocol) = struct

  module C = Controller_itp.Make(S)
Clément Fumex's avatar
Clément Fumex committed
135

136
  let _debug = Debug.register_flag "itp_server"
Clément Fumex's avatar
Clément Fumex committed
137 138 139 140 141 142

  (************************)
  (* parsing command line *)
  (************************)

  let files = Queue.create ()
143
  (* TODO never used let _opt_parser = ref None *)
Clément Fumex's avatar
Clément Fumex committed
144

145
  (* Files are passed with request Open *)
Clément Fumex's avatar
Clément Fumex committed
146 147
  let config, base_config, env =
    let c, b, e = Whyconf.Args.init () in
Clément Fumex's avatar
Clément Fumex committed
148 149
    c, b, e

Clément Fumex's avatar
Clément Fumex committed
150 151 152 153 154 155 156 157
  let get_configs () = config, base_config

  let task_driver =
    let main = Whyconf.get_main config in
    let d = Filename.concat (Whyconf.datadir main)
        (Filename.concat "drivers" "why3_itp.drv")
    in
    Driver.load_driver env d []
Clément Fumex's avatar
Clément Fumex committed
158 159 160 161

  let provers : Whyconf.config_prover Whyconf.Mprover.t =
    Whyconf.get_provers config

162 163
  let get_prover_list (config: Whyconf.config) =
    Mstr.fold (fun x _ acc -> x :: acc) (Whyconf.get_prover_shortcuts config) []
Sylvain Dailler's avatar
Sylvain Dailler committed
164

165 166 167 168
  let prover_list: prover list = get_prover_list config
  let transformation_list: transformation list =
    List.map fst (Session_user_interface.list_transforms ())
  let strategies_list: strategy list = []
Sylvain Dailler's avatar
Sylvain Dailler committed
169 170

(* TODO write this *)
171 172 173 174
  let infos =
    {
     provers = prover_list;
     transformations = transformation_list;
Clément Fumex's avatar
Clément Fumex committed
175 176 177
     strategies = strategies_list;
     commands =
       List.map (fun (c,_,_) -> c) Session_user_interface.commands
178
   }
Sylvain Dailler's avatar
Sylvain Dailler committed
179

180 181
  (* Controller is not initialized: we cannot process any request *)
  let init_controller = ref false
182

183 184 185 186
  (* Create_controller creates a dummy controller *)
  let cont =
    init_controller := false;
    create_controller env
Sylvain Dailler's avatar
Sylvain Dailler committed
187

Clément Fumex's avatar
Clément Fumex committed
188 189
  (* ------------ init controller ------------ *)

190 191 192
  (* Init cont is called only when an Open is requested *)
  let init_cont f =
    Queue.add f files;
Clément Fumex's avatar
Clément Fumex committed
193
    try
Clément Fumex's avatar
Clément Fumex committed
194
      (Session_user_interface.cont_from_files cont [] "" env files provers;
195 196
       init_controller := true;
       P.notify (Initialized infos))
Clément Fumex's avatar
Clément Fumex committed
197 198
    with e ->
      Format.eprintf "%a@." Exn_printer.exn_printer e;
199
      P.notify (Dead (Pp.string_of Exn_printer.exn_printer e));
Clément Fumex's avatar
Clément Fumex committed
200 201
      exit 1

Sylvain Dailler's avatar
Sylvain Dailler committed
202
  (* -----------------------------------   ------------------------------------- *)
Clément Fumex's avatar
Clément Fumex committed
203

Sylvain Dailler's avatar
Sylvain Dailler committed
204 205 206 207 208
  let get_info_and_type ses (node: any) =
    match node with
    | AFile file ->
        let name = file.file_name in
        let proved = file_proved cont file in
209
        NFile, {name; proved}
Sylvain Dailler's avatar
Sylvain Dailler committed
210 211 212
    | ATh th     ->
        let name = (theory_name th).Ident.id_string in
        let proved = th_proved cont th in
213
        NTheory, {name; proved}
Sylvain Dailler's avatar
Sylvain Dailler committed
214 215 216
    | ATn tn     ->
        let name = get_transf_name ses tn in
        let proved = tn_proved cont tn in
217
        NTransformation, {name; proved}
Sylvain Dailler's avatar
Sylvain Dailler committed
218 219 220
    | APn pn     ->
        let name = (get_proof_name ses pn).Ident.id_string in
        let proved = pn_proved cont pn in
221
          NGoal, {name; proved}
Sylvain Dailler's avatar
Sylvain Dailler committed
222 223 224
    | APa pan    ->
        let pa = get_proof_attempt_node ses pan in
        let name = Pp.string_of Whyconf.print_prover pa.prover in
Clément Fumex's avatar
Clément Fumex committed
225 226 227
        let pr, proved = match pa.Session_itp.proof_state with
        | Some pr -> Some pr.pr_answer, pr.pr_answer = Valid
        | None -> None, false
Sylvain Dailler's avatar
Sylvain Dailler committed
228
        in
Clément Fumex's avatar
Clément Fumex committed
229 230
        (NProofAttempt (pr, pa.proof_obsolete)),
        {name; proved}
Sylvain Dailler's avatar
Sylvain Dailler committed
231

232 233 234 235 236 237 238 239 240 241
(* fresh gives new fresh "names" for node_ID using a counter.
   reset resets the counter so that we can regenerate node_IDs as if session
   was fresh *)
  let reset, fresh =
    let count = ref 0 in
    (fun () ->
      count := 0),
    fun () ->
      count := !count + 1;
      !count
Sylvain Dailler's avatar
Sylvain Dailler committed
242

243
  let model_any : any Hint.t = Hint.create 17
Clément Fumex's avatar
Clément Fumex committed
244

245
  let any_from_node_ID (nid:node_ID) : any = Hint.find model_any nid
Clément Fumex's avatar
Clément Fumex committed
246 247 248 249 250 251 252 253 254 255 256 257

  let pan_to_node_ID  : node_ID Hpan.t = Hpan.create 17
  let pn_to_node_ID   : node_ID Hpn.t = Hpn.create 17
  let tn_to_node_ID   : node_ID Htn.t = Htn.create 17
  let th_to_node_ID   : node_ID Ident.Hid.t = Ident.Hid.create 7
  let file_to_node_ID : node_ID Hstr.t = Hstr.create 3

  let node_ID_from_pan  pan  = Hpan.find pan_to_node_ID pan
  let node_ID_from_pn   pn   = Hpn.find pn_to_node_ID pn
  let node_ID_from_tn   tn   = Htn.find tn_to_node_ID tn
  let node_ID_from_th   th   = Ident.Hid.find th_to_node_ID (theory_name th)
  let node_ID_from_file file = Hstr.find file_to_node_ID (file.file_name)
Clément Fumex's avatar
Clément Fumex committed
258

259 260 261 262 263 264 265 266
  let node_ID_from_any  any  =
    match any with
    | AFile file -> node_ID_from_file file
    | ATh th     -> node_ID_from_th th
    | ATn tn     -> node_ID_from_tn tn
    | APn pn     -> node_ID_from_pn pn
    | APa pan    -> node_ID_from_pan pan

Sylvain Dailler's avatar
Sylvain Dailler committed
267 268
(* TODO match this *)
exception Bad_prover_name of prover
Clément Fumex's avatar
Clément Fumex committed
269

Sylvain Dailler's avatar
Sylvain Dailler committed
270 271 272 273
  let get_prover p =
    match return_prover p config with
    | None -> raise (Bad_prover_name p)
    | Some c -> c
Clément Fumex's avatar
Clément Fumex committed
274

Sylvain Dailler's avatar
Sylvain Dailler committed
275 276 277 278 279 280 281
  let add_node_to_table node new_id =
    match node with
    | AFile file -> Hstr.add file_to_node_ID file.file_name new_id
    | ATh th     -> Ident.Hid.add th_to_node_ID (theory_name th) new_id
    | ATn tn     -> Htn.add tn_to_node_ID tn new_id
    | APn pn     -> Hpn.add pn_to_node_ID pn new_id
    | APa pan    -> Hpan.add pan_to_node_ID pan new_id
Clément Fumex's avatar
Clément Fumex committed
282 283 284

  (* create a new node in the_tree, update the tables and send a
     notification about it *)
Sylvain Dailler's avatar
Sylvain Dailler committed
285
  let new_node ~parent node : node_ID =
286 287
    let new_id = fresh () in
      Hint.add model_any new_id node;
Clément Fumex's avatar
Clément Fumex committed
288
      let ses = cont.controller_session in
Sylvain Dailler's avatar
Sylvain Dailler committed
289 290
      let typ, info = get_info_and_type ses node in
      add_node_to_table node new_id;
291
      P.notify (New_node (new_id, parent, typ, info));
Clément Fumex's avatar
Clément Fumex committed
292 293
      new_id

294 295
  (* TODO this is a dummy constant for root content *)
  let root_info = { proved = false; name = ""}
Clément Fumex's avatar
Clément Fumex committed
296
  let root = 0
Sylvain Dailler's avatar
Sylvain Dailler committed
297 298

  (* ----------------- build tree from tables ----------------- *)
299 300 301 302 303 304 305 306

  (*
     build_the_tree() returns the whole session tree as notifications beginning
     with the notification corresponding to a "root node" creation (root of the
     files)
  *)
(* TODO remove this unnecessary
  let build_the_tree () : unit =
Sylvain Dailler's avatar
Sylvain Dailler committed
307 308
    let ses = cont.controller_session in
    let files = get_files ses in
309
    P.notify (New_node (0, 0, Root, root_info));
Sylvain Dailler's avatar
Sylvain Dailler committed
310 311 312 313 314 315 316 317 318
    let l = Stdlib.Hstr.fold
      (fun _file_key file acc ->
         let file_node_ID = node_ID_from_file file in
         let pos_ID = pos_from_node file_node_ID in
         let node_type, node_info = get_info_and_type ses (AFile file) in
         let l = List.fold_left (fun acc th ->
               build_subtree_from_theory ses th :: acc) [] file.file_theories in
         Node (file_node_ID, pos_ID, node_type, node_info, l) :: acc
      ) files [] in
319
*)
Sylvain Dailler's avatar
Sylvain Dailler committed
320

Clément Fumex's avatar
Clément Fumex committed
321
  (* ----------------- init the tree --------------------------- *)
322 323 324 325
  (* Iter on the session tree with a function [f parent current] with type
     node_ID -> any -> unit *)
  let iter_subtree_proof_attempt_from_goal
    (f: parent:node_ID -> any -> unit) parent id =
Clément Fumex's avatar
Clément Fumex committed
326
    Whyconf.Hprover.iter
327
      (fun _pa panid -> f ~parent (APa panid))
Clément Fumex's avatar
Clément Fumex committed
328 329
      (get_proof_attempt_ids cont.controller_session id)

330 331
  let rec iter_subtree_from_goal
    (f: parent:node_ID -> any -> unit) parent id =
Clément Fumex's avatar
Clément Fumex committed
332
    let ses = cont.controller_session in
333 334
    f ~parent (APn id);
    let nid = node_ID_from_pn id in
Clément Fumex's avatar
Clément Fumex committed
335
    List.iter
336
      (fun trans_id -> iter_subtree_from_trans f nid trans_id)
Clément Fumex's avatar
Clément Fumex committed
337
      (get_transformations ses id);
338
    iter_subtree_proof_attempt_from_goal f nid id
Clément Fumex's avatar
Clément Fumex committed
339

340 341
  and iter_subtree_from_trans
    (f: parent:node_ID -> any -> unit) parent trans_id =
Clément Fumex's avatar
Clément Fumex committed
342
    let ses = cont.controller_session in
343 344
    f ~parent (ATn trans_id);
    let nid = node_ID_from_tn trans_id in
Clément Fumex's avatar
Clément Fumex committed
345
    List.iter
346
      (fun goal_id -> (iter_subtree_from_goal f nid goal_id))
Sylvain Dailler's avatar
Sylvain Dailler committed
347
      (get_sub_tasks ses trans_id)
Clément Fumex's avatar
Clément Fumex committed
348

349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
  let iter_subtree_from_theory
    (f: parent:node_ID -> any -> unit) parent theory_id =
    f ~parent (ATh theory_id);
    let nid = node_ID_from_th theory_id in
    List.iter (iter_subtree_from_goal f nid)
               (theory_goals theory_id)

  let iter_subtree_from_file
    (f: parent:node_ID -> any -> unit) parent file =
    f ~parent (AFile file);
    let nid = node_ID_from_file file in
    List.iter (iter_subtree_from_theory f nid)
      file.file_theories

  let iter_the_files (f: parent:node_ID -> any -> unit) parent : unit =
Clément Fumex's avatar
Clément Fumex committed
364 365 366 367
    let ses = cont.controller_session in
    let files = get_files ses in
    Stdlib.Hstr.iter
      (fun _ file ->
368
        iter_subtree_from_file f parent file)
Sylvain Dailler's avatar
Sylvain Dailler committed
369
      files
Clément Fumex's avatar
Clément Fumex committed
370

371 372 373 374 375
  let _init_the_tree (): unit =
    let f ~parent node_id = ignore (new_node ~parent node_id) in
    iter_the_files f root

  let init_and_send_subtree_from_trans parent trans_id : unit =
Clément Fumex's avatar
Clément Fumex committed
376 377
    iter_subtree_from_trans
      (fun ~parent id -> ignore (new_node ~parent id)) parent trans_id
378 379

  let init_and_send_the_tree (): unit =
Clément Fumex's avatar
Clément Fumex committed
380
    iter_the_files (fun ~parent id -> ignore (new_node ~parent id)) root
381 382 383 384 385 386 387 388 389 390

  let resend_the_tree (): unit =
    let ses = cont.controller_session in
    let send_node ~parent any =
      let node_id = node_ID_from_any any in
      let node_type, node_info = get_info_and_type ses any in
      P.notify (New_node (node_id, parent, node_type, node_info)) in
    P.notify (New_node (0, 0, NRoot, root_info));
    iter_the_files send_node root

Clément Fumex's avatar
Clément Fumex committed
391 392 393 394 395 396 397 398 399 400 401 402 403
  (* -- send the task -- *)

  let send_task nid =
    match any_from_node_ID nid with
    | APn id ->
      let task = get_task cont.controller_session id in
      let tables = get_tables cont.controller_session id in
      let s = Pp.string_of
          (fun fmt -> Driver.print_task ~cntexample:false task_driver fmt tables)
          task in
      P.notify (Task (nid,s))
    | _ ->
      P.notify (Task (nid, "can not associate a task to a node that is not a goal."))
404

Clément Fumex's avatar
Clément Fumex committed
405 406 407 408 409
  (* ----------------- Schedule proof attempt -------------------- *)

  (* Callback of a proof_attempt *)
  let callback_update_tree_proof cont panid pa_status =
    let ses = cont.controller_session in
Clément Fumex's avatar
Clément Fumex committed
410
    begin match pa_status with
Clément Fumex's avatar
Clément Fumex committed
411 412 413 414 415 416 417 418 419 420
    | Scheduled ->
      begin
        try
          ignore (node_ID_from_pan panid)
        (* TODO: do we notify here ? *)
        with Not_found ->
          let parent_id = get_proof_attempt_parent ses panid in
          let parent = node_ID_from_pn parent_id in
          ignore (new_node ~parent (APa panid))
      end
Clément Fumex's avatar
Clément Fumex committed
421
(*    | Done pr ->
Clément Fumex's avatar
Clément Fumex committed
422
      P.notify (Node_change (node_ID_from_pan panid,
Clément Fumex's avatar
Clément Fumex committed
423 424
                             {proved=(pr.pr_answer=Valid); name=""})); *)
      (* TODO: we don't want to resend the name every time, separate
Clément Fumex's avatar
Clément Fumex committed
425 426
         updatable from the rest *)
    | _  -> () (* TODO ? *)
Clément Fumex's avatar
Clément Fumex committed
427 428
    end;
    P.notify (Proof_update (node_ID_from_pan panid, pa_status))
Clément Fumex's avatar
Clément Fumex committed
429

430 431 432 433 434 435 436 437
  let notify_change x b =
    try (
      let node_ID = node_ID_from_any x in
      let _, node_info = get_info_and_type cont.controller_session x in
      P.notify (Node_change (node_ID, {name = node_info.name; proved = b}))
     )
    with Not_found -> ()

Clément Fumex's avatar
Clément Fumex committed
438
  let schedule_proof_attempt nid (p: Whyconf.config_prover) limit =
Clément Fumex's avatar
Clément Fumex committed
439 440
    let prover = p.Whyconf.prover in
    let callback = callback_update_tree_proof cont in
Clément Fumex's avatar
Clément Fumex committed
441 442 443 444 445 446 447
    let goals =
      match any_from_node_ID nid with
      | APn pnid   -> [pnid]
      | APa panid  ->
          let ses = cont.controller_session in
          [get_proof_attempt_parent ses panid]
      | ATn tn     ->
Clément Fumex's avatar
Clément Fumex committed
448
        List.rev (unproven_goals_below_tn cont [] tn)
Clément Fumex's avatar
Clément Fumex committed
449
      | AFile file ->
Clément Fumex's avatar
Clément Fumex committed
450
        List.rev (unproven_goals_below_file cont file)
Clément Fumex's avatar
Clément Fumex committed
451
      | ATh th     ->
Clément Fumex's avatar
Clément Fumex committed
452 453
        List.rev (unproven_goals_below_th cont [] th)
    in
454
    List.iter (fun id -> C.schedule_proof_attempt cont id prover ~limit ~callback ~notification:notify_change)
Clément Fumex's avatar
Clément Fumex committed
455 456 457 458 459 460 461 462 463 464 465
      goals

  (* ----------------- Schedule transformation -------------------- *)

  (* Callback of a transformation *)
  let callback_update_tree_transform status =
    match status with
    | TSdone trans_id ->
      let ses = cont.controller_session in
      let id = get_trans_parent ses trans_id in
      let nid = node_ID_from_pn id in
466
      init_and_send_subtree_from_trans nid trans_id
Clément Fumex's avatar
Clément Fumex committed
467 468 469 470
    | TSfailed (id, e) ->
      let msg =
        Pp.sprintf "%a" (get_exception_message cont.controller_session id) e
      in
471
      P.notify (Message (Strat_error(node_ID_from_pn id, msg)))
Clément Fumex's avatar
Clément Fumex committed
472 473 474 475 476
    | _ -> ()

  let rec apply_transform nid t args =
    match any_from_node_ID nid with
    | APn id ->
477 478 479
      (* let node_ID = node_ID_from_any x in *)
      (* let _, node_info = get_info_and_type cont.controller_session x in *)
      (* P.notify (Node_change (node_ID, {name = node_info.name; proved = b})) in *)
Clément Fumex's avatar
Clément Fumex committed
480
      let callback = callback_update_tree_transform in
481
      C.schedule_transformation cont id t args ~callback ~notification:notify_change
Clément Fumex's avatar
Clément Fumex committed
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
    | APa panid ->
      let parent_id = get_proof_attempt_parent cont.controller_session panid in
      let parent = node_ID_from_pn parent_id in
      apply_transform parent t args
    | ATn _ | AFile _ | ATh _ ->
      (* TODO: propagate trans to all subgoals, just the first one, do nothing ... ?  *)
      ()

  (* ----------------- run strategy -------------------- *)

  let run_strategy_on_task nid s =
    match any_from_node_ID nid with
    | APn id ->
      let l = Session_user_interface.strategies
          cont.controller_env config
      in
      let st = List.filter (fun (_,c,_,_) -> c=s) l in
      begin
        match st with
        | [(n,_,_,st)] ->
          Format.printf "running strategy '%s'@." n;
          let callback sts =
            Format.printf "Strategy status: %a@." print_strategy_status sts
          in
          let callback_pa = callback_update_tree_proof cont in
          let callback_tr st = callback_update_tree_transform st in
508
          C.run_strategy_on_goal cont id st ~callback_pa ~callback_tr ~callback ~notification:notify_change
Clément Fumex's avatar
Clément Fumex committed
509 510 511
        | _ -> Format.printf "Strategy '%s' not found@." s
      end
    | _ -> ()
Clément Fumex's avatar
Clément Fumex committed
512

Sylvain Dailler's avatar
Sylvain Dailler committed
513 514 515 516 517 518 519
  (* ----------------- Save session --------------------- *)
  let save_session () =
    Session_itp.save_session cont.controller_session;
    P.notify Saved

  (* ----------------- Reload session ------------------- *)
  let clear_tables () : unit =
520 521
    reset ();
    Hint.clear model_any;
Sylvain Dailler's avatar
Sylvain Dailler committed
522 523 524 525 526 527 528 529 530
    Hpan.clear pan_to_node_ID;
    Hpn.clear pn_to_node_ID;
    Htn.clear tn_to_node_ID;
    Ident.Hid.clear th_to_node_ID;
    Hstr.clear file_to_node_ID

  let reload_session () : unit =
    clear_tables ();
    reload_files cont env ~use_shapes:true;
531
    init_and_send_the_tree ()
Sylvain Dailler's avatar
Sylvain Dailler committed
532 533 534

  let replay_session () : unit =
    clear_tables ();
535 536
    let callback = fun lr ->
      P.notify (Message (Replay_Info (Pp.string_of C.replay_print lr))) in
Sylvain Dailler's avatar
Sylvain Dailler committed
537
    (* TODO make replay print *)
538
    C.replay ~use_steps:false cont ~callback:callback ~remove_obsolete:false
Sylvain Dailler's avatar
Sylvain Dailler committed
539

Clément Fumex's avatar
Clément Fumex committed
540
  (* ----------------- treat_request -------------------- *)
Clément Fumex's avatar
Clément Fumex committed
541

542 543 544
  let rec treat_request (r,nid) =
    try (
    match r with
545 546 547 548 549 550
    | Prove_req (p,limit)     -> schedule_proof_attempt nid (get_prover p) limit
    | Transform_req (t, args) -> apply_transform nid t args
    | Strategy_req st         -> run_strategy_on_task nid st
    | Save_req                -> save_session ()
    | Reload_req              -> reload_session ();
    | Get_Session_Tree_req    -> resend_the_tree ()
Clément Fumex's avatar
Clément Fumex committed
551
    | Get_task                -> send_task nid
552 553
    | Replay_req              -> replay_session (); resend_the_tree ()
    | Command_req cmd         ->
Sylvain Dailler's avatar
Sylvain Dailler committed
554
      begin
Sylvain Dailler's avatar
Sylvain Dailler committed
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
        if nid = 0 then
          (* root_node is not in any_from_node_ID table *)
          P.notify (Message (Information "Should be done on a proof node"))
        else
          match any_from_node_ID nid with
          | APn pn_id ->
            begin
              match (interp config cont (Some pn_id) cmd) with
              | Transform (s, _t, args) -> treat_request (Transform_req (s, args), nid)
              | Query s                 -> P.notify (Message (Query_Info (nid, s)))
              | Prove (p, limit)        -> schedule_proof_attempt nid p limit
              | Strategies st           -> run_strategy_on_task nid st
              | Help_message s          -> P.notify (Message (Help s))
              | QError s                -> P.notify (Message (Query_Error (nid, s)))
              | Other (s, _args)        ->
570
                P.notify (Message (Information ("Unknown command"^s)))
Sylvain Dailler's avatar
Sylvain Dailler committed
571 572
            end
          | _ ->
573 574
            P.notify (Message (Information "Should be done on a proof node"))
            (* TODO make it an error *)
Sylvain Dailler's avatar
Sylvain Dailler committed
575
      end
576 577 578
    | Open_req file_name      ->
        if !init_controller then
          Controller_itp.add_file cont file_name
Clément Fumex's avatar
Clément Fumex committed
579 580 581 582 583
        else begin
          init_cont file_name;
          init_and_send_the_tree ()
        end
    | Set_max_tasks_req i     -> C.set_max_tasks i
584
    | Exit_req                -> exit 0 (* TODO *)
585 586 587 588
     )
    with e -> P.notify (Message (Error (Pp.string_of
      (fun fmt (r,nid,e) -> Format.fprintf fmt
          "There was an unrecoverable error during treatment of request:\n %a\non node: %d\nwith exception: %a"
589
    print_request r nid Exn_printer.exn_printer e ) (r, nid, e))))
Clément Fumex's avatar
Clément Fumex committed
590 591 592 593 594

  let treat_requests () : bool =
    List.iter treat_request (P.get_requests ());
    true

595 596 597 598 599 600 601 602 603 604 605 606
  let update_monitor =
    let tr = ref 0 in
    let sr = ref 0 in
    let rr = ref 0 in
    fun t s r ->
      if (not (t = !tr && s = !sr && r = !rr)) then
        begin
          P.notify (Message (Task_Monitor (t,s,r)));
          tr := t;
          sr := s;
          rr := r
        end
Clément Fumex's avatar
Clément Fumex committed
607

Clément Fumex's avatar
Clément Fumex committed
608 609 610 611
  let _ =
    S.timeout ~ms:100 treat_requests;
    (* S.idle ~prio:1 treat_requests; *)
    C.register_observer update_monitor
Clément Fumex's avatar
Clément Fumex committed
612 613

end