src/HOL/TPTP/TPTP_Parser/tptp_to_dot.ML
author sultana
Wed, 19 Feb 2014 15:57:02 +0000
changeset 55591 5a9ef4473133
parent 55590 14e8e51c7fa8
child 55592 37c1abaf4876
permissions -rw-r--r--
experimented with presentation of DOT+LaTeX;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47412
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
     1
(*  Title:      HOL/TPTP/TPTP_Parser/tptp_to_dot.ML
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
     2
    Author:     Nik Sultana, Cambridge University Computer Laboratory
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
     3
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
     4
Translates parsed TPTP proofs into DOT format. This can then be processed
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
     5
by an accompanying script to translate the proofs into other formats.
55586
c94f1a72d9c5 added more node shapes (matched with roles);
sultana
parents: 53389
diff changeset
     6
c94f1a72d9c5 added more node shapes (matched with roles);
sultana
parents: 53389
diff changeset
     7
It tries to adhere to the symbols used in IDV, as described in
c94f1a72d9c5 added more node shapes (matched with roles);
sultana
parents: 53389
diff changeset
     8
"An Interactive Derivation Viewer" by Trac & Puzis & Sutcliffe, UITP 2006.
47412
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
     9
*)
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    10
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    11
signature TPTP_TO_DOT =
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    12
sig
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    13
  (*DOT-drawing function, works directly on parsed TPTP*)
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    14
  val tptp_dot_node : bool -> bool -> TPTP_Syntax.tptp_line -> string
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    15
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    16
  (*Parse a (LEO-II+E) proof and produce a DOT file*)
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    17
  val write_proof_dot : string -> string -> unit
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    18
end
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    19
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    20
structure TPTP_To_Dot : TPTP_TO_DOT =
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    21
struct
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    22
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    23
open TPTP_Syntax
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    24
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    25
(*Draw an arc between two nodes*)
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    26
fun dot_arc reverse (src, label) target =
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    27
  "\"" ^ (if reverse then target else src) ^
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    28
  "\" -> \"" ^ (if reverse then src else target) ^
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    29
  "\" " ^ (case label of
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    30
              NONE => ""
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    31
            | SOME label => "[label=\"" ^ label ^ "\"];") ^ "\n"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    32
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    33
(*Node shapes indicate the role of the related clauses.*)
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    34
exception NO_ROLE_SHAPE
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    35
fun the_role_shape role =
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    36
  case role of
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    37
    Role_Axiom => "triangle"
55586
c94f1a72d9c5 added more node shapes (matched with roles);
sultana
parents: 53389
diff changeset
    38
  | Role_Hypothesis => "invtrapezium"
c94f1a72d9c5 added more node shapes (matched with roles);
sultana
parents: 53389
diff changeset
    39
  | Role_Definition => "invtriangle" (*NOTE this is not standard wrt IDV*)
c94f1a72d9c5 added more node shapes (matched with roles);
sultana
parents: 53389
diff changeset
    40
  | Role_Assumption => "trapezium" (*NOTE this is not standard wrt IDV*)
c94f1a72d9c5 added more node shapes (matched with roles);
sultana
parents: 53389
diff changeset
    41
  | Role_Lemma => "hexagon"
c94f1a72d9c5 added more node shapes (matched with roles);
sultana
parents: 53389
diff changeset
    42
  | Role_Theorem => "star" (*NOTE this is not standard wrt IDV*)
55590
14e8e51c7fa8 edges are now being shown in the proof graph;
sultana
parents: 55587
diff changeset
    43
55591
5a9ef4473133 experimented with presentation of DOT+LaTeX;
sultana
parents: 55590
diff changeset
    44
  (*FIXME add a parameter to switch the behaviour described below.*)
