why3session_html.ml 17 KB
Newer Older
1 2
(**************************************************************************)
(*                                                                        *)
MARCHE Claude's avatar
MARCHE Claude committed
3
(*  Copyright (C) 2010-2012                                               *)
4 5 6
(*    François Bobot                                                      *)
(*    Jean-Christophe Filliâtre                                           *)
(*    Claude Marché                                                       *)
MARCHE Claude's avatar
MARCHE Claude committed
7
(*    Guillaume Melquiond                                                 *)
8 9 10 11 12 13 14 15 16 17 18 19 20
(*    Andrei Paskevich                                                    *)
(*                                                                        *)
(*  This software is free software; you can redistribute it and/or        *)
(*  modify it under the terms of the GNU Library General Public           *)
(*  License version 2.1, with the special exception on linking            *)
(*  described in file LICENSE.                                            *)
(*                                                                        *)
(*  This software is distributed in the hope that it will be useful,      *)
(*  but WITHOUT ANY WARRANTY; without even the implied warranty of        *)
(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                  *)
(*                                                                        *)
(**************************************************************************)

21
open Format
22
open Why3
23
open Why3session_lib
24

25 26
module S = Session

27
let output_dir = ref ""
28
let opt_context = ref false
29

30
type style = SimpleTree | Jstree | Table
31

32 33
let opt_style = ref Table
let default_style = "table"
34 35

let set_opt_style = function
36
  | "simple" -> opt_style := SimpleTree
37
  | "jstree" -> opt_style := Jstree
38 39 40 41 42
  | "table" -> opt_style := Table
  | _ -> 
    eprintf "Unknown html style, ignored@."

let () = set_opt_style default_style
43

44 45 46 47 48 49 50 51 52
let opt_pp = ref []

let set_opt_pp_in,set_opt_pp_cmd,set_opt_pp_out =
  let suf = ref "" in
  let cmd = ref "" in
  (fun s -> suf := s),
  (fun s -> cmd := s),
  (fun s -> opt_pp := (!suf,(!cmd,s))::!opt_pp)

53
let spec =
54 55
  ("-o",
   Arg.Set_string output_dir,
MARCHE Claude's avatar
MARCHE Claude committed
56
   "<path> output directory ('-' for stdout)") ::
57
  ("--context", Arg.Set opt_context,
MARCHE Claude's avatar
MARCHE Claude committed
58
   " adds context around the generated HTML code") ::
59
  ("--style", Arg.Symbol (["simpletree";"jstree";"table"], set_opt_style),
MARCHE Claude's avatar
MARCHE Claude committed
60
   " style to use, defaults to '" ^ default_style ^ "'."
61
) ::
62
  ("--add_pp", Arg.Tuple
63 64 65
    [Arg.String set_opt_pp_in;
     Arg.String set_opt_pp_cmd;
     Arg.String set_opt_pp_out],
MARCHE Claude's avatar
MARCHE Claude committed
66
  "<suffix> <cmd> <out_suffix> declares a pretty-printer for edited proofs") ::
67 68
  ("--coqdoc",
   Arg.Unit (fun ()->
69
    opt_pp := (".v",("coqdoc --no-index --html -o %o %i",".html"))::!opt_pp),
MARCHE Claude's avatar
MARCHE Claude committed
70
  " use coqdoc to print Coq proofs") ::
71
  common_options
72

François Bobot's avatar
François Bobot committed
73
open Session
74 75
open Util

76
type context =
77
    (string ->
François Bobot's avatar
François Bobot committed
78
     (formatter -> notask session -> unit) -> notask session
79 80
     -> unit, formatter, unit) format

81 82 83 84 85 86 87 88 89 90 91
let run_file (context : context) print_session fname =
  let project_dir = Session.get_project_dir fname in
  let session = Session.read_session project_dir in
  let output_dir =
    if !output_dir = "" then project_dir else !output_dir
  in
  let basename = Filename.basename project_dir in
  let cout =
    if output_dir = "-" then stdout else
      open_out (Filename.concat output_dir (basename^".html"))
  in
92 93 94 95 96 97 98 99
  let fmt = formatter_of_out_channel cout in
  if !opt_context
  then fprintf fmt context basename (print_session basename) session
  else print_session basename fmt session;
  pp_print_flush fmt ();
  if output_dir <> "-" then close_out cout


