session.ml 75.8 KB
Newer Older
Andrei Paskevich's avatar
Andrei Paskevich committed
1
2
3
4
5
6
7
8
9
10
(********************************************************************)
(*                                                                  *)
(*  The Why3 Verification Platform   /   The Why3 Development Team  *)
(*  Copyright 2010-2012   --   INRIA - CNRS - Paris-Sud University  *)
(*                                                                  *)
(*  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"
28
  ~desc:"About the why3 session creation, reading and writing."
François Bobot's avatar
François Bobot committed
29
30
31

(** {2 Type definitions} *)

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

34
35
36
37
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
38
39
40
41
42
43
44
    | 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

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

51
52
53
54
55
56
57
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 =
58
  let c = Lists.compare String.compare x.ip_library y.ip_library in
François Bobot's avatar
François Bobot committed
59
  if c <> 0 then -c else (* in order to be bottom up *)
60
  let c = String.compare x.ip_theory y.ip_theory in
François Bobot's avatar
François Bobot committed
61
  if c <> 0 then c else
62
  let c = Lists.compare String.compare x.ip_qualid y.ip_qualid in
63
  c
François Bobot's avatar
François Bobot committed
64
65

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

module Mpos = Map.Make(Pos)
module Spos = Mpos.Set
Andrei Paskevich's avatar
Andrei Paskevich committed
74
module Hpos = XHashtbl.Make(Pos)
François Bobot's avatar
François Bobot committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99

type meta_args = meta_arg list

module Mmeta_args = Map.Make(struct
  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)

100
  let compare = Lists.compare compare_meta_arg
François Bobot's avatar
François Bobot committed
101
102
103
end)
module Smeta_args = Mmeta_args.Set

Andrei Paskevich's avatar
Andrei Paskevich committed
104
105
type metas_args = Smeta_args.t Mstr.t

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

type idpos = {
112
113
114
  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
115
116
117
118
119
120
121
122
123
}

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

124
(* dead code
François Bobot's avatar
François Bobot committed
125
126
127
128
129
130
131
132
133
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
134
*)
François Bobot's avatar
François Bobot committed
135

François Bobot's avatar
François Bobot committed
136
type 'a goal =
137
138
139
140
141
142
143
144
145
146
147
  { mutable goal_key  : 'a;
    goal_name : Ident.ident;
    goal_expl : string option;
    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
148
    mutable goal_metas : 'a metas Mmetas_args.t;
149
  }
François Bobot's avatar
François Bobot committed
150
151

and 'a proof_attempt =
152
153
154
155
156
157
158
159
160
161
  { 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
162
163
164
165

and 'a goal_parent =
  | Parent_theory of 'a theory
  | Parent_transf of 'a transf
François Bobot's avatar
François Bobot committed
166
167
168
169
170
171
172
173
174
175
176
177
  | 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201

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;
202
      file_format : string option;
François Bobot's avatar
François Bobot committed
203
204
205
206
207
208
209
210
211
      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
212
      mutable session_shape_version : int;
François Bobot's avatar
François Bobot committed
213
214
215
216
217
218
219
      session_dir   : string; (** Absolute path *)
    }

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

220
type loaded_provers = loaded_prover option PHprover.t
François Bobot's avatar
François Bobot committed
221
222
223

