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