src/HOL/Tools/Sledgehammer/sledgehammer_filter_mash.ML
author blanchet
Wed, 18 Jul 2012 08:44:04 +0200
changeset 49327 b40722a81ac9
parent 49326 3c4e10606567
child 49328 0faafdffa662
permissions -rw-r--r--
implemented meshing of Iter and MaSh results
blanchet@49263
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_filter_mash.ML
blanchet@49263
     2
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@49263
     3
blanchet@49263
     4
Sledgehammer's machine-learning-based relevance filter (MaSh).
blanchet@49263
     5
*)
blanchet@49263
     6
blanchet@49263
     7
signature SLEDGEHAMMER_FILTER_MASH =
blanchet@49263
     8
sig
blanchet@49266
     9
  type status = ATP_Problem_Generate.status
blanchet@49266
    10
  type stature = ATP_Problem_Generate.stature
blanchet@49311
    11
  type fact = Sledgehammer_Fact.fact
blanchet@49311
    12
  type fact_override = Sledgehammer_Fact.fact_override
blanchet@49266
    13
  type params = Sledgehammer_Provers.params
blanchet@49303
    14
  type relevance_fudge = Sledgehammer_Provers.relevance_fudge
blanchet@49266
    15
  type prover_result = Sledgehammer_Provers.prover_result
blanchet@49266
    16
blanchet@49323
    17
  val trace : bool Config.T
blanchet@49318
    18
  val escape_meta : string -> string
blanchet@49318
    19
  val escape_metas : string list -> string
blanchet@49323
    20
  val unescape_meta : string -> string
blanchet@49323
    21
  val unescape_metas : string -> string list
blanchet@49326
    22
  val extract_query : string -> string * string list
blanchet@49327
    23
  val suggested_facts : string list -> fact list -> fact list
blanchet@49327
    24
  val mesh_facts : int -> fact list -> fact list -> fact list -> fact list
blanchet@49314
    25
  val all_non_tautological_facts_of :
blanchet@49314
    26
    theory -> status Termtab.table -> fact list
blanchet@49266
    27
  val theory_ord : theory * theory -> order
blanchet@49266
    28
  val thm_ord : thm * thm -> order
blanchet@49327
    29
  val thy_facts_from_thms : fact list -> string list Symtab.table
blanchet@49266
    30
  val has_thy : theory -> thm -> bool
blanchet@49318
    31
  val parent_facts : theory -> string list Symtab.table -> string list
blanchet@49317
    32
  val features_of : theory -> status -> term list -> string list
blanchet@49266
    33
  val isabelle_dependencies_of : string list -> thm -> string list
blanchet@49266
    34
  val goal_of_thm : theory -> thm -> thm
blanchet@49311
    35
  val run_prover : Proof.context -> params -> fact list -> thm -> prover_result
blanchet@49319
    36
  val thy_name_of_fact : string -> string
blanchet@49323
    37
  val mash_RESET : Proof.context -> unit
blanchet@49323
    38
  val mash_ADD :
blanchet@49323
    39
    Proof.context -> (string * string list * string list * string list) list
blanchet@49323
    40
    -> unit
blanchet@49323
    41
  val mash_DEL : Proof.context -> string list -> string list -> unit
blanchet@49326
    42
  val mash_QUERY : Proof.context -> string list * string list -> string list
blanchet@49323
    43
  val mash_reset : Proof.context -> unit
blanchet@49323
    44
  val mash_can_suggest_facts : Proof.context -> bool
blanchet@49313
    45
  val mash_suggest_facts :
blanchet@49326
    46
    Proof.context -> params -> string -> term list -> term -> fact list
blanchet@49326
    47
    -> fact list
blanchet@49323
    48
  val mash_can_learn_thy : Proof.context -> theory -> bool
blanchet@49323
    49
  val mash_learn_thy : Proof.context -> theory -> real -> unit
blanchet@49324
    50
  val mash_learn_proof : Proof.context -> theory -> term -> thm list -> unit
blanchet@49303
    51
  val relevant_facts :
blanchet@49307
    52
    Proof.context -> params -> string -> int -> fact_override -> term list
blanchet@49311
    53
    -> term -> fact list -> fact list
blanchet@49263
    54
end;
blanchet@49263
    55
blanchet@49263
    56
structure Sledgehammer_Filter_MaSh : SLEDGEHAMMER_FILTER_MASH =
blanchet@49263
    57
struct
blanchet@49264
    58
blanchet@49266
    59
open ATP_Util
blanchet@49266
    60
open ATP_Problem_Generate
blanchet@49266
    61