type 'a env_session =
    { env : Env.env;
224
      mutable whyconf : Whyconf.config;
François Bobot's avatar
François Bobot committed
225
226
227
      loaded_provers : loaded_provers;
      session : 'a session}

228
let update_env_session_config e c = e.whyconf <- c
François Bobot's avatar
François Bobot committed
229
230
231
232
233
234
235
236
237
238

(*************************)
(**       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
239
  | Metas of 'a metas
François Bobot's avatar
François Bobot committed
240
241
242

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
243
244
  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
245
246
247
248

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
249
250
251
and metas_iter_proof_attempt f t =
  goal_iter_proof_attempt f t.metas_goal

François Bobot's avatar
François Bobot committed
252
253
254
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
255
256
257
let metas_iter_proof_attempt f m =
  goal_iter_proof_attempt f m.metas_goal

François Bobot's avatar
François Bobot committed
258
259
260
261
262
263
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

264
let iter_proof_attempt f = function
François Bobot's avatar
François Bobot committed
265
266
267
268
269
    | 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
270
    | Metas m -> metas_iter_proof_attempt f m
François Bobot's avatar
François Bobot committed
271
272
273
274
275
276
277
278
279
280
281

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
282
let iter_goal fp ft fm g =
François Bobot's avatar
François Bobot committed
283
  PHprover.iter (fun _ a -> fp a) g.goal_external_proofs;
François Bobot's avatar
François Bobot committed
284
285
  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
286
287
288
289

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

François Bobot's avatar
François Bobot committed
290
291
let iter_metas f t = f t.metas_goal

François Bobot's avatar
François Bobot committed
292
let goal_iter f g =
293
294
  PHprover.iter
    (fun _ a -> f (Proof_attempt a)) g.goal_external_proofs;
François Bobot's avatar
François Bobot committed
295
296
297
  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
298
299
300
301

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

François Bobot's avatar
François Bobot committed
302
303
304
let metas_iter f t =
  f (Goal t.metas_goal)

François Bobot's avatar
François Bobot committed
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
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
320
    | Metas m -> metas_iter f m
François Bobot's avatar
François Bobot committed
321
322
323
324
325
326
327
328
329

(** 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
330
331
332
333
334
335
336
337
        | 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
338
        | Goal g ->
339
340
341
          if g.goal_verified
          then g.goal_name.Ident.id_string
          else g.goal_name.Ident.id_string^"?"
342
        | Proof_attempt pr ->
343
          Pp.sprintf_wnl "%a%s%s%s%s"
344
            Whyconf.print_prover pr.proof_prover
345
346
347
348
            (match pr.proof_state with
              | Done { Call_provers.pr_answer = Call_provers.Valid} -> ""
              | InternalFailure _ -> "!"
              | _ -> "?")
349
350
351
            (if pr.proof_obsolete || pr.proof_archived then " " else "")
            (if pr.proof_obsolete then "O" else "")
            (if pr.proof_archived then "A" else "")
352
353
354
        | Transf tr ->
          if tr.transf_verified
          then tr.transf_name
François Bobot's avatar
François Bobot committed
355
356
357
358
359
360
          else tr.transf_name^"?"
        | Metas metas ->
          if metas.metas_verified
          then "metas..."
          else "metas..."^"?"
      in
François Bobot's avatar
François Bobot committed
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
      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
379
380
381
382
383
384
385
386
387
388
389
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
390
391
392
393
394
395
396
  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
397
  empty_session ?shape_version project_dir
François Bobot's avatar
François Bobot committed
398
399


400
(* dead code
François Bobot's avatar
François Bobot committed
401
402
403
404
405
406
407
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;
408
    loaded_provers = PHprover.create 5;
François Bobot's avatar
François Bobot committed
409
  }
410
*)
François Bobot's avatar
François Bobot committed
411
412
413
414
415

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

416
(* dead code
François Bobot's avatar
François Bobot committed
417
418
419
420
421
422
423
424
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
425
    | Parent_metas  metas -> get_session_metas metas
François Bobot's avatar
François Bobot committed
426

427

François Bobot's avatar
François Bobot committed
428
429
and get_session_trans transf = get_session_goal transf.transf_parent

François Bobot's avatar
François Bobot committed
430
431
and get_session_metas metas = get_session_goal metas.metas_parent

François Bobot's avatar
François Bobot committed
432
let get_session_proof_attempt pa = get_session_goal pa.proof_parent
433
*)
François Bobot's avatar
François Bobot committed
434

435
let get_used_provers session =
François Bobot's avatar
François Bobot committed
436
437
438
439
440
441
442
443
  let sprover = ref Sprover.empty in
  session_iter_proof_attempt
    (fun pa -> sprover := Sprover.add pa.proof_prover !sprover)
    session;
  !sprover

exception NoTask

444
let goal_task g = Opt.get_exn NoTask g.goal_task
François Bobot's avatar
François Bobot committed
445
446
let goal_task_option g = g.goal_task

447
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
448
449
450
451
452
453
454

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

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

MARCHE Claude's avatar
MARCHE Claude committed
457
458
459
460
461
462
463
464
465
466
467
468
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
469
470
471
472
473
474
475
476
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"
477
       | Call_provers.OutOfMemory -> "outofmemory"
François Bobot's avatar
François Bobot committed
478
479
480
481
482
       | Call_provers.Invalid -> "invalid")
    r.Call_provers.pr_time

let save_status fmt s =
  match s with
483
    | Unedited ->
484
        fprintf fmt "@\n<unedited/>"
485
    | Scheduled | Running | Interrupted | JustEdited ->
486
        fprintf fmt "@\n<undone/>"
François Bobot's avatar
François Bobot committed
487
    | InternalFailure msg ->
488
        fprintf fmt "@\n<internalfailure reason=\"%a\"/>"
MARCHE Claude's avatar
MARCHE Claude committed
489
          save_string (Printexc.to_string msg)
François Bobot's avatar
François Bobot committed
490
491
492
493
494
    | Done r -> save_result fmt r


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

497
let save_proof_attempt fmt (id,a) =
François Bobot's avatar
François Bobot committed
498
  fprintf fmt
499
500
    "@\n@[<v 1><proof@ prover=\"%i\"@ timelimit=\"%d\"@ \
memlimit=\"%d\"@ %aobsolete=\"%b\"@ archived=\"%b\">"
501
    id a.proof_timelimit a.proof_memlimit
502
503
    (opt "edited") a.proof_edited_as a.proof_obsolete
    a.proof_archived;
François Bobot's avatar
François Bobot committed
504
505
506
507
  save_status fmt a.proof_state;
  fprintf fmt "@]@\n</proof>"

let save_ident fmt id =
MARCHE Claude's avatar
MARCHE Claude committed
508
  fprintf fmt "name=\"%a\"" save_string id.Ident.id_string;
François Bobot's avatar
François Bobot committed
509
510
511
512
  match id.Ident.id_loc with
    | None -> ()
    | Some loc ->
      let file,lnum,cnumb,cnume = Loc.get loc in
513
      let file = Sysutil.relativize_filename !session_dir_for_save file in
François Bobot's avatar
François Bobot committed
514
      fprintf fmt
MARCHE Claude's avatar
MARCHE Claude committed
515
516
        "@ locfile=\"%a\"@ loclnum=\"%i\" loccnumb=\"%i\" loccnume=\"%i\""
        save_string file lnum cnumb cnume
François Bobot's avatar
François Bobot committed
517
518

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

François Bobot's avatar
François Bobot committed
521
let rec save_goal provers fmt g =
522
  assert (Tc.string_of_shape g.goal_shape <> "");
François Bobot's avatar
François Bobot committed
523
  fprintf fmt
MARCHE Claude's avatar
MARCHE Claude committed
524
    "@\n@[<v 1><goal@ %a@ %asum=\"%a\"@ proved=\"%b\"@ \
525
expanded=\"%b\"@ shape=\"%a\">"
François Bobot's avatar
François Bobot committed
526
527
    save_ident g.goal_name
    (opt "expl") g.goal_expl
528
529
530
    save_string (Tc.string_of_checksum g.goal_checksum)
    g.goal_verified  g.goal_expanded
    save_string (Tc.string_of_shape g.goal_shape);
531
  Ident.Slab.iter (save_label fmt) g.goal_name.Ident.id_label;
532
533
534
535
536
537
538
539
  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
540
  Mmetas_args.iter (save_metas provers fmt) g.goal_metas;
François Bobot's avatar
François Bobot committed
541
542
  fprintf fmt "@]@\n</goal>"

543
and save_trans provers fmt t =
MARCHE Claude's avatar
MARCHE Claude committed
544
545
  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
546
547
548
  List.iter (save_goal provers fmt) t.transf_goals;
  fprintf fmt "@]@\n</transf>"

François Bobot's avatar
François Bobot committed
549
550
551
552
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 =
553
554
555
556
557
558
559
    fprintf fmt "@\n@[<v 1>ip_theory=\"%a\">" save_string pos.ip_theory;
    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;
    fprintf fmt "@]";
  in
François Bobot's avatar
François Bobot committed
560
561
  let save_ts_pos fmt ts pos =
    fprintf fmt "@\n@[<v 1><ts_pos@ name=\"%a\"@ arity=\"%i\"@ \
562
    id=\"%i\"@ %a</ts_pos>@]"
François Bobot's avatar
François Bobot committed
563
564
565
566
      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? *)
