session_itp.ml 67.5 KB
Newer Older
1 2 3
(********************************************************************)
(*                                                                  *)
(*  The Why3 Verification Platform   /   The Why3 Development Team  *)
Guillaume Melquiond's avatar
Guillaume Melquiond committed
4
(*  Copyright 2010-2019   --   Inria - CNRS - Paris-Sud University  *)
5 6 7 8 9 10
(*                                                                  *)
(*  This software is distributed under the terms of the GNU Lesser  *)
(*  General Public License version 2.1, with the special exception  *)
(*  on linking described in file LICENSE.                           *)
(*                                                                  *)
(********************************************************************)
11

12
open Wstdlib
Clément Fumex's avatar
Clément Fumex committed
13 14 15 16

module Hprover = Whyconf.Hprover

let debug = Debug.register_info_flag "session_itp"
Clément Fumex's avatar
Clément Fumex committed
17 18
    ~desc:"Pring@ debugging@ messages@ about@ Why3@ session@ \
           creation,@ reading@ and@ writing."
Clément Fumex's avatar
Clément Fumex committed
19

20 21
let debug_merge = Debug.lookup_flag "session_pairing"

Sylvain Dailler's avatar
Sylvain Dailler committed
22 23
let debug_stack_trace = Debug.lookup_flag "stack_trace"

Clément Fumex's avatar
Clément Fumex committed
24 25
type transID = int
type proofNodeID = int
26
type proofAttemptID = int
27
type fileID = int
Clément Fumex's avatar
Clément Fumex committed
28

MARCHE Claude's avatar
MARCHE Claude committed
29
let print_proofNodeID fmt id =
MARCHE Claude's avatar
MARCHE Claude committed
30 31 32 33
  Format.fprintf fmt "proofNodeID<%d>" id

let print_proofAttemptID fmt id =
  Format.fprintf fmt "proofAttemptID<%d>" id
MARCHE Claude's avatar
MARCHE Claude committed
34

Clément Fumex's avatar
Clément Fumex committed
35
type theory = {
Clément Fumex's avatar
Clément Fumex committed
36
  theory_name                   : Ident.ident;
MARCHE Claude's avatar
MARCHE Claude committed
37
  mutable theory_goals          : proofNodeID list;
38
  mutable theory_parent_name    : fileID;
39
  mutable theory_is_detached    : bool;
Clément Fumex's avatar
Clément Fumex committed
40 41
}

42
let theory_name t = t.theory_name
43
let theory_goals t = t.theory_goals
44

Clément Fumex's avatar
Clément Fumex committed
45
type proof_parent = Trans of transID | Theory of theory
Clément Fumex's avatar
Clément Fumex committed
46

47 48
type proof_attempt_node = {
  parent                 : proofNodeID;
49
  mutable prover         : Whyconf.prover;
Clément Fumex's avatar
Clément Fumex committed
50 51
  limit                  : Call_provers.resource_limit;
  mutable proof_state    : Call_provers.prover_result option;
Clément Fumex's avatar
Clément Fumex committed
52
  (* None means that the call was not done or never returned *)
Clément Fumex's avatar
Clément Fumex committed
53
  mutable proof_obsolete : bool;
54
  mutable proof_script   : string option;  (* non empty for external ITP *)
Clément Fumex's avatar
Clément Fumex committed
55 56
}

57 58 59 60
let set_proof_script pa file =
  assert (pa.proof_script = None);
  pa.proof_script <- Some file

Clément Fumex's avatar
Clément Fumex committed
61
type proof_node = {
Clément Fumex's avatar
Clément Fumex committed
62
  proofn_name                    : Ident.ident;
MARCHE Claude's avatar
MARCHE Claude committed
63
  proofn_expl                    : string;
Clément Fumex's avatar
Clément Fumex committed
64
  proofn_parent                  : proof_parent;
65
  proofn_attempts                : proofAttemptID Hprover.t;
Clément Fumex's avatar
Clément Fumex committed
66 67 68 69
  mutable proofn_transformations : transID list;
}

type transformation_node = {
Clément Fumex's avatar
Clément Fumex committed
70 71
  transf_name                      : string;
  transf_args                      : string list;
72
  mutable transf_subtasks          : proofNodeID list;
Clément Fumex's avatar
Clément Fumex committed
73
  transf_parent                    : proofNodeID;
74
  transf_is_detached               : bool;
Clément Fumex's avatar
Clément Fumex committed
75 76
}

77 78 79
type file_path = string list
let string_of_file_path p = String.concat "/" p

Clément Fumex's avatar
Clément Fumex committed
80
type file = {
81 82 83 84
  file_id                : int;
  mutable file_path      : file_path;
  (* access path to the source, in the normal order
  i.e. ["..";"foo.mlw"] *)
Clément Fumex's avatar
Clément Fumex committed
85
  file_format            : string option;
86 87
  file_is_detached       : bool;
  mutable file_theories  : theory list;
Clément Fumex's avatar
Clément Fumex committed
88 89
}

90 91
let file_id f = f.file_id
let file_path f = f.file_path
92 93 94
let file_format f = f.file_format
let file_theories f = f.file_theories

95 96 97 98 99 100 101
type any =
  | AFile of file
  | ATh of theory
  | ATn of transID
  | APn of proofNodeID
  | APa of proofAttemptID

102 103 104 105 106 107 108 109
let rec basename p =
  match p with
  | [] -> raise Not_found
  | [f] -> f
  | _ :: tl -> basename tl

let print_file_path fmt p = Format.fprintf fmt "%a" (Pp.print_list Pp.slash Pp.string) p

110 111
let fprintf_any fmt a =
  match a with
112
  | AFile f -> Format.fprintf fmt "<AFile [%a]>" print_file_path f.file_path
113 114 115 116 117
  | ATh th ->  Format.fprintf fmt "<ATh %s>" th.theory_name.Ident.id_string
  | ATn trid -> Format.fprintf fmt "<ATn %d>" trid
  | APn pnid -> Format.fprintf fmt "<APn %d>" pnid
  | APa paid -> Format.fprintf fmt "<APa %d>" paid

118 119
module Hpn = Hint
module Htn = Hint
120
module Hpan = Hint
121
module Hfile = Hint
122

