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