src/HOL/Tools/Sledgehammer/sledgehammer_util.ML
author haftmann
Sat, 19 Jun 2010 09:50:30 +0200
changeset 37470 fcc768dc9dd0
parent 37414 d0cea0796295
child 37498 b426cbdb5a23
permissions -rw-r--r--
more binding; avoid arcane Rep and Abs prefixes
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
36378
f32c567dbcaa remove some bloat
blanchet
parents: 36183
diff changeset
     9
  val pairf : ('a -> 'b) -> ('a -> 'c) -> 'a -> 'b * 'c
36142
f5e15e9aae10 make Sledgehammer "minimize" output less confusing + round up (not down) time limits to nearest second
blanchet
parents: 36062
diff changeset
    10
  val plural_s : int -> string
35963
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    11
  val serial_commas : string -> string list -> string list
36169
27b1cc58715e store nonmangled names along with mangled type names in Sledgehammer for debugging purposes
blanchet
parents: 36142
diff changeset
    12
  val replace_all : string -> string -> string -> string
27b1cc58715e store nonmangled names along with mangled type names in Sledgehammer for debugging purposes
blanchet
parents: 36142
diff changeset
    13
  val remove_all : string -> string -> string
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
    14
  val timestamp : unit -> string
35963
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    15
  val parse_bool_option : bool -> string -> string -> bool option
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    16
  val parse_time_option : string -> string -> Time.time option
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36478
diff changeset
    17
  val nat_subscript : int -> string
36478
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36402
diff changeset
    18
  val unyxml : string -> string
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36402
diff changeset
    19
  val maybe_quote : string -> string
36555
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
    20
  val monomorphic_term : Type.tyenv -> term -> term
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
    21
  val specialize_type : theory -> (string * typ) -> term -> term
35963
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    22
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
    23
 
35963
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    24
structure Sledgehammer_Util : SLEDGEHAMMER_UTIL =
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    25
struct
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    26
36378
f32c567dbcaa remove some bloat
blanchet
parents: 36183
diff changeset
    27
fun pairf f g x = (f x, g x)
f32c567dbcaa remove some bloat
blanchet
parents: 36183
diff changeset
    28
36142
f5e15e9aae10 make Sledgehammer "minimize" output less confusing + round up (not down) time limits to nearest second
blanchet
parents: 36062
diff changeset
    29
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
    30
35963
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    31
fun serial_commas _ [] = ["??"]
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    32
  | serial_commas _ [s] = [s]
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    33
  | serial_commas conj [s1, s2] = [s1, conj, s2]
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    34
  | serial_commas conj [s1, s2, s3] = [s1 ^ ",", s2 ^ ",", conj, s3]
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    35
  | serial_commas conj (s :: ss) = s ^ "," :: serial_commas conj ss
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    36
36169
27b1cc58715e store nonmangled names along with mangled type names in Sledgehammer for debugging purposes
blanchet
parents: 36142
diff changeset
    37
fun replace_all bef aft =
27b1cc58715e store nonmangled names along with mangled type names in Sledgehammer for debugging purposes
blanchet
parents: 36142
diff changeset
    38
  let
27b1cc58715e store nonmangled names along with mangled type names in Sledgehammer for debugging purposes
blanchet
parents: 36142
diff changeset
    39
    fun aux seen "" = String.implode (rev seen)
27b1cc58715e store nonmangled names along with mangled type names in Sledgehammer for debugging purposes
blanchet
parents: 36142
diff changeset
    40
      | aux seen s =
36183
f723348b2231 Sledgehammer: the empty set of fact () should mean nothing, not unchanged
blanchet
parents: 36170
diff changeset
    41
        if String.isPrefix bef s then
36169
27b1cc58715e store nonmangled names along with mangled type names in Sledgehammer for debugging purposes
blanchet
parents: 36142
diff changeset
    42
          aux seen "" ^ aft ^ aux [] (unprefix bef s)
27b1cc58715e store nonmangled names along with mangled type names in Sledgehammer for debugging purposes
blanchet
parents: 36142
diff changeset
    43
        else
27b1cc58715e store nonmangled names along with mangled type names in Sledgehammer for debugging purposes
blanchet
parents: 36142
diff changeset
    44
          aux (String.sub (s, 0) :: seen) (String.extract (s, 1, NONE))