123
type session = {
124 125
  proofAttempt_table            : proof_attempt_node Hint.t;
  mutable next_proofAttemptID   : int;
Clément Fumex's avatar
Clément Fumex committed
126
  proofNode_table               : proof_node Hint.t;
Clément Fumex's avatar
Clément Fumex committed
127 128 129
  mutable next_proofNodeID      : int;
  trans_table                   : transformation_node Hint.t;
  mutable next_transID          : int;
130
  mutable next_fileID           : int;
Clément Fumex's avatar
Clément Fumex committed
131
  session_dir                   : string; (** Absolute path *)
132
  session_files                 : file Hfile.t;
133
  session_sum_shape_table       : (Termcode.checksum * Termcode.shape) Hpn.t;
Clément Fumex's avatar
Clément Fumex committed
134
  session_prover_ids            : int Hprover.t;
135 136
  (* tasks *)
  session_raw_tasks : Task.task Hpn.t;
137
  session_task_tables : Trans.naming_table Hpn.t;
138
  (* proved status *)
139
  file_state: bool Hfile.t;
140 141 142
  th_state: bool Ident.Hid.t;
  tn_state: bool Htn.t;
  pn_state : bool Hpn.t;
Clément Fumex's avatar
Clément Fumex committed
143
}
Clément Fumex's avatar
Clément Fumex committed
144

145
let system_path s f =
MARCHE Claude's avatar
MARCHE Claude committed
146
  Sysutil.system_dependent_absolute_path s.session_dir f.file_path
147

148
let theory_parent s th =
149
  Debug.dprintf debug "[Session_itp.theory_parent] th.parent_name = %d@."
150
                th.theory_parent_name;
151
  Hfile.find s.session_files th.theory_parent_name
152

153
let session_iter_proof_attempt f s =
154
  Hint.iter f s.proofAttempt_table
155

156
(* This is not needed. Keeping it as information on the structure
157 158 159 160 161 162 163
type tree = {
    tree_node_id : proofNodeID;
    tree_goal_name : string;
    tree_proof_attempts : proof_attempt list; (* proof attempts on this node *)
    tree_transformations : (transID * string * tree list) list;
                                (* transformations on this node *)
  }
164
*)
MARCHE Claude's avatar
MARCHE Claude committed
165

166
(*
Clément Fumex's avatar
Clément Fumex committed
167
let rec get_tree s id : tree =
MARCHE Claude's avatar
MARCHE Claude committed
168
  let t = Hint.find s.proofNode_table id in
MARCHE Claude's avatar
MARCHE Claude committed
169
  let pal =
170
    Hprover.fold (fun _ pa acc -> pa.proofa_attempt::acc) t.proofn_attempts []
MARCHE Claude's avatar
MARCHE Claude committed
171
  in
MARCHE Claude's avatar
MARCHE Claude committed
172
  let trl = List.map (get_trans s) t.proofn_transformations in
173 174 175 176 177
  { tree_node_id = id;
    tree_goal_name = t.proofn_name.Ident.id_string;
    tree_proof_attempts = pal;
    tree_transformations = trl;
  }
MARCHE Claude's avatar
MARCHE Claude committed
178 179 180

and get_trans s id =
  let tr = Hint.find s.trans_table id in
Clément Fumex's avatar
Clément Fumex committed
181
  (id, tr.transf_name, List.map (get_tree s) tr.transf_subtasks)
182
*)
MARCHE Claude's avatar
MARCHE Claude committed
183

184
(*
Clément Fumex's avatar
Clément Fumex committed
185
let get_theories s =
MARCHE Claude's avatar
MARCHE Claude committed
186
  Hstr.fold
187
    (fun _fn f acc ->
MARCHE Claude's avatar
MARCHE Claude committed
188 189
     let c =
       List.map
Clément Fumex's avatar
Clément Fumex committed
190
         (fun th -> (th.theory_name.Ident.id_string, th.theory_goals))
MARCHE Claude's avatar
MARCHE Claude committed
191 192
         f.file_theories
     in
193
     (f,c) :: acc)
MARCHE Claude's avatar
MARCHE Claude committed
194
    s.session_files []
195 196 197
 *)

let get_files s = s.session_files
198
(* let get_file s name = Hstr.find s.session_files name *)
Clément Fumex's avatar
Clément Fumex committed
199 200
let get_dir s = s.session_dir

201
(*
Clément Fumex's avatar
Clément Fumex committed
202 203 204 205 206
let get_node (s : session) (n : int) =
  let _ = Hint.find s.proofNode_table n in n

let get_trans (s : session) (n : int) =
  let _ = Hint.find s.trans_table n in n
207
*)
Clément Fumex's avatar
Clément Fumex committed
208

209
(* Generation of new IDs *)
Clément Fumex's avatar
Clément Fumex committed
210 211 212 213 214 215 216 217 218 219
let gen_transID (s : session) =
  let id = s.next_transID in
  s.next_transID <- id + 1;
  id

let gen_proofNodeID (s : session) =
  let id = s.next_proofNodeID in
  s.next_proofNodeID <- id + 1;
  id

220 221 222 223 224
let gen_proofAttemptID (s : session) =
  let id = s.next_proofAttemptID in
  s.next_proofAttemptID <- id + 1;
  id

225 226 227 228 229
let gen_fileID (s : session) =
  let id = s.next_fileID in
  s.next_fileID <- id + 1;
  id

230 231
(* Get elements of the session tree *)

Clément Fumex's avatar
Clément Fumex committed
232
exception BadID
233

234
let get_proof_attempt_node (s : session) (id : proofAttemptID) =
235 236 237 238
  try
    Hint.find s.proofAttempt_table id
  with Not_found -> raise BadID

Clément Fumex's avatar
Clément Fumex committed
239 240 241 242 243
let get_proofNode (s : session) (id : proofNodeID) =
  try
    Hint.find s.proofNode_table id
  with Not_found -> raise BadID

244
let get_task s id =
245 246
  Hpn.find s.session_raw_tasks id

247 248 249 250
let get_task_name_table s n =
  let t = get_task s n in
  let table = try
    Hpn.find s.session_task_tables n
251
  with Not_found ->
252 253 254 255 256
    let ta = Args_wrapper.build_naming_tables t in
    Hpn.add s.session_task_tables n ta;
    ta
  in
  t,table
257

Clément Fumex's avatar
Clément Fumex committed
258 259 260 261 262
let get_transfNode (s : session) (id : transID) =
  try
    Hint.find s.trans_table id
  with Not_found -> raise BadID

Clément Fumex's avatar
Clément Fumex committed
263 264 265
let get_transformations (s : session) (id : proofNodeID) =
  (get_proofNode s id).proofn_transformations

266 267 268
let get_proof_attempt_ids (s : session) (id : proofNodeID) =
  (get_proofNode s id).proofn_attempts

MARCHE Claude's avatar
MARCHE Claude committed
269
let get_proof_attempt_parent (s : session) (a : proofAttemptID) =
270
  (get_proof_attempt_node s a).parent
MARCHE Claude's avatar
MARCHE Claude committed
271

272
let get_proof_attempts (s : session) (id : proofNodeID) =
273
  Hprover.fold (fun _ a l ->
274 275
                let pa = get_proof_attempt_node s a in
                pa :: l)
276
               (get_proofNode s id).proofn_attempts []
277