100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
module Table =
struct


  let rec transf_depth tr =
    List.fold_left
      (fun depth g -> max depth (goal_depth g)) 0 tr.S.transf_goals
  and goal_depth g =
    S.PHstr.fold
      (fun _st tr depth -> max depth (1 + transf_depth tr))
      g.S.goal_transformations 1

  let theory_depth t =
    List.fold_left
      (fun depth g -> max depth (goal_depth g)) 0 t.S.theory_goals

  let rec provers_stats provers theory =
    S.theory_iter_proof_attempt (fun a ->
      Hashtbl.replace provers a.S.proof_prover a.S.proof_prover) theory

  let print_prover = Whyconf.print_prover


  let color_of_status ?(dark=false) fmt b = 
    fprintf fmt "%s" (if b then 
        if dark then "008000" else "C0FFC0" 
      else "FF0000")

let print_results fmt provers proofs =
  List.iter (fun p ->
    fprintf fmt "<td bgcolor=\"#";
    begin
      try
        let pr = S.PHprover.find proofs p in
        let s = pr.S.proof_state in
        match s with
	  | S.Done res ->
	    begin
	      match res.Call_provers.pr_answer with
		| Call_provers.Valid ->
                  fprintf fmt "C0FFC0\">%.2f" res.Call_provers.pr_time
		| Call_provers.Invalid ->
                  fprintf fmt "FF0000\">Invalid"
		| Call_provers.Timeout ->
                  fprintf fmt "FF8000\">Timeout"
		| Call_provers.Unknown _ ->
MARCHE Claude's avatar
MARCHE Claude committed
146
                  fprintf fmt "FF8000\">%.2f" res.Call_provers.pr_time
147
		| _ ->
MARCHE Claude's avatar
MARCHE Claude committed
148
                  fprintf fmt "FF8000\">Failure "
149
	    end
MARCHE Claude's avatar
MARCHE Claude committed
150 151
	  | _ -> fprintf fmt "E0E0E0\">Undone"
      with Not_found -> fprintf fmt "E0E0E0\">---"
152 153 154
    end;
    fprintf fmt "</td>") provers

MARCHE Claude's avatar
MARCHE Claude committed
155 156 157 158 159 160 161
let rec num_lines acc tr =
  List.fold_left
    (fun acc g -> 1 + 
      PHstr.fold (fun _ tr acc -> 1 + num_lines acc tr) 
      g.goal_transformations acc)
    acc tr.transf_goals

162 163 164
  let rec print_transf fmt depth max_depth provers tr =
    fprintf fmt "<tr>";
    for i=1 to 0 (* depth-1 *) do fprintf fmt "<td></td>" done;
MARCHE Claude's avatar
MARCHE Claude committed
165 166 167 168 169
    fprintf fmt "<td bgcolor=\"#%a\" colspan=\"%d\">" 
      (color_of_status ~dark:false) tr.S.transf_verified 
      (max_depth - depth + 1);
    (* for i=1 to depth-1 do fprintf fmt "&nbsp;&nbsp;&nbsp;&nbsp;" done; *)
    fprintf fmt "%s</td>" tr.transf_name ;
170 171 172 173
    for i=1 (* depth *) to (*max_depth - 1 + *) List.length provers do
      fprintf fmt "<td bgcolor=\"#E0E0E0\"></td>"
    done;
    fprintf fmt "</tr>@\n";
MARCHE Claude's avatar
MARCHE Claude committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187
    fprintf fmt "<td rowspan=\"%d\">&nbsp;&nbsp;</td>" (num_lines 0 tr);
    let (_:bool) = List.fold_left
      (fun is_first g ->
        print_goal fmt is_first (depth+1) max_depth provers g;
        false)
      true tr.transf_goals
    in ()

  and print_goal fmt is_first depth max_depth provers g =
    if not is_first then fprintf fmt "<tr>";
    (* for i=1 to 0 (\* depth-1 *\) do fprintf fmt "<td></td>" done; *)
    fprintf fmt "<td bgcolor=\"#%a\" colspan=\"%d\">" 
      (color_of_status ~dark:false) g.S.goal_verified (max_depth - depth + 1);
    (* for i=1 to depth-1 do fprintf fmt "&nbsp;&nbsp;&nbsp;&nbsp;" done; *)
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
    fprintf fmt "%s</td>" (S.goal_expl g);
(*    for i=depth to max_depth-1 do fprintf fmt "<td></td>" done; *)
    print_results fmt provers g.goal_external_proofs;
    fprintf fmt "</tr>@\n";
    PHstr.iter
      (fun _ tr -> print_transf fmt depth max_depth provers tr)
      g.goal_transformations

  let print_theory fmt th =
    let depth = theory_depth th in
    if depth > 0 then
    let provers = Hashtbl.create 9 in
    provers_stats provers th;
    let provers =
      Hashtbl.fold (fun _ pr acc -> pr :: acc) provers []
    in
    let provers = List.sort Whyconf.Prover.compare provers in
    let name = th.S.theory_name.Ident.id_string in
    fprintf fmt "<h2><font color=\"#%a\">Theory \"%s\": %s</font></h2>@\n" 
      (color_of_status ~dark:true) th.S.theory_verified
      name (if th.S.theory_verified then "fully verified" else "not fully verified")
    ;