27b1cc58715e store nonmangled names along with mangled type names in Sledgehammer for debugging purposes
blanchet
parents: 36142
diff changeset
    45
  in aux [] end
27b1cc58715e store nonmangled names along with mangled type names in Sledgehammer for debugging purposes
blanchet
parents: 36142
diff changeset
    46
fun remove_all bef = replace_all bef ""
27b1cc58715e store nonmangled names along with mangled type names in Sledgehammer for debugging purposes
blanchet
parents: 36142
diff changeset
    47
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
    48
val timestamp = Date.fmt "%Y-%m-%d %H:%M:%S" o Date.fromTimeLocal o Time.now
0cdb76723c88 added original constant names to Sledgehammer internal terms + output short names if "debug" is set (for increased readability)
blanchet
parents: 36169
diff changeset
    49
35963
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    50
fun parse_bool_option option name s =
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    51
  (case s of
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    52
     "smart" => if option then NONE else raise Option
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    53
   | "false" => SOME false
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    54
   | "true" => SOME true
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    55
   | "" => SOME true
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    56
   | _ => raise Option)
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    57
  handle Option.Option =>
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    58
         let val ss = map quote ((option ? cons "smart") ["true", "false"]) in
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    59
           error ("Parameter " ^ quote name ^ " must be assigned " ^
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    60
                  space_implode " " (serial_commas "or" ss) ^ ".")
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    61
         end
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    62
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    63
fun parse_time_option _ "none" = NONE
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    64
  | parse_time_option name s =
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    65
    let
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    66
      val msecs =
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    67
        case space_explode " " s of
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    68
          [s1, "min"] => 60000 * the (Int.fromString s1)
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    69
        | [s1, "s"] => 1000 * the (Int.fromString s1)
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    70
        | [s1, "ms"] => the (Int.fromString s1)
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    71
        | _ => 0
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    72
    in
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    73
      if msecs <= 0 then
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    74
        error ("Parameter " ^ quote name ^ " must be assigned a positive time \
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    75
               \value (e.g., \"60 s\", \"200 ms\") or \"none\".")
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    76
      else
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    77
        SOME (Time.fromMilliseconds msecs)
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    78
    end
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
    79
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36478
diff changeset
    80
val subscript = implode o map (prefix "\<^isub>") o explode
37321
9d7cfae95b30 honor "xsymbols"
blanchet
parents: 36960
diff changeset
    81
fun nat_subscript n =
9d7cfae95b30 honor "xsymbols"
blanchet
parents: 36960
diff changeset
    82
  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
    83
36478
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36402
diff changeset
    84
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
    85
  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
    86
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
    87
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36402
diff changeset
    88
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
    89
fun maybe_quote y =
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36402
diff changeset
    90
  let val s = unyxml y in
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36402
diff changeset
    91
    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
    92
           not (is_long_identifier (perhaps (try (unprefix "?")) s))) orelse
36960
01594f816e3a prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
wenzelm
parents: 36555
diff changeset
    93
           Keyword.is_keyword s) ? quote
36478
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36402
diff changeset
    94
  end
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36402
diff changeset
    95
36555
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
    96
fun monomorphic_term subst t =
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
    97
  map_types (map_type_tvar (fn v =>
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
    98
      case Type.lookup subst v of
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
    99
        SOME typ => typ
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   100
      | NONE => raise TERM ("monomorphic_term: uninstanitated schematic type \
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   101
                            \variable", [t]))) t
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   102
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   103
fun specialize_type thy (s, T) t =
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   104
  let
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   105
    fun subst_for (Const (s', T')) =
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   106
      if s = s' then
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   107
        SOME (Sign.typ_match thy (T', T) Vartab.empty)
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   108
        handle Type.TYPE_MATCH => NONE
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   109
      else
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   110
        NONE
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   111
    | subst_for (t1 $ t2) =
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   112
      (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
   113
    | subst_for (Abs (_, _, t')) = subst_for t'
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   114
    | subst_for _ = NONE
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   115
  in
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   116
    case subst_for t of
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   117
      SOME subst => monomorphic_term subst t
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   118
    | NONE => raise Type.TYPE_MATCH
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   119
  end
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   120
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36496
diff changeset
   121
35963
943e2582dc87 added options to Sledgehammer;
blanchet
parents:
diff changeset
   122
end;