src/HOL/TPTP/TPTP_Test.thy
author huffman
Sat, 21 Apr 2012 10:59:52 +0200
changeset 47647 ec29cc09599d
parent 47558 55b42f9af99d
child 47687 bfbd2d0bb348
permissions -rw-r--r--
renamed contravariant relator set_rel to vset_rel, to make room for new covariant relator
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47512
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
     1
(*  Title:      HOL/TPTP/TPTP_Test.thy
47518
b2f209258621 more cleaning of tptp tests;
sultana
parents: 47512
diff changeset
     2
    Author:     Nik Sultana, Cambridge University Computer Laboratory
47512
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
     3
47558
55b42f9af99d phase out "$TPTP_PROBLEMS_PATH"; prefer "$TPTP" for consistency with CASC setup
blanchet
parents: 47547
diff changeset
     4
Some test support for the TPTP interface. Some of the tests rely on the Isabelle
55b42f9af99d phase out "$TPTP_PROBLEMS_PATH"; prefer "$TPTP" for consistency with CASC setup
blanchet
parents: 47547
diff changeset
     5
environment variable $TPTP, which should point to the TPTP-vX.Y.Z directory.
47512
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
     6
*)
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
     7
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
     8
theory TPTP_Test
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
     9
imports TPTP_Parser
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    10
begin
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    11
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    12
ML {*
47518
b2f209258621 more cleaning of tptp tests;
sultana
parents: 47512
diff changeset
    13
  val tptp_test_out = Attrib.setup_config_string @{binding "tptp_test_out"} (K "")
b2f209258621 more cleaning of tptp tests;
sultana
parents: 47512
diff changeset
    14
  val tptp_test_all = Attrib.setup_config_bool @{binding "tptp_test_all"} (K false)
b2f209258621 more cleaning of tptp tests;
sultana
parents: 47512
diff changeset
    15
  val tptp_test_timeout =
b2f209258621 more cleaning of tptp tests;
sultana
parents: 47512
diff changeset
    16
    Attrib.setup_config_int @{binding "tptp_test_timeout"} (K 5)
b2f209258621 more cleaning of tptp tests;
sultana
parents: 47512
diff changeset
    17
  fun test_all ctxt = Config.get ctxt tptp_test_all
b2f209258621 more cleaning of tptp tests;
sultana
parents: 47512
diff changeset
    18
  fun get_timeout ctxt = Config.get ctxt tptp_test_timeout
47512
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    19
  fun S x y z = x z (y z)
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    20
*}
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    21
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    22
section "Parser tests"
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    23
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    24
ML {*
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    25
  fun payload_of (TPTP_Syntax.Annotated_Formula (_, _, _, _, fmla, _)) = fmla
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    26
  val payloads_of = map payload_of
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    27
*}
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    28
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    29
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    30
section "Source problems"
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    31
ML {*
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    32
  (*problem source*)
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    33
  val tptp_probs_dir =
47558
55b42f9af99d phase out "$TPTP_PROBLEMS_PATH"; prefer "$TPTP" for consistency with CASC setup
blanchet
parents: 47547
diff changeset
    34
    Path.explode "$TPTP/Problems"
47512
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    35
    |> Path.expand;
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    36
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    37
  (*list of files to under test*)
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    38
  val files = TPTP_Syntax.get_file_list tptp_probs_dir;
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    39
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    40
(*  (*test problem-name parsing and mangling*)
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    41
  val problem_names =
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    42
    map (Path.base #>
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    43
         Path.implode #>
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    44
         TPTP_Problem_Name.parse_problem_name #>
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    45
         TPTP_Problem_Name.mangle_problem_name)
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    46
        files*)
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    47
*}
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    48
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    49
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    50
section "Supporting test functions"
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    51
ML {*
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    52
  fun report ctxt str =
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    53
    let
47518
b2f209258621 more cleaning of tptp tests;
sultana
parents: 47512
diff changeset
    54
      val tptp_test_out = Config.get ctxt tptp_test_out
47512
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    55
    in
47518
b2f209258621 more cleaning of tptp tests;
sultana
parents: 47512
diff changeset
    56
      if tptp_test_out = "" then warning str
47512
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    57
      else
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    58
        let
47518
b2f209258621 more cleaning of tptp tests;
sultana
parents: 47512
diff changeset
    59
          val out_stream = TextIO.openAppend tptp_test_out
47512
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    60
        in (TextIO.output (out_stream, str ^ "\n");
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    61
            TextIO.flushOut out_stream;
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    62
            TextIO.closeOut out_stream)
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    63
        end
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    64
    end
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    65
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    66
  fun test_fn ctxt f msg default_val file_name =
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    67
    let
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    68
      val _ = TPTP_Syntax.debug tracing (msg ^ " " ^ Path.print file_name)
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    69
    in
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    70
     (f file_name; ())
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    71
     (*otherwise report exceptions as warnings*)
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    72
     handle exn =>
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    73
       if Exn.is_interrupt exn then
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    74
         reraise exn
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    75
       else
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    76
         (report ctxt (msg ^ " test: file " ^ Path.print file_name ^
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    77
          " raised exception: " ^ ML_Compiler.exn_message exn);
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    78
          default_val)
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    79
    end
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    80
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    81
  fun timed_test ctxt f =
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    82
    let
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    83
      fun f' x = (f x; ())
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    84
      val time =
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    85
        Timing.timing (List.app f') files
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    86
        |> fst
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    87
      val duration =
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    88
        #elapsed time
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    89
        |> Time.toSeconds
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    90
        |> Real.fromLargeInt
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    91
      val average =
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    92
        (StringCvt.FIX (SOME 3),
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    93
         (duration / Real.fromInt (length files)))
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    94
        |-> Real.fmt
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    95
    in
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    96
      report ctxt ("Test timing: " ^ Timing.message time ^ "\n(about " ^ average ^
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    97
       "s per problem)")
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    98
    end
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    99
*}
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
   100
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
   101
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
   102
ML {*
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
   103
  fun situate file_name = Path.append tptp_probs_dir (Path.explode file_name);
47547
1a5dc8377b5c more tptp testing support functions;
sultana
parents: 47518
diff changeset
   104
47512
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
   105
  fun parser_test ctxt = (*FIXME argument order*)
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
   106
    test_fn ctxt
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
   107
     (fn file_name =>
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
   108
        Path.implode file_name
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
   109
        |> (fn file =>
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
   110
             ((*report ctxt file; this is if you want the filename in the log*)
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
   111
              TPTP_Parser.parse_file file)))
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
   112
     "parser"
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
   113
     ()
47547
1a5dc8377b5c more tptp testing support functions;
sultana
parents: 47518
diff changeset
   114
1a5dc8377b5c more tptp testing support functions;
sultana
parents: 47518
diff changeset
   115
  fun parse_timed file =
1a5dc8377b5c more tptp testing support functions;
sultana
parents: 47518
diff changeset
   116
    Timing.timing TPTP_Parser.parse_file (Path.implode file)
47512
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
   117
*}
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
   118
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
   119
end