Clément Fumex's avatar
Clément Fumex committed
278 279 280
let get_sub_tasks (s : session) (id : transID) =
  (get_transfNode s id).transf_subtasks

281 282 283 284
let get_transf_args (s : session) (id : transID) =
  (get_transfNode s id).transf_args

let get_transf_name (s : session) (id : transID) =
285 286 287 288 289
  (get_transfNode s id).transf_name

let get_proof_name (s : session) (id : proofNodeID) =
  (get_proofNode s id).proofn_name

MARCHE Claude's avatar
MARCHE Claude committed
290 291 292
let get_proof_expl (s : session) (id : proofNodeID) =
  (get_proofNode s id).proofn_expl

293 294 295 296 297 298
let get_proof_parent (s : session) (id : proofNodeID) =
  (get_proofNode s id).proofn_parent

let get_trans_parent (s : session) (id : transID) =
  (get_transfNode s id).transf_parent

299
let goal_is_detached s pn =
300
  try let (_:Task.task) = get_task s pn in false
301 302 303 304 305 306 307 308 309 310
  with Not_found -> true

let transf_is_detached s tn =
  (get_transfNode s tn).transf_is_detached

let proof_attempt_is_detached s pa =
  let pa = get_proof_attempt_node s pa in
  goal_is_detached s pa.parent

let is_detached (s: session) (a: any) =
311
  match a with
312
  | AFile file -> file.file_is_detached
313
  | ATh th     -> th.theory_is_detached
314 315 316
  | ATn tn     -> transf_is_detached s tn
  | APn pn     -> goal_is_detached s pn
  | APa pa     -> proof_attempt_is_detached s pa
317

Sylvain Dailler's avatar
Sylvain Dailler committed
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
let rec get_encapsulating_theory s any =
  match any with
  | AFile _f -> assert (false)
  | ATh th -> th
  | ATn tn ->
      let pn_id = get_trans_parent s tn in
      get_encapsulating_theory s (APn pn_id)
  | APn pn ->
      (match get_proof_parent s pn with
      | Theory th -> th
      | Trans tn -> get_encapsulating_theory s (ATn tn)
      )
  | APa pa ->
      let pn = get_proof_attempt_parent s pa in
      get_encapsulating_theory s (APn pn)

let get_encapsulating_file s any =
  match any with
  | AFile f -> f
  | ATh th -> theory_parent s th
  | _ ->
      let th = get_encapsulating_theory s any in
      theory_parent s th

342
(*
343 344 345
let set_obsolete s paid b =
  let pa = get_proof_attempt_node s paid in
  pa.proof_obsolete <- b
346
 *)
347

348 349 350 351 352
let check_if_already_exists s pid t args =
    let sub_transfs = get_transformations s pid in
    List.exists (fun tr_id ->
      get_transf_name s tr_id = t && get_transf_args s tr_id = args &&
      not (is_detached s (ATn tr_id))) sub_transfs
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385

(* Iterations functions on the session tree *)

let rec fold_all_any_of_transn s f acc trid =
  let tr = get_transfNode s trid in
  let acc =
    List.fold_left
      (fold_all_any_of_proofn s f)
      acc tr.transf_subtasks
  in
  f acc (ATn trid)

and fold_all_any_of_proofn s f acc pnid =
  let pn = get_proofNode s pnid in
  let acc =
    List.fold_left
      (fun acc trid ->
        fold_all_any_of_transn s f acc trid)
      acc pn.proofn_transformations
  in
  let acc =
    Hprover.fold
      (fun _p paid acc ->
        f acc (APa paid))
      pn.proofn_attempts acc
  in
  f acc (APn pnid)

let fold_all_any_of_theory s f acc th =
  let acc = List.fold_left (fold_all_any_of_proofn s f) acc th.theory_goals in
  f acc (ATh th)

let fold_all_any_of_file s f acc file =
Sylvain Dailler's avatar
Sylvain Dailler committed
386 387
  let acc =
    List.fold_left (fold_all_any_of_theory s f) acc file.file_theories in
388 389 390 391 392 393 394 395 396 397
  f acc (AFile file)

let fold_all_any s f acc any =
  match any with
  | AFile file -> fold_all_any_of_file s f acc file
  | ATh th -> fold_all_any_of_theory s f acc th
  | APn pn -> fold_all_any_of_proofn s f acc pn
  | ATn tn -> fold_all_any_of_transn s f acc tn
  | APa _ -> f acc any

398 399
let fold_all_session s f acc =
  let files = get_files s in
400
  Hfile.fold (fun _key file acc -> fold_all_any s f acc (AFile file)) files acc
401

402

403 404 405 406 407 408 409 410 411 412 413 414 415
let rec fold_all_sub_goals_of_proofn s f acc pnid =
  let pn = get_proofNode s pnid in
  let acc =
    List.fold_left
      (fun acc trid ->
         let tr = get_transfNode s trid in
         List.fold_left
           (fold_all_sub_goals_of_proofn s f)
           acc tr.transf_subtasks)
      acc pn.proofn_transformations
  in
  f acc pn

MARCHE Claude's avatar
MARCHE Claude committed
416 417 418 419
let goal_iter_proof_attempt s f g =
  fold_all_sub_goals_of_proofn
    s
    (fun _ pn -> Hprover.iter
Sylvain Dailler's avatar
Sylvain Dailler committed
420 421 422
                 (fun _ pa ->
                  let pan = get_proof_attempt_node s pa in
                  f pa pan)
MARCHE Claude's avatar
MARCHE Claude committed
423 424
                 pn.proofn_attempts) () g

425 426 427
let fold_all_sub_goals_of_theory s f acc th =
  List.fold_left (fold_all_sub_goals_of_proofn s f) acc th.theory_goals

MARCHE Claude's avatar
MARCHE Claude committed
428
(*
Clément Fumex's avatar
Clément Fumex committed
429 430
let theory_iter_proofn s f th =
  fold_all_sub_goals_of_theory s (fun _ -> f) () th
MARCHE Claude's avatar
MARCHE Claude committed
431
*)
Clément Fumex's avatar
Clément Fumex committed
432 433

let theory_iter_proof_attempt s f th =
MARCHE Claude's avatar
MARCHE Claude committed
434
  fold_all_sub_goals_of_theory s
Sylvain Dailler's avatar
Sylvain Dailler committed
435 436 437
    (fun _ pn -> Hprover.iter (fun _ pa ->
                             let pan = get_proof_attempt_node s pa in
                             f pa pan)
MARCHE Claude's avatar
MARCHE Claude committed
438 439 440 441 442 443 444
         pn.proofn_attempts) () th

let file_iter_proof_attempt s f file =
  List.iter
    (theory_iter_proof_attempt s f)
    (file_theories file)

