src/HOL/TPTP/TPTP_Parser/tptp_to_dot.ML
author blanchet
Mon, 29 Jul 2013 15:30:31 +0200
changeset 52754 d9d90d29860e
parent 47426 26c1a97c7784
child 53389 74cee48bccd6
permissions -rw-r--r--
added support for E 1.8's internal proof objects (eliminating the need for "eproof_ram")
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.
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
     6
*)
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
     7
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
     8
signature TPTP_TO_DOT =
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
     9
sig
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    10
  (*DOT-drawing function, works directly on parsed TPTP*)
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    11
  val tptp_dot_node : bool -> bool -> TPTP_Syntax.tptp_line -> string
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    12
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    13
  (*Parse a (LEO-II+E) proof and produce a DOT file*)
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    14
  val write_proof_dot : string -> string -> unit
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    15
end
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    16
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    17
structure TPTP_To_Dot : TPTP_TO_DOT =
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    18
struct
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
open TPTP_Syntax
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    21
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    22
(*Draw an arc between two nodes*)
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    23
fun dot_arc reverse (src, label) target =
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    24
  "\"" ^ (if reverse then target else src) ^
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    25
  "\" -> \"" ^ (if reverse then src else target) ^
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    26
  "\" " ^ (case label of
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    27
              NONE => ""
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    28
            | SOME label => "[label=\"" ^ label ^ "\"];") ^ "\n"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    29
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    30
(*Node shapes indicate the role of the related clauses.*)
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    31
exception NO_ROLE_SHAPE
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    32
fun the_role_shape role =
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    33
  case role of
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    34
    Role_Axiom => "triangle"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    35
  | Role_Hypothesis => "???"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    36
  | Role_Definition => raise NO_ROLE_SHAPE
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    37
  | Role_Assumption => "???"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    38
  | Role_Lemma => "???"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    39
  | Role_Theorem => "???"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    40
  | Role_Conjecture => "house"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    41
  | Role_Negated_Conjecture => "invhouse"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    42
  | Role_Plain => "circle"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    43
  | Role_Fi_Domain => raise NO_ROLE_SHAPE
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    44
  | Role_Fi_Functors => raise NO_ROLE_SHAPE
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    45
  | Role_Fi_Predicates => raise NO_ROLE_SHAPE
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    46
  | Role_Type => raise NO_ROLE_SHAPE
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    47
  | Role_Unknown => raise NO_ROLE_SHAPE
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    48
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    49
fun have_role_shape role =
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    50
  (the_role_shape role; true)
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    51
  handle NO_ROLE_SHAPE => false
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    52
       | exc => raise exc
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    53
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    54
(*Different styles are applied to nodes relating to clauses written in
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    55
  difference languages.*)
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    56
exception NO_LANG_STYLE
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    57
fun the_lang_style lang =
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    58
  case lang of
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    59
      CNF => "dotted"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    60
    | FOF => "dashed"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    61
    | THF => "filled"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    62
    | _ => raise NO_LANG_STYLE
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    63
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    64
(*Does the formula just consist of "$false"?*)
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    65
fun is_last_line CNF (Pred (Interpreted_Logic False, [])) = true
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    66
  | is_last_line THF (Atom (THF_Atom_term
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    67
      (Term_Func (Interpreted_Logic False, [])))) = true
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    68
  | is_last_line _ _ = false
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    69
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    70
fun tptp_dot_node with_label reverse_arrows
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    71
 (Annotated_Formula (_, lang, n, role, fmla_tptp, annot)) =
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    72
 (*don't expect to find 'Include' in proofs*)
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    73
 if have_role_shape role
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    74
 then
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    75
   "\"" ^ n ^
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    76
   "\" [shape=\"" ^
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    77
      (if is_last_line lang fmla_tptp then "doublecircle"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    78
       else the_role_shape role) ^
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    79
   "\", style=\"" ^ the_lang_style lang ^
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    80
   "\", label=\"" ^ n ^ "\"];\n" ^
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    81
   (case TPTP_Proof.extract_inference_info annot of
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    82
     NONE => ""
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    83
   | SOME (rule, ids) =>
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    84
       map (dot_arc reverse_arrows
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    85
             (n, if with_label then SOME rule else NONE)) ids
47426
26c1a97c7784 standardized ML aliases;
wenzelm
parents: 47412
diff changeset
    86
       |> implode)
47412
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    87
 else ""
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    88
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    89
(*FIXME add opts to label arcs etc*)
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    90
fun write_proof_dot input_file output_file =
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    91
  let
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    92
    (*rankdir=\"LR\";\n*)
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    93
    val defaults =
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    94
      "node[fixedsize=true];\n" ^
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    95
      "node[width=.5];\n" ^
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    96
      "node[shape=plaintext];\n" ^
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    97
      "node[fillcolor=lightgray];\n" ^
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    98
      "node[fontsize=40];\n" ^
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    99
      "edge[dir=none];\n"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   100
  in
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   101
    TPTP_Parser.parse_file input_file
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   102
    |> map (tptp_dot_node false true)
47426
26c1a97c7784 standardized ML aliases;
wenzelm
parents: 47412
diff changeset
   103
    |> implode
47412
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   104
    |> (fn str => "digraph ProofGraph {\n" ^ defaults ^ str ^ "}")
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   105
    |> File.write (Path.explode output_file)
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   106
  end
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
end