src/HOL/TPTP/lib/Tools/tptp_graph
author wenzelm
Wed, 09 Mar 2016 19:30:09 +0100
changeset 62573 27f90319a499
parent 62475 43e64c770f28
child 62588 cd266473b81b
permissions -rwxr-xr-x
isabelle.Build uses ML_Process directly; isabelle_process is for batch mode only; removed unused feeder (already part of "isabelle console");
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47412
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
     1
#!/usr/bin/env bash
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
     2
#
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
     3
# Author: Nik Sultana, Cambridge University Computer Lab
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
     4
#
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
     5
# DESCRIPTION: TPTP visualisation utility
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
PRG="$(basename "$0")"
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
#FIXME inline or move to settings
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    11
DOT2TEX=dot2tex
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    12
DOT=dot
47473
111cd6351613 aligned tptp_graph dependencies to Isabelle conventions;
sultana
parents: 47412
diff changeset
    13
PERL=perl
47412
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    14
PDFLATEX=pdflatex
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    15
[ -n "$ISABELLE_PDFLATEX" ] && PDFLATEX=$ISABELLE_PDFLATEX
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    16
DOT2TEX_VERSION="$($DOT2TEX -V 2> /dev/null)"
47473
111cd6351613 aligned tptp_graph dependencies to Isabelle conventions;
sultana
parents: 47412
diff changeset
    17
DOT_VERSION="$($DOT -V 2>&1 | grep Graphviz)"
111cd6351613 aligned tptp_graph dependencies to Isabelle conventions;
sultana
parents: 47412
diff changeset
    18
PERL_VERSION="$($PERL -v | grep -e "v[0-9]\+." 2> /dev/null)"
47412
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    19
PDFLATEX_VERSION="$($PDFLATEX -version | head -1 2> /dev/null)"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    20
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    21
function check_deps()
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
  #FIXME how well does this work across different platforms and/or versions of
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    24
  #      the tools?
47473
111cd6351613 aligned tptp_graph dependencies to Isabelle conventions;
sultana
parents: 47412
diff changeset
    25
  for DEP in DOT2TEX DOT PERL PDFLATEX
47412
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    26
  do
47473
111cd6351613 aligned tptp_graph dependencies to Isabelle conventions;
sultana
parents: 47412
diff changeset
    27
    eval DEP_VAL=\$${DEP}
111cd6351613 aligned tptp_graph dependencies to Isabelle conventions;
sultana
parents: 47412
diff changeset
    28
    eval DEP_VERSION=\$${DEP}_VERSION
111cd6351613 aligned tptp_graph dependencies to Isabelle conventions;
sultana
parents: 47412
diff changeset
    29
    if [ -z "$DEP_VERSION" ]; then
47412
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    30
      echo "$DEP not installed"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    31
    else
47473
111cd6351613 aligned tptp_graph dependencies to Isabelle conventions;
sultana
parents: 47412
diff changeset
    32
      echo "$DEP ($DEP_VAL) : $DEP_VERSION"
47412
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    33
    fi
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    34
  done
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    35
}
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    36
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    37
function usage() {
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    38
  echo
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    39
  echo "Usage: isabelle $PRG [OPTIONS] IN_FILE OUT_FILE"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    40
  echo
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    41
  echo "  Options are:"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    42
  echo "    -d           probe for dependencies"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    43
  echo "    -k           don't delete temp files, and print their location"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    44
  echo "    -n           print name of the generated file"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    45
  echo
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    46
  echo "  Produces a DOT/TeX/PDF from a TPTP problem/proof, depending on whether"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    47
  echo "  the extension of OUT_FILE is dot/tex/pdf."
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    48
  echo
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    49
  exit 1
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    50
}
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    51
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    52
OUTPUT_FORMAT=2
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    53
SHOW_TARGET=""
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    54
KEEP_TEMP=""
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    55
NON_EXEC=""
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    56
47473
111cd6351613 aligned tptp_graph dependencies to Isabelle conventions;
sultana
parents: 47412
diff changeset
    57
while getopts "dnkX" OPT
47412
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    58
do
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    59
  #FIXME could add "quiet" mode to send stderr (and some stdout) to /dev/null
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    60
  case "$OPT" in
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    61
    n)
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    62
      SHOW_TARGET=true
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
    k)
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    65
      KEEP_TEMP=true
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    66
      ;;
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    67
    X)
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    68
      NON_EXEC=true
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
    d)
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    71
      check_deps
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    72
      exit 0
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    73
      ;;
