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