MARCHE Claude's avatar
MARCHE Claude committed
210

211
    fprintf fmt "<table border=\"1\"><tr><td colspan=\"%d\">Obligations</td>" depth;
MARCHE Claude's avatar
MARCHE Claude committed
212
    (* fprintf fmt "<table border=\"1\"><tr><td>Obligations</td>"; *)
213 214 215 216
    List.iter
      (fun pr -> fprintf fmt "<td text-rotation=\"90\">%a</td>" print_prover pr)
      provers;
    fprintf fmt "</td></tr>@\n";
MARCHE Claude's avatar
MARCHE Claude committed
217
    List.iter (print_goal fmt true 1 depth provers) th.theory_goals;
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
    fprintf fmt "</table>@\n"

  let print_file fmt f =
    (* fprintf fmt "<h1>File %s</h1>@\n" f.file_name; *)
    fprintf fmt "%a"
      (Pp.print_list Pp.newline print_theory) f.file_theories

  let print_session name fmt s =
    fprintf fmt "<h1>Why3 Proof Results for Project \"%s\"</h1>@\n" name;
    fprintf fmt "%a"
      (Pp.print_iter2 PHstr.iter Pp.newline Pp.nothing Pp.nothing
         print_file) s.session_files


  let context : context = "<!DOCTYPE html
PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\">
<head>
<title>Why3 session of %s</title>
</head>
<body>
%a
</body>
</html>
"

  let run_one = run_file context print_session

end



251 252 253
module Simple =
struct

254
  let print_prover = Whyconf.print_prover
255 256

  let print_proof_status fmt = function
François Bobot's avatar
François Bobot committed
257
    | Undone _ -> fprintf fmt "Undone"
258 259 260
    | Done pr -> fprintf fmt "Done : %a" Call_provers.print_prover_result pr
    | InternalFailure exn ->
      fprintf fmt "Failure : %a"Exn_printer.exn_printer exn
261

262 263
  let print_proof_attempt fmt pa =
    fprintf fmt "<li>%a : %a</li>"
François Bobot's avatar
François Bobot committed
264
      print_prover pa.proof_prover
265 266 267 268 269
      print_proof_status pa.proof_state

  let rec print_transf fmt tr =
    fprintf fmt "<li>%s : <ul>%a</ul></li>"
      tr.transf_name
François Bobot's avatar
François Bobot committed
270
      (Pp.print_list Pp.newline print_goal) tr.transf_goals
271 272 273

  and print_goal fmt g =
    fprintf fmt "<li>%s : <ul>%a%a</ul></li>"
François Bobot's avatar
François Bobot committed
274 275
      g.goal_name.Ident.id_string
      (Pp.print_iter2 PHprover.iter Pp.newline Pp.nothing
276
         Pp.nothing print_proof_attempt)
François Bobot's avatar
François Bobot committed
277 278
      g.goal_external_proofs
      (Pp.print_iter2 PHstr.iter Pp.newline Pp.nothing
279
         Pp.nothing print_transf)
François Bobot's avatar
François Bobot committed
280
      g.goal_transformations
281 282 283

  let print_theory fmt th =
    fprintf fmt "<li>%s : <ul>%a</ul></li>"
François Bobot's avatar
François Bobot committed
284 285
      th.theory_name.Ident.id_string
      (Pp.print_list Pp.newline print_goal) th.theory_goals
286 287 288 289

  let print_file fmt f =
    fprintf fmt "<li>%s : <ul>%a</ul></li>"
      f.file_name
François Bobot's avatar
François Bobot committed
290
      (Pp.print_list Pp.newline print_theory) f.file_theories
291 292 293

  let print_session _name fmt s =
    fprintf fmt "<ul>%a</ul>"
François Bobot's avatar
François Bobot committed
294 295
      (Pp.print_iter2 PHstr.iter Pp.newline Pp.nothing Pp.nothing
         print_file) s.session_files
296 297 298


  let context : context = "<!DOCTYPE html
299 300 301 302
PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\">
<head>
303
<title>Why3 session of %s</title>
304 305 306 307 308 309
</head>
<body>
%a
</body>
</html>
"
310

311
  let run_one = run_file context print_session
312