567
    fprintf fmt "@\n@[<v 1><ls_pos@ name=\"%a\"@ id=\"%i\"@ %a</ls_pos>@]"
François Bobot's avatar
François Bobot committed
568
      save_string ls.ls_name.id_string
569
      (ls_hash ls) save_pos pos
François Bobot's avatar
François Bobot committed
570
571
  in
  let save_pr_pos fmt pr pos =
572
    fprintf fmt "@\n@[<v 1><pr_pos@ name=\"%a\"@ id=\"%i\"@ %a</pr_pos>@]"
François Bobot's avatar
François Bobot committed
573
      save_string pr.pr_name.id_string
574
      (pr_hash pr) save_pos pos
François Bobot's avatar
François Bobot committed
575
576
577
578
579
580
581
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
609
610
611
612
  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;
  fprintf fmt "@]@\n</meta_args>"

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
613
614
615
616
617
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;
618
  Ident.Slab.iter (save_label fmt) t.theory_name.Ident.id_label;
François Bobot's avatar
François Bobot committed
619
620
621
622
  List.iter (save_goal provers fmt) t.theory_goals;
  fprintf fmt "@]@\n</theory>"

let save_file provers fmt _ f =
623
624
625
626
  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
627
628
629
630
  List.iter (save_theory provers fmt) f.file_theories;
  fprintf fmt "@]@\n</file>"

