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