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