session_itp.ml 56.1 KB
Newer Older
1 2 3 4 5





Clément Fumex's avatar
Clément Fumex committed
6 7 8 9 10
open Stdlib

module Hprover = Whyconf.Hprover

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

Sylvain Dailler's avatar
Sylvain Dailler committed
14 15
let debug_stack_trace = Debug.lookup_flag "stack_trace"

Clément Fumex's avatar
Clément Fumex committed
16 17
type transID = int
type proofNodeID = int
18
type proofAttemptID = int
Clément Fumex's avatar
Clément Fumex committed
19

MARCHE Claude's avatar
MARCHE Claude committed
20 21 22
let print_proofNodeID fmt id =
  Format.fprintf fmt "%d" id

Clément Fumex's avatar
Clément Fumex committed
23
type theory = {
Clément Fumex's avatar
Clément Fumex committed
24 25
  theory_name                   : Ident.ident;
  theory_goals                  : proofNodeID list;
26
  theory_parent_name            : string;
Clément Fumex's avatar
Clément Fumex committed
27 28
  mutable theory_detached_goals : proofNodeID list;
  mutable theory_checksum       : Termcode.checksum option;
Clément Fumex's avatar
Clément Fumex committed
29 30
}

31
let theory_name t = t.theory_name
32
let theory_goals t = t.theory_goals
Clément Fumex's avatar
Clément Fumex committed
33
let theory_detached_goals t = t.theory_detached_goals
34

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

37 38
type proof_attempt_node = {
  parent                 : proofNodeID;
Clément Fumex's avatar
Clément Fumex committed
39 40 41
  prover                 : Whyconf.prover;
  limit                  : Call_provers.resource_limit;
  mutable proof_state    : Call_provers.prover_result option;
Clément Fumex's avatar
Clément Fumex committed
42
  (* None means that the call was not done or never returned *)
Clément Fumex's avatar
Clément Fumex committed
43 44
  mutable proof_obsolete : bool;
  proof_script           : string option;  (* non empty for external ITP *)
Clément Fumex's avatar
Clément Fumex committed
45 46 47
}

type proof_node = {
Clément Fumex's avatar
Clément Fumex committed
48
  proofn_name                    : Ident.ident;
MARCHE Claude's avatar
MARCHE Claude committed
49
  proofn_expl                    : string;
Clément Fumex's avatar
Clément Fumex committed
50
  proofn_task                    : Task.task;
51
  proofn_table                   : Task.names_table option;
Clément Fumex's avatar
Clément Fumex committed
52
  proofn_parent                  : proof_parent;
53
  proofn_checksum                : Termcode.checksum option;
Clément Fumex's avatar
Clément Fumex committed
54
  proofn_shape                   : Termcode.shape;
55
  proofn_attempts                : proofAttemptID Hprover.t;
Clément Fumex's avatar
Clément Fumex committed
56 57 58 59
  mutable proofn_transformations : transID list;
}

type transformation_node = {
Clément Fumex's avatar
Clément Fumex committed
60 61 62 63 64
  transf_name                      : string;
  transf_args                      : string list;
  transf_subtasks                  : proofNodeID list;
  transf_parent                    : proofNodeID;
  mutable transf_detached_subtasks : proofNodeID list;
Clément Fumex's avatar
Clément Fumex committed
65 66 67
}

type file = {
Clément Fumex's avatar
Clément Fumex committed
68 69 70 71
  file_name              : string;
  file_format            : string option;
  file_theories          : theory list;
  file_detached_theories : theory list;
Clément Fumex's avatar
Clément Fumex committed
72 73
}

74 75 76 77 78 79 80
type any =
  | AFile of file
  | ATh of theory
  | ATn of transID
  | APn of proofNodeID
  | APa of proofAttemptID

81 82
module Hpn = Hint
module Htn = Hint
83
module Hpan = Hint
84

85
type session = {
86 87
  proofAttempt_table            : proof_attempt_node Hint.t;
  mutable next_proofAttemptID   : int;
Clément Fumex's avatar
Clément Fumex committed
88
  proofNode_table               : proof_node Hint.t;
Clément Fumex's avatar
Clément Fumex committed
89 90 91
  mutable next_proofNodeID      : int;
  trans_table                   : transformation_node Hint.t;
  mutable next_transID          : int;
Clément Fumex's avatar
Clément Fumex committed
92
  session_dir                   : string; (** Absolute path *)
Clément Fumex's avatar
Clément Fumex committed
93 94 95
  session_files                 : file Hstr.t;
  mutable session_shape_version : int;
  session_prover_ids            : int Hprover.t;
Clément Fumex's avatar
Clément Fumex committed
96
}
Clément Fumex's avatar
Clément Fumex committed
97

98 99 100
let theory_parent s th =
  Hstr.find s.session_files th.theory_parent_name

101
let session_iter_proof_attempt f s =
102
  Hint.iter f s.proofAttempt_table
103

104
(* This is not needed. Keeping it as information on the structure
105 106 107 108 109 110 111
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 *)
  }
112
*)
MARCHE Claude's avatar
MARCHE Claude committed
113

