itp_server.ml 37.3 KB
Newer Older
1
open Format
Sylvain Dailler's avatar
Sylvain Dailler committed
2
open Stdlib
3
4
open Session_itp
open Controller_itp
Sylvain Dailler's avatar
Sylvain Dailler committed
5
open Server_utils
6
open Itp_communication
7

Sylvain Dailler's avatar
Sylvain Dailler committed
8
exception Bad_prover_name of string
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

(**********************************)
(* list unproven goal and related *)
(**********************************)

(* If the transformation is proved, return acc.
   Else, return the concatenation of the reversed list of unproven
   goals below the transformation and acc *)
let rec unproven_goals_below_tn cont acc tn =
  if tn_proved cont tn then
    acc                         (* we ignore "dead" goals *)
  else
    let sub_tasks = get_sub_tasks cont.controller_session tn in
    List.fold_left (unproven_goals_below_pn cont) acc sub_tasks

(* Same as unproven_goals_below_tn; note that if goal is not proved
   and there is no transformation, goal is returned (else it is not) *)
and unproven_goals_below_pn cont acc goal =
  if pn_proved cont goal then
    acc                         (* we ignore "dead" transformations *)
  else
    match get_transformations cont.controller_session goal with
    | [] -> goal :: acc
    | tns -> List.fold_left (unproven_goals_below_tn cont) acc tns

(* Same as unproven_goals_below_tn *)
let unproven_goals_below_th cont acc th =
  if th_proved cont th then
    acc
  else
    let goals = theory_goals th in
    List.fold_left (unproven_goals_below_pn cont) acc goals

(* Same as unproven_goals_below_tn *)
let unproven_goals_below_file cont file =
  if file_proved cont file then
    []
  else
    let theories = file.file_theories in
    List.fold_left (unproven_goals_below_th cont) [] theories

50
51
52
53
54
55
56
57
58
59
60
61
62
let unproven_goals_below_id cont id =
  match id  with
  | APn pnid   -> [pnid]
  | APa panid  ->
     let ses = cont.controller_session in
     [get_proof_attempt_parent ses panid]
  | ATn tn     ->
     List.rev (unproven_goals_below_tn cont [] tn)
  | AFile file ->
     List.rev (unproven_goals_below_file cont file)
  | ATh th     ->
     List.rev (unproven_goals_below_th cont [] th)

63

Sylvain Dailler's avatar
Sylvain Dailler committed
64
65
66
(****************)
(* Command list *)
(****************)
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
let commands =
  [
    "list-transforms", "list available transformations", Qnotask list_transforms_query;
    "list-provers", "list available provers", Qnotask list_provers;
(*
    "list-strategies", "list available strategies", list_strategies;
*)
    "print", "<s> print the declaration where s was defined", Qtask print_id;
    "search", "<s> print some declarations where s appear", Qtask search_id;
(*
    "r", "reload the session (test only)", test_reload;
    "rp", "replay", test_replay;
    "s", "save the current session", test_save_session;
    "ng", "go to the next goal", then_print (move_to_goal_ret_p next_node);
    "pg", "go to the prev goal", then_print (move_to_goal_ret_p prev_node);
    "gu", "go to the goal up",  then_print (move_to_goal_ret_p zipper_up);
    "gd", "go to the goal down",  then_print (move_to_goal_ret_p zipper_down);
    "gr", "go to the goal right",  then_print (move_to_goal_ret_p zipper_right);
    "gl", "go to the goal left",  then_print (move_to_goal_ret_p zipper_left)
 *)
  ]

let commands_table = Stdlib.Hstr.create 17
let () =
  List.iter
    (fun (c,_,f) -> Stdlib.Hstr.add commands_table c f)
    commands

Sylvain Dailler's avatar
Sylvain Dailler committed
95
96
97
98
(*******************)
(* Strategies list *)
(*******************)
let loaded_strategies = ref []
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253

(****** Exception handling *********)

let print_term s id fmt t =
  let tables = match (Session_itp.get_tables s id) with
  | None -> Args_wrapper.build_name_tables (Session_itp.get_task s id)
  | Some tables -> tables in
  Why3printer.print_term tables fmt t

let print_type s id fmt t =
  let tables = match (Session_itp.get_tables s id) with
  | None -> Args_wrapper.build_name_tables (Session_itp.get_task s id)
  | Some tables -> tables in
  Why3printer.print_ty tables fmt t

let print_ts s id fmt t =
  let tables = match (Session_itp.get_tables s id) with
  | None -> Args_wrapper.build_name_tables (Session_itp.get_task s id)
  | Some tables -> tables in
  Why3printer.print_ts tables fmt t

let print_ls s id fmt t =
  let tables = match (Session_itp.get_tables s id) with
  | None -> Args_wrapper.build_name_tables (Session_itp.get_task s id)
  | Some tables -> tables in
  Why3printer.print_ls tables fmt t

let print_tv s id fmt t =
  let tables = match (Session_itp.get_tables s id) with
  | None -> Args_wrapper.build_name_tables (Session_itp.get_task s id)
  | Some tables -> tables in
  Why3printer.print_tv tables fmt t

let print_vsty s id fmt t =
  let tables = match (Session_itp.get_tables s id) with
  | None -> Args_wrapper.build_name_tables (Session_itp.get_task s id)
  | Some tables -> tables in
  Why3printer.print_forget_vsty tables fmt t

let print_pr s id fmt t =
  let tables = match (Session_itp.get_tables s id) with
  | None -> Args_wrapper.build_name_tables (Session_itp.get_task s id)
  | Some tables -> tables in
  Why3printer.print_pr tables fmt t

