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

module Hprover = Whyconf.Hprover

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

type transID = int
type proofNodeID = int

Clément Fumex's avatar
Clément Fumex committed
12 13 14 15 16 17 18
type theory = {
  theory_name     : Ident.ident;
  theory_checksum : Termcode.checksum option;
  theory_goals    : proofNodeID list;
}

type proof_parent = Trans of transID | Theory of theory
Clément Fumex's avatar
Clément Fumex committed
19 20 21 22 23 24

type proof_attempt = {
  prover         : Whyconf.prover;
  timelimit      : int;
  memlimit       : int;
  stepslimit     : int;
Clément Fumex's avatar
Clément Fumex committed
25 26
  proof_state    : Call_provers.prover_result option;
  (* None means that the call was not done or never returned *)
Clément Fumex's avatar
Clément Fumex committed
27 28 29 30 31 32 33 34 35 36
  proof_obsolete : bool;
  proof_script   : string option;  (* non empty for external ITP *)
}

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

type proof_node = {
Clément Fumex's avatar
Clément Fumex committed
37
  proofn_name                    : Ident.ident;
Clément Fumex's avatar
Clément Fumex committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
  proofn_task                    : Task.task;
  proofn_parent                  : proof_parent;
  proofn_attempts                : proof_attempt_node Hprover.t;
  mutable proofn_transformations : transID list;
}

type trans_arg =
  | TAint      of int
  | TAstring   of string
  | TAterm     of Term.term
  | TAty       of Ty.ty
  | TAtysymbol of Ty.tysymbol
  (* | ... *)

type transformation_node = {
  transf_name     : string;
  transf_args     : trans_arg list;
  transf_subtasks : proofNodeID list;
  transf_parent   : proofNodeID;
}

type file = {
  file_name     : string;
  file_format   : string option;
  file_theories : theory list;
}

Clément Fumex's avatar
Clément Fumex committed
65 66
 type session = {
  proofNode_table                    : proof_node Hint.t;
Clément Fumex's avatar
Clément Fumex committed
67 68 69 70 71 72 73
  mutable next_proofNodeID      : int;
  trans_table                   : transformation_node Hint.t;
  mutable next_transID          : int;
  session_files                 : file Hstr.t;
  mutable session_shape_version : int;
  session_prover_ids            : int Hprover.t;
  session_file_name             : string;
Clément Fumex's avatar
Clément Fumex committed
74
}
Clément Fumex's avatar
Clément Fumex committed
75

MARCHE Claude's avatar
MARCHE Claude committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
type tree =
  Tree of
    (int * string * int * (int * string * int * tree list) list)

let rec get_goal s id : tree =
  let t = Hint.find s.proofNode_table id in
  let parent = match t.proofn_parent with
    | Theory _ -> -1
    | Trans n -> n
  in
  let trl = List.map (get_trans s) t.proofn_transformations in
  Tree (id,t.proofn_name.Ident.id_string,parent,trl)

and get_trans s id =
  let tr = Hint.find s.trans_table id in
  (id,tr.transf_name,tr.transf_parent,List.map (get_goal s) tr.transf_subtasks)

let get_tree s =
  Hstr.fold
    (fun fn f acc ->
     let c =
       List.map
         (fun th ->
          let goals = List.map (get_goal s) th.theory_goals in
          (th.theory_name.Ident.id_string,goals))
         f.file_theories
     in
     (fn,c) :: acc)
    s.session_files []

Clément Fumex's avatar
Clément Fumex committed
106 107 108 109 110 111 112 113 114 115
let gen_transID (s : session) =
  let id = s.next_transID in
  s.next_transID <- id + 1;
  id

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

Clément Fumex's avatar
Clément Fumex committed
116 117 118 119 120 121 122 123 124 125 126
exception BadID
let get_proofNode (s : session) (id : proofNodeID) =
  try
    Hint.find s.proofNode_table id
  with Not_found -> raise BadID

let get_transfNode (s : session) (id : transID) =
  try
    Hint.find s.trans_table id
  with Not_found -> raise BadID

