session_itp.ml 46.3 KB
Newer Older
Clément Fumex's avatar
Clément Fumex committed
1 2 3 4 5
open Stdlib

module Hprover = Whyconf.Hprover

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

type transID = int
type proofNodeID = int

MARCHE Claude's avatar
MARCHE Claude committed
12 13 14
let print_proofNodeID fmt id =
  Format.fprintf fmt "%d" id

Clément Fumex's avatar
Clément Fumex committed
15
type theory = {
Clément Fumex's avatar
Clément Fumex committed
16 17 18 19
  theory_name                   : Ident.ident;
  theory_goals                  : proofNodeID list;
  mutable theory_detached_goals : proofNodeID list;
  mutable theory_checksum       : Termcode.checksum option;
Clément Fumex's avatar
Clément Fumex committed
20 21
}

22
let theory_name t = t.theory_name
23
let theory_goals t = t.theory_goals
Clément Fumex's avatar
Clément Fumex committed
24
let theory_detached_goals t = t.theory_detached_goals
25

Clément Fumex's avatar
Clément Fumex committed
26
type proof_parent = Trans of transID | Theory of theory
Clément Fumex's avatar
Clément Fumex committed
27 28

type proof_attempt = {
Clément Fumex's avatar
Clément Fumex committed
29 30 31
  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
32
  (* None means that the call was not done or never returned *)
Clément Fumex's avatar
Clément Fumex committed
33 34
  mutable proof_obsolete : bool;
  proof_script           : string option;  (* non empty for external ITP *)
Clément Fumex's avatar
Clément Fumex committed
35 36 37 38 39 40 41 42
}

type proof_attempt_node = {
  proofa_parent  : proofNodeID;
  proofa_attempt : proof_attempt;
}

type proof_node = {
Clément Fumex's avatar
Clément Fumex committed
43
  proofn_name                    : Ident.ident;
Clément Fumex's avatar
Clément Fumex committed
44 45
  proofn_task                    : Task.task;
  proofn_parent                  : proof_parent;
46
  proofn_checksum                : Termcode.checksum option;
Clément Fumex's avatar
Clément Fumex committed
47
  proofn_shape                   : Termcode.shape;
Clément Fumex's avatar
Clément Fumex committed
48 49 50 51 52 53
  proofn_attempts                : proof_attempt_node Hprover.t;
  mutable proofn_transformations : transID list;
}


type transformation_node = {
Clément Fumex's avatar
Clément Fumex committed
54 55 56 57 58
  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
59 60 61
}

type file = {
Clément Fumex's avatar
Clément Fumex committed
62 63 64 65
  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
66 67
}

68 69
module Proofnodeid = struct
  type t = proofNodeID
70
  let _compare (x : proofNodeID) y  = Pervasives.compare x y
71 72 73 74 75 76
  let equal (x : proofNodeID) y = x = y
  let hash  (x : proofNodeID) = x
end

module Transid = struct
  type t = transID
77
  let _compare (x : transID) y  = Pervasives.compare x y
78 79 80 81 82 83 84
  let equal (x : transID) y = x = y
  let hash  (x : transID) = x
end

module Hpn = Exthtbl.Make(Proofnodeid)
module Htn = Exthtbl.Make(Transid)

85
type session = {
Clément Fumex's avatar
Clément Fumex committed
86
  proofNode_table               : proof_node Hint.t;
Clément Fumex's avatar
Clément Fumex committed
87 88 89
  mutable next_proofNodeID      : int;
  trans_table                   : transformation_node Hint.t;
  mutable next_transID          : int;
Clément Fumex's avatar
Clément Fumex committed
90
  session_dir                   : string; (** Absolute path *)
Clément Fumex's avatar
Clément Fumex committed
91 92 93
  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
94
}
Clément Fumex's avatar
Clément Fumex committed
95

96 97 98 99 100 101 102
(* TODO replace *)
let init_Hpn (s : session) (h: 'a Hpn.t) (d: 'a) : unit =
  Hint.iter (fun k _pn -> Hpn.replace h k d) s.proofNode_table

let init_Htn (s : session) (h: 'a Htn.t) (d: 'a) : unit =
  Hint.iter (fun k _pn -> Htn.replace h k d) s.trans_table

103 104 105 106 107 108 109 110 111
let session_iter_proofNode f s =
  Hint.iter
    (fun id pn -> if id < s.next_proofNodeID then f pn)
    s.proofNode_table

let session_iter_proof_attempt f =
  session_iter_proofNode
    (fun pn ->
       Hprover.iter
112
         (fun _ pan -> f pan.proofa_parent pan.proofa_attempt)
113 114
        pn.proofn_attempts)

115
(* This is not needed. Keeping it as information on the structure
116 117 118 119 120 121 122
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 *)
  }
123
*)
MARCHE Claude's avatar
MARCHE Claude committed
124