let print_pat s id fmt t =
  let tables = match (Session_itp.get_tables s id) with
  | None -> Args_wrapper.build_name_tables (Session_itp.get_task s id)
  | Some tables -> tables in
  Why3printer.print_pat tables fmt t

(* Exception reporting *)

(* TODO remove references to id.id_string in this function *)
let bypass_pretty s id =
  begin fun fmt exn -> match exn with
  | Ty.TypeMismatch (t1,t2) ->
      fprintf fmt "Type mismatch between %a and %a"
        (print_type s id) t1 (print_type s id) t2
  | Ty.BadTypeArity ({Ty.ts_args = []} as ts, _) ->
      fprintf fmt "Type symbol %a expects no arguments" (print_ts s id) ts
  | Ty.BadTypeArity (ts, app_arg) ->
      let i = List.length ts.Ty.ts_args in
      fprintf fmt "Type symbol %a expects %i argument%s but is applied to %i"
        (print_ts s id) ts i (if i = 1 then "" else "s") app_arg
  | Ty.DuplicateTypeVar tv ->
      fprintf fmt "Type variable %a is used twice" (print_tv s id) tv
  | Ty.UnboundTypeVar tv ->
      fprintf fmt "Unbound type variable: %a" (print_tv s id) tv
  | Ty.UnexpectedProp ->
      fprintf fmt "Unexpected propositional type"
  | Term.BadArity ({Term.ls_args = []} as ls, _) ->
      fprintf fmt "%s %a expects no arguments"
        (if ls.Term.ls_value = None then "Predicate" else "Function") (print_ls s id) ls
  | Term.BadArity (ls, app_arg) ->
      let i = List.length ls.Term.ls_args in
      fprintf fmt "%s %a expects %i argument%s but is applied to %i"
        (if ls.Term.ls_value = None then "Predicate" else "Function")
        (print_ls s id) ls i (if i = 1 then "" else "s") app_arg
  | Term.EmptyCase ->
      fprintf fmt "Empty match expression"
  | Term.DuplicateVar vs ->
      fprintf fmt "Variable %a is used twice" (print_vsty s id) vs
  | Term.UncoveredVar vs ->
      fprintf fmt "Variable %a uncovered in \"or\"-pattern" (print_vsty s id) vs
  | Term.FunctionSymbolExpected ls ->
      fprintf fmt "Not a function symbol: %a" (print_ls s id) ls
  | Term.PredicateSymbolExpected ls ->
      fprintf fmt "Not a predicate symbol: %a" (print_ls s id) ls
  | Term.ConstructorExpected ls ->
      fprintf fmt "%s %a is not a constructor"
        (if ls.Term.ls_value = None then "Predicate" else "Function") (print_ls s id) ls
  | Term.TermExpected t ->
      fprintf fmt "Not a term: %a" (print_term s id) t
  | Term.FmlaExpected t ->
      fprintf fmt "Not a formula: %a" (print_term s id) t
  | Pattern.ConstructorExpected (ls,ty) ->
      fprintf fmt "%s %a is not a constructor of type %a"
        (if ls.Term.ls_value = None then "Predicate" else "Function") (print_ls s id) ls
        (print_type s id) ty
  | Pattern.NonExhaustive pl ->
      fprintf fmt "Pattern not covered by a match:@\n  @[%a@]"
        (print_pat s id) (List.hd pl)
  | Decl.BadConstructor ls ->
      fprintf fmt "Bad constructor: %a" (print_ls s id) ls
  | Decl.BadRecordField ls ->
      fprintf fmt "Not a record field: %a" (print_ls s id) ls
  | Decl.RecordFieldMissing (_cs,ls) ->
      fprintf fmt "Field %a is missing" (print_ls s id) ls
  | Decl.DuplicateRecordField (_cs,ls) ->
      fprintf fmt "Field %a is used twice in the same constructor" (print_ls s id) ls
  | Decl.IllegalTypeAlias ts ->
      fprintf fmt
        "Type symbol %a is a type alias and cannot be declared as algebraic"
        (print_ts s id) ts
  | Decl.NonFoundedTypeDecl ts ->
      fprintf fmt "Cannot construct a value of type %a" (print_ts s id) ts
  | Decl.NonPositiveTypeDecl (_ts, ls, ty) ->
      fprintf fmt "Constructor %a \
          contains a non strictly positive occurrence of type %a"
        (print_ls s id) ls (print_type s id) ty
  | Decl.InvalidIndDecl (_ls, pr) ->
      fprintf fmt "Ill-formed inductive clause %a"
        (print_pr s id) pr
  | Decl.NonPositiveIndDecl (_ls, pr, ls1) ->
      fprintf fmt "Inductive clause %a contains \
          a non strictly positive occurrence of symbol %a"
        (print_pr s id) pr (print_ls s id) ls1
  | Decl.BadLogicDecl (ls1,ls2) ->
      fprintf fmt "Ill-formed definition: symbols %a and %a are different"
        (print_ls s id) ls1 (print_ls s id) ls2
  | Decl.UnboundVar vs ->
      fprintf fmt "Unbound variable: %a" (print_vsty s id) vs
  | Decl.ClashIdent id ->
      fprintf fmt "Ident %s is defined twice" id.Ident.id_string
  | Decl.EmptyDecl ->
      fprintf fmt "Empty declaration"
  | Decl.EmptyAlgDecl ts ->
      fprintf fmt "Algebraic type %a has no constructors" (print_ts s id) ts
  | Decl.EmptyIndDecl ls ->
      fprintf fmt "Inductive predicate %a has no constructors" (print_ls s id) ls
  | Decl.KnownIdent id ->
      fprintf fmt "Ident %s is already declared" id.Ident.id_string
  | Decl.UnknownIdent id ->
      fprintf fmt "Ident %s is not yet declared" id.Ident.id_string
  | Decl.RedeclaredIdent id ->
      fprintf fmt "Ident %s is already declared, with a different declaration"
        id.Ident.id_string
  | Decl.NoTerminationProof ls ->
      fprintf fmt "Cannot prove the termination of %a" (print_ls s id) ls
  | _ -> Format.fprintf fmt "Uncaught: %a" Exn_printer.exn_printer exn
  end