313
end
314

315

316 317 318
module Jstree =
struct

319 320 321 322 323
  let print_verified fmt b =
    if b
    then fprintf fmt "class='verified'"
    else fprintf fmt "class='notverified'"

324
  let print_prover = Whyconf.print_prover
325 326

  let print_proof_status fmt = function
François Bobot's avatar
François Bobot committed
327
    | Undone _ -> fprintf fmt "<span class='notverified'>Undone</span>"
328 329
    | Done pr -> fprintf fmt "<span class='verified'>Done : %a</span>"
      Call_provers.print_prover_result pr
330
    | InternalFailure exn ->
331 332
      fprintf fmt "<span class='notverified'>Failure : %a</span>"
        Exn_printer.exn_printer exn
333

334 335 336 337 338 339 340 341 342 343 344 345 346 347
  let cmd_regexp = Str.regexp "%\\(.\\)"

  let pp_run input cmd output =
    let replace s = match Str.matched_group 1 s with
      | "%" -> "%"
      | "i" -> input
      | "o" -> output
      | _ -> failwith "unknown format specifier, use %%i, %%o"
    in
    let cmd = Str.global_substitute cmd_regexp replace cmd in
    let c = Sys.command cmd in
    if c <> 0 then
      eprintf "[Error] '%s' stopped abnormaly : code %i" cmd c

348 349
 let edited_dst = Filename.concat !output_dir "edited"

350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
  let find_pp edited =
    let basename = Filename.basename edited in
    try
      let suff,(cmd,suff_out) =
        List.find (fun (s,_) -> ends_with basename s) !opt_pp in
      let base =
        String.sub basename 0 (String.length basename - String.length suff) in
      let base_dst = (base^suff_out) in
      if !opt_context then
        pp_run edited cmd (Filename.concat edited_dst base_dst);
      base_dst
    with Not_found ->
      eprintf "Default %s@." basename;
      (** default printer *)
      let base = try Filename.chop_extension basename with _ -> basename in
      let base_dst = base^".txt" in
      if !opt_context then
        Sysutil.copy_file edited (Filename.concat edited_dst base_dst);
      base_dst

370
  let print_proof_attempt fmt pa =
François Bobot's avatar
François Bobot committed
371
    match pa.proof_edited_as with
372 373
      | None ->
        fprintf fmt "@[<hov><li rel='prover' ><a href='#'>%a : %a</a></li>@]"
François Bobot's avatar
François Bobot committed
374
          print_prover pa.proof_prover
375 376 377 378 379 380
          print_proof_status pa.proof_state
      | Some f ->
        let output = find_pp f in
        fprintf fmt "@[<hov><li rel='prover' ><a href='#' \
onclick=\"showedited('%s'); return false;\">%a : %a</a></li>@]"
          output
François Bobot's avatar
François Bobot committed
381
          print_prover pa.proof_prover
382
          print_proof_status pa.proof_state
383 384 385

  let rec print_transf fmt tr =
    fprintf fmt
386 387 388
      "@[<hov><li rel='transf'><a href='#'>\
<span %a>%s</span></a>@,<ul>%a</ul></li>@]"
      print_verified tr.transf_verified
389
      tr.transf_name
François Bobot's avatar
François Bobot committed
390
      (Pp.print_list Pp.newline print_goal) tr.transf_goals
391 392 393

  and print_goal fmt g =
    fprintf fmt
394 395 396
      "@[<hov><li rel='goal'><a href='#'>\
<span %a>%s</a></a>@,<ul>%a%a</ul></li>@]"
      print_verified g.goal_verified
François Bobot's avatar
François Bobot committed
397 398
      g.goal_name.Ident.id_string
      (Pp.print_iter2 PHprover.iter Pp.newline Pp.nothing
399
         Pp.nothing print_proof_attempt)
François Bobot's avatar
François Bobot committed
400 401
      g.goal_external_proofs
      (Pp.print_iter2 PHstr.iter Pp.newline Pp.nothing
402
         Pp.nothing print_transf)
François Bobot's avatar
François Bobot committed
403
      g.goal_transformations
404 405 406

  let print_theory fmt th =
    fprintf fmt
407 408 409
      "@[<hov><li rel='theory'><a href='#'>\
<span %a>%s</span></a>@,<ul>%a</ul></li>@]"
      print_verified th.theory_verified
François Bobot's avatar
François Bobot committed
410 411
      th.theory_name.Ident.id_string
      (Pp.print_list Pp.newline print_goal) th.theory_goals
412 413

  let print_file fmt f =