125
(*
Clément Fumex's avatar
Clément Fumex committed
126
let rec get_tree s id : tree =
MARCHE Claude's avatar
MARCHE Claude committed
127
  let t = Hint.find s.proofNode_table id in
MARCHE Claude's avatar
MARCHE Claude committed
128
  let pal =
129
    Hprover.fold (fun _ pa acc -> pa.proofa_attempt::acc) t.proofn_attempts []
MARCHE Claude's avatar
MARCHE Claude committed
130
  in
MARCHE Claude's avatar
MARCHE Claude committed
131
  let trl = List.map (get_trans s) t.proofn_transformations in
132 133 134 135 136
  { 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
137 138 139

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

143
(*
Clément Fumex's avatar
Clément Fumex committed
144
let get_theories s =
MARCHE Claude's avatar
MARCHE Claude committed
145
  Hstr.fold
146
    (fun _fn f acc ->
MARCHE Claude's avatar
MARCHE Claude committed
147 148
     let c =
       List.map
Clément Fumex's avatar
Clément Fumex committed
149
         (fun th -> (th.theory_name.Ident.id_string, th.theory_goals))
MARCHE Claude's avatar
MARCHE Claude committed
150 151
         f.file_theories
     in
152
     (f,c) :: acc)
MARCHE Claude's avatar
MARCHE Claude committed
153
    s.session_files []
154 155 156
 *)

let get_files s = s.session_files
Clément Fumex's avatar
Clément Fumex committed
157 158 159
let get_dir s = s.session_dir

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

161
(*
Clément Fumex's avatar
Clément Fumex committed
162 163 164 165 166
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
167
*)
Clément Fumex's avatar
Clément Fumex committed
168

Clément Fumex's avatar
Clément Fumex committed
169 170 171 172 173 174 175 176 177 178
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

Clément Fumex's avatar
Clément Fumex committed
179
exception BadID
180

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

186 187 188 189
let get_task (s:session) (id:proofNodeID) =
  let node = get_proofNode s id in
  node.proofn_task

Clément Fumex's avatar
Clément Fumex committed
190 191 192 193 194
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
195 196 197
let get_transformations (s : session) (id : proofNodeID) =
  (get_proofNode s id).proofn_transformations

198 199 200
let get_proof_attempts (s : session) (id : proofNodeID) =
  Hprover.fold (fun _ a l -> a.proofa_attempt :: l) (get_proofNode s id).proofn_attempts []

Clément Fumex's avatar
Clément Fumex committed
201 202 203
let get_sub_tasks (s : session) (id : transID) =
  (get_transfNode s id).transf_subtasks

Clément Fumex's avatar
Clément Fumex committed
204 205 206
let get_detached_sub_tasks (s : session) (id : transID) =
  (get_transfNode s id).transf_detached_subtasks

207 208 209 210
let get_transf_args (s : session) (id : transID) =
  (get_transfNode s id).transf_args

let get_transf_name (s : session) (id : transID) =
211 212 213 214 215
  (get_transfNode s id).transf_name

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

216 217 218 219 220 221
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

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
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
238 239 240 241 242 243 244
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
    (fun pn -> Hprover.iter (fun _ pan -> f pan.proofa_attempt) pn.proofn_attempts) th

Clément Fumex's avatar
Clément Fumex committed
245 246 247
open Format
open Ident

MARCHE Claude's avatar
MARCHE Claude committed
248 249 250
let print_proof_attempt fmt pa =
  fprintf fmt "%a tl=%d %a"
          Whyconf.print_prover pa.prover
251
          pa.limit.Call_provers.limit_time
MARCHE Claude's avatar
MARCHE Claude committed
252 253
          (Pp.print_option Call_provers.print_prover_result) pa.proof_state

254 255 256 257 258 259 260
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
261
    "@[<hv 1> Goal %s;@ parent %s;@ sum %s;@ @[<hv 1>[%a]@]@ @[<hv 1>[%a]@]@]"
262 263 264 265 266 267 268 269
    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)
      (Hprover.fold (fun _key e l -> e.proofa_attempt :: l) pn.proofn_attempts [])
    (Pp.print_list Pp.semi (print_trans_node s)) pn.proofn_transformations

and print_trans_node s fmt id =
  let tn = get_transfNode s id in
270
  let args = get_transf_args s id in
271 272 273
  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
MARCHE Claude's avatar
MARCHE Claude committed
274
  fprintf fmt "@[<hv 1> Trans %s;@ args %a;@ parent %s;@ [%a]@]" name (Pp.print_list Pp.colon pp_print_string) args parent
275 276 277 278 279 280 281 282 283 284 285 286
    (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
287

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

292

Clément Fumex's avatar
Clément Fumex committed
293
let empty_session ?shape_version dir =
Clément Fumex's avatar
Clément Fumex committed
294 295 296 297
  let shape_version = match shape_version with
    | Some v -> v
    | None -> Termcode.current_shape_version
  in
Clément Fumex's avatar
Clément Fumex committed
298
  { proofNode_table = Hint.create 97;
Clément Fumex's avatar
Clément Fumex committed
299 300 301
    next_proofNodeID = 0;
    trans_table = Hint.create 97;
    next_transID = 0;
Clément Fumex's avatar
Clément Fumex committed
302
    session_dir = dir;
Clément Fumex's avatar
Clément Fumex committed
303 304 305 306 307
    session_files = Hstr.create 3;
    session_shape_version = shape_version;
    session_prover_ids = Hprover.create 7;
  }

308 309 310
(**************************************************)
(* proof node/attempt/transformation manipulation *)
(**************************************************)
MARCHE Claude's avatar
MARCHE Claude committed
311

Clément Fumex's avatar
Clément Fumex committed
312 313 314
let mk_proof_attempt session pid pa =
  let pn = get_proofNode session pid in
  let node = { proofa_parent = pid; proofa_attempt = pa } in
Clément Fumex's avatar
Clément Fumex committed
315 316
  Hprover.replace pn.proofn_attempts pa.prover node

Clément Fumex's avatar
Clément Fumex committed
317 318 319 320 321 322 323 324 325
let add_proof_attempt session prover limit state obsolete edit parentID =
  let pa = { prover = prover;
             limit = limit;
             proof_state = state;
             proof_obsolete = obsolete;
             proof_script = edit;
           } in
  mk_proof_attempt session parentID pa

Clément Fumex's avatar
Clément Fumex committed
326 327
let graft_proof_attempt (s : session) (id : proofNodeID) (pr : Whyconf.prover)
    ~timelimit =
Clément Fumex's avatar
Clément Fumex committed
328 329 330 331
  let limit = { Call_provers.limit_time  = timelimit;
                Call_provers.limit_mem   = 0;
                Call_provers.limit_steps = -1; } in
  add_proof_attempt s pr limit None false None id
Clément Fumex's avatar
Clément Fumex committed
332

Clément Fumex's avatar
Clément Fumex committed
333 334 335 336 337
let remove_proof_attempt (s : session) (id : proofNodeID)
    (prover : Whyconf.prover) =
  let pn = get_proofNode s id in
  Hprover.remove pn.proofn_attempts prover

Clément Fumex's avatar
Clément Fumex committed
338
(* [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
339
   of proofNodeID [id] of parent [p] of task [t] *)
Clément Fumex's avatar
Clément Fumex committed
340
let mk_proof_node ~version (s : session) (n : Ident.ident) (t : Task.task)
Clément Fumex's avatar
Clément Fumex committed
341
    (parent : proof_parent) (node_id : proofNodeID) =
Clément Fumex's avatar
Clément Fumex committed
342
  let sum = Some (Termcode.task_checksum ~version t) in
Clément Fumex's avatar
Clément Fumex committed
343
  let shape = Termcode.t_shape_task ~version t in
Clément Fumex's avatar
Clément Fumex committed
344 345 346
  let pn = { proofn_name = n;
             proofn_task = t;
             proofn_parent = parent;
347
             proofn_checksum = sum;
Clément Fumex's avatar
Clément Fumex committed
348
             proofn_shape = shape;
Clément Fumex's avatar
Clément Fumex committed
349
             proofn_attempts = Hprover.create 7;
Clément Fumex's avatar
Clément Fumex committed
350
             proofn_transformations = [] } in
Clément Fumex's avatar
Clément Fumex committed
351
  Hint.add s.proofNode_table node_id pn
Clément Fumex's avatar
Clément Fumex committed
352

Clément Fumex's avatar
Clément Fumex committed
353
let mk_proof_node_no_task (s : session) (n : Ident.ident)
Clément Fumex's avatar
Clément Fumex committed
354
    (parent : proof_parent) (node_id : proofNodeID) sum shape =
355 356 357 358
  let pn = { proofn_name = n;
             proofn_task = None;
             proofn_parent = parent;
             proofn_checksum = sum;
Clément Fumex's avatar
Clément Fumex committed
359
             proofn_shape = shape;
360
             proofn_attempts = Hprover.create 7;
Clément Fumex's avatar
Clément Fumex committed
361
             proofn_transformations = [] } in
362
  Hint.add s.proofNode_table node_id pn
Clément Fumex's avatar
Clément Fumex committed
363 364 365 366

let mk_proof_node_task (s : session) (t : Task.task)
    (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
367
  mk_proof_node ~version:s.session_shape_version s name t parent node_id
Clément Fumex's avatar
Clément Fumex committed
368

Clément Fumex's avatar
Clément Fumex committed
369
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
370
  let id = gen_proofNodeID s in
Clément Fumex's avatar
Clément Fumex committed
371 372 373 374 375 376 377 378 379
  let gid,expl,_ = Termcode.goal_expl_task ~root:false t in
(*  let expl = match expl with
    | None -> string_of_int index ^ "."
    | Some e -> string_of_int index ^ ". " ^ e
  in
    let expl = Some expl in *)
  let goal_name = parent_name ^ "." ^ string_of_int index in
  let goal_name = Ident.id_register (Ident.id_derive goal_name gid) in
  mk_proof_node ~version:s.session_shape_version s goal_name t (Trans tid) id;
Clément Fumex's avatar
Clément Fumex committed
380 381
  id

Clément Fumex's avatar
Clément Fumex committed
382
let mk_transf_node (s : session) (id : proofNodeID) (node_id : transID)
383
    (name : string) (args : string list) (pnl : proofNodeID list) =
Clément Fumex's avatar
Clément Fumex committed
384 385 386 387
  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
388 389
             transf_parent = id;
             transf_detached_subtasks = [] } in
Clément Fumex's avatar
Clément Fumex committed
390 391 392 393
  Hint.add s.trans_table node_id tn;
  pn.proofn_transformations <- node_id::pn.proofn_transformations

let graft_transf  (s : session) (id : proofNodeID) (name : string)
394
    (args : string list) (tl : Task.task list) =
Clément Fumex's avatar
Clément Fumex committed
395
  let tid = gen_transID s in
Clément Fumex's avatar
Clément Fumex committed
396 397
  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
398
  mk_transf_node s id tid name args sub_tasks;
Clément Fumex's avatar
Clément Fumex committed
399
  tid
Clément Fumex's avatar
Clément Fumex committed
400 401 402 403 404 405

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
Clément Fumex's avatar
Clément Fumex committed
406 407 408 409 410
  pn.proofn_transformations <- trans_up

let update_proof_attempt s id pr st =
  let n = get_proofNode s id in
  let pa = Hprover.find n.proofn_attempts pr in
411 412 413
  pa.proofa_attempt.proof_state <- Some st;
  pa.proofa_attempt.proof_obsolete <- false

Clément Fumex's avatar
Clément Fumex committed
414 415 416 417 418

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

Clément Fumex's avatar
Clément Fumex committed
419 420
let db_filename = "why3session.xml"
let shape_filename = "why3shapes"
Clément Fumex's avatar
Clément Fumex committed
421
let compressed_shape_filename = "why3shapes.gz"
Clément Fumex's avatar
Clément Fumex committed
422 423
let session_dir_for_save = ref "."

Clément Fumex's avatar
Clément Fumex committed
424 425 426 427 428 429
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
430 431 432
    | "true" -> true
    | "false" -> false
    | _ -> assert false
Clément Fumex's avatar
Clément Fumex committed
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
  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
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
  | "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
500 501 502 503 504 505 506 507

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
508 509 510 511 512 513 514
      (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
515 516 517 518 519 520 521 522 523 524 525 526 527
  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
528 529 530 531
(* [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
532 533
  | "goal" ->
    let gname = load_ident g in
534 535
    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
536 537 538 539 540
    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
541 542 543 544 545
    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
546
(* [load_proof_or_transf s op pid a] load either a proof attempt or a
Clément Fumex's avatar
Clément Fumex committed
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
   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
570 571 572 573 574
          let limit = { Call_provers.limit_time  = timelimit;
                        Call_provers.limit_mem   = memlimit;
                        Call_provers.limit_steps = steplimit; }
          in
          add_proof_attempt session p limit res obsolete edit pid
Clément Fumex's avatar
Clément Fumex committed
575 576 577 578 579
        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" ->
580 581 582 583 584 585 586 587 588 589 590
      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 ->
591 592 593
                       match th.Xml.name with
                       | "goal" -> (gen_proofNodeID session) :: goals
                       | _ -> goals) [] a.Xml.elements)
594 595 596 597 598
      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
599 600
    | "metas" -> ()
    | "label" -> ()
Clément Fumex's avatar
Clément Fumex committed
601
    | s ->
602 603 604
      Warning.emit
        "[Warning] Session.load_proof_or_transf: unexpected element '%s'@."
        s
Clément Fumex's avatar
Clément Fumex committed
605 606 607 608 609 610 611

let load_theory session old_provers acc th =
  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
612
    let goals = List.rev (List.fold_left (fun goals th -> match th.Xml.name with
Clément Fumex's avatar
Clément Fumex committed
613
        | "goal" -> (gen_proofNodeID session) :: goals
Clément Fumex's avatar
Clément Fumex committed
614
        | _ -> goals) [] th.Xml.elements) in
Clément Fumex's avatar
Clément Fumex committed
615 616
    let mth = { theory_name = thname;
                theory_checksum = checksum;
Clément Fumex's avatar
Clément Fumex committed
617 618
                theory_goals = goals;
                theory_detached_goals = [] } in
Clément Fumex's avatar
Clément Fumex committed
619 620 621 622 623 624 625 626 627
    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

628
let load_file session old_provers f =
Clément Fumex's avatar
Clément Fumex committed
629 630 631 632 633 634 635 636 637
  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
           (load_theory session old_provers) [] f.Xml.elements) in
    let mf = { file_name = fn;
               file_format = fmt;
Clément Fumex's avatar
Clément Fumex committed
638 639
               file_theories = ft;
               file_detached_theories = [] } in
Clément Fumex's avatar
Clément Fumex committed
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
    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
660
        old_provers
Clément Fumex's avatar
Clément Fumex committed
661 662 663 664
    end
  | s ->
    Warning.emit "[Warning] Session.load_file: unexpected element '%s'@." s;
    old_provers
Clément Fumex's avatar
Clément Fumex committed
665 666 667

let build_session (s : session) xml =
  match xml.Xml.name with
Clément Fumex's avatar
Clément Fumex committed
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
  | "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
685

Clément Fumex's avatar
Clément Fumex committed
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 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 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 786 787 788
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
789 790 791
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
792 793 794
  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
795
      try
Clément Fumex's avatar
Clément Fumex committed
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
        Termcode.reset_dict ();
        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
814

Clément Fumex's avatar
Clément Fumex committed
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
(* -------------------- 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
847
let save_detached_goals old_s detached_goals_id s parent =
Clément Fumex's avatar
Clément Fumex committed
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
  let save_proof parent old_pa_n =
    let old_pa = old_pa_n.proofa_attempt in
    add_proof_attempt s old_pa.prover old_pa.limit
      old_pa.proof_state true old_pa.proof_script
      parent
  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 "");
    Hprover.iter (fun _ -> save_proof id) detached_goal.proofn_attempts;
    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

Clément Fumex's avatar
Clément Fumex committed
878
let save_detached_theory old_s detached_theory s =
Clément Fumex's avatar
Clément Fumex committed
879
  let goalsID =
Clément Fumex's avatar
Clément Fumex committed
880
    save_detached_goals old_s detached_theory.theory_goals s (Theory detached_theory) in
Clément Fumex's avatar
Clément Fumex committed
881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
    (* List.map (fun _ -> gen_proofNodeID s) detached_theory.theory_goals in *)
  { theory_name = detached_theory.theory_name;
    theory_checksum = None;
    theory_goals = goalsID;
    theory_detached_goals = [] }

let merge_proof new_s obsolete new_goal _ old_pa_n =
  let old_pa = old_pa_n.proofa_attempt in
  let obsolete = obsolete || old_pa.proof_obsolete in
  found_obsolete := obsolete || !found_obsolete;
  add_proof_attempt new_s old_pa.prover old_pa.limit
    old_pa.proof_state obsolete old_pa.proof_script
    new_goal

let add_registered_transformation s env old_tr goal_id =
  let goal = get_proofNode s goal_id in
  try
    let tr = List.find (fun transID -> (get_transfNode s transID).transf_name = old_tr.transf_name)
        goal.proofn_transformations in
    (* NOTE: should not happen *)
    Debug.dprintf debug "[merge_theory] trans found@.";
    tr
  with Not_found ->
    Debug.dprintf debug "[merge_theory] trans not found@.";
    let task = goal.proofn_task in
    let subgoals = Trans.apply_transform_args old_tr.transf_name env old_tr.transf_args task in
    graft_transf s goal_id old_tr.transf_name old_tr.transf_args subgoals

Clément Fumex's avatar
Clément Fumex committed
909
let rec merge_goal ~use_shapes env new_s old_s obsolete old_goal new_goal_id =
Clément Fumex's avatar
Clément Fumex committed
910
  Hprover.iter (merge_proof new_s obsolete new_goal_id) old_goal.proofn_attempts;
Clément Fumex's avatar
Clément Fumex committed
911
  List.iter (merge_trans ~use_shapes env old_s new_s new_goal_id) old_goal.proofn_transformations;
Clément Fumex's avatar
Clément Fumex committed
912 913 914
  let new_trans = (get_proofNode new_s new_goal_id) in
  new_trans.proofn_transformations <- List.rev new_trans.proofn_transformations

Clément Fumex's avatar
Clément Fumex committed
915
and merge_trans ~use_shapes env old_s new_s new_goal_id old_tr_id =
Clément Fumex's avatar
Clément Fumex committed
916 917 918 919 920 921 922 923 924 925
  let old_tr = get_transfNode old_s old_tr_id in
  let new_tr_id =
    add_registered_transformation new_s env old_tr new_goal_id
  in
  let new_tr = get_transfNode new_s new_tr_id in
  (* attach the session to the subtasks to be able to instantiate Pairing *)
  let old_subtasks = List.map (fun id -> id,old_s)
      old_tr.transf_subtasks in
  let new_subtasks = List.map (fun id -> id,new_s)
      new_tr.transf_subtasks in
Clément Fumex's avatar
Clément Fumex committed
926 927 928 929 930 931 932 933
  List.iter
    (fun (id,s) -> match (get_proofNode s id).proofn_checksum with
       | Some _ -> Debug.dprintf debug "[merge] old subgoal has no checksum@."
       | None ->  Debug.dprintf debug "[merge] old subgoal has no checksum@.") old_subtasks;
  List.iter
    (fun (id,s) -> match (get_proofNode s id).proofn_checksum with
       | Some _ -> Debug.dprintf debug "[merge] new subgoal has no checksum@."
       | None ->  Debug.dprintf debug "[merge] new subgoal has no checksum@.") new_subtasks;
Clément Fumex's avatar
Clément Fumex committed
934
  let associated,detached =
Clément Fumex's avatar
Clément Fumex committed
935
    AssoGoals.associate ~use_shapes old_subtasks new_subtasks
Clément Fumex's avatar
Clément Fumex committed
936 937 938 939
  in
  List.iter (function
      | ((new_goal_id,_), Some ((old_goal_id,_), obsolete)) ->
        Debug.dprintf debug "[merge_theory] pairing paired one goal, yeah !@.";
Clément Fumex's avatar
Clément Fumex committed
940
        merge_goal ~use_shapes env new_s old_s obsolete (get_proofNode old_s old_goal_id) new_goal_id
Clément Fumex's avatar
Clément Fumex committed
941 942 943
      | ((id,s), None) ->
        Debug.dprintf debug "[merge_theory] pairing found missed sub goal :( : %s @."
          (get_proofNode s id).proofn_name.Ident.id_string;
Clément Fumex's avatar
Clément Fumex committed
944 945 946 947
        found_missed_goals_in_theory := true)
    associated;
  (* save the detached goals *)
  let detached = List.map (fun (a,_) -> a) detached in
Clément Fumex's avatar
Clément Fumex committed
948
  new_tr.transf_detached_subtasks <- save_detached_goals old_s detached new_s (Trans new_tr_id)
Clément Fumex's avatar
Clément Fumex committed
949

Clément Fumex's avatar
Clément Fumex committed
950
let merge_theory ~use_shapes env old_s old_th s th : unit =
Clément Fumex's avatar
Clément Fumex committed
951 952 953
  let found_missed_goals_in_theory = ref false in
  let old_goals_table = Hstr.create 7 in
  (* populate old_goals_table *)
MARCHE Claude's avatar
MARCHE Claude committed
954
  List.iter
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 972 973 974 975 976 977 978 979 980
    (fun id ->
       let pn = get_proofNode old_s id in
       Hstr.add old_goals_table pn.proofn_name.Ident.id_string id)
    old_th.theory_goals;
  let new_goals = ref [] in
  (* merge goals *)
  List.iter
    (fun ng_id ->
       try
         let new_goal = get_proofNode s ng_id in
         (* look for old_goal with matching name *)
         let old_goal = get_proofNode old_s
           (Hstr.find old_goals_table new_goal.proofn_name.Ident.id_string) in
         Hstr.remove old_goals_table new_goal.proofn_name.Ident.id_string;
         let goal_obsolete =
           match new_goal.proofn_checksum, old_goal.proofn_checksum with
           | None, _ -> assert false
           | Some _, None ->
             Debug.dprintf debug "[merge_theory] goal has no checksum@.";
             true
           | Some s1, Some s2 ->
             Debug.dprintf debug "[merge_theory] goal has checksum@.";
             not (Termcode.equal_checksum s1 s2)
         in
         if goal_obsolete then
           found_obsolete := true;
Clément Fumex's avatar
Clément Fumex committed
981
         merge_goal ~use_shapes env s old_s goal_obsolete old_goal ng_id
Clément Fumex's avatar
Clément Fumex committed
982 983 984 985 986 987 988 989 990 991
       with
       | Not_found ->
         (* if no goal of matching name is found store it to look for
            matching shape *)
         new_goals := (ng_id,s) :: !new_goals)
    th.theory_goals;
  (* check shapes if no old_goal is found with matching name *)
  (* attach the session to the subtasks to be able to instantiate Pairing *)
  let detached_goals = Hstr.fold (fun _key g tl -> (g,old_s) :: tl) old_goals_table [] in
  let associated,detached =
Clément Fumex's avatar
Clément Fumex committed
992
    AssoGoals.associate ~use_shapes detached_goals !new_goals
Clément Fumex's avatar
Clément Fumex committed
993
  in
Clément Fumex's avatar
Clément Fumex committed
994 995 996
  List.iter (function
      | ((new_goal_id,_), Some ((old_goal_id,_), obsolete)) ->
        Debug.dprintf debug "[merge_theory] pairing paired one goal, yeah !@.";
Clément Fumex's avatar
Clément Fumex committed
997
        merge_goal ~use_shapes env s old_s obsolete (get_proofNode old_s old_goal_id) new_goal_id
Clément Fumex's avatar
Clément Fumex committed
998 999 1000 1001 1002 1003
      | (_, None) ->
        Debug.dprintf debug "[merge_theory] pairing found missed sub goal :( @.";
        found_missed_goals_in_theory := true)
    associated;
  (* store the detached goals *)
  let detached = List.map (fun (a,_) -> a) detached in
Clément Fumex's avatar
Clément Fumex committed
1004
  th.theory_detached_goals <- save_detached_goals old_s detached s (Theory th);
Clément Fumex's avatar
Clément Fumex committed
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
  (* set goals as non obsolete if no goals was missed and the theory
     checksum match with the previous one *)
  if not !found_missed_goals_in_theory then begin
    let to_checksum = CombinedTheoryChecksum.compute s th in
    let same_checksum =
      match old_th.theory_checksum with
      | None -> false
      | Some c -> Termcode.equal_checksum c to_checksum
    in
    if same_checksum then
      (* we set all_goals as non obsolete *)
      let set_non_obsolete pan = pan.proof_obsolete <-false in
      theory_iter_proof_attempt s set_non_obsolete th
  end

(* add a theory and its goals to a session. if a previous theory is
   provided in merge try to merge the new theory with the previous one *)
Clément Fumex's avatar
Clément Fumex committed
1022
let make_theory_section ~use_shapes ?merge (s:session) (th:Theory.theory)  : theory =
Clément Fumex's avatar
Clément Fumex committed
1023 1024 1025
  let add_goal parent goal id =
    let name,_expl,task = Termcode.goal_expl_task ~root:true goal in
    mk_proof_node ~version:s.session_shape_version s name task parent id;
Clément Fumex's avatar
Clément Fumex committed
1026
  in
Clément Fumex's avatar
Clément Fumex committed
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
  let tasks = List.rev (Task.split_theory th None None) in
  let goalsID = List.map (fun _ -> gen_proofNodeID s) tasks in
  let theory = { theory_name = th.Theory.th_name;
                 theory_checksum = None;
                 theory_goals = goalsID;
                 theory_detached_goals = [] } in
  let parent = Theory theory in
  List.iter2 (add_goal parent) tasks goalsID;
  begin
    match merge with
    | Some (old_s, old_th, env) ->
Clément Fumex's avatar
Clément Fumex committed
1038
      merge_theory ~use_shapes env old_s old_th s theory
Clément Fumex's avatar
Clément Fumex committed
1039 1040 1041 1042 1043 1044
    | _ -> ()
  end;
  theory

(* add a why file from a session, if merge is provided try to merge
   its theories with the previous ones with matching names *)
Clément Fumex's avatar
Clément Fumex committed
1045
let add_file_section ~use_shapes ?merge (s:session) (fn:string) (theories:Theory.theory list) format
Clément Fumex's avatar
Clément Fumex committed
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
  : unit =
  let fn = Sysutil.relativize_filename s.session_dir fn in
  if Hstr.mem s.session_files fn then
    Debug.dprintf debug "[session] file %s already in database@." fn
  else
    let theories,detached =
      match merge with
      | Some (old_ses, old_th, env) ->
        let old_th_table = Hstr.create 7 in
        List.iter
          (fun th -> Hstr.add old_th_table th.theory_name.Ident.id_string th)
          old_th;
        let add_theory (th: Theory.theory) =
          try
            (* look for a theory with same name *)
            let theory_name = th.Theory.th_name.Ident.id_string in
            (* if we found one, we remove it from the table and merge it *)
            let old_th = Hstr.find old_th_table theory_name in
            Hstr.remove old_th_table theory_name;
Clément Fumex's avatar
Clément Fumex committed
1065
            make_theory_section ~use_shapes ~merge:(old_ses,old_th,env) s th
Clément Fumex's avatar
Clément Fumex committed
1066 1067
          with Not_found ->
            (* if no theory was found we make a new theory section *)
Clément Fumex's avatar
Clément Fumex committed
1068
            make_theory_section ~use_shapes s th
Clément Fumex's avatar
Clément Fumex committed
1069 1070 1071 1072 1073
        in
        let theories = List.map add_theory theories in
        (* we save the remaining, detached *)
        let detached = Hstr.fold
            (fun _key th tl ->
Clément Fumex's avatar
Clément Fumex committed
1074
               (save_detached_theory old_ses th s) :: tl)
Clément Fumex's avatar
Clément Fumex committed
1075 1076 1077
            old_th_table [] in
        theories, detached
      | None ->
Clément Fumex's avatar
Clément Fumex committed
1078
        List.map (make_theory_section ~use_shapes s) theories, []
Clément Fumex's avatar
Clément Fumex committed
1079 1080 1081 1082 1083 1084 1085
    in
    let f = { file_name = fn;
              file_format = format;
              file_theories = theories;
              file_detached_theories = detached }
    in
    Hstr.add s.session_files fn f
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101

(************************)
(* saving state on disk *)
(************************)

module Mprover = Whyconf.Mprover
module Sprover = Whyconf.Sprover
module PHprover = Whyconf.Hprover

open Format

let save_string = Pp.html_string

type save_ctxt = {
  prover_ids : int PHprover.t;
  provers : (int * int * int * int) Mprover.t;
Clément Fumex's avatar
Clément Fumex committed
1102
  ch_shapes : Compress.Compress_z.out_channel;
1103 1104 1105 1106 1107
}

let get_used_provers_with_stats session =
  let prover_table = PHprover.create 5 in
  session_iter_proof_attempt
1108
    (fun _ pa ->
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 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
      (* record mostly used pa.proof_timelimit pa.proof_memlimit *)
      let prover = pa.prover in
      let timelimits,steplimits,memlimits =
        try PHprover.find prover_table prover
        with Not_found ->
          let x = (Hashtbl.create 5,Hashtbl.create 5,Hashtbl.create 5) in
          PHprover.add prover_table prover x;
          x
      in
      let lim_time = pa.limit.Call_provers.limit_time in
      let lim_mem = pa.limit.Call_provers.limit_mem in
      let lim_steps = pa.limit.Call_provers.limit_steps in
      let tf = try Hashtbl.find timelimits lim_time with Not_found -> 0 in
      let sf = try Hashtbl.find steplimits lim_steps with Not_found -> 0 in
      let mf = try Hashtbl.find memlimits lim_mem with Not_found -> 0 in
      Hashtbl.replace timelimits lim_time (tf+1);
      Hashtbl.replace steplimits lim_steps (sf+1);
      Hashtbl.replace memlimits lim_mem (mf+1))
    session;
  prover_table

let get_prover_to_save prover_ids p (timelimits,steplimits,memlimits) provers =
  let mostfrequent_timelimit,_ =
    Hashtbl.fold
      (fun t f ((_,f') as t') -> if f > f' then (t,f) else t')
      timelimits
      (0,0)
  in
  let mostfrequent_steplimit,_ =
    Hashtbl.fold
      (fun s f ((_,f') as s') -> if f > f' then (s,f) else s')
      steplimits
      (0,0)
  in
  let mostfrequent_memlimit,_ =
    Hashtbl.fold
      (fun m f ((_,f') as m') -> if f > f' then (m,f) else m')
      memlimits
      (0,0)
  in
  let id =
    try
      PHprover.find prover_ids p
    with Not_found ->
      (* we need to find an unused prover id *)
      let occurs = Hashtbl.create 7 in
      PHprover.iter (fun _ n -> Hashtbl.add occurs n ()) prover_ids;
      let id = ref 0 in
      try
        while true do
          try
            let _ = Hashtbl.find occurs !id in incr id
          with Not_found -> raise Exit
        done;
        assert false
      with Exit ->
        PHprover.add prover_ids p !id;
        !id
  in
  Mprover.add p (id,mostfrequent_timelimit,mostfrequent_steplimit,mostfrequent_memlimit) provers

let opt pr lab fmt = function
  | None -> ()
  | Some s -> fprintf fmt "@ %s=\"%a\"" lab pr s

let opt_string = opt save_string

let save_prover fmt id (p,mostfrequent_timelimit,mostfrequent_steplimit,mostfrequent_memlimit) =
  let steplimit =
    if mostfrequent_steplimit < 0 then None else Some mostfrequent_steplimit
  in
  fprintf fmt "@\n@[<h><prover@ id=\"%i\"@ name=\"%a\"@ \
               version=\"%a\"%a@ timelimit=\"%d\"%a@ memlimit=\"%d\"/>@]"
    id save_string p.Whyconf.prover_name save_string p.Whyconf.prover_version
    (fun fmt s -> if s <> "" then fprintf fmt "@ alternative=\"%a\""
        save_string s)
    p.Whyconf.prover_altern
    mostfrequent_timelimit
    (opt pp_print_int "steplimit") steplimit
    mostfrequent_memlimit

let save_bool_def name def fmt b =
  if b <> def then fprintf fmt "@ %s=\"%b\"" name b

let save_int_def name def fmt n =
  if n <> def then fprintf fmt "@ %s=\"%d\"" name n

let save_result fmt r =
  let steps = if  r.Call_provers.pr_steps >= 0 then
                Some  r.Call_provers.pr_steps
              else
                None
  in
  fprintf fmt "<result@ status=\"%s\"@ time=\"%.2f\"%a/>"
    (match r.Call_provers.pr_answer with
       | Call_provers.Valid -> "valid"
       | Call_provers.Failure _ -> "failure"
       | Call_provers.Unknown _ -> "unknown"
       | Call_provers.HighFailure -> "highfailure"
       | Call_provers.Timeout -> "timeout"
       | Call_provers.OutOfMemory -> "outofmemory"
       | Call_provers.StepLimitExceeded -> "steplimitexceeded"
       | Call_provers.Invalid -> "invalid")
    r.Call_provers.pr_time
    (opt pp_print_int "steps") steps

let save_status fmt s =
  match s with
  | Some result -> save_result fmt result
  | None -> fprintf fmt "<undone/>"

let save_proof_attempt fmt ((id,tl,sl,ml),a) =
  fprintf fmt
    "@\n@[<h><proof@ prover=\"%i\"%a%a%a%a>"
    id
    (save_int_def "timelimit" tl) (a.limit.Call_provers.limit_time)
    (save_int_def "steplimit" sl) (a.limit.Call_provers.limit_steps)
    (save_int_def "memlimit" ml) (a.limit.Call_provers.limit_mem)
    (save_bool_def "obsolete" false) a.proof_obsolete;
  save_status fmt a.proof_state;
  fprintf fmt "</proof>@]"

let save_ident fmt id =
Clément Fumex's avatar
Clément Fumex committed
1232
  let n = id.Ident.id_string
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
  in
  fprintf fmt "name=\"%a\"" save_string n

let save_checksum fmt s =
  fprintf fmt "%s" (Termcode.string_of_checksum s)

let rec save_goal s ctxt fmt pnid =
  let pn = get_proofNode s pnid in
  fprintf fmt
    "@\n@[<v 0>@[<h><goal@ %a>@]"
    save_ident pn.proofn_name;
Clément Fumex's avatar
Clément Fumex committed
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
  let sum =
    match pn.proofn_checksum with
    | None -> assert false
    | Some s -> Termcode.string_of_checksum s
  in
  let shape = Termcode.string_of_shape pn.proofn_shape in
  assert (shape <> "");
  Compress.Compress_z.output_string ctxt.ch_shapes sum;
  Compress.Compress_z.output_char ctxt.ch_shapes ' ';
  Compress.Compress_z.output_string ctxt.ch_shapes shape;
  Compress.Compress_z.output_char ctxt.ch_shapes '\n';
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
  let l = Hprover.fold
      (fun _ a acc -> (Mprover.find a.proofa_attempt.prover ctxt.provers,
                       a.proofa_attempt) :: acc)
      pn.proofn_attempts [] in
  let l = List.sort (fun ((i1,_,_,_),_) ((i2,_,_,_),_) -> compare i1 i2) l in
  List.iter (save_proof_attempt fmt) l;
  let l =
    List.fold_left (fun acc t -> (get_transfNode s t) :: acc) [] pn.proofn_transformations
  in
  let l = List.sort (fun t1 t2 -> compare t1.transf_name t2.transf_name) l in
  List.iter (save_trans s ctxt fmt) l;
Clément Fumex's avatar
Clément Fumex committed
1266
  fprintf fmt "@]@\n</goal>";
1267 1268

and save_trans s ctxt fmt t =
1269 1270 1271 1272 1273 1274 1275
  let arg_id = ref 0 in
  let save_arg fmt s =
    arg_id := !arg_id + 1;
    fprintf fmt "arg%i=\"%a\"" !arg_id save_string s
  in
  fprintf fmt "@\n@[<hov 1>@[<h><transf@ name=\"%a\" %a>@]"
    save_string t.transf_name (Pp.print_list Pp.space save_arg) t.transf_args;
1276 1277 1278 1279 1280 1281
  List.iter (save_goal s ctxt fmt) t.transf_subtasks;
  fprintf fmt "@]@\n</transf>"

let save_theory s ctxt fmt t =
  (* commented out since the session needs to be updated for goals to
     have a checksum *)
Clément Fumex's avatar
Clément Fumex committed
1282 1283
  let c = CombinedTheoryChecksum.compute s t in
  t.theory_checksum <- Some c;
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
  fprintf fmt
    "@\n@[<v 1>@[<h><theory@ %a%a>@]"
    save_ident t.theory_name
    (opt save_checksum "sum") t.theory_checksum;
  List.iter (save_goal s ctxt fmt) t.theory_goals;
  fprintf fmt "@]@\n</theory>"

let save_file s ctxt fmt _ f =
  fprintf fmt
    "@\n@[<v 0>@[<h><file@ name=\"%a\"%a>@]"
    save_string f.file_name (opt_string "format")
    f.file_format;
  List.iter (save_theory s ctxt fmt) f.file_theories;
  fprintf fmt "@]@\n</file>"

Clément Fumex's avatar
Clément Fumex committed
1299
let save fname shfname session =
1300
  let ch = open_out fname in
Clément Fumex's avatar
Clément Fumex committed
1301
  let chsh = Compress.Compress_z.open_out shfname in
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
  let fmt = formatter_of_out_channel ch in
  fprintf fmt "<?xml version=\"1.0\" encoding=\"UTF-8\"?>@\n";
  fprintf fmt "<!DOCTYPE why3session PUBLIC \"-//Why3//proof session v5//EN\"@ \"http://why3.lri.fr/why3session.dtd\">@\n";
  fprintf fmt "@[<v 0><why3session shape_version=\"%d\">"
    session.session_shape_version;
  Termcode.reset_dict ();
  let prover_ids = session.session_prover_ids in
  let provers =
    PHprover.fold (get_prover_to_save prover_ids)
      (get_used_provers_with_stats session) Mprover.empty
  in
  let provers_to_save =
    Mprover.fold
      (fun p (id,mostfrequent_timelimit,mostfrequent_steplimit,mostfrequent_memlimit) acc ->
        Mint.add id (p,mostfrequent_timelimit,mostfrequent_steplimit,mostfrequent_memlimit) acc)
      provers Mint.empty
  in
  Mint.iter (save_prover fmt) provers_to_save;
Clément Fumex's avatar
Clément Fumex committed
1320