let save_prover fmt p (provers,id) =
631
  fprintf fmt "@\n@[<v 1><prover@ id=\"%i\"@ \
MARCHE Claude's avatar
MARCHE Claude committed
632
633
name=\"%a\"@ version=\"%a\"%a/>@]"
    id save_string p.C.prover_name save_string p.C.prover_version
634
635
    (fun fmt s -> if s <> "" then fprintf fmt "@ alternative=\"%a\""
        save_string s)
636
    p.C.prover_altern;
François Bobot's avatar
François Bobot committed
637
638
  Mprover.add p id provers, id+1

MARCHE Claude's avatar
MARCHE Claude committed
639
let save fname config session =
François Bobot's avatar
François Bobot committed
640
641
642
  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
643
  fprintf fmt "<!DOCTYPE why3session SYSTEM \"%a\">@\n"
644
645
    save_string (Filename.concat (Whyconf.datadir (Whyconf.get_main config))
                   "why3session.dtd");
646
647
(*
  let rel_file = Sysutil.relativize_filename !session_dir_for_save fname in
MARCHE Claude's avatar
MARCHE Claude committed
648
  fprintf fmt "@[<v 1><why3session@ name=\"%a\" shape_version=\"%d\">"
649
650
651
652
    save_string rel_file session.session_shape_version;
*)
  fprintf fmt "@[<v 1><why3session shape_version=\"%d\">"
    session.session_shape_version;
653
  let provers,_ = Sprover.fold (save_prover fmt) (get_used_provers session)
