src/HOL/Tools/Sledgehammer/sledgehammer_util.ML
author blanchet
Mon Jun 02 17:34:26 2014 +0200 (2014-06-02 ago)
changeset 57158 f028d93798e6
parent 57108 dc0b4f50e288
child 57306 ff10067b2248
permissions -rw-r--r--
simplified counterexample handling
blanchet@36062
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_util.ML
blanchet@35963
     2
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@35963
     3
blanchet@35963
     4
General-purpose functions used by the Sledgehammer modules.
blanchet@35963
     5
*)
blanchet@35963
     6
blanchet@35963
     7
signature SLEDGEHAMMER_UTIL =
blanchet@35963
     8
sig
blanchet@48392
     9
  val sledgehammerN : string
blanchet@50608
    10
  val log2 : real -> real
blanchet@55223
    11
  val map3 : ('a -> 'b -> 'c -> 'd) -> 'a list -> 'b list -> 'c list -> 'd list
blanchet@51181
    12
  val app_hd : ('a -> 'a) -> 'a list -> 'a list
blanchet@36142
    13
  val plural_s : int -> string
blanchet@35963
    14
  val serial_commas : string -> string list -> string list
blanchet@38738
    15
  val simplify_spaces : string -> string
blanchet@48656
    16
  val with_cleanup : ('a -> unit) -> ('a -> 'b) -> 'a -> 'b
blanchet@48318
    17
  val time_mult : real -> Time.time -> Time.time
blanchet@35963
    18
  val parse_bool_option : bool -> string -> string -> bool option
blanchet@54816
    19
  val parse_time : string -> string -> Time.time
blanchet@38044
    20
  val subgoal_count : Proof.state -> int
blanchet@38696
    21
  val reserved_isar_keyword_table : unit -> unit Symtab.table
blanchet@54816
    22
  val thms_in_proof : (string Symtab.table * string Symtab.table) option -> thm -> string list
smolkas@50267
    23
  val thms_of_name : Proof.context -> string -> thm list
blanchet@50557
    24
  val one_day : Time.time
blanchet@50557
    25
  val one_year : Time.time
blanchet@50048
    26
  val with_vanilla_print_mode : ('a -> 'b) -> 'a -> 'b
blanchet@53815
    27
  val hackish_string_of_term : Proof.context -> term -> string
blanchet@53815
    28
  val spying : bool -> (unit -> Proof.state * int * string * string) -> unit
blanchet@35963
    29
end;
blanchet@39318
    30
blanchet@35963
    31
structure Sledgehammer_Util : SLEDGEHAMMER_UTIL =
blanchet@35963
    32
struct
blanchet@35963
    33
blanchet@43085
    34
open ATP_Util
blanchet@43085
    35
blanchet@48392
    36
val sledgehammerN = "sledgehammer"
blanchet@48392
    37
blanchet@50608
    38
val log10_2 = Math.log10 2.0
blanchet@50608
    39
fun log2 n = Math.log10 n / log10_2
blanchet@50608
    40
blanchet@55223
    41
fun map3 xs = Ctr_Sugar_Util.map3 xs
blanchet@55223
    42
blanchet@51181
    43
fun app_hd f (x :: xs) = f x :: xs
blanchet@51181
    44
blanchet@36142
    45
fun plural_s n = if n = 1 then "" else "s"
blanchet@36062
    46
blanchet@43029
    47
val serial_commas = Try.serial_commas
blanchet@43085
    48
val simplify_spaces = strip_spaces false (K true)
blanchet@37962
    49
blanchet@48656
    50
fun with_cleanup clean_up f x =
blanchet@48656
    51
  Exn.capture f x
blanchet@48656
    52
  |> tap (fn _ => clean_up x)
blanchet@48656
    53
  |> Exn.release
blanchet@48656
    54
blanchet@55212
    55
fun time_mult k t = Time.fromMilliseconds (Real.ceil (k * Real.fromInt (Time.toMilliseconds t)))
blanchet@48318
    56
blanchet@35963
    57
fun parse_bool_option option name s =
blanchet@35963
    58
  (case s of
wenzelm@51930
    59
     "smart" => if option then NONE else raise Option.Option
blanchet@35963
    60
   | "false" => SOME false
blanchet@35963
    61
   | "true" => SOME true
blanchet@35963
    62
   | "" => SOME true
wenzelm@51930
    63
   | _ => raise Option.Option)
blanchet@35963
    64
  handle Option.Option =>
blanchet@55285
    65
    let val ss = map quote ((option ? cons "smart") ["true", "false"]) in
blanchet@55285
    66
      error ("Parameter " ^ quote name ^ " must be assigned " ^
blanchet@55285
    67
       space_implode " " (serial_commas "or" ss) ^ ".")
blanchet@55285
    68
    end
blanchet@35963
    69
blanchet@40341
    70
val has_junk =
wenzelm@40627
    71
  exists (fn s => not (Symbol.is_digit s) andalso s <> ".") o raw_explode (* FIXME Symbol.explode (?) *)
blanchet@40341
    72
blanchet@54816
    73
fun parse_time name s =
blanchet@54816
    74
  let val secs = if has_junk s then NONE else Real.fromString s in
blanchet@54816
    75
    if is_none secs orelse Real.< (the secs, 0.0) then
blanchet@55285
    76
      error ("Parameter " ^ quote name ^ " must be assigned a nonnegative \number of seconds \
blanchet@55285
    77
        \(e.g., \"60\", \"0.5\") or \"none\".")
blanchet@54816
    78
    else
blanchet@54816
    79
      seconds (the secs)
blanchet@54816
    80
  end
blanchet@35963
    81
blanchet@43029
    82
val subgoal_count = Try.subgoal_count
blanchet@38044
    83
blanchet@38696
    84
fun reserved_isar_keyword_table () =
blanchet@48292
    85
  Keyword.dest () |-> union (op =) |> map (rpair ()) |> Symtab.make
blanchet@38696
    86
blanchet@57108
    87
(* FIXME: Similar yet different code in "mirabelle.ML". The code here has a few fixes that seem to
blanchet@57108
    88
   be missing over there; or maybe the two code portions are not doing the same? *)
blanchet@54116
    89
fun fold_body_thm outer_name (map_plain_name, map_inclass_name) =
blanchet@48251
    90
  let
blanchet@54116
    91
    fun app_thm map_name (_, (name, _, body)) accum =
blanchet@54116
    92
      let
blanchet@54116
    93
        val (anonymous, enter_class) =
blanchet@54116
    94
          (* The "name = outer_name" case caters for the uncommon case where the proved theorem
blanchet@54116
    95
             occurs in its own proof (e.g., "Transitive_Closure.trancl_into_trancl"). *)
blanchet@54116
    96
          if name = "" orelse name = outer_name then (true, false)
blanchet@54116
    97
          else if map_inclass_name name = SOME outer_name then (true, true)
blanchet@54116
    98
          else (false, false)
blanchet@54116
    99
      in
blanchet@54116
   100
        if anonymous then
blanchet@54116
   101
          accum |> app_body (if enter_class then map_inclass_name else map_name) (Future.join body)
blanchet@54116
   102
        else
blanchet@54116
   103
          accum |> union (op =) (the_list (map_name name))
blanchet@54116
   104
      end
blanchet@54116
   105
    and app_body map_name (PBody {thms, ...}) = fold (app_thm map_name) thms
blanchet@57108
   106
  in
blanchet@57108
   107
    app_body map_plain_name
blanchet@57108
   108
  end
blanchet@48251
   109
blanchet@50735
   110
fun thms_in_proof name_tabs th =
blanchet@54116
   111
  let val map_names = (case name_tabs of SOME p => pairself Symtab.lookup p | NONE => `I SOME) in
blanchet@54116
   112
    fold_body_thm (Thm.get_name_hint th) map_names (Proofterm.strip_thm (Thm.proof_body_of th)) []
blanchet@54116
   113
  end
blanchet@48251
   114
smolkas@50267
   115
fun thms_of_name ctxt name =
smolkas@50267
   116
  let
smolkas@50267
   117
    val lex = Keyword.get_lexicons
smolkas@50267
   118
    val get = maps (Proof_Context.get_fact ctxt o fst)
smolkas@50267
   119
  in
smolkas@50267
   120
    Source.of_string name
smolkas@50267
   121
    |> Symbol.source
smolkas@50267
   122
    |> Token.source {do_recover = SOME false} lex Position.start
smolkas@50267
   123
    |> Token.source_proper
smolkas@50267
   124
    |> Source.source Token.stopper (Parse_Spec.xthms1 >> get) NONE
smolkas@50267
   125
    |> Source.exhaust
smolkas@50267
   126
  end
smolkas@50267
   127
blanchet@50557
   128
val one_day = seconds (24.0 * 60.0 * 60.0)
blanchet@50557
   129
val one_year = seconds (365.0 * 24.0 * 60.0 * 60.0)
blanchet@50557
   130
blanchet@50048
   131
fun with_vanilla_print_mode f x =
blanchet@54821
   132
  Print_Mode.setmp (filter (curry (op =) Symbol.xsymbolsN) (print_mode_value ())) f x
blanchet@50048
   133
blanchet@53815
   134
fun hackish_string_of_term ctxt =
blanchet@53815
   135
  with_vanilla_print_mode (Syntax.string_of_term ctxt) #> simplify_spaces
blanchet@53815
   136
blanchet@54062
   137
val spying_version = "c"
blanchet@53815
   138
blanchet@53815
   139
fun spying false _ = ()
blanchet@53815
   140
  | spying true f =
blanchet@53815
   141
    let
blanchet@53815
   142
      val (state, i, tool, message) = f ()
blanchet@53815
   143
      val ctxt = Proof.context_of state
blanchet@53815
   144
      val goal = Logic.get_goal (prop_of (#goal (Proof.goal state))) i
blanchet@53815
   145
      val hash = String.substring (SHA1.rep (SHA1.digest (hackish_string_of_term ctxt goal)), 0, 12)
blanchet@53815
   146
    in
blanchet@53815
   147
      File.append (Path.explode "$ISABELLE_HOME_USER/spy_sledgehammer")
blanchet@53815
   148
        (spying_version ^ " " ^ timestamp () ^ ": " ^ hash ^ ": " ^ tool ^ ": " ^ message ^ "\n")
blanchet@53815
   149
    end
blanchet@53815
   150
blanchet@35963
   151
end;