open Sledgehammer_Util
blanchet@49266
    62
open Sledgehammer_Fact
blanchet@49266
    63
open Sledgehammer_Filter_Iter
blanchet@49266
    64
open Sledgehammer_Provers
blanchet@49266
    65
blanchet@49323
    66
val trace =
blanchet@49323
    67
  Attrib.setup_config_bool @{binding sledgehammer_filter_mash_trace} (K false)
blanchet@49323
    68
fun trace_msg ctxt msg = if Config.get ctxt trace then tracing (msg ()) else ()
blanchet@49323
    69
blanchet@49324
    70
fun mash_dir () =
blanchet@49324
    71
  getenv "ISABELLE_HOME_USER" ^ "/mash"
blanchet@49324
    72
  |> tap (fn dir => Isabelle_System.mkdir (Path.explode dir))
blanchet@49324
    73
fun mash_state_path () = mash_dir () ^ "/state" |> Path.explode
blanchet@49266
    74
blanchet@49266
    75
(*** Isabelle helpers ***)
blanchet@49266
    76
blanchet@49323
    77
fun meta_char c =
blanchet@49266
    78
  if Char.isAlphaNum c orelse c = #"_" orelse c = #"." orelse c = #"(" orelse
blanchet@49266
    79
     c = #")" orelse c = #"," then
blanchet@49266
    80
    String.str c
blanchet@49266
    81
  else
blanchet@49266
    82
    (* fixed width, in case more digits follow *)
blanchet@49266
    83
    "\\" ^ stringN_of_int 3 (Char.ord c)
blanchet@49266
    84
blanchet@49323
    85