Clément Fumex's avatar
Clément Fumex committed
127 128 129 130 131
let empty_session ?shape_version (file : string) =
  let shape_version = match shape_version with
    | Some v -> v
    | None -> Termcode.current_shape_version
  in
Clément Fumex's avatar
Clément Fumex committed
132
  { proofNode_table = Hint.create 97;
Clément Fumex's avatar
Clément Fumex committed
133 134 135 136 137 138 139 140 141
    next_proofNodeID = 0;
    trans_table = Hint.create 97;
    next_transID = 0;
    session_files = Hstr.create 3;
    session_shape_version = shape_version;
    session_prover_ids = Hprover.create 7;
    session_file_name = file;
  }

MARCHE Claude's avatar
MARCHE Claude committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
let add_file_section (s:session) (fn:string) ?format (ths:Theory.theory list): unit =
  let theories = []
(*
    List.rev_map
      (fun (_,thname,th) ->
       let tasks =
         List.rev_map
           (fun t -> t)
           (Task.split_theory th None None)
       in
       { theory_name     = thname;
         theory_checksum = None;
         theory_goals    = tasks }) ths
 *)
  in
  let f = { file_name = fn;
            file_format = format;
            file_theories = List.rev theories }
  in
  Hstr.add s.session_files fn f

exception BadID

Clément Fumex's avatar
Clément Fumex committed
165
let graft_proof_attempt (s : session) (id : proofNodeID) (pa : proof_attempt) =
Clément Fumex's avatar
Clément Fumex committed
166 167 168 169 170 171 172 173 174
  let pn = get_proofNode s id in
  let node = { proofa_parent = id; proofa_attempt = pa } in
  Hprover.replace pn.proofn_attempts pa.prover node

let remove_proof_attempt (s : session) (id : proofNodeID)
    (prover : Whyconf.prover) =
  let pn = get_proofNode s id in
  Hprover.remove pn.proofn_attempts prover

Clément Fumex's avatar
Clément Fumex committed
175
(* [mk_proof_node s n t p id] register in the session [s] a proof node
Clément Fumex's avatar
Clément Fumex committed
176
   of proofNodeID [id] of parent [p] of task [t] *)
Clément Fumex's avatar
Clément Fumex committed
177 178 179 180 181
let mk_proof_node (s : session) (n : Ident.ident) (t : Task.task)
    (parent : proof_parent) (node_id : proofNodeID) =
  let pn = { proofn_name = n;
             proofn_task = t;
             proofn_parent = parent;
Clément Fumex's avatar
Clément Fumex committed
182 183 184
             proofn_attempts = Hprover.create 7;
             proofn_transformations = []} in
  Hint.add s.proofNode_table node_id pn
Clément Fumex's avatar
Clément Fumex committed
185

Clément Fumex's avatar
Clément Fumex committed
186 187 188 189 190 191 192 193 194 195 196
let mk_proof_node_no_task (s : session) (n : Ident.ident)
    (parent : proof_parent) (node_id : proofNodeID) =
  mk_proof_node s n None parent node_id

let mk_proof_node_task (s : session) (t : Task.task)
    (parent : proof_parent) (node_id : proofNodeID) =
  let name,_,_ = Termcode.goal_expl_task ~root:false t in
  mk_proof_node s name t parent node_id

let mk_transf_proof_node (s : session) (tid : int)
    (t : Task.task) =
Clément Fumex's avatar
Clément Fumex committed
197
  let id = gen_proofNodeID s in
Clément Fumex's avatar
Clément Fumex committed
198
  mk_proof_node_task s t (Trans tid) id;
Clément Fumex's avatar
Clément Fumex committed
199 200
  id

Clément Fumex's avatar
Clément Fumex committed
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
let mk_transf_node (s : session) (id : proofNodeID) (node_id : transID)
    (name : string) (args : trans_arg list) (pnl : proofNodeID list) =
  let pn = get_proofNode s id in
  let tn = { transf_name = name;
             transf_args = args;
             transf_subtasks = pnl;
             transf_parent = id; } in
  Hint.add s.trans_table node_id tn;
  pn.proofn_transformations <- node_id::pn.proofn_transformations

