src/HOL/Tools/Sledgehammer/sledgehammer_util.ML
author blanchet
Tue, 27 Jul 2010 17:43:11 +0200
changeset 38019 e207a64e1e0b
parent 37995 06f02b15ef8a
child 38044 463177795c49
permissions -rw-r--r--
complete renaming of "Sledgehammer_TPTP_Format" to "ATP_Problem"
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36062
194cb6e3c13f get rid of Polyhash, since it's no longer used
blanchet
parents: 35963
diff changeset
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_util.ML
35963
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
     2
    Author:     Jasmin Blanchette, TU Muenchen
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
     3
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
     4
General-purpose functions used by the Sledgehammer modules.
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
     5
*)
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
     6
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
     7
signature SLEDGEHAMMER_UTIL =
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
     8
sig
36142
f5e15e9aae10 make Sledgehammer "minimize" output less confusing + round up (not down) time limits to nearest second
blanchet
parents: 36062
diff changeset
     9
  val plural_s : int -> string
35963
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    10
  val serial_commas : string -> string list -> string list
37962
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37575
diff changeset
    11
  val strip_spaces : string -> string
35963
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    12
  val parse_bool_option : bool -> string -> string -> bool option
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    13
  val parse_time_option : string -> string -> Time.time option
37962
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37575
diff changeset
    14
  val scan_integer : string list -> int * string list
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36478
diff changeset
    15
  val nat_subscript : int -> string
36478
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36402
diff changeset
    16
  val unyxml : string -> string
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36402
diff changeset
    17
  val maybe_quote : string -> string
36555
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
    18
  val monomorphic_term : Type.tyenv -> term -> term
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
    19
  val specialize_type : theory -> (string * typ) -> term -> term
37995
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37962
diff changeset
    20
  val strip_subgoal : thm -> int -> (string * typ) list * term list * term
35963
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    21
end;
36170
0cdb76723c88 added original constant names to Sledgehammer internal terms + output short names if "debug" is set (for increased readability)
blanchet
parents: 36169
diff changeset
    22
 
35963
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    23
structure Sledgehammer_Util : SLEDGEHAMMER_UTIL =
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    24
struct
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    25
36142
f5e15e9aae10 make Sledgehammer "minimize" output less confusing + round up (not down) time limits to nearest second
blanchet
parents: 36062
diff changeset
    26
fun plural_s n = if n = 1 then "" else "s"
36062
194cb6e3c13f get rid of Polyhash, since it's no longer used
blanchet
parents: 35963
diff changeset
    27
35963
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    28
fun serial_commas _ [] = ["??"]
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    29
  | serial_commas _ [s] = [s]
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    30
  | serial_commas conj [s1, s2] = [s1, conj, s2]
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    31
  | serial_commas conj [s1, s2, s3] = [s1 ^ ",", s2 ^ ",", conj, s3]
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    32
  | serial_commas conj (s :: ss) = s ^ "," :: serial_commas conj ss
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    33
37962
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37575
diff changeset
    34
fun is_ident_char c = Char.isAlphaNum c orelse c = #"_"
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37575
diff changeset
    35
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37575
diff changeset
    36
fun strip_spaces_in_list [] = ""
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37575
diff changeset
    37
  | strip_spaces_in_list [c1] = if Char.isSpace c1 then "" else str c1
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37575
diff changeset
    38
  | strip_spaces_in_list [c1, c2] =
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37575
diff changeset
    39
    strip_spaces_in_list [c1] ^ strip_spaces_in_list [c2]
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37575
diff changeset
    40
  | strip_spaces_in_list (c1 :: c2 :: c3 :: cs) =
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37575
diff changeset
    41
    if Char.isSpace c1 then
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37575
diff changeset
    42
      strip_spaces_in_list (c2 :: c3 :: cs)
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37575
diff changeset
    43
    else if Char.isSpace c2 then
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37575
diff changeset
    44
      if Char.isSpace c3 then
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37575
diff changeset
    45
        strip_spaces_in_list (c1 :: c3 :: cs)
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37575
diff changeset
    46
      else
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37575
diff changeset
    47
        str c1 ^ (if forall is_ident_char [c1, c3] then " " else "") ^
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37575
diff changeset
    48
        strip_spaces_in_list (c3 :: cs)
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37575
diff changeset
    49
    else
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37575
diff changeset
    50
      str c1 ^ strip_spaces_in_list (c2 :: c3 :: cs)
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37575
diff changeset
    51
val strip_spaces = strip_spaces_in_list o String.explode
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37575
diff changeset
    52