114
(*
Clément Fumex's avatar
Clément Fumex committed
115
let rec get_tree s id : tree =
MARCHE Claude's avatar
MARCHE Claude committed
116
  let t = Hint.find s.proofNode_table id in
MARCHE Claude's avatar
MARCHE Claude committed
117
  let pal =
118
    Hprover.fold (fun _ pa acc -> pa.proofa_attempt::acc) t.proofn_attempts []
MARCHE Claude's avatar
MARCHE Claude committed
119
  in
MARCHE Claude's avatar
MARCHE Claude committed
120
  let trl = List.map (get_trans s) t.proofn_transformations in
121 122 123 124 125
  { 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
126 127 128

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

132
(*
Clément Fumex's avatar
Clément Fumex committed
133
let get_theories s =
MARCHE Claude's avatar
MARCHE Claude committed
134
  Hstr.fold
135
    (fun _fn f acc ->
MARCHE Claude's avatar
MARCHE Claude committed
136 137
     let c =
       List.map
Clément Fumex's avatar
Clément Fumex committed
138
         (fun th -> (th.theory_name.Ident.id_string, th.theory_goals))
MARCHE Claude's avatar
MARCHE Claude committed
139 140
         f.file_theories
     in
141
     (f,c) :: acc)
MARCHE Claude's avatar
MARCHE Claude committed
142
    s.session_files []
143 144 145
 *)

let get_files s = s.session_files
146
let get_file s name = Hstr.find s.session_files name
Clément Fumex's avatar
Clément Fumex committed
147 148 149
let get_dir s = s.session_dir

let get_shape_version s = s.session_shape_version
MARCHE Claude's avatar
MARCHE Claude committed
150

151
(*
Clément Fumex's avatar
Clément Fumex committed
152 153 154 155 156
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
157
*)
Clément Fumex's avatar
Clément Fumex committed
158

159
(* Generation of new IDs *)
Clément Fumex's avatar
Clément Fumex committed
160 161 162 163 164 165 166 167 168 169
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

170 171 172 173 174
let gen_proofAttemptID (s : session) =
  let id = s.next_proofAttemptID in
  s.next_proofAttemptID <- id + 1;
  id

175 176
(* Get elements of the session tree *)

Clément Fumex's avatar
Clément Fumex committed
177
exception BadID
178

179
let get_proof_attempt_node (s : session) (id : proofAttemptID) =
180 181 182 183
  try
    Hint.find s.proofAttempt_table id
  with Not_found -> raise BadID

Clément Fumex's avatar
Clément Fumex committed
184 185 186 187 188
let get_proofNode (s : session) (id : proofNodeID) =
  try
    Hint.find s.proofNode_table id
  with Not_found -> raise BadID

189 190 191 192
let get_task (s:session) (id:proofNodeID) =
  let node = get_proofNode s id in
  node.proofn_task

193
let get_table (s: session) (id: proofNodeID) =
194 195 196
  let node = get_proofNode s id in
  node.proofn_table

Clément Fumex's avatar
Clément Fumex committed
197 198 199 200 201
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
202 203 204
let get_transformations (s : session) (id : proofNodeID) =
  (get_proofNode s id).proofn_transformations

205 206 207
let get_proof_attempt_ids (s : session) (id : proofNodeID) =
  (get_proofNode s id).proofn_attempts

MARCHE Claude's avatar
MARCHE Claude committed
208
let get_proof_attempt_parent (s : session) (a : proofAttemptID) =
209
  (get_proof_attempt_node s a).parent
MARCHE Claude's avatar
MARCHE Claude committed
210

211
let get_proof_attempts (s : session) (id : proofNodeID) =
212
  Hprover.fold (fun _ a l ->
213 214
                let pa = get_proof_attempt_node s a in
                pa :: l)
215
               (get_proofNode s id).proofn_attempts []
216

Clément Fumex's avatar
Clément Fumex committed
217 218 219
let get_sub_tasks (s : session) (id : transID) =
  (get_transfNode s id).transf_subtasks

Clément Fumex's avatar
Clément Fumex committed
220 221 222
let get_detached_sub_tasks (s : session) (id : transID) =
  (get_transfNode s id).transf_detached_subtasks

223 224 225 226
let get_transf_args (s : session) (id : transID) =
  (get_transfNode s id).transf_args

let get_transf_name (s : session) (id : transID) =
227 228 229 230 231
  (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
232 233 234
let get_proof_expl (s : session) (id : proofNodeID) =
  (get_proofNode s id).proofn_expl

235 236 237 238 239 240
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

241 242 243 244 245 246 247 248 249 250 251 252 253 254
(* TODO to be done with detached transformations *)
let get_detached_trans (_s: session) (_id: proofNodeID) =
  []

let is_detached (s: session) (a: any) =
  match a with
  | AFile _file -> false
  | ATh th     ->
    let parent_name = th.theory_parent_name in
    let parent = Hstr.find s.session_files parent_name in
    List.exists (fun x -> x = th) parent.file_detached_theories
  | ATn tn     ->
    let pn_id = get_trans_parent s tn in
    let pn = get_proofNode s pn_id in
Sylvain Dailler's avatar
Sylvain Dailler committed
255 256
    pn.proofn_task = None ||
    List.exists (fun x -> x = tn) (get_detached_trans s pn_id)
257 258 259 260 261 262 263
  | APn pn     ->
    let pn = get_proofNode s pn in
    pn.proofn_task = None
  | APa pa     ->
    let pa = get_proof_attempt_node s pa in
    let pn_id = pa.parent in
    let pn = get_proofNode s pn_id in
264
    pn.proofn_task = None
265

Sylvain Dailler's avatar
Sylvain Dailler committed
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
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

290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312

(* 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

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

317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
(* 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
349 350
  let acc =
    List.fold_left (fold_all_any_of_theory s f) acc file.file_theories in
351 352 353 354 355 356 357 358 359 360 361 362
  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

exception RemoveError

363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
(* Cannot remove a proof_attempt that was scheduled but did not finish yet.
   It can be interrupted though. *)
let removable_proof_attempt s pa =
  let pa = get_proof_attempt_node s pa in
  match pa.proof_state with
  | None -> false
  | Some _pr -> true

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

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

381 382 383 384 385 386 387 388 389 390 391 392
let remove_subtree s (n: any) ~notification : unit =

  let remove s (n: any) =
    (* These removal functions should not be used for direct removal: subtrees
       must be removed first.  *)
    let remove_file s (f: file) =
      Hstr.remove s.session_files f.file_name in
    let remove_proof_node s pnid =
      Hint.remove s.proofNode_table pnid in
    let remove_theory _s (_th: theory) =
      (* Not in any table *)
      () in
393 394 395 396 397 398
    match n with
    | ATn tn -> remove_transformation s tn
    | APa pa -> remove_proof_attempt_pa s pa
    | AFile f -> remove_file s f
    | APn pn -> remove_proof_node s pn
    | ATh th -> remove_theory s th
399
  in
400 401 402 403 404

  (* If a subtree cannot be removed then fail *)
  if not (check_removable s n) then
    raise RemoveError;

405
  match n with
406 407
  | APn _pn when not (is_detached s n) -> raise RemoveError
  | ATh _th when not (is_detached s n) -> raise RemoveError
Sylvain Dailler's avatar
Sylvain Dailler committed
408
  | _ ->
409
    fold_all_any s (fun _ x -> remove s x; notification x) () n
410

411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
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

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

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

let theory_iter_proof_attempt s f th =
  theory_iter_proofn s
432
    (fun pn -> Hprover.iter (fun _ pan ->
433 434 435
                             let pan = get_proof_attempt_node s pan in
                             f pan)
         pn.proofn_attempts) th
Clément Fumex's avatar
Clément Fumex committed
436

Sylvain Dailler's avatar
Sylvain Dailler committed
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
(**************)
(* 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
460 461 462
open Format
open Ident

MARCHE Claude's avatar
MARCHE Claude committed
463 464 465
let print_proof_attempt fmt pa =
  fprintf fmt "%a tl=%d %a"
          Whyconf.print_prover pa.prover
466
          pa.limit.Call_provers.limit_time
MARCHE Claude's avatar
MARCHE Claude committed
467 468
          (Pp.print_option Call_provers.print_prover_result) pa.proof_state

469 470 471 472 473 474 475
let rec print_proof_node s (fmt: Format.formatter) p =
  let pn = get_proofNode s p in
  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
476
    "@[<hv 1> Goal %s;@ parent %s;@ sum %s;@ @[<hv 1>[%a]@]@ @[<hv 1>[%a]@]@]"
477 478 479
    pn.proofn_name.id_string parent
    (Opt.fold (fun _ a -> Termcode.string_of_checksum a) "None" pn.proofn_checksum)
    (Pp.print_list Pp.semi print_proof_attempt)
480
      (Hprover.fold (fun _key e l ->
481 482 483 484
                     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
485 486 487

and print_trans_node s fmt id =
  let tn = get_transfNode s id in
488
  let args = get_transf_args s id in
489 490 491
  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
492 493
  fprintf fmt "@[<hv 1> Trans %s;@ args %a;@ parent %s;@ [%a]@]"
    name (Pp.print_list Pp.colon pp_print_string) args parent
494 495 496 497 498 499 500 501 502 503 504 505
    (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) =
  fprintf fmt "@[<hv 2> File %s;@ [%a]@]" file.file_name
    (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
506

507
let _print_session fmt s =
508
  let l = Hstr.fold (fun _ f acc -> (f,f.file_theories) :: acc) (get_files s) [] in
MARCHE Claude's avatar
MARCHE Claude committed
509
  fprintf fmt "%a@." (print_s s) l;;
Clément Fumex's avatar
Clément Fumex committed
510

511

Clément Fumex's avatar
Clément Fumex committed
512
let empty_session ?shape_version dir =
Clément Fumex's avatar
Clément Fumex committed
513 514 515 516
  let shape_version = match shape_version with
    | Some v -> v
    | None -> Termcode.current_shape_version
  in
517 518 519
  { proofAttempt_table = Hint.create 97;
    next_proofAttemptID = 0;
    proofNode_table = Hint.create 97;
Clément Fumex's avatar
Clément Fumex committed
520 521 522
    next_proofNodeID = 0;
    trans_table = Hint.create 97;
    next_transID = 0;
Clément Fumex's avatar
Clément Fumex committed
523
    session_dir = dir;
Clément Fumex's avatar
Clément Fumex committed
524 525 526 527 528
    session_files = Hstr.create 3;
    session_shape_version = shape_version;
    session_prover_ids = Hprover.create 7;
  }

529 530 531
(**************************************************)
(* proof node/attempt/transformation manipulation *)
(**************************************************)
MARCHE Claude's avatar
MARCHE Claude committed
532

533 534
exception AlreadyExist

535 536
let add_proof_attempt session prover limit state obsolete edit parentID =
  let pn = get_proofNode session parentID in
537 538 539 540 541 542 543 544 545 546 547 548 549 550
  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
551

Sylvain Dailler's avatar
Sylvain Dailler committed
552
let graft_proof_attempt ?file (s : session) (id : proofNodeID) (pr : Whyconf.prover)
553
    ~limit =
554 555 556 557
  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
558 559
    let pa = { pa with limit = limit;
               proof_state = None;
560
               proof_obsolete = false} in
561 562 563
    Hint.replace s.proofAttempt_table id pa;
    id
  with Not_found ->
Sylvain Dailler's avatar
Sylvain Dailler committed
564
    add_proof_attempt s pr limit None false file id
Clément Fumex's avatar
Clément Fumex committed
565

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

Clément Fumex's avatar
Clément Fumex committed
567
(* [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
568
   of proofNodeID [id] of parent [p] of task [t] *)
MARCHE Claude's avatar
MARCHE Claude committed
569
let mk_proof_node ~version ~expl (s : session) (n : Ident.ident) (t : Task.task)
Clément Fumex's avatar
Clément Fumex committed
570
    (parent : proof_parent) (node_id : proofNodeID) =
571
  let tables = Args_wrapper.build_name_tables t in
Clément Fumex's avatar
Clément Fumex committed
572
  let sum = Some (Termcode.task_checksum ~version t) in
MARCHE Claude's avatar
MARCHE Claude committed
573
  let shape = Termcode.t_shape_task ~version ~expl t in
Clément Fumex's avatar
Clément Fumex committed
574
  let pn = { proofn_name = n;
MARCHE Claude's avatar
MARCHE Claude committed
575
             proofn_expl = expl;
Clément Fumex's avatar
Clément Fumex committed
576
             proofn_task = t;
577
             proofn_table = Some tables;
Clément Fumex's avatar
Clément Fumex committed
578
             proofn_parent = parent;
579
             proofn_checksum = sum;
Clément Fumex's avatar
Clément Fumex committed
580
             proofn_shape = shape;
Clément Fumex's avatar
Clément Fumex committed
581
             proofn_attempts = Hprover.create 7;
Clément Fumex's avatar
Clément Fumex committed
582
             proofn_transformations = [] } in
Clément Fumex's avatar
Clément Fumex committed
583
  Hint.add s.proofNode_table node_id pn
Clément Fumex's avatar
Clément Fumex committed
584

Clément Fumex's avatar
Clément Fumex committed
585
let mk_proof_node_no_task (s : session) (n : Ident.ident)
Clément Fumex's avatar
Clément Fumex committed
586
    (parent : proof_parent) (node_id : proofNodeID) sum shape =
587
  let pn = { proofn_name = n;
MARCHE Claude's avatar
MARCHE Claude committed
588
             proofn_expl = "";
589
             proofn_task = None;
590
             proofn_table = None;
591 592
             proofn_parent = parent;
             proofn_checksum = sum;
Clément Fumex's avatar
Clément Fumex committed
593
             proofn_shape = shape;
594
             proofn_attempts = Hprover.create 7;
Clément Fumex's avatar
Clément Fumex committed
595
             proofn_transformations = [] } in
596
  Hint.add s.proofNode_table node_id pn
Clément Fumex's avatar
Clément Fumex committed
597

Sylvain Dailler's avatar
Sylvain Dailler committed
598 599 600 601 602 603 604 605 606 607 608 609 610 611
(* Detach a new proof to a proof_parent *)
let graft_detached_proof_on_parent s (pn: proofNodeID) (parent: proof_parent) =
  match parent with
  | Theory th ->
      th.theory_detached_goals <- th.theory_detached_goals @ [pn]
  | Trans tr_id ->
      let tr = get_transfNode s tr_id in
      tr.transf_detached_subtasks <- tr.transf_detached_subtasks @ [pn]

(* Intended as a feature to save a proof (also for testing detached stuff) *)
let copy_proof_node_as_detached (s: session) (pn_id: proofNodeID) =
  let pn = get_proofNode s pn_id in
  let new_pn_id = gen_proofNodeID s in
  let parent = pn.proofn_parent in
Sylvain Dailler's avatar
Sylvain Dailler committed
612 613 614
  let new_goal = Ident.id_register (Ident.id_clone pn.proofn_name) in
  let checksum = pn.proofn_checksum in
  let shape = pn.proofn_shape in
615
  let _: unit = mk_proof_node_no_task s new_goal parent new_pn_id checksum shape in
Sylvain Dailler's avatar
Sylvain Dailler committed
616 617 618
  graft_detached_proof_on_parent s new_pn_id parent;
  new_pn_id

619
let _mk_proof_node_task (s : session) (t : Task.task)
Clément Fumex's avatar
Clément Fumex committed
620 621
    (parent : proof_parent) (node_id : proofNodeID) =
  let name,_,_ = Termcode.goal_expl_task ~root:false t in
Clément Fumex's avatar
Clément Fumex committed
622
  mk_proof_node ~version:s.session_shape_version s name t parent node_id
Clément Fumex's avatar
Clément Fumex committed
623

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

Clément Fumex's avatar
Clément Fumex committed
634
let mk_transf_node (s : session) (id : proofNodeID) (node_id : transID)
635
    (name : string) (args : string list) (pnl : proofNodeID list) =
Clément Fumex's avatar
Clément Fumex committed
636 637 638 639
  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
640 641
             transf_parent = id;
             transf_detached_subtasks = [] } in
Clément Fumex's avatar
Clément Fumex committed
642 643 644
  Hint.add s.trans_table node_id tn;
  pn.proofn_transformations <- node_id::pn.proofn_transformations

645
exception BadCopyDetached of string
Sylvain Dailler's avatar
Sylvain Dailler committed
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661

let rec copy_structure ~notification s from_any to_any : unit =
  match from_any, to_any with
  | APn from_id, APn to_id ->
    let transformations = get_transformations s from_id in
    let new_transformations =
      List.map (fun x ->
        let tr_id = gen_transID s in
        let old_tr = get_transfNode s x in
        mk_transf_node s to_id tr_id old_tr.transf_name old_tr.transf_args [];
        notification ~parent:to_any (ATn tr_id);
        copy_structure ~notification s (ATn x) (ATn tr_id);
        tr_id) transformations in
    (get_proofNode s to_id).proofn_transformations <- new_transformations;
    Hprover.iter (fun _k old_pa ->
      let old_pa = get_proof_attempt_node s old_pa in
Sylvain Dailler's avatar
Sylvain Dailler committed
662 663 664 665
      let pa_id =
        add_proof_attempt s old_pa.prover old_pa.limit None true None to_id in
      notification ~parent:to_any (APa pa_id))
      (get_proofNode s from_id).proofn_attempts
Sylvain Dailler's avatar
Sylvain Dailler committed
666 667 668 669 670 671 672 673 674 675 676 677 678 679
  | ATn from_tn, ATn to_tn ->
    let sub_tasks = get_sub_tasks s from_tn in
    let new_sub_tasks =
      List.map (fun old_pn_id ->
        let old_pn = get_proofNode s old_pn_id in
        let pn_id = gen_proofNodeID s in
        let new_id = Ident.id_register (Ident.id_clone old_pn.proofn_name) in
        mk_proof_node_no_task s new_id (Trans to_tn)
          pn_id old_pn.proofn_checksum old_pn.proofn_shape;
        notification ~parent:to_any (APn pn_id);
        copy_structure ~notification s (APn old_pn_id) (APn pn_id);
        pn_id) sub_tasks in
    let tr = get_transfNode s to_tn in
    tr.transf_detached_subtasks <- new_sub_tasks
680
  | _ -> raise (BadCopyDetached "copy_structure")
Sylvain Dailler's avatar
Sylvain Dailler committed
681

Clément Fumex's avatar
Clément Fumex committed
682
let graft_transf  (s : session) (id : proofNodeID) (name : string)
683
    (args : string list) (tl : Task.task list) =
Clément Fumex's avatar
Clément Fumex committed
684
  let tid = gen_transID s in
Clément Fumex's avatar
Clément Fumex committed
685 686
  let parent_name = (get_proofNode s id).proofn_name.Ident.id_string in
  let sub_tasks = List.mapi (mk_transf_proof_node s parent_name tid) tl in
687
  mk_transf_node s id tid name args sub_tasks;
Clément Fumex's avatar
Clément Fumex committed
688
  tid
Clément Fumex's avatar
Clément Fumex committed
689

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

691
let update_proof_attempt ?(obsolete=false) s id pr st =
Sylvain Dailler's avatar
Sylvain Dailler committed
692 693 694 695 696
  try
    let n = get_proofNode s id in
    let pa = Hprover.find n.proofn_attempts pr in
    let pa = get_proof_attempt_node s pa in
    pa.proof_state <- Some st;
697
    pa.proof_obsolete <- obsolete
Sylvain Dailler's avatar
Sylvain Dailler committed
698
  with
699
  | BadID when not (Debug.test_flag debug_stack_trace) -> assert false
Clément Fumex's avatar
Clément Fumex committed
700 701 702 703 704

(****************************)
(*     session opening      *)
(****************************)

Clément Fumex's avatar
Clément Fumex committed
705 706
let db_filename = "why3session.xml"
let shape_filename = "why3shapes"
Clément Fumex's avatar
Clément Fumex committed
707
let compressed_shape_filename = "why3shapes.gz"
Clément Fumex's avatar
Clément Fumex committed
708 709
let session_dir_for_save = ref "."

Clément Fumex's avatar
Clément Fumex committed
710 711 712 713 714 715
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
716 717 718
    | "true" -> true
    | "false" -> false
    | _ -> assert false
Clément Fumex's avatar
Clément Fumex committed
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
  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

let load_result r =
  match r.Xml.name with
Clément Fumex's avatar
Clément Fumex committed
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785
  | "result" ->
    let status = string_attribute "status" r in
    let answer =
      match status with
      | "valid" -> Call_provers.Valid
      | "invalid" -> Call_provers.Invalid
      | "unknown" -> Call_provers.Unknown ("", None)
      | "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
    Some {
      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" -> None
  | "unedited" -> None
  | s ->
    Warning.emit "[Warning] Session.load_result: unexpected element '%s'@."
      s;
    None
Clément Fumex's avatar
Clément Fumex committed
786 787 788 789 790 791 792 793

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
  let label = List.fold_left
Clément Fumex's avatar
Clément Fumex committed
794 795 796 797 798 799 800
      (fun acc label ->
         match label with
         | {Xml.name = "label"} ->
           let lab = string_attribute "name" label in
           Ident.Slab.add (Ident.create_label lab) acc
         | _ -> acc
      ) Ident.Slab.empty elt.Xml.elements in
Clément Fumex's avatar
Clément Fumex committed
801 802 803 804 805 806 807 808 809 810 811 812 813
  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
      Ident.id_user ~label name pos
    with Not_found | Invalid_argument _ ->
      Ident.id_fresh ~label name in
  Ident.id_register preid

Clément Fumex's avatar
Clément Fumex committed
814 815 816 817
(* [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
818 819
  | "goal" ->
    let gname = load_ident g in
820 821
    let csum = string_attribute_opt "sum" g in
    let sum = Opt.map Termcode.checksum_of_string csum in
Clément Fumex's avatar
Clément Fumex committed
822 823 824 825 826
    let shape =
      try Termcode.shape_of_string (List.assoc "shape" g.Xml.attributes)
      with Not_found -> Termcode.shape_of_string ""
    in
    mk_proof_node_no_task session gname parent id sum shape;
Clément Fumex's avatar
Clément Fumex committed
827 828 829 830 831
    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
832
(* [load_proof_or_transf s op pid a] load either a proof attempt or a
Clément Fumex's avatar
Clément Fumex committed
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
   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
            | [] -> None
            | _ ->
              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
856 857 858 859
          let limit = { Call_provers.limit_time  = timelimit;
                        Call_provers.limit_mem   = memlimit;
                        Call_provers.limit_steps = steplimit; }
          in
860
          ignore(add_proof_attempt session p limit res obsolete edit pid)
Clément Fumex's avatar
Clément Fumex committed
861 862 863 864 865
        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" ->
866 867 868 869 870 871 872 873 874 875 876
      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
      let subtasks_ids =
        List.rev (List.fold_left
                    (fun goals th ->
877 878 879
                       match th.Xml.name with
                       | "goal" -> (gen_proofNodeID session) :: goals
                       | _ -> goals) [] a.Xml.elements)
880 881 882 883 884
      in
      mk_transf_node session pid tid trname args subtasks_ids;
      List.iter2
        (load_goal session old_provers (Trans tid))
        a.Xml.elements subtasks_ids;
Clément Fumex's avatar
Clément Fumex committed
885 886
    | "metas" -> ()
    | "label" -> ()
Clément Fumex's avatar
Clément Fumex committed
887
    | s ->
888 889 890
      Warning.emit
        "[Warning] Session.load_proof_or_transf: unexpected element '%s'@."
        s
Clément Fumex's avatar
Clément Fumex committed
891

892
let load_theory session parent_name old_provers acc th =
Clément Fumex's avatar
Clément Fumex committed
893 894 895 896 897
  match th.Xml.name with
  | "theory" ->
    let thname = load_ident th in
    let csum = string_attribute_opt "sum" th in
    let checksum = Opt.map Termcode.checksum_of_string csum in
Clément Fumex's avatar
Clément Fumex committed
898
    let goals = List.rev (List.fold_left (fun goals th -> match th.Xml.name with
Clément Fumex's avatar
Clément Fumex committed
899
        | "goal" -> (gen_proofNodeID session) :: goals
Clément Fumex's avatar
Clément Fumex committed
900
        | _ -> goals) [] th.Xml.elements) in
Clément Fumex's avatar
Clément Fumex committed
901 902
    let mth = { theory_name = thname;
                theory_checksum = checksum;
Clément Fumex's avatar
Clément Fumex committed
903
                theory_goals = goals;
904
                theory_parent_name = parent_name;
Clément Fumex's avatar
Clément Fumex committed
905
                theory_detached_goals = [] } in
Clément Fumex's avatar
Clément Fumex committed
906 907 908 909 910 911 912 913 914
    List.iter2
      (load_goal session old_provers (Theory mth))
      th.Xml.elements goals;
    mth::acc
  | s ->
    Warning.emit "[Warning] Session.load_theory: unexpected element '%s'@."
      s;
    acc

915
let load_file session old_provers f =
Clément Fumex's avatar
Clément Fumex committed
916 917 918 919 920 921
  match f.Xml.name with
  | "file" ->
    let fn = string_attribute "name" f in
    let fmt = load_option "format" f in
    let ft = List.rev
        (List.fold_left
922
           (load_theory session fn old_provers) [] f.Xml.elements) in
Clément Fumex's avatar
Clément Fumex committed
923 924
    let mf = { file_name = fn;
               file_format = fmt;
Clément Fumex's avatar
Clément Fumex committed
925 926
               file_theories = ft;
               file_detached_theories = [] } in
Clément Fumex's avatar
Clément Fumex committed
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
    Hstr.add session.session_files fn mf;
    old_provers
  | "prover" ->
    (* The id is just for the session file *)
    let id = string_attribute "id" f in
    begin
      try
        let id = int_of_string id in
        let name = string_attribute "name" f in
        let version = string_attribute "version" f in
        let altern = string_attribute_def "alternative" f "" in
        let timelimit = int_attribute_def "timelimit" f 5 in
        let steplimit = int_attribute_def "steplimit" f 1 in
        let memlimit = int_attribute_def "memlimit" f 1000 in
        let p = {Whyconf.prover_name = name;
                 prover_version = version;
                 prover_altern = altern} in
        Mint.add id (p,timelimit,steplimit,memlimit) old_provers
      with Failure _ ->
        Warning.emit "[Warning] Session.load_file: unexpected non-numeric prover id '%s'@." id;
Clément Fumex's avatar
Clément Fumex committed
947
        old_provers
Clément Fumex's avatar
Clément Fumex committed
948 949 950 951
    end
  | s ->
    Warning.emit "[Warning] Session.load_file: unexpected element '%s'@." s;
    old_provers
Clément Fumex's avatar
Clément Fumex committed
952 953 954

let build_session (s : session) xml =
  match xml.Xml.name with
Clément Fumex's avatar
Clément Fumex committed
955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
  | "why3session" ->
    let shape_version = int_attribute_def "shape_version" xml 1 in
    s.session_shape_version <- shape_version;
    Debug.dprintf debug "[Info] load_session: shape version is %d@\n" shape_version;
    (* just to keep the old_provers somewhere *)
    let old_provers =
      List.fold_left (load_file s) Mint.empty xml.Xml.elements
    in
    Mint.iter
      (fun id (p,_,_,_) ->
         Debug.dprintf debug "prover %d: %a@." id Whyconf.print_prover p;
         Hprover.replace s.session_prover_ids p id)
      old_provers;
    Debug.dprintf debug "[Info] load_session: done@\n"
  | s ->
    Warning.emit "[Warning] Session.load_session: unexpected element '%s'@."
      s
Clément Fumex's avatar
Clément Fumex committed
972

Clément Fumex's avatar
Clément Fumex committed
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
exception ShapesFileError of string

module ReadShapes (C:Compress.S) = struct

let shape = Buffer.create 97
let sum = Strings.create 32

let read_sum_and_shape ch =
  let nsum = C.input ch sum 0 32 in
  if nsum = 0 then raise End_of_file;
  if nsum <> 32 then
    begin
      try
        C.really_input ch sum nsum (32-nsum)
      with End_of_file ->
        raise
          (ShapesFileError
             ("shapes files corrupted (checksum '" ^
                 (String.sub sum 0 nsum) ^
                 "' too short), ignored"))
    end;
  if try C.input_char ch <> ' ' with End_of_file -> true then
      raise (ShapesFileError "shapes files corrupted (space missing), ignored");
    Buffer.clear shape;
    try
      while true do
        let c = C.input_char ch in
        if c = '\n' then raise Exit;
        Buffer.add_char shape c
      done;
      assert false
    with
      | End_of_file ->
        raise (ShapesFileError "shapes files corrupted (premature end of file), ignored");
      | Exit -> Strings.copy sum, Buffer.contents shape


  let use_shapes = ref true

  let fix_attributes ch name attrs =
    if name = "goal" then
      try
        let sum,shape = read_sum_and_shape ch in
        let attrs =
          try
            let old_sum = List.assoc "sum" attrs in
            if sum <> old_sum then
              begin
                Format.eprintf "old sum = %s ; new sum = %s@." old_sum sum;
                raise
                  (ShapesFileError
                     "shapes files corrupted (sums do not correspond)")
              end;
            attrs
          with Not_found -> ("sum", sum) :: attrs
        in
        ("shape",shape) :: attrs
      with _ -> use_shapes := false; attrs
    else attrs

let read_xml_and_shapes xml_fn compressed_fn =
  use_shapes := true;
  try
    let ch = C.open_in compressed_fn in
    let xml = Xml.from_file ~fixattrs:(fix_attributes ch) xml_fn in
    C.close_in ch;
    xml, !use_shapes
  with Sys_error msg ->
    raise (ShapesFileError ("cannot open shapes file for reading: " ^ msg))
end

module ReadShapesNoCompress = ReadShapes(Compress.Compress_none)
module ReadShapesCompress = ReadShapes(Compress.Compress_z)

let read_file_session_and_shapes dir xml_filename =
  try
  let compressed_shape_filename =
    Filename.concat dir compressed_shape_filename
  in
  if Sys.file_exists compressed_shape_filename then
    if Compress.compression_supported then
     ReadShapesCompress.read_xml_and_shapes
       xml_filename compressed_shape_filename
    else
      begin
        Warning.emit "[Warning] could not read goal shapes because \
                                Why3 was not compiled with compress support@.";
        Xml.from_file xml_filename, false
      end
  else
    let shape_filename = Filename.concat dir shape_filename in
    if Sys.file_exists shape_filename then
      ReadShapesNoCompress.read_xml_and_shapes xml_filename shape_filename
    else
      begin
        Warning.emit "[Warning] could not find goal shapes file@.";
        Xml.from_file xml_filename, false
      end
with e ->
  Warning.emit "[Warning] failed to read goal shapes: %s@."
    (Printexc.to_string e);
  Xml.from_file xml_filename, false

Clément Fumex's avatar
Clément Fumex committed
1076 1077 1078
let load_session (dir : string) =
  let session = empty_session dir in
  let file = Filename.concat dir db_filename in
Clément Fumex's avatar
Clément Fumex committed
1079 1080 1081
  let use_shapes =
    (* If the xml is present we read it, otherwise we consider it empty *)
    if Sys.file_exists file then
Clément Fumex's avatar
Clément Fumex committed
1082
      try
MARCHE Claude's avatar
MARCHE Claude committed
1083
(*
Clément Fumex's avatar
Clément Fumex committed
1084
        Termcode.reset_dict ();
MARCHE Claude's avatar
MARCHE Claude committed
1085
*)
Clément Fumex's avatar
Clément Fumex committed
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
        let xml,use_shapes =
          read_file_session_and_shapes dir file in
        try
          build_session session xml.Xml.content;
          use_shapes
        with Sys_error msg ->
          failwith ("Open session: sys error " ^ msg)
      with
      | Sys_error msg ->
        (* xml does not exist yet *)
        raise (SessionFileError msg)
      | Xml.Parse_error s ->
        Warning.emit "XML database corrupted, ignored (%s)@." s;
        raise (SessionFileError "XML corrupted")
    else false
  in
    session, use_shapes
Clément Fumex's avatar
Clément Fumex committed
1103

Clément Fumex's avatar
Clément Fumex committed
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
(* -------------------- merge/update session --------------------------- *)

module CombinedTheoryChecksum = struct

  let b = Buffer.create 1024

  let f () pn =
    match pn.proofn_checksum with
    | None -> assert false
    | Some c -> Buffer.add_string b (Termcode.string_of_checksum c)

  let compute s th =
    let () = fold_all_sub_goals_of_theory s f () th in
    let c = Termcode.buffer_checksum b in
    Buffer.clear b; c

end

(** Pairing *)

module Goal = struct
  type 'a t = proofNodeID * session
  let checksum (id,s) = (get_proofNode s id).proofn_checksum
  let shape (id,s)    = (get_proofNode s id).proofn_shape
  let name (id,s)     = (get_proofNode s id).proofn_name
end

module AssoGoals = Termcode.Pairing(Goal)(Goal)

let found_obsolete = ref false
let found_missed_goals_in_theory = ref false

Clément Fumex's avatar
Clément Fumex committed
1136
let save_detached_goals old_s detached_goals_id s parent =
Clément Fumex's avatar
Clément Fumex committed
1137
  let save_proof parent old_pa_n =
1138
    let old_pa = old_pa_n in
1139
    ignore (add_proof_attempt s old_pa.prover old_pa.limit
Clément Fumex's avatar
Clément Fumex committed
1140
      old_pa.proof_state true old_pa.proof_script
1141
      parent)
Clément Fumex's avatar
Clément Fumex committed
1142 1143 1144 1145 1146
  in
  let rec save_goal parent detached_goal_id id =
    let detached_goal = get_proofNode old_s detached_goal_id in
    mk_proof_node_no_task s detached_goal.proofn_name parent id None
      (Termcode.shape_of_string "");
1147
    Hprover.iter (fun _ pa ->
1148
                  let pa = get_proof_attempt_node old_s pa in
1149
                  save_proof id pa) detached_goal.proofn_attempts;
Clément Fumex's avatar
Clément Fumex committed
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
    List.iter (save_trans id) detached_goal.proofn_transformations;
    let new_trans = (get_proofNode s id) in
    new_trans.proofn_transformations <- List.rev new_trans.proofn_transformations
  and save_trans parent_id old_id =
    let old_tr = get_transfNode old_s old_id in
    let name = old_tr.transf_name in
    let args = old_tr.transf_args in
    let id = gen_transID s in
    let subtasks_id = List.map (fun _ -> gen_proofNodeID s) old_tr.transf_subtasks in
    mk_transf_node s parent_id id name args subtasks_id;
    List.iter2 (fun pn_id -> save_goal (Trans id) pn_id)
      old_tr.transf_subtasks subtasks_id
  in
  List.map
      (fun detached_goal -> let id = gen_proofNodeID s in
        save_goal parent detached_goal id;
        id)
      detached_goals_id

1169
let save_detached_theory parent_name old_s detached_theory s =
Clément Fumex's avatar
Clément Fumex committed
1170
  let goalsID =
Clément Fumex's avatar
Clément Fumex committed
1171
    save_detached_goals old_s detached_theory.theory_goals s (Theory detached_theory) in
Clément Fumex's avatar
Clément Fumex committed
1172 1173 1174 1175
    (* List.map (fun _ -> gen_proofNodeID s) detached_theory.theory_goals in *)
  { theory_name = detached_theory.theory_name;
    theory_checksum = None;
    theory_goals = goalsID;
1176
    theory_parent_name = parent_name;
Clément Fumex's avatar
Clément Fumex committed
1177 1178
    theory_detached_goals = [] }

1179
let merge_proof new_s ~goal_obsolete new_goal _ old_pa_n =
1180
  let old_pa = old_pa_n in
1181
  let obsolete = goal_obsolete || old_pa.proof_obsolete in
Clément Fumex's avatar
Clément Fumex committed
1182
  found_obsolete