47517
6bc4c66d8c1b improved tptp_graph robustness by relying on thy;
sultana
parents: 47473
diff changeset
    74
    *)
6bc4c66d8c1b improved tptp_graph robustness by relying on thy;
sultana
parents: 47473
diff changeset
    75
      exit 1
6bc4c66d8c1b improved tptp_graph robustness by relying on thy;
sultana
parents: 47473
diff changeset
    76
      ;;
47412
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    77
  esac
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    78
done
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    79
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    80
shift $(($OPTIND - 1))
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    81
[ "$#" -ne 2 -o "$1" = "-?" ] && usage
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    82
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    83
case "${2##*.}" in
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    84
    dot)
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    85
      OUTPUT_FORMAT=0
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    86
      ;;
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    87
    tex)
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    88
      OUTPUT_FORMAT=1
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    89
      ;;
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    90
    pdf)
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    91
      OUTPUT_FORMAT=2
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    92
      ;;
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    93
    *)
47517
6bc4c66d8c1b improved tptp_graph robustness by relying on thy;
sultana
parents: 47473
diff changeset
    94
      echo "Unrecognised output file extension \".${2##*.}\"."
47412
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    95
      exit 1
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    96
      ;;
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    97
esac
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    98
47517
6bc4c66d8c1b improved tptp_graph robustness by relying on thy;
sultana
parents: 47473
diff changeset
    99
## set some essential variables, prepare the work directory
47412
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   100
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   101
WORKDIR=""
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   102
while :
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   103
do
47473
111cd6351613 aligned tptp_graph dependencies to Isabelle conventions;
sultana
parents: 47412
diff changeset
   104
  #FIXME not perfectly reliable method, but probably good enough
111cd6351613 aligned tptp_graph dependencies to Isabelle conventions;
sultana
parents: 47412
diff changeset
   105
  WORKDIR="${ISABELLE_TMP_PREFIX}-tptpgraph$RANDOM"
111cd6351613 aligned tptp_graph dependencies to Isabelle conventions;
sultana
parents: 47412
diff changeset
   106
  [ ! -d "$WORKDIR" ] && break
47412
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   107
done
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   108
OUTPUT_FILENAME="$(basename "$2")"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   109
FILEDIR="$(cd "$(dirname "$2")"; cd "$(pwd -P)"; pwd)"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   110
FILENAME="${OUTPUT_FILENAME%.*}"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   111
WD="$(pwd)"
47517
6bc4c66d8c1b improved tptp_graph robustness by relying on thy;
sultana
parents: 47473
diff changeset
   112
mkdir -p $WORKDIR
47412
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   113
47517
6bc4c66d8c1b improved tptp_graph robustness by relying on thy;
sultana
parents: 47473
diff changeset
   114