let graft_transf  (s : session) (id : proofNodeID) (name : string)
    (args : trans_arg list) (tl : Task.task list) =
  let tid = gen_transID s in
  let sub_tasks = List.map (mk_transf_proof_node s tid) tl in
  mk_transf_node s id tid name args sub_tasks

let remove_transformation (s : session) (id : transID) =
  let nt = get_transfNode s id in
  Hint.remove s.trans_table id;
  let pn = get_proofNode s nt.transf_parent in
  let trans_up = List.filter (fun tid -> tid != id) pn.proofn_transformations in
  pn.proofn_transformations <- trans_up;
Clément Fumex's avatar
Clément Fumex committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246

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

let db_filename = "why3session.xml"
let shape_filename = "why3shapes"
let compressed_shape_filename = "why3shapes.gz"
let session_dir_for_save = ref "."

let save_string = Pp.html_string

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

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

let bool_attribute field r def =
  try
    match List.assoc field r.Xml.attributes with
Clément Fumex's avatar
Clément Fumex committed
247 248 249
    | "true" -> true
    | "false" -> false
    | _ -> assert false
Clément Fumex's avatar
Clément Fumex committed
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
  with Not_found -> def

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

let 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

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

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

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

let load_result r =
  match r.Xml.name with
Clément Fumex's avatar
Clément Fumex committed
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
  | "result" ->
    let status = string_attribute "status" r in
    let answer =
      match status with
      | "valid" -> Call_provers.Valid
      | "invalid" -> Call_provers.Invalid
      | "unknown" -> Call_provers.Unknown ("", None)
      | "timeout" -> Call_provers.Timeout
      | "outofmemory" -> Call_provers.OutOfMemory
      | "failure" -> Call_provers.Failure ""
      | "highfailure" -> Call_provers.HighFailure
      | "steplimitexceeded" -> Call_provers.StepLimitExceeded
      | "stepslimitexceeded" -> Call_provers.StepLimitExceeded
      | s ->
        Warning.emit
          "[Warning] Session.load_result: unexpected status '%s'@." s;
        Call_provers.HighFailure
    in
    let time =
      try float_of_string (List.assoc "time" r.Xml.attributes)
      with Not_found -> 0.0
    in
    let steps =
      try int_of_string (List.assoc "steps" r.Xml.attributes)
      with Not_found -> -1
    in
    Some {
      Call_provers.pr_answer = answer;
      Call_provers.pr_time = time;
      Call_provers.pr_output = "";
      Call_provers.pr_status = Unix.WEXITED 0;
      Call_provers.pr_steps = steps;
      Call_provers.pr_model = Model_parser.default_model;
    }
  | "undone" -> None
  | "unedited" -> None
  | s ->
    Warning.emit "[Warning] Session.load_result: unexpected element '%s'@."
      s;
    None
Clément Fumex's avatar
Clément Fumex committed
326 327 328 329 330 331 332 333

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

let load_ident elt =
  let name = string_attribute "name" elt in
  let label = List.fold_left
Clément Fumex's avatar
Clément Fumex committed
334 335 336 337 338 339 340
      (fun acc label ->
         match label with
         | {Xml.name = "label"} ->
           let lab = string_attribute "name" label in
           Ident.Slab.add (Ident.create_label lab) acc
         | _ -> acc
      ) Ident.Slab.empty elt.Xml.elements in
Clément Fumex's avatar
Clément Fumex committed
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
  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

type load_ctxt = {
  old_provers : (Whyconf.prover * int * int * int) Mint.t ;
}

let read_file_session_and_shapes dir xml_filename =
  try
Clément Fumex's avatar
Clément Fumex committed
360 361 362 363 364 365 366 367
    let compressed_shape_filename =
      Filename.concat dir compressed_shape_filename
    in
    if Sys.file_exists compressed_shape_filename then
      (*    if Compress.compression_supported then
            Session.ReadShapesCompress.read_xml_and_shapes
             xml_filename compressed_shape_filename
            else *)
Clément Fumex's avatar
Clément Fumex committed
368 369
      begin
        Warning.emit "[Warning] could not read goal shapes because \
Clément Fumex's avatar
Clément Fumex committed
370
                      Why3 was not compiled with compress support@.";