5a9ef4473133 experimented with presentation of DOT+LaTeX;
sultana
parents: 55590
diff changeset
    45
  (*NOTE can change the next three to plaintext to avoid clutter if
5a9ef4473133 experimented with presentation of DOT+LaTeX;
sultana
parents: 55590
diff changeset
    46
         the inference's conclusion formula is also being displayed.*)
5a9ef4473133 experimented with presentation of DOT+LaTeX;
sultana
parents: 55590
diff changeset
    47
  | Role_Conjecture => (* "plaintext" *) "house"
5a9ef4473133 experimented with presentation of DOT+LaTeX;
sultana
parents: 55590
diff changeset
    48
  | Role_Negated_Conjecture => (* "plaintext" *) "invhouse"
55587
5d3db2c626e3 experimenting with improving DOT output, and embedding LaTeX code for formulas (rather than only giving the clause number);
sultana
parents: 55586
diff changeset
    49
  | Role_Plain => "plaintext" (* "circle" *) (*could also use none*)
55590
14e8e51c7fa8 edges are now being shown in the proof graph;
sultana
parents: 55587
diff changeset
    50
47412
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    51
  | Role_Fi_Domain => raise NO_ROLE_SHAPE
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    52
  | Role_Fi_Functors => raise NO_ROLE_SHAPE
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    53
  | Role_Fi_Predicates => raise NO_ROLE_SHAPE
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    54
  | Role_Type => raise NO_ROLE_SHAPE
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    55
  | Role_Unknown => raise NO_ROLE_SHAPE
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    56
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    57
fun have_role_shape role =
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    58
  (the_role_shape role; true)
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    59
  handle NO_ROLE_SHAPE => false
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    60
       | exc => raise exc
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    61
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    62
(*Different styles are applied to nodes relating to clauses written in
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    63
  difference languages.*)
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    64
exception NO_LANG_STYLE
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    65
fun the_lang_style lang =
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    66
  case lang of
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    67
      CNF => "dotted"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    68
    | FOF => "dashed"
55587
5d3db2c626e3 experimenting with improving DOT output, and embedding LaTeX code for formulas (rather than only giving the clause number);
sultana
parents: 55586
diff changeset
    69
    | THF => "" (* "filled" *)
47412
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    70
    | _ => raise NO_LANG_STYLE
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    71
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    72
(*Does the formula just consist of "$false"?*)
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    73
fun is_last_line CNF (Pred (Interpreted_Logic False, [])) = true
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    74
  | is_last_line THF (Atom (THF_Atom_term
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    75
      (Term_Func (Interpreted_Logic False, [])))) = true
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    76
  | is_last_line _ _ = false
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    77
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    78
fun tptp_dot_node with_label reverse_arrows
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    79
 (Annotated_Formula (_, lang, n, role, fmla_tptp, annot)) =
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    80
 (*don't expect to find 'Include' in proofs*)
55587
5d3db2c626e3 experimenting with improving DOT output, and embedding LaTeX code for formulas (rather than only giving the clause number);
sultana
parents: 55586
diff changeset
    81
 if have_role_shape role andalso role <> Role_Definition then
47412
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    82
   "\"" ^ n ^
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    83
   "\" [shape=\"" ^
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    84
      (if is_last_line lang fmla_tptp then "doublecircle"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    85
       else the_role_shape role) ^
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    86
   "\", style=\"" ^ the_lang_style lang ^
55587
5d3db2c626e3 experimenting with improving DOT output, and embedding LaTeX code for formulas (rather than only giving the clause number);
sultana
parents: 55586
diff changeset
    87
   (if role = Role_Definition then "\"];\n" else
55591
5a9ef4473133 experimented with presentation of DOT+LaTeX;
sultana
parents: 55590
diff changeset
    88
     (* "\", label=\"$" ^ TPTP_Syntax.latex_of_tptp_formula fmla_tptp ^ "$\"];\n") ^ *)
5a9ef4473133 experimented with presentation of DOT+LaTeX;
sultana
parents: 55590
diff changeset
    89
(*FIXME  add a parameter to switch to using the following code, which lowers, centers, and horizontally-bounds the label.
5a9ef4473133 experimented with presentation of DOT+LaTeX;
sultana
parents: 55590
diff changeset
    90
        (this is useful if you want to keep the shapes but also show formulas)*)
5a9ef4473133 experimented with presentation of DOT+LaTeX;
sultana
parents: 55590
diff changeset
    91
    "\", label=\"\\\\begin{minipage}{10cm}\\\\vspace{21mm}\\\\centering$" ^ TPTP_Syntax.latex_of_tptp_formula fmla_tptp ^ "$\\\\end{minipage}\"];\n") ^
53389
74cee48bccd6 brought up to date with TPTP_Proof;
sultana
parents: 47426
diff changeset
    92
   (case TPTP_Proof.extract_source_info annot of
74cee48bccd6 brought up to date with TPTP_Proof;
sultana
parents: 47426
diff changeset
    93
        SOME (TPTP_Proof.Inference (rule, _, pinfos)) =>
74cee48bccd6 brought up to date with TPTP_Proof;
sultana
parents: 47426
diff changeset
    94
          let
74cee48bccd6 brought up to date with TPTP_Proof;
sultana
parents: 47426
diff changeset
    95
            fun parent_id (TPTP_Proof.Parent n) = n
74cee48bccd6 brought up to date with TPTP_Proof;
sultana
parents: 47426
diff changeset
    96
              | parent_id (TPTP_Proof.ParentWithDetails (n, _)) = n
74cee48bccd6 brought up to date with TPTP_Proof;
sultana
parents: 47426
diff changeset
    97
            val parent_ids = map parent_id pinfos
74cee48bccd6 brought up to date with TPTP_Proof;
sultana
parents: 47426
diff changeset
    98
          in
74cee48bccd6 brought up to date with TPTP_Proof;
sultana
parents: 47426
diff changeset
    99
            map
74cee48bccd6 brought up to date with TPTP_Proof;
sultana
parents: 47426
diff changeset
   100
              (dot_arc reverse_arrows
74cee48bccd6 brought up to date with TPTP_Proof;
sultana
parents: 47426
diff changeset
   101
               (n, if with_label then SOME rule else NONE))
74cee48bccd6 brought up to date with TPTP_Proof;
sultana
parents: 47426
diff changeset
   102
              parent_ids
74cee48bccd6 brought up to date with TPTP_Proof;
sultana
parents: 47426
diff changeset
   103
            |> implode
74cee48bccd6 brought up to date with TPTP_Proof;
sultana
parents: 47426
diff changeset
   104
          end
74cee48bccd6 brought up to date with TPTP_Proof;
sultana
parents: 47426
diff changeset
   105
      | _ => "")
47412
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   106
 else ""
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   107
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   108
(*FIXME add opts to label arcs etc*)
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   109
fun write_proof_dot input_file output_file =
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   110
  let
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   111
    (*rankdir=\"LR\";\n*)
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   112
    val defaults =
55587
5d3db2c626e3 experimenting with improving DOT output, and embedding LaTeX code for formulas (rather than only giving the clause number);
sultana
parents: 55586
diff changeset
   113
      "graph[nodesep=3];\n" ^
47412
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   114
      "node[fixedsize=true];\n" ^
55587
5d3db2c626e3 experimenting with improving DOT output, and embedding LaTeX code for formulas (rather than only giving the clause number);
sultana
parents: 55586
diff changeset
   115
      "node[width=0.5];\n" ^
47412
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   116
      "node[shape=plaintext];\n" ^
55587
5d3db2c626e3 experimenting with improving DOT output, and embedding LaTeX code for formulas (rather than only giving the clause number);
sultana
parents: 55586
diff changeset
   117
      (* "node[fillcolor=lightgray];\n" ^ *)
55590
14e8e51c7fa8 edges are now being shown in the proof graph;
sultana
parents: 55587
diff changeset
   118
      "node[fontsize=50];\n"
47412
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   119
  in
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   120
    TPTP_Parser.parse_file input_file
55587
5d3db2c626e3 experimenting with improving DOT output, and embedding LaTeX code for formulas (rather than only giving the clause number);
sultana
parents: 55586
diff changeset
   121
    |> map (tptp_dot_node true true)
47426
26c1a97c7784 standardized ML aliases;
wenzelm
parents: 47412
diff changeset
   122
    |> implode
47412
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   123
    |> (fn str => "digraph ProofGraph {\n" ^ defaults ^ str ^ "}")
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   124
    |> File.write (Path.explode output_file)
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   125
  end
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   126
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   127
end