let get_exception_message ses id fmt e =
  match e with
254
255
  | Controller_itp.Noprogress ->
      Format.fprintf fmt "Transformation made no progress\n"
256
257
258
259
260
261
262
263
  | Case.Arg_trans_type (s, ty1, ty2) ->
      Format.fprintf fmt "Error in transformation %s during unification of the following terms:\n %a \n %a"
        s (print_type ses id) ty1 (print_type ses id) ty2
  | Case.Arg_trans_term (s, t1, t2) ->
      Format.fprintf fmt "Error in transformation %s during unification of following two terms:\n %a \n %a" s
        (print_term ses id) t1 (print_term ses id) t2
  | Case.Arg_trans (s) ->
      Format.fprintf fmt "Error in transformation function: %s \n" s
Sylvain Dailler's avatar
Sylvain Dailler committed
264
  | Args_wrapper.Arg_hyp_not_found (s) ->
265
266
267
268
269
270
271
272
      Format.fprintf fmt "Following hypothesis was not found: %s \n" s
  | Args_wrapper.Arg_theory_not_found (s) ->
      Format.fprintf fmt "Theory not found: %s" s
  | e ->
      bypass_pretty ses id fmt e



Clément Fumex's avatar
Clément Fumex committed
273
274


Sylvain Dailler's avatar
Sylvain Dailler committed
275
(* Debugging functions *)
276
277
let print_request fmt r =
  match r with
278
279
280
281
282
283
284
  | Command_req (_nid, s)           -> fprintf fmt "command \"%s\"" s
  | Prove_req (_nid, prover, _rl)   -> fprintf fmt "prove with %s" prover
  | Transform_req (_nid, tr, _args) -> fprintf fmt "transformation :%s" tr
  | Strategy_req (_nid, st)         -> fprintf fmt "strategy %s" st
  | Open_session_req f              -> fprintf fmt "open session file %s" f
  | Add_file_req f                  -> fprintf fmt "open file %s" f
  | Set_max_tasks_req i             -> fprintf fmt "set max tasks %i" i
285
  | Get_file_contents _f            -> fprintf fmt "get file contents"
286
  | Get_first_unproven_node _nid    -> fprintf fmt "get first unproven node"
287
  | Get_task _nid                   -> fprintf fmt "get task"
288
  | Remove_subtree _nid             -> fprintf fmt "remove subtree"
Sylvain Dailler's avatar
Sylvain Dailler committed
289
290
  | Copy_paste _                    -> fprintf fmt "copy paste"
  | Copy_detached _                 -> fprintf fmt "copy detached"
291
292
293
294
295
  | 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"
296

Sylvain Dailler's avatar
Sylvain Dailler committed
297
298
299
300
301
302
303
304
305
306
307
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"
308
  | Parse_Or_Type_Error s  -> fprintf fmt "parse_or_type_error:\n %s" s
Sylvain Dailler's avatar
Sylvain Dailler committed
309
  | Error s                -> fprintf fmt "%s" s
310
  | Open_File_Error s        -> fprintf fmt "%s" s
Sylvain Dailler's avatar
Sylvain Dailler committed
311
312
313

let print_notify fmt n =
  match n with
314
315
316
  | Node_change (_ni, _nf)             -> fprintf fmt "node change"
  | New_node (ni, _pni, _nt,  _nf, _d) -> fprintf fmt "new node %d" ni
  | Remove _ni                         -> fprintf fmt "remove"
317
  | Next_Unproven_Node_Id (_ni, _nj)   -> fprintf fmt "next unproven node_id"
318
319
320
  | Initialized _gi                    -> fprintf fmt "initialized"
  | Saved                              -> fprintf fmt "saved"
  | Message msg                        ->
Sylvain Dailler's avatar
Sylvain Dailler committed
321
      print_msg fmt msg
322
  | Dead s                             -> fprintf fmt "dead :%s" s
323
  | File_contents (_f, _s)             -> fprintf fmt "file contents"
324
  | Task (_ni, _s)                     -> fprintf fmt "task"
Sylvain Dailler's avatar
Sylvain Dailler committed
325

Clément Fumex's avatar
Clément Fumex committed
326
module type Protocol = sig
Clément Fumex's avatar
Clément Fumex committed
327
328
  val get_requests : unit -> ide_request list
  val notify : notification -> unit
Clément Fumex's avatar
Clément Fumex committed
329
330
end

331
332
333
module Make (S:Controller_itp.Scheduler) (P:Protocol) = struct

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

335
336
337
338
339
  (* Manipulations of files that handle errors *)
  let cont_from_session_dir cont f =
    try cont_from_session_dir cont f; true with
    | e ->
        let s = Format.asprintf "%a@." Exn_printer.exn_printer e in