Sylvain Dailler's avatar
Sylvain Dailler committed
445 446 447 448 449 450 451 452 453
let any_iter_proof_attempt s f any =
  match any with
  | AFile file -> file_iter_proof_attempt s f file
  | ATh th -> theory_iter_proof_attempt s f th
  | ATn tr ->
      let subgoals = get_sub_tasks s tr in
      List.iter (fun g -> goal_iter_proof_attempt s f g) subgoals
  | APn pn -> goal_iter_proof_attempt s f pn
  | APa pa -> f pa (get_proof_attempt_node s pa)
454 455 456



Sylvain Dailler's avatar
Sylvain Dailler committed
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
(**************)
(* Copy/Paste *)
(**************)

let get_any_parent s a =
  match a with
  | AFile _f -> None
  | ATh th  -> Some (AFile (theory_parent s th))
  | ATn tr  -> Some (APn (get_trans_parent s tr))
  | APn pn  ->
      (match (get_proofNode s pn).proofn_parent with
      | Theory th -> Some (ATh th)
      | Trans tr -> Some (ATn tr))
  | APa pa  ->
      Some (APn (get_proof_attempt_node s pa).parent)

(* True if bid is an ancestor of aid, false if not *)
let rec is_below s (aid: any) (bid: any) =
  aid = bid ||
  match (get_any_parent s aid) with
  | None     -> false
  | Some pid -> is_below s pid bid

Clément Fumex's avatar
Clément Fumex committed
480 481 482
open Format
open Ident

MARCHE Claude's avatar
MARCHE Claude committed
483 484 485
let print_proof_attempt fmt pa =
  fprintf fmt "%a tl=%d %a"
          Whyconf.print_prover pa.prover
486
          pa.limit.Call_provers.limit_time
487 488
          (Pp.print_option (Call_provers.print_prover_result ~json_model:false))
          pa.proof_state
MARCHE Claude's avatar
MARCHE Claude committed
489

490 491
let rec print_proof_node s (fmt: Format.formatter) p =
  let pn = get_proofNode s p in
492
  let sum,_ = Hpn.find s.session_sum_shape_table p in
493 494 495 496 497
  let parent = match pn.proofn_parent with
  | Theory t -> t.theory_name.id_string
  | Trans id -> (get_transfNode s id).transf_name
  in
  fprintf fmt
MARCHE Claude's avatar
MARCHE Claude committed
498
    "@[<hv 1> Goal %s;@ parent %s;@ sum %s;@ @[<hv 1>[%a]@]@ @[<hv 1>[%a]@]@]"
499
    pn.proofn_name.id_string parent
500
    (Termcode.string_of_checksum sum)
501
    (Pp.print_list Pp.semi print_proof_attempt)
502
      (Hprover.fold (fun _key e l ->
503 504 505 506
                     let e = get_proof_attempt_node s e in
                     e :: l)
        pn.proofn_attempts [])
      (Pp.print_list Pp.semi (print_trans_node s)) pn.proofn_transformations
507 508 509

and print_trans_node s fmt id =
  let tn = get_transfNode s id in
510
  let args = get_transf_args s id in
511 512 513
  let name = tn.transf_name in
  let l = tn.transf_subtasks in
  let parent = (get_proofNode s tn.transf_parent).proofn_name.id_string in
Sylvain Dailler's avatar
Sylvain Dailler committed
514 515
  fprintf fmt "@[<hv 1> Trans %s;@ args %a;@ parent %s;@ [%a]@]"
    name (Pp.print_list Pp.colon pp_print_string) args parent
516 517 518 519 520 521 522
    (Pp.print_list Pp.semi (print_proof_node s)) l

let print_theory s fmt th : unit =
  fprintf fmt "@[<hv 2> Theory %s;@ [%a]@]" th.theory_name.Ident.id_string
    (Pp.print_list Pp.semi (fun fmt a -> print_proof_node s fmt a)) th.theory_goals

let print_file s fmt (file, thl) =
523
  fprintf fmt "@[<hv 2> File [%a];@ [%a]@]" print_file_path file.file_path
524 525 526 527
    (Pp.print_list Pp.semi (print_theory s)) thl

let print_s s fmt =
  fprintf fmt "@[%a@]" (Pp.print_list Pp.semi (print_file s))
Clément Fumex's avatar
Clément Fumex committed
528

529
let _print_session fmt s =
530
  let l = Hfile.fold (fun _ f acc -> (f,f.file_theories) :: acc) (get_files s) [] in
MARCHE Claude's avatar
MARCHE Claude committed
531
  fprintf fmt "%a@." (print_s s) l;;
Clément Fumex's avatar
Clément Fumex committed
532

533

534
let empty_session ?from dir =
535
  let prover_ids =
536
    match from with
537 538
    | Some v -> v.session_prover_ids
    | None -> Hprover.create 7
Clément Fumex's avatar
Clément Fumex committed
539
  in
540 541 542
  { proofAttempt_table = Hint.create 97;
    next_proofAttemptID = 0;
    proofNode_table = Hint.create 97;
Clément Fumex's avatar
Clément Fumex committed
543 544 545
    next_proofNodeID = 0;
    trans_table = Hint.create 97;
    next_transID = 0;
546
    next_fileID = 0;
Clément Fumex's avatar
Clément Fumex committed
547
    session_dir = dir;
548
    session_files = Hfile.create 3;
549
    session_prover_ids = prover_ids;
550
    session_raw_tasks = Hpn.create 97;
551
    session_task_tables = Hpn.create 97;
552
    session_sum_shape_table = Hpn.create 97;
553
    file_state = Hfile.create 3;
554
    th_state = Ident.Hid.create 7;
555 556
    tn_state = Htn.create 97;
    pn_state = Hpn.create 97;
Clément Fumex's avatar
Clément Fumex committed
557 558
  }

559 560 561
(**************************************************)
(* proof node/attempt/transformation manipulation *)
(**************************************************)
MARCHE Claude's avatar
MARCHE Claude committed
562

563 564
exception AlreadyExist

MARCHE Claude's avatar
MARCHE Claude committed
565
let add_proof_attempt session prover limit state ~obsolete edit parentID =
566
  let pn = get_proofNode session parentID in
567 568 569 570 571 572 573 574 575 576 577 578 579 580
  try
    let _ = Hprover.find pn.proofn_attempts prover in
    raise AlreadyExist
  with Not_found ->
    let id = gen_proofAttemptID session in
    let pa = { parent = parentID;
               prover = prover;
               limit = limit;
               proof_state = state;
               proof_obsolete = obsolete;
               proof_script = edit } in
    Hprover.add pn.proofn_attempts prover id;
    Hint.replace session.proofAttempt_table id pa;
    id
581

Sylvain Dailler's avatar
Sylvain Dailler committed
582
let graft_proof_attempt ?file (s : session) (id : proofNodeID) (pr : Whyconf.prover)
583
    ~limit =
