src/HOL/TPTP/mash_export.ML
author blanchet
Thu, 17 Jan 2013 19:20:56 +0100
changeset 51969 7bc58677860e
parent 51922 a86708897266
child 51980 7a7d1418301e
permissions -rw-r--r--
added step to skip some queries
     1 (*  Title:      HOL/TPTP/mash_export.ML
     2     Author:     Jasmin Blanchette, TU Muenchen
     3     Copyright   2012
     4 
     5 Export Isabelle theory information for MaSh (Machine-learning for Sledgehammer).
     6 *)
     7 
     8 signature MASH_EXPORT =
     9 sig
    10   type params = Sledgehammer_Provers.params
    11 
    12   val generate_accessibility :
    13     Proof.context -> theory list -> bool -> string -> unit
    14   val generate_features :
    15     Proof.context -> string -> theory list -> bool -> string -> unit
    16   val generate_isar_dependencies :
    17     Proof.context -> theory list -> bool -> string -> unit
    18   val generate_prover_dependencies :
    19     Proof.context -> params -> int * int option -> theory list -> bool -> string
    20     -> unit
    21   val generate_isar_commands :
    22     Proof.context -> string -> (int * int option) * int -> theory list -> string
    23     -> unit
    24   val generate_prover_commands :
    25     Proof.context -> params -> (int * int option) * int -> theory list -> string
    26     -> unit
    27   val generate_mepo_suggestions :
    28     Proof.context -> params -> (int * int option) * int -> theory list -> int
    29     -> string -> unit
    30   val generate_mesh_suggestions : int -> string -> string -> string -> unit
    31 end;
    32 
    33 structure MaSh_Export : MASH_EXPORT =
    34 struct
    35 
    36 open Sledgehammer_Fact
    37 open Sledgehammer_MePo
    38 open Sledgehammer_MaSh
    39 
    40 fun in_range (from, to) j =
    41   j >= from andalso (to = NONE orelse j <= the to)
    42 
    43 fun has_thm_thy th thy =
    44   Context.theory_name thy = Context.theory_name (theory_of_thm th)
    45 
    46 fun has_thys thys th = exists (has_thm_thy th) thys
    47 
    48 fun all_facts ctxt =
    49   let val css = Sledgehammer_Fact.clasimpset_rule_table_of ctxt in
    50     Sledgehammer_Fact.all_facts ctxt true false Symtab.empty [] [] css
    51     |> sort (thm_ord o pairself snd)
    52   end
    53 
    54 fun generate_accessibility ctxt thys include_thys file_name =
    55   let
    56     val path = file_name |> Path.explode
    57     val _ = File.write path ""
    58     fun do_fact fact prevs =
    59       let
    60         val s = encode_str fact ^ ": " ^ encode_strs prevs ^ "\n"
    61         val _ = File.append path s
    62       in [fact] end
    63     val facts =
    64       all_facts ctxt
    65       |> not include_thys ? filter_out (has_thys thys o snd)
    66       |> map (snd #> nickname_of_thm)
    67   in fold do_fact facts []; () end
    68 
    69 fun generate_features ctxt prover thys include_thys file_name =
    70   let
    71     val path = file_name |> Path.explode
    72     val _ = File.write path ""
    73     val facts =
    74       all_facts ctxt
    75       |> not include_thys ? filter_out (has_thys thys o snd)
    76     fun do_fact ((_, stature), th) =
    77       let
    78         val name = nickname_of_thm th
    79         val feats =
    80           features_of ctxt prover (theory_of_thm th) stature [prop_of th]
    81         val s =
    82           encode_str name ^ ": " ^ encode_features (sort_wrt fst feats) ^ "\n"
    83       in File.append path s end
    84   in List.app do_fact facts end
    85 
    86 fun isar_or_prover_dependencies_of ctxt params_opt facts name_tabs th
    87                                    isar_deps_opt =
    88   case params_opt of
    89     SOME (params as {provers = prover :: _, ...}) =>
    90     prover_dependencies_of ctxt params prover 0 facts name_tabs th |> snd
    91   | NONE =>
    92     case isar_deps_opt of
    93       SOME deps => deps
    94     | NONE => isar_dependencies_of name_tabs th
    95 
    96 fun generate_isar_or_prover_dependencies ctxt params_opt range thys include_thys
    97                                          file_name =
    98   let
    99     val path = file_name |> Path.explode
   100     val facts =
   101       all_facts ctxt |> not include_thys ? filter_out (has_thys thys o snd)
   102     val name_tabs = build_name_tables nickname_of_thm facts
   103     fun do_fact (j, (_, th)) =
   104       if in_range range j then
   105         let
   106           val name = nickname_of_thm th
   107           val _ = tracing ("Fact " ^ string_of_int j ^ ": " ^ name)
   108           val deps =
   109             isar_or_prover_dependencies_of ctxt params_opt facts name_tabs th
   110                                            NONE
   111         in encode_str name ^ ": " ^ encode_strs deps ^ "\n" end
   112       else
   113         ""
   114     val lines = Par_List.map do_fact (tag_list 1 facts)
   115   in File.write_list path lines end
   116 
   117 fun generate_isar_dependencies ctxt =
   118   generate_isar_or_prover_dependencies ctxt NONE (1, NONE)
   119 
   120 fun generate_prover_dependencies ctxt params =
   121   generate_isar_or_prover_dependencies ctxt (SOME params)
   122 
   123 fun is_bad_query ctxt ho_atp step j th isar_deps =
   124   j mod step <> 0 orelse
   125   Thm.legacy_get_kind th = "" orelse
   126   null (these (trim_dependencies th isar_deps)) orelse
   127   is_blacklisted_or_something ctxt ho_atp (Thm.get_name_hint th)
   128 
   129 fun generate_isar_or_prover_commands ctxt prover params_opt (range, step) thys
   130                                      file_name =
   131   let
   132     val ho_atp = Sledgehammer_Provers.is_ho_atp ctxt prover
   133     val path = file_name |> Path.explode
   134     val facts = all_facts ctxt
   135     val (new_facts, old_facts) = facts |> List.partition (has_thys thys o snd)
   136     val name_tabs = build_name_tables nickname_of_thm facts
   137     fun do_fact (j, ((name, ((_, stature), th)), prevs)) =
   138       if in_range range j then
   139         let
   140           val _ = tracing ("Fact " ^ string_of_int j ^ ": " ^ name)
   141           val feats =
   142             features_of ctxt prover (theory_of_thm th) stature [prop_of th]
   143           val isar_deps = isar_dependencies_of name_tabs th
   144           val deps =
   145             isar_or_prover_dependencies_of ctxt params_opt facts name_tabs th
   146                                            (SOME isar_deps)
   147           val core =
   148             encode_str name ^ ": " ^ encode_strs prevs ^ "; " ^
   149             encode_features (sort_wrt fst feats)
   150           val query =
   151             if is_bad_query ctxt ho_atp step j th isar_deps then ""
   152             else "? " ^ core ^ "\n"
   153           val update =
   154             "! " ^ core ^ "; " ^
   155             encode_strs (these (trim_dependencies th deps)) ^ "\n"
   156         in query ^ update end
   157       else
   158         ""
   159     val parents =
   160       map (nickname_of_thm o snd) (the_list (try List.last old_facts))
   161     val new_facts = new_facts |> map (`(nickname_of_thm o snd))
   162     val prevss = fst (split_last (parents :: map (single o fst) new_facts))
   163     val lines = Par_List.map do_fact (tag_list 1 (new_facts ~~ prevss))
   164   in File.write_list path lines end
   165 
   166 fun generate_isar_commands ctxt prover =
   167   generate_isar_or_prover_commands ctxt prover NONE
   168 
   169 fun generate_prover_commands ctxt (params as {provers = prover :: _, ...}) =
   170   generate_isar_or_prover_commands ctxt prover (SOME params)
   171 
   172 fun generate_mepo_suggestions ctxt (params as {provers = prover :: _, ...})
   173                               (range, step) thys max_suggs file_name =
   174   let
   175     val ho_atp = Sledgehammer_Provers.is_ho_atp ctxt prover
   176     val path = file_name |> Path.explode
   177     val facts = all_facts ctxt
   178     val (new_facts, old_facts) = facts |> List.partition (has_thys thys o snd)
   179     val name_tabs = build_name_tables nickname_of_thm facts
   180     fun do_fact (j, ((_, th), old_facts)) =
   181       if in_range range j then
   182         let
   183           val name = nickname_of_thm th
   184           val _ = tracing ("Fact " ^ string_of_int j ^ ": " ^ name)
   185           val goal = goal_of_thm (Proof_Context.theory_of ctxt) th
   186           val (_, hyp_ts, concl_t) = ATP_Util.strip_subgoal ctxt goal 1
   187           val isar_deps = isar_dependencies_of name_tabs th
   188         in
   189           if is_bad_query ctxt ho_atp step j th isar_deps then
   190             ""
   191           else
   192             let
   193               val suggs =
   194                 old_facts
   195                 |> Sledgehammer_MePo.mepo_suggested_facts ctxt params prover
   196                        max_suggs NONE hyp_ts concl_t
   197                 |> map (nickname_of_thm o snd)
   198             in encode_str name ^ ": " ^ encode_strs suggs ^ "\n" end
   199         end
   200       else
   201         ""
   202     fun accum x (yss as ys :: _) = (x :: ys) :: yss
   203     val old_factss = tl (fold accum new_facts [old_facts])
   204     val lines = Par_List.map do_fact (tag_list 1 (new_facts ~~ rev old_factss))
   205   in File.write_list path lines end
   206 
   207 fun generate_mesh_suggestions max_suggs mash_file_name mepo_file_name
   208                               mesh_file_name =
   209   let
   210     val mesh_path = Path.explode mesh_file_name
   211     val _ = File.write mesh_path ""
   212     fun do_fact (mash_line, mepo_line) =
   213       let
   214         val (name, mash_suggs) =
   215           extract_suggestions mash_line
   216           ||> (map fst #> weight_mash_facts)
   217         val (name', mepo_suggs) =
   218           extract_suggestions mepo_line
   219           ||> (map fst #> weight_mash_facts)
   220         val _ = if name = name' then () else error "Input files out of sync."
   221         val mess =
   222           [(mepo_weight, (mepo_suggs, [])),
   223            (mash_weight, (mash_suggs, []))]
   224         val mesh_suggs = mesh_facts (op =) max_suggs mess
   225         val mesh_line = encode_str name ^ ": " ^ encode_strs mesh_suggs ^ "\n"
   226       in File.append mesh_path mesh_line end
   227     val mash_lines = Path.explode mash_file_name |> File.read_lines
   228     val mepo_lines = Path.explode mepo_file_name |> File.read_lines
   229   in
   230     if length mash_lines = length mepo_lines then
   231       List.app do_fact (mash_lines ~~ mepo_lines)
   232     else
   233       warning "Skipped: MaSh file missing or out of sync with MePo file."
   234   end
   235 
   236 end;