src/HOL/Tools/ATP/atp_problem_generate.ML
changeset 54798 287e569eebb2
parent 54791 3478fadb514e
child 54820 d9ab86c044fd
equal deleted inserted replaced
54797:be020ec8560c 54798:287e569eebb2
   259 val prefixed_app_op_name = const_prefix ^ app_op_name
   259 val prefixed_app_op_name = const_prefix ^ app_op_name
   260 val prefixed_type_tag_name = const_prefix ^ type_tag_name
   260 val prefixed_type_tag_name = const_prefix ^ type_tag_name
   261 
   261 
   262 (*Escaping of special characters.
   262 (*Escaping of special characters.
   263   Alphanumeric characters are left unchanged.
   263   Alphanumeric characters are left unchanged.
   264   The character _ goes to __
   264   The character _ goes to __.
   265   Characters in the range ASCII space to / go to _A to _P, respectively.
   265   Characters in the range ASCII space to / go to _A to _P, respectively.
   266   Other characters go to _nnn where nnn is the decimal ASCII code.*)
   266   Other characters go to _nnn where nnn is the decimal ASCII code. *)
   267 val upper_a_minus_space = Char.ord #"A" - Char.ord #" "
   267 val upper_a_minus_space = Char.ord #"A" - Char.ord #" "
   268 
   268 
   269 fun ascii_of_char c =
   269 fun ascii_of_char c =
   270   if Char.isAlphaNum c then
   270   if Char.isAlphaNum c then
   271     String.str c
   271     String.str c
   569     else
   569     else
   570       []
   570       []
   571   else
   571   else
   572     (s, T) |> Sign.const_typargs thy
   572     (s, T) |> Sign.const_typargs thy
   573 
   573 
   574 (* Converts an Isabelle/HOL term (with combinators) into an intermediate term.
   574 (* Converts an Isabelle/HOL term (with combinators) into an intermediate term. Also accumulates sort
   575    Also accumulates sort infomation. *)
   575    infomation. *)
   576 fun iterm_of_term thy type_enc bs (P $ Q) =
   576 fun iterm_of_term thy type_enc bs (P $ Q) =
   577     let
   577     let
   578       val (P', P_atomics_Ts) = iterm_of_term thy type_enc bs P
   578       val (P', P_atomics_Ts) = iterm_of_term thy type_enc bs P
   579       val (Q', Q_atomics_Ts) = iterm_of_term thy type_enc bs Q
   579       val (Q', Q_atomics_Ts) = iterm_of_term thy type_enc bs Q
   580     in (IApp (P', Q'), union (op =) P_atomics_Ts Q_atomics_Ts) end
   580     in (IApp (P', Q'), union (op =) P_atomics_Ts Q_atomics_Ts) end
   581   | iterm_of_term thy type_enc _ (Const (c, T)) =
   581   | iterm_of_term thy type_enc _ (Const (c, T)) =
   582     (IConst (`(make_fixed_const (SOME type_enc)) c, T,
   582     (IConst (`(make_fixed_const (SOME type_enc)) c, T, robust_const_type_args thy (c, T)),
   583              robust_const_type_args thy (c, T)),
       
   584      atomic_types_of T)
   583      atomic_types_of T)
   585   | iterm_of_term _ _ _ (Free (s, T)) =
   584   | iterm_of_term _ _ _ (Free (s, T)) = (IConst (`make_fixed_var s, T, []), atomic_types_of T)
   586     (IConst (`make_fixed_var s, T, []), atomic_types_of T)
       
   587   | iterm_of_term _ type_enc _ (Var (v as (s, _), T)) =
   585   | iterm_of_term _ type_enc _ (Var (v as (s, _), T)) =
   588     (if String.isPrefix Meson_Clausify.new_skolem_var_prefix s then
   586     (if String.isPrefix Meson_Clausify.new_skolem_var_prefix s then
   589        let
   587        let
   590          val Ts = T |> strip_type |> swap |> op ::
   588          val Ts = T |> strip_type |> swap |> op ::
   591          val s' = new_skolem_const_name s (length Ts)
   589          val s' = new_skolem_const_name s (length Ts)
   598     let
   596     let
   599       fun vary s = s |> AList.defined (op =) bs s ? vary o Symbol.bump_string
   597       fun vary s = s |> AList.defined (op =) bs s ? vary o Symbol.bump_string
   600       val s = vary s
   598       val s = vary s
   601       val name = `make_bound_var s
   599       val name = `make_bound_var s
   602       val (tm, atomic_Ts) = iterm_of_term thy type_enc ((s, (name, T)) :: bs) t
   600       val (tm, atomic_Ts) = iterm_of_term thy type_enc ((s, (name, T)) :: bs) t
   603     in (IAbs ((name, T), tm), union (op =) atomic_Ts (atomic_types_of T)) end
   601     in
       
   602       (IAbs ((name, T), tm), union (op =) atomic_Ts (atomic_types_of T))
       
   603     end
   604 
   604 
   605 (* "_query" and "_at" are for the ASCII-challenged Metis and Mirabelle. *)
   605 (* "_query" and "_at" are for the ASCII-challenged Metis and Mirabelle. *)
   606 val queries = ["?", "_query"]
   606 val queries = ["?", "_query"]
   607 val ats = ["@", "_at"]
   607 val ats = ["@", "_at"]
   608 
   608 
   834    role : atp_formula_role,
   834    role : atp_formula_role,
   835    iformula : (string * string, typ, iterm, string * string) atp_formula,
   835    iformula : (string * string, typ, iterm, string * string) atp_formula,
   836    atomic_types : typ list}
   836    atomic_types : typ list}
   837 
   837 
   838 fun update_iformula f ({name, stature, role, iformula, atomic_types} : ifact) =
   838 fun update_iformula f ({name, stature, role, iformula, atomic_types} : ifact) =
   839   {name = name, stature = stature, role = role, iformula = f iformula,
   839   {name = name, stature = stature, role = role, iformula = f iformula, atomic_types = atomic_types}
   840    atomic_types = atomic_types} : ifact
   840   : ifact
   841 
   841 
   842 fun ifact_lift f ({iformula, ...} : ifact) = f iformula
   842 fun ifact_lift f ({iformula, ...} : ifact) = f iformula
   843 
   843 
   844 fun insert_type thy get_T x xs =
   844 fun insert_type thy get_T x xs =
   845   let val T = get_T x in
   845   let val T = get_T x in
  1005 
  1005 
  1006 fun class_atoms type_enc (cls, T) =
  1006 fun class_atoms type_enc (cls, T) =
  1007   map (fn cl => class_atom type_enc (cl, T)) cls
  1007   map (fn cl => class_atom type_enc (cl, T)) cls
  1008 
  1008 
  1009 fun class_membs_of_types type_enc add_sorts_on_typ Ts =
  1009 fun class_membs_of_types type_enc add_sorts_on_typ Ts =
  1010   [] |> (polymorphism_of_type_enc type_enc <> Type_Class_Polymorphic andalso
  1010   [] |> level_of_type_enc type_enc <> No_Types ? fold add_sorts_on_typ Ts
  1011          level_of_type_enc type_enc <> No_Types)
       
  1012         ? fold add_sorts_on_typ Ts
       
  1013 
  1011 
  1014 fun mk_aconns c = split_last #> uncurry (fold_rev (mk_aconn c))
  1012 fun mk_aconns c = split_last #> uncurry (fold_rev (mk_aconn c))
  1015 
  1013 
  1016 fun mk_ahorn [] phi = phi
  1014 fun mk_ahorn [] phi = phi
  1017   | mk_ahorn phis psi = AConn (AImplies, [mk_aconns AAnd phis, psi])
  1015   | mk_ahorn phis psi = AConn (AImplies, [mk_aconns AAnd phis, psi])
  1778    (("fEx", false),
  1776    (("fEx", false),
  1779     @{thms fEx_table fComp_law fAll_law fEx_law} |> map (pair Non_Rec_Def))]
  1777     @{thms fEx_table fComp_law fAll_law fEx_law} |> map (pair Non_Rec_Def))]
  1780   |> map (apsnd (map (apsnd zero_var_indexes)))
  1778   |> map (apsnd (map (apsnd zero_var_indexes)))
  1781 
  1779 
  1782 fun bound_tvars type_enc sorts Ts =
  1780 fun bound_tvars type_enc sorts Ts =
  1783   case filter is_TVar Ts of
  1781   (case filter is_TVar Ts of
  1784     [] => I
  1782     [] => I
  1785   | Ts =>
  1783   | Ts =>
  1786     (sorts ? mk_ahorn (Ts |> class_membs_of_types type_enc add_sorts_on_tvar
  1784     ((sorts andalso polymorphism_of_type_enc type_enc <> Type_Class_Polymorphic)
  1787                           |> map (class_atom type_enc)))
  1785      ? mk_ahorn (Ts
       
  1786        |> class_membs_of_types type_enc add_sorts_on_tvar
       
  1787        |> map (class_atom type_enc)))
  1788     #> (case type_enc of
  1788     #> (case type_enc of
  1789           Native (_, poly, _) =>
  1789          Native (_, poly, _) =>
  1790           mk_atyquant AForall
  1790          mk_atyquant AForall (map (fn TVar (z as (_, S)) =>
  1791               (map (fn TVar (z as (_, S)) =>
  1791            (AType (tvar_name z, []),
  1792                        (AType (tvar_name z, []),
  1792             if poly = Type_Class_Polymorphic then
  1793                         if poly = Type_Class_Polymorphic then
  1793               map (`make_class) (normalize_classes S)
  1794                           map (`make_class) (normalize_classes S)
  1794             else
  1795                         else
  1795               [])) Ts)
  1796                           [])) Ts)
  1796         | _ => mk_aquant AForall (map (fn TVar z => (tvar_name z, NONE)) Ts)))
  1797         | _ =>
       
  1798           mk_aquant AForall (map (fn TVar z => (tvar_name z, NONE)) Ts))
       
  1799 
  1797 
  1800 fun eq_formula type_enc atomic_Ts bounds pred_sym tm1 tm2 =
  1798 fun eq_formula type_enc atomic_Ts bounds pred_sym tm1 tm2 =
  1801   (if pred_sym then AConn (AIff, [AAtom tm1, AAtom tm2])
  1799   (if pred_sym then AConn (AIff, [AAtom tm1, AAtom tm2])
  1802    else AAtom (ATerm ((`I tptp_equal, []), [tm1, tm2])))
  1800    else AAtom (ATerm ((`I tptp_equal, []), [tm1, tm2])))
  1803   |> mk_aquant AForall bounds
  1801   |> mk_aquant AForall bounds
  2216            |> bound_tvars type_enc true atomic_types, NONE, [])
  2214            |> bound_tvars type_enc true atomic_types, NONE, [])
  2217 
  2215 
  2218 fun lines_of_free_types type_enc (facts : ifact list) =
  2216 fun lines_of_free_types type_enc (facts : ifact list) =
  2219   if is_type_enc_polymorphic type_enc then
  2217   if is_type_enc_polymorphic type_enc then
  2220     let
  2218     let
  2221       val type_classes =
  2219       val type_classes = (polymorphism_of_type_enc type_enc = Type_Class_Polymorphic)
  2222         (polymorphism_of_type_enc type_enc = Type_Class_Polymorphic)
       
  2223       fun line j (cl, T) =
  2220       fun line j (cl, T) =
  2224         if type_classes then
  2221         if type_classes then
  2225           Class_Memb (class_memb_prefix ^ string_of_int j, [],
  2222           Class_Memb (class_memb_prefix ^ string_of_int j, [],
  2226                       native_ho_type_of_typ type_enc false 0 T, `make_class cl)
  2223                       native_ho_type_of_typ type_enc false 0 T, `make_class cl)
  2227         else
  2224         else
  2228           Formula ((tfree_clause_prefix ^ string_of_int j, ""), Hypothesis,
  2225           Formula ((tfree_clause_prefix ^ string_of_int j, ""), Hypothesis,
  2229                    class_atom type_enc (cl, T), NONE, [])
  2226                    class_atom type_enc (cl, T), NONE, [])
  2230       val membs =
  2227       val membs =
  2231         fold (union (op =)) (map #atomic_types facts) []
  2228         fold (union (op =)) (map #atomic_types facts) []
  2232         |> class_membs_of_types type_enc add_sorts_on_tfree
  2229         |> class_membs_of_types type_enc add_sorts_on_tfree
  2233     in map2 line (0 upto length membs - 1) membs end
  2230     in
       
  2231       map2 line (0 upto length membs - 1) membs
       
  2232     end
  2234   else
  2233   else
  2235     []
  2234     []
  2236 
  2235 
  2237 (** Symbol declarations **)
  2236 (** Symbol declarations **)
  2238 
  2237 
  2401            |> bound_tvars type_enc true (atomic_types_of T),
  2400            |> bound_tvars type_enc true (atomic_types_of T),
  2402            NONE, isabelle_info inductiveN helper_rank)
  2401            NONE, isabelle_info inductiveN helper_rank)
  2403 
  2402 
  2404 fun line_of_tags_mono_type ctxt mono type_enc T =
  2403 fun line_of_tags_mono_type ctxt mono type_enc T =
  2405   let val x_var = ATerm ((`make_bound_var "X", []), []) in
  2404   let val x_var = ATerm ((`make_bound_var "X", []), []) in
  2406     Formula ((tags_sym_formula_prefix ^ ascii_of (mangled_type type_enc T), ""),
  2405     Formula ((tags_sym_formula_prefix ^ ascii_of (mangled_type type_enc T), ""), Axiom,
  2407              Axiom,
       
  2408              eq_formula type_enc (atomic_types_of T) [] false
  2406              eq_formula type_enc (atomic_types_of T) [] false
  2409                   (tag_with_type ctxt mono type_enc NONE T x_var) x_var,
  2407                   (tag_with_type ctxt mono type_enc NONE T x_var) x_var,
  2410              NONE, isabelle_info non_rec_defN helper_rank)
  2408              NONE, isabelle_info non_rec_defN helper_rank)
  2411   end
  2409   end
  2412 
  2410