session.ml 75.2 KB
Newer Older
Andrei Paskevich's avatar
Andrei Paskevich committed
1
2
3
(********************************************************************)
(*                                                                  *)
(*  The Why3 Verification Platform   /   The Why3 Development Team  *)
4
(*  Copyright 2010-2013   --   INRIA - CNRS - Paris-Sud University  *)
Andrei Paskevich's avatar
Andrei Paskevich committed
5
6
7
8
9
10
(*                                                                  *)
(*  This software is distributed under the terms of the GNU Lesser  *)
(*  General Public License version 2.1, with the special exception  *)
(*  on linking described in file LICENSE.                           *)
(*                                                                  *)
(********************************************************************)
François Bobot's avatar
François Bobot committed
11
12

open Debug
13
open Stdlib
François Bobot's avatar
François Bobot committed
14
15
16
17
18
19
open Ty
open Ident
open Decl
open Term
open Theory
open Task
François Bobot's avatar
François Bobot committed
20

21
22
23
24
module Mprover = Whyconf.Mprover
module Sprover = Whyconf.Sprover
module PHprover = Whyconf.Hprover
module C = Whyconf
25
module Tc = Termcode
26

27
let debug = Debug.register_info_flag "session"
Andrei Paskevich's avatar
Andrei Paskevich committed
28
29
  ~desc:"Pring@ debugging@ messages@ about@ Why3@ session@ \
         creation,@ reading@ and@ writing."
François Bobot's avatar
François Bobot committed
30
31
32

(** {2 Type definitions} *)

33
module PHstr = Hstr
François Bobot's avatar
François Bobot committed
34

35
36
37
38
type proof_attempt_status =
    | Unedited (** editor not yet run for interactive proof *)
    | JustEdited (** edited but not run yet *)
    | Interrupted (** external proof has never completed *)
François Bobot's avatar
François Bobot committed
39
40
41
42
43
44
45
    | Scheduled (** external proof attempt is scheduled *)
    | Running (** external proof attempt is in progress *)
    | Done of Call_provers.prover_result (** external proof done *)
    | InternalFailure of exn (** external proof aborted by internal error *)

type task_option = Task.task option

46
47
48
49
type ident_path =
  { ip_library : string list;
    ip_theory : string;
    ip_qualid : string list;
François Bobot's avatar
François Bobot committed
50
51
  }

52
53
54
55
56
57
58
let print_ident_path fmt ip =
  Format.fprintf fmt "%a.%s.%a"
    (Pp.print_list Pp.dot Pp.string) ip.ip_library
    ip.ip_theory
    (Pp.print_list Pp.dot Pp.string) ip.ip_qualid

let compare_ident_path x y =
59
  let c = Lists.compare String.compare x.ip_library y.ip_library in
François Bobot's avatar
François Bobot committed
60
  if c <> 0 then -c else (* in order to be bottom up *)
61
  let c = String.compare x.ip_theory y.ip_theory in
François Bobot's avatar
François Bobot committed
62
  if c <> 0 then c else
63
  let c = Lists.compare String.compare x.ip_qualid y.ip_qualid in
64
  c
François Bobot's avatar
François Bobot committed
65
66

module Pos = struct
67
68
  type t = ident_path
  let compare = compare_ident_path
François Bobot's avatar
François Bobot committed
69
70
71
72
  let equal x y = (x : t) = y
  let hash x = Hashtbl.hash (x : t)
end

73
74
75
module Mpos = Extmap.Make(Pos)
module Spos = Extset.MakeOfMap(Mpos)
module Hpos = Exthtbl.Make(Pos)
François Bobot's avatar
François Bobot committed
76
77
78

type meta_args = meta_arg list

79
module Mmeta_args = Extmap.Make(struct
François Bobot's avatar
François Bobot committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
  type t = meta_args

  let meta_arg_id = function
    | MAty _  -> 0
    | MAts _  -> 1
    | MAls _  -> 2
    | MApr _  -> 3
    | MAstr _ -> 4
    | MAint _ -> 5

  let compare_meta_arg x y =
    match x,y with
  (** These hash are in fact tag *)
    | MAty  x, MAty  y -> compare (ty_hash x) (ty_hash y)
    | MAts  x, MAts  y -> compare (ts_hash x) (ts_hash y)
    | MAls  x, MAls  y -> compare (ls_hash x) (ls_hash y)
    | MApr  x, MApr  y -> compare (pr_hash x) (pr_hash y)
    | MAstr x, MAstr y -> String.compare x y
    | MAint x, MAint y -> compare x y
    | _ -> compare (meta_arg_id x) (meta_arg_id y)

101
  let compare = Lists.compare compare_meta_arg
François Bobot's avatar
François Bobot committed
102
end)
103
104

module Smeta_args = Extset.MakeOfMap(Mmeta_args)
François Bobot's avatar
François Bobot committed
105

Andrei Paskevich's avatar
Andrei Paskevich committed
106
107
type metas_args = Smeta_args.t Mstr.t

108
module Mmetas_args = Extmap.Make(struct
François Bobot's avatar
François Bobot committed
109
110
111
112
113
  type t = metas_args
  let compare = Mstr.compare Smeta_args.compare
end)

type idpos = {
114
115
116
  idpos_ts : ident_path Mts.t;
  idpos_ls : ident_path Mls.t;
  idpos_pr : ident_path Mpr.t;
François Bobot's avatar
François Bobot committed
117
118
119
120
121
122
123
124
125
}

let empty_idpos =
  {
    idpos_ts = Mts.empty;
    idpos_ls = Mls.empty;
    idpos_pr = Mpr.empty;
  }

126
(* dead code
François Bobot's avatar
François Bobot committed
127
128
129
130
131
132
133
134
135
let posid_of_idpos idpos =
  let posid = Mpos.empty in
  let posid = Mts.fold (fun ts pos ->
    Mpos.add pos (MAts ts)) idpos.idpos_ts posid  in
  let posid = Mls.fold (fun ls pos ->
    Mpos.add pos (MAls ls)) idpos.idpos_ls posid  in
  let posid = Mpr.fold (fun pr pos ->
    Mpos.add pos (MApr pr)) idpos.idpos_pr posid  in
  posid
136
*)
François Bobot's avatar
François Bobot committed
137

MARCHE Claude's avatar
MARCHE Claude committed
138
139
type expl = string option

François Bobot's avatar
François Bobot committed
140
type 'a goal =
141
142
  { mutable goal_key  : 'a;
    goal_name : Ident.ident;
MARCHE Claude's avatar
MARCHE Claude committed
143
    goal_expl : expl;
144
145
146
147
148
149
150
151
    goal_parent : 'a goal_parent;
    mutable goal_checksum : Tc.checksum;
    mutable goal_shape : Tc.shape;
    mutable goal_verified : bool;
    goal_task: task_option;
    mutable goal_expanded : bool;
    goal_external_proofs : 'a proof_attempt PHprover.t;
    goal_transformations : 'a transf PHstr.t;
François Bobot's avatar
François Bobot committed
152
    mutable goal_metas : 'a metas Mmetas_args.t;
153
  }
François Bobot's avatar
François Bobot committed
154
155

and 'a proof_attempt =
156
157
158
159
160
161
162
163
164
165
  { proof_key : 'a;
    mutable proof_prover : Whyconf.prover;
    proof_parent : 'a goal;
    mutable proof_state : proof_attempt_status;
    mutable proof_timelimit : int;
    mutable proof_memlimit : int;
    mutable proof_obsolete : bool;
    mutable proof_archived : bool;
    mutable proof_edited_as : string option;
  }