Clément Fumex's avatar
Clément Fumex committed
371 372
        Xml.from_file xml_filename, false
      end
Clément Fumex's avatar
Clément Fumex committed
373 374 375 376 377
    else
      let shape_filename = Filename.concat dir shape_filename in
      (*    if Sys.file_exists shape_filename then
            ReadShapesNoCompress.read_xml_and_shapes xml_filename shape_filename
            else*)
Clément Fumex's avatar
Clément Fumex committed
378 379 380 381
      begin
        Warning.emit "[Warning] could not find goal shapes file@.";
        Xml.from_file xml_filename, false
      end
Clément Fumex's avatar
Clément Fumex committed
382 383 384 385 386 387 388 389 390
  with e ->
    Warning.emit "[Warning] failed to read goal shapes: %s@."
      (Printexc.to_string e);
    Xml.from_file xml_filename, false

(* [load_goal s op p g id] loads the goal of parent [p] from the xml
   [g] of nodeID [id] into the session [s] *)
let rec load_goal session old_provers parent g id =
  match g.Xml.name with
Clément Fumex's avatar
Clément Fumex committed
391 392 393
  | "goal" ->
    let gname = load_ident g in
    mk_proof_node_no_task session gname parent id;
Clément Fumex's avatar
Clément Fumex committed
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
    List.iter (load_proof_or_transf session old_provers id) g.Xml.elements;
  | "label" -> ()
  | s ->
    Warning.emit "[Warning] Session.load_goal: unexpected element '%s'@." s

(* [load_proof_or_transf s op id a] load either a proof attempt or a
   transformation of parent id [pid] from the xml [a] into the session
   [s] *)
and load_proof_or_transf session old_provers pid a =
  match a.Xml.name with
    | "proof" ->
      begin
        let prover = string_attribute "prover" a in
        try
          let prover = int_of_string prover in
          let (p,timelimit,steplimit,memlimit) = Mint.find prover old_provers in
          let res = match a.Xml.elements with
            | [r] -> load_result r
            | [] -> None
            | _ ->
              Warning.emit "[Error] Too many result elements@.";
              raise (LoadError (a,"too many result elements"))
          in
          let edit = load_option "edited" a in
          let edit = match edit with None | Some "" -> None | _ -> edit in
          let obsolete = bool_attribute "obsolete" a false in
          let timelimit = int_attribute_def "timelimit" a timelimit in
	  let steplimit = int_attribute_def "steplimit" a steplimit in
          let memlimit = int_attribute_def "memlimit" a memlimit in
          let pa = { prover = p;
                     timelimit = timelimit;
                     memlimit = memlimit;
                     stepslimit = steplimit;
                     proof_state = res;
                     proof_obsolete = obsolete;
                     proof_script = edit;
                   } in
          graft_proof_attempt session pid pa
        with Failure _ | Not_found ->
          Warning.emit "[Error] prover id not listed in header '%s'@." prover;
          raise (LoadError (a,"prover not listing in header"))
      end
    | "transf" ->
        let trname = string_attribute "name" a in
        let tid = gen_transID session in
        let subtasks = List.fold_left (fun goals th -> match th.Xml.name with
        | "goal" -> (gen_proofNodeID session) :: goals
        | _ -> goals) [] a.Xml.elements in
        mk_transf_node session pid tid trname [] subtasks;
        List.iter2
          (load_goal session old_provers (Trans tid))
          a.Xml.elements subtasks;
    | "metas" -> ()
    | "label" -> ()
Clément Fumex's avatar
Clément Fumex committed
448
    | s ->
Clément Fumex's avatar
Clément Fumex committed
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
        Warning.emit
          "[Warning] Session.load_proof_or_transf: unexpected element '%s'@."
          s

