src/HOL/Tools/ATP/atp_translate.ML
changeset 44493 a867ebb12209
parent 44491 de026aecab9b
child 44495 996b2022ff78
equal deleted inserted replaced
44492:c3e28c869499 44493:a867ebb12209
    24   datatype type_level =
    24   datatype type_level =
    25     All_Types | Noninf_Nonmono_Types | Fin_Nonmono_Types | Const_Arg_Types |
    25     All_Types | Noninf_Nonmono_Types | Fin_Nonmono_Types | Const_Arg_Types |
    26     No_Types
    26     No_Types
    27   datatype type_heaviness = Heavyweight | Lightweight
    27   datatype type_heaviness = Heavyweight | Lightweight
    28 
    28 
    29   datatype type_sys =
    29   datatype type_enc =
    30     Simple_Types of order * type_level |
    30     Simple_Types of order * type_level |
    31     Preds of polymorphism * type_level * type_heaviness |
    31     Preds of polymorphism * type_level * type_heaviness |
    32     Tags of polymorphism * type_level * type_heaviness
    32     Tags of polymorphism * type_level * type_heaviness
    33 
    33 
    34   val bound_var_prefix : string
    34   val bound_var_prefix : string
    73   val new_skolem_var_name_from_const : string -> string
    73   val new_skolem_var_name_from_const : string -> string
    74   val num_type_args : theory -> string -> int
    74   val num_type_args : theory -> string -> int
    75   val atp_irrelevant_consts : string list
    75   val atp_irrelevant_consts : string list
    76   val atp_schematic_consts_of : term -> typ list Symtab.table
    76   val atp_schematic_consts_of : term -> typ list Symtab.table
    77   val is_locality_global : locality -> bool
    77   val is_locality_global : locality -> bool
    78   val type_sys_from_string : string -> type_sys
    78   val type_enc_from_string : string -> type_enc
    79   val polymorphism_of_type_sys : type_sys -> polymorphism
    79   val polymorphism_of_type_enc : type_enc -> polymorphism
    80   val level_of_type_sys : type_sys -> type_level
    80   val level_of_type_enc : type_enc -> type_level
    81   val is_type_sys_virtually_sound : type_sys -> bool
    81   val is_type_enc_virtually_sound : type_enc -> bool
    82   val is_type_sys_fairly_sound : type_sys -> bool
    82   val is_type_enc_fairly_sound : type_enc -> bool
    83   val choose_format : format list -> type_sys -> format * type_sys
    83   val choose_format : format list -> type_enc -> format * type_enc
    84   val mk_aconns :
    84   val mk_aconns :
    85     connective -> ('a, 'b, 'c) formula list -> ('a, 'b, 'c) formula
    85     connective -> ('a, 'b, 'c) formula list -> ('a, 'b, 'c) formula
    86   val unmangled_const : string -> string * string fo_term list
    86   val unmangled_const : string -> string * string fo_term list
    87   val unmangled_const_name : string -> string
    87   val unmangled_const_name : string -> string
    88   val helper_table : ((string * bool) * thm list) list
    88   val helper_table : ((string * bool) * thm list) list
    89   val factsN : string
    89   val factsN : string
    90   val prepare_atp_problem :
    90   val prepare_atp_problem :
    91     Proof.context -> format -> formula_kind -> formula_kind -> type_sys -> bool
    91     Proof.context -> format -> formula_kind -> formula_kind -> type_enc -> bool
    92     -> bool -> bool -> bool -> term list -> term
    92     -> bool -> bool -> bool -> term list -> term
    93     -> ((string * locality) * term) list
    93     -> ((string * locality) * term) list
    94     -> string problem * string Symtab.table * int * int
    94     -> string problem * string Symtab.table * int * int
    95        * (string * locality) list vector * int list * int Symtab.table
    95        * (string * locality) list vector * int list * int Symtab.table
    96   val atp_problem_weights : string problem -> (string * real) list
    96   val atp_problem_weights : string problem -> (string * real) list
   507 datatype type_level =
   507 datatype type_level =
   508   All_Types | Noninf_Nonmono_Types | Fin_Nonmono_Types | Const_Arg_Types |
   508   All_Types | Noninf_Nonmono_Types | Fin_Nonmono_Types | Const_Arg_Types |
   509   No_Types
   509   No_Types
   510 datatype type_heaviness = Heavyweight | Lightweight
   510 datatype type_heaviness = Heavyweight | Lightweight
   511 
   511 
   512 datatype type_sys =
   512 datatype type_enc =
   513   Simple_Types of order * type_level |
   513   Simple_Types of order * type_level |
   514   Preds of polymorphism * type_level * type_heaviness |
   514   Preds of polymorphism * type_level * type_heaviness |
   515   Tags of polymorphism * type_level * type_heaviness
   515   Tags of polymorphism * type_level * type_heaviness
   516 
   516 
   517 fun try_unsuffixes ss s =
   517 fun try_unsuffixes ss s =
   518   fold (fn s' => fn NONE => try (unsuffix s') s | some => some) ss NONE
   518   fold (fn s' => fn NONE => try (unsuffix s') s | some => some) ss NONE
   519 
   519 
   520 fun type_sys_from_string s =
   520 fun type_enc_from_string s =
   521   (case try (unprefix "poly_") s of
   521   (case try (unprefix "poly_") s of
   522      SOME s => (SOME Polymorphic, s)
   522      SOME s => (SOME Polymorphic, s)
   523    | NONE =>
   523    | NONE =>
   524      case try (unprefix "mono_") s of
   524      case try (unprefix "mono_") s of
   525        SOME s => (SOME Monomorphic, s)
   525        SOME s => (SOME Monomorphic, s)
   555          | ("erased", (NONE, All_Types (* naja *), Lightweight)) =>
   555          | ("erased", (NONE, All_Types (* naja *), Lightweight)) =>
   556            Preds (Polymorphic, No_Types, Lightweight)
   556            Preds (Polymorphic, No_Types, Lightweight)
   557          | _ => raise Same.SAME)
   557          | _ => raise Same.SAME)
   558   handle Same.SAME => error ("Unknown type system: " ^ quote s ^ ".")
   558   handle Same.SAME => error ("Unknown type system: " ^ quote s ^ ".")
   559 
   559 
   560 fun is_type_sys_higher_order (Simple_Types (Higher_Order, _)) = true
   560 fun is_type_enc_higher_order (Simple_Types (Higher_Order, _)) = true
   561   | is_type_sys_higher_order _ = false
   561   | is_type_enc_higher_order _ = false
   562 
   562 
   563 fun polymorphism_of_type_sys (Simple_Types _) = Mangled_Monomorphic
   563 fun polymorphism_of_type_enc (Simple_Types _) = Mangled_Monomorphic
   564   | polymorphism_of_type_sys (Preds (poly, _, _)) = poly
   564   | polymorphism_of_type_enc (Preds (poly, _, _)) = poly
   565   | polymorphism_of_type_sys (Tags (poly, _, _)) = poly
   565   | polymorphism_of_type_enc (Tags (poly, _, _)) = poly
   566 
   566 
   567 fun level_of_type_sys (Simple_Types (_, level)) = level
   567 fun level_of_type_enc (Simple_Types (_, level)) = level
   568   | level_of_type_sys (Preds (_, level, _)) = level
   568   | level_of_type_enc (Preds (_, level, _)) = level
   569   | level_of_type_sys (Tags (_, level, _)) = level
   569   | level_of_type_enc (Tags (_, level, _)) = level
   570 
   570 
   571 fun heaviness_of_type_sys (Simple_Types _) = Heavyweight
   571 fun heaviness_of_type_enc (Simple_Types _) = Heavyweight
   572   | heaviness_of_type_sys (Preds (_, _, heaviness)) = heaviness
   572   | heaviness_of_type_enc (Preds (_, _, heaviness)) = heaviness
   573   | heaviness_of_type_sys (Tags (_, _, heaviness)) = heaviness
   573   | heaviness_of_type_enc (Tags (_, _, heaviness)) = heaviness
   574 
   574 
   575 fun is_type_level_virtually_sound level =
   575 fun is_type_level_virtually_sound level =
   576   level = All_Types orelse level = Noninf_Nonmono_Types
   576   level = All_Types orelse level = Noninf_Nonmono_Types
   577 val is_type_sys_virtually_sound =
   577 val is_type_enc_virtually_sound =
   578   is_type_level_virtually_sound o level_of_type_sys
   578   is_type_level_virtually_sound o level_of_type_enc
   579 
   579 
   580 fun is_type_level_fairly_sound level =
   580 fun is_type_level_fairly_sound level =
   581   is_type_level_virtually_sound level orelse level = Fin_Nonmono_Types
   581   is_type_level_virtually_sound level orelse level = Fin_Nonmono_Types
   582 val is_type_sys_fairly_sound = is_type_level_fairly_sound o level_of_type_sys
   582 val is_type_enc_fairly_sound = is_type_level_fairly_sound o level_of_type_enc
   583 
   583 
   584 fun choose_format formats (Simple_Types (order, level)) =
   584 fun choose_format formats (Simple_Types (order, level)) =
   585     if member (op =) formats THF then
   585     if member (op =) formats THF then
   586       (THF, Simple_Types (order, level))
   586       (THF, Simple_Types (order, level))
   587     else if member (op =) formats TFF then
   587     else if member (op =) formats TFF then
   588       (TFF, Simple_Types (First_Order, level))
   588       (TFF, Simple_Types (First_Order, level))
   589     else
   589     else
   590       choose_format formats (Preds (Mangled_Monomorphic, level, Heavyweight))
   590       choose_format formats (Preds (Mangled_Monomorphic, level, Heavyweight))
   591   | choose_format formats type_sys =
   591   | choose_format formats type_enc =
   592     (case hd formats of
   592     (case hd formats of
   593        CNF_UEQ =>
   593        CNF_UEQ =>
   594        (CNF_UEQ, case type_sys of
   594        (CNF_UEQ, case type_enc of
   595                    Preds stuff =>
   595                    Preds stuff =>
   596                    (if is_type_sys_fairly_sound type_sys then Tags else Preds)
   596                    (if is_type_enc_fairly_sound type_enc then Tags else Preds)
   597                        stuff
   597                        stuff
   598                  | _ => type_sys)
   598                  | _ => type_enc)
   599      | format => (format, type_sys))
   599      | format => (format, type_enc))
   600 
   600 
   601 type translated_formula =
   601 type translated_formula =
   602   {name : string,
   602   {name : string,
   603    locality : locality,
   603    locality : locality,
   604    kind : formula_kind,
   604    kind : formula_kind,
   626   Mangled_Type_Args of bool |
   626   Mangled_Type_Args of bool |
   627   No_Type_Args
   627   No_Type_Args
   628 
   628 
   629 fun should_drop_arg_type_args (Simple_Types _) =
   629 fun should_drop_arg_type_args (Simple_Types _) =
   630     false (* since TFF doesn't support overloading *)
   630     false (* since TFF doesn't support overloading *)
   631   | should_drop_arg_type_args type_sys =
   631   | should_drop_arg_type_args type_enc =
   632     level_of_type_sys type_sys = All_Types andalso
   632     level_of_type_enc type_enc = All_Types andalso
   633     heaviness_of_type_sys type_sys = Heavyweight
   633     heaviness_of_type_enc type_enc = Heavyweight
   634 
   634 
   635 fun general_type_arg_policy (Tags (_, All_Types, Heavyweight)) = No_Type_Args
   635 fun general_type_arg_policy (Tags (_, All_Types, Heavyweight)) = No_Type_Args
   636   | general_type_arg_policy type_sys =
   636   | general_type_arg_policy type_enc =
   637     if level_of_type_sys type_sys = No_Types then
   637     if level_of_type_enc type_enc = No_Types then
   638       No_Type_Args
   638       No_Type_Args
   639     else if polymorphism_of_type_sys type_sys = Mangled_Monomorphic then
   639     else if polymorphism_of_type_enc type_enc = Mangled_Monomorphic then
   640       Mangled_Type_Args (should_drop_arg_type_args type_sys)
   640       Mangled_Type_Args (should_drop_arg_type_args type_enc)
   641     else
   641     else
   642       Explicit_Type_Args (should_drop_arg_type_args type_sys)
   642       Explicit_Type_Args (should_drop_arg_type_args type_enc)
   643 
   643 
   644 fun type_arg_policy type_sys s =
   644 fun type_arg_policy type_enc s =
   645   if s = @{const_name HOL.eq} orelse
   645   if s = @{const_name HOL.eq} orelse
   646      (s = app_op_name andalso level_of_type_sys type_sys = Const_Arg_Types) then
   646      (s = app_op_name andalso level_of_type_enc type_enc = Const_Arg_Types) then
   647     No_Type_Args
   647     No_Type_Args
   648   else if s = type_tag_name then
   648   else if s = type_tag_name then
   649     (if polymorphism_of_type_sys type_sys = Mangled_Monomorphic then
   649     (if polymorphism_of_type_enc type_enc = Mangled_Monomorphic then
   650        Mangled_Type_Args
   650        Mangled_Type_Args
   651      else
   651      else
   652        Explicit_Type_Args) false
   652        Explicit_Type_Args) false
   653   else
   653   else
   654     general_type_arg_policy type_sys
   654     general_type_arg_policy type_enc
   655 
   655 
   656 (*Make literals for sorted type variables*)
   656 (*Make literals for sorted type variables*)
   657 fun generic_add_sorts_on_type (_, []) = I
   657 fun generic_add_sorts_on_type (_, []) = I
   658   | generic_add_sorts_on_type ((x, i), s :: ss) =
   658   | generic_add_sorts_on_type ((x, i), s :: ss) =
   659     generic_add_sorts_on_type ((x, i), ss)
   659     generic_add_sorts_on_type ((x, i), ss)
   667 fun add_sorts_on_tfree (TFree (s, S)) = generic_add_sorts_on_type ((s, ~1), S)
   667 fun add_sorts_on_tfree (TFree (s, S)) = generic_add_sorts_on_type ((s, ~1), S)
   668   | add_sorts_on_tfree _ = I
   668   | add_sorts_on_tfree _ = I
   669 fun add_sorts_on_tvar (TVar z) = generic_add_sorts_on_type z
   669 fun add_sorts_on_tvar (TVar z) = generic_add_sorts_on_type z
   670   | add_sorts_on_tvar _ = I
   670   | add_sorts_on_tvar _ = I
   671 
   671 
   672 fun type_literals_for_types type_sys add_sorts_on_typ Ts =
   672 fun type_literals_for_types type_enc add_sorts_on_typ Ts =
   673   [] |> level_of_type_sys type_sys <> No_Types ? fold add_sorts_on_typ Ts
   673   [] |> level_of_type_enc type_enc <> No_Types ? fold add_sorts_on_typ Ts
   674 
   674 
   675 fun mk_aconns c phis =
   675 fun mk_aconns c phis =
   676   let val (phis', phi') = split_last phis in
   676   let val (phis', phi') = split_last phis in
   677     fold_rev (mk_aconn c) phis' phi'
   677     fold_rev (mk_aconn c) phis' phi'
   678   end
   678   end
   703 fun close_formula_universally phi = close_universally term_vars phi
   703 fun close_formula_universally phi = close_universally term_vars phi
   704 
   704 
   705 val homo_infinite_type_name = @{type_name ind} (* any infinite type *)
   705 val homo_infinite_type_name = @{type_name ind} (* any infinite type *)
   706 val homo_infinite_type = Type (homo_infinite_type_name, [])
   706 val homo_infinite_type = Type (homo_infinite_type_name, [])
   707 
   707 
   708 fun fo_term_from_typ format type_sys =
   708 fun fo_term_from_typ format type_enc =
   709   let
   709   let
   710     fun term (Type (s, Ts)) =
   710     fun term (Type (s, Ts)) =
   711       ATerm (case (is_type_sys_higher_order type_sys, s) of
   711       ATerm (case (is_type_enc_higher_order type_enc, s) of
   712                (true, @{type_name bool}) => `I tptp_bool_type
   712                (true, @{type_name bool}) => `I tptp_bool_type
   713              | (true, @{type_name fun}) => `I tptp_fun_type
   713              | (true, @{type_name fun}) => `I tptp_fun_type
   714              | _ => if s = homo_infinite_type_name andalso
   714              | _ => if s = homo_infinite_type_name andalso
   715                        (format = TFF orelse format = THF) then
   715                        (format = TFF orelse format = THF) then
   716                       `I tptp_individual_type
   716                       `I tptp_individual_type
   720     | term (TFree (s, _)) = ATerm (`make_fixed_type_var s, [])
   720     | term (TFree (s, _)) = ATerm (`make_fixed_type_var s, [])
   721     | term (TVar ((x as (s, _)), _)) =
   721     | term (TVar ((x as (s, _)), _)) =
   722       ATerm ((make_schematic_type_var x, s), [])
   722       ATerm ((make_schematic_type_var x, s), [])
   723   in term end
   723   in term end
   724 
   724 
   725 fun fo_term_for_type_arg format type_sys T =
   725 fun fo_term_for_type_arg format type_enc T =
   726   if T = dummyT then NONE else SOME (fo_term_from_typ format type_sys T)
   726   if T = dummyT then NONE else SOME (fo_term_from_typ format type_enc T)
   727 
   727 
   728 (* This shouldn't clash with anything else. *)
   728 (* This shouldn't clash with anything else. *)
   729 val mangled_type_sep = "\000"
   729 val mangled_type_sep = "\000"
   730 
   730 
   731 fun generic_mangled_type_name f (ATerm (name, [])) = f name
   731 fun generic_mangled_type_name f (ATerm (name, [])) = f name
   740      s = tptp_individual_type then
   740      s = tptp_individual_type then
   741     s
   741     s
   742   else
   742   else
   743     simple_type_prefix ^ ascii_of s
   743     simple_type_prefix ^ ascii_of s
   744 
   744 
   745 fun ho_type_from_fo_term type_sys pred_sym ary =
   745 fun ho_type_from_fo_term type_enc pred_sym ary =
   746   let
   746   let
   747     fun to_atype ty =
   747     fun to_atype ty =
   748       AType ((make_simple_type (generic_mangled_type_name fst ty),
   748       AType ((make_simple_type (generic_mangled_type_name fst ty),
   749               generic_mangled_type_name snd ty))
   749               generic_mangled_type_name snd ty))
   750     fun to_afun f1 f2 tys = AFun (f1 (hd tys), f2 (nth tys 1))
   750     fun to_afun f1 f2 tys = AFun (f1 (hd tys), f2 (nth tys 1))
   751     fun to_fo 0 ty = if pred_sym then bool_atype else to_atype ty
   751     fun to_fo 0 ty = if pred_sym then bool_atype else to_atype ty
   752       | to_fo ary (ATerm (_, tys)) = to_afun to_atype (to_fo (ary - 1)) tys
   752       | to_fo ary (ATerm (_, tys)) = to_afun to_atype (to_fo (ary - 1)) tys
   753     fun to_ho (ty as ATerm ((s, _), tys)) =
   753     fun to_ho (ty as ATerm ((s, _), tys)) =
   754       if s = tptp_fun_type then to_afun to_ho to_ho tys else to_atype ty
   754       if s = tptp_fun_type then to_afun to_ho to_ho tys else to_atype ty
   755   in if is_type_sys_higher_order type_sys then to_ho else to_fo ary end
   755   in if is_type_enc_higher_order type_enc then to_ho else to_fo ary end
   756 
   756 
   757 fun mangled_type format type_sys pred_sym ary =
   757 fun mangled_type format type_enc pred_sym ary =
   758   ho_type_from_fo_term type_sys pred_sym ary
   758   ho_type_from_fo_term type_enc pred_sym ary
   759   o fo_term_from_typ format type_sys
   759   o fo_term_from_typ format type_enc
   760 
   760 
   761 fun mangled_const_name format type_sys T_args (s, s') =
   761 fun mangled_const_name format type_enc T_args (s, s') =
   762   let
   762   let
   763     val ty_args = T_args |> map_filter (fo_term_for_type_arg format type_sys)
   763     val ty_args = T_args |> map_filter (fo_term_for_type_arg format type_enc)
   764     fun type_suffix f g =
   764     fun type_suffix f g =
   765       fold_rev (curry (op ^) o g o prefix mangled_type_sep
   765       fold_rev (curry (op ^) o g o prefix mangled_type_sep
   766                 o generic_mangled_type_name f) ty_args ""
   766                 o generic_mangled_type_name f) ty_args ""
   767   in (s ^ type_suffix fst ascii_of, s' ^ type_suffix snd I) end
   767   in (s ^ type_suffix fst ascii_of, s' ^ type_suffix snd I) end
   768 
   768 
   787 fun unmangled_const s =
   787 fun unmangled_const s =
   788   let val ss = space_explode mangled_type_sep s in
   788   let val ss = space_explode mangled_type_sep s in
   789     (hd ss, map unmangled_type (tl ss))
   789     (hd ss, map unmangled_type (tl ss))
   790   end
   790   end
   791 
   791 
   792 fun introduce_proxies type_sys =
   792 fun introduce_proxies type_enc =
   793   let
   793   let
   794     fun intro top_level (CombApp (tm1, tm2)) =
   794     fun intro top_level (CombApp (tm1, tm2)) =
   795         CombApp (intro top_level tm1, intro false tm2)
   795         CombApp (intro top_level tm1, intro false tm2)
   796       | intro top_level (CombConst (name as (s, _), T, T_args)) =
   796       | intro top_level (CombConst (name as (s, _), T, T_args)) =
   797         (case proxify_const s of
   797         (case proxify_const s of
   798            SOME proxy_base =>
   798            SOME proxy_base =>
   799            if top_level orelse is_type_sys_higher_order type_sys then
   799            if top_level orelse is_type_enc_higher_order type_enc then
   800              case (top_level, s) of
   800              case (top_level, s) of
   801                (_, "c_False") => (`I tptp_false, [])
   801                (_, "c_False") => (`I tptp_false, [])
   802              | (_, "c_True") => (`I tptp_true, [])
   802              | (_, "c_True") => (`I tptp_true, [])
   803              | (false, "c_Not") => (`I tptp_not, [])
   803              | (false, "c_Not") => (`I tptp_not, [])
   804              | (false, "c_conj") => (`I tptp_and, [])
   804              | (false, "c_conj") => (`I tptp_and, [])
   813           | NONE => (name, T_args))
   813           | NONE => (name, T_args))
   814         |> (fn (name, T_args) => CombConst (name, T, T_args))
   814         |> (fn (name, T_args) => CombConst (name, T, T_args))
   815       | intro _ tm = tm
   815       | intro _ tm = tm
   816   in intro true end
   816   in intro true end
   817 
   817 
   818 fun combformula_from_prop thy type_sys eq_as_iff =
   818 fun combformula_from_prop thy type_enc eq_as_iff =
   819   let
   819   let
   820     fun do_term bs t atomic_types =
   820     fun do_term bs t atomic_types =
   821       combterm_from_term thy bs (Envir.eta_contract t)
   821       combterm_from_term thy bs (Envir.eta_contract t)
   822       |>> (introduce_proxies type_sys #> AAtom)
   822       |>> (introduce_proxies type_enc #> AAtom)
   823       ||> union (op =) atomic_types
   823       ||> union (op =) atomic_types
   824     fun do_quant bs q s T t' =
   824     fun do_quant bs q s T t' =
   825       let val s = singleton (Name.variant_list (map fst bs)) s in
   825       let val s = singleton (Name.variant_list (map fst bs)) s in
   826         do_formula ((s, T) :: bs) t'
   826         do_formula ((s, T) :: bs) t'
   827         #>> mk_aquant q [(`make_bound_var s, SOME T)]
   827         #>> mk_aquant q [(`make_bound_var s, SOME T)]
   938       |> perhaps (try (HOLogic.dest_Trueprop))
   938       |> perhaps (try (HOLogic.dest_Trueprop))
   939       |> introduce_combinators_in_term ctxt kind
   939       |> introduce_combinators_in_term ctxt kind
   940   end
   940   end
   941 
   941 
   942 (* making fact and conjecture formulas *)
   942 (* making fact and conjecture formulas *)
   943 fun make_formula thy type_sys eq_as_iff name loc kind t =
   943 fun make_formula thy type_enc eq_as_iff name loc kind t =
   944   let
   944   let
   945     val (combformula, atomic_types) =
   945     val (combformula, atomic_types) =
   946       combformula_from_prop thy type_sys eq_as_iff t []
   946       combformula_from_prop thy type_enc eq_as_iff t []
   947   in
   947   in
   948     {name = name, locality = loc, kind = kind, combformula = combformula,
   948     {name = name, locality = loc, kind = kind, combformula = combformula,
   949      atomic_types = atomic_types}
   949      atomic_types = atomic_types}
   950   end
   950   end
   951 
   951 
   952 fun make_fact ctxt format type_sys eq_as_iff preproc presimp_consts
   952 fun make_fact ctxt format type_enc eq_as_iff preproc presimp_consts
   953               ((name, loc), t) =
   953               ((name, loc), t) =
   954   let val thy = Proof_Context.theory_of ctxt in
   954   let val thy = Proof_Context.theory_of ctxt in
   955     case t |> preproc ? preprocess_prop ctxt presimp_consts Axiom
   955     case t |> preproc ? preprocess_prop ctxt presimp_consts Axiom
   956            |> make_formula thy type_sys (eq_as_iff andalso format <> CNF) name
   956            |> make_formula thy type_enc (eq_as_iff andalso format <> CNF) name
   957                            loc Axiom of
   957                            loc Axiom of
   958       formula as {combformula = AAtom (CombConst ((s, _), _, _)), ...} =>
   958       formula as {combformula = AAtom (CombConst ((s, _), _, _)), ...} =>
   959       if s = tptp_true then NONE else SOME formula
   959       if s = tptp_true then NONE else SOME formula
   960     | formula => SOME formula
   960     | formula => SOME formula
   961   end
   961   end
   962 
   962 
   963 fun make_conjecture ctxt format prem_kind type_sys preproc presimp_consts ts =
   963 fun make_conjecture ctxt format prem_kind type_enc preproc presimp_consts ts =
   964   let
   964   let
   965     val thy = Proof_Context.theory_of ctxt
   965     val thy = Proof_Context.theory_of ctxt
   966     val last = length ts - 1
   966     val last = length ts - 1
   967   in
   967   in
   968     map2 (fn j => fn t =>
   968     map2 (fn j => fn t =>
   975                     if prem_kind = Conjecture then update_combformula mk_anot
   975                     if prem_kind = Conjecture then update_combformula mk_anot
   976                     else I)
   976                     else I)
   977               in
   977               in
   978                 t |> preproc ?
   978                 t |> preproc ?
   979                      (preprocess_prop ctxt presimp_consts kind #> freeze_term)
   979                      (preprocess_prop ctxt presimp_consts kind #> freeze_term)
   980                   |> make_formula thy type_sys (format <> CNF) (string_of_int j)
   980                   |> make_formula thy type_enc (format <> CNF) (string_of_int j)
   981                                   Local kind
   981                                   Local kind
   982                   |> maybe_negate
   982                   |> maybe_negate
   983               end)
   983               end)
   984          (0 upto last) ts
   984          (0 upto last) ts
   985   end
   985   end
  1229                      else dummyT)
  1229                      else dummyT)
  1230         end
  1230         end
  1231     end
  1231     end
  1232     handle TYPE _ => T_args
  1232     handle TYPE _ => T_args
  1233 
  1233 
  1234 fun enforce_type_arg_policy_in_combterm ctxt format type_sys =
  1234 fun enforce_type_arg_policy_in_combterm ctxt format type_enc =
  1235   let
  1235   let
  1236     val thy = Proof_Context.theory_of ctxt
  1236     val thy = Proof_Context.theory_of ctxt
  1237     fun aux arity (CombApp (tm1, tm2)) =
  1237     fun aux arity (CombApp (tm1, tm2)) =
  1238         CombApp (aux (arity + 1) tm1, aux 0 tm2)
  1238         CombApp (aux (arity + 1) tm1, aux 0 tm2)
  1239       | aux arity (CombConst (name as (s, _), T, T_args)) =
  1239       | aux arity (CombConst (name as (s, _), T, T_args)) =
  1243            let
  1243            let
  1244              val s'' = invert_const s''
  1244              val s'' = invert_const s''
  1245              fun filtered_T_args false = T_args
  1245              fun filtered_T_args false = T_args
  1246                | filtered_T_args true = filter_type_args thy s'' arity T_args
  1246                | filtered_T_args true = filter_type_args thy s'' arity T_args
  1247            in
  1247            in
  1248              case type_arg_policy type_sys s'' of
  1248              case type_arg_policy type_enc s'' of
  1249                Explicit_Type_Args drop_args =>
  1249                Explicit_Type_Args drop_args =>
  1250                (name, filtered_T_args drop_args)
  1250                (name, filtered_T_args drop_args)
  1251              | Mangled_Type_Args drop_args =>
  1251              | Mangled_Type_Args drop_args =>
  1252                (mangled_const_name format type_sys (filtered_T_args drop_args)
  1252                (mangled_const_name format type_enc (filtered_T_args drop_args)
  1253                                    name, [])
  1253                                    name, [])
  1254              | No_Type_Args => (name, [])
  1254              | No_Type_Args => (name, [])
  1255            end)
  1255            end)
  1256         |> (fn (name, T_args) => CombConst (name, T, T_args))
  1256         |> (fn (name, T_args) => CombConst (name, T, T_args))
  1257       | aux _ tm = tm
  1257       | aux _ tm = tm
  1258   in aux 0 end
  1258   in aux 0 end
  1259 
  1259 
  1260 fun repair_combterm ctxt format type_sys sym_tab =
  1260 fun repair_combterm ctxt format type_enc sym_tab =
  1261   not (is_type_sys_higher_order type_sys)
  1261   not (is_type_enc_higher_order type_enc)
  1262   ? (introduce_explicit_apps_in_combterm sym_tab
  1262   ? (introduce_explicit_apps_in_combterm sym_tab
  1263      #> introduce_predicators_in_combterm sym_tab)
  1263      #> introduce_predicators_in_combterm sym_tab)
  1264   #> enforce_type_arg_policy_in_combterm ctxt format type_sys
  1264   #> enforce_type_arg_policy_in_combterm ctxt format type_enc
  1265 fun repair_fact ctxt format type_sys sym_tab =
  1265 fun repair_fact ctxt format type_enc sym_tab =
  1266   update_combformula (formula_map
  1266   update_combformula (formula_map
  1267       (repair_combterm ctxt format type_sys sym_tab))
  1267       (repair_combterm ctxt format type_enc sym_tab))
  1268 
  1268 
  1269 (** Helper facts **)
  1269 (** Helper facts **)
  1270 
  1270 
  1271 (* The Boolean indicates that a fairly sound type encoding is needed. *)
  1271 (* The Boolean indicates that a fairly sound type encoding is needed. *)
  1272 val helper_table =
  1272 val helper_table =
  1311     Formula (type_tag_idempotence_helper_name, Axiom,
  1311     Formula (type_tag_idempotence_helper_name, Axiom,
  1312              AAtom (ATerm (`I tptp_equal, [tag tagged_a, tagged_a]))
  1312              AAtom (ATerm (`I tptp_equal, [tag tagged_a, tagged_a]))
  1313              |> close_formula_universally, simp_info, NONE)
  1313              |> close_formula_universally, simp_info, NONE)
  1314   end
  1314   end
  1315 
  1315 
  1316 fun should_specialize_helper type_sys t =
  1316 fun should_specialize_helper type_enc t =
  1317   case general_type_arg_policy type_sys of
  1317   case general_type_arg_policy type_enc of
  1318     Mangled_Type_Args _ => not (null (Term.hidden_polymorphism t))
  1318     Mangled_Type_Args _ => not (null (Term.hidden_polymorphism t))
  1319   | _ => false
  1319   | _ => false
  1320 
  1320 
  1321 fun helper_facts_for_sym ctxt format type_sys (s, {types, ...} : sym_info) =
  1321 fun helper_facts_for_sym ctxt format type_enc (s, {types, ...} : sym_info) =
  1322   case strip_prefix_and_unascii const_prefix s of
  1322   case strip_prefix_and_unascii const_prefix s of
  1323     SOME mangled_s =>
  1323     SOME mangled_s =>
  1324     let
  1324     let
  1325       val thy = Proof_Context.theory_of ctxt
  1325       val thy = Proof_Context.theory_of ctxt
  1326       val unmangled_s = mangled_s |> unmangled_const_name
  1326       val unmangled_s = mangled_s |> unmangled_const_name
  1329           (if mangled_s = unmangled_s then "" else "_" ^ ascii_of mangled_s) ^
  1329           (if mangled_s = unmangled_s then "" else "_" ^ ascii_of mangled_s) ^
  1330           (if needs_fairly_sound then typed_helper_suffix
  1330           (if needs_fairly_sound then typed_helper_suffix
  1331            else untyped_helper_suffix),
  1331            else untyped_helper_suffix),
  1332           Helper),
  1332           Helper),
  1333          let val t = th |> prop_of in
  1333          let val t = th |> prop_of in
  1334            t |> should_specialize_helper type_sys t
  1334            t |> should_specialize_helper type_enc t
  1335                 ? (case types of
  1335                 ? (case types of
  1336                      [T] => specialize_type thy (invert_const unmangled_s, T)
  1336                      [T] => specialize_type thy (invert_const unmangled_s, T)
  1337                    | _ => I)
  1337                    | _ => I)
  1338          end)
  1338          end)
  1339       val make_facts =
  1339       val make_facts =
  1340         map_filter (make_fact ctxt format type_sys false false [])
  1340         map_filter (make_fact ctxt format type_enc false false [])
  1341       val fairly_sound = is_type_sys_fairly_sound type_sys
  1341       val fairly_sound = is_type_enc_fairly_sound type_enc
  1342     in
  1342     in
  1343       helper_table
  1343       helper_table
  1344       |> maps (fn ((helper_s, needs_fairly_sound), ths) =>
  1344       |> maps (fn ((helper_s, needs_fairly_sound), ths) =>
  1345                   if helper_s <> unmangled_s orelse
  1345                   if helper_s <> unmangled_s orelse
  1346                      (needs_fairly_sound andalso not fairly_sound) then
  1346                      (needs_fairly_sound andalso not fairly_sound) then
  1349                     ths ~~ (1 upto length ths)
  1349                     ths ~~ (1 upto length ths)
  1350                     |> map (dub_and_inst needs_fairly_sound)
  1350                     |> map (dub_and_inst needs_fairly_sound)
  1351                     |> make_facts)
  1351                     |> make_facts)
  1352     end
  1352     end
  1353   | NONE => []
  1353   | NONE => []
  1354 fun helper_facts_for_sym_table ctxt format type_sys sym_tab =
  1354 fun helper_facts_for_sym_table ctxt format type_enc sym_tab =
  1355   Symtab.fold_rev (append o helper_facts_for_sym ctxt format type_sys) sym_tab
  1355   Symtab.fold_rev (append o helper_facts_for_sym ctxt format type_enc) sym_tab
  1356                   []
  1356                   []
  1357 
  1357 
  1358 (***************************************************************)
  1358 (***************************************************************)
  1359 (* Type Classes Present in the Axiom or Conjecture Clauses     *)
  1359 (* Type Classes Present in the Axiom or Conjecture Clauses     *)
  1360 (***************************************************************)
  1360 (***************************************************************)
  1391   in add end
  1391   in add end
  1392 
  1392 
  1393 fun type_constrs_of_terms thy ts =
  1393 fun type_constrs_of_terms thy ts =
  1394   Symtab.keys (fold (add_type_constrs_in_term thy) ts Symtab.empty)
  1394   Symtab.keys (fold (add_type_constrs_in_term thy) ts Symtab.empty)
  1395 
  1395 
  1396 fun translate_formulas ctxt format prem_kind type_sys preproc hyp_ts concl_t
  1396 fun translate_formulas ctxt format prem_kind type_enc preproc hyp_ts concl_t
  1397                        facts =
  1397                        facts =
  1398   let
  1398   let
  1399     val thy = Proof_Context.theory_of ctxt
  1399     val thy = Proof_Context.theory_of ctxt
  1400     val fact_ts = facts |> map snd
  1400     val fact_ts = facts |> map snd
  1401     val presimp_consts = Meson.presimplified_consts ctxt
  1401     val presimp_consts = Meson.presimplified_consts ctxt
  1402     val make_fact = make_fact ctxt format type_sys true preproc presimp_consts
  1402     val make_fact = make_fact ctxt format type_enc true preproc presimp_consts
  1403     val (facts, fact_names) =
  1403     val (facts, fact_names) =
  1404       facts |> map (fn (name, t) => (name, t) |> make_fact |> rpair name)
  1404       facts |> map (fn (name, t) => (name, t) |> make_fact |> rpair name)
  1405             |> map_filter (try (apfst the))
  1405             |> map_filter (try (apfst the))
  1406             |> ListPair.unzip
  1406             |> ListPair.unzip
  1407     (* Remove existing facts from the conjecture, as this can dramatically
  1407     (* Remove existing facts from the conjecture, as this can dramatically
  1414     val subs = tfree_classes_of_terms all_ts
  1414     val subs = tfree_classes_of_terms all_ts
  1415     val supers = tvar_classes_of_terms all_ts
  1415     val supers = tvar_classes_of_terms all_ts
  1416     val tycons = type_constrs_of_terms thy all_ts
  1416     val tycons = type_constrs_of_terms thy all_ts
  1417     val conjs =
  1417     val conjs =
  1418       hyp_ts @ [concl_t]
  1418       hyp_ts @ [concl_t]
  1419       |> make_conjecture ctxt format prem_kind type_sys preproc presimp_consts
  1419       |> make_conjecture ctxt format prem_kind type_enc preproc presimp_consts
  1420     val (supers', arity_clauses) =
  1420     val (supers', arity_clauses) =
  1421       if level_of_type_sys type_sys = No_Types then ([], [])
  1421       if level_of_type_enc type_enc = No_Types then ([], [])
  1422       else make_arity_clauses thy tycons supers
  1422       else make_arity_clauses thy tycons supers
  1423     val class_rel_clauses = make_class_rel_clauses thy subs supers'
  1423     val class_rel_clauses = make_class_rel_clauses thy subs supers'
  1424   in
  1424   in
  1425     (fact_names |> map single, (conjs, facts, class_rel_clauses, arity_clauses))
  1425     (fact_names |> map single, (conjs, facts, class_rel_clauses, arity_clauses))
  1426   end
  1426   end
  1432 
  1432 
  1433 fun formula_from_fo_literal (pos, t) = AAtom t |> not pos ? mk_anot
  1433 fun formula_from_fo_literal (pos, t) = AAtom t |> not pos ? mk_anot
  1434 
  1434 
  1435 val type_pred = `make_fixed_const type_pred_name
  1435 val type_pred = `make_fixed_const type_pred_name
  1436 
  1436 
  1437 fun type_pred_combterm ctxt format type_sys T tm =
  1437 fun type_pred_combterm ctxt format type_enc T tm =
  1438   CombApp (CombConst (type_pred, T --> @{typ bool}, [T])
  1438   CombApp (CombConst (type_pred, T --> @{typ bool}, [T])
  1439            |> enforce_type_arg_policy_in_combterm ctxt format type_sys, tm)
  1439            |> enforce_type_arg_policy_in_combterm ctxt format type_enc, tm)
  1440 
  1440 
  1441 fun is_var_positively_naked_in_term _ (SOME false) _ accum = accum
  1441 fun is_var_positively_naked_in_term _ (SOME false) _ accum = accum
  1442   | is_var_positively_naked_in_term name _ (ATerm ((s, _), tms)) accum =
  1442   | is_var_positively_naked_in_term name _ (ATerm ((s, _), tms)) accum =
  1443     accum orelse (is_tptp_equal s andalso member (op =) tms (ATerm (name, [])))
  1443     accum orelse (is_tptp_equal s andalso member (op =) tms (ATerm (name, [])))
  1444 fun should_predicate_on_var_in_formula pos phi (SOME true) name =
  1444 fun should_predicate_on_var_in_formula pos phi (SOME true) name =
  1445     formula_fold pos (is_var_positively_naked_in_term name) phi false
  1445     formula_fold pos (is_var_positively_naked_in_term name) phi false
  1446   | should_predicate_on_var_in_formula _ _ _ _ = true
  1446   | should_predicate_on_var_in_formula _ _ _ _ = true
  1447 
  1447 
  1448 fun mk_const_aterm format type_sys x T_args args =
  1448 fun mk_const_aterm format type_enc x T_args args =
  1449   ATerm (x, map_filter (fo_term_for_type_arg format type_sys) T_args @ args)
  1449   ATerm (x, map_filter (fo_term_for_type_arg format type_enc) T_args @ args)
  1450 
  1450 
  1451 fun tag_with_type ctxt format nonmono_Ts type_sys pos T tm =
  1451 fun tag_with_type ctxt format nonmono_Ts type_enc pos T tm =
  1452   CombConst (type_tag, T --> T, [T])
  1452   CombConst (type_tag, T --> T, [T])
  1453   |> enforce_type_arg_policy_in_combterm ctxt format type_sys
  1453   |> enforce_type_arg_policy_in_combterm ctxt format type_enc
  1454   |> term_from_combterm ctxt format nonmono_Ts type_sys (Top_Level pos)
  1454   |> term_from_combterm ctxt format nonmono_Ts type_enc (Top_Level pos)
  1455   |> (fn ATerm (s, tms) => ATerm (s, tms @ [tm]))
  1455   |> (fn ATerm (s, tms) => ATerm (s, tms @ [tm]))
  1456 and term_from_combterm ctxt format nonmono_Ts type_sys =
  1456 and term_from_combterm ctxt format nonmono_Ts type_enc =
  1457   let
  1457   let
  1458     fun aux site u =
  1458     fun aux site u =
  1459       let
  1459       let
  1460         val (head, args) = strip_combterm_comb u
  1460         val (head, args) = strip_combterm_comb u
  1461         val (x as (s, _), T_args) =
  1461         val (x as (s, _), T_args) =
  1467           case site of
  1467           case site of
  1468             Top_Level pos =>
  1468             Top_Level pos =>
  1469             (pos, if is_tptp_equal s then Eq_Arg pos else Elsewhere)
  1469             (pos, if is_tptp_equal s then Eq_Arg pos else Elsewhere)
  1470           | Eq_Arg pos => (pos, Elsewhere)
  1470           | Eq_Arg pos => (pos, Elsewhere)
  1471           | Elsewhere => (NONE, Elsewhere)
  1471           | Elsewhere => (NONE, Elsewhere)
  1472         val t = mk_const_aterm format type_sys x T_args
  1472         val t = mk_const_aterm format type_enc x T_args
  1473                     (map (aux arg_site) args)
  1473                     (map (aux arg_site) args)
  1474         val T = combtyp_of u
  1474         val T = combtyp_of u
  1475       in
  1475       in
  1476         t |> (if should_tag_with_type ctxt nonmono_Ts type_sys site u T then
  1476         t |> (if should_tag_with_type ctxt nonmono_Ts type_enc site u T then
  1477                 tag_with_type ctxt format nonmono_Ts type_sys pos T
  1477                 tag_with_type ctxt format nonmono_Ts type_enc pos T
  1478               else
  1478               else
  1479                 I)
  1479                 I)
  1480       end
  1480       end
  1481   in aux end
  1481   in aux end
  1482 and formula_from_combformula ctxt format nonmono_Ts type_sys
  1482 and formula_from_combformula ctxt format nonmono_Ts type_enc
  1483                              should_predicate_on_var =
  1483                              should_predicate_on_var =
  1484   let
  1484   let
  1485     fun do_term pos =
  1485     fun do_term pos =
  1486       term_from_combterm ctxt format nonmono_Ts type_sys (Top_Level pos)
  1486       term_from_combterm ctxt format nonmono_Ts type_enc (Top_Level pos)
  1487     val do_bound_type =
  1487     val do_bound_type =
  1488       case type_sys of
  1488       case type_enc of
  1489         Simple_Types (_, level) =>
  1489         Simple_Types (_, level) =>
  1490         homogenized_type ctxt nonmono_Ts level 0
  1490         homogenized_type ctxt nonmono_Ts level 0
  1491         #> mangled_type format type_sys false 0 #> SOME
  1491         #> mangled_type format type_enc false 0 #> SOME
  1492       | _ => K NONE
  1492       | _ => K NONE
  1493     fun do_out_of_bound_type pos phi universal (name, T) =
  1493     fun do_out_of_bound_type pos phi universal (name, T) =
  1494       if should_predicate_on_type ctxt nonmono_Ts type_sys
  1494       if should_predicate_on_type ctxt nonmono_Ts type_enc
  1495              (fn () => should_predicate_on_var pos phi universal name) T then
  1495              (fn () => should_predicate_on_var pos phi universal name) T then
  1496         CombVar (name, T)
  1496         CombVar (name, T)
  1497         |> type_pred_combterm ctxt format type_sys T
  1497         |> type_pred_combterm ctxt format type_enc T
  1498         |> do_term pos |> AAtom |> SOME
  1498         |> do_term pos |> AAtom |> SOME
  1499       else
  1499       else
  1500         NONE
  1500         NONE
  1501     fun do_formula pos (AQuant (q, xs, phi)) =
  1501     fun do_formula pos (AQuant (q, xs, phi)) =
  1502         let
  1502         let
  1515         end
  1515         end
  1516       | do_formula pos (AConn conn) = aconn_map pos do_formula conn
  1516       | do_formula pos (AConn conn) = aconn_map pos do_formula conn
  1517       | do_formula pos (AAtom tm) = AAtom (do_term pos tm)
  1517       | do_formula pos (AAtom tm) = AAtom (do_term pos tm)
  1518   in do_formula end
  1518   in do_formula end
  1519 
  1519 
  1520 fun bound_tvars type_sys Ts =
  1520 fun bound_tvars type_enc Ts =
  1521   mk_ahorn (map (formula_from_fo_literal o fo_literal_from_type_literal)
  1521   mk_ahorn (map (formula_from_fo_literal o fo_literal_from_type_literal)
  1522                 (type_literals_for_types type_sys add_sorts_on_tvar Ts))
  1522                 (type_literals_for_types type_enc add_sorts_on_tvar Ts))
  1523 
  1523 
  1524 (* Each fact is given a unique fact number to avoid name clashes (e.g., because
  1524 (* Each fact is given a unique fact number to avoid name clashes (e.g., because
  1525    of monomorphization). The TPTP explicitly forbids name clashes, and some of
  1525    of monomorphization). The TPTP explicitly forbids name clashes, and some of
  1526    the remote provers might care. *)
  1526    the remote provers might care. *)
  1527 fun formula_line_for_fact ctxt format prefix encode freshen pos nonmono_Ts
  1527 fun formula_line_for_fact ctxt format prefix encode freshen pos nonmono_Ts
  1528         type_sys (j, {name, locality, kind, combformula, atomic_types}) =
  1528         type_enc (j, {name, locality, kind, combformula, atomic_types}) =
  1529   (prefix ^ (if freshen then string_of_int j ^ "_" else "") ^ encode name,
  1529   (prefix ^ (if freshen then string_of_int j ^ "_" else "") ^ encode name,
  1530    kind,
  1530    kind,
  1531    combformula
  1531    combformula
  1532    |> close_combformula_universally
  1532    |> close_combformula_universally
  1533    |> formula_from_combformula ctxt format nonmono_Ts type_sys
  1533    |> formula_from_combformula ctxt format nonmono_Ts type_enc
  1534                                should_predicate_on_var_in_formula
  1534                                should_predicate_on_var_in_formula
  1535                                (if pos then SOME true else NONE)
  1535                                (if pos then SOME true else NONE)
  1536    |> bound_tvars type_sys atomic_types
  1536    |> bound_tvars type_enc atomic_types
  1537    |> close_formula_universally,
  1537    |> close_formula_universally,
  1538    NONE,
  1538    NONE,
  1539    case locality of
  1539    case locality of
  1540      Intro => intro_info
  1540      Intro => intro_info
  1541    | Elim => elim_info
  1541    | Elim => elim_info
  1564                           o fo_literal_from_arity_literal) prem_lits)
  1564                           o fo_literal_from_arity_literal) prem_lits)
  1565                     (formula_from_fo_literal
  1565                     (formula_from_fo_literal
  1566                          (fo_literal_from_arity_literal concl_lits))
  1566                          (fo_literal_from_arity_literal concl_lits))
  1567            |> close_formula_universally, intro_info, NONE)
  1567            |> close_formula_universally, intro_info, NONE)
  1568 
  1568 
  1569 fun formula_line_for_conjecture ctxt format nonmono_Ts type_sys
  1569 fun formula_line_for_conjecture ctxt format nonmono_Ts type_enc
  1570         ({name, kind, combformula, atomic_types, ...} : translated_formula) =
  1570         ({name, kind, combformula, atomic_types, ...} : translated_formula) =
  1571   Formula (conjecture_prefix ^ name, kind,
  1571   Formula (conjecture_prefix ^ name, kind,
  1572            formula_from_combformula ctxt format nonmono_Ts type_sys
  1572            formula_from_combformula ctxt format nonmono_Ts type_enc
  1573                should_predicate_on_var_in_formula (SOME false)
  1573                should_predicate_on_var_in_formula (SOME false)
  1574                (close_combformula_universally combformula)
  1574                (close_combformula_universally combformula)
  1575            |> bound_tvars type_sys atomic_types
  1575            |> bound_tvars type_enc atomic_types
  1576            |> close_formula_universally, NONE, NONE)
  1576            |> close_formula_universally, NONE, NONE)
  1577 
  1577 
  1578 fun free_type_literals type_sys ({atomic_types, ...} : translated_formula) =
  1578 fun free_type_literals type_enc ({atomic_types, ...} : translated_formula) =
  1579   atomic_types |> type_literals_for_types type_sys add_sorts_on_tfree
  1579   atomic_types |> type_literals_for_types type_enc add_sorts_on_tfree
  1580                |> map fo_literal_from_type_literal
  1580                |> map fo_literal_from_type_literal
  1581 
  1581 
  1582 fun formula_line_for_free_type j lit =
  1582 fun formula_line_for_free_type j lit =
  1583   Formula (tfree_clause_prefix ^ string_of_int j, Hypothesis,
  1583   Formula (tfree_clause_prefix ^ string_of_int j, Hypothesis,
  1584            formula_from_fo_literal lit, NONE, NONE)
  1584            formula_from_fo_literal lit, NONE, NONE)
  1585 fun formula_lines_for_free_types type_sys facts =
  1585 fun formula_lines_for_free_types type_enc facts =
  1586   let
  1586   let
  1587     val litss = map (free_type_literals type_sys) facts
  1587     val litss = map (free_type_literals type_enc) facts
  1588     val lits = fold (union (op =)) litss []
  1588     val lits = fold (union (op =)) litss []
  1589   in map2 formula_line_for_free_type (0 upto length lits - 1) lits end
  1589   in map2 formula_line_for_free_type (0 upto length lits - 1) lits end
  1590 
  1590 
  1591 (** Symbol declarations **)
  1591 (** Symbol declarations **)
  1592 
  1592 
  1593 fun should_declare_sym type_sys pred_sym s =
  1593 fun should_declare_sym type_enc pred_sym s =
  1594   is_tptp_user_symbol s andalso not (String.isPrefix bound_var_prefix s) andalso
  1594   is_tptp_user_symbol s andalso not (String.isPrefix bound_var_prefix s) andalso
  1595   (case type_sys of
  1595   (case type_enc of
  1596      Simple_Types _ => true
  1596      Simple_Types _ => true
  1597    | Tags (_, _, Lightweight) => true
  1597    | Tags (_, _, Lightweight) => true
  1598    | _ => not pred_sym)
  1598    | _ => not pred_sym)
  1599 
  1599 
  1600 fun sym_decl_table_for_facts ctxt type_sys repaired_sym_tab (conjs, facts) =
  1600 fun sym_decl_table_for_facts ctxt type_enc repaired_sym_tab (conjs, facts) =
  1601   let
  1601   let
  1602     fun add_combterm in_conj tm =
  1602     fun add_combterm in_conj tm =
  1603       let val (head, args) = strip_combterm_comb tm in
  1603       let val (head, args) = strip_combterm_comb tm in
  1604         (case head of
  1604         (case head of
  1605            CombConst ((s, s'), T, T_args) =>
  1605            CombConst ((s, s'), T, T_args) =>
  1606            let val pred_sym = is_pred_sym repaired_sym_tab s in
  1606            let val pred_sym = is_pred_sym repaired_sym_tab s in
  1607              if should_declare_sym type_sys pred_sym s then
  1607              if should_declare_sym type_enc pred_sym s then
  1608                Symtab.map_default (s, [])
  1608                Symtab.map_default (s, [])
  1609                    (insert_type ctxt #3 (s', T_args, T, pred_sym, length args,
  1609                    (insert_type ctxt #3 (s', T_args, T, pred_sym, length args,
  1610                                          in_conj))
  1610                                          in_conj))
  1611              else
  1611              else
  1612                I
  1612                I
  1616       end
  1616       end
  1617     fun add_fact in_conj =
  1617     fun add_fact in_conj =
  1618       fact_lift (formula_fold NONE (K (add_combterm in_conj)))
  1618       fact_lift (formula_fold NONE (K (add_combterm in_conj)))
  1619   in
  1619   in
  1620     Symtab.empty
  1620     Symtab.empty
  1621     |> is_type_sys_fairly_sound type_sys
  1621     |> is_type_enc_fairly_sound type_enc
  1622        ? (fold (add_fact true) conjs #> fold (add_fact false) facts)
  1622        ? (fold (add_fact true) conjs #> fold (add_fact false) facts)
  1623   end
  1623   end
  1624 
  1624 
  1625 (* This inference is described in section 2.3 of Claessen et al.'s "Sorting it
  1625 (* This inference is described in section 2.3 of Claessen et al.'s "Sorting it
  1626    out with monotonicity" paper presented at CADE 2011. *)
  1626    out with monotonicity" paper presented at CADE 2011. *)
  1639 fun add_fact_nonmonotonic_types ctxt level sound
  1639 fun add_fact_nonmonotonic_types ctxt level sound
  1640         ({kind, locality, combformula, ...} : translated_formula) =
  1640         ({kind, locality, combformula, ...} : translated_formula) =
  1641   formula_fold (SOME (kind <> Conjecture))
  1641   formula_fold (SOME (kind <> Conjecture))
  1642                (add_combterm_nonmonotonic_types ctxt level sound locality)
  1642                (add_combterm_nonmonotonic_types ctxt level sound locality)
  1643                combformula
  1643                combformula
  1644 fun nonmonotonic_types_for_facts ctxt type_sys sound facts =
  1644 fun nonmonotonic_types_for_facts ctxt type_enc sound facts =
  1645   let val level = level_of_type_sys type_sys in
  1645   let val level = level_of_type_enc type_enc in
  1646     if level = Noninf_Nonmono_Types orelse level = Fin_Nonmono_Types then
  1646     if level = Noninf_Nonmono_Types orelse level = Fin_Nonmono_Types then
  1647       [] |> fold (add_fact_nonmonotonic_types ctxt level sound) facts
  1647       [] |> fold (add_fact_nonmonotonic_types ctxt level sound) facts
  1648          (* We must add "bool" in case the helper "True_or_False" is added
  1648          (* We must add "bool" in case the helper "True_or_False" is added
  1649             later. In addition, several places in the code rely on the list of
  1649             later. In addition, several places in the code rely on the list of
  1650             nonmonotonic types not being empty. *)
  1650             nonmonotonic types not being empty. *)
  1651          |> insert_type ctxt I @{typ bool}
  1651          |> insert_type ctxt I @{typ bool}
  1652     else
  1652     else
  1653       []
  1653       []
  1654   end
  1654   end
  1655 
  1655 
  1656 fun decl_line_for_sym ctxt format nonmono_Ts type_sys s
  1656 fun decl_line_for_sym ctxt format nonmono_Ts type_enc s
  1657                       (s', T_args, T, pred_sym, ary, _) =
  1657                       (s', T_args, T, pred_sym, ary, _) =
  1658   let
  1658   let
  1659     val (T_arg_Ts, level) =
  1659     val (T_arg_Ts, level) =
  1660       case type_sys of
  1660       case type_enc of
  1661         Simple_Types (_, level) => ([], level)
  1661         Simple_Types (_, level) => ([], level)
  1662       | _ => (replicate (length T_args) homo_infinite_type, No_Types)
  1662       | _ => (replicate (length T_args) homo_infinite_type, No_Types)
  1663   in
  1663   in
  1664     Decl (sym_decl_prefix ^ s, (s, s'),
  1664     Decl (sym_decl_prefix ^ s, (s, s'),
  1665           (T_arg_Ts ---> (T |> homogenized_type ctxt nonmono_Ts level ary))
  1665           (T_arg_Ts ---> (T |> homogenized_type ctxt nonmono_Ts level ary))
  1666           |> mangled_type format type_sys pred_sym (length T_arg_Ts + ary))
  1666           |> mangled_type format type_enc pred_sym (length T_arg_Ts + ary))
  1667   end
  1667   end
  1668 
  1668 
  1669 fun formula_line_for_preds_sym_decl ctxt format conj_sym_kind nonmono_Ts
  1669 fun formula_line_for_preds_sym_decl ctxt format conj_sym_kind nonmono_Ts
  1670         poly_nonmono_Ts type_sys n s j (s', T_args, T, _, ary, in_conj) =
  1670         poly_nonmono_Ts type_enc n s j (s', T_args, T, _, ary, in_conj) =
  1671   let
  1671   let
  1672     val (kind, maybe_negate) =
  1672     val (kind, maybe_negate) =
  1673       if in_conj then (conj_sym_kind, conj_sym_kind = Conjecture ? mk_anot)
  1673       if in_conj then (conj_sym_kind, conj_sym_kind = Conjecture ? mk_anot)
  1674       else (Axiom, I)
  1674       else (Axiom, I)
  1675     val (arg_Ts, res_T) = chop_fun ary T
  1675     val (arg_Ts, res_T) = chop_fun ary T
  1679     val bounds =
  1679     val bounds =
  1680       bound_names ~~ arg_Ts |> map (fn (name, T) => CombConst (name, T, []))
  1680       bound_names ~~ arg_Ts |> map (fn (name, T) => CombConst (name, T, []))
  1681     val sym_needs_arg_types = n > 1 orelse exists (curry (op =) dummyT) T_args
  1681     val sym_needs_arg_types = n > 1 orelse exists (curry (op =) dummyT) T_args
  1682     fun should_keep_arg_type T =
  1682     fun should_keep_arg_type T =
  1683       sym_needs_arg_types orelse
  1683       sym_needs_arg_types orelse
  1684       not (should_predicate_on_type ctxt nonmono_Ts type_sys (K false) T)
  1684       not (should_predicate_on_type ctxt nonmono_Ts type_enc (K false) T)
  1685     val bound_Ts =
  1685     val bound_Ts =
  1686       arg_Ts |> map (fn T => if should_keep_arg_type T then SOME T else NONE)
  1686       arg_Ts |> map (fn T => if should_keep_arg_type T then SOME T else NONE)
  1687   in
  1687   in
  1688     Formula (preds_sym_formula_prefix ^ s ^
  1688     Formula (preds_sym_formula_prefix ^ s ^
  1689              (if n > 1 then "_" ^ string_of_int j else ""), kind,
  1689              (if n > 1 then "_" ^ string_of_int j else ""), kind,
  1690              CombConst ((s, s'), T, T_args)
  1690              CombConst ((s, s'), T, T_args)
  1691              |> fold (curry (CombApp o swap)) bounds
  1691              |> fold (curry (CombApp o swap)) bounds
  1692              |> type_pred_combterm ctxt format type_sys res_T
  1692              |> type_pred_combterm ctxt format type_enc res_T
  1693              |> AAtom |> mk_aquant AForall (bound_names ~~ bound_Ts)
  1693              |> AAtom |> mk_aquant AForall (bound_names ~~ bound_Ts)
  1694              |> formula_from_combformula ctxt format poly_nonmono_Ts type_sys
  1694              |> formula_from_combformula ctxt format poly_nonmono_Ts type_enc
  1695                                          (K (K (K (K true)))) (SOME true)
  1695                                          (K (K (K (K true)))) (SOME true)
  1696              |> n > 1 ? bound_tvars type_sys (atyps_of T)
  1696              |> n > 1 ? bound_tvars type_enc (atyps_of T)
  1697              |> close_formula_universally
  1697              |> close_formula_universally
  1698              |> maybe_negate,
  1698              |> maybe_negate,
  1699              intro_info, NONE)
  1699              intro_info, NONE)
  1700   end
  1700   end
  1701 
  1701 
  1702 fun formula_lines_for_lightweight_tags_sym_decl ctxt format conj_sym_kind
  1702 fun formula_lines_for_lightweight_tags_sym_decl ctxt format conj_sym_kind
  1703         poly_nonmono_Ts type_sys n s
  1703         poly_nonmono_Ts type_enc n s
  1704         (j, (s', T_args, T, pred_sym, ary, in_conj)) =
  1704         (j, (s', T_args, T, pred_sym, ary, in_conj)) =
  1705   let
  1705   let
  1706     val ident_base =
  1706     val ident_base =
  1707       lightweight_tags_sym_formula_prefix ^ s ^
  1707       lightweight_tags_sym_formula_prefix ^ s ^
  1708       (if n > 1 then "_" ^ string_of_int j else "")
  1708       (if n > 1 then "_" ^ string_of_int j else "")
  1711       else (Axiom, I)
  1711       else (Axiom, I)
  1712     val (arg_Ts, res_T) = chop_fun ary T
  1712     val (arg_Ts, res_T) = chop_fun ary T
  1713     val bound_names =
  1713     val bound_names =
  1714       1 upto length arg_Ts |> map (`I o make_bound_var o string_of_int)
  1714       1 upto length arg_Ts |> map (`I o make_bound_var o string_of_int)
  1715     val bounds = bound_names |> map (fn name => ATerm (name, []))
  1715     val bounds = bound_names |> map (fn name => ATerm (name, []))
  1716     val cst = mk_const_aterm format type_sys (s, s') T_args
  1716     val cst = mk_const_aterm format type_enc (s, s') T_args
  1717     val atomic_Ts = atyps_of T
  1717     val atomic_Ts = atyps_of T
  1718     fun eq tms =
  1718     fun eq tms =
  1719       (if pred_sym then AConn (AIff, map AAtom tms)
  1719       (if pred_sym then AConn (AIff, map AAtom tms)
  1720        else AAtom (ATerm (`I tptp_equal, tms)))
  1720        else AAtom (ATerm (`I tptp_equal, tms)))
  1721       |> bound_tvars type_sys atomic_Ts
  1721       |> bound_tvars type_enc atomic_Ts
  1722       |> close_formula_universally
  1722       |> close_formula_universally
  1723       |> maybe_negate
  1723       |> maybe_negate
  1724     (* See also "should_tag_with_type". *)
  1724     (* See also "should_tag_with_type". *)
  1725     fun should_encode T =
  1725     fun should_encode T =
  1726       should_encode_type ctxt poly_nonmono_Ts All_Types T orelse
  1726       should_encode_type ctxt poly_nonmono_Ts All_Types T orelse
  1727       (case type_sys of
  1727       (case type_enc of
  1728          Tags (Polymorphic, level, Lightweight) =>
  1728          Tags (Polymorphic, level, Lightweight) =>
  1729          level <> All_Types andalso Monomorph.typ_has_tvars T
  1729          level <> All_Types andalso Monomorph.typ_has_tvars T
  1730        | _ => false)
  1730        | _ => false)
  1731     val tag_with = tag_with_type ctxt format poly_nonmono_Ts type_sys NONE
  1731     val tag_with = tag_with_type ctxt format poly_nonmono_Ts type_enc NONE
  1732     val add_formula_for_res =
  1732     val add_formula_for_res =
  1733       if should_encode res_T then
  1733       if should_encode res_T then
  1734         cons (Formula (ident_base ^ "_res", kind,
  1734         cons (Formula (ident_base ^ "_res", kind,
  1735                        eq [tag_with res_T (cst bounds), cst bounds],
  1735                        eq [tag_with res_T (cst bounds), cst bounds],
  1736                        simp_info, NONE))
  1736                        simp_info, NONE))
  1755   end
  1755   end
  1756 
  1756 
  1757 fun result_type_of_decl (_, _, T, _, ary, _) = chop_fun ary T |> snd
  1757 fun result_type_of_decl (_, _, T, _, ary, _) = chop_fun ary T |> snd
  1758 
  1758 
  1759 fun problem_lines_for_sym_decls ctxt format conj_sym_kind nonmono_Ts
  1759 fun problem_lines_for_sym_decls ctxt format conj_sym_kind nonmono_Ts
  1760                                 poly_nonmono_Ts type_sys (s, decls) =
  1760                                 poly_nonmono_Ts type_enc (s, decls) =
  1761   case type_sys of
  1761   case type_enc of
  1762     Simple_Types _ =>
  1762     Simple_Types _ =>
  1763     decls |> map (decl_line_for_sym ctxt format nonmono_Ts type_sys s)
  1763     decls |> map (decl_line_for_sym ctxt format nonmono_Ts type_enc s)
  1764   | Preds _ =>
  1764   | Preds _ =>
  1765     let
  1765     let
  1766       val decls =
  1766       val decls =
  1767         case decls of
  1767         case decls of
  1768           decl :: (decls' as _ :: _) =>
  1768           decl :: (decls' as _ :: _) =>
  1774               decls
  1774               decls
  1775           end
  1775           end
  1776         | _ => decls
  1776         | _ => decls
  1777       val n = length decls
  1777       val n = length decls
  1778       val decls =
  1778       val decls =
  1779         decls |> filter (should_predicate_on_type ctxt poly_nonmono_Ts type_sys
  1779         decls |> filter (should_predicate_on_type ctxt poly_nonmono_Ts type_enc
  1780                                                   (K true)
  1780                                                   (K true)
  1781                          o result_type_of_decl)
  1781                          o result_type_of_decl)
  1782     in
  1782     in
  1783       (0 upto length decls - 1, decls)
  1783       (0 upto length decls - 1, decls)
  1784       |-> map2 (formula_line_for_preds_sym_decl ctxt format conj_sym_kind
  1784       |-> map2 (formula_line_for_preds_sym_decl ctxt format conj_sym_kind
  1785                    nonmono_Ts poly_nonmono_Ts type_sys n s)
  1785                    nonmono_Ts poly_nonmono_Ts type_enc n s)
  1786     end
  1786     end
  1787   | Tags (_, _, heaviness) =>
  1787   | Tags (_, _, heaviness) =>
  1788     (case heaviness of
  1788     (case heaviness of
  1789        Heavyweight => []
  1789        Heavyweight => []
  1790      | Lightweight =>
  1790      | Lightweight =>
  1791        let val n = length decls in
  1791        let val n = length decls in
  1792          (0 upto n - 1 ~~ decls)
  1792          (0 upto n - 1 ~~ decls)
  1793          |> maps (formula_lines_for_lightweight_tags_sym_decl ctxt format
  1793          |> maps (formula_lines_for_lightweight_tags_sym_decl ctxt format
  1794                       conj_sym_kind poly_nonmono_Ts type_sys n s)
  1794                       conj_sym_kind poly_nonmono_Ts type_enc n s)
  1795        end)
  1795        end)
  1796 
  1796 
  1797 fun problem_lines_for_sym_decl_table ctxt format conj_sym_kind nonmono_Ts
  1797 fun problem_lines_for_sym_decl_table ctxt format conj_sym_kind nonmono_Ts
  1798                                      poly_nonmono_Ts type_sys sym_decl_tab =
  1798                                      poly_nonmono_Ts type_enc sym_decl_tab =
  1799   sym_decl_tab
  1799   sym_decl_tab
  1800   |> Symtab.dest
  1800   |> Symtab.dest
  1801   |> sort_wrt fst
  1801   |> sort_wrt fst
  1802   |> rpair []
  1802   |> rpair []
  1803   |-> fold_rev (append o problem_lines_for_sym_decls ctxt format conj_sym_kind
  1803   |-> fold_rev (append o problem_lines_for_sym_decls ctxt format conj_sym_kind
  1804                              nonmono_Ts poly_nonmono_Ts type_sys)
  1804                              nonmono_Ts poly_nonmono_Ts type_enc)
  1805 
  1805 
  1806 fun needs_type_tag_idempotence (Tags (poly, level, heaviness)) =
  1806 fun needs_type_tag_idempotence (Tags (poly, level, heaviness)) =
  1807     poly <> Mangled_Monomorphic andalso
  1807     poly <> Mangled_Monomorphic andalso
  1808     ((level = All_Types andalso heaviness = Lightweight) orelse
  1808     ((level = All_Types andalso heaviness = Lightweight) orelse
  1809      level = Noninf_Nonmono_Types orelse level = Fin_Nonmono_Types)
  1809      level = Noninf_Nonmono_Types orelse level = Fin_Nonmono_Types)
  1823 val conjsN = "Conjectures"
  1823 val conjsN = "Conjectures"
  1824 val free_typesN = "Type variables"
  1824 val free_typesN = "Type variables"
  1825 
  1825 
  1826 val explicit_apply = NONE (* for experimental purposes *)
  1826 val explicit_apply = NONE (* for experimental purposes *)
  1827 
  1827 
  1828 fun prepare_atp_problem ctxt format conj_sym_kind prem_kind type_sys sound
  1828 fun prepare_atp_problem ctxt format conj_sym_kind prem_kind type_enc sound
  1829         exporter readable_names preproc hyp_ts concl_t facts =
  1829         exporter readable_names preproc hyp_ts concl_t facts =
  1830   let
  1830   let
  1831     val (format, type_sys) = choose_format [format] type_sys
  1831     val (format, type_enc) = choose_format [format] type_enc
  1832     val (fact_names, (conjs, facts, class_rel_clauses, arity_clauses)) =
  1832     val (fact_names, (conjs, facts, class_rel_clauses, arity_clauses)) =
  1833       translate_formulas ctxt format prem_kind type_sys preproc hyp_ts concl_t
  1833       translate_formulas ctxt format prem_kind type_enc preproc hyp_ts concl_t
  1834                          facts
  1834                          facts
  1835     val sym_tab = conjs @ facts |> sym_table_for_facts ctxt explicit_apply
  1835     val sym_tab = conjs @ facts |> sym_table_for_facts ctxt explicit_apply
  1836     val nonmono_Ts =
  1836     val nonmono_Ts =
  1837       conjs @ facts |> nonmonotonic_types_for_facts ctxt type_sys sound
  1837       conjs @ facts |> nonmonotonic_types_for_facts ctxt type_enc sound
  1838     val repair = repair_fact ctxt format type_sys sym_tab
  1838     val repair = repair_fact ctxt format type_enc sym_tab
  1839     val (conjs, facts) = (conjs, facts) |> pairself (map repair)
  1839     val (conjs, facts) = (conjs, facts) |> pairself (map repair)
  1840     val repaired_sym_tab =
  1840     val repaired_sym_tab =
  1841       conjs @ facts |> sym_table_for_facts ctxt (SOME false)
  1841       conjs @ facts |> sym_table_for_facts ctxt (SOME false)
  1842     val helpers =
  1842     val helpers =
  1843       repaired_sym_tab |> helper_facts_for_sym_table ctxt format type_sys
  1843       repaired_sym_tab |> helper_facts_for_sym_table ctxt format type_enc
  1844                        |> map repair
  1844                        |> map repair
  1845     val poly_nonmono_Ts =
  1845     val poly_nonmono_Ts =
  1846       if null nonmono_Ts orelse nonmono_Ts = [@{typ bool}] orelse
  1846       if null nonmono_Ts orelse nonmono_Ts = [@{typ bool}] orelse
  1847          polymorphism_of_type_sys type_sys <> Polymorphic then
  1847          polymorphism_of_type_enc type_enc <> Polymorphic then
  1848         nonmono_Ts
  1848         nonmono_Ts
  1849       else
  1849       else
  1850         [TVar (("'a", 0), HOLogic.typeS)]
  1850         [TVar (("'a", 0), HOLogic.typeS)]
  1851     val sym_decl_lines =
  1851     val sym_decl_lines =
  1852       (conjs, helpers @ facts)
  1852       (conjs, helpers @ facts)
  1853       |> sym_decl_table_for_facts ctxt type_sys repaired_sym_tab
  1853       |> sym_decl_table_for_facts ctxt type_enc repaired_sym_tab
  1854       |> problem_lines_for_sym_decl_table ctxt format conj_sym_kind nonmono_Ts
  1854       |> problem_lines_for_sym_decl_table ctxt format conj_sym_kind nonmono_Ts
  1855                                                poly_nonmono_Ts type_sys
  1855                                                poly_nonmono_Ts type_enc
  1856     val helper_lines =
  1856     val helper_lines =
  1857       0 upto length helpers - 1 ~~ helpers
  1857       0 upto length helpers - 1 ~~ helpers
  1858       |> map (formula_line_for_fact ctxt format helper_prefix I false true
  1858       |> map (formula_line_for_fact ctxt format helper_prefix I false true
  1859                                     poly_nonmono_Ts type_sys)
  1859                                     poly_nonmono_Ts type_enc)
  1860       |> (if needs_type_tag_idempotence type_sys then
  1860       |> (if needs_type_tag_idempotence type_enc then
  1861             cons (type_tag_idempotence_fact ())
  1861             cons (type_tag_idempotence_fact ())
  1862           else
  1862           else
  1863             I)
  1863             I)
  1864     (* Reordering these might confuse the proof reconstruction code or the SPASS
  1864     (* Reordering these might confuse the proof reconstruction code or the SPASS
  1865        FLOTTER hack. *)
  1865        FLOTTER hack. *)
  1866     val problem =
  1866     val problem =
  1867       [(explicit_declsN, sym_decl_lines),
  1867       [(explicit_declsN, sym_decl_lines),
  1868        (factsN,
  1868        (factsN,
  1869         map (formula_line_for_fact ctxt format fact_prefix ascii_of
  1869         map (formula_line_for_fact ctxt format fact_prefix ascii_of
  1870                                    (not exporter) (not exporter) nonmono_Ts
  1870                                    (not exporter) (not exporter) nonmono_Ts
  1871                                    type_sys)
  1871                                    type_enc)
  1872             (0 upto length facts - 1 ~~ facts)),
  1872             (0 upto length facts - 1 ~~ facts)),
  1873        (class_relsN, map formula_line_for_class_rel_clause class_rel_clauses),
  1873        (class_relsN, map formula_line_for_class_rel_clause class_rel_clauses),
  1874        (aritiesN, map formula_line_for_arity_clause arity_clauses),
  1874        (aritiesN, map formula_line_for_arity_clause arity_clauses),
  1875        (helpersN, helper_lines),
  1875        (helpersN, helper_lines),
  1876        (conjsN,
  1876        (conjsN,
  1877         map (formula_line_for_conjecture ctxt format nonmono_Ts type_sys)
  1877         map (formula_line_for_conjecture ctxt format nonmono_Ts type_enc)
  1878             conjs),
  1878             conjs),
  1879        (free_typesN, formula_lines_for_free_types type_sys (facts @ conjs))]
  1879        (free_typesN, formula_lines_for_free_types type_enc (facts @ conjs))]
  1880     val problem =
  1880     val problem =
  1881       problem
  1881       problem
  1882       |> (case format of
  1882       |> (case format of
  1883             CNF => ensure_cnf_problem
  1883             CNF => ensure_cnf_problem
  1884           | CNF_UEQ => filter_cnf_ueq_problem
  1884           | CNF_UEQ => filter_cnf_ueq_problem