584 585 586 587
  let pn = get_proofNode s id in
  try
    let id = Hprover.find pn.proofn_attempts pr in
    let pa = Hint.find s.proofAttempt_table id in
Sylvain Dailler's avatar
Sylvain Dailler committed
588 589
    let pa = { pa with limit = limit;
               proof_state = None;
590
               proof_obsolete = false} in
MARCHE Claude's avatar
MARCHE Claude committed
591
    (* Hprover.replace pn.proofn_attempts pr id; useless *)
592 593 594
    Hint.replace s.proofAttempt_table id pa;
    id
  with Not_found ->
MARCHE Claude's avatar
MARCHE Claude committed
595
    add_proof_attempt s pr limit None ~obsolete:false file id
Clément Fumex's avatar
Clément Fumex committed
596

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

Clément Fumex's avatar
Clément Fumex committed
598
(* [mk_proof_node s n t p id] register in the session [s] a proof node
Clément Fumex's avatar
Clément Fumex committed
599
   of proofNodeID [id] of parent [p] of task [t] *)
600
let mk_proof_node ~shape_version ~expl (s : session) (n : Ident.ident) (t : Task.task)
Clément Fumex's avatar
Clément Fumex committed
601 602
    (parent : proof_parent) (node_id : proofNodeID) =
  let pn = { proofn_name = n;
MARCHE Claude's avatar
MARCHE Claude committed
603
             proofn_expl = expl;
Clément Fumex's avatar
Clément Fumex committed
604
             proofn_parent = parent;
Clément Fumex's avatar
Clément Fumex committed
605
             proofn_attempts = Hprover.create 7;
Clément Fumex's avatar
Clément Fumex committed
606
             proofn_transformations = [] } in
607
  Hint.add s.proofNode_table node_id pn;
608
  Hpn.add s.session_raw_tasks node_id t;
609 610 611 612 613
  let sum = Termcode.task_checksum ~version:shape_version t in
  let shape = Termcode.t_shape_task ~version:shape_version ~expl t in
  Hpn.add s.session_sum_shape_table node_id (sum,shape)

let mk_new_proof_node = mk_proof_node ~shape_version:Termcode.current_shape_version
Clément Fumex's avatar
Clément Fumex committed
614

Clément Fumex's avatar
Clément Fumex committed
615
let mk_proof_node_no_task (s : session) (n : Ident.ident)
616
    (parent : proof_parent) (node_id : proofNodeID) sum shape expl proved =
617
  let pn = { proofn_name = n;
618
             proofn_expl = expl;
619 620
             proofn_parent = parent;
             proofn_attempts = Hprover.create 7;
Clément Fumex's avatar
Clément Fumex committed
621
             proofn_transformations = [] } in
622
  Hint.add s.proofNode_table node_id pn;
623
  Hpn.add s.session_sum_shape_table node_id (sum,shape);
624
  Hint.add s.pn_state node_id proved
Clément Fumex's avatar
Clément Fumex committed
625

626
let mk_new_transf_proof_node (s : session) (parent_name : string)
Sylvain Dailler's avatar
Sylvain Dailler committed
627
    (tid : transID) (index : int) (t : Task.task) =
Clément Fumex's avatar
Clément Fumex committed
628
  let id = gen_proofNodeID s in
MARCHE Claude's avatar
MARCHE Claude committed
629
  let gid,expl,_ = Termcode.goal_expl_task ~root:false t in
Clément Fumex's avatar
Clément Fumex committed
630 631
  let goal_name = parent_name ^ "." ^ string_of_int index in
  let goal_name = Ident.id_register (Ident.id_derive goal_name gid) in
632
  mk_new_proof_node ~expl s goal_name t (Trans tid) id;
Clément Fumex's avatar
Clément Fumex committed
633 634
  id

Clément Fumex's avatar
Clément Fumex committed
635
let mk_transf_node (s : session) (id : proofNodeID) (node_id : transID)
636 637
                   (name : string) (args : string list) ~(proved:bool) ~(detached:bool)
                   (pnl : proofNodeID list) =
Clément Fumex's avatar
Clément Fumex committed
638 639 640 641
  let pn = get_proofNode s id in
  let tn = { transf_name = name;
             transf_args = args;
             transf_subtasks = pnl;
Clément Fumex's avatar
Clément Fumex committed
642
             transf_parent = id;
643
             transf_is_detached  = detached;
MARCHE Claude's avatar
MARCHE Claude committed
644 645
           }
  in
Clément Fumex's avatar
Clément Fumex committed
646
  Hint.add s.trans_table node_id tn;
647
  Htn.add s.tn_state node_id proved;
Clément Fumex's avatar
Clément Fumex committed
648 649
  pn.proofn_transformations <- node_id::pn.proofn_transformations

Sylvain Dailler's avatar
Sylvain Dailler committed
650

Clément Fumex's avatar
Clément Fumex committed
651
let graft_transf  (s : session) (id : proofNodeID) (name : string)
652
    (args : string list) (tl : Task.task list) =
Clément Fumex's avatar
Clément Fumex committed
653
  let tid = gen_transID s in
Clément Fumex's avatar
Clément Fumex committed
654
  let parent_name = (get_proofNode s id).proofn_name.Ident.id_string in
655
  let sub_tasks = List.mapi (mk_new_transf_proof_node s parent_name tid) tl in
656
  let proved = sub_tasks = [] in
657
  mk_transf_node s id tid name args ~proved sub_tasks ~detached:false;
Clément Fumex's avatar
Clément Fumex committed
658
  tid
Clément Fumex's avatar
Clément Fumex committed
659

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

661
let update_proof_attempt ?(obsolete=false) notifier s id pr st =
Sylvain Dailler's avatar
Sylvain Dailler committed
662 663
  try
    let n = get_proofNode s id in
664 665
    let paid = Hprover.find n.proofn_attempts pr in
    let pa = get_proof_attempt_node s paid in
Sylvain Dailler's avatar
Sylvain Dailler committed
666
    pa.proof_state <- Some st;
667 668
    pa.proof_obsolete <- obsolete;
    notifier (APa paid)
Sylvain Dailler's avatar
Sylvain Dailler committed
669
  with
670
  | BadID when not (Debug.test_flag debug_stack_trace) -> assert false
Clément Fumex's avatar
Clément Fumex committed
671

672 673 674 675 676 677 678 679 680 681 682 683 684 685

(* proved status *)


let tn_proved s tid = Htn.find_def s.tn_state false tid
let pn_proved s pid = Hpn.find_def s.pn_state false pid
let th_proved s th  =
  try Hid.find s.th_state th.theory_name
  with Not_found ->
    let b = theory_goals th = [] in
    Hid.add s.th_state th.theory_name b;
    b