340
        P.notify (Message (Parse_Or_Type_Error s)); false
341
342
343
344
345

  let cont_from_file cont f =
    try cont_from_file cont f; true with
    | e ->
        let s = Format.asprintf "%a@." Exn_printer.exn_printer e in
346
        P.notify (Message (Parse_Or_Type_Error s)); false
347
348
349
350
351

  let reload_files cont ~use_shapes =
    try reload_files cont ~use_shapes; true with
    | e ->
        let s = Format.asprintf "%a@." Exn_printer.exn_printer e in
352
        P.notify (Message (Parse_Or_Type_Error s)); false
353
354
355
356
357

  let add_file c ?format fname =
    try add_file c ?format fname; true with
    | e ->
        let s = Format.asprintf "%a@." Exn_printer.exn_printer e in
358
        P.notify (Message (Parse_Or_Type_Error s)); false
359

360
  let debug = Debug.register_flag "itp_server" ~desc:"ITP server"
Clément Fumex's avatar
Clément Fumex committed
361

362
363
364
  type server_data =
    { config : Whyconf.config;
      task_driver : Driver.driver;
365
      sd_provers : Whyconf.config_prover Whyconf.Mprover.t;
366
367
      cont : Controller_itp.controller;
    }
Clément Fumex's avatar
Clément Fumex committed
368

369
  let server_data = ref None
Clément Fumex's avatar
Clément Fumex committed
370

371
  let get_server_data () =
372
373
374
375
376
    match !server_data with
    | None ->
       Format.eprintf "[ITP server] not yet initialized@.";
       exit 1
    | Some x -> x
Clément Fumex's avatar
Clément Fumex committed
377

378
  let task_driver config env =
379
380
381
382
383
384
385
386
387
    try
      let main = Whyconf.get_main config in
      let d = Filename.concat (Whyconf.datadir main)
                              (Filename.concat "drivers" "why3_itp.drv")
      in
      let d = Driver.load_driver env d [] in
      Debug.dprintf debug "[ITP server] driver for task printing loaded@.";
      d
    with e ->
388
389
      Format.eprintf "Fatal error while loading itp driver: %a@." Exn_printer.exn_printer e;
      exit 1
Clément Fumex's avatar
Clément Fumex committed
390

391
392
  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
393

394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
  let init_server config env =
    let provers = Whyconf.get_provers config in
    let c = create_controller env in
    let task_driver = task_driver config env in
    Whyconf.Mprover.iter
      (fun _ p ->
       try
         let d = Driver.load_driver c.controller_env p.Whyconf.driver [] in
         Whyconf.Hprover.add c.controller_provers p.Whyconf.prover (p,d)
       with e ->
         Format.eprintf
           "[ITP server] error loading driver for prover %a: %a@."
           Whyconf.print_prover p.Whyconf.prover
           Exn_printer.exn_printer e)
      provers;
    server_data := Some
                     { config = config;
                       task_driver = task_driver;
412
                       sd_provers = provers;
413
                       cont = c }
Sylvain Dailler's avatar
Sylvain Dailler committed
414

Clément Fumex's avatar
Clément Fumex committed
415

416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
  (**********************)
  (* Read and send file *)
  (**********************)

  let read_and_send f =
    try
      let d = get_server_data() in
      let fn = Sysutil.absolutize_filename
          (Session_itp.get_dir d.cont.controller_session) f in
      let s = Sysutil.file_contents fn in
      P.notify (File_contents (f, s))
    with Invalid_argument s ->
      P.notify (Message (Error s))



Sylvain Dailler's avatar
Sylvain Dailler committed
432
  (* -----------------------------------   ------------------------------------- *)
Clément Fumex's avatar
Clément Fumex committed
433

434
435
436
  let get_node_type (node: any) =
    match node with
    | AFile _ -> NFile
Sylvain Dailler's avatar
Sylvain Dailler committed
437
438
439
440
    | ATh _   -> NTheory
    | ATn _   -> NTransformation
    | APn _   -> NGoal
    | APa _   -> NProofAttempt
441
442

  let get_node_name (node: any) =
443
    let d = get_server_data () in
444
445
446
447
448
449
    match node with
    | AFile file ->
      file.file_name
    | ATh th ->
      (theory_name th).Ident.id_string
    | ATn tn ->
450
      get_transf_name d.cont.controller_session tn
451
    | APn pn ->
452
      (get_proof_name d.cont.controller_session pn).Ident.id_string
453
    | APa pa ->
454
      let pa = get_proof_attempt_node d.cont.controller_session pa in
455
456
      Pp.string_of Whyconf.print_prover pa.prover

457
458
459
460
  let get_node_detached (node: any) =
    let d = get_server_data () in
    is_detached d.cont.controller_session node

461
462
463
  let get_node_proved new_id (node: any) =
    let d = get_server_data () in
    let cont = d.cont in
464
    match node with
465
466
    | AFile file ->
      P.notify (Node_change (new_id, Proved (file_proved cont file)))
467
    | ATh th ->
468
469
470
471
472
473
474
475
476
477
      P.notify (Node_change (new_id, Proved (th_proved cont th)))
    | ATn tn ->
      P.notify (Node_change (new_id, Proved (tn_proved cont tn)))
    | APn pn ->
      P.notify (Node_change (new_id, Proved (pn_proved cont pn)))
    | APa pa ->
      let pa = get_proof_attempt_node cont.controller_session pa in
      let is_obsolete = pa.proof_obsolete in
      let resource_limit = pa.limit in
      begin
