src/HOL/Tools/Sledgehammer/sledgehammer_atp_translate.ML
changeset 42939 0134d6650092
parent 42895 c8d9bce88f89
child 42943 62a14c80d194
equal deleted inserted replaced
42938:c124032ac96f 42939:0134d6650092
     7 *)
     7 *)
     8 
     8 
     9 signature SLEDGEHAMMER_ATP_TRANSLATE =
     9 signature SLEDGEHAMMER_ATP_TRANSLATE =
    10 sig
    10 sig
    11   type 'a fo_term = 'a ATP_Problem.fo_term
    11   type 'a fo_term = 'a ATP_Problem.fo_term
       
    12   type format = ATP_Problem.format
    12   type formula_kind = ATP_Problem.formula_kind
    13   type formula_kind = ATP_Problem.formula_kind
    13   type 'a problem = 'a ATP_Problem.problem
    14   type 'a problem = 'a ATP_Problem.problem
    14   type locality = Sledgehammer_Filter.locality
    15   type locality = Sledgehammer_Filter.locality
    15 
    16 
    16   datatype polymorphism = Polymorphic | Monomorphic | Mangled_Monomorphic
    17   datatype polymorphism = Polymorphic | Monomorphic | Mangled_Monomorphic
    42   val unmangled_const : string -> string * string fo_term list
    43   val unmangled_const : string -> string * string fo_term list
    43   val translate_atp_fact :
    44   val translate_atp_fact :
    44     Proof.context -> bool -> (string * locality) * thm
    45     Proof.context -> bool -> (string * locality) * thm
    45     -> translated_formula option * ((string * locality) * thm)
    46     -> translated_formula option * ((string * locality) * thm)
    46   val prepare_atp_problem :
    47   val prepare_atp_problem :
    47     Proof.context -> formula_kind -> formula_kind -> type_system -> bool
    48     Proof.context -> format -> formula_kind -> formula_kind -> type_system
    48     -> term list -> term
    49     -> bool -> term list -> term
    49     -> (translated_formula option * ((string * 'a) * thm)) list
    50     -> (translated_formula option * ((string * 'a) * thm)) list
    50     -> string problem * string Symtab.table * int * int
    51     -> string problem * string Symtab.table * int * int
    51        * (string * 'a) list vector * int list * int Symtab.table
    52        * (string * 'a) list vector * int list * int Symtab.table
    52   val atp_problem_weights : string problem -> (string * real) list
    53   val atp_problem_weights : string problem -> (string * real) list
    53 end;
    54 end;
   906              Intro => intro_info
   907              Intro => intro_info
   907            | Elim => elim_info
   908            | Elim => elim_info
   908            | Simp => simp_info
   909            | Simp => simp_info
   909            | _ => NONE)
   910            | _ => NONE)
   910 
   911 
   911 fun formula_line_for_class_rel_clause (ClassRelClause {name, subclass,
   912 fun formula_line_for_class_rel_clause
   912                                                        superclass, ...}) =
   913         (ClassRelClause {name, subclass, superclass, ...}) =
   913   let val ty_arg = ATerm (`I "T", []) in
   914   let val ty_arg = ATerm (`I "T", []) in
   914     Formula (class_rel_clause_prefix ^ ascii_of name, Axiom,
   915     Formula (class_rel_clause_prefix ^ ascii_of name, Axiom,
   915              AConn (AImplies, [AAtom (ATerm (subclass, [ty_arg])),
   916              AConn (AImplies, [AAtom (ATerm (subclass, [ty_arg])),
   916                                AAtom (ATerm (superclass, [ty_arg]))])
   917                                AAtom (ATerm (superclass, [ty_arg]))])
   917              |> close_formula_universally, intro_info, NONE)
   918              |> close_formula_universally, intro_info, NONE)
   920 fun fo_literal_from_arity_literal (TConsLit (c, t, args)) =
   921 fun fo_literal_from_arity_literal (TConsLit (c, t, args)) =
   921     (true, ATerm (c, [ATerm (t, map (fn arg => ATerm (arg, [])) args)]))
   922     (true, ATerm (c, [ATerm (t, map (fn arg => ATerm (arg, [])) args)]))
   922   | fo_literal_from_arity_literal (TVarLit (c, sort)) =
   923   | fo_literal_from_arity_literal (TVarLit (c, sort)) =
   923     (false, ATerm (c, [ATerm (sort, [])]))
   924     (false, ATerm (c, [ATerm (sort, [])]))
   924 
   925 
   925 fun formula_line_for_arity_clause (ArityClause {name, prem_lits, concl_lits,
   926 fun formula_line_for_arity_clause
   926                                                 ...}) =
   927         (ArityClause {name, prem_lits, concl_lits, ...}) =
   927   Formula (arity_clause_prefix ^ ascii_of name, Axiom,
   928   Formula (arity_clause_prefix ^ ascii_of name, Axiom,
   928            mk_ahorn (map (formula_from_fo_literal o apfst not
   929            mk_ahorn (map (formula_from_fo_literal o apfst not
   929                           o fo_literal_from_arity_literal) prem_lits)
   930                           o fo_literal_from_arity_literal) prem_lits)
   930                     (formula_from_fo_literal
   931                     (formula_from_fo_literal
   931                          (fo_literal_from_arity_literal concl_lits))
   932                          (fo_literal_from_arity_literal concl_lits))
   933 
   934 
   934 fun formula_line_for_conjecture ctxt nonmono_Ts type_sys
   935 fun formula_line_for_conjecture ctxt nonmono_Ts type_sys
   935         ({name, kind, combformula, ...} : translated_formula) =
   936         ({name, kind, combformula, ...} : translated_formula) =
   936   Formula (conjecture_prefix ^ name, kind,
   937   Formula (conjecture_prefix ^ name, kind,
   937            formula_from_combformula ctxt nonmono_Ts type_sys
   938            formula_from_combformula ctxt nonmono_Ts type_sys
   938                                     is_var_nonmonotonic_in_formula false
   939                is_var_nonmonotonic_in_formula false
   939                                     (close_combformula_universally combformula)
   940                (close_combformula_universally combformula)
   940            |> close_formula_universally, NONE, NONE)
   941            |> close_formula_universally, NONE, NONE)
   941 
   942 
   942 fun free_type_literals type_sys ({atomic_types, ...} : translated_formula) =
   943 fun free_type_literals type_sys ({atomic_types, ...} : translated_formula) =
   943   atomic_types |> atp_type_literals_for_types type_sys Conjecture
   944   atomic_types |> atp_type_literals_for_types type_sys Conjecture
   944                |> map fo_literal_from_type_literal
   945                |> map fo_literal_from_type_literal
  1181 
  1182 
  1182 fun should_add_ti_ti_helper (Tags (Polymorphic, level, Heavy)) =
  1183 fun should_add_ti_ti_helper (Tags (Polymorphic, level, Heavy)) =
  1183     level = Nonmonotonic_Types orelse level = Finite_Types
  1184     level = Nonmonotonic_Types orelse level = Finite_Types
  1184   | should_add_ti_ti_helper _ = false
  1185   | should_add_ti_ti_helper _ = false
  1185 
  1186 
       
  1187 fun offset_of_heading_in_problem _ [] j = j
       
  1188   | offset_of_heading_in_problem needle ((heading, lines) :: problem) j =
       
  1189     if heading = needle then j
       
  1190     else offset_of_heading_in_problem needle problem (j + length lines)
       
  1191 
  1186 val type_declsN = "Types"
  1192 val type_declsN = "Types"
  1187 val sym_declsN = "Symbol types"
  1193 val sym_declsN = "Symbol types"
  1188 val factsN = "Relevant facts"
  1194 val factsN = "Relevant facts"
  1189 val class_relsN = "Class relationships"
  1195 val class_relsN = "Class relationships"
  1190 val aritiesN = "Arities"
  1196 val aritiesN = "Arities"
  1191 val helpersN = "Helper facts"
  1197 val helpersN = "Helper facts"
  1192 val conjsN = "Conjectures"
  1198 val conjsN = "Conjectures"
  1193 val free_typesN = "Type variables"
  1199 val free_typesN = "Type variables"
  1194 
  1200 
  1195 fun offset_of_heading_in_problem _ [] j = j
  1201 fun prepare_atp_problem ctxt format conj_sym_kind prem_kind type_sys
  1196   | offset_of_heading_in_problem needle ((heading, lines) :: problem) j =
  1202                         explicit_apply hyp_ts concl_t facts =
  1197     if heading = needle then j
       
  1198     else offset_of_heading_in_problem needle problem (j + length lines)
       
  1199 
       
  1200 fun prepare_atp_problem ctxt conj_sym_kind prem_kind type_sys explicit_apply
       
  1201                         hyp_ts concl_t facts =
       
  1202   let
  1203   let
  1203     val (fact_names, (conjs, facts, class_rel_clauses, arity_clauses)) =
  1204     val (fact_names, (conjs, facts, class_rel_clauses, arity_clauses)) =
  1204       translate_formulas ctxt prem_kind type_sys hyp_ts concl_t facts
  1205       translate_formulas ctxt prem_kind type_sys hyp_ts concl_t facts
  1205     val sym_tab = conjs @ facts |> sym_table_for_facts explicit_apply
  1206     val sym_tab = conjs @ facts |> sym_table_for_facts explicit_apply
  1206     val nonmono_Ts = conjs @ facts |> nonmonotonic_types_for_facts ctxt type_sys
  1207     val nonmono_Ts = conjs @ facts |> nonmonotonic_types_for_facts ctxt type_sys
  1221       |> problem_lines_for_sym_decl_table ctxt conj_sym_kind lavish_nonmono_Ts
  1222       |> problem_lines_for_sym_decl_table ctxt conj_sym_kind lavish_nonmono_Ts
  1222                                           type_sys
  1223                                           type_sys
  1223     val helper_lines =
  1224     val helper_lines =
  1224       map (formula_line_for_fact ctxt helper_prefix lavish_nonmono_Ts type_sys)
  1225       map (formula_line_for_fact ctxt helper_prefix lavish_nonmono_Ts type_sys)
  1225           (0 upto length helpers - 1 ~~ helpers)
  1226           (0 upto length helpers - 1 ~~ helpers)
  1226       |> should_add_ti_ti_helper type_sys ? cons (ti_ti_helper_fact ())
  1227       |> (if should_add_ti_ti_helper type_sys then
       
  1228             cons (ti_ti_helper_fact ())
       
  1229           else
       
  1230             I)
  1227     (* Reordering these might confuse the proof reconstruction code or the SPASS
  1231     (* Reordering these might confuse the proof reconstruction code or the SPASS
  1228        Flotter hack. *)
  1232        Flotter hack. *)
  1229     val problem =
  1233     val problem =
  1230       [(sym_declsN, sym_decl_lines),
  1234       [(sym_declsN, sym_decl_lines),
  1231        (factsN, map (formula_line_for_fact ctxt fact_prefix nonmono_Ts type_sys)
  1235        (factsN, map (formula_line_for_fact ctxt fact_prefix nonmono_Ts type_sys)
  1241       |> (case type_sys of
  1245       |> (case type_sys of
  1242             Simple_Types _ =>
  1246             Simple_Types _ =>
  1243             cons (type_declsN,
  1247             cons (type_declsN,
  1244                   map decl_line_for_tff_type (tff_types_in_problem problem))
  1248                   map decl_line_for_tff_type (tff_types_in_problem problem))
  1245           | _ => I)
  1249           | _ => I)
       
  1250     val problem =
       
  1251       problem |> (if format = CNF_UEQ then filter_cnf_ueq_problem else I)
  1246     val (problem, pool) =
  1252     val (problem, pool) =
  1247       problem |> nice_atp_problem (Config.get ctxt readable_names)
  1253       problem |> nice_atp_problem (Config.get ctxt readable_names)
  1248     val helpers_offset = offset_of_heading_in_problem helpersN problem 0
  1254     val helpers_offset = offset_of_heading_in_problem helpersN problem 0
  1249     val typed_helpers =
  1255     val typed_helpers =
  1250       map_filter (fn (j, {name, ...}) =>
  1256       map_filter (fn (j, {name, ...}) =>