let load_theory session old_provers acc th =
  match th.Xml.name with
  | "theory" ->
    let thname = load_ident th in
    let csum = string_attribute_opt "sum" th in
    let checksum = Opt.map Termcode.checksum_of_string csum in
    let goals = List.fold_left (fun goals th -> match th.Xml.name with
        | "goal" -> (gen_proofNodeID session) :: goals
        | _ -> goals) [] th.Xml.elements in
    let mth = { theory_name = thname;
                theory_checksum = checksum;
                theory_goals = goals; } in
    List.iter2
      (load_goal session old_provers (Theory mth))
      th.Xml.elements goals;
    mth::acc
  | s ->
    Warning.emit "[Warning] Session.load_theory: unexpected element '%s'@."
      s;
    acc

let load_file session old_provers f = (* old_provers *)
  match f.Xml.name with
  | "file" ->
    let fn = string_attribute "name" f in
    let fmt = load_option "format" f in
    let ft = List.rev
        (List.fold_left
           (load_theory session old_provers) [] f.Xml.elements) in
    let mf = { file_name = fn;
               file_format = fmt;
               file_theories = ft; } in
    Hstr.add session.session_files fn mf;
    old_provers
  | "prover" ->
    (* The id is just for the session file *)
    let id = string_attribute "id" f in
    begin
      try
        let id = int_of_string id in
        let name = string_attribute "name" f in
        let version = string_attribute "version" f in
        let altern = string_attribute_def "alternative" f "" in
        let timelimit = int_attribute_def "timelimit" f 5 in
        let steplimit = int_attribute_def "steplimit" f 1 in
        let memlimit = int_attribute_def "memlimit" f 1000 in
        let p = {Whyconf.prover_name = name;
                 prover_version = version;
                 prover_altern = altern} in
        Mint.add id (p,timelimit,steplimit,memlimit) old_provers
      with Failure _ ->
        Warning.emit "[Warning] Session.load_file: unexpected non-numeric prover id '%s'@." id;
Clément Fumex's avatar
Clément Fumex committed
505
        old_provers
Clément Fumex's avatar
Clément Fumex committed
506 507 508 509
    end
  | s ->
    Warning.emit "[Warning] Session.load_file: unexpected element '%s'@." s;
    old_provers
Clément Fumex's avatar
Clément Fumex committed
510 511 512

let build_session (s : session) xml =
  match xml.Xml.name with
Clément Fumex's avatar
Clément Fumex committed
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
  | "why3session" ->
    let shape_version = int_attribute_def "shape_version" xml 1 in
    s.session_shape_version <- shape_version;
    Debug.dprintf debug "[Info] load_session: shape version is %d@\n" shape_version;
    (* just to keep the old_provers somewhere *)
    let old_provers =
      List.fold_left (load_file s) Mint.empty xml.Xml.elements
    in
    Mint.iter
      (fun id (p,_,_,_) ->
         Debug.dprintf debug "prover %d: %a@." id Whyconf.print_prover p;
         Hprover.replace s.session_prover_ids p id)
      old_provers;
    Debug.dprintf debug "[Info] load_session: done@\n"
  | s ->
    Warning.emit "[Warning] Session.load_session: unexpected element '%s'@."
      s
Clément Fumex's avatar
Clément Fumex committed
530 531 532 533

let load_session (file : string) =
  let session = empty_session file in
  let use_shapes =
Clément Fumex's avatar
Clément Fumex committed
534
    (* If the xml is present we read it, otherwise we consider it empty *)
Clément Fumex's avatar
Clément Fumex committed
535 536 537 538 539 540 541 542 543 544 545
    if Sys.file_exists file then
      try
        Termcode.reset_dict ();
        let dir = Filename.dirname file in
        let xml,use_shapes = read_file_session_and_shapes dir file in
        try
          build_session session xml.Xml.content;
          use_shapes
        with Sys_error msg ->
          failwith ("Open session: sys error " ^ msg)
      with
Clément Fumex's avatar
Clément Fumex committed
546
      | Sys_error msg ->
Clément Fumex's avatar
Clément Fumex committed
547
        (* xml does not exist yet *)
Clément Fumex's avatar
Clément Fumex committed
548 549 550 551 552
        raise (SessionFileError msg)
      | Xml.Parse_error s ->
        Warning.emit "XML database corrupted, ignored (%s)@." s;
        raise (SessionFileError "XML corrupted")
    else false
Clément Fumex's avatar
Clément Fumex committed
553 554
  in
  session, use_shapes