478
        match pa.Session_itp.proof_state with
479
480
481
482
483
484
        | Some pa ->
            P.notify (Node_change (
                       new_id, Proof_status_change
                                (Done pa, is_obsolete, resource_limit)))
        | _ -> ()
      end
485

486
(*
Sylvain Dailler's avatar
Sylvain Dailler committed
487
488
489
490
491
  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
492
        NFile, {name; proved}
Sylvain Dailler's avatar
Sylvain Dailler committed
493
494
495
    | ATh th     ->
        let name = (theory_name th).Ident.id_string in
        let proved = th_proved cont th in
496
        NTheory, {name; proved}
Sylvain Dailler's avatar
Sylvain Dailler committed
497
498
499
    | ATn tn     ->
        let name = get_transf_name ses tn in
        let proved = tn_proved cont tn in
500
        NTransformation, {name; proved}
Sylvain Dailler's avatar
Sylvain Dailler committed
501
502
503
    | APn pn     ->
        let name = (get_proof_name ses pn).Ident.id_string in
        let proved = pn_proved cont pn in
504
          NGoal, {name; proved}
Sylvain Dailler's avatar
Sylvain Dailler committed
505
506
507
    | 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
508
509
510
        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
511
        in
Clément Fumex's avatar
Clément Fumex committed
512
513
        (NProofAttempt (pr, pa.proof_obsolete)),
        {name; proved}
514
*)
Sylvain Dailler's avatar
Sylvain Dailler committed
515