let file_proved s f =
686
  try Hfile.find s.file_state f.file_id
687 688
  with Not_found ->
    let b = f.file_theories = [] in
689
    Hfile.add s.file_state f.file_id b;
690 691
    b

692 693 694 695 696 697 698 699 700 701 702
let pa_proved s paid =
  let pa = get_proof_attempt_node s paid in
  match pa.proof_state with
  | None -> false
  | Some pa ->
     begin
       match pa.Call_provers.pr_answer with
       | Call_provers.Valid -> true
       | _ -> false
     end

703 704 705 706 707 708
let any_proved s any : bool =
  match any with
  | AFile file -> file_proved s file
  | ATh th -> th_proved s th
  | ATn tn -> tn_proved s tn
  | APn pn -> pn_proved s pn
709
  | APa pa -> pa_proved s pa
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730




(* status update *)


type notifier = any -> unit

let pa_ok pa =
  not pa.proof_obsolete &&
    match pa.proof_state
    with
    | Some { Call_provers.pr_answer = Call_provers.Valid} -> true
    | _ -> false

(* [update_goal_node c id] update the proof status of node id
   update is propagated to parents when required. *)

let update_file_node notification s f =
  let ths = f.file_theories in
731 732 733 734
  if ths = [] then
    (* No updates if ths is empty *)
    ()
  else
735 736 737
    let proved =
      List.for_all (fun th -> th.theory_is_detached || th_proved s th) ths
    in
738 739
    if proved <> file_proved s f then
      begin
740
        Hfile.replace s.file_state f.file_id proved;
741 742
        notification (AFile f);
      end
743 744 745

let update_theory_node notification s th =
  let goals = theory_goals th in
746 747 748
  let proved =
    List.for_all (fun pn -> goal_is_detached s pn || pn_proved s pn) goals
  in
749 750
  if proved <> th_proved s th then
    begin
751 752
      Debug.dprintf debug "[Session] setting theory %s to status proved=%b@."
                    th.theory_name.Ident.id_string proved;
753 754 755 756
      Hid.replace s.th_state (theory_name th) proved;
      notification (ATh th);
      try let p = theory_parent s th in
          update_file_node notification s p
757 758 759
      with Not_found when not (Debug.test_flag Debug.stack_trace) ->
        Format.eprintf "[Fatal] Session_itp.update_theory_node: parent missing@.";
        assert false
760 761 762 763 764
    end

let rec update_goal_node notification s id =
  let tr_list = get_transformations s id in
  let pa_list = get_proof_attempts s id in
765 766 767 768 769 770 771 772 773
  let proved =
    List.exists
      (fun tr -> not (transf_is_detached s tr) &&
                   tn_proved s tr) tr_list
    ||
      List.exists
        (fun pa -> not (goal_is_detached s pa.parent) &&
                     pa_ok pa) pa_list
  in
774 775
  if proved <> pn_proved s id then
    begin
776
      (* too noisy, uncomment if you really need it
777 778
      Debug.dprintf debug "[Session] setting goal node %a to status proved=%b@."
                    print_proofNodeID id proved;
779
       *)
780 781 782 783 784
      Hpn.replace s.pn_state id proved;
      notification (APn id);
      match get_proof_parent s id with
      | Trans trans_id -> update_trans_node notification s trans_id
      | Theory th -> update_theory_node notification s th
785 786 787 788
      | exception Not_found when not (Debug.test_flag Debug.stack_trace) ->
                  Format.eprintf "Session_itp.update_goal_node: parent missing@.";
                  Printexc.print_backtrace stderr;
                  assert false
789 790 791 792
    end

and update_trans_node notification s trid =
  let proof_list = get_sub_tasks s trid in
793
  let proved = List.for_all (fun pn -> goal_is_detached s pn || pn_proved s pn) proof_list in
794 795 796 797 798
  if proved <> tn_proved s trid then
    begin
      Htn.replace s.tn_state trid proved;
      notification (ATn trid);
      update_goal_node notification s (get_trans_parent s trid)
799 800 801 802 803 804 805
    end;
  (* Specific case in which we *always* need to call notification because
     transformation are created with proved=true when they don't have subtasks.
     This means they won't be updated in the next if so the parent goal will
     never get updated. *)
  if proof_list = [] then
    update_goal_node notification s (get_trans_parent s trid)
806 807 808 809 810 811 812 813 814 815 816


let update_any_node s notification a =
  match a with
  | APn id -> update_goal_node notification s id
  | ATn id -> update_trans_node notification s id
  | APa _ -> assert false
  | AFile f -> update_file_node notification s f
  | ATh th -> update_theory_node notification s th


817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
let change_prover notification s id opr npr =
  try
    let n = get_proofNode s id in
    let paid = Hprover.find n.proofn_attempts opr in
    let pa = get_proof_attempt_node s paid in
    Hprover.remove n.proofn_attempts opr;
    pa.prover <- npr;
    pa.proof_obsolete <- true;
    Hprover.add n.proofn_attempts npr paid;
    update_goal_node notification s id
  with
  | Not_found -> ()
  | BadID when not (Debug.test_flag debug_stack_trace) -> assert false



833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
(* Remove elements of the session tree *)

let remove_transformation (s : session) (id : transID) =
  let nt = get_transfNode s id in
  Hint.remove s.trans_table id;
  let pn = get_proofNode s nt.transf_parent in
  let trans_up = List.filter (fun tid -> tid != id) pn.proofn_transformations in
  pn.proofn_transformations <- trans_up

let remove_proof_attempt (s : session) (id : proofNodeID)
    (prover : Whyconf.prover) =
  let pn = get_proofNode s id in
  let pa = Hprover.find pn.proofn_attempts prover in
  Hprover.remove pn.proofn_attempts prover;
  Hint.remove s.proofAttempt_table pa

let remove_proof_attempt_pa s (id: proofAttemptID) =
  let pa = get_proof_attempt_node s id in
  let pn = pa.parent in
  let prover = pa.prover in
  remove_proof_attempt s pn prover

let mark_obsolete s (id: proofAttemptID) =
  let pa = get_proof_attempt_node s id in
  pa.proof_obsolete <- true


exception RemoveError

let remove_subtree ~(notification:notifier) ~(removed:notifier) s (n: any) =
  let remove (n: any) =
    match n with
    | ATn tn -> remove_transformation s tn
    | APa pa -> remove_proof_attempt_pa s pa
867
    | AFile f -> Hfile.remove s.session_files f.file_id