François Bobot's avatar
François Bobot committed
166
167
168
169

and 'a goal_parent =
  | Parent_theory of 'a theory
  | Parent_transf of 'a transf
François Bobot's avatar
François Bobot committed
170
171
172
173
174
175
176
177
178
179
180
181
  | Parent_metas  of 'a metas

and 'a metas =
  { mutable metas_key : 'a;
    metas_added : metas_args;
    metas_idpos : idpos;
    metas_parent : 'a goal;
    mutable metas_verified : bool;
    mutable metas_goal : 'a goal;
    (** Not mutated after the creation *)
    mutable metas_expanded : bool;
  }
François Bobot's avatar
François Bobot committed
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205

and 'a transf =
    { mutable transf_key : 'a;
      transf_name : string;
      (** Why3 tranformation name *)
      transf_parent : 'a goal;
      mutable transf_verified : bool;
      mutable transf_goals : 'a goal list;
      (** Not mutated after the creation of the session *)
      mutable transf_expanded : bool;
    }

and 'a theory =
    { mutable theory_key : 'a;
      theory_name : Ident.ident;
      theory_parent : 'a file;
      mutable theory_goals : 'a goal list;
      mutable theory_verified : bool;
      mutable theory_expanded : bool;
    }

and 'a file =
    { mutable file_key : 'a;
      file_name : string;
206
      file_format : string option;
François Bobot's avatar
François Bobot committed
207
208
209
210
211
212
213
214
215
      file_parent : 'a session;
      mutable file_theories: 'a theory list;
      (** Not mutated after the creation *)
      mutable file_verified : bool;
      mutable file_expanded : bool;
    }

and 'a session =
    { session_files : 'a file PHstr.t;
MARCHE Claude's avatar
MARCHE Claude committed
216
      mutable session_shape_version : int;
François Bobot's avatar
François Bobot committed
217
218
219
220
221
222
223
      session_dir   : string; (** Absolute path *)
    }

type loaded_prover =
    { prover_config : Whyconf.config_prover;
      prover_driver : Driver.driver}

224
type loaded_provers = loaded_prover option PHprover.t
François Bobot's avatar
François Bobot committed
225
226
227

