src/HOL/Tools/Sledgehammer/sledgehammer_filter_mash.ML
author blanchet
Wed, 18 Jul 2012 08:44:03 +0200
changeset 49316 e5c5037a3104
parent 49314 5e5c6616f0fe
child 49317 6cf5e58f1185
permissions -rw-r--r--
started implementing MaSh client-side I/O
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@49266
    17
  val fact_name_of : string -> string
blanchet@49314
    18
  val all_non_tautological_facts_of :
blanchet@49314
    19
    theory -> status Termtab.table -> fact list
blanchet@49266
    20
  val theory_ord : theory * theory -> order
blanchet@49266
    21
  val thm_ord : thm * thm -> order
blanchet@49266
    22
  val thms_by_thy : ('a * thm) list -> (string * thm list) list
blanchet@49266
    23
  val has_thy : theory -> thm -> bool
blanchet@49311
    24
  val parent_facts : (string * thm list) list -> theory -> string list
blanchet@49266
    25
  val features_of : theory -> status * thm -> string list
blanchet@49266
    26
  val isabelle_dependencies_of : string list -> thm -> string list
blanchet@49266
    27
  val goal_of_thm : theory -> thm -> thm
blanchet@49311
    28
  val run_prover : Proof.context -> params -> fact list -> thm -> prover_result
blanchet@49314
    29
  val generate_accessibility : Proof.context -> theory -> bool -> string -> unit
blanchet@49314
    30
  val generate_features : Proof.context -> theory -> bool -> string -> unit
blanchet@49314
    31
  val generate_isa_dependencies :
blanchet@49314
    32
    Proof.context -> theory -> bool -> string -> unit
blanchet@49266
    33
  val generate_atp_dependencies :
blanchet@49266
    34
    Proof.context -> params -> theory -> bool -> string -> unit
blanchet@49313
    35
  val mash_reset : unit -> unit
blanchet@49313
    36
  val mash_can_suggest_facts : unit -> bool
blanchet@49313
    37
  val mash_suggest_facts :
blanchet@49313
    38
    theory -> params -> string -> int -> term list -> term -> fact list
blanchet@49313
    39
    -> fact list * fact list
blanchet@49313
    40
  val mash_can_learn_thy : theory -> bool
blanchet@49313
    41
  val mash_learn_thy : theory -> real -> unit
blanchet@49313
    42
  val mash_learn_proof : theory -> term -> thm list -> unit
blanchet@49303
    43
  val relevant_facts :
blanchet@49307
    44
    Proof.context -> params -> string -> int -> fact_override -> term list
blanchet@49311
    45
    -> term -> fact list -> fact list
blanchet@49263
    46
end;
blanchet@49263
    47
blanchet@49263
    48
structure Sledgehammer_Filter_MaSh : SLEDGEHAMMER_FILTER_MASH =
blanchet@49263
    49
struct
blanchet@49264
    50
blanchet@49266
    51
open ATP_Util
blanchet@49266
    52
open ATP_Problem_Generate
blanchet@49266
    53
open Sledgehammer_Util
blanchet@49266
    54
open Sledgehammer_Fact
blanchet@49266
    55
open Sledgehammer_Filter_Iter
blanchet@49266
    56
open Sledgehammer_Provers
blanchet@49266
    57
blanchet@49316
    58
val mash_dir = "mash"
blanchet@49316
    59
val model_file = "model"
blanchet@49316
    60
val state_file = "state"
blanchet@49316
    61
blanchet@49316
    62
fun mash_path file = getenv "ISABELLE_HOME_USER" ^ "/" ^ mash_dir ^ "/" ^ file
blanchet@49316
    63
blanchet@49266
    64
blanchet@49266
    65
(*** Low-level communication with MaSh ***)
blanchet@49266
    66
blanchet@49313
    67
fun mash_RESET () =
blanchet@49316
    68
  warning "MaSh RESET"
blanchet@49264
    69
blanchet@49316
    70
fun mash_ADD fact access feats deps =
blanchet@49316
    71
  warning ("MaSh ADD " ^ fact ^ ": " ^ space_implode " " access ^ "; " ^
blanchet@49264
    72
           space_implode " " feats ^ "; " ^ space_implode " " deps)
blanchet@49264
    73
blanchet@49313
    74
fun mash_DEL fact =
blanchet@49316
    75
  warning ("MaSh DEL " ^ fact)
blanchet@49264
    76
blanchet@49316
    77
fun mash_SUGGEST access feats =
blanchet@49316
    78
  warning ("MaSh SUGGEST " ^ space_implode " " access ^ "; " ^
blanchet@49264
    79
           space_implode " " feats)
blanchet@49264
    80
blanchet@49266
    81
blanchet@49266
    82
(*** Isabelle helpers ***)
blanchet@49266
    83
blanchet@49266
    84
fun escape_meta_char c =
blanchet@49266
    85
  if Char.isAlphaNum c orelse c = #"_" orelse c = #"." orelse c = #"(" orelse
blanchet@49266
    86
     c = #")" orelse c = #"," then
blanchet@49266
    87
    String.str c
blanchet@49266
    88
  else
blanchet@49266
    89
    (* fixed width, in case more digits follow *)
blanchet@49266
    90
    "\\" ^ stringN_of_int 3 (Char.ord c)
blanchet@49266
    91
blanchet@49266
    92
val escape_meta = String.translate escape_meta_char
blanchet@49266
    93
blanchet@49266
    94
val thy_prefix = "y_"
blanchet@49266
    95
blanchet@49266
    96
val fact_name_of = escape_meta
blanchet@49266
    97
val thy_name_of = prefix thy_prefix o escape_meta
blanchet@49266
    98
val const_name_of = prefix const_prefix o escape_meta
blanchet@49266
    99
val type_name_of = prefix type_const_prefix o escape_meta
blanchet@49266
   100
val class_name_of = prefix class_prefix o escape_meta
blanchet@49266
   101
blanchet@49266
   102
val thy_name_of_thm = theory_of_thm #> Context.theory_name
blanchet@49266
   103
blanchet@49266
   104
local
blanchet@49266
   105
blanchet@49266
   106
fun has_bool @{typ bool} = true
blanchet@49266
   107
  | has_bool (Type (_, Ts)) = exists has_bool Ts
blanchet@49266
   108
  | has_bool _ = false
blanchet@49266
   109
blanchet@49266
   110
fun has_fun (Type (@{type_name fun}, _)) = true
blanchet@49266
   111
  | has_fun (Type (_, Ts)) = exists has_fun Ts
blanchet@49266
   112
  | has_fun _ = false
blanchet@49266
   113
blanchet@49266
   114
val is_conn = member (op =)
blanchet@49266
   115
  [@{const_name Trueprop}, @{const_name HOL.conj}, @{const_name HOL.disj},
blanchet@49266
   116
   @{const_name HOL.implies}, @{const_name Not},
blanchet@49266
   117
   @{const_name All}, @{const_name Ex}, @{const_name Ball}, @{const_name Bex},
blanchet@49266
   118
   @{const_name HOL.eq}]
blanchet@49266
   119
blanchet@49266
   120
val has_bool_arg_const =
blanchet@49266
   121
  exists_Const (fn (c, T) =>
blanchet@49266
   122
                   not (is_conn c) andalso exists has_bool (binder_types T))
blanchet@49266
   123
blanchet@49266
   124
fun higher_inst_const thy (c, T) =
blanchet@49266
   125
  case binder_types T of
blanchet@49266
   126
    [] => false
blanchet@49266
   127
  | Ts => length (binder_types (Sign.the_const_type thy c)) <> length Ts
blanchet@49266
   128
blanchet@49266
   129
val binders = [@{const_name All}, @{const_name Ex}]
blanchet@49266
   130
blanchet@49266
   131
in
blanchet@49266
   132
blanchet@49266
   133
fun is_fo_term thy t =
blanchet@49266
   134
  let
blanchet@49266
   135
    val t =
blanchet@49266
   136
      t |> Envir.beta_eta_contract
blanchet@49266
   137
        |> transform_elim_prop
blanchet@49266
   138
        |> Object_Logic.atomize_term thy
blanchet@49266
   139
  in
blanchet@49266
   140
    Term.is_first_order binders t andalso
blanchet@49266
   141
    not (exists_subterm (fn Var (_, T) => has_bool T orelse has_fun T
blanchet@49266
   142
                          | _ => false) t orelse
blanchet@49266
   143
         has_bool_arg_const t orelse exists_Const (higher_inst_const thy) t)
blanchet@49266
   144
  end
blanchet@49266
   145
blanchet@49266
   146
end
blanchet@49266
   147
blanchet@49266
   148
fun interesting_terms_types_and_classes term_max_depth type_max_depth t =
blanchet@49266
   149
  let
blanchet@49266
   150
    val bad_types = [@{type_name prop}, @{type_name bool}, @{type_name fun}]
blanchet@49266
   151
    val bad_consts = atp_widely_irrelevant_consts
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@49266
   155
      | do_add_type (TFree (_, S)) = union (op =) (map class_name_of S)
blanchet@49266
   156
      | do_add_type (TVar (_, S)) = union (op =) (map class_name_of 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@49266
   183
  in [] |> add_patterns t |> sort string_ord end
blanchet@49266
   184
blanchet@49266
   185
fun is_likely_tautology th =
blanchet@49266
   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@49266
   204
fun thms_by_thy ths =
blanchet@49266
   205
  ths |> map (snd #> `thy_name_of_thm)
blanchet@49266
   206
      |> AList.group (op =)
blanchet@49266
   207
      |> sort (int_ord o pairself (length o Theory.ancestors_of o theory_of_thm
blanchet@49266
   208
                                   o hd o snd))
blanchet@49266
   209
      |> map (apsnd (sort thm_ord))
blanchet@49266
   210
blanchet@49266
   211
fun has_thy thy th = (Context.theory_name thy = thy_name_of_thm th)
blanchet@49266
   212
blanchet@49311
   213
fun add_last_thms thy_ths thy =
blanchet@49311
   214
  case AList.lookup (op =) thy_ths (Context.theory_name thy) of
blanchet@49311
   215
    SOME (ths as _ :: _) => insert Thm.eq_thm (List.last ths)
blanchet@49311
   216
  | _ => add_parent_thms thy_ths thy
blanchet@49311
   217
and add_parent_thms thy_ths thy =
blanchet@49311
   218
  fold (add_last_thms thy_ths) (Theory.parents_of thy)
blanchet@49311
   219
blanchet@49311
   220
fun parent_facts thy_ths thy =
blanchet@49311
   221
  add_parent_thms thy_ths thy []
blanchet@49266
   222
  |> map (fact_name_of o Thm.get_name_hint)
blanchet@49266
   223
blanchet@49266
   224
fun is_exists (s, _) = (s = @{const_name Ex} orelse s = @{const_name Ex1})
blanchet@49266
   225
blanchet@49312
   226
val term_max_depth = 1
blanchet@49312
   227
val type_max_depth = 1
blanchet@49266
   228
blanchet@49266
   229
(* TODO: Generate type classes for types? *)
blanchet@49266
   230
fun features_of thy (status, th) =
blanchet@49266
   231
  let val t = Thm.prop_of th in
blanchet@49266
   232
    thy_name_of (thy_name_of_thm th) ::
blanchet@49312
   233
    interesting_terms_types_and_classes term_max_depth type_max_depth t
blanchet@49266
   234
    |> not (has_no_lambdas t) ? cons "lambdas"
blanchet@49266
   235
    |> exists_Const is_exists t ? cons "skolems"
blanchet@49266
   236
    |> not (is_fo_term thy t) ? cons "ho"
blanchet@49266
   237
    |> (case status of
blanchet@49266
   238
          General => I
blanchet@49266
   239
        | Induction => cons "induction"
blanchet@49266
   240
        | Intro => cons "intro"
blanchet@49266
   241
        | Inductive => cons "inductive"
blanchet@49266
   242
        | Elim => cons "elim"
blanchet@49266
   243
        | Simp => cons "simp"
blanchet@49266
   244
        | Def => cons "def")
blanchet@49266
   245
  end
blanchet@49266
   246
blanchet@49266
   247
fun isabelle_dependencies_of all_facts =
blanchet@49266
   248
  thms_in_proof (SOME all_facts)
blanchet@49266
   249
  #> map fact_name_of #> sort string_ord
blanchet@49266
   250
blanchet@49266
   251
val freezeT = Type.legacy_freeze_type
blanchet@49266
   252
blanchet@49266
   253
fun freeze (t $ u) = freeze t $ freeze u
blanchet@49266
   254
  | freeze (Abs (s, T, t)) = Abs (s, freezeT T, freeze t)
blanchet@49266
   255
  | freeze (Var ((s, _), T)) = Free (s, freezeT T)
blanchet@49266
   256
  | freeze (Const (s, T)) = Const (s, freezeT T)
blanchet@49266
   257
  | freeze (Free (s, T)) = Free (s, freezeT T)
blanchet@49266
   258
  | freeze t = t
blanchet@49266
   259
blanchet@49266
   260
fun goal_of_thm thy = prop_of #> freeze #> cterm_of thy #> Goal.init
blanchet@49266
   261
blanchet@49266
   262
fun run_prover ctxt (params as {provers, ...}) facts goal =
blanchet@49266
   263
  let
blanchet@49266
   264
    val problem =
blanchet@49266
   265
      {state = Proof.init ctxt, goal = goal, subgoal = 1, subgoal_count = 1,
blanchet@49304
   266
       facts = facts |> map (apfst (apfst (fn name => name ())))
blanchet@49304
   267
                     |> map Sledgehammer_Provers.Untranslated_Fact}
blanchet@49266
   268
    val prover =
blanchet@49266
   269
      Sledgehammer_Minimize.get_minimizing_prover ctxt
blanchet@49266
   270
          Sledgehammer_Provers.Normal (hd provers)
blanchet@49266
   271
  in prover params (K (K (K ""))) problem end
blanchet@49266
   272
blanchet@49314
   273
fun generate_accessibility ctxt thy include_thy file_name =
blanchet@49266
   274
  let
blanchet@49266
   275
    val path = file_name |> Path.explode
blanchet@49266
   276
    val _ = File.write path ""
blanchet@49314
   277
    val css_table = clasimpset_rule_table_of ctxt
blanchet@49266
   278
    fun do_thm th prevs =
blanchet@49266
   279
      let
blanchet@49266
   280
        val s = th ^ ": " ^ space_implode " " prevs ^ "\n"
blanchet@49266
   281
        val _ = File.append path s
blanchet@49266
   282
      in [th] end
blanchet@49266
   283
    val thy_ths =
blanchet@49314
   284
      all_non_tautological_facts_of thy css_table
blanchet@49266
   285
      |> not include_thy ? filter_out (has_thy thy o snd)
blanchet@49266
   286
      |> thms_by_thy
blanchet@49266
   287
    fun do_thy ths =
blanchet@49266
   288
      let
blanchet@49266
   289
        val thy = theory_of_thm (hd ths)
blanchet@49311
   290
        val parents = parent_facts thy_ths thy
blanchet@49266
   291
        val ths = ths |> map (fact_name_of o Thm.get_name_hint)
blanchet@49266
   292
      in fold do_thm ths parents; () end
blanchet@49266
   293
  in List.app (do_thy o snd) thy_ths end
blanchet@49266
   294
blanchet@49314
   295
fun generate_features ctxt thy include_thy file_name =
blanchet@49266
   296
  let
blanchet@49266
   297
    val path = file_name |> Path.explode
blanchet@49266
   298
    val _ = File.write path ""
blanchet@49314
   299
    val css_table = clasimpset_rule_table_of ctxt
blanchet@49266
   300
    val facts =
blanchet@49314
   301
      all_non_tautological_facts_of thy css_table
blanchet@49266
   302
      |> not include_thy ? filter_out (has_thy thy o snd)
blanchet@49266
   303
    fun do_fact ((_, (_, status)), th) =
blanchet@49266
   304
      let
blanchet@49266
   305
        val name = Thm.get_name_hint th
blanchet@49266
   306
        val feats = features_of thy (status, th)
blanchet@49266
   307
        val s = fact_name_of name ^ ": " ^ space_implode " " feats ^ "\n"
blanchet@49266
   308
      in File.append path s end
blanchet@49266
   309
  in List.app do_fact facts end
blanchet@49266
   310
blanchet@49314
   311
fun generate_isa_dependencies ctxt thy include_thy file_name =
blanchet@49266
   312
  let
blanchet@49266
   313
    val path = file_name |> Path.explode
blanchet@49266
   314
    val _ = File.write path ""
blanchet@49314
   315
    val css_table = clasimpset_rule_table_of ctxt
blanchet@49266
   316
    val ths =
blanchet@49314
   317
      all_non_tautological_facts_of thy css_table
blanchet@49266
   318
      |> not include_thy ? filter_out (has_thy thy o snd)
blanchet@49266
   319
      |> map snd
blanchet@49266
   320
    val all_names = ths |> map Thm.get_name_hint
blanchet@49266
   321
    fun do_thm th =
blanchet@49266
   322
      let
blanchet@49266
   323
        val name = Thm.get_name_hint th
blanchet@49266
   324
        val deps = isabelle_dependencies_of all_names th
blanchet@49266
   325
        val s = fact_name_of name ^ ": " ^ space_implode " " deps ^ "\n"
blanchet@49266
   326
      in File.append path s end
blanchet@49266
   327
  in List.app do_thm ths end
blanchet@49266
   328
blanchet@49308
   329
fun generate_atp_dependencies ctxt (params as {provers, max_facts, ...}) thy
blanchet@49266
   330
                              include_thy file_name =
blanchet@49266
   331
  let
blanchet@49266
   332
    val path = file_name |> Path.explode
blanchet@49266
   333
    val _ = File.write path ""
blanchet@49314
   334
    val css_table = clasimpset_rule_table_of ctxt
blanchet@49266
   335
    val facts =
blanchet@49314
   336
      all_non_tautological_facts_of thy css_table
blanchet@49266
   337
      |> not include_thy ? filter_out (has_thy thy o snd)
blanchet@49266
   338
    val ths = facts |> map snd
blanchet@49266
   339
    val all_names = ths |> map Thm.get_name_hint
blanchet@49266
   340
    fun is_dep dep (_, th) = fact_name_of (Thm.get_name_hint th) = dep
blanchet@49266
   341
    fun add_isa_dep facts dep accum =
blanchet@49266
   342
      if exists (is_dep dep) accum then
blanchet@49266
   343
        accum
blanchet@49266
   344
      else case find_first (is_dep dep) facts of
blanchet@49304
   345
        SOME ((name, status), th) => accum @ [((name, status), th)]
blanchet@49266
   346
      | NONE => accum (* shouldn't happen *)
blanchet@49266
   347
    fun fix_name ((_, stature), th) =
blanchet@49304
   348
      ((fn () => th |> Thm.get_name_hint |> fact_name_of, stature), th)
blanchet@49266
   349
    fun do_thm th =
blanchet@49266
   350
      let
blanchet@49266
   351
        val name = Thm.get_name_hint th
blanchet@49266
   352
        val goal = goal_of_thm thy th
blanchet@49307
   353
        val (_, hyp_ts, concl_t) = ATP_Util.strip_subgoal ctxt goal 1
blanchet@49266
   354
        val deps =
blanchet@49266
   355
          case isabelle_dependencies_of all_names th of
blanchet@49266
   356
            [] => []
blanchet@49266
   357
          | isa_dep as [_] => isa_dep (* can hardly beat that *)
blanchet@49266
   358
          | isa_deps =>
blanchet@49266
   359
            let
blanchet@49266
   360
              val facts =
blanchet@49266
   361
                facts |> filter (fn (_, th') => thm_ord (th', th) = LESS)
blanchet@49266
   362
              val facts =
blanchet@49307
   363
                facts |> iterative_relevant_facts ctxt params (hd provers)
blanchet@49308
   364
                             (the max_facts) NONE hyp_ts concl_t
blanchet@49266
   365
                      |> fold (add_isa_dep facts) isa_deps
blanchet@49266
   366
                      |> map fix_name
blanchet@49266
   367
            in
blanchet@49266
   368
              case run_prover ctxt params facts goal of
blanchet@49266
   369
                {outcome = NONE, used_facts, ...} =>
blanchet@49266
   370
                used_facts |> map fst |> sort string_ord
blanchet@49266
   371
              | _ => isa_deps
blanchet@49266
   372
            end
blanchet@49266
   373
        val s = fact_name_of name ^ ": " ^ space_implode " " deps ^ "\n"
blanchet@49266
   374
      in File.append path s end
blanchet@49266
   375
  in List.app do_thm ths end
blanchet@49266
   376
blanchet@49266
   377
blanchet@49266
   378
(*** High-level communication with MaSh ***)
blanchet@49266
   379
blanchet@49316
   380
type mash_state =
blanchet@49316
   381
  {completed_thys : unit Symtab.table,
blanchet@49316
   382
   thy_facts : string list Symtab.table}
blanchet@49264
   383
blanchet@49316
   384
val mash_zero =
blanchet@49316
   385
  {completed_thys = Symtab.empty,
blanchet@49316
   386
   thy_facts = Symtab.empty}
blanchet@49316
   387
blanchet@49316
   388
local
blanchet@49316
   389
blanchet@49316
   390
fun mash_load (state as (true, _)) = state
blanchet@49316
   391
  | mash_load _ =
blanchet@49316
   392
    (true,
blanchet@49316
   393
     case mash_path state_file |> Path.explode |> File.read_lines of
blanchet@49316
   394
       [] => mash_zero
blanchet@49316
   395
     | comp_line :: facts_lines =>
blanchet@49316
   396
       let
blanchet@49316
   397
         fun comp_thys_of_line comp_line =
blanchet@49316
   398
           Symtab.make (comp_line |> space_explode " " |> map (rpair ()))
blanchet@49316
   399
         fun add_facts_line line =
blanchet@49316
   400
           case space_explode " " line of
blanchet@49316
   401
             thy :: facts => Symtab.update_new (thy, facts)
blanchet@49316
   402
           | _ => I (* shouldn't happen *)
blanchet@49316
   403
       in
blanchet@49316
   404
         {completed_thys = comp_thys_of_line comp_line,
blanchet@49316
   405
          thy_facts = fold add_facts_line facts_lines Symtab.empty}
blanchet@49316
   406
       end)
blanchet@49316
   407
blanchet@49316
   408
fun mash_save ({completed_thys, thy_facts} : mash_state) =
blanchet@49316
   409
  let
blanchet@49316
   410
    val path = mash_path state_file |> Path.explode
blanchet@49316
   411
    val comp_line = (completed_thys |> Symtab.keys |> space_implode " ") ^ "\n"
blanchet@49316
   412
    fun fact_line_for (thy, facts) = space_implode " " (thy :: facts) ^ "\n"
blanchet@49316
   413
  in
blanchet@49316
   414
    File.write path comp_line;
blanchet@49316
   415
    Symtab.fold (fn thy_fact => fn () =>
blanchet@49316
   416
                    File.append path (fact_line_for thy_fact)) thy_facts
blanchet@49316
   417
  end
blanchet@49316
   418
blanchet@49316
   419
val state =
blanchet@49316
   420
  Synchronized.var "Sledgehammer_Filter_MaSh.state" (false, mash_zero)
blanchet@49316
   421
blanchet@49316
   422
in
blanchet@49316
   423
blanchet@49316
   424
fun mash_change f =
blanchet@49316
   425
  Synchronized.change state (apsnd (tap mash_save o f) o mash_load)
blanchet@49316
   426
blanchet@49316
   427
fun mash_value () = Synchronized.change_result state (`snd o mash_load)
blanchet@49316
   428
blanchet@49316
   429
end
blanchet@49316
   430
blanchet@49316
   431
fun mash_reset () = mash_change (fn _ => (mash_RESET (); mash_zero))
blanchet@49316
   432
blanchet@49316
   433
fun mash_can_suggest_facts () =
blanchet@49316
   434
  not (Symtab.is_empty (#thy_facts (mash_value ())))
blanchet@49264
   435
blanchet@49313
   436
fun mash_suggest_facts ctxt params prover max_facts hyp_ts concl_t facts =
blanchet@49316
   437
  let
blanchet@49316
   438
    val access = []
blanchet@49316
   439
    val feats = []
blanchet@49316
   440
    val suggs = mash_SUGGEST access feats
blanchet@49316
   441
  in (facts, []) end
blanchet@49264
   442
blanchet@49316
   443
fun mash_can_learn_thy thy =
blanchet@49316
   444
  not (Symtab.defined (#completed_thys (mash_value ()))
blanchet@49316
   445
                      (Context.theory_name thy))
blanchet@49264
   446
blanchet@49313
   447
fun mash_learn_thy thy timeout = ()
blanchet@49313
   448
blanchet@49313
   449
fun mash_learn_proof thy t ths = ()
blanchet@49316
   450
(*###
blanchet@49316
   451
  let
blanchet@49316
   452
  in
blanchet@49316
   453
    mash_ADD
blanchet@49316
   454
  end
blanchet@49316
   455
*)
blanchet@49264
   456
blanchet@49308
   457
fun relevant_facts ctxt params prover max_facts
blanchet@49313
   458
        ({add, only, ...} : fact_override) hyp_ts concl_t facts =
blanchet@49303
   459
  if only then
blanchet@49304
   460
    facts
blanchet@49308
   461
  else if max_facts <= 0 then
blanchet@49303
   462
    []
blanchet@49303
   463
  else
blanchet@49303
   464
    let
blanchet@49303
   465
      val add_ths = Attrib.eval_thms ctxt add
blanchet@49307
   466
      fun prepend_facts ths accepts =
blanchet@49303
   467
        ((facts |> filter (member Thm.eq_thm_prop ths o snd)) @
blanchet@49307
   468
         (accepts |> filter_out (member Thm.eq_thm_prop ths o snd)))
blanchet@49308
   469
        |> take max_facts
blanchet@49313
   470
      val iter_facts =
blanchet@49313
   471
        iterative_relevant_facts ctxt params prover max_facts NONE hyp_ts
blanchet@49313
   472
                                 concl_t facts
blanchet@49313
   473
      val (mash_facts, mash_rejected) =
blanchet@49313
   474
        mash_suggest_facts ctxt params prover max_facts hyp_ts concl_t facts
blanchet@49313
   475
      val mesh_facts = iter_facts
blanchet@49303
   476
    in
blanchet@49313
   477
      mesh_facts
blanchet@49303
   478
      |> not (null add_ths) ? prepend_facts add_ths
blanchet@49303
   479
    end
blanchet@49303
   480
blanchet@49263
   481
end;