function generate_dot()
6bc4c66d8c1b improved tptp_graph robustness by relying on thy;
sultana
parents: 47473
diff changeset
   115
{
6bc4c66d8c1b improved tptp_graph robustness by relying on thy;
sultana
parents: 47473
diff changeset
   116
  LOADER="tptp_graph_$RANDOM"
6bc4c66d8c1b improved tptp_graph robustness by relying on thy;
sultana
parents: 47473
diff changeset
   117
  echo "theory $LOADER imports \"$TPTP_HOME/TPTP_Parser\" \
53392
a1a45fdc53a3 updated syntax to use 'ML_file' rather than 'uses';
sultana
parents: 53386
diff changeset
   118
begin ML_file \"$TPTP_HOME/TPTP_Parser/tptp_to_dot.ML\" \
a1a45fdc53a3 updated syntax to use 'ML_file' rather than 'uses';
sultana
parents: 53386
diff changeset
   119
ML {* TPTP_To_Dot.write_proof_dot \"$1\" \"$2\" *} end" \
47517
6bc4c66d8c1b improved tptp_graph robustness by relying on thy;
sultana
parents: 47473
diff changeset
   120
        > $WORKDIR/$LOADER.thy
62573
27f90319a499 isabelle.Build uses ML_Process directly;
wenzelm
parents: 62475
diff changeset
   121
  "$ISABELLE_PROCESS" -e "use_thy \"$WORKDIR/$LOADER\";" Pure
47517
6bc4c66d8c1b improved tptp_graph robustness by relying on thy;
sultana
parents: 47473
diff changeset
   122
}
47412
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   123
47517
6bc4c66d8c1b improved tptp_graph robustness by relying on thy;
sultana
parents: 47473
diff changeset
   124
function cleanup_workdir()
6bc4c66d8c1b improved tptp_graph robustness by relying on thy;
sultana
parents: 47473
diff changeset
   125
{
6bc4c66d8c1b improved tptp_graph robustness by relying on thy;
sultana
parents: 47473
diff changeset
   126
  if [ -n "$KEEP_TEMP" ]; then
6bc4c66d8c1b improved tptp_graph robustness by relying on thy;
sultana
parents: 47473
diff changeset
   127
      echo $WORKDIR
6bc4c66d8c1b improved tptp_graph robustness by relying on thy;
sultana
parents: 47473
diff changeset
   128
  else
6bc4c66d8c1b improved tptp_graph robustness by relying on thy;
sultana
parents: 47473
diff changeset
   129
      rm -rf $WORKDIR
6bc4c66d8c1b improved tptp_graph robustness by relying on thy;
sultana
parents: 47473
diff changeset
   130
  fi
6bc4c66d8c1b improved tptp_graph robustness by relying on thy;
sultana
parents: 47473
diff changeset
   131
}
6bc4c66d8c1b improved tptp_graph robustness by relying on thy;
sultana
parents: 47473
diff changeset
   132
6bc4c66d8c1b improved tptp_graph robustness by relying on thy;
sultana
parents: 47473
diff changeset
   133
if [ "$OUTPUT_FORMAT" -eq 0 ]; then
6bc4c66d8c1b improved tptp_graph robustness by relying on thy;
sultana
parents: 47473
diff changeset
   134
  [ -z "$NON_EXEC" ] && generate_dot "$1" "$2"
6bc4c66d8c1b improved tptp_graph robustness by relying on thy;
sultana
parents: 47473
diff changeset
   135
  cleanup_workdir
6bc4c66d8c1b improved tptp_graph robustness by relying on thy;
sultana
parents: 47473
diff changeset
   136
  exit 0
6bc4c66d8c1b improved tptp_graph robustness by relying on thy;
sultana
parents: 47473
diff changeset
   137
fi
6bc4c66d8c1b improved tptp_graph robustness by relying on thy;
sultana
parents: 47473
diff changeset
   138
6bc4c66d8c1b improved tptp_graph robustness by relying on thy;
sultana
parents: 47473
diff changeset
   139
## generate and process files in temporary workdir, then move required
6bc4c66d8c1b improved tptp_graph robustness by relying on thy;
sultana
parents: 47473
diff changeset
   140
## output file to destination dir
6bc4c66d8c1b improved tptp_graph robustness by relying on thy;
sultana
parents: 47473
diff changeset
   141
47412
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   142
[ -z "$NON_EXEC" ] && generate_dot $1 "$WORKDIR/${FILENAME}.dot"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   143
cd $WORKDIR
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   144
if [ -z "$NON_EXEC" ]; then
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   145
  $DOT -Txdot "${FILENAME}.dot" \
53386
16696e649fea corrected syntax filter;
sultana
parents: 47517
diff changeset
   146
  | $DOT2TEX -f pgf -t raw --crop \
16696e649fea corrected syntax filter;
sultana
parents: 47517
diff changeset
   147
  | $PERL -w -p -e 's/_/\\_/g' > "${FILENAME}.tex"
47412
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   148
fi
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   149
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   150
if [ "$OUTPUT_FORMAT" -eq 1 ]; then
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   151
  TARGET=$FILENAME.tex
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   152
else
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   153
  TARGET=$FILENAME.pdf
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   154
  [ -z "$NON_EXEC" ] && $PDFLATEX "${FILENAME}.tex"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   155
fi
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   156
[ -z "$NON_EXEC" ] && mv $TARGET $WD
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   157
cd $WD
47517
6bc4c66d8c1b improved tptp_graph robustness by relying on thy;
sultana
parents: 47473
diff changeset
   158
cleanup_workdir
47412
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   159
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   160
[ -n "$SHOW_TARGET" ] && echo "$FILEDIR/$TARGET"