35963
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    53
fun parse_bool_option option name s =
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    54
  (case s of
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    55
     "smart" => if option then NONE else raise Option
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    56
   | "false" => SOME false
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    57
   | "true" => SOME true
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    58
   | "" => SOME true
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    59
   | _ => raise Option)
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    60
  handle Option.Option =>
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    61
         let val ss = map quote ((option ? cons "smart") ["true", "false"]) in
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    62
           error ("Parameter " ^ quote name ^ " must be assigned " ^
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    63
                  space_implode " " (serial_commas "or" ss) ^ ".")
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    64
         end
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    65
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    66
fun parse_time_option _ "none" = NONE
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    67
  | parse_time_option name s =
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    68
    let
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    69
      val msecs =
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    70
        case space_explode " " s of
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    71
          [s1, "min"] => 60000 * the (Int.fromString s1)
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    72
        | [s1, "s"] => 1000 * the (Int.fromString s1)
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    73
        | [s1, "ms"] => the (Int.fromString s1)
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    74
        | _ => 0
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    75
    in
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    76
      if msecs <= 0 then
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    77
        error ("Parameter " ^ quote name ^ " must be assigned a positive time \
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    78
               \value (e.g., \"60 s\", \"200 ms\") or \"none\".")
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    79
      else
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    80
        SOME (Time.fromMilliseconds msecs)
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    81
    end
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    82
37962
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37575
diff changeset
    83
fun is_head_digit s = Char.isDigit (String.sub (s, 0))
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37575
diff changeset
    84
val scan_integer = Scan.many1 is_head_digit >> (the o Int.fromString o implode)
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37575
diff changeset
    85
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36478
diff changeset
    86
val subscript = implode o map (prefix "\<^isub>") o explode
37321
9d7cfae95b30 honor "xsymbols"
blanchet
parents: 36960
diff changeset
    87
fun nat_subscript n =
9d7cfae95b30 honor "xsymbols"
blanchet
parents: 36960
diff changeset
    88
  n |> string_of_int |> print_mode_active Symbol.xsymbolsN ? subscript
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36478
diff changeset
    89
36478
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36402
diff changeset
    90
fun plain_string_from_xml_tree t =
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36402
diff changeset
    91
  Buffer.empty |> XML.add_content t |> Buffer.content
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36402
diff changeset
    92
val unyxml = plain_string_from_xml_tree o YXML.parse
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36402
diff changeset
    93
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36402
diff changeset
    94
val is_long_identifier = forall Syntax.is_identifier o space_explode "."
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36402
diff changeset
    95
fun maybe_quote y =
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36402
diff changeset
    96
  let val s = unyxml y in
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36402
diff changeset
    97
    y |> ((not (is_long_identifier (perhaps (try (unprefix "'")) s)) andalso
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36402
diff changeset
    98
           not (is_long_identifier (perhaps (try (unprefix "?")) s))) orelse
36960
01594f816e3a prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
wenzelm
parents: 36555
diff changeset
    99
           Keyword.is_keyword s) ? quote
36478
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36402
diff changeset
   100
  end
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36402
diff changeset
   101
36555
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   102
fun monomorphic_term subst t =
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   103
  map_types (map_type_tvar (fn v =>
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   104
      case Type.lookup subst v of
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   105
        SOME typ => typ
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   106
      | NONE => raise TERM ("monomorphic_term: uninstanitated schematic type \
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   107
                            \variable", [t]))) t
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   108
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   109
fun specialize_type thy (s, T) t =
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   110
  let
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   111
    fun subst_for (Const (s', T')) =
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   112
      if s = s' then
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   113
        SOME (Sign.typ_match thy (T', T) Vartab.empty)
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   114
        handle Type.TYPE_MATCH => NONE
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   115
      else
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   116
        NONE
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   117
    | subst_for (t1 $ t2) =
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   118
      (case subst_for t1 of SOME x => SOME x | NONE => subst_for t2)
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   119
    | subst_for (Abs (_, _, t')) = subst_for t'
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   120
    | subst_for _ = NONE
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   121
  in
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   122
    case subst_for t of
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   123
      SOME subst => monomorphic_term subst t
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   124
    | NONE => raise Type.TYPE_MATCH
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   125
  end
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   126
37995
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37962
diff changeset
   127
fun strip_subgoal goal i =
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37962
diff changeset
   128
  let
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37962
diff changeset
   129
    val (t, frees) = Logic.goal_params (prop_of goal) i
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37962
diff changeset
   130
    val hyp_ts = t |> Logic.strip_assums_hyp |> map (curry subst_bounds frees)
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37962
diff changeset
   131
    val concl_t = t |> Logic.strip_assums_concl |> curry subst_bounds frees
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37962
diff changeset
   132
  in (rev (map dest_Free frees), hyp_ts, concl_t) end
36555
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   133
35963
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
   134
end;