868 869 870 871 872 873 874 875 876 877 878 879 880 881
    | APn pn ->
       let node = Hint.find s.proofNode_table pn in
       Hint.remove s.proofNode_table pn;
       begin
         match node.proofn_parent with
         | Theory th ->
            th.theory_goals <- List.filter ((<>) pn) th.theory_goals
         | Trans tr ->
            let nt = get_transfNode s tr in
            nt.transf_subtasks <- List.filter ((<>) pn) nt.transf_subtasks
       end
    | ATh th ->
       let f = theory_parent s th in
       f.file_theories <- List.filter ((!=) th) f.file_theories
882 883
  in
  match n with
884
  | (AFile _ | APn _ | ATh _) when not (is_detached s n) ->
885 886 887
               raise RemoveError
  | _ ->
     let p = get_any_parent s n in
Sylvain Dailler's avatar
Sylvain Dailler committed
888
     fold_all_any s (fun _ x -> remove x; removed x) () n;
889 890
     Opt.iter (update_any_node s notification) p

Clément Fumex's avatar
Clément Fumex committed
891 892 893 894
(****************************)
(*     session opening      *)
(****************************)

Clément Fumex's avatar
Clément Fumex committed
895 896
let db_filename = "why3session.xml"
let shape_filename = "why3shapes"
Clément Fumex's avatar
Clément Fumex committed
897
let compressed_shape_filename = "why3shapes.gz"
Clément Fumex's avatar
Clément Fumex committed
898 899
let session_dir_for_save = ref "."

Clément Fumex's avatar
Clément Fumex committed
900 901 902 903 904 905
exception LoadError of Xml.element * string
exception SessionFileError of string

let bool_attribute field r def =
  try
    match List.assoc field r.Xml.attributes with
Clément Fumex's avatar
Clément Fumex committed
906 907 908
    | "true" -> true
    | "false" -> false
    | _ -> assert false
Clément Fumex's avatar
Clément Fumex committed
909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933
  with Not_found -> def

let int_attribute_def field r def =
  try
    int_of_string (List.assoc field r.Xml.attributes)
  with Not_found | Invalid_argument _ -> def

let string_attribute_def field r def=
  try
    List.assoc field r.Xml.attributes
  with Not_found -> def

let string_attribute_opt field r =
  try
    Some (List.assoc field r.Xml.attributes)
  with Not_found -> None

let string_attribute field r =
  try
    List.assoc field r.Xml.attributes
  with Not_found ->
    eprintf "[Error] missing required attribute '%s' from element '%s'@."
      field r.Xml.name;
    assert false

934 935 936 937 938 939 940 941 942 943
let default_unknown_result =
     {
       Call_provers.pr_answer = Call_provers.Failure "";
       Call_provers.pr_time = 0.0;
       Call_provers.pr_output = "";
       Call_provers.pr_status = Unix.WEXITED 0;
       Call_provers.pr_steps = -1;
       Call_provers.pr_model = Model_parser.default_model;
     }

Clément Fumex's avatar
Clément Fumex committed
944 945
let load_result r =
  match r.Xml.name with
Clément Fumex's avatar
Clément Fumex committed
946
  | "result" ->
947 948 949 950 951
     let status = string_attribute "status" r in
     let answer =
       match status with
       | "valid" -> Call_provers.Valid
       | "invalid" -> Call_provers.Invalid
952
       | "unknown" -> Call_provers.Unknown ""
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980
       | "timeout" -> Call_provers.Timeout
       | "outofmemory" -> Call_provers.OutOfMemory
       | "failure" -> Call_provers.Failure ""
       | "highfailure" -> Call_provers.HighFailure
       | "steplimitexceeded" -> Call_provers.StepLimitExceeded
       | "stepslimitexceeded" -> Call_provers.StepLimitExceeded
       | s ->
          Warning.emit
            "[Warning] Session.load_result: unexpected status '%s'@." s;
          Call_provers.HighFailure
     in
     let time =
       try float_of_string (List.assoc "time" r.Xml.attributes)
       with Not_found -> 0.0
     in
     let steps =
       try int_of_string (List.assoc "steps" r.Xml.attributes)
       with Not_found -> -1
     in
     {
       Call_provers.pr_answer = answer;
       Call_provers.pr_time = time;
       Call_provers.pr_output = "";
       Call_provers.pr_status = Unix.WEXITED 0;
       Call_provers.pr_steps = steps;
       Call_provers.pr_model = Model_parser.default_model;
     }
  | "undone" | "unedited" -> default_unknown_result
Clément Fumex's avatar
Clément Fumex committed
981 982 983
  | s ->
    Warning.emit "[Warning] Session.load_result: unexpected element '%s'@."
      s;
984
    default_unknown_result
Clément Fumex's avatar
Clément Fumex committed
985 986 987 988 989 990 991

let load_option attr g =
  try Some (List.assoc attr g.Xml.attributes)
  with Not_found -> None

let load_ident elt =
  let name = string_attribute "name" elt in
Andrei Paskevich's avatar
Andrei Paskevich committed
992
  let attrs = List.fold_left
Clément Fumex's avatar
Clément Fumex committed
993 994 995
      (fun acc label ->
         match label with
         | {Xml.name = "label"} ->
Andrei Paskevich's avatar
Andrei Paskevich committed
996 997
           let name = string_attribute "name" label in
           Ident.Sattr.add (Ident.create_attribute name) acc
Clément Fumex's avatar
Clément Fumex committed
998
         | _ -> acc
Andrei Paskevich's avatar
Andrei Paskevich committed
999
      ) Ident.Sattr.empty elt.Xml.elements in
Clément Fumex's avatar
Clément Fumex committed
1000 1001 1002 1003 1004 1005 1006 1007
  let preid =
    try
      let load_exn attr g = List.assoc attr g.Xml.attributes in
      let file = load_exn "locfile" elt in
      let lnum =  int_of_string (load_exn "loclnum" elt) in
      let cnumb = int_of_string (load_exn "loccnumb" elt) in
      let cnume = int_of_string (load_exn "loccnume" elt) in
      let pos = Loc.user_position file lnum cnumb cnume in
Andrei Paskevich's avatar
Andrei Paskevich committed
1008
      Ident.id_user ~attrs name pos
Clément Fumex's avatar
Clément Fumex committed
1009
    with Not_found | Invalid_argument _ ->
Andrei Paskevich's avatar
Andrei Paskevich committed
1010
      Ident.id_fresh ~attrs name in
Clément Fumex's avatar
Clément Fumex committed
1011 1012
  Ident.id_register preid

Clément Fumex's avatar
Clément Fumex committed
1013 1014 1015 1016
(* [load_goal s op p g id] loads the goal of parent [p] from the xml
   [g] of nodeID [id] into the session [s] *)
let rec load_goal session old_provers parent g id =
  match g.Xml.name with
Clément Fumex's avatar
Clément Fumex committed
1017 1018
  | "goal" ->
    let gname = load_ident g in
1019 1020 1021
    (* even if sum and shape are not in the XML file but in the shape
  file, these attributes are there thanks to ~fixattr on
  Xml.from_file *)