414 415 416 417
    fprintf fmt
      "@[<hov><li rel='file'><a href='#'>\
<span %a>%s</span></a>@,<ul>%a</ul></li>@]"
      print_verified f.file_verified
418
      f.file_name
François Bobot's avatar
François Bobot committed
419
      (Pp.print_list Pp.newline print_theory) f.file_theories
420 421 422 423

  let print_session_aux name fmt s =
    fprintf fmt "@[<hov><ul><a href='#'>%s</a>@,%a</ul>@]"
      name
François Bobot's avatar
François Bobot committed
424 425
      (Pp.print_iter2 PHstr.iter Pp.newline Pp.nothing Pp.nothing print_file)
      s.session_files
426 427 428 429 430 431 432 433 434 435 436 437 438 439


  let print_session name fmt s =
    fprintf fmt
      "<a href='#' onclick=\"$('#%s_session').jstree('open_all');\">
expand all
</a> <a href='#' onclick=\"$('#%s_session').jstree('close_all');\">
close all
</a>
<div id=\"%s_session\" class=\"session\">
%a
</div>
<script type=\"text/javascript\" class=\"source\">
$(function () {
440
  $(\"#%s_session\").jstree({
441 442 443 444 445 446 447 448 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
      \"types\" : {
	   \"types\" : {
	   \"file\" : {
		\"icon\" : { \"image\" : \"images/folder16.png\"},
		      },
	   \"theory\" : {
		\"icon\" : { \"image\" : \"images/folder16.png\"},
		      },
	   \"goal\" : {
		\"icon\" : { \"image\" : \"images/file16.png\"},
		      },
	   \"prover\" : {
		\"icon\" : { \"image\" : \"images/wizard16.png\"},
		      },
	   \"transf\" : {
		\"icon\" : { \"image\" : \"images/configure16.png\"},
		      },
                        }
                },
      \"plugins\" : [\"themes\",\"html_data\",\"types\"]
        });
});
</script>
"
  name name name (print_session_aux name) s name


  let context : context = "<!DOCTYPE html
PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\">
<head>
    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />
    <title>Why3 session of %s</title>
475 476
    <link rel=\"stylesheet\" type=\"text/css\"
     href=\"javascript/session.css\" />
477
    <script type=\"text/javascript\" src=\"javascript/jquery.js\"></script>
478 479 480 481
    <script type=\"text/javascript\" src=\"javascript/jquery.jstree.js\">\
</script>
    <script type=\"text/javascript\" src=\"javascript/session.js\">\
</script>
482 483 484
</head>
<body>
%a
485 486 487 488 489 490 491 492
<iframe src=\"\"  id='edited'>
<p>Your browser does not support iframes.</p>
</iframe>
<script type=\"text/javascript\" class=\"source\">
$(function () {
  $('#edited').hide()
})
</script>
493 494 495 496
</body>
</html>
"

497
  let run_files config =
498 499
    if !opt_context then
      if not (Sys.file_exists edited_dst) then Unix.mkdir edited_dst 0o755;
500
    iter_files (run_file context print_session);
501
    if !opt_context then
502
      let data_dir = Whyconf.datadir (Whyconf.get_main config) in
503 504
      (** copy images *)
      let img_dir_src = Filename.concat data_dir "images" in
505
      let img_dir_dst = Filename.concat !output_dir "images" in
506 507 508 509 510 511 512 513
      if not (Sys.file_exists img_dir_dst) then Unix.mkdir img_dir_dst 0o755;
      List.iter (fun img_name ->
        let from = Filename.concat img_dir_src img_name in
        let to_  = Filename.concat img_dir_dst img_name in
        Sysutil.copy_file from to_)
        ["folder16.png";"file16.png";"wizard16.png";"configure16.png"];
      (** copy javascript *)
      let js_dir_src = Filename.concat data_dir "javascript" in
514
      let js_dir_dst = Filename.concat !output_dir "javascript" in
515 516 517 518 519
      Sysutil.copy_dir js_dir_src js_dir_dst

end


520 521

let run () =
522
  let _,config,should_exit1 = read_env_spec () in
523
  if should_exit1 then exit 1;
524 525 526 527
  match !opt_style with
    | Table -> iter_files Table.run_one
    | SimpleTree -> iter_files Simple.run_one
    | Jstree -> Jstree.run_files config
528 529 530 531 532 533 534 535


let cmd =
  { cmd_spec = spec;
    cmd_desc = "output session in HTML format.";
    cmd_name = "html";
    cmd_run  = run;
  }
536 537 538 539 540 541 542


(*
Local Variables:
compile-command: "unset LANG; make -C ../.. bin/why3session.byte"
End:
*)