fun unmeta_chars accum [] = String.implode (rev accum)
blanchet@49323
    86
  | unmeta_chars accum (#"\\" :: d1 :: d2 :: d3 :: cs) =
blanchet@49323
    87
    (case Int.fromString (String.implode [d1, d2, d3]) of
blanchet@49323
    88
       SOME n => unmeta_chars (Char.chr n :: accum) cs
blanchet@49323
    89
     | NONE => "" (* error *))
blanchet@49323
    90
  | unmeta_chars _ (#"\\" :: _) = "" (* error *)
blanchet@49323
    91
  | unmeta_chars accum (c :: cs) = unmeta_chars (c :: accum) cs
blanchet@49323
    92
blanchet@49323
    93
val escape_meta = String.translate meta_char
blanchet@49318
    94
val escape_metas = map escape_meta #> space_implode " "
blanchet@49323
    95
val unescape_meta = unmeta_chars [] o String.explode
blanchet@49323
    96
val unescape_metas = map unescape_meta o space_explode " "
blanchet@49266
    97
blanchet@49326
    98
val explode_suggs =
blanchet@49326
    99
  space_explode " " #> filter_out (curry (op =) "") #> map unescape_meta
blanchet@49326
   100
fun extract_query line =
blanchet@49326
   101
  case space_explode ":" line of
blanchet@49326
   102
    [goal_name, suggs] => (unescape_meta goal_name, explode_suggs suggs)
blanchet@49327
   103
  | _ => ("", [])
blanchet@49326
   104
blanchet@49326
   105
fun find_suggested facts sugg =
blanchet@49326
   106
  find_first (fn (_, th) => Thm.get_name_hint th = sugg) facts
blanchet@49326
   107
fun suggested_facts suggs facts = map_filter (find_suggested facts) suggs
blanchet@49326
   108
blanchet@49327
   109
fun mesh_facts max_facts iter_facts mash_facts mash_rejects =
blanchet@49327
   110
  let
blanchet@49327
   111
    val fact_eq = Thm.eq_thm o pairself snd
blanchet@49327
   112
    val num_iter = length iter_facts
blanchet@49327
   113
    val num_mash = length mash_facts
blanchet@49327
   114
    fun score_in f fs n =
blanchet@49327
   115
      case find_index (curry fact_eq f) fs of
blanchet@49327
   116
        ~1 => length fs
blanchet@49327
   117
      | j => j
blanchet@49327
   118
    fun score_of fact =
blanchet@49327
   119
      score_in fact iter_facts num_iter + score_in fact mash_facts num_mash
blanchet@49327
   120
  in
blanchet@49327
   121
    union fact_eq iter_facts mash_facts
blanchet@49327
   122
    |> map (`score_of) |> sort (int_ord o pairself fst) |> map snd
blanchet@49327
   123
    |> take max_facts
blanchet@49327
   124
  end
blanchet@49327
   125
blanchet@49318
   126
val thy_feature_prefix = "y_"
blanchet@49266
   127
blanchet@49318
   128
val thy_feature_name_of = prefix thy_feature_prefix
blanchet@49318
   129
val const_name_of = prefix const_prefix
blanchet@49318
   130
val type_name_of = prefix type_const_prefix
blanchet@49318
   131
val class_name_of = prefix class_prefix
blanchet@49266
   132
blanchet@49266
   133
local
blanchet@49266
   134
blanchet@49266
   135
fun has_bool @{typ bool} = true
blanchet@49266
   136
  | has_bool (Type (_, Ts)) = exists has_bool Ts
blanchet@49266
   137
  | has_bool _ = false
blanchet@49266
   138
blanchet@49266
   139
fun has_fun (Type (@{type_name fun}, _)) = true
blanchet@49266
   140
  | has_fun (Type (_, Ts)) = exists has_fun Ts
blanchet@49266
   141
  | has_fun _ = false
blanchet@49266
   142
blanchet@49266
   143
val is_conn = member (op =)
blanchet@49266
   144
  [@{const_name Trueprop}, @{const_name HOL.conj}, @{const_name HOL.disj},
blanchet@49266
   145
   @{const_name HOL.implies}, @{const_name Not},
blanchet@49266
   146
   @{const_name All}, @{const_name Ex}, @{const_name Ball}, @{const_name Bex},
blanchet@49266
   147
   @{const_name HOL.eq}]
blanchet@49266
   148
blanchet@49266
   149
val has_bool_arg_const =
blanchet@49266
   150
  exists_Const (fn (c, T) =>
blanchet@49266
   151
                   not (is_conn c) andalso exists has_bool (binder_types T))
blanchet@49266
   152
blanchet@49266
   153
fun higher_inst_const thy (c, T) =
blanchet@49266
   154
  case binder_types T of
blanchet@49266
   155
    [] => false
blanchet@49266
   156
  | Ts => length (binder_types (Sign.the_const_type thy c)) <> length Ts
blanchet@49266
   157
blanchet@49266
   158
val binders = [@{const_name All}, @{const_name Ex}]
blanchet@49266
   159
blanchet@49266
   160
in
blanchet@49266
   161
blanchet@49266
   162
fun is_fo_term thy t =
blanchet@49266
   163
  let
blanchet@49266
   164
    val t =
blanchet@49266
   165
      t |> Envir.beta_eta_contract
blanchet@49266
   166
        |> transform_elim_prop
blanchet@49266
   167
        |> Object_Logic.atomize_term thy
blanchet@49266
   168
  in
blanchet@49266
   169
    Term.is_first_order binders t andalso
blanchet@49266
   170
    not (exists_subterm (fn Var (_, T) => has_bool T orelse has_fun T
blanchet@49266
   171
                          | _ => false) t orelse
blanchet@49266
   172
         has_bool_arg_const t orelse exists_Const (higher_inst_const thy) t)
blanchet@49266
   173
  end
blanchet@49266
   174
blanchet@49266
   175
end
blanchet@49266
   176
blanchet@49317
   177
fun interesting_terms_types_and_classes term_max_depth type_max_depth ts =
blanchet@49266
   178
  let
blanchet@49266
   179
    val bad_types = [@{type_name prop}, @{type_name bool}, @{type_name fun}]
blanchet@49266
   180
    val bad_consts = atp_widely_irrelevant_consts
blanchet@49319
   181
    fun add_classes @{sort type} = I
blanchet@49319
   182
      | add_classes S = union (op =) (map class_name_of S)
blanchet@49266
   183
    fun do_add_type (Type (s, Ts)) =
blanchet@49266
   184
        (not (member (op =) bad_types s) ? insert (op =) (type_name_of s))
blanchet@49266
   185
        #> fold do_add_type Ts
blanchet@49319
   186
      | do_add_type (TFree (_, S)) = add_classes S
blanchet@49319
   187
      | do_add_type (TVar (_, S)) = add_classes S
blanchet@49266
   188
    fun add_type T = type_max_depth >= 0 ? do_add_type T
blanchet@49266
   189
    fun mk_app s args =
blanchet@49266
   190
      if member (op <>) args "" then s ^ "(" ^ space_implode "," args ^ ")"
blanchet@49266
   191
      else s
blanchet@49266
   192
    fun patternify ~1 _ = ""
blanchet@49266
   193
      | patternify depth t =
blanchet@49266
   194
        case strip_comb t of
blanchet@49266
   195
          (Const (s, _), args) =>
blanchet@49266
   196
          mk_app (const_name_of s) (map (patternify (depth - 1)) args)
blanchet@49266
   197
        | _ => ""
blanchet@49266
   198
    fun add_term_patterns ~1 _ = I
blanchet@49266
   199
      | add_term_patterns depth t =
blanchet@49266
   200
        insert (op =) (patternify depth t)
blanchet@49266
   201
        #> add_term_patterns (depth - 1) t
blanchet@49266
   202
    val add_term = add_term_patterns term_max_depth
blanchet@49266
   203
    fun add_patterns t =
blanchet@49266
   204
      let val (head, args) = strip_comb t in
blanchet@49266
   205
        (case head of
blanchet@49266
   206
           Const (s, T) =>
blanchet@49266
   207
           not (member (op =) bad_consts s) ? (add_term t #> add_type T)
blanchet@49266
   208
         | Free (_, T) => add_type T
blanchet@49266
   209
         | Var (_, T) => add_type T
blanchet@49266
   210
         | Abs (_, T, body) => add_type T #> add_patterns body
blanchet@49266
   211
         | _ => I)
blanchet@49266
   212
        #> fold add_patterns args
blanchet@49266
   213
      end
blanchet@49317
   214
  in [] |> fold add_patterns ts |> sort string_ord end
blanchet@49266
   215
blanchet@49266
   216
fun is_likely_tautology th =
blanchet@49317
   217
  null (interesting_terms_types_and_classes 0 ~1 [prop_of th]) andalso
blanchet@49266
   218
  not (Thm.eq_thm_prop (@{thm ext}, th))
blanchet@49266
   219
blanchet@49266
   220
fun is_too_meta thy th =
blanchet@49266
   221
  fastype_of (Object_Logic.atomize_term thy (prop_of th)) <> @{typ bool}
blanchet@49266
   222
blanchet@49314
   223
fun all_non_tautological_facts_of thy css_table =
blanchet@49314
   224
  all_facts_of thy css_table
blanchet@49266
   225
  |> filter_out ((is_likely_tautology orf is_too_meta thy) o snd)
blanchet@49266
   226
blanchet@49266
   227
fun theory_ord p =
blanchet@49266
   228
  if Theory.eq_thy p then EQUAL
blanchet@49266
   229
  else if Theory.subthy p then LESS
blanchet@49266
   230
  else if Theory.subthy (swap p) then GREATER
blanchet@49266
   231
  else EQUAL
blanchet@49266
   232
blanchet@49266
   233
val thm_ord = theory_ord o pairself theory_of_thm
blanchet@49266
   234
blanchet@49318
   235
(* ### FIXME: optimize *)
blanchet@49318
   236
fun thy_facts_from_thms ths =
blanchet@49318
   237
  ths |> map (snd #> `(theory_of_thm #> Context.theory_name))
blanchet@49266
   238
      |> AList.group (op =)
blanchet@49266
   239
      |> sort (int_ord o pairself (length o Theory.ancestors_of o theory_of_thm
blanchet@49266
   240
                                   o hd o snd))
blanchet@49318
   241
      |> map (apsnd (sort (rev_order o thm_ord) #> map Thm.get_name_hint))
blanchet@49318
   242
      |> Symtab.make
blanchet@49266
   243
blanchet@49318
   244
fun has_thy thy th =
blanchet@49318
   245
  Context.theory_name thy = Context.theory_name (theory_of_thm th)
blanchet@49266
   246
blanchet@49318
   247
fun parent_facts thy thy_facts =
blanchet@49318
   248
  let
blanchet@49318
   249
    fun add_last thy =
blanchet@49318
   250
      case Symtab.lookup thy_facts (Context.theory_name thy) of
blanchet@49318
   251
        SOME (last_fact :: _) => insert (op =) last_fact
blanchet@49318
   252
      | _ => add_parent thy
blanchet@49318
   253
    and add_parent thy = fold add_last (Theory.parents_of thy)
blanchet@49318
   254
  in add_parent thy [] end
blanchet@49266
   255
blanchet@49266
   256
fun is_exists (s, _) = (s = @{const_name Ex} orelse s = @{const_name Ex1})
blanchet@49266
   257
blanchet@49312
   258
val term_max_depth = 1
blanchet@49312
   259
val type_max_depth = 1
blanchet@49266
   260
blanchet@49266
   261
(* TODO: Generate type classes for types? *)
blanchet@49317
   262
fun features_of thy status ts =
blanchet@49318
   263
  thy_feature_name_of (Context.theory_name thy) ::
blanchet@49317
   264
  interesting_terms_types_and_classes term_max_depth type_max_depth ts
blanchet@49317
   265
  |> exists (not o is_lambda_free) ts ? cons "lambdas"
blanchet@49317
   266
  |> exists (exists_Const is_exists) ts ? cons "skolems"
blanchet@49317
   267
  |> exists (not o is_fo_term thy) ts ? cons "ho"
blanchet@49317
   268
  |> (case status of
blanchet@49317
   269
        General => I
blanchet@49317
   270
      | Induction => cons "induction"
blanchet@49317
   271
      | Intro => cons "intro"
blanchet@49317
   272
      | Inductive => cons "inductive"
blanchet@49317
   273
      | Elim => cons "elim"
blanchet@49317
   274
      | Simp => cons "simp"
blanchet@49317
   275
      | Def => cons "def")
blanchet@49266
   276
blanchet@49266
   277
fun isabelle_dependencies_of all_facts =
blanchet@49318
   278
  thms_in_proof (SOME all_facts) #> sort string_ord
blanchet@49266
   279
blanchet@49266
   280
val freezeT = Type.legacy_freeze_type
blanchet@49266
   281
blanchet@49266
   282
fun freeze (t $ u) = freeze t $ freeze u
blanchet@49266
   283
  | freeze (Abs (s, T, t)) = Abs (s, freezeT T, freeze t)
blanchet@49266
   284
  | freeze (Var ((s, _), T)) = Free (s, freezeT T)
blanchet@49266
   285
  | freeze (Const (s, T)) = Const (s, freezeT T)
blanchet@49266
   286
  | freeze (Free (s, T)) = Free (s, freezeT T)
blanchet@49266
   287
  | freeze t = t
blanchet@49266
   288
blanchet@49266
   289
fun goal_of_thm thy = prop_of #> freeze #> cterm_of thy #> Goal.init
blanchet@49266
   290
blanchet@49266
   291
fun run_prover ctxt (params as {provers, ...}) facts goal =
blanchet@49266
   292
  let
blanchet@49266
   293
    val problem =
blanchet@49266
   294
      {state = Proof.init ctxt, goal = goal, subgoal = 1, subgoal_count = 1,
blanchet@49304
   295
       facts = facts |> map (apfst (apfst (fn name => name ())))
blanchet@49304
   296
                     |> map Sledgehammer_Provers.Untranslated_Fact}
blanchet@49266
   297
    val prover =
blanchet@49266
   298
      Sledgehammer_Minimize.get_minimizing_prover ctxt
blanchet@49266
   299
          Sledgehammer_Provers.Normal (hd provers)
blanchet@49266
   300
  in prover params (K (K (K ""))) problem end
blanchet@49266
   301
blanchet@49318
   302
fun accessibility_of thy thy_facts =
blanchet@49318
   303
  case Symtab.lookup thy_facts (Context.theory_name thy) of
blanchet@49318
   304
    SOME (fact :: _) => [fact]
blanchet@49318
   305
  | _ => parent_facts thy thy_facts
blanchet@49318
   306
blanchet@49319
   307
val thy_name_of_fact = hd o Long_Name.explode
blanchet@49266
   308
blanchet@49266
   309
blanchet@49317
   310
(*** Low-level communication with MaSh ***)
blanchet@49317
   311
blanchet@49326
   312
fun run_mash ctxt save write_cmds read_preds =
blanchet@49326
   313
  let
blanchet@49326
   314
    val temp_dir = getenv "ISABELLE_TMP"
blanchet@49326
   315
    val serial = serial_string ()
blanchet@49326
   316
    val cmd_file = temp_dir ^ "/mash_commands." ^ serial
blanchet@49326
   317
    val cmd_path = Path.explode cmd_file
blanchet@49326
   318
    val pred_file = temp_dir ^ "/mash_preds." ^ serial
blanchet@49326
   319
    val log_file = temp_dir ^ "/mash_log." ^ serial
blanchet@49326
   320
    val command =
blanchet@49326
   321
      getenv "MASH_HOME" ^ "/mash.py --inputFile " ^ cmd_file ^
blanchet@49326
   322
      " --outputDir " ^ mash_dir () ^ " --predictions " ^ pred_file ^
blanchet@49326
   323
      " --log " ^ log_file ^ (if save then " --saveModel" else "") ^
blanchet@49326
   324
      " > /dev/null"
blanchet@49326
   325
    val _ = File.write cmd_path ""
blanchet@49326
   326
    val _ = write_cmds (File.append cmd_path)
blanchet@49326
   327
    val _ = trace_msg ctxt (fn () => "  running " ^ command)
blanchet@49326
   328
    val _ = Isabelle_System.bash command
blanchet@49326
   329
  in read_preds (fn () => File.read_lines (Path.explode pred_file)) end
blanchet@49326
   330
blanchet@49326
   331
fun str_of_update (fact, access, feats, deps) =
blanchet@49326
   332
  "! " ^ escape_meta fact ^ ": " ^ escape_metas access ^ "; " ^
blanchet@49326
   333
  escape_metas feats ^ "; " ^ escape_metas deps ^ "\n"
blanchet@49326
   334
blanchet@49326
   335
fun str_of_query (access, feats) =
blanchet@49326
   336
  "? " ^ escape_metas access ^ "; " ^ escape_metas feats
blanchet@49326
   337
blanchet@49323
   338
fun mash_RESET ctxt =
blanchet@49324
   339
  let val path = mash_dir () |> Path.explode in
blanchet@49324
   340
    trace_msg ctxt (K "MaSh RESET");
blanchet@49324
   341
    File.fold_dir (fn file => fn () =>
blanchet@49324
   342
                      File.rm (Path.append path (Path.basic file)))
blanchet@49324
   343
                  path ()
blanchet@49324
   344
  end
blanchet@49317
   345
blanchet@49324
   346
fun mash_ADD _ [] = ()
blanchet@49326
   347
  | mash_ADD ctxt upds =
blanchet@49326
   348
    (trace_msg ctxt (fn () => "MaSh ADD " ^ space_implode " " (map #1 upds));
blanchet@49326
   349
     run_mash ctxt true (fn append => List.app (append o str_of_update) upds)
blanchet@49326
   350
              (K ()))
blanchet@49317
   351
blanchet@49323
   352
fun mash_DEL ctxt facts feats =
blanchet@49323
   353
  trace_msg ctxt (fn () =>
blanchet@49323
   354
      "MaSh DEL " ^ escape_metas facts ^ "; " ^ escape_metas feats)
blanchet@49317
   355
blanchet@49326
   356
fun mash_QUERY ctxt (query as (_, feats)) =
blanchet@49326
   357
  (trace_msg ctxt (fn () => "MaSh SUGGEST " ^ space_implode " " feats);
blanchet@49326
   358
   run_mash ctxt false (fn append => append (str_of_query query))
blanchet@49326
   359
                 (fn preds => snd (extract_query (List.last (preds ()))))
blanchet@49326
   360
   handle List.Empty => [])
blanchet@49317
   361
blanchet@49317
   362
blanchet@49266
   363
(*** High-level communication with MaSh ***)
blanchet@49266
   364
blanchet@49316
   365
type mash_state =
blanchet@49323
   366
  {dirty_thys : unit Symtab.table,
blanchet@49316
   367
   thy_facts : string list Symtab.table}
blanchet@49264
   368
blanchet@49319
   369
val empty_state =
blanchet@49323
   370
  {dirty_thys = Symtab.empty,
blanchet@49316
   371
   thy_facts = Symtab.empty}
blanchet@49316
   372
blanchet@49316
   373
local
blanchet@49316
   374
blanchet@49316
   375
fun mash_load (state as (true, _)) = state
blanchet@49316
   376
  | mash_load _ =
blanchet@49324
   377
    let val path = mash_state_path () in
blanchet@49317
   378
      (true,
blanchet@49317
   379
       case try File.read_lines path of
blanchet@49323
   380
         SOME (dirty_line :: facts_lines) =>
blanchet@49317
   381
         let
blanchet@49321
   382
           fun dirty_thys_of_line line =
blanchet@49323
   383
             Symtab.make (line |> unescape_metas |> map (rpair ()))
blanchet@49317
   384
           fun add_facts_line line =
blanchet@49323
   385
             case unescape_metas line of
blanchet@49317
   386
               thy :: facts => Symtab.update_new (thy, facts)
blanchet@49317
   387
             | _ => I (* shouldn't happen *)
blanchet@49317
   388
         in
blanchet@49323
   389
           {dirty_thys = dirty_thys_of_line dirty_line,
blanchet@49317
   390
            thy_facts = fold add_facts_line facts_lines Symtab.empty}
blanchet@49317
   391
         end
blanchet@49319
   392
       | _ => empty_state)
blanchet@49317
   393
    end
blanchet@49316
   394
blanchet@49323
   395
fun mash_save ({dirty_thys, thy_facts} : mash_state) =
blanchet@49316
   396
  let
blanchet@49324
   397
    val path = mash_state_path ()
blanchet@49323
   398
    val dirty_line = (escape_metas (Symtab.keys dirty_thys)) ^ "\n"
blanchet@49318
   399
    fun fact_line_for (thy, facts) = escape_metas (thy :: facts) ^ "\n"
blanchet@49316
   400
  in
blanchet@49323
   401
    File.write path dirty_line;
blanchet@49316
   402
    Symtab.fold (fn thy_fact => fn () =>
blanchet@49323
   403
                    File.append path (fact_line_for thy_fact)) thy_facts ()
blanchet@49316
   404
  end
blanchet@49316
   405
blanchet@49317
   406
val global_state =
blanchet@49319
   407
  Synchronized.var "Sledgehammer_Filter_MaSh.global_state" (false, empty_state)
blanchet@49316
   408
blanchet@49316
   409
in
blanchet@49316
   410
blanchet@49321
   411
fun mash_map f =
blanchet@49317
   412
  Synchronized.change global_state (mash_load ##> (f #> tap mash_save))
blanchet@49316
   413
blanchet@49319
   414
fun mash_get () = Synchronized.change_result global_state (mash_load #> `snd)
blanchet@49317
   415
blanchet@49323
   416
fun mash_reset ctxt =
blanchet@49317
   417
  Synchronized.change global_state (fn _ =>
blanchet@49324
   418
      (mash_RESET ctxt; File.write (mash_state_path ()) "";
blanchet@49323
   419
       (true, empty_state)))
blanchet@49316
   420
blanchet@49316
   421
end
blanchet@49316
   422
blanchet@49323
   423
fun mash_can_suggest_facts (_ : Proof.context) =
blanchet@49323
   424
  not (Symtab.is_empty (#thy_facts (mash_get ())))
blanchet@49264
   425
blanchet@49326
   426
fun mash_suggest_facts ctxt params prover hyp_ts concl_t facts =
blanchet@49316
   427
  let
blanchet@49317
   428
    val thy = Proof_Context.theory_of ctxt
blanchet@49326
   429
    val thy_facts = #thy_facts (mash_get ())
blanchet@49326
   430
    val access = accessibility_of thy thy_facts
blanchet@49317
   431
    val feats = features_of thy General (concl_t :: hyp_ts)
blanchet@49326
   432
    val suggs = mash_QUERY ctxt (access, feats)
blanchet@49326
   433
  in suggested_facts suggs facts end
blanchet@49264
   434
blanchet@49323
   435
fun mash_can_learn_thy (_ : Proof.context) thy =
blanchet@49321
   436
  not (Symtab.defined (#dirty_thys (mash_get ())) (Context.theory_name thy))
blanchet@49264
   437
blanchet@49319
   438
fun is_fact_in_thy_facts thy_facts fact =
blanchet@49319
   439
  case Symtab.lookup thy_facts (thy_name_of_fact fact) of
blanchet@49319
   440
    SOME facts => member (op =) facts fact
blanchet@49319
   441
  | NONE => false
blanchet@49319
   442
blanchet@49321
   443
fun zip_facts news [] = news
blanchet@49321
   444
  | zip_facts [] olds = olds
blanchet@49321
   445
  | zip_facts (new :: news) (old :: olds) =
blanchet@49321
   446
    if new = old then
blanchet@49321
   447
      new :: zip_facts news olds
blanchet@49321
   448
    else if member (op =) news old then
blanchet@49321
   449
      old :: zip_facts (filter_out (curry (op =) old) news) olds
blanchet@49321
   450
    else if member (op =) olds new then
blanchet@49321
   451
      new :: zip_facts news (filter_out (curry (op =) new) olds)
blanchet@49321
   452
    else
blanchet@49321
   453
      new :: old :: zip_facts news olds
blanchet@49321
   454
blanchet@49321
   455
fun add_thy_facts_from_thys new old =
blanchet@49321
   456
  let
blanchet@49321
   457
    fun add_thy (thy, new_facts) =
blanchet@49321
   458
      case Symtab.lookup old thy of
blanchet@49321
   459
        SOME old_facts => Symtab.update (thy, zip_facts old_facts new_facts)
blanchet@49321
   460
      | NONE => Symtab.update_new (thy, new_facts)
blanchet@49321
   461
  in old |> Symtab.fold add_thy new end
blanchet@49321
   462
blanchet@49323
   463
fun mash_learn_thy ctxt thy timeout =
blanchet@49319
   464
  let
blanchet@49319
   465
    val css_table = Sledgehammer_Fact.clasimpset_rule_table_of ctxt
blanchet@49319
   466
    val facts = all_non_tautological_facts_of thy css_table
blanchet@49321
   467
    val {thy_facts, ...} = mash_get ()
blanchet@49319
   468
    fun is_old (_, th) = is_fact_in_thy_facts thy_facts (Thm.get_name_hint th)
blanchet@49319
   469
    val (old_facts, new_facts) =
blanchet@49319
   470
      facts |> List.partition is_old ||> sort (thm_ord o pairself snd)
blanchet@49323
   471
  in
blanchet@49323
   472
    if null new_facts then
blanchet@49323
   473
      ()
blanchet@49323
   474
    else
blanchet@49319
   475
      let
blanchet@49323
   476
        val ths = facts |> map snd
blanchet@49323
   477
        val all_names = ths |> map Thm.get_name_hint
blanchet@49326
   478
        fun do_fact ((_, (_, status)), th) (prevs, upds) =
blanchet@49323
   479
          let
blanchet@49323
   480
            val name = Thm.get_name_hint th
blanchet@49323
   481
            val feats = features_of thy status [prop_of th]
blanchet@49323
   482
            val deps = isabelle_dependencies_of all_names th
blanchet@49326
   483
            val upd = (name, prevs, feats, deps)
blanchet@49326
   484
          in ([name], upd :: upds) end
blanchet@49323
   485
        val parents = parent_facts thy thy_facts
blanchet@49326
   486
        val (_, upds) = (parents, []) |> fold do_fact new_facts
blanchet@49323
   487
        val new_thy_facts = new_facts |> thy_facts_from_thms
blanchet@49323
   488
        fun trans {dirty_thys, thy_facts} =
blanchet@49326
   489
          (mash_ADD ctxt (rev upds);
blanchet@49323
   490
           {dirty_thys = dirty_thys,
blanchet@49323
   491
            thy_facts = thy_facts |> add_thy_facts_from_thys new_thy_facts})
blanchet@49323
   492
      in mash_map trans end
blanchet@49323
   493
  end
blanchet@49319
   494
blanchet@49324
   495
fun mash_learn_proof ctxt thy t ths =
blanchet@49324
   496
  mash_map (fn state as {dirty_thys, thy_facts} =>
blanchet@49324
   497
    let val deps = ths |> map Thm.get_name_hint in
blanchet@49324
   498
      if forall (is_fact_in_thy_facts thy_facts) deps then
blanchet@49324
   499
        let
blanchet@49324
   500
          val fact = ATP_Util.timestamp () (* should be fairly fresh *)
blanchet@49324
   501
          val access = accessibility_of thy thy_facts
blanchet@49324
   502
          val feats = features_of thy General [t]
blanchet@49324
   503
        in
blanchet@49324
   504
          mash_ADD ctxt [(fact, access, feats, deps)];
blanchet@49324
   505
          {dirty_thys = dirty_thys, thy_facts = thy_facts}
blanchet@49324
   506
        end
blanchet@49324
   507
      else
blanchet@49324
   508
        state
blanchet@49324
   509
    end)
blanchet@49264
   510
blanchet@49308
   511
fun relevant_facts ctxt params prover max_facts
blanchet@49313
   512
        ({add, only, ...} : fact_override) hyp_ts concl_t facts =
blanchet@49303
   513
  if only then
blanchet@49304
   514
    facts
blanchet@49308
   515
  else if max_facts <= 0 then
blanchet@49303
   516
    []
blanchet@49303
   517
  else
blanchet@49303
   518
    let
blanchet@49303
   519
      val add_ths = Attrib.eval_thms ctxt add
blanchet@49307
   520
      fun prepend_facts ths accepts =
blanchet@49303
   521
        ((facts |> filter (member Thm.eq_thm_prop ths o snd)) @
blanchet@49307
   522
         (accepts |> filter_out (member Thm.eq_thm_prop ths o snd)))
blanchet@49308
   523
        |> take max_facts
blanchet@49313
   524
      val iter_facts =
blanchet@49313
   525
        iterative_relevant_facts ctxt params prover max_facts NONE hyp_ts
blanchet@49313
   526
                                 concl_t facts
blanchet@49327
   527
      val (mash_facts, mash_rejects) =
blanchet@49326
   528
        facts |> mash_suggest_facts ctxt params prover hyp_ts concl_t
blanchet@49326
   529
              |> chop max_facts
blanchet@49303
   530
    in
blanchet@49327
   531
      mesh_facts max_facts iter_facts mash_facts mash_rejects
blanchet@49303
   532
      |> not (null add_ths) ? prepend_facts add_ths
blanchet@49303
   533
    end
blanchet@49303
   534
blanchet@49263
   535
end;