MARCHE Claude's avatar
MARCHE Claude committed
1022 1023
    let csum = string_attribute_def "sum" g "" in
    let sum = Termcode.checksum_of_string csum in
Clément Fumex's avatar
Clément Fumex committed
1024 1025 1026 1027
    let shape =
      try Termcode.shape_of_string (List.assoc "shape" g.Xml.attributes)
      with Not_found -> Termcode.shape_of_string ""
    in
1028
    let expl = string_attribute_def "expl" g "" in
1029
    let proved = bool_attribute "proved" g false in
1030
    mk_proof_node_no_task session gname parent id sum shape expl proved;
Clément Fumex's avatar
Clément Fumex committed
1031 1032 1033 1034 1035
    List.iter (load_proof_or_transf session old_provers id) g.Xml.elements;
  | "label" -> ()
  | s ->
    Warning.emit "[Warning] Session.load_goal: unexpected element '%s'@." s

Clément Fumex's avatar
Clément Fumex committed
1036
(* [load_proof_or_transf s op pid a] load either a proof attempt or a
Clément Fumex's avatar
Clément Fumex committed
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
   transformation of parent id [pid] from the xml [a] into the session
   [s] *)
and load_proof_or_transf session old_provers pid a =
  match a.Xml.name with
    | "proof" ->
      begin
        let prover = string_attribute "prover" a in
        try
          let prover = int_of_string prover in
          let (p,timelimit,steplimit,memlimit) = Mint.find prover old_provers in
          let res = match a.Xml.elements with
            | [r] -> load_result r
1049
            | [] -> default_unknown_result
Clément Fumex's avatar
Clément Fumex committed
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
            | _ ->
              Warning.emit "[Error] Too many result elements@.";
              raise (LoadError (a,"too many result elements"))
          in
          let edit = load_option "edited" a in
          let edit = match edit with None | Some "" -> None | _ -> edit in
          let obsolete = bool_attribute "obsolete" a false in
          let timelimit = int_attribute_def "timelimit" a timelimit in
	  let steplimit = int_attribute_def "steplimit" a steplimit in
          let memlimit = int_attribute_def "memlimit" a memlimit in
Clément Fumex's avatar
Clément Fumex committed
1060 1061 1062 1063
          let limit = { Call_provers.limit_time  = timelimit;
                        Call_provers.limit_mem   = memlimit;
                        Call_provers.limit_steps = steplimit; }
          in
MARCHE Claude's avatar
MARCHE Claude committed
1064
          ignore(add_proof_attempt session p limit (Some res) ~obsolete edit pid)
Clément Fumex's avatar
Clément Fumex committed
1065 1066 1067 1068 1069
        with Failure _ | Not_found ->
          Warning.emit "[Error] prover id not listed in header '%s'@." prover;
          raise (LoadError (a,"prover not listing in header"))
      end
    | "transf" ->
1070 1071 1072 1073 1074 1075 1076 1077
      let trname = string_attribute "name" a in
      let rec get_args id =
        match string_attribute_opt ("arg"^(string_of_int id)) a with
        | Some arg -> arg :: (get_args (id+1))
        | None -> []
      in
      let args = get_args 1 in
      let tid = gen_transID session in
1078
      let proved = bool_attribute "proved" a false in
1079 1080 1081
      let subtasks_ids =
        List.rev (List.fold_left
                    (fun goals th ->
1082 1083 1084
                       match th.Xml.name with
                       | "goal" -> (gen_proofNodeID session) :: goals
                       | _ -> goals) [] a.Xml.elements)
1085
      in
1086
      mk_transf_node session pid tid trname args ~proved subtasks_ids ~detached:true;
1087 1088 1089
      List.iter2
        (load_goal session old_provers (Trans tid))
        a.Xml.elements subtasks_ids;
Clément Fumex's avatar
Clément Fumex committed
1090 1091
    | "metas" -> ()
    | "label" -> ()
Clément Fumex's avatar
Clément Fumex committed
1092
    | s ->
1093 1094 1095
      Warning.emit
        "[Warning] Session.load_proof_or_transf: unexpected element '%s'@."
        s
Clément Fumex's avatar
Clément Fumex committed
1096

1097
let load_theory session parent_name old_provers (path,acc) th =
Clément Fumex's avatar
Clément Fumex committed
1098 1099 1100
  match th.Xml.name with
  | "theory" ->
    let thname = load_ident th in
Clément Fumex's avatar
Clément Fumex committed
1101
    let goals = List.rev (List.fold_left (fun goals th -> match th.Xml.name with
Clément Fumex's avatar
Clément Fumex committed
1102
        | "goal" -> (gen_proofNodeID session) :: goals
Clément Fumex's avatar
Clément Fumex committed
1103
        | _ -> goals) [] th.Xml.elements) in
Clément Fumex's avatar
Clément Fumex committed
1104
    let mth = { theory_name = thname;
1105
                theory_is_detached = true;
Clément Fumex's avatar
Clément Fumex committed
1106
                theory_goals = goals;
1107
                theory_parent_name = parent_name;
MARCHE Claude's avatar
MARCHE Claude committed
1108
              } in
Clément Fumex's avatar
Clément Fumex committed
1109 1110 1111
    List.iter2
      (load_goal session old_provers (Theory mth))
      th.Xml.elements goals;
1112 1113
    let proved = bool_attribute "proved" th false in
    Hid.add session.th_state thname proved;
1114 1115 1116 1117
    (path,mth::acc)
  | "path" ->
     let fn = string_attribute "name" th in
     (fn::path,acc)
Clément Fumex's avatar
Clément Fumex committed
1118 1119 1120
  | s ->
    Warning.emit "[Warning] Session.load_theory: unexpected element '%s'@."
      s;
1121
    (path,acc)
Clément Fumex's avatar
Clément Fumex committed
1122

1123
let load_file session old_provers f =
Clément Fumex's avatar
Clément Fumex committed
1124 1125
  match f.Xml.name with
  | "file" ->
1126
    let fn = string_attribute_opt "name" f in
Clément Fumex's avatar
Clément Fumex committed
1127
    let fmt = load_option "format" f in
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
    let fid = gen_fileID session in
    let path,ft =
      List.fold_left
        (load_theory session fid old_provers) ([],[]) f.Xml.elements
    in
    let path = match path,fn with
      | [], Some fn ->
         let l = Sysutil.system_independent_path_of_file fn in
         Debug.dprintf debug "Loaded path from concrete file name: %a@." print_file_path l;
         l
      | [],None -> assert false
      | p,_ -> List.rev p
    in
    let mf = { file_id = fid;
               file_path = path;
Clément Fumex's avatar
Clément Fumex committed
1143
               file_format = fmt;
1144
               file_is_detached = true;