src/HOL/Tools/ATP/atp_problem_generate.ML
changeset 53586 bd5fa6425993
parent 52995 ab98feb66684
child 54089 b13f6731f873
equal deleted inserted replaced
53585:fcd36f587512 53586:bd5fa6425993
     6 Translation of HOL to FOL for Metis and Sledgehammer.
     6 Translation of HOL to FOL for Metis and Sledgehammer.
     7 *)
     7 *)
     8 
     8 
     9 signature ATP_PROBLEM_GENERATE =
     9 signature ATP_PROBLEM_GENERATE =
    10 sig
    10 sig
    11   type ('a, 'b) ho_term = ('a, 'b) ATP_Problem.ho_term
    11   type ('a, 'b) atp_term = ('a, 'b) ATP_Problem.atp_term
    12   type connective = ATP_Problem.connective
    12   type atp_connective = ATP_Problem.atp_connective
    13   type ('a, 'b, 'c, 'd) formula = ('a, 'b, 'c, 'd) ATP_Problem.formula
    13   type ('a, 'b, 'c, 'd) atp_formula = ('a, 'b, 'c, 'd) ATP_Problem.atp_formula
    14   type atp_format = ATP_Problem.atp_format
    14   type atp_format = ATP_Problem.atp_format
    15   type formula_role = ATP_Problem.formula_role
    15   type atp_formula_role = ATP_Problem.atp_formula_role
    16   type 'a problem = 'a ATP_Problem.problem
    16   type 'a atp_problem = 'a ATP_Problem.atp_problem
    17 
    17 
    18   datatype mode = Metis | Sledgehammer | Sledgehammer_Completish | Exporter
    18   datatype mode = Metis | Sledgehammer | Sledgehammer_Completish | Exporter
    19 
    19 
    20   datatype scope = Global | Local | Assum | Chained
    20   datatype scope = Global | Local | Assum | Chained
    21   datatype status =
    21   datatype status =
    98   val is_type_enc_sound : type_enc -> bool
    98   val is_type_enc_sound : type_enc -> bool
    99   val type_enc_of_string : strictness -> string -> type_enc
    99   val type_enc_of_string : strictness -> string -> type_enc
   100   val adjust_type_enc : atp_format -> type_enc -> type_enc
   100   val adjust_type_enc : atp_format -> type_enc -> type_enc
   101   val is_lambda_free : term -> bool
   101   val is_lambda_free : term -> bool
   102   val mk_aconns :
   102   val mk_aconns :
   103     connective -> ('a, 'b, 'c, 'd) formula list -> ('a, 'b, 'c, 'd) formula
   103     atp_connective -> ('a, 'b, 'c, 'd) atp_formula list
   104   val unmangled_const : string -> string * (string, 'b) ho_term list
   104     -> ('a, 'b, 'c, 'd) atp_formula
       
   105   val unmangled_const : string -> string * (string, 'b) atp_term list
   105   val unmangled_const_name : string -> string list
   106   val unmangled_const_name : string -> string list
   106   val helper_table : ((string * bool) * (status * thm) list) list
   107   val helper_table : ((string * bool) * (status * thm) list) list
   107   val trans_lams_of_string :
   108   val trans_lams_of_string :
   108     Proof.context -> type_enc -> string -> term list -> term list * term list
   109     Proof.context -> type_enc -> string -> term list -> term list * term list
   109   val string_of_status : status -> string
   110   val string_of_status : status -> string
   110   val factsN : string
   111   val factsN : string
   111   val prepare_atp_problem :
   112   val prepare_atp_problem :
   112     Proof.context -> atp_format -> formula_role -> type_enc -> mode -> string
   113     Proof.context -> atp_format -> atp_formula_role -> type_enc -> mode
   113     -> bool -> bool -> bool -> term list -> term
   114     -> string -> bool -> bool -> bool -> term list -> term
   114     -> ((string * stature) * term) list
   115     -> ((string * stature) * term) list
   115     -> string problem * string Symtab.table * (string * stature) list vector
   116     -> string atp_problem * string Symtab.table * (string * stature) list vector
   116        * (string * term) list * int Symtab.table
   117        * (string * term) list * int Symtab.table
   117   val atp_problem_selection_weights : string problem -> (string * real) list
   118   val atp_problem_selection_weights : string atp_problem -> (string * real) list
   118   val atp_problem_term_order_info : string problem -> (string * int) list
   119   val atp_problem_term_order_info : string atp_problem -> (string * int) list
   119 end;
   120 end;
   120 
   121 
   121 structure ATP_Problem_Generate : ATP_PROBLEM_GENERATE =
   122 structure ATP_Problem_Generate : ATP_PROBLEM_GENERATE =
   122 struct
   123 struct
   123 
   124 
   824   | intentionalize_def t = t
   825   | intentionalize_def t = t
   825 
   826 
   826 type ifact =
   827 type ifact =
   827   {name : string,
   828   {name : string,
   828    stature : stature,
   829    stature : stature,
   829    role : formula_role,
   830    role : atp_formula_role,
   830    iformula : (string * string, typ, iterm, string * string) formula,
   831    iformula : (string * string, typ, iterm, string * string) atp_formula,
   831    atomic_types : typ list}
   832    atomic_types : typ list}
   832 
   833 
   833 fun update_iformula f ({name, stature, role, iformula, atomic_types} : ifact) =
   834 fun update_iformula f ({name, stature, role, iformula, atomic_types} : ifact) =
   834   {name = name, stature = stature, role = role, iformula = f iformula,
   835   {name = name, stature = stature, role = role, iformula = f iformula,
   835    atomic_types = atomic_types} : ifact
   836    atomic_types = atomic_types} : ifact
   914              map term Ts)
   915              map term Ts)
   915     | term (TFree (s, _)) = AType (`make_tfree s, [])
   916     | term (TFree (s, _)) = AType (`make_tfree s, [])
   916     | term (TVar z) = AType (tvar_name z, [])
   917     | term (TVar z) = AType (tvar_name z, [])
   917   in term end
   918   in term end
   918 
   919 
   919 fun ho_term_of_ho_type (AType (name, tys)) =
   920 fun atp_term_of_ho_type (AType (name, tys)) =
   920     ATerm ((name, []), map ho_term_of_ho_type tys)
   921     ATerm ((name, []), map atp_term_of_ho_type tys)
   921   | ho_term_of_ho_type _ = raise Fail "unexpected type"
   922   | atp_term_of_ho_type _ = raise Fail "unexpected type"
   922 
   923 
   923 fun ho_type_of_type_arg type_enc T =
   924 fun ho_type_of_type_arg type_enc T =
   924   if T = dummyT then NONE else SOME (raw_ho_type_of_typ type_enc T)
   925   if T = dummyT then NONE else SOME (raw_ho_type_of_typ type_enc T)
   925 
   926 
   926 (* This shouldn't clash with anything else. *)
   927 (* This shouldn't clash with anything else. *)
   981 
   982 
   982 fun process_type_args type_enc T_args =
   983 fun process_type_args type_enc T_args =
   983   if is_type_enc_native type_enc then
   984   if is_type_enc_native type_enc then
   984     (map (native_ho_type_of_typ type_enc false 0) T_args, [])
   985     (map (native_ho_type_of_typ type_enc false 0) T_args, [])
   985   else
   986   else
   986     ([], map_filter (Option.map ho_term_of_ho_type
   987     ([], map_filter (Option.map atp_term_of_ho_type
   987                      o ho_type_of_type_arg type_enc) T_args)
   988                      o ho_type_of_type_arg type_enc) T_args)
   988 
   989 
   989 fun class_atom type_enc (cl, T) =
   990 fun class_atom type_enc (cl, T) =
   990   let
   991   let
   991     val cl = `make_class cl
   992     val cl = `make_class cl
  2069   | _ => K NONE
  2070   | _ => K NONE
  2070 
  2071 
  2071 fun tag_with_type ctxt mono type_enc pos T tm =
  2072 fun tag_with_type ctxt mono type_enc pos T tm =
  2072   IConst (type_tag, T --> T, [T])
  2073   IConst (type_tag, T --> T, [T])
  2073   |> mangle_type_args_in_iterm type_enc
  2074   |> mangle_type_args_in_iterm type_enc
  2074   |> ho_term_of_iterm ctxt mono type_enc pos
  2075   |> atp_term_of_iterm ctxt mono type_enc pos
  2075   |> (fn ATerm ((s, tys), tms) => ATerm ((s, tys), tms @ [tm])
  2076   |> (fn ATerm ((s, tys), tms) => ATerm ((s, tys), tms @ [tm])
  2076        | _ => raise Fail "unexpected lambda-abstraction")
  2077        | _ => raise Fail "unexpected lambda-abstraction")
  2077 and ho_term_of_iterm ctxt mono type_enc pos =
  2078 and atp_term_of_iterm ctxt mono type_enc pos =
  2078   let
  2079   let
  2079     fun term site u =
  2080     fun term site u =
  2080       let
  2081       let
  2081         val (head, args) = strip_iterm_comb u
  2082         val (head, args) = strip_iterm_comb u
  2082         val pos =
  2083         val pos =
  2110   in term (Top_Level pos) end
  2111   in term (Top_Level pos) end
  2111 and formula_of_iformula ctxt mono type_enc should_guard_var =
  2112 and formula_of_iformula ctxt mono type_enc should_guard_var =
  2112   let
  2113   let
  2113     val thy = Proof_Context.theory_of ctxt
  2114     val thy = Proof_Context.theory_of ctxt
  2114     val level = level_of_type_enc type_enc
  2115     val level = level_of_type_enc type_enc
  2115     val do_term = ho_term_of_iterm ctxt mono type_enc
  2116     val do_term = atp_term_of_iterm ctxt mono type_enc
  2116     fun do_out_of_bound_type pos phi universal (name, T) =
  2117     fun do_out_of_bound_type pos phi universal (name, T) =
  2117       if should_guard_type ctxt mono type_enc
  2118       if should_guard_type ctxt mono type_enc
  2118              (fn () => should_guard_var thy level pos phi universal name) T then
  2119              (fn () => should_guard_var thy level pos phi universal name) T then
  2119         IVar (name, T)
  2120         IVar (name, T)
  2120         |> type_guard_iterm type_enc T
  2121         |> type_guard_iterm type_enc T
  2597         val (base_name as (base_s, _), T_args) =
  2598         val (base_name as (base_s, _), T_args) =
  2598           mangle_type_args_in_const type_enc base_name T_args
  2599           mangle_type_args_in_const type_enc base_name T_args
  2599         val base_ary = min_ary_of sym_tab0 base_s
  2600         val base_ary = min_ary_of sym_tab0 base_s
  2600         fun do_const name = IConst (name, T, T_args)
  2601         fun do_const name = IConst (name, T, T_args)
  2601         val filter_ty_args = filter_type_args_in_iterm thy ctrss type_enc
  2602         val filter_ty_args = filter_type_args_in_iterm thy ctrss type_enc
  2602         val ho_term_of = ho_term_of_iterm ctxt mono type_enc (SOME true)
  2603         val atp_term_of = atp_term_of_iterm ctxt mono type_enc (SOME true)
  2603         val name1 as (s1, _) =
  2604         val name1 as (s1, _) =
  2604           base_name |> ary - 1 > base_ary ? aliased_uncurried (ary - 1)
  2605           base_name |> ary - 1 > base_ary ? aliased_uncurried (ary - 1)
  2605         val name2 as (s2, _) = base_name |> aliased_uncurried ary
  2606         val name2 as (s2, _) = base_name |> aliased_uncurried ary
  2606         val (arg_Ts, _) = chop_fun ary T
  2607         val (arg_Ts, _) = chop_fun ary T
  2607         val bound_names =
  2608         val bound_names =
  2617           |> filter_ty_args
  2618           |> filter_ty_args
  2618         val do_bound_type = do_bound_type ctxt mono type_enc
  2619         val do_bound_type = do_bound_type ctxt mono type_enc
  2619         val eq =
  2620         val eq =
  2620           eq_formula type_enc (atomic_types_of T)
  2621           eq_formula type_enc (atomic_types_of T)
  2621                      (map (apsnd do_bound_type) bounds) false
  2622                      (map (apsnd do_bound_type) bounds) false
  2622                      (ho_term_of tm1) (ho_term_of tm2)
  2623                      (atp_term_of tm1) (atp_term_of tm2)
  2623       in
  2624       in
  2624         ([tm1, tm2],
  2625         ([tm1, tm2],
  2625          [Formula ((uncurried_alias_eq_prefix ^ s2, ""), role,
  2626          [Formula ((uncurried_alias_eq_prefix ^ s2, ""), role,
  2626                    eq |> maybe_negate, NONE,
  2627                    eq |> maybe_negate, NONE,
  2627                    isabelle_info non_rec_defN helper_rank)])
  2628                    isabelle_info non_rec_defN helper_rank)])