type 'a env_session =
    { env : Env.env;
228
      mutable whyconf : Whyconf.config;
François Bobot's avatar
François Bobot committed
229
230
231
      loaded_provers : loaded_provers;
      session : 'a session}

232
let update_env_session_config e c = e.whyconf <- c
François Bobot's avatar
François Bobot committed
233
234
235
236
237
238
239
240
241
242

(*************************)
(**       Iterators      *)
(*************************)
type 'a any =
  | File of 'a file
  | Theory of 'a theory
  | Goal of 'a goal
  | Proof_attempt of 'a proof_attempt
  | Transf of 'a transf
François Bobot's avatar
François Bobot committed
243
  | Metas of 'a metas
François Bobot's avatar
François Bobot committed
244
245
246

let rec goal_iter_proof_attempt f g =
  PHprover.iter (fun _ a -> f a) g.goal_external_proofs;
François Bobot's avatar
François Bobot committed
247
248
  PHstr.iter (fun _ t -> transf_iter_proof_attempt f t) g.goal_transformations;
  Mmetas_args.iter (fun _ t -> metas_iter_proof_attempt f t) g.goal_metas;
François Bobot's avatar
François Bobot committed
249
250
251
252

and transf_iter_proof_attempt f t =
  List.iter (goal_iter_proof_attempt f) t.transf_goals

François Bobot's avatar
François Bobot committed
253
254
255
and metas_iter_proof_attempt f t =
  goal_iter_proof_attempt f t.metas_goal

François Bobot's avatar
François Bobot committed
256
257
258
let theory_iter_proof_attempt f t =
  List.iter (goal_iter_proof_attempt f) t.theory_goals

François Bobot's avatar
François Bobot committed
259
260
261
let metas_iter_proof_attempt f m =
  goal_iter_proof_attempt f m.metas_goal

François Bobot's avatar
François Bobot committed
262
263
264
265
266
267
let file_iter_proof_attempt f t =
  List.iter (theory_iter_proof_attempt f) t.file_theories

let session_iter_proof_attempt f s =
  PHstr.iter (fun _ file -> file_iter_proof_attempt f file) s.session_files

268
let iter_proof_attempt f = function
François Bobot's avatar
François Bobot committed
269
270
271
272
273
    | Goal g -> goal_iter_proof_attempt f g
    | Theory th -> theory_iter_proof_attempt f th
    | File file -> file_iter_proof_attempt f file
    | Proof_attempt a -> f a
    | Transf tr -> transf_iter_proof_attempt f tr
François Bobot's avatar
François Bobot committed
274
    | Metas m -> metas_iter_proof_attempt f m
François Bobot's avatar
François Bobot committed
275
276
277
278
279
280
281
282
283
284
285

let rec goal_iter_leaf_goal ~unproved_only f g =
  if not (g.goal_verified && unproved_only) then
    let r = ref true in
    PHstr.iter
      (fun _ t -> r := false;
        List.iter (goal_iter_leaf_goal ~unproved_only f) t.transf_goals)
      g.goal_transformations;
    if !r then f g

(** iterators not reccursive *)
François Bobot's avatar
François Bobot committed
286
let iter_goal fp ft fm g =
François Bobot's avatar
François Bobot committed
287
  PHprover.iter (fun _ a -> fp a) g.goal_external_proofs;
François Bobot's avatar
François Bobot committed
288
289
  PHstr.iter (fun _ t -> ft t) g.goal_transformations;
  Mmetas_args.iter (fun _ t -> fm t) g.goal_metas
François Bobot's avatar
François Bobot committed
290
291
292
293

let iter_transf f t =
  List.iter (fun g -> f g) t.transf_goals

François Bobot's avatar
François Bobot committed
294
295
let iter_metas f t = f t.metas_goal

296
297
298
299
let iter_file f fi = List.iter f fi.file_theories

let iter_session f s = PHstr.iter (fun _ th -> f th) s.session_files

François Bobot's avatar
François Bobot committed
300
let goal_iter f g =
301
302
  PHprover.iter
    (fun _ a -> f (Proof_attempt a)) g.goal_external_proofs;
François Bobot's avatar
François Bobot committed
303
304
305
  PHstr.iter (fun _ t -> f (Transf t)) g.goal_transformations;
  Mmetas_args.iter (fun _ t -> f (Metas t)) g.goal_metas

François Bobot's avatar
François Bobot committed
306
307
308
309

let transf_iter f t =
  List.iter (fun g -> f (Goal g)) t.transf_goals

François Bobot's avatar
François Bobot committed
310
311
312
let metas_iter f t =
  f (Goal t.metas_goal)

François Bobot's avatar
François Bobot committed
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
let theory_iter f t =
  List.iter (fun g -> f (Goal g)) t.theory_goals

let file_iter f t =
  List.iter (fun th -> f (Theory th)) t.file_theories

let session_iter f s =
  PHstr.iter (fun _ file -> f (File file)) s.session_files

let iter f = function
    | Goal g -> goal_iter f g
    | Theory th -> theory_iter f th
    | File file -> file_iter f file
    | Proof_attempt _ -> ()
    | Transf tr -> transf_iter f tr
François Bobot's avatar
François Bobot committed
328
    | Metas m -> metas_iter f m
François Bobot's avatar
François Bobot committed
329
330
331
332
333
334
335
336
337

(** Print session *)


module PTreeT = struct
  type 'a t = | Any of 'a any | Session of 'a session
  let decomp = function
    | Any t ->
      let s = match t with
338
339
340
341
342
343
344
345
        | File f ->
          if f.file_verified
          then f.file_name
          else f.file_name^"?"
        | Theory th ->
          if th.theory_verified
          then th.theory_name.Ident.id_string
          else th.theory_name.Ident.id_string^"?"
François Bobot's avatar
François Bobot committed
346
        | Goal g ->
347
348
349
          if g.goal_verified
          then g.goal_name.Ident.id_string
          else g.goal_name.Ident.id_string^"?"
350
        | Proof_attempt pr ->
351
          Pp.sprintf_wnl "%a%s%s%s%s"
352
            Whyconf.print_prover pr.proof_prover
353
354
355
356
            (match pr.proof_state with
              | Done { Call_provers.pr_answer = Call_provers.Valid} -> ""
              | InternalFailure _ -> "!"
              | _ -> "?")
357
358
359
            (if pr.proof_obsolete || pr.proof_archived then " " else "")
            (if pr.proof_obsolete then "O" else "")
            (if pr.proof_archived then "A" else "")
360
361
362
        | Transf tr ->
          if tr.transf_verified
          then tr.transf_name
François Bobot's avatar
François Bobot committed
363
364
365
366
367
368
          else tr.transf_name^"?"
        | Metas metas ->
          if metas.metas_verified
          then "metas..."
          else "metas..."^"?"
      in
François Bobot's avatar
François Bobot committed
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
      let l = ref [] in
      iter (fun a -> l := (Any a)::!l) t;
      s,!l
    | Session s ->
      let l = ref [] in
      session_iter (fun a -> l := (Any a)::!l) s;
      Filename.basename s.session_dir,!l

end

module PTree = Print_tree.PMake(PTreeT)

let print_any fmt any = PTree.print fmt (PTreeT.Any any)

let print_session fmt s = PTree.print fmt (PTreeT.Session s)

(** 2 Create a session *)

MARCHE Claude's avatar
MARCHE Claude committed
387
388
389
390
391
392
393
394
395
396
397
let empty_session ?shape_version dir =
  let shape_version = match shape_version with
    | Some v -> v
    | None -> Termcode.current_shape_version
  in
  { session_files = PHstr.create 3;
    session_shape_version = shape_version;
    session_dir   = dir;
  }

let create_session ?shape_version project_dir =
François Bobot's avatar
François Bobot committed
398
399
400
401
402
403
404
  if not (Sys.file_exists project_dir) then
    begin
      dprintf debug
        "[Info] '%s' does not exists. Creating directory of that name \
 for the project@." project_dir;
      Unix.mkdir project_dir 0o777
    end;
MARCHE Claude's avatar
MARCHE Claude committed
405
  empty_session ?shape_version project_dir
François Bobot's avatar
François Bobot committed
406
407


408
(* dead code
François Bobot's avatar
François Bobot committed
409
410
411
412
413
414
415
let load_env_session ?(includes=[]) session conf_path_opt =
  let config = Whyconf.read_config conf_path_opt in
  let loadpath = (Whyconf.loadpath (Whyconf.get_main config)) @ includes in
  let env = Env.create_env loadpath in
  { session = session;
    env = env;
    whyconf = config;
416
    loaded_provers = PHprover.create 5;
François Bobot's avatar
François Bobot committed
417
  }
418
*)
François Bobot's avatar
François Bobot committed
419
420
421
422
423

(************************)
(* session accessor     *)
(************************)

424
(* dead code
François Bobot's avatar
François Bobot committed
425
426
427
428
429
430
431
432
let get_session_file file = file.file_parent

let get_session_theory th = get_session_file th.theory_parent

let rec get_session_goal goal =
  match goal.goal_parent with
    | Parent_transf trans -> get_session_trans trans
    | Parent_theory th    -> get_session_theory th
François Bobot's avatar
François Bobot committed
433
    | Parent_metas  metas -> get_session_metas metas
François Bobot's avatar
François Bobot committed
434

435

François Bobot's avatar
François Bobot committed
436
437
and get_session_trans transf = get_session_goal transf.transf_parent

François Bobot's avatar
François Bobot committed
438
439
and get_session_metas metas = get_session_goal metas.metas_parent

François Bobot's avatar
François Bobot committed
440
let get_session_proof_attempt pa = get_session_goal pa.proof_parent
441
*)
François Bobot's avatar
François Bobot committed
442

443
let get_used_provers session =
François Bobot's avatar
François Bobot committed
444
445
446
447
448
449
450
451
  let sprover = ref Sprover.empty in
  session_iter_proof_attempt
    (fun pa -> sprover := Sprover.add pa.proof_prover !sprover)
    session;
  !sprover

exception NoTask

452
let goal_task g = Opt.get_exn NoTask g.goal_task
François Bobot's avatar
François Bobot committed
453
454
let goal_task_option g = g.goal_task

455
let goal_expl g = Opt.get_def g.goal_name.Ident.id_string g.goal_expl
François Bobot's avatar
François Bobot committed
456
457
458
459
460
461
462

(************************)
(* saving state on disk *)
(************************)
open Format

let db_filename = "why3session.xml"
463
let session_dir_for_save = ref "."
François Bobot's avatar
François Bobot committed
464

MARCHE Claude's avatar
MARCHE Claude committed
465
466
467
468
469
470
471
472
473
474
475
476
let save_string fmt s =
  for i=0 to String.length s - 1 do
    match String.get s i with
      | '\"' -> pp_print_string fmt "&quot;"
      | '\'' -> pp_print_string fmt "&apos;"
      | '<' -> pp_print_string fmt "&lt;"
      | '>' -> pp_print_string fmt "&gt;"
      | '&' -> pp_print_string fmt "&amp;"
      | c -> pp_print_char fmt c
  done


François Bobot's avatar
François Bobot committed
477
478
479
480
481
482
483
484
let save_result fmt r =
  fprintf fmt "@\n<result status=\"%s\" time=\"%.2f\"/>"
    (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"
485
       | Call_provers.OutOfMemory -> "outofmemory"
François Bobot's avatar
François Bobot committed
486
487
488
489
490
       | Call_provers.Invalid -> "invalid")
    r.Call_provers.pr_time

let save_status fmt s =
  match s with
491
    | Unedited ->
492
        fprintf fmt "@\n<unedited/>"
493
    | Scheduled | Running | Interrupted | JustEdited ->
494
        fprintf fmt "@\n<undone/>"
François Bobot's avatar
François Bobot committed
495
    | InternalFailure msg ->
496
        fprintf fmt "@\n<internalfailure reason=\"%a\"/>"
MARCHE Claude's avatar
MARCHE Claude committed
497
          save_string (Printexc.to_string msg)
François Bobot's avatar
François Bobot committed
498
499
500
501
502
    | Done r -> save_result fmt r


let opt lab fmt = function
  | None -> ()
MARCHE Claude's avatar
MARCHE Claude committed
503
  | Some s -> fprintf fmt "%s=\"%a\"@ " lab save_string s
François Bobot's avatar
François Bobot committed
504

505
let save_proof_attempt fmt (id,a) =
François Bobot's avatar
François Bobot committed
506
  fprintf fmt
507
508
    "@\n@[<v 1><proof@ prover=\"%i\"@ timelimit=\"%d\"@ \
memlimit=\"%d\"@ %aobsolete=\"%b\"@ archived=\"%b\">"
509
    id a.proof_timelimit a.proof_memlimit
510
511
    (opt "edited") a.proof_edited_as a.proof_obsolete
    a.proof_archived;
François Bobot's avatar
François Bobot committed
512
513
514
515
  save_status fmt a.proof_state;
  fprintf fmt "@]@\n</proof>"

let save_ident fmt id =
MARCHE Claude's avatar
MARCHE Claude committed
516
  fprintf fmt "name=\"%a\"" save_string id.Ident.id_string;
François Bobot's avatar
François Bobot committed
517
518
519
520
  match id.Ident.id_loc with
    | None -> ()
    | Some loc ->
      let file,lnum,cnumb,cnume = Loc.get loc in
521
      let file = Sysutil.relativize_filename !session_dir_for_save file in
François Bobot's avatar
François Bobot committed
522
      fprintf fmt
MARCHE Claude's avatar
MARCHE Claude committed
523
524
        "@ locfile=\"%a\"@ loclnum=\"%i\" loccnumb=\"%i\" loccnume=\"%i\""
        save_string file lnum cnumb cnume
François Bobot's avatar
François Bobot committed
525
526

let save_label fmt s =
MARCHE Claude's avatar
MARCHE Claude committed
527
  fprintf fmt "@\n@[<v 1><label@ name=\"%a\"/>@]" save_string s.Ident.lab_string
528

François Bobot's avatar
François Bobot committed
529
let rec save_goal provers fmt g =
530
  assert (Tc.string_of_shape g.goal_shape <> "");
François Bobot's avatar
François Bobot committed
531
  fprintf fmt
MARCHE Claude's avatar
MARCHE Claude committed
532
    "@\n@[<v 1><goal@ %a@ %asum=\"%a\"@ proved=\"%b\"@ \
533
expanded=\"%b\"@ shape=\"%a\">"
François Bobot's avatar
François Bobot committed
534
535
    save_ident g.goal_name
    (opt "expl") g.goal_expl
536
537
538
    save_string (Tc.string_of_checksum g.goal_checksum)
    g.goal_verified  g.goal_expanded
    save_string (Tc.string_of_shape g.goal_shape);
539
  Ident.Slab.iter (save_label fmt) g.goal_name.Ident.id_label;
540
541
542
543
544
545
546
547
  let l = PHprover.fold
    (fun _ a acc -> (Mprover.find a.proof_prover provers, a) :: acc)
    g.goal_external_proofs [] in
  let l = List.sort (fun (i1,_) (i2,_) -> compare i1 i2) l in
  List.iter (save_proof_attempt fmt) l;
  let l = PHstr.fold (fun _ t acc -> t :: acc) g.goal_transformations [] in
  let l = List.sort (fun t1 t2 -> compare t1.transf_name t2.transf_name) l in
  List.iter (save_trans provers fmt) l;
François Bobot's avatar
François Bobot committed
548
  Mmetas_args.iter (save_metas provers fmt) g.goal_metas;
François Bobot's avatar
François Bobot committed
549
550
  fprintf fmt "@]@\n</goal>"

551
and save_trans provers fmt t =
MARCHE Claude's avatar
MARCHE Claude committed
552
553
  fprintf fmt "@\n@[<v 1><transf@ name=\"%a\"@ proved=\"%b\"@ expanded=\"%b\">"
    save_string t.transf_name t.transf_verified t.transf_expanded;
François Bobot's avatar
François Bobot committed
554
555
556
  List.iter (save_goal provers fmt) t.transf_goals;
  fprintf fmt "@]@\n</transf>"

François Bobot's avatar
François Bobot committed
557
558
559
560
and save_metas provers fmt _ m =
  fprintf fmt "@\n@[<v 1><metas@ proved=\"%b\"@ expanded=\"%b\">"
    m.metas_verified m.metas_expanded;
  let save_pos fmt pos =
Andrei Paskevich's avatar
Andrei Paskevich committed
561
    fprintf fmt "ip_theory=\"%a\">" save_string pos.ip_theory;
562
563
564
565
566
    List.iter (fprintf fmt "@\n@[<v 1><ip_library@ name=\"%a\"/>@]" save_string)
      pos.ip_library;
    List.iter (fprintf fmt "@\n@[<v 1><ip_qualid@ name=\"%a\"/>@]" save_string)
      pos.ip_qualid;
  in
François Bobot's avatar
François Bobot committed
567
568
  let save_ts_pos fmt ts pos =
    fprintf fmt "@\n@[<v 1><ts_pos@ name=\"%a\"@ arity=\"%i\"@ \
Andrei Paskevich's avatar
Andrei Paskevich committed
569
    id=\"%i\"@ %a@]@\n</ts_pos>"
François Bobot's avatar
François Bobot committed
570
571
572
573
      save_string ts.ts_name.id_string (List.length ts.ts_args)
      (ts_hash ts) save_pos pos in
  let save_ls_pos fmt ls pos =
    (** TODO: add the signature? *)
Andrei Paskevich's avatar
Andrei Paskevich committed
574
    fprintf fmt "@\n@[<v 1><ls_pos@ name=\"%a\"@ id=\"%i\"@ %a@]@\n</ls_pos>"
François Bobot's avatar
François Bobot committed
575
      save_string ls.ls_name.id_string
576
      (ls_hash ls) save_pos pos
François Bobot's avatar
François Bobot committed
577
578
  in
  let save_pr_pos fmt pr pos =
Andrei Paskevich's avatar
Andrei Paskevich committed
579
    fprintf fmt "@\n@[<v 1><pr_pos@ name=\"%a\"@ id=\"%i\"@ %a@]@\n</pr_pos>"
François Bobot's avatar
François Bobot committed
580
      save_string pr.pr_name.id_string
581
      (pr_hash pr) save_pos pos
François Bobot's avatar
François Bobot committed
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
  in
  Mts.iter (save_ts_pos fmt) m.metas_idpos.idpos_ts;
  Mls.iter (save_ls_pos fmt) m.metas_idpos.idpos_ls;
  Mpr.iter (save_pr_pos fmt) m.metas_idpos.idpos_pr;
  Mstr.iter (fun s smeta_args ->
    Smeta_args.iter (save_meta_args fmt s) smeta_args) m.metas_added;
  save_goal provers fmt m.metas_goal;
  fprintf fmt "@]@\n</metas>"

and save_meta_args fmt s l =
  fprintf fmt "@\n@[<v 1><meta@ name=\"%a\">" save_string s;
  let save_meta_arg fmt = function
    | MAty ty -> fprintf fmt "@\n@[<v 1><meta_arg_ty/>";
      save_ty fmt ty;
      fprintf fmt "@]@\n</meta_arg_ty>"
    | MAts ts ->
      fprintf fmt "@\n@[<v 1><meta_arg_ts@ id=\"%i\"/>@]" (ts_hash ts)
    | MAls ls ->
      fprintf fmt "@\n@[<v 1><meta_arg_ls@ id=\"%i\"/>@]" (ls_hash ls)
    | MApr pr ->
      fprintf fmt "@\n@[<v 1><meta_arg_pr@ id=\"%i\"/>@]" (pr_hash pr)
    | MAstr s ->
      fprintf fmt "@\n@[<v 1><meta_arg_str@ val=\"%s\"/>@]" s
    | MAint i ->
      fprintf fmt "@\n@[<v 1><meta_arg_int@ val=\"%i\"/>@]" i
  in
  List.iter (save_meta_arg fmt) l;
609
  fprintf fmt "@]@\n</meta>"
François Bobot's avatar
François Bobot committed
610
611
612
613
614
615
616
617
618
619

and save_ty fmt ty =
  match ty.ty_node with
  | Tyvar tv ->
    fprintf fmt "@\n@[<v 1><ty_var@ id=\"%i\"/>@]" (tv_hash tv)
  | Tyapp (ts,l) ->
    fprintf fmt "@\n@[<v 1><ty_app@ id=\"%i\"/>" (ts_hash ts);
    List.iter (save_ty fmt) l;
    fprintf fmt "@]@\n</ty_app>"

François Bobot's avatar
François Bobot committed
620
621
622
623
624
let save_theory provers fmt t =
  fprintf fmt
    "@\n@[<v 1><theory@ %a@ verified=\"%b\"@ expanded=\"%b\">"
    save_ident t.theory_name
    t.theory_verified t.theory_expanded;
625
  Ident.Slab.iter (save_label fmt) t.theory_name.Ident.id_label;
François Bobot's avatar
François Bobot committed
626
627
628
629
  List.iter (save_goal provers fmt) t.theory_goals;
  fprintf fmt "@]@\n</theory>"

let save_file provers fmt _ f =
630
631
632
633
  fprintf fmt
    "@\n@[<v 1><file@ name=\"%a\"@ %averified=\"%b\"@ expanded=\"%b\">"
    save_string f.file_name (opt "format")
    f.file_format f.file_verified f.file_expanded;
François Bobot's avatar
François Bobot committed
634
635
636
637
  List.iter (save_theory provers fmt) f.file_theories;
  fprintf fmt "@]@\n</file>"

let save_prover fmt p (provers,id) =
638
  fprintf fmt "@\n@[<v 1><prover@ id=\"%i\"@ \
MARCHE Claude's avatar
MARCHE Claude committed
639
640
name=\"%a\"@ version=\"%a\"%a/>@]"
    id save_string p.C.prover_name save_string p.C.prover_version
641
642
    (fun fmt s -> if s <> "" then fprintf fmt "@ alternative=\"%a\""
        save_string s)
643
    p.C.prover_altern;
François Bobot's avatar
François Bobot committed
644
645
  Mprover.add p id provers, id+1

MARCHE Claude's avatar
MARCHE Claude committed
646
let save fname _config session =
François Bobot's avatar
François Bobot committed
647
648
649
  let ch = open_out fname in
  let fmt = formatter_of_out_channel ch in
  fprintf fmt "<?xml version=\"1.0\" encoding=\"UTF-8\"?>@\n";
MARCHE Claude's avatar
MARCHE Claude committed
650
  fprintf fmt "<!DOCTYPE why3session PUBLIC \"-//Why3//proof session v2//EN\" \"http://why3.lri.fr/why3session.dtd\">@\n";
651
652
(*
  let rel_file = Sysutil.relativize_filename !session_dir_for_save fname in
MARCHE Claude's avatar
MARCHE Claude committed
653
  fprintf fmt "@[<v 1><why3session@ name=\"%a\" shape_version=\"%d\">"
654
655
656
657
    save_string rel_file session.session_shape_version;
*)
  fprintf fmt "@[<v 1><why3session shape_version=\"%d\">"
    session.session_shape_version;
658
  let provers,_ = Sprover.fold (save_prover fmt) (get_used_provers session)
François Bobot's avatar
François Bobot committed
659
660
661
662
663
664
    (Mprover.empty,0) in
  PHstr.iter (save_file provers fmt) session.session_files;
  fprintf fmt "@]@\n</why3session>";
  fprintf fmt "@.";
  close_out ch

MARCHE Claude's avatar
MARCHE Claude committed
665
let save_session config session =
François Bobot's avatar
François Bobot committed
666
667
  let f = Filename.concat session.session_dir db_filename in
  Sysutil.backup_file f;
668
  session_dir_for_save := session.session_dir;
MARCHE Claude's avatar
MARCHE Claude committed
669
  save f config session
François Bobot's avatar
François Bobot committed
670
671
672
673
674
675
676

(*******************************)
(*          explanations       *)
(*******************************)

let expl_regexp = Str.regexp "expl:\\(.*\\)"

677
let check_expl lab acc =
Andrei Paskevich's avatar
Andrei Paskevich committed
678
  let lab = lab.Ident.lab_string in
679
680
681
682
  if Str.string_match expl_regexp lab 0
  then Some (Str.matched_group 1 lab)
  else acc

683
684
685
let check_expl lab =
  if Ident.Slab.mem Split_goal.stop_split lab then None
  else Ident.Slab.fold check_expl lab None
686
687
688
689
690
691
692
693
694
695
696
697
698

let rec get_expl_fmla acc f =
  if f.t_ty <> None then acc else
  let res = check_expl f.Term.t_label in
  if res = None then match f.t_node with
    | Term.Ttrue | Term.Tfalse | Term.Tapp _ -> acc
    | Term.Tbinop(Term.Timplies,_,f) -> get_expl_fmla acc f
    | Term.Tlet _ | Term.Tcase _ | Term.Tquant (Term.Tforall, _) ->
        Term.t_fold get_expl_fmla acc f
    | _ -> raise Exit
  else if acc = None then res else raise Exit

let get_expl_fmla f = try get_expl_fmla None f with Exit -> None
François Bobot's avatar
François Bobot committed
699

MARCHE Claude's avatar
MARCHE Claude committed
700
701
let goal_expl_task ~root task =
  let gid = (Task.task_goal task).Decl.pr_name in
702
  let info =
MARCHE Claude's avatar
MARCHE Claude committed
703
704
705
706
707
    let fmla = Task.task_goal_fmla task in
    let res = get_expl_fmla fmla in
    if res <> None || not root then res else check_expl gid.Ident.id_label
  in
  gid, info, task
François Bobot's avatar
François Bobot committed
708
709
710
711
712
713
714
715


(*****************************)
(*   update verified field   *)
(*****************************)
type 'a notify = 'a any -> unit
let notify : 'a notify = fun _ -> ()

716
717
let file_verified f =
  List.for_all (fun t -> t.theory_verified) f.file_theories
François Bobot's avatar
François Bobot committed
718

719
720
721
722
723
let theory_verified t =
  List.for_all (fun g -> g.goal_verified) t.theory_goals

let transf_verified t =
  List.for_all (fun g -> g.goal_verified) t.transf_goals
François Bobot's avatar
François Bobot committed
724

François Bobot's avatar
François Bobot committed
725
726
let metas_verified m = m.metas_goal.goal_verified

François Bobot's avatar
François Bobot committed
727
728
729
730
731
732
let proof_verified a =
  (not a.proof_obsolete) &&
    match a.proof_state with
      | Done { Call_provers.pr_answer = Call_provers.Valid} -> true
      | _ -> false

733
let goal_verified g =
François Bobot's avatar
François Bobot committed
734
735
736
737
738
739
740
741
742
    try
      PHprover.iter
        (fun _ a ->
          if proof_verified a
          then raise Exit)
        g.goal_external_proofs;
      PHstr.iter
        (fun _ t -> if t.transf_verified then raise Exit)
        g.goal_transformations;
François Bobot's avatar
François Bobot committed
743
744
745
      Mmetas_args.iter
        (fun _ t -> if t.metas_verified then raise Exit)
        g.goal_metas;
François Bobot's avatar
François Bobot committed
746
747
      false
    with Exit -> true
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765

let check_file_verified notify f =
  let b = file_verified f in
  if f.file_verified <> b then begin
    f.file_verified <- b;
    notify (File f)
  end

let check_theory_proved notify t =
  let b = theory_verified t in
  if t.theory_verified <> b then begin
    t.theory_verified <- b;
    notify (Theory t);
    check_file_verified notify t.theory_parent
  end

let rec check_goal_proved notify g =
  let b = goal_verified g in
François Bobot's avatar
François Bobot committed
766
767
768
769
770
771
  if g.goal_verified <> b then begin
    g.goal_verified <- b;
    notify (Goal g);
    match g.goal_parent with
      | Parent_theory t -> check_theory_proved notify t
      | Parent_transf t -> check_transf_proved notify t
François Bobot's avatar
François Bobot committed
772
      | Parent_metas t -> check_metas_proved notify t
François Bobot's avatar
François Bobot committed
773
774
775
  end

and check_transf_proved notify t =
776
  let b = transf_verified t in
François Bobot's avatar
François Bobot committed
777
778
779
780
781
782
  if t.transf_verified <> b then begin
    t.transf_verified <- b;
    notify (Transf t);
    check_goal_proved notify t.transf_parent
  end

François Bobot's avatar
François Bobot committed
783
784
785
786
787
788
789
790
791

and check_metas_proved notify m =
  let b = metas_verified m in
  if m.metas_verified <> b then begin
    m.metas_verified <- b;
    notify (Metas m);
    check_goal_proved notify m.metas_parent
  end

François Bobot's avatar
François Bobot committed
792
793
794
795
796
797
798
(******************************)
(* raw additions to the model *)
(******************************)
type 'a keygen = ?parent:'a -> unit -> 'a

let add_external_proof
    ?(notify=notify)
799
    ~(keygen:'a keygen) ~obsolete
800
    ~archived ~timelimit ~memlimit ~edit (g:'a goal) p result =
801
  assert (edit <> Some "");
François Bobot's avatar
François Bobot committed
802
803
804
805
806
  let key = keygen ~parent:g.goal_key () in
  let a = { proof_prover = p;
            proof_parent = g;
            proof_key = key;
            proof_obsolete = obsolete;
807
            proof_archived = archived;
François Bobot's avatar
François Bobot committed
808
809
            proof_state = result;
            proof_timelimit = timelimit;
810
            proof_memlimit = memlimit;
François Bobot's avatar
François Bobot committed
811
812
813
            proof_edited_as = edit;
          }
  in
814
  PHprover.replace g.goal_external_proofs p a;
François Bobot's avatar
François Bobot committed
815
816
817
818
819
820
821
822
823
  check_goal_proved notify g;
  a

let remove_external_proof ?(notify=notify) a =
  let g = a.proof_parent in
  PHprover.remove g.goal_external_proofs a.proof_prover;
  check_goal_proved notify g


824
let set_proof_state ?(notify=notify) ~obsolete ~archived res a =
François Bobot's avatar
François Bobot committed
825
826
  a.proof_state <- res;
  a.proof_obsolete <- obsolete;
827
  a.proof_archived <- archived;
François Bobot's avatar
François Bobot committed
828
829
830
  notify (Proof_attempt a);
  check_goal_proved notify a.proof_parent

831
832
833
834
835
836
let change_prover a p =
  let g = a.proof_parent in
  PHprover.remove g.goal_external_proofs a.proof_prover;
  PHprover.add g.goal_external_proofs p a;
  a.proof_prover <- p;
  a.proof_obsolete <- true
837

François Bobot's avatar
François Bobot committed
838
839
840
let set_edited_as edited_as a = a.proof_edited_as <- edited_as

let set_timelimit timelimit a = a.proof_timelimit <- timelimit
841
let set_memlimit memlimit a = a.proof_memlimit <- memlimit
François Bobot's avatar
François Bobot committed
842

843
844
845
846
847
let set_obsolete ?(notify=notify) a =
  a.proof_obsolete <- true;
  notify (Proof_attempt a);
  check_goal_proved notify a.proof_parent

848
let set_archived a b = a.proof_archived <- b
849

850
let get_edited_as_abs session pr =
851
  Opt.map (Filename.concat session.session_dir) pr.proof_edited_as
852

François Bobot's avatar
François Bobot committed
853
854
855
(* [raw_add_goal parent name expl sum t] adds a goal to the given parent
   DOES NOT record the new goal in its parent, thus this should not be exported
*)
856
let raw_add_no_task ~(keygen:'a keygen) ~(expanded:bool) parent name expl sum shape =
François Bobot's avatar
François Bobot committed
857
858
859
  let parent_key = match parent with
    | Parent_theory mth -> mth.theory_key
    | Parent_transf mtr -> mtr.transf_key
François Bobot's avatar
François Bobot committed
860
    | Parent_metas  mms -> mms.metas_key
François Bobot's avatar
François Bobot committed
861
862
863
864
865
866
867
868
869
870
871
  in
  let key = keygen ~parent:parent_key () in
  let goal = { goal_name = name;
               goal_expl = expl;
               goal_parent = parent;
               goal_task = None ;
               goal_checksum = sum;
               goal_shape = shape;
               goal_key = key;
               goal_external_proofs = PHprover.create 7;
               goal_transformations = PHstr.create 3;
François Bobot's avatar
François Bobot committed
872
               goal_metas = Mmetas_args.empty;
François Bobot's avatar
François Bobot committed
873
               goal_verified = false;
874
               goal_expanded = expanded;
François Bobot's avatar
François Bobot committed
875
876
877
878
             }
  in
  goal

879
let raw_add_task ~version ~(keygen:'a keygen) ~(expanded:bool) parent name expl t =
François Bobot's avatar
François Bobot committed
880
881
882
  let parent_key = match parent with
    | Parent_theory mth -> mth.theory_key
    | Parent_transf mtr -> mtr.transf_key
François Bobot's avatar
François Bobot committed
883
    | Parent_metas  mms -> mms.metas_key
François Bobot's avatar
François Bobot committed
884
885
  in
  let key = keygen ~parent:parent_key () in
886
887
  let sum = Termcode.task_checksum ~version t in
  let shape = Termcode.t_shape_buf ~version
888
    (Task.task_goal_fmla t) in
François Bobot's avatar
François Bobot committed
889
890
891
892
893
894
895
896
897
  let goal = { goal_name = name;
               goal_expl = expl;
               goal_parent = parent;
               goal_task = Some t ;
               goal_checksum = sum;
               goal_shape = shape;
               goal_key = key;
               goal_external_proofs = PHprover.create 7;
               goal_transformations = PHstr.create 3;
François Bobot's avatar
François Bobot committed
898
               goal_metas = Mmetas_args.empty;
François Bobot's avatar
François Bobot committed
899
               goal_verified = false;
900
               goal_expanded = expanded;
François Bobot's avatar
François Bobot committed
901
902
903
904
905
906
907
908
             }
  in
  goal


(* [raw_add_transformation g name adds a transformation to the given goal g
   Adds no subgoals, thus this should not be exported
*)
909
let raw_add_transformation ~(keygen:'a keygen) ~(expanded:bool) g name =
François Bobot's avatar
François Bobot committed
910
911
912
913
914
915
916
  let parent = g.goal_key in
  let key = keygen ~parent () in
  let tr = { transf_name = name;
             transf_parent = g;
             transf_verified = false;
             transf_key = key;
             transf_goals = [];
917
             transf_expanded = expanded;
François Bobot's avatar
François Bobot committed
918
919
           }
  in
920
  PHstr.replace g.goal_transformations name tr;
François Bobot's avatar
François Bobot committed
921
922
  tr

923
let raw_add_metas ~(keygen:'a keygen) ~(expanded:bool) g added idpos =
François Bobot's avatar
François Bobot committed
924
925
926
927
928
929
930
931
  let parent = g.goal_key in
  let key = keygen ~parent () in
  let ms = { metas_added = added;
             metas_idpos = idpos;
             metas_parent = g;
             metas_verified = false;
             metas_key = key;
             metas_goal = g;
932
             metas_expanded = expanded;
François Bobot's avatar
François Bobot committed
933
934
935
936
937
           }
  in
  g.goal_metas <- Mmetas_args.add added ms g.goal_metas;
  ms

938
let raw_add_theory ~(keygen:'a keygen) ~(expanded:bool) mfile thname =
François Bobot's avatar
François Bobot committed
939
940
941
942
943
  let parent = mfile.file_key in
  let key = keygen ~parent () in
  let mth = { theory_name = thname;
              theory_key = key;
              theory_parent = mfile;
944
              theory_goals = [];
François Bobot's avatar
François Bobot committed
945
              theory_verified = false;
946
              theory_expanded = expanded;
François Bobot's avatar
François Bobot committed
947
948
949
950
            }
  in
  mth

951
let raw_add_file ~(keygen:'a keygen) ~(expanded:bool) session f fmt =
François Bobot's avatar
François Bobot committed
952
953
954
  let key = keygen () in
  let mfile = { file_name = f;
                file_key = key;
955
956
                file_format = fmt;
                file_theories = [];
François Bobot's avatar
François Bobot committed
957
                file_verified = false;
958
                file_expanded = expanded;
François Bobot's avatar
François Bobot committed
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
                file_parent  = session;
              }
  in
  PHstr.replace session.session_files f mfile;
  mfile


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

exception LoadError of Xml.element * string
(** LoadError (xml,messg) *)

let bool_attribute field r def =
  try
    match List.assoc field r.Xml.attributes with
      | "true" -> true
      | "false" -> false
      | _ -> assert false
  with Not_found -> def

François Bobot's avatar
François Bobot committed
981
let int_attribute_def field r def =
François Bobot's avatar
François Bobot committed
982
983
984
985
  try
    int_of_string (List.assoc field r.Xml.attributes)
  with Not_found | Invalid_argument _ -> def

François Bobot's avatar
François Bobot committed
986
987
988
989
990
991
992
993
994
let int_attribute field r =
  try
    int_of_string (List.assoc field r.Xml.attributes)
  with Not_found | Invalid_argument _ ->
    (** TODO: use real error *)
    eprintf "[Error] missing required attribute '%s' from element '%s'@."
      field r.Xml.name;
    assert false

François Bobot's avatar
François Bobot committed
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
let string_attribute_def field r def=
  try
    List.assoc field r.Xml.attributes
  with Not_found -> def

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 keygen ?parent:_ () = ()

let load_result r =
  match r.Xml.name with
    | "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 ""
            | "timeout" -> Call_provers.Timeout
1020
            | "outofmemory" -> Call_provers.OutOfMemory
François Bobot's avatar
François Bobot committed
1021
            | "failure" -> Call_provers.Failure ""
1022
            | "highfailure" -> Call_provers.HighFailure
François Bobot's avatar
François Bobot committed
1023
1024
1025
            | s ->
                eprintf
                  "[Warning] Session.load_result: unexpected status '%s'@." s;
1026
                Call_provers.HighFailure
François Bobot's avatar
François Bobot committed
1027
1028
1029
1030
1031
1032
1033
1034
1035
        in
        let time =
          try float_of_string (List.assoc "time" r.Xml.attributes)
          with Not_found -> 0.0
        in
        Done {
          Call_provers.pr_answer = answer;
          Call_provers.pr_time = time;
          Call_provers.pr_output = "";
1036
          Call_provers.pr_status = Unix.WEXITED 0
François Bobot's avatar
François Bobot committed
1037
        }
1038
1039
    | "undone" -> Interrupted
    | "unedited" -> Unedited
François Bobot's avatar
François Bobot committed
1040
1041
    | s ->
        eprintf "[Warning] Session.load_result: unexpected element '%s'@." s;
1042
        Interrupted
François Bobot's avatar
François Bobot committed
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052

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
    (fun acc label ->
      match label with
Andrei Paskevich's avatar
Andrei Paskevich committed
1053
1054
        | {Xml.name = "label"} ->
            let lab = string_attribute "name" label in
1055
            Ident.Slab.add (Ident.create_label lab) acc
François Bobot's avatar
François Bobot committed
1056
        | _ -> acc
1057
    ) Ident.Slab.empty elt.Xml.elements in
François Bobot's avatar
François Bobot committed
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
  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

let rec load_goal ~old_provers parent acc g =
  match g.Xml.name with
    | "goal" ->
        let gname = load_ident g in
        let expl = load_option "expl" g in
1076
1077
        let sum = Tc.checksum_of_string (string_attribute_def "sum" g "") in
        let shape = Tc.shape_of_string (string_attribute_def "shape" g "") in
1078
1079
1080
1081
        let expanded = bool_attribute "expanded" g true in
        let mg =
          raw_add_no_task ~keygen ~expanded parent gname expl sum shape
        in
François Bobot's avatar
François Bobot committed
1082
        List.iter (load_proof_or_transf ~old_provers mg) g.Xml.elements;
1083
        mg.goal_verified <- goal_verified mg;
François Bobot's avatar
François Bobot committed
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
        mg::acc
    | "label" -> acc
    | s ->
        eprintf "[Warning] Session.load_goal: unexpected element '%s'@." s;
        acc

and load_proof_or_transf ~old_provers mg a =
  match a.Xml.name with
    | "proof" ->
        let prover = string_attribute "prover" a in
        let p =
          try
1096
            let p = Mstr.find prover old_provers in
François Bobot's avatar
François Bobot committed
1097
1098
1099
1100
1101
1102
1103
            p
          with Not_found ->
            eprintf "[Error] prover not listing in header '%s'@." prover;
            raise (LoadError (a,"prover not listing in header"))
        in
        let res = match a.Xml.elements with
          | [r] -> load_result r
1104
          | [] -> Interrupted
François Bobot's avatar
François Bobot committed
1105
1106
1107
1108
1109
1110
          | _ ->
            eprintf "[Error] Too many result elements@.";
            raise (LoadError (a,"too many result elements"))

        in
        let edit = load_option "edited" a in
1111
        let edit = match edit with None | Some "" -> None | _ -> edit in
François Bobot's avatar
François Bobot committed
1112
        let obsolete = bool_attribute "obsolete" a true in
1113
        let archived = bool_attribute "archived" a false in
François Bobot's avatar
François Bobot committed
1114
1115
        let timelimit = int_attribute_def "timelimit" a 2 in
        let memlimit = int_attribute_def "memlimit" a 0 in
1116
(*
François Bobot's avatar
François Bobot committed
1117
        if timelimit < 0 then begin
1118
            eprintf "[Error] incorrect or unspecified  timelimit '%i'@."
François Bobot's avatar
François Bobot committed
1119
1120
1121
1122
              timelimit;
            raise (LoadError (a,sprintf "incorrect or unspecified timelimit %i"
              timelimit))
        end;
1123
*)
François Bobot's avatar
François Bobot committed
1124
        let (_ : 'a proof_attempt) =
1125
          add_external_proof ~keygen ~archived ~obsolete
1126
            ~timelimit ~memlimit ~edit mg p res
François Bobot's avatar
François Bobot committed
1127
1128
1129
1130
        in
        ()
    | "transf" ->
        let trname = string_attribute "name" a in
1131
1132
        let expanded = bool_attribute "expanded" a true in
        let mtr = raw_add_transformation ~keygen ~expanded mg trname in
François Bobot's avatar
François Bobot committed
1133
1134
1135
1136
1137
        mtr.transf_goals <-
          List.rev
          (List.fold_left
             (load_goal ~old_provers (Parent_transf mtr))
             [] a.Xml.elements);
François Bobot's avatar
François Bobot committed
1138
        (* already done by raw_add_transformation:
François Bobot's avatar
François Bobot committed
1139
           Hashtbl.add mg.transformations trname mtr *)
François Bobot's avatar
François Bobot committed
1140
        (** The attribute "proved" is required but not read *)
1141
        mtr.transf_verified <- transf_verified mtr
François Bobot's avatar
François Bobot committed
1142
    | "metas" -> load_metas ~old_provers mg a;
François Bobot's avatar
François Bobot committed
1143
1144
1145
1146
1147
1148
    | "label" -> ()
    | s ->
        eprintf
          "[Warning] Session.load_proof_or_transf: unexpected element '%s'@."
          s

François Bobot's avatar
François Bobot committed
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
and load_metas ~old_provers mg a =
  let hts = Hint.create 10 in
  let hls = Hint.create 10 in
  let hpr = Hint.create 10 in
  let idpos, metas_args, goal =
    List.fold_left (fun (idpos, metas, goal) a ->
      match a.Xml.name with
      | "ts_pos" | "ls_pos" | "pr_pos" ->
        let name = string_attribute "name" a in
        let intid = int_attribute "id" a in
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
        let library, qualid =
          List.fold_left (fun (library,qualid) a ->
            match a.Xml.name with
            | "ip_library" -> string_attribute "name" a::library, qualid
            | "ip_qualid" -> library, string_attribute "name" a::qualid
            | _ ->
              raise (LoadError(a,"Unexpected element")))
            ([],[]) a.Xml.elements in
        let pos = { ip_library = List.rev library;
                    ip_theory = string_attribute "ip_theory" a;
                    ip_qualid = List.rev qualid;
François Bobot's avatar
François Bobot committed
1170
1171
1172
1173
                  } in
        let idpos = begin match a.Xml.name with
          | "ts_pos" ->
            let arity = int_attribute "arity" a in
1174
            let tvs = Util.foldi (fun l _ ->
François Bobot's avatar
François Bobot committed
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
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
              (create_tvsymbol (Ident.id_fresh "a"))::l)
              [] 0 arity in
            let ts = Ty.create_tysymbol (Ident.id_fresh name) tvs None in
            Hint.add hts intid ts;
            let idpos_ts = Mts.add ts pos idpos.idpos_ts in
            { idpos with idpos_ts = idpos_ts }
          | "ls_pos" ->
            (** TODO signature? *)
            let ls = Term.create_lsymbol (Ident.id_fresh name) [] None in
            Hint.add hls intid ls;
            let idpos_ls = Mls.add ls pos idpos.idpos_ls in
            { idpos with idpos_ls = idpos_ls }
          | "pr_pos" ->
            let pr = Decl.create_prsymbol (Ident.id_fresh name) in
            Hint.add hpr intid pr;
            let idpos_pr = Mpr.add pr pos idpos.idpos_pr in
            { idpos with idpos_pr = idpos_pr }
          | _ -> assert false
        end in
        (idpos, metas, goal)

      | "meta" -> (idpos, a::metas, goal)
      | "goal"  -> (idpos, metas, a::goal)
      | _ ->
        raise (LoadError(a,"Unexpected element"))
    ) (empty_idpos,[],[]) a.Xml.elements in
  let load_ty a =
    let newtv = Hint.memo 0 (fun _ ->
      Ty.ty_var (create_tvsymbol (Ident.id_fresh "a"))) in
    let rec aux a =
      match a.Xml.name with
      | "ty_var" -> newtv (int_attribute "id" a)
      | "ty_app" ->
        let intid = int_attribute "id" a in
        let ts = Hint.find hts intid in
        Ty.ty_app ts (List.map aux a.Xml.elements)
    | _ ->
      raise (LoadError(a,"Unexpected element")) in
    aux a in
  let load_meta_args a =
    try match a.Xml.name with
    | "meta_arg_ty" -> begin match a.Xml.elements with
      | [ty] -> MAty (load_ty ty)
      | _ -> raise (LoadError (a,"This element must contain only one element"))
    end
    | "meta_arg_str" -> MAstr (string_attribute "val" a)
    | "meta_arg_int" -> MAint (int_attribute "val" a)
    | "meta_arg_ts" ->
      let intid = int_attribute "id" a in
      let ts = Hint.find hts intid in
      MAts ts
    | "meta_arg_ls" ->
      let intid = int_attribute "id" a in
      let ls = Hint.find hls intid in
      MAls ls
    | "meta_arg_pr" ->
      let intid = int_attribute "id" a in
      let pr = Hint.find hpr intid in
      MApr pr
    | _ ->
      raise (LoadError(a,"Unexpected element"))
    with Not_found ->
      raise (LoadError (a,"Unknown id"))
  in
  let load_meta metas_args a =
    let args = List.map load_meta_args a.Xml.elements in
    Mstr.change (function
    | None -> Some (Smeta_args.singleton args)
    | Some s -> Some (Smeta_args.add args s))
      (string_attribute "name" a)
      metas_args
  in
  let metas_args =
    List.fold_left load_meta Mstr.empty metas_args in
1249
1250
  let expanded = bool_attribute "expanded" a true in
  let metas = raw_add_metas ~keygen ~expanded mg metas_args idpos in
François Bobot's avatar
François Bobot committed
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
  let goal = match goal with
    | [] -> raise (LoadError (a,"No subgoal for this metas"))
    | [goal] -> goal
    | _ ->
      raise (LoadError (a,"Only one goal can appear in a metas element")) in
  metas.metas_goal <-
    List.hd (load_goal ~old_provers (Parent_metas metas) [] goal);
  (* already done by raw_add_transformation:
     Hashtbl.add mg.transformations trname mtr *)
  (** The attribute "proved" is required but not read *)
  metas.metas_verified <- metas_verified metas



François Bobot's avatar
François Bobot committed
1265
1266
1267
1268
let load_theory ~old_provers mf acc th =
  match th.Xml.name with
    | "theory" ->
        let thname = load_ident th in
1269
1270
        let expanded = bool_attribute "expanded" th true in
        let mth = raw_add_theory ~keygen ~expanded mf thname in
François Bobot's avatar
François Bobot committed
1271
1272
1273
1274
1275
        mth.theory_goals <-
          List.rev
          (List.fold_left
             (load_goal ~old_provers (Parent_theory mth))
             [] th.Xml.elements);
1276
        mth.theory_verified <- theory_verified mth;
François Bobot's avatar
François Bobot committed
1277
1278
1279
1280
1281
1282
1283
1284
1285
        mth::acc
    | s ->
        eprintf "[Warning] Session.load_theory: unexpected element '%s'@." s;
        acc

let load_file session old_provers f =
  match f.Xml.name with
    | "file" ->
        let fn = string_attribute "name" f in
1286
        let fmt = load_option "format" f in
1287
1288
        let expanded = bool_attribute "expanded" f true in
        let mf = raw_add_file ~keygen ~expanded session fn fmt in
François Bobot's avatar
François Bobot committed
1289
1290
1291
1292
        mf.file_theories <-
          List.rev
          (List.fold_left
             (load_theory ~old_provers mf) [] f.Xml.elements);