François Bobot's avatar
François Bobot committed
654
655
656
657
658
659
    (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
660
let save_session config session =
François Bobot's avatar
François Bobot committed
661
662
  let f = Filename.concat session.session_dir db_filename in
  Sysutil.backup_file f;
663
  session_dir_for_save := session.session_dir;
MARCHE Claude's avatar
MARCHE Claude committed
664
  save f config session
François Bobot's avatar
François Bobot committed
665
666
667
668
669
670
671

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

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

672
let check_expl lab acc =
Andrei Paskevich's avatar
Andrei Paskevich committed
673
  let lab = lab.Ident.lab_string in
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
  if Str.string_match expl_regexp lab 0
  then Some (Str.matched_group 1 lab)
  else acc

let check_expl lab = Ident.Slab.fold check_expl lab None

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
692
693
694
695
696

type expl = string option

let get_explanation id task =
  let fmla = Task.task_goal_fmla task in
697
698
  let res = get_expl_fmla fmla in
  if res <> None then res else check_expl id.Ident.id_label
François Bobot's avatar
François Bobot committed
699
700
701
702
703
704
705
706


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

707
708
let file_verified f =
  List.for_all (fun t -> t.theory_verified) f.file_theories
François Bobot's avatar
François Bobot committed
709

710
711
712
713
714
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
715

François Bobot's avatar
François Bobot committed
716
717
let metas_verified m = m.metas_goal.goal_verified

François Bobot's avatar
François Bobot committed
718
719
720
721
722
723
let proof_verified a =
  (not a.proof_obsolete) &&
    match a.proof_state with
      | Done { Call_provers.pr_answer = Call_provers.Valid} -> true
      | _ -> false

724
let goal_verified g =
François Bobot's avatar
François Bobot committed
725
726
727
728
729
730
731
732
733
    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
734
735
736
      Mmetas_args.iter
        (fun _ t -> if t.metas_verified then raise Exit)
        g.goal_metas;
François Bobot's avatar
François Bobot committed
737
738
      false
    with Exit -> true
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756

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
757
758
759
760
761
762
  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
763
      | Parent_metas t -> check_metas_proved notify t
François Bobot's avatar
François Bobot committed
764
765
766
  end

and check_transf_proved notify t =
767
  let b = transf_verified t in
François Bobot's avatar
François Bobot committed
768
769
770
771
772
773
  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
774
775
776
777
778
779
780
781
782

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
783
784
785
786
787
788
789
(******************************)
(* raw additions to the model *)
(******************************)
type 'a keygen = ?parent:'a -> unit -> 'a

let add_external_proof
    ?(notify=notify)
790
    ~(keygen:'a keygen) ~obsolete
791
    ~archived ~timelimit ~memlimit ~edit (g:'a goal) p result =
792
  assert (edit <> Some "");
François Bobot's avatar
François Bobot committed
793
794
795
796
797
  let key = keygen ~parent:g.goal_key () in
  let a = { proof_prover = p;
            proof_parent = g;
            proof_key = key;
            proof_obsolete = obsolete;
798
            proof_archived = archived;
François Bobot's avatar
François Bobot committed
799
800
            proof_state = result;
            proof_timelimit = timelimit;
801
            proof_memlimit = memlimit;
François Bobot's avatar
François Bobot committed
802
803
804
            proof_edited_as = edit;
          }
  in
805
  PHprover.replace g.goal_external_proofs p a;
François Bobot's avatar
François Bobot committed
806
807
808
809
810
811
812
813
814
  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


815
let set_proof_state ?(notify=notify) ~obsolete ~archived res a =
François Bobot's avatar
François Bobot committed
816
817
  a.proof_state <- res;
  a.proof_obsolete <- obsolete;
818
  a.proof_archived <- archived;
François Bobot's avatar
François Bobot committed
819
820
821
  notify (Proof_attempt a);
  check_goal_proved notify a.proof_parent

822
823
824
825
826
827
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
828

François Bobot's avatar
François Bobot committed
829
830
831
let set_edited_as edited_as a = a.proof_edited_as <- edited_as

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

834
835
836
837
838
let set_obsolete ?(notify=notify) a =
  a.proof_obsolete <- true;
  notify (Proof_attempt a);
  check_goal_proved notify a.proof_parent

839
let set_archived a b = a.proof_archived <- b
840

841
let get_edited_as_abs session pr =
842
  Opt.map (Filename.concat session.session_dir) pr.proof_edited_as
843

François Bobot's avatar
François Bobot committed
844
845
846
(* [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
*)
847
let raw_add_no_task ~(keygen:'a keygen) ~(expanded:bool) parent name expl sum shape =
François Bobot's avatar
François Bobot committed
848
849
850
  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
851
    | Parent_metas  mms -> mms.metas_key
François Bobot's avatar
François Bobot committed
852
853
854
855
856
857
858
859
860
861
862
  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
863
               goal_metas = Mmetas_args.empty;
François Bobot's avatar
François Bobot committed
864
               goal_verified = false;
865
               goal_expanded = expanded;
François Bobot's avatar
François Bobot committed
866
867
868
869
             }
  in
  goal

870
let raw_add_task ~version ~(keygen:'a keygen) ~(expanded:bool) parent name expl t =
François Bobot's avatar
François Bobot committed
871
872
873
  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
874
    | Parent_metas  mms -> mms.metas_key
François Bobot's avatar
François Bobot committed
875
876
  in
  let key = keygen ~parent:parent_key () in
877
878
  let sum = Termcode.task_checksum ~version t in
  let shape = Termcode.t_shape_buf ~version
879
    (Task.task_goal_fmla t) in
François Bobot's avatar
François Bobot committed
880
881
882
883
884
885
886
887
888
  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
889
               goal_metas = Mmetas_args.empty;
François Bobot's avatar
François Bobot committed
890
               goal_verified = false;
891
               goal_expanded = expanded;
François Bobot's avatar
François Bobot committed
892
893
894
895
896
897
898
899
             }
  in
  goal


(* [raw_add_transformation g name adds a transformation to the given goal g
   Adds no subgoals, thus this should not be exported
*)
900
let raw_add_transformation ~(keygen:'a keygen) ~(expanded:bool) g name =
François Bobot's avatar
François Bobot committed
901
902
903
904
905
906
907
  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 = [];
908
             transf_expanded = expanded;
François Bobot's avatar
François Bobot committed
909
910
           }
  in
911
  PHstr.replace g.goal_transformations name tr;
François Bobot's avatar
François Bobot committed
912
913
  tr

914
let raw_add_metas ~(keygen:'a keygen) ~(expanded:bool) g added idpos =
François Bobot's avatar
François Bobot committed
915
916
917
918
919
920
921
922
  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;
923
             metas_expanded = expanded;
François Bobot's avatar
François Bobot committed
924
925
926
927
928
           }
  in
  g.goal_metas <- Mmetas_args.add added ms g.goal_metas;
  ms

929
let raw_add_theory ~(keygen:'a keygen) ~(expanded:bool) mfile thname =
François Bobot's avatar
François Bobot committed
930
931
932
933
934
  let parent = mfile.file_key in
  let key = keygen ~parent () in
  let mth = { theory_name = thname;
              theory_key = key;
              theory_parent = mfile;
935
              theory_goals = [];
François Bobot's avatar
François Bobot committed
936
              theory_verified = false;
937
              theory_expanded = expanded;
François Bobot's avatar
François Bobot committed
938
939
940
941
            }
  in
  mth

942
let raw_add_file ~(keygen:'a keygen) ~(expanded:bool) session f fmt =
François Bobot's avatar
François Bobot committed
943
944
945
  let key = keygen () in
  let mfile = { file_name = f;
                file_key = key;
946
947
                file_format = fmt;
                file_theories = [];
François Bobot's avatar
François Bobot committed
948
                file_verified = false;
949
                file_expanded = expanded;
François Bobot's avatar
François Bobot committed
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
                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
972
let int_attribute_def field r def =
François Bobot's avatar
François Bobot committed
973
974
975
976
  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
977
978
979
980
981
982
983
984
985
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
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
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:_ () = ()

For faster browsing, not all history is shown. View entire blame