src/HOL/TPTP/TPTP_Test.thy
author haftmann
Thu, 19 Jun 2025 17:15:40 +0200
changeset 82734 89347c0cc6a3
parent 78727 1b052426a2b7
permissions -rw-r--r--
treat map_filter similar to list_all, list_ex, list_ex1
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
63167
0909deb8059b isabelle update_cartouches -c -t;
wenzelm
parents: 62505
diff changeset
    12
ML \<open>
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)
63167
0909deb8059b isabelle update_cartouches -c -t;
wenzelm
parents: 62505
diff changeset
    20
\<close>
47512
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
63167
0909deb8059b isabelle update_cartouches -c -t;
wenzelm
parents: 62505
diff changeset
    24
ML \<open>
47512
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
63167
0909deb8059b isabelle update_cartouches -c -t;
wenzelm
parents: 62505
diff changeset
    27
\<close>
47512
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"
63167
0909deb8059b isabelle update_cartouches -c -t;
wenzelm
parents: 62505
diff changeset
    31
ML \<open>
47512
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;
63167
0909deb8059b isabelle update_cartouches -c -t;
wenzelm
parents: 62505
diff changeset
    36
\<close>
47512
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    37
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    38
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    39
section "Supporting test functions"
63167
0909deb8059b isabelle update_cartouches -c -t;
wenzelm
parents: 62505
diff changeset
    40
ML \<open>
47512
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    41
  fun report ctxt str =
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    42
    let
47518
b2f209258621 more cleaning of tptp tests;
sultana
parents: 47512
diff changeset
    43
      val tptp_test_out = Config.get ctxt tptp_test_out
47512
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    44
    in
47518
b2f209258621 more cleaning of tptp tests;
sultana
parents: 47512
diff changeset
    45
      if tptp_test_out = "" then warning str
47512
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    46
      else
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    47
        let
47518
b2f209258621 more cleaning of tptp tests;
sultana
parents: 47512
diff changeset
    48
          val out_stream = TextIO.openAppend tptp_test_out
47512
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    49
        in (TextIO.output (out_stream, str ^ "\n");
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    50
            TextIO.flushOut out_stream;
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    51
            TextIO.closeOut out_stream)
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    52
        end
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    53
    end
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    54
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    55
  fun test_fn ctxt f msg default_val file_name =
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    56
    let
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    57
      val _ = TPTP_Syntax.debug tracing (msg ^ " " ^ Path.print file_name)
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    58
    in
78727
1b052426a2b7 avoid accidental 'handle' of interrupts;
wenzelm
parents: 72511
diff changeset
    59
     \<^try>\<open>(f file_name; ()) catch exn =>
47512
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    60
     (*otherwise report exceptions as warnings*)
78727
1b052426a2b7 avoid accidental 'handle' of interrupts;
wenzelm
parents: 72511
diff changeset
    61
       (report ctxt (msg ^ " test: file " ^ Path.print file_name ^
1b052426a2b7 avoid accidental 'handle' of interrupts;
wenzelm
parents: 72511
diff changeset
    62
        " raised exception: " ^ Runtime.exn_message exn);
1b052426a2b7 avoid accidental 'handle' of interrupts;
wenzelm
parents: 72511
diff changeset
    63
        default_val)\<close>
47512
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    64
    end
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    65
55595
2e2e9bc7c4c6 made list of test files a parameter to timed_test in TPTP_Test.thy, and updated dependent definitions;
sultana
parents: 47687
diff changeset
    66
  fun timed_test ctxt f test_files =
47512
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    67
    let
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    68
      fun f' x = (f x; ())
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    69
      val time =
55595
2e2e9bc7c4c6 made list of test files a parameter to timed_test in TPTP_Test.thy, and updated dependent definitions;
sultana
parents: 47687
diff changeset
    70
        Timing.timing (List.app f') test_files
47512
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    71
        |> fst
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    72
      val duration =
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    73
        #elapsed time
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    74
        |> Time.toSeconds
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    75
        |> Real.fromLargeInt
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    76
      val average =
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    77
        (StringCvt.FIX (SOME 3),
55595
2e2e9bc7c4c6 made list of test files a parameter to timed_test in TPTP_Test.thy, and updated dependent definitions;
sultana
parents: 47687
diff changeset
    78
         (duration / Real.fromInt (length test_files)))
47512
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    79
        |-> Real.fmt
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    80
    in
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    81
      report ctxt ("Test timing: " ^ Timing.message time ^ "\n(about " ^ average ^
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    82
       "s per problem)")
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    83
    end
63167
0909deb8059b isabelle update_cartouches -c -t;
wenzelm
parents: 62505
diff changeset
    84
\<close>
47512
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    85
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    86
63167
0909deb8059b isabelle update_cartouches -c -t;
wenzelm
parents: 62505
diff changeset
    87
ML \<open>
72511
460d743010bc clarified signature: overloaded "+" for Path.append;
wenzelm
parents: 63167
diff changeset
    88
  fun situate file_name = tptp_probs_dir + Path.explode file_name;
47547
1a5dc8377b5c more tptp testing support functions;
sultana
parents: 47518
diff changeset
    89
47512
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    90
  fun parser_test ctxt = (*FIXME argument order*)
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    91
    test_fn ctxt
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    92
     (fn file_name =>
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    93
        Path.implode file_name
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    94
        |> (fn file =>
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    95
             ((*report ctxt file; this is if you want the filename in the log*)
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    96
              TPTP_Parser.parse_file file)))
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    97
     "parser"
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
    98
     ()
47547
1a5dc8377b5c more tptp testing support functions;
sultana
parents: 47518
diff changeset
    99
1a5dc8377b5c more tptp testing support functions;
sultana
parents: 47518
diff changeset
   100
  fun parse_timed file =
1a5dc8377b5c more tptp testing support functions;
sultana
parents: 47518
diff changeset
   101
    Timing.timing TPTP_Parser.parse_file (Path.implode file)
63167
0909deb8059b isabelle update_cartouches -c -t;
wenzelm
parents: 62505
diff changeset
   102
\<close>
47512
b381d428a725 reorganised tptp testing thys
sultana
parents:
diff changeset
   103
62390
842917225d56 more canonical names
nipkow
parents: 56303
diff changeset
   104
end