516
517
518
519
520
521
522
523
524
525
(* 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
526

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

529
  let any_from_node_ID (nid:node_ID) : any = Hint.find model_any nid
Clément Fumex's avatar
Clément Fumex committed
530
531
532
533
534
535
536
537
538
539
540
541

  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
542

543
544
545
546
547
548
549
550
  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
551
  let get_prover p =
552
    let d = get_server_data () in
553
    match return_prover p d.config with
Sylvain Dailler's avatar
Sylvain Dailler committed
554
555
    | None -> raise (Bad_prover_name p)
    | Some c -> c
Clément Fumex's avatar
Clément Fumex committed
556

Sylvain Dailler's avatar
Sylvain Dailler committed
557
558
559
560
561
562
563
  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
564

Sylvain Dailler's avatar
Sylvain Dailler committed
565
  (* Create a new node in the_tree, update the tables and send a
Clément Fumex's avatar
Clément Fumex committed
566
     notification about it *)
Sylvain Dailler's avatar
Sylvain Dailler committed
567
  let new_node ~parent node : node_ID =
568
569
    let new_id = fresh () in
      Hint.add model_any new_id node;
570
571
      let node_type = get_node_type node in
      let node_name = get_node_name node in
572
      let node_detached = get_node_detached node in
Sylvain Dailler's avatar
Sylvain Dailler committed
573
      add_node_to_table node new_id;
574
      P.notify (New_node (new_id, parent, node_type, node_name, node_detached));
575
576
      if node_type = NFile then
        read_and_send node_name;
577
      get_node_proved new_id node;
Clément Fumex's avatar
Clément Fumex committed
578
579
      new_id

Sylvain Dailler's avatar
Sylvain Dailler committed
580
581
582
  (****************************)
  (* Iter on the session tree *)
  (****************************)
Sylvain Dailler's avatar
Sylvain Dailler committed
583

584
585
586
587
  (* 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 =
588
    let d = get_server_data () in
Clément Fumex's avatar
Clément Fumex committed
589
    Whyconf.Hprover.iter
590
      (fun _pa panid -> f ~parent (APa panid))
591
      (get_proof_attempt_ids d.cont.controller_session id)
Clément Fumex's avatar
Clément Fumex committed
592

593
594
  let rec iter_subtree_from_goal
    (f: parent:node_ID -> any -> unit) parent id =
595
596
    let d = get_server_data () in
    let ses = d.cont.controller_session in
597
598
    f ~parent (APn id);
    let nid = node_ID_from_pn id in
Clément Fumex's avatar
Clément Fumex committed
599
    List.iter
600
      (fun trans_id -> iter_subtree_from_trans f nid trans_id)
Clément Fumex's avatar
Clément Fumex committed
601
      (get_transformations ses id);
602
    iter_subtree_proof_attempt_from_goal f nid id
Clément Fumex's avatar
Clément Fumex committed
603

604
605
  and iter_subtree_from_trans
    (f: parent:node_ID -> any -> unit) parent trans_id =
606
607
    let d = get_server_data () in
    let ses = d.cont.controller_session in
608
609
    f ~parent (ATn trans_id);
    let nid = node_ID_from_tn trans_id in
Clément Fumex's avatar
Clément Fumex committed
610
    List.iter
611
      (fun goal_id -> (iter_subtree_from_goal f nid goal_id))
Sylvain Dailler's avatar
Sylvain Dailler committed
612
      (get_sub_tasks ses trans_id)
Clément Fumex's avatar
Clément Fumex committed
613

614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
  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 =
629
630
    let d = get_server_data () in
    let ses = d.cont.controller_session in
Clément Fumex's avatar
Clément Fumex committed
631
632
633
    let files = get_files ses in
    Stdlib.Hstr.iter
      (fun _ file ->
634
        iter_subtree_from_file f parent file)
Sylvain Dailler's avatar
Sylvain Dailler committed
635
      files
Clément Fumex's avatar
Clément Fumex committed
636

Sylvain Dailler's avatar
Sylvain Dailler committed
637
638
639
640
  (**********************************)
  (* Initialization of session tree *)
  (**********************************)

641
642
  let _init_the_tree (): unit =
    let f ~parent node_id = ignore (new_node ~parent node_id) in
Sylvain Dailler's avatar
Sylvain Dailler committed
643
    iter_the_files f root_node
644
645

  let init_and_send_subtree_from_trans parent trans_id : unit =
Clément Fumex's avatar
Clément Fumex committed
646
647
    iter_subtree_from_trans
      (fun ~parent id -> ignore (new_node ~parent id)) parent trans_id
648

Sylvain Dailler's avatar
Sylvain Dailler committed
649
650
  let init_and_send_file f =
    iter_subtree_from_file (fun ~parent id -> ignore (new_node ~parent id))
Sylvain Dailler's avatar
Sylvain Dailler committed
651
      root_node f
Sylvain Dailler's avatar
Sylvain Dailler committed
652

653
  let init_and_send_the_tree (): unit =
Sylvain Dailler's avatar
Sylvain Dailler committed
654
    iter_the_files (fun ~parent id -> ignore (new_node ~parent id)) root_node
655
656
657
658

  let resend_the_tree (): unit =
    let send_node ~parent any =
      let node_id = node_ID_from_any any in
659
660
      let node_name = get_node_name any in
      let node_type = get_node_type any in
661
      let node_detached = get_node_detached any in
662
663
      P.notify (New_node (node_id, parent, node_type, node_name, node_detached));
      get_node_proved node_id any in
Sylvain Dailler's avatar
Sylvain Dailler committed
664
    iter_the_files send_node root_node
665

Clément Fumex's avatar
Clément Fumex committed
666
  (* -- send the task -- *)
667
668
669
670
671
672
673
  let task_of_id d id =
    let task = get_task d.cont.controller_session id in
    let tables = get_tables d.cont.controller_session id in
    Pp.string_of
      (Driver.print_task ~cntexample:false ?name_table:tables d.task_driver)
      task

Clément Fumex's avatar
Clément Fumex committed
674
  let send_task nid =
675
    let d = get_server_data () in
Clément Fumex's avatar
Clément Fumex committed
676
677
    match any_from_node_ID nid with
    | APn id ->
678
679
680
681
682
683
684
685
686
687
688
689
690
691
       let s = task_of_id d id in
       P.notify (Task (nid,s))
    | ATh t ->
       P.notify (Task (nid, "Theory " ^ (theory_name t).Ident.id_string))
    | APa pid ->
       let parid = get_proof_attempt_parent  d.cont.controller_session pid in
       let s = task_of_id d parid in
       P.notify (Task (nid,s))
    | AFile f ->
       P.notify (Task (nid, "File " ^ f.file_name))
    | ATn tid ->
       let name = get_transf_name d.cont.controller_session tid in
       let args = get_transf_args d.cont.controller_session tid in
       P.notify (Task (nid, "Transformation " ^ String.concat " " (name :: args)))
692

Sylvain Dailler's avatar
Sylvain Dailler committed
693
694
695
696
697
698
699
700
701
702
703
704
  (* -------------------- *)

  (* Add a file into the session when (Add_file_req f) is sent *)
  (* Note that f is the path from execution directory to the file and fn is the
     path from the session directory to the file. *)
  let add_file_to_session cont f =
    let fn = Sysutil.relativize_filename
      (Session_itp.get_dir cont.controller_session) f in
    let files = get_files cont.controller_session in
    if (not (Stdlib.Hstr.mem files fn)) then
      if (Sys.file_exists f) then
      begin
705
706
707
708
        let b = add_file cont f in
        if b then
          let file = Stdlib.Hstr.find files fn in
          init_and_send_file file
Sylvain Dailler's avatar
Sylvain Dailler committed
709
710
      end
      else
MARCHE Claude's avatar
MARCHE Claude committed
711
        P.notify (Message (Open_File_Error ("File not found: " ^ f)))
712
    else
Sylvain Dailler's avatar
Sylvain Dailler committed
713
      P.notify (Message (Open_File_Error ("File already in session: " ^ fn)))
714
715


716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
  (* ------------ init controller ------------ *)

  (* Init cont on file or directory. It is called only when an
     Open_session_req is requested *)
  let init_cont f =
    let d = get_server_data () in
    let prover_list = get_prover_list d.config in
    let transformation_list = List.map fst (list_transforms ()) in
    let strategies_list =
      let l = strategies d.cont.controller_env d.config loaded_strategies in
      List.map (fun (a,_,_,_) -> a) l
    in
    let infos =
      {
        provers = prover_list;
        transformations = transformation_list;
        strategies = strategies_list;
        commands =
          List.map (fun (c,_,_) -> c) commands
      }
    in
    try (
738
      if Sys.file_exists f then
739
        begin
740
          if Sys.is_directory f then
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
            begin
              let b = cont_from_session_dir d.cont f in
              if b then
                P.notify (Initialized infos);
              b
            end
          else
            begin
              let b = cont_from_file d.cont f in
              if b then
                begin
                  add_file_to_session d.cont f;
                  P.notify (Initialized infos);
                end;
              b
            end
        end
      else
        begin
MARCHE Claude's avatar
MARCHE Claude committed
760
          P.notify (Message (Open_File_Error ("File not found: " ^ f)));
761
762
763
764
765
766
767
768
769
770
771
772
          false
        end)
    with
      | NotADirectory f ->
          P.notify (Message (Open_File_Error ("Not a directory: " ^ f))); false
      | BadFileName f ->
          P.notify (Message (Open_File_Error ("Bad file name: " ^ f))); false
      | e ->
          Format.eprintf "%a@." Exn_printer.exn_printer e;
          P.notify (Dead (Pp.string_of Exn_printer.exn_printer e));
          exit 1

Clément Fumex's avatar
Clément Fumex committed
773
774
775
776
777
  (* ----------------- 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
778
    begin match pa_status with
Clément Fumex's avatar
Clément Fumex committed
779
780
781
782
783
784
785
786
787
788
789
    | 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
    | _  -> () (* TODO ? *)
Clément Fumex's avatar
Clément Fumex committed
790
    end;
791
792
793
    let limit = (get_proof_attempt_node cont.controller_session panid).limit in
    let new_status = Proof_status_change (pa_status, false, limit) in
    P.notify (Node_change (node_ID_from_pan panid, new_status))
Clément Fumex's avatar
Clément Fumex committed
794

795
  let notify_change_proved x b =
796
797
    try (
      let node_ID = node_ID_from_any x in
798
      P.notify (Node_change (node_ID, Proved b))
799
800
801
     )
    with Not_found -> ()

Clément Fumex's avatar
Clément Fumex committed
802
  let schedule_proof_attempt nid (p: Whyconf.config_prover) limit =
803
    let d = get_server_data () in
Clément Fumex's avatar
Clément Fumex committed
804
    let prover = p.Whyconf.prover in
805
    let callback = callback_update_tree_proof d.cont in
806
    let unproven_goals = unproven_goals_below_id d.cont (any_from_node_ID nid) in
807
    List.iter (fun id -> C.schedule_proof_attempt d.cont id prover
Sylvain Dailler's avatar
Sylvain Dailler committed
808
                ~limit ~callback ~notification:notify_change_proved)
809
      unproven_goals
Clément Fumex's avatar
Clément Fumex committed
810
811
812
813
814

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

  (* Callback of a transformation *)
  let callback_update_tree_transform status =
815
    let d = get_server_data () in
Clément Fumex's avatar
Clément Fumex committed
816
817
    match status with
    | TSdone trans_id ->
818
      let ses = d.cont.controller_session in
Clément Fumex's avatar
Clément Fumex committed
819
820
      let id = get_trans_parent ses trans_id in
      let nid = node_ID_from_pn id in
821
      init_and_send_subtree_from_trans nid trans_id
Clément Fumex's avatar
Clément Fumex committed
822
823
    | TSfailed (id, e) ->
      let msg =
824
        Pp.sprintf "%a" (get_exception_message d.cont.controller_session id) e
Clément Fumex's avatar
Clément Fumex committed
825
      in
Sylvain Dailler's avatar
Sylvain Dailler committed
826
      P.notify (Message (Transf_error (node_ID_from_pn id, msg)))
Clément Fumex's avatar
Clément Fumex committed
827
828
829
    | _ -> ()

  let rec apply_transform nid t args =
830
    let d = get_server_data () in
Clément Fumex's avatar
Clément Fumex committed
831
832
833
    match any_from_node_ID nid with
    | APn id ->
      let callback = callback_update_tree_transform in
834
      C.schedule_transformation d.cont id t args ~callback ~notification:notify_change_proved
Clément Fumex's avatar
Clément Fumex committed
835
    | APa panid ->
836
      let parent_id = get_proof_attempt_parent d.cont.controller_session panid in
Clément Fumex's avatar
Clément Fumex committed
837
838
839
840
841
842
843
844
845
      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 =
846
    let d = get_server_data () in
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
    let unproven_goals = unproven_goals_below_id d.cont (any_from_node_ID nid) in
    let l = strategies d.cont.controller_env d.config loaded_strategies in
    let st = List.filter (fun (_,c,_,_) -> c=s) l in
    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 d.cont in
       let callback_tr st = callback_update_tree_transform st in
       List.iter (fun id ->
                  C.run_strategy_on_goal d.cont id st ~callback_pa ~callback_tr ~callback ~notification:notify_change_proved)
                 unproven_goals
    | _ -> Format.printf "Strategy '%s' not found@." s
Clément Fumex's avatar
Clément Fumex committed
862

Sylvain Dailler's avatar
Sylvain Dailler committed
863
864
  (* ----------------- Save session --------------------- *)
  let save_session () =
865
866
    let d = get_server_data () in
    Session_itp.save_session d.cont.controller_session;
Sylvain Dailler's avatar
Sylvain Dailler committed
867
868
869
870
    P.notify Saved

  (* ----------------- Reload session ------------------- *)
  let clear_tables () : unit =
871
872
    reset ();
    Hint.clear model_any;
Sylvain Dailler's avatar
Sylvain Dailler committed
873
874
875
876
877
878
879
    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 =
880
    let d = get_server_data () in
Sylvain Dailler's avatar
Sylvain Dailler committed
881
    clear_tables ();
882
883
884
    let b = reload_files d.cont ~use_shapes:true in
    if b then
      init_and_send_the_tree ()
Sylvain Dailler's avatar
Sylvain Dailler committed
885
886

  let replay_session () : unit =
887
    let d = get_server_data () in
888
889
    let callback = fun lr ->
      P.notify (Message (Replay_Info (Pp.string_of C.replay_print lr))) in
Sylvain Dailler's avatar
Sylvain Dailler committed
890
    (* TODO make replay print *)
891
    C.replay ~use_steps:false d.cont ~callback:callback ~remove_obsolete:false
Sylvain Dailler's avatar
Sylvain Dailler committed
892

893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
  (* ----------------- locate next unproven node -------------------- *)

  let notify_first_unproven_node d ni =
    let any = any_from_node_ID ni in
      let unproven_any =
        get_first_unproven_goal_around
          ~proved:(Controller_itp.any_proved d.cont)
          ~children:(get_undetached_children_no_pa d.cont.controller_session)
          ~get_parent:(get_any_parent d.cont.controller_session)
          ~is_goal:(fun any -> match any with | APn _ -> true | _ -> false)
          ~is_pa:(fun any -> match any with | APa _ -> true | _ -> false)
          any in
      begin
        match unproven_any with
        | None -> () (* If no node is found we don't tell IDE to move *)
        | Some any ->
            P.notify (Next_Unproven_Node_Id (ni, node_ID_from_any any))
      end


Clément Fumex's avatar
Clément Fumex committed
913
  (* ----------------- treat_request -------------------- *)
Clément Fumex's avatar
Clément Fumex committed
914

915

Sylvain Dailler's avatar
Sylvain Dailler committed
916
917
918
919
920
921
922
923
  let get_proof_node_id nid =
    try
      match any_from_node_ID nid with
      | APn pn_id -> Some pn_id
      | _ -> None
    with
      Not_found -> None

924
  let rec treat_request r =
925
    let d = get_server_data () in
926
927
    try (
    match r with
928
    | Prove_req (nid,p,limit)      ->
Sylvain Dailler's avatar
Sylvain Dailler committed
929
930
931
932
933
934
935
      let p = try Some (get_prover p) with
      | Bad_prover_name p -> P.notify (Message (Proof_error (nid, "Bad prover name" ^ p))); None
      in
      begin match p with
      | None -> ()
      | Some p -> schedule_proof_attempt nid p limit
      end
936
937
938
    | Transform_req (nid, t, args) -> apply_transform nid t args
    | Strategy_req (nid, st)       -> run_strategy_on_task nid st
    | Save_req                     -> save_session ()
Sylvain Dailler's avatar
Sylvain Dailler committed
939
    | Reload_req                   -> reload_session ()
940
    | Get_Session_Tree_req         -> resend_the_tree ()
941
942
    | Get_first_unproven_node ni   ->
      notify_first_unproven_node d ni
943
944
945
946
947
948
949
950
951
952
    | Remove_subtree nid           ->
        let n = any_from_node_ID nid in
        begin
        try
          Session_itp.remove_subtree d.cont.controller_session n ~notification:(fun x ->
            let nid = node_ID_from_any x in
            P.notify (Remove nid))
        with RemoveError -> (* TODO send an error instead of information *)
          P.notify (Message (Information "Cannot remove a proof node or theory"))
        end
Sylvain Dailler's avatar
Sylvain Dailler committed
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
    | Copy_paste (from_id, to_id)    ->
        let from_any = any_from_node_ID from_id in
        let to_any = any_from_node_ID to_id in
        C.copy_paste ~notification:notify_change_proved
          ~callback_pa:(callback_update_tree_proof d.cont)
          ~callback_tr:(callback_update_tree_transform)
          d.cont from_any to_any

    | Copy_detached from_id        ->
        let from_any = any_from_node_ID from_id in
        let copy ~parent p =
          let parent = node_ID_from_any parent in
          ignore (new_node ~parent p)
        in
        C.copy_detached ~copy d.cont from_any
968
    | Get_file_contents f          ->
969
        read_and_send f
970
971
972
    | Get_task nid                 -> send_task nid
    | Replay_req                   -> replay_session (); resend_the_tree ()
    | Command_req (nid, cmd)       ->
Sylvain Dailler's avatar
Sylvain Dailler committed
973
      begin
Sylvain Dailler's avatar
Sylvain Dailler committed
974
        let snid = get_proof_node_id nid in
975
        match (interp commands commands_table d.config d.cont snid cmd) with
976
        | Transform (s, _t, args) -> treat_request (Transform_req (nid, s, args))
Sylvain Dailler's avatar
Sylvain Dailler committed
977
978
979
980
981
982
        | 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)        ->
Sylvain Dailler's avatar
Sylvain Dailler committed
983
            P.notify (Message (Information ("Unknown command: "^s)))
Sylvain Dailler's avatar
Sylvain Dailler committed
984
      end
Sylvain Dailler's avatar
Sylvain Dailler committed
985
    | Add_file_req f ->
986
987
988
989
      add_file_to_session d.cont f;
      let f = Sysutil.relativize_filename
          (Session_itp.get_dir d.cont.controller_session) f in
      read_and_send f
990
    | Open_session_req file_or_dir_name      ->
991
992
993
994
995
996
997
      begin
        let b = init_cont file_or_dir_name in
        if b then
          reload_session ()
        else
          () (* Eventually print debug here *)
      end
Clément Fumex's avatar
Clément Fumex committed
998
    | Set_max_tasks_req i     -> C.set_max_tasks i
999
    | Exit_req                -> exit 0
1000
1001
     )
    with e -> P.notify (Message (Error (Pp.string_of
1002
1003
1004
      (fun fmt (r,e) -> Format.fprintf fmt
          "There was an unrecoverable error during treatment of request:\n %a\nwith exception: %a"
    print_request r Exn_printer.exn_printer e ) (r, e))))
Clément Fumex's avatar
Clément Fumex committed
1005
1006
1007
1008
1009

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

1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
  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
1022

Clément Fumex's avatar
Clément Fumex committed
1023
1024
1025
1026
  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
1027
1028

end