src/HOL/Tools/ATP/atp_problem_generate.ML
changeset 49148 a5ab5964065f
parent 49147 9aa0fad4e864
child 49149 fa23e699494c
equal deleted inserted replaced
49147:9aa0fad4e864 49148:a5ab5964065f
   357 fun make_all_bound_var x = all_bound_var_prefix ^ ascii_of x
   357 fun make_all_bound_var x = all_bound_var_prefix ^ ascii_of x
   358 fun make_exist_bound_var x = exist_bound_var_prefix ^ ascii_of x
   358 fun make_exist_bound_var x = exist_bound_var_prefix ^ ascii_of x
   359 fun make_schematic_var v = schematic_var_prefix ^ ascii_of_indexname v
   359 fun make_schematic_var v = schematic_var_prefix ^ ascii_of_indexname v
   360 fun make_fixed_var x = fixed_var_prefix ^ ascii_of x
   360 fun make_fixed_var x = fixed_var_prefix ^ ascii_of x
   361 
   361 
   362 fun make_schematic_type_var (x, i) =
   362 fun make_tvar (s, i) = tvar_prefix ^ (ascii_of_indexname (unprefix "'" s, i))
   363   tvar_prefix ^ (ascii_of_indexname (unprefix "'" x, i))
   363 fun make_tfree s = tfree_prefix ^ (ascii_of (unprefix "'" s))
   364 fun make_fixed_type_var x = tfree_prefix ^ (ascii_of (unprefix "'" x))
   364 fun tvar_name (x as (s, _)) = (make_tvar x, s)
   365 
   365 
   366 (* "HOL.eq" and choice are mapped to the ATP's equivalents *)
   366 (* "HOL.eq" and choice are mapped to the ATP's equivalents *)
   367 local
   367 local
   368   val choice_const = (fst o dest_Const o HOLogic.choice_const) Term.dummyT
   368   val choice_const = (fst o dest_Const o HOLogic.choice_const) Term.dummyT
   369   fun default c = const_prefix ^ lookup_const c
   369   fun default c = const_prefix ^ lookup_const c
   405                        not (member (op =) atp_monomorph_bad_consts s)
   405                        not (member (op =) atp_monomorph_bad_consts s)
   406                        ? add_schematic_const x
   406                        ? add_schematic_const x
   407                       | _ => I)
   407                       | _ => I)
   408 fun atp_schematic_consts_of t = add_schematic_consts_of t Symtab.empty
   408 fun atp_schematic_consts_of t = add_schematic_consts_of t Symtab.empty
   409 
   409 
       
   410 val tvar_a_str = "'a"
       
   411 val tvar_a = TVar ((tvar_a_str, 0), HOLogic.typeS)
       
   412 val tvar_a_name = tvar_name (tvar_a_str, 0)
       
   413 val itself_name = `make_fixed_type_const @{type_name itself}
       
   414 val TYPE_name = `(make_fixed_const NONE) @{const_name TYPE}
       
   415 val tvar_a_atype = AType (tvar_a_name, [])
       
   416 val a_itself_atype = AType (itself_name, [tvar_a_atype])
       
   417 
   410 (** Definitions and functions for FOL clauses and formulas for TPTP **)
   418 (** Definitions and functions for FOL clauses and formulas for TPTP **)
   411 
   419 
   412 (** Isabelle arities **)
   420 (** Isabelle arities **)
   413 
       
   414 type arity_atom = name * name * name list
       
   415 
   421 
   416 val type_class = the_single @{sort type}
   422 val type_class = the_single @{sort type}
   417 
   423 
   418 type arity_clause =
   424 type arity_clause =
   419   {name : string,
   425   {name : string,
   420    prem_atoms : arity_atom list,
   426    prems : (string * typ) list,
   421    concl_atom : arity_atom}
   427    concl : string * typ}
   422 
   428 
   423 fun add_prem_atom tvar =
   429 fun add_prem_atom T = fold (fn s => s <> type_class ? cons (s, T))
   424   fold (fn s => s <> type_class ? cons (`make_type_class s, `I tvar, []))
       
   425 
   430 
   426 (* Arity of type constructor "tcon :: (arg1, ..., argN) res" *)
   431 (* Arity of type constructor "tcon :: (arg1, ..., argN) res" *)
   427 fun make_axiom_arity_clause (tcons, name, (cls, args)) =
   432 fun make_axiom_arity_clause (tc, name, (class, args)) =
   428   let
   433   let
   429     val tvars = map (prefix tvar_prefix o string_of_int) (1 upto length args)
   434     val tvars =
   430     val tvars_srts = ListPair.zip (tvars, args)
   435       map (fn j => TVar ((tvar_a_str, j), @{sort HOL.type}))
       
   436           (1 upto length args)
   431   in
   437   in
   432     {name = name,
   438     {name = name, prems = fold (uncurry add_prem_atom) (tvars ~~ args) [],
   433      prem_atoms = [] |> fold (uncurry add_prem_atom) tvars_srts,
   439      concl = (class, Type (tc, tvars))}
   434      concl_atom = (`make_type_class cls, `make_fixed_type_const tcons,
       
   435                    tvars ~~ tvars)}
       
   436   end
   440   end
   437 
   441 
   438 fun arity_clause _ _ (_, []) = []
   442 fun arity_clause _ _ (_, []) = []
   439   | arity_clause seen n (tcons, ("HOL.type", _) :: ars) =  (* ignore *)
   443   | arity_clause seen n (tcons, ("HOL.type", _) :: ars) =  (* ignore *)
   440     arity_clause seen n (tcons, ars)
   444     arity_clause seen n (tcons, ars)
   485 
   489 
   486 (** Isabelle class relations **)
   490 (** Isabelle class relations **)
   487 
   491 
   488 type class_rel_clause =
   492 type class_rel_clause =
   489   {name : string,
   493   {name : string,
   490    subclass : name,
   494    subclass : string,
   491    superclass : name}
   495    superclass : string}
   492 
   496 
   493 (* Generate all pairs (sub, super) such that sub is a proper subclass of super
   497 (* Generate all pairs (sub, super) such that sub is a proper subclass of super
   494    in theory "thy". *)
   498    in theory "thy". *)
   495 fun class_pairs _ [] _ = []
   499 fun class_pairs _ [] _ = []
   496   | class_pairs thy subs supers =
   500   | class_pairs thy subs supers =
   499         fun add_super sub super = class_less (sub, super) ? cons (sub, super)
   503         fun add_super sub super = class_less (sub, super) ? cons (sub, super)
   500         fun add_supers sub = fold (add_super sub) supers
   504         fun add_supers sub = fold (add_super sub) supers
   501       in fold add_supers subs [] end
   505       in fold add_supers subs [] end
   502 
   506 
   503 fun make_class_rel_clause (sub, super) =
   507 fun make_class_rel_clause (sub, super) =
   504   {name = sub ^ "_" ^ super, subclass = `make_type_class sub,
   508   {name = sub ^ "_" ^ super, subclass = sub, superclass = super}
   505    superclass = `make_type_class super}
       
   506 
   509 
   507 fun make_class_rel_clauses thy subs supers =
   510 fun make_class_rel_clauses thy subs supers =
   508   map make_class_rel_clause (class_pairs thy subs supers)
   511   map make_class_rel_clause (class_pairs thy subs supers)
   509 
   512 
   510 (* intermediate terms *)
   513 (* intermediate terms *)
   525     fun stripc (IApp (t, u), ts) = stripc (t, u :: ts)
   528     fun stripc (IApp (t, u), ts) = stripc (t, u :: ts)
   526       | stripc x = x
   529       | stripc x = x
   527   in stripc (u, []) end
   530   in stripc (u, []) end
   528 
   531 
   529 fun atomic_types_of T = fold_atyps (insert (op =)) T []
   532 fun atomic_types_of T = fold_atyps (insert (op =)) T []
   530 
       
   531 val tvar_a_str = "'a"
       
   532 val tvar_a = TVar ((tvar_a_str, 0), HOLogic.typeS)
       
   533 val tvar_a_name = (make_schematic_type_var (tvar_a_str, 0), tvar_a_str)
       
   534 val itself_name = `make_fixed_type_const @{type_name itself}
       
   535 val TYPE_name = `(make_fixed_const NONE) @{const_name TYPE}
       
   536 val tvar_a_atype = AType (tvar_a_name, [])
       
   537 val a_itself_atype = AType (itself_name, [tvar_a_atype])
       
   538 
   533 
   539 fun new_skolem_const_name s num_T_args =
   534 fun new_skolem_const_name s num_T_args =
   540   [new_skolem_const_prefix, s, string_of_int num_T_args]
   535   [new_skolem_const_prefix, s, string_of_int num_T_args]
   541   |> Long_Name.implode
   536   |> Long_Name.implode
   542 
   537 
   870         else
   865         else
   871           All_Type_Args
   866           All_Type_Args
   872       end
   867       end
   873   end
   868   end
   874 
   869 
   875 (* Make atoms for sorted type variables. *)
       
   876 fun generic_add_sorts_on_type (_, []) = I
       
   877   | generic_add_sorts_on_type ((x, i), s :: ss) =
       
   878     generic_add_sorts_on_type ((x, i), ss)
       
   879     #> (if s = the_single @{sort HOL.type} then
       
   880           I
       
   881         else if i = ~1 then
       
   882           insert (op =) (`make_type_class s, `make_fixed_type_var x)
       
   883         else
       
   884           insert (op =) (`make_type_class s,
       
   885                          (make_schematic_type_var (x, i), x)))
       
   886 fun add_sorts_on_tfree (TFree (s, S)) = generic_add_sorts_on_type ((s, ~1), S)
       
   887   | add_sorts_on_tfree _ = I
       
   888 fun add_sorts_on_tvar (TVar z) = generic_add_sorts_on_type z
       
   889   | add_sorts_on_tvar _ = I
       
   890 
       
   891 fun type_class_formula type_enc class arg =
       
   892   AAtom (ATerm ((class, []), arg ::
       
   893       (case type_enc of
       
   894          Native (First_Order, Raw_Polymorphic Without_Phantom_Type_Vars, _) =>
       
   895          [ATerm ((TYPE_name, []), [arg])]
       
   896        | _ => [])))
       
   897 fun formulas_for_types type_enc add_sorts_on_typ Ts =
       
   898   [] |> level_of_type_enc type_enc <> No_Types ? fold add_sorts_on_typ Ts
       
   899      |> map (fn (class, name) =>
       
   900                 type_class_formula type_enc class (ATerm ((name, []), [])))
       
   901 
       
   902 fun mk_aconns c phis =
       
   903   let val (phis', phi') = split_last phis in
       
   904     fold_rev (mk_aconn c) phis' phi'
       
   905   end
       
   906 fun mk_ahorn [] phi = phi
       
   907   | mk_ahorn phis psi = AConn (AImplies, [mk_aconns AAnd phis, psi])
       
   908 fun mk_aquant _ [] phi = phi
       
   909   | mk_aquant q xs (phi as AQuant (q', xs', phi')) =
       
   910     if q = q' then AQuant (q, xs @ xs', phi') else AQuant (q, xs, phi)
       
   911   | mk_aquant q xs phi = AQuant (q, xs, phi)
       
   912 
       
   913 fun close_universally add_term_vars phi =
       
   914   let
       
   915     fun add_formula_vars bounds (AQuant (_, xs, phi)) =
       
   916         add_formula_vars (map fst xs @ bounds) phi
       
   917       | add_formula_vars bounds (AConn (_, phis)) =
       
   918         fold (add_formula_vars bounds) phis
       
   919       | add_formula_vars bounds (AAtom tm) = add_term_vars bounds tm
       
   920   in mk_aquant AForall (add_formula_vars [] phi []) phi end
       
   921 
       
   922 fun add_term_vars bounds (ATerm ((name as (s, _), _), tms)) =
       
   923     (if is_tptp_variable s andalso
       
   924         not (String.isPrefix tvar_prefix s) andalso
       
   925         not (member (op =) bounds name) then
       
   926        insert (op =) (name, NONE)
       
   927      else
       
   928        I)
       
   929     #> fold (add_term_vars bounds) tms
       
   930   | add_term_vars bounds (AAbs (((name, _), tm), args)) =
       
   931     add_term_vars (name :: bounds) tm #> fold (add_term_vars bounds) args
       
   932 fun close_formula_universally phi = close_universally add_term_vars phi
       
   933 
       
   934 fun add_iterm_vars bounds (IApp (tm1, tm2)) =
       
   935     fold (add_iterm_vars bounds) [tm1, tm2]
       
   936   | add_iterm_vars _ (IConst _) = I
       
   937   | add_iterm_vars bounds (IVar (name, T)) =
       
   938     not (member (op =) bounds name) ? insert (op =) (name, SOME T)
       
   939   | add_iterm_vars bounds (IAbs (_, tm)) = add_iterm_vars bounds tm
       
   940 
       
   941 fun close_iformula_universally phi = close_universally add_iterm_vars phi
       
   942 
       
   943 val fused_infinite_type_name = "ATP.fused_inf" (* shouldn't clash *)
   870 val fused_infinite_type_name = "ATP.fused_inf" (* shouldn't clash *)
   944 val fused_infinite_type = Type (fused_infinite_type_name, [])
   871 val fused_infinite_type = Type (fused_infinite_type_name, [])
   945 
       
   946 fun tvar_name (x as (s, _)) = (make_schematic_type_var x, s)
       
   947 
   872 
   948 fun ho_term_from_typ type_enc =
   873 fun ho_term_from_typ type_enc =
   949   let
   874   let
   950     fun term (Type (s, Ts)) =
   875     fun term (Type (s, Ts)) =
   951       ATerm ((case (is_type_enc_higher_order type_enc, s) of
   876       ATerm ((case (is_type_enc_higher_order type_enc, s) of
   955                         is_type_enc_native type_enc then
   880                         is_type_enc_native type_enc then
   956                        `I tptp_individual_type
   881                        `I tptp_individual_type
   957                      else
   882                      else
   958                        `make_fixed_type_const s,
   883                        `make_fixed_type_const s,
   959               []), map term Ts)
   884               []), map term Ts)
   960     | term (TFree (s, _)) = ATerm ((`make_fixed_type_var s, []), [])
   885     | term (TFree (s, _)) = ATerm ((`make_tfree s, []), [])
   961     | term (TVar (x, _)) = ATerm ((tvar_name x, []), [])
   886     | term (TVar (x, _)) = ATerm ((tvar_name x, []), [])
   962   in term end
   887   in term end
   963 
   888 
   964 fun ho_term_for_type_arg type_enc T =
   889 fun ho_term_for_type_arg type_enc T =
   965   if T = dummyT then NONE else SOME (ho_term_from_typ type_enc T)
   890   if T = dummyT then NONE else SOME (ho_term_from_typ type_enc T)
  1008   in if is_type_enc_higher_order type_enc then to_ho else to_fo ary end
   933   in if is_type_enc_higher_order type_enc then to_ho else to_fo ary end
  1009 
   934 
  1010 fun ho_type_from_typ type_enc pred_sym ary =
   935 fun ho_type_from_typ type_enc pred_sym ary =
  1011   ho_type_from_ho_term type_enc pred_sym ary
   936   ho_type_from_ho_term type_enc pred_sym ary
  1012   o ho_term_from_typ type_enc
   937   o ho_term_from_typ type_enc
       
   938 
       
   939 (* Make atoms for sorted type variables. *)
       
   940 fun generic_add_sorts_on_type _ [] = I
       
   941   | generic_add_sorts_on_type T (s :: ss) =
       
   942     generic_add_sorts_on_type T ss
       
   943     #> (if s = the_single @{sort HOL.type} then I else insert (op =) (s, T))
       
   944 fun add_sorts_on_tfree (T as TFree (_, S)) = generic_add_sorts_on_type T S
       
   945   | add_sorts_on_tfree _ = I
       
   946 fun add_sorts_on_tvar (T as TVar (_, S)) = generic_add_sorts_on_type T S
       
   947   | add_sorts_on_tvar _ = I
       
   948 
       
   949 fun process_type_args type_enc T_args =
       
   950   if is_type_enc_native type_enc then
       
   951     (map (ho_type_from_typ type_enc false 0) T_args, [])
       
   952   else
       
   953     ([], map_filter (ho_term_for_type_arg type_enc) T_args)
       
   954 
       
   955 fun type_class_atom type_enc (class, T) =
       
   956   let
       
   957     val class = `make_type_class class
       
   958     val (ty_args, tm_args) = process_type_args type_enc [T]
       
   959     val tm_args =
       
   960       tm_args @
       
   961       (case type_enc of
       
   962          Native (First_Order, Raw_Polymorphic Without_Phantom_Type_Vars, _) =>
       
   963          [ATerm ((TYPE_name, ty_args), [])]
       
   964        | _ => [])
       
   965   in AAtom (ATerm ((class, ty_args), tm_args)) end
       
   966 fun formulas_for_types type_enc add_sorts_on_typ Ts =
       
   967   [] |> level_of_type_enc type_enc <> No_Types ? fold add_sorts_on_typ Ts
       
   968      |> map (type_class_atom type_enc)
       
   969 
       
   970 fun mk_aconns c phis =
       
   971   let val (phis', phi') = split_last phis in
       
   972     fold_rev (mk_aconn c) phis' phi'
       
   973   end
       
   974 
       
   975 fun mk_ahorn [] phi = phi
       
   976   | mk_ahorn phis psi = AConn (AImplies, [mk_aconns AAnd phis, psi])
       
   977 
       
   978 fun mk_aquant _ [] phi = phi
       
   979   | mk_aquant q xs (phi as AQuant (q', xs', phi')) =
       
   980     if q = q' then AQuant (q, xs @ xs', phi') else AQuant (q, xs, phi)
       
   981   | mk_aquant q xs phi = AQuant (q, xs, phi)
       
   982 
       
   983 fun mk_atyquant _ [] phi = phi
       
   984   | mk_atyquant q xs (phi as ATyQuant (q', xs', phi')) =
       
   985     if q = q' then ATyQuant (q, xs @ xs', phi') else ATyQuant (q, xs, phi)
       
   986   | mk_atyquant q xs phi = ATyQuant (q, xs, phi)
       
   987 
       
   988 fun close_universally add_term_vars phi =
       
   989   let
       
   990     fun add_formula_vars bounds (ATyQuant (_, _, phi)) =
       
   991         add_formula_vars bounds phi
       
   992       | add_formula_vars bounds (AQuant (_, xs, phi)) =
       
   993         add_formula_vars (map fst xs @ bounds) phi
       
   994       | add_formula_vars bounds (AConn (_, phis)) =
       
   995         fold (add_formula_vars bounds) phis
       
   996       | add_formula_vars bounds (AAtom tm) = add_term_vars bounds tm
       
   997   in mk_aquant AForall (add_formula_vars [] phi []) phi end
       
   998 
       
   999 fun add_term_vars bounds (ATerm ((name as (s, _), _), tms)) =
       
  1000     (if is_tptp_variable s andalso
       
  1001         not (String.isPrefix tvar_prefix s) andalso
       
  1002         not (member (op =) bounds name) then
       
  1003        insert (op =) (name, NONE)
       
  1004      else
       
  1005        I)
       
  1006     #> fold (add_term_vars bounds) tms
       
  1007   | add_term_vars bounds (AAbs (((name, _), tm), args)) =
       
  1008     add_term_vars (name :: bounds) tm #> fold (add_term_vars bounds) args
       
  1009 fun close_formula_universally phi = close_universally add_term_vars phi
       
  1010 
       
  1011 fun add_iterm_vars bounds (IApp (tm1, tm2)) =
       
  1012     fold (add_iterm_vars bounds) [tm1, tm2]
       
  1013   | add_iterm_vars _ (IConst _) = I
       
  1014   | add_iterm_vars bounds (IVar (name, T)) =
       
  1015     not (member (op =) bounds name) ? insert (op =) (name, SOME T)
       
  1016   | add_iterm_vars bounds (IAbs (_, tm)) = add_iterm_vars bounds tm
       
  1017 
       
  1018 fun close_iformula_universally phi = close_universally add_iterm_vars phi
  1013 
  1019 
  1014 fun aliased_uncurried ary (s, s') =
  1020 fun aliased_uncurried ary (s, s') =
  1015   (s ^ ascii_of_uncurried_alias_sep ^ string_of_int ary, s' ^ string_of_int ary)
  1021   (s ^ ascii_of_uncurried_alias_sep ^ string_of_int ary, s' ^ string_of_int ary)
  1016 fun unaliased_uncurried (s, s') =
  1022 fun unaliased_uncurried (s, s') =
  1017   case space_explode uncurried_alias_sep s of
  1023   case space_explode uncurried_alias_sep s of
  1716     @{thms fAll_table fComp_law fAll_law fEx_law} |> map (pair Def)),
  1722     @{thms fAll_table fComp_law fAll_law fEx_law} |> map (pair Def)),
  1717    (("fEx", false),
  1723    (("fEx", false),
  1718     @{thms fEx_table fComp_law fAll_law fEx_law} |> map (pair Def))]
  1724     @{thms fEx_table fComp_law fAll_law fEx_law} |> map (pair Def))]
  1719   |> map (apsnd (map (apsnd zero_var_indexes)))
  1725   |> map (apsnd (map (apsnd zero_var_indexes)))
  1720 
  1726 
  1721 fun atype_of_type_vars (Native (_, Raw_Polymorphic _, _)) = SOME atype_of_types
       
  1722   | atype_of_type_vars (Native (_, Type_Class_Polymorphic, _)) =
       
  1723     SOME atype_of_types (* ### FIXME *)
       
  1724   | atype_of_type_vars _ = NONE
       
  1725 
       
  1726 fun bound_tvars type_enc sorts Ts =
  1727 fun bound_tvars type_enc sorts Ts =
  1727   (sorts ? mk_ahorn (formulas_for_types type_enc add_sorts_on_tvar Ts))
  1728   case filter is_TVar Ts of
  1728   #> mk_aquant AForall
  1729     [] => I
  1729         (map_filter (fn TVar (x as (s, _), _) =>
  1730   | Ts =>
  1730                         SOME ((make_schematic_type_var x, s),
  1731     (sorts ? mk_ahorn (formulas_for_types type_enc add_sorts_on_tvar Ts))
  1731                               atype_of_type_vars type_enc)
  1732     #> (if is_type_enc_native type_enc then
  1732                       | _ => NONE) Ts)
  1733           mk_atyquant AForall
       
  1734                       (map (fn TVar (x, _) => AType (tvar_name x, [])) Ts)
       
  1735         else
       
  1736           mk_aquant AForall (map (fn TVar (x, _) => (tvar_name x, NONE)) Ts))
  1733 
  1737 
  1734 fun eq_formula type_enc atomic_Ts bounds pred_sym tm1 tm2 =
  1738 fun eq_formula type_enc atomic_Ts bounds pred_sym tm1 tm2 =
  1735   (if pred_sym then AConn (AIff, [AAtom tm1, AAtom tm2])
  1739   (if pred_sym then AConn (AIff, [AAtom tm1, AAtom tm2])
  1736    else AAtom (ATerm ((`I tptp_equal, []), [tm1, tm2])))
  1740    else AAtom (ATerm ((`I tptp_equal, []), [tm1, tm2])))
  1737   |> mk_aquant AForall bounds
  1741   |> mk_aquant AForall bounds
  1992     granularity_of_type_level level <> All_Vars andalso
  1996     granularity_of_type_level level <> All_Vars andalso
  1993     should_encode_type ctxt mono level T
  1997     should_encode_type ctxt mono level T
  1994   | should_generate_tag_bound_decl _ _ _ _ _ = false
  1998   | should_generate_tag_bound_decl _ _ _ _ _ = false
  1995 
  1999 
  1996 fun mk_aterm type_enc name T_args args =
  2000 fun mk_aterm type_enc name T_args args =
  1997 (* ### FIXME
  2001   let val (ty_args, tm_args) = process_type_args type_enc T_args in
  1998   if is_type_enc_polymorphic type_enc then
  2002     ATerm ((name, ty_args), tm_args @ args)
  1999     ATerm ((name, map (ho_type_from_typ type_enc false 0) T_args), args)
  2003   end
  2000   else *)
       
  2001     ATerm ((name, []), map_filter (ho_term_for_type_arg type_enc) T_args @ args)
       
  2002 
  2004 
  2003 fun do_bound_type ctxt mono type_enc =
  2005 fun do_bound_type ctxt mono type_enc =
  2004   case type_enc of
  2006   case type_enc of
  2005     Native (_, _, level) =>
  2007     Native (_, _, level) =>
  2006     fused_type ctxt mono level 0 #> ho_type_from_typ type_enc false 0 #> SOME
  2008     fused_type ctxt mono level 0 #> ho_type_from_typ type_enc false 0 #> SOME
  2030             in map (term arg_site) args |> mk_aterm type_enc name T_args end
  2032             in map (term arg_site) args |> mk_aterm type_enc name T_args end
  2031           | IVar (name, _) =>
  2033           | IVar (name, _) =>
  2032             map (term Elsewhere) args |> mk_aterm type_enc name []
  2034             map (term Elsewhere) args |> mk_aterm type_enc name []
  2033           | IAbs ((name, T), tm) =>
  2035           | IAbs ((name, T), tm) =>
  2034             if is_type_enc_higher_order type_enc then
  2036             if is_type_enc_higher_order type_enc then
  2035               AAbs (((name, ho_type_from_typ type_enc true 0 T),
  2037               AAbs (((name, ho_type_from_typ type_enc true (* FIXME? why "true"? *) 0 T),
  2036                      term Elsewhere tm), map (term Elsewhere) args)
  2038                      term Elsewhere tm), map (term Elsewhere) args)
  2037             else
  2039             else
  2038               raise Fail "unexpected lambda-abstraction"
  2040               raise Fail "unexpected lambda-abstraction"
  2039           | IApp _ => raise Fail "impossible \"IApp\""
  2041           | IApp _ => raise Fail "impossible \"IApp\""
  2040         val T = ityp_of u
  2042         val T = ityp_of u
  2061           val var = ATerm ((name, []), [])
  2063           val var = ATerm ((name, []), [])
  2062           val tagged_var = tag_with_type ctxt mono type_enc pos T var
  2064           val tagged_var = tag_with_type ctxt mono type_enc pos T var
  2063         in SOME (AAtom (ATerm ((`I tptp_equal, []), [tagged_var, var]))) end
  2065         in SOME (AAtom (ATerm ((`I tptp_equal, []), [tagged_var, var]))) end
  2064       else
  2066       else
  2065         NONE
  2067         NONE
  2066     fun do_formula pos (AQuant (q, xs, phi)) =
  2068     fun do_formula pos (ATyQuant (q, xs, phi)) =
       
  2069         ATyQuant (q, map (ho_type_from_typ type_enc false 0) xs,
       
  2070                   do_formula pos phi)
       
  2071       | do_formula pos (AQuant (q, xs, phi)) =
  2067         let
  2072         let
  2068           val phi = phi |> do_formula pos
  2073           val phi = phi |> do_formula pos
  2069           val universal = Option.map (q = AExists ? not) pos
  2074           val universal = Option.map (q = AExists ? not) pos
  2070           val do_bound_type = do_bound_type ctxt mono type_enc
  2075           val do_bound_type = do_bound_type ctxt mono type_enc
  2071         in
  2076         in
  2106    end)
  2111    end)
  2107   |> Formula
  2112   |> Formula
  2108 
  2113 
  2109 fun formula_line_for_class_rel_clause type_enc
  2114 fun formula_line_for_class_rel_clause type_enc
  2110         ({name, subclass, superclass, ...} : class_rel_clause) =
  2115         ({name, subclass, superclass, ...} : class_rel_clause) =
  2111   let val ty_arg = ATerm ((tvar_a_name, []), []) in
  2116   Formula (class_rel_clause_prefix ^ ascii_of name, Axiom,
  2112     Formula (class_rel_clause_prefix ^ ascii_of name, Axiom,
  2117            AConn (AImplies,
  2113              AConn (AImplies,
  2118                   [type_class_atom type_enc (subclass, tvar_a),
  2114                     [type_class_formula type_enc subclass ty_arg,
  2119                    type_class_atom type_enc (superclass, tvar_a)])
  2115                      type_class_formula type_enc superclass ty_arg])
  2120            |> bound_tvars type_enc false [tvar_a],
  2116              |> mk_aquant AForall
  2121            NONE, isabelle_info inductiveN helper_rank)
  2117                           [(tvar_a_name, atype_of_type_vars type_enc)],
  2122 
       
  2123 fun formula_line_for_arity_clause type_enc
       
  2124         ({name, prems, concl} : arity_clause) =
       
  2125   let
       
  2126     val atomic_Ts = fold (fold_atyps (insert (op =)) o snd) (concl :: prems) []
       
  2127   in
       
  2128     Formula (arity_clause_prefix ^ name, Axiom,
       
  2129              mk_ahorn (map (type_class_atom type_enc) prems)
       
  2130                       (type_class_atom type_enc concl)
       
  2131              |> bound_tvars type_enc true atomic_Ts,
  2118              NONE, isabelle_info inductiveN helper_rank)
  2132              NONE, isabelle_info inductiveN helper_rank)
  2119   end
  2133   end
  2120 
       
  2121 fun formula_from_arity_atom type_enc (class, t, args) =
       
  2122   ATerm ((t, []), map (fn arg => ATerm ((arg, []), [])) args)
       
  2123   |> type_class_formula type_enc class
       
  2124 
       
  2125 fun formula_line_for_arity_clause type_enc
       
  2126         ({name, prem_atoms, concl_atom} : arity_clause) =
       
  2127   Formula (arity_clause_prefix ^ name, Axiom,
       
  2128            mk_ahorn (map (formula_from_arity_atom type_enc) prem_atoms)
       
  2129                     (formula_from_arity_atom type_enc concl_atom)
       
  2130            |> mk_aquant AForall
       
  2131                   (map (rpair (atype_of_type_vars type_enc)) (#3 concl_atom)),
       
  2132            NONE, isabelle_info inductiveN helper_rank)
       
  2133 
  2134 
  2134 fun formula_line_for_conjecture ctxt mono type_enc
  2135 fun formula_line_for_conjecture ctxt mono type_enc
  2135         ({name, role, iformula, atomic_types, ...} : ifact) =
  2136         ({name, role, iformula, atomic_types, ...} : ifact) =
  2136   Formula (conjecture_prefix ^ name, role,
  2137   Formula (conjecture_prefix ^ name, role,
  2137            iformula
  2138            iformula
  2138            |> formula_from_iformula ctxt mono type_enc
  2139            |> formula_from_iformula ctxt mono type_enc
  2139                   should_guard_var_in_formula (SOME false)
  2140                   should_guard_var_in_formula (SOME false)
  2140            |> close_formula_universally
  2141            |> close_formula_universally
  2141            |> bound_tvars type_enc true atomic_types, NONE, [])
  2142            |> bound_tvars type_enc true atomic_types, NONE, [])
  2142 
  2143 
  2143 fun type_enc_needs_free_types (Native (_, Raw_Polymorphic _, _)) = true
       
  2144   | type_enc_needs_free_types (Native _) = false
       
  2145   | type_enc_needs_free_types _ = true
       
  2146 
       
  2147 fun formula_line_for_free_type j phi =
       
  2148   Formula (tfree_clause_prefix ^ string_of_int j, Hypothesis, phi, NONE, [])
       
  2149 fun formula_lines_for_free_types type_enc (facts : ifact list) =
  2144 fun formula_lines_for_free_types type_enc (facts : ifact list) =
  2150   if type_enc_needs_free_types type_enc then
  2145   let
  2151     let
  2146     fun line j phi =
  2152       val phis =
  2147       Formula (tfree_clause_prefix ^ string_of_int j, Hypothesis, phi, NONE, [])
  2153         fold (union (op =)) (map #atomic_types facts) []
  2148     val phis =
  2154         |> formulas_for_types type_enc add_sorts_on_tfree
  2149       fold (union (op =)) (map #atomic_types facts) []
  2155     in map2 formula_line_for_free_type (0 upto length phis - 1) phis end
  2150       |> formulas_for_types type_enc add_sorts_on_tfree
  2156   else
  2151   in map2 line (0 upto length phis - 1) phis end
  2157     []
       
  2158 
  2152 
  2159 (** Symbol declarations **)
  2153 (** Symbol declarations **)
  2160 
  2154 
  2161 fun decl_line_for_class order phantoms s =
  2155 fun decl_line_for_class order phantoms s =
  2162   let val name as (s, _) = `make_type_class s in
  2156   let val name as (s, _) = `make_type_class s in
  2163     Decl (sym_decl_prefix ^ s, name,
  2157     Decl (sym_decl_prefix ^ s, name,
  2164           if order = First_Order then
  2158           if order = First_Order then
  2165             ATyAbs ([tvar_a_name],
  2159             APi ([tvar_a_name],
  2166                     if phantoms = Without_Phantom_Type_Vars then
  2160                  if phantoms = Without_Phantom_Type_Vars then
  2167                       AFun (a_itself_atype, bool_atype)
  2161                    AFun (a_itself_atype, bool_atype)
  2168                     else
  2162                  else
  2169                       bool_atype)
  2163                    bool_atype)
  2170           else
  2164           else
  2171             AFun (atype_of_types, bool_atype))
  2165             AFun (atype_of_types, bool_atype))
  2172   end
  2166   end
  2173 
  2167 
  2174 fun decl_lines_for_classes type_enc classes =
  2168 fun decl_lines_for_classes type_enc classes =
  2205          | IAbs (_, tm) => add_iterm_syms tm
  2199          | IAbs (_, tm) => add_iterm_syms tm
  2206          | _ => I)
  2200          | _ => I)
  2207         #> fold add_iterm_syms args
  2201         #> fold add_iterm_syms args
  2208       end
  2202       end
  2209     val add_fact_syms = K add_iterm_syms |> formula_fold NONE |> ifact_lift
  2203     val add_fact_syms = K add_iterm_syms |> formula_fold NONE |> ifact_lift
  2210     fun add_formula_var_types (AQuant (_, xs, phi)) =
  2204     fun add_formula_var_types (ATyQuant (_, _, phi)) = add_formula_var_types phi
       
  2205       | add_formula_var_types (AQuant (_, xs, phi)) =
  2211         fold (fn (_, SOME T) => insert_type thy I T | _ => I) xs
  2206         fold (fn (_, SOME T) => insert_type thy I T | _ => I) xs
  2212         #> add_formula_var_types phi
  2207         #> add_formula_var_types phi
  2213       | add_formula_var_types (AConn (_, phis)) =
  2208       | add_formula_var_types (AConn (_, phis)) =
  2214         fold add_formula_var_types phis
  2209         fold add_formula_var_types phis
  2215       | add_formula_var_types _ = I
  2210       | add_formula_var_types _ = I
  2357   in
  2352   in
  2358     Decl (sym_decl_prefix ^ s, (s, s'),
  2353     Decl (sym_decl_prefix ^ s, (s, s'),
  2359           T |> fused_type ctxt mono (level_of_type_enc type_enc) ary
  2354           T |> fused_type ctxt mono (level_of_type_enc type_enc) ary
  2360             |> ho_type_from_typ type_enc pred_sym ary
  2355             |> ho_type_from_typ type_enc pred_sym ary
  2361             |> not (null T_args)
  2356             |> not (null T_args)
  2362                ? curry ATyAbs (map (tvar_name o fst o dest_TVar) T_args))
  2357                ? curry APi (map (tvar_name o fst o dest_TVar) T_args))
  2363   end
  2358   end
  2364 
  2359 
  2365 fun honor_conj_sym_role in_conj =
  2360 fun honor_conj_sym_role in_conj =
  2366   if in_conj then (Hypothesis, I) else (Axiom, I)
  2361   if in_conj then (Hypothesis, I) else (Axiom, I)
  2367 
  2362 
  2572            String.isPrefix tfree_prefix s then
  2567            String.isPrefix tfree_prefix s then
  2573           atype_of_types
  2568           atype_of_types
  2574         else
  2569         else
  2575           individual_atype
  2570           individual_atype
  2576       | _ => individual_atype
  2571       | _ => individual_atype
  2577     fun typ 0 = if pred_sym then bool_atype else ind
  2572     fun typ 0 0 = if pred_sym then bool_atype else ind
  2578       | typ ary = AFun (ind, typ (ary - 1))
  2573       | typ 0 tm_ary = AFun (ind, typ 0 (tm_ary - 1))
       
  2574       | typ ty_ary tm_ary = APi (replicate ty_ary tvar_a_name, typ 0 tm_ary)
  2579   in typ end
  2575   in typ end
  2580 
  2576 
  2581 fun nary_type_constr_type n =
  2577 fun nary_type_constr_type n =
  2582   funpow n (curry AFun atype_of_types) atype_of_types
  2578   funpow n (curry AFun atype_of_types) atype_of_types
  2583 
  2579 
  2590         I
  2586         I
  2591     fun do_type (AType (name, tys)) =
  2587     fun do_type (AType (name, tys)) =
  2592         do_sym name (fn () => nary_type_constr_type (length tys))
  2588         do_sym name (fn () => nary_type_constr_type (length tys))
  2593         #> fold do_type tys
  2589         #> fold do_type tys
  2594       | do_type (AFun (ty1, ty2)) = do_type ty1 #> do_type ty2
  2590       | do_type (AFun (ty1, ty2)) = do_type ty1 #> do_type ty2
  2595       | do_type (ATyAbs (_, ty)) = do_type ty
  2591       | do_type (APi (_, ty)) = do_type ty
  2596     fun do_term pred_sym (ATerm ((name as (s, _), tys), tms)) =
  2592     fun do_term pred_sym (ATerm ((name as (s, _), tys), tms)) =
  2597         do_sym name (fn _ => default_type type_enc pred_sym s (length tms))
  2593         do_sym name
       
  2594             (fn _ => default_type type_enc pred_sym s (length tys) (length tms))
  2598         #> fold do_type tys #> fold (do_term false) tms
  2595         #> fold do_type tys #> fold (do_term false) tms
  2599       | do_term _ (AAbs (((_, ty), tm), args)) =
  2596       | do_term _ (AAbs (((_, ty), tm), args)) =
  2600         do_type ty #> do_term false tm #> fold (do_term false) args
  2597         do_type ty #> do_term false tm #> fold (do_term false) args
  2601     fun do_formula (AQuant (_, xs, phi)) =
  2598     fun do_formula (ATyQuant (_, _, phi)) = do_formula phi
       
  2599       | do_formula (AQuant (_, xs, phi)) =
  2602         fold do_type (map_filter snd xs) #> do_formula phi
  2600         fold do_type (map_filter snd xs) #> do_formula phi
  2603       | do_formula (AConn (_, phis)) = fold do_formula phis
  2601       | do_formula (AConn (_, phis)) = fold do_formula phis
  2604       | do_formula (AAtom tm) = do_term true tm
  2602       | do_formula (AAtom tm) = do_term true tm
  2605     fun do_problem_line (Decl (_, _, ty)) = do_type ty
  2603     fun do_problem_line (Decl (_, _, ty)) = do_type ty
  2606       | do_problem_line (Formula (_, _, phi, _, _)) = do_formula phi
  2604       | do_problem_line (Formula (_, _, phi, _, _)) = do_formula phi
  2792 fun make_head_roll (ATerm ((s, _), tms)) =
  2790 fun make_head_roll (ATerm ((s, _), tms)) =
  2793     if may_be_app s tms then make_head_roll (hd tms) ||> append (tl tms)
  2791     if may_be_app s tms then make_head_roll (hd tms) ||> append (tl tms)
  2794     else (s, tms)
  2792     else (s, tms)
  2795   | make_head_roll _ = ("", [])
  2793   | make_head_roll _ = ("", [])
  2796 
  2794 
  2797 fun strip_up_to_predicator (AQuant (_, _, phi)) = strip_up_to_predicator phi
  2795 fun strip_up_to_predicator (ATyQuant (_, _, phi)) = strip_up_to_predicator phi
       
  2796   | strip_up_to_predicator (AQuant (_, _, phi)) = strip_up_to_predicator phi
  2798   | strip_up_to_predicator (AConn (_, phis)) = maps strip_up_to_predicator phis
  2797   | strip_up_to_predicator (AConn (_, phis)) = maps strip_up_to_predicator phis
  2799   | strip_up_to_predicator (AAtom tm) = [strip_predicator tm]
  2798   | strip_up_to_predicator (AAtom tm) = [strip_predicator tm]
  2800 
  2799 
  2801 fun strip_ahorn_etc (AQuant (_, _, phi)) = strip_ahorn_etc phi
  2800 fun strip_ahorn_etc (ATyQuant (_, _, phi)) = strip_ahorn_etc phi
       
  2801   | strip_ahorn_etc (AQuant (_, _, phi)) = strip_ahorn_etc phi
  2802   | strip_ahorn_etc (AConn (AImplies, [phi1, phi2])) =
  2802   | strip_ahorn_etc (AConn (AImplies, [phi1, phi2])) =
  2803     strip_ahorn_etc phi2 |>> append (strip_up_to_predicator phi1)
  2803     strip_ahorn_etc phi2 |>> append (strip_up_to_predicator phi1)
  2804   | strip_ahorn_etc phi = ([], hd (strip_up_to_predicator phi))
  2804   | strip_ahorn_etc phi = ([], hd (strip_up_to_predicator phi))
  2805 
  2805 
  2806 fun strip_iff_etc (AQuant (_, _, phi)) = strip_iff_etc phi
  2806 fun strip_iff_etc (ATyQuant (_, _, phi)) = strip_iff_etc phi
       
  2807   | strip_iff_etc (AQuant (_, _, phi)) = strip_iff_etc phi
  2807   | strip_iff_etc (AConn (AIff, [phi1, phi2])) =
  2808   | strip_iff_etc (AConn (AIff, [phi1, phi2])) =
  2808     pairself strip_up_to_predicator (phi1, phi2)
  2809     pairself strip_up_to_predicator (phi1, phi2)
  2809   | strip_iff_etc _ = ([], [])
  2810   | strip_iff_etc _ = ([], [])
  2810 
  2811 
  2811 val max_term_order_weight = 2147483647
  2812 val max_term_order_weight = 2147483647