src/HOL/Tools/ATP/atp_problem_generate.ML
changeset 54829 157c7dfcbcd8
parent 54820 d9ab86c044fd
child 54830 152539a103d8
equal deleted inserted replaced
54828:b2271ad695db 54829:157c7dfcbcd8
   169 fun polymorphism_of_type_enc (Native (_, poly, _)) = poly
   169 fun polymorphism_of_type_enc (Native (_, poly, _)) = poly
   170   | polymorphism_of_type_enc (Guards (poly, _)) = poly
   170   | polymorphism_of_type_enc (Guards (poly, _)) = poly
   171   | polymorphism_of_type_enc (Tags (poly, _)) = poly
   171   | polymorphism_of_type_enc (Tags (poly, _)) = poly
   172 
   172 
   173 fun is_type_enc_polymorphic type_enc =
   173 fun is_type_enc_polymorphic type_enc =
   174   case polymorphism_of_type_enc type_enc of
   174   (case polymorphism_of_type_enc type_enc of
   175     Raw_Polymorphic _ => true
   175     Raw_Polymorphic _ => true
   176   | Type_Class_Polymorphic => true
   176   | Type_Class_Polymorphic => true
   177   | _ => false
   177   | _ => false)
   178 
   178 
   179 fun is_type_enc_mangling type_enc =
   179 fun is_type_enc_mangling type_enc =
   180   polymorphism_of_type_enc type_enc = Mangled_Monomorphic
   180   polymorphism_of_type_enc type_enc = Mangled_Monomorphic
   181 
   181 
   182 fun level_of_type_enc (Native (_, _, level)) = level
   182 fun level_of_type_enc (Native (_, _, level)) = level
   293         if #"A" <= c andalso c<= #"P" then
   293         if #"A" <= c andalso c<= #"P" then
   294           (* translation of #" " to #"/" *)
   294           (* translation of #" " to #"/" *)
   295           un (Char.chr (Char.ord c - upper_a_minus_space) :: rcs) cs
   295           un (Char.chr (Char.ord c - upper_a_minus_space) :: rcs) cs
   296         else
   296         else
   297           let val digits = List.take (c :: cs, 3) handle General.Subscript => [] in
   297           let val digits = List.take (c :: cs, 3) handle General.Subscript => [] in
   298             case Int.fromString (String.implode digits) of
   298             (case Int.fromString (String.implode digits) of
   299               SOME n => un (Char.chr n :: rcs) (List.drop (cs, 2))
   299               SOME n => un (Char.chr n :: rcs) (List.drop (cs, 2))
   300             | NONE => un (c :: #"_" :: rcs) cs (* ERROR *)
   300             | NONE => un (c :: #"_" :: rcs) cs (* ERROR *))
   301           end
   301           end
   302       | un rcs (c :: cs) = un (c :: rcs) cs
   302       | un rcs (c :: cs) = un (c :: rcs) cs
   303   in un [] o String.explode end
   303   in un [] o String.explode end
   304 
   304 
   305 (* If string s has the prefix s1, return the result of deleting it,
   305 (* If string s has the prefix s1, return the result of deleting it,
   363 
   363 
   364 val invert_const = perhaps (Symtab.lookup const_trans_table_inv)
   364 val invert_const = perhaps (Symtab.lookup const_trans_table_inv)
   365 val unproxify_const = perhaps (Symtab.lookup const_trans_table_unprox)
   365 val unproxify_const = perhaps (Symtab.lookup const_trans_table_unprox)
   366 
   366 
   367 fun lookup_const c =
   367 fun lookup_const c =
   368   case Symtab.lookup const_trans_table c of
   368   (case Symtab.lookup const_trans_table c of
   369     SOME c' => c'
   369     SOME c' => c'
   370   | NONE => ascii_of c
   370   | NONE => ascii_of c)
   371 
   371 
   372 fun ascii_of_indexname (v, 0) = ascii_of v
   372 fun ascii_of_indexname (v, 0) = ascii_of v
   373   | ascii_of_indexname (v, i) = ascii_of v ^ "_" ^ string_of_int i
   373   | ascii_of_indexname (v, i) = ascii_of v ^ "_" ^ string_of_int i
   374 
   374 
   375 fun make_bound_var x = bound_var_prefix ^ ascii_of x
   375 fun make_bound_var x = bound_var_prefix ^ ascii_of x
   606 fun try_unsuffixes ss s =
   606 fun try_unsuffixes ss s =
   607   fold (fn s' => fn NONE => try (unsuffix s') s | some => some) ss NONE
   607   fold (fn s' => fn NONE => try (unsuffix s') s | some => some) ss NONE
   608 
   608 
   609 fun type_enc_of_string strictness s =
   609 fun type_enc_of_string strictness s =
   610   (case try (unprefix "tc_") s of
   610   (case try (unprefix "tc_") s of
   611      SOME s => (SOME Type_Class_Polymorphic, s)
   611     SOME s => (SOME Type_Class_Polymorphic, s)
   612    | NONE =>
   612   | NONE =>
   613        case try (unprefix "poly_") s of
   613     (case try (unprefix "poly_") s of
   614          (* It's still unclear whether all TFF1 implementations will support
   614       (* It's still unclear whether all TFF1 implementations will support
   615             type signatures such as "!>[A : $tType] : $o", with phantom type
   615          type signatures such as "!>[A : $tType] : $o", with phantom type
   616             variables. *)
   616          variables. *)
   617          SOME s => (SOME (Raw_Polymorphic With_Phantom_Type_Vars), s)
   617       SOME s => (SOME (Raw_Polymorphic With_Phantom_Type_Vars), s)
   618        | NONE =>
   618     | NONE =>
   619          case try (unprefix "raw_mono_") s of
   619       (case try (unprefix "raw_mono_") s of
   620            SOME s => (SOME Raw_Monomorphic, s)
   620         SOME s => (SOME Raw_Monomorphic, s)
   621          | NONE =>
   621       | NONE =>
   622            case try (unprefix "mono_") s of
   622         (case try (unprefix "mono_") s of
   623              SOME s => (SOME Mangled_Monomorphic, s)
   623           SOME s => (SOME Mangled_Monomorphic, s)
   624            | NONE => (NONE, s))
   624         | NONE => (NONE, s)))))
   625   ||> (fn s =>
   625   ||> (fn s =>
   626           case try_unsuffixes queries s of
   626        (case try_unsuffixes queries s of
   627           SOME s =>
   627          SOME s =>
   628           (case try_unsuffixes queries s of
   628          (case try_unsuffixes queries s of
   629              SOME s => (Nonmono_Types (strictness, Non_Uniform), s)
   629            SOME s => (Nonmono_Types (strictness, Non_Uniform), s)
   630            | NONE => (Nonmono_Types (strictness, Uniform), s))
   630          | NONE => (Nonmono_Types (strictness, Uniform), s))
   631          | NONE =>
   631         | NONE =>
   632            (case try_unsuffixes ats s of
   632           (case try_unsuffixes ats s of
   633               SOME s => (Undercover_Types, s)
   633             SOME s => (Undercover_Types, s)
   634             | NONE => (All_Types, s)))
   634           | NONE => (All_Types, s))))
   635   |> (fn (poly, (level, core)) =>
   635   |> (fn (poly, (level, core)) =>
   636          case (core, (poly, level)) of
   636         (case (core, (poly, level)) of
   637            ("native", (SOME poly, _)) =>
   637           ("native", (SOME poly, _)) =>
   638            (case (poly, level) of
   638           (case (poly, level) of
   639               (Mangled_Monomorphic, _) =>
   639             (Mangled_Monomorphic, _) =>
   640               if is_type_level_uniform level then
   640             if is_type_level_uniform level then Native (First_Order, Mangled_Monomorphic, level)
   641                 Native (First_Order, Mangled_Monomorphic, level)
   641             else raise Same.SAME
   642               else
   642           | (Raw_Monomorphic, _) => raise Same.SAME
   643                 raise Same.SAME
   643           | (poly, All_Types) => Native (First_Order, poly, All_Types))
   644             | (Raw_Monomorphic, _) => raise Same.SAME
   644         | ("native_higher", (SOME poly, _)) =>
   645             | (poly, All_Types) => Native (First_Order, poly, All_Types))
   645           (case (poly, level) of
   646          | ("native_higher", (SOME poly, _)) =>
   646             (_, Nonmono_Types _) => raise Same.SAME
   647            (case (poly, level) of
   647           | (_, Undercover_Types) => raise Same.SAME
   648               (_, Nonmono_Types _) => raise Same.SAME
   648           | (Mangled_Monomorphic, _) =>
   649             | (_, Undercover_Types) => raise Same.SAME
   649             if is_type_level_uniform level then
   650             | (Mangled_Monomorphic, _) =>
   650               Native (Higher_Order THF_With_Choice, Mangled_Monomorphic, level)
   651               if is_type_level_uniform level then
   651             else
   652                 Native (Higher_Order THF_With_Choice, Mangled_Monomorphic,
   652               raise Same.SAME
   653                         level)
   653            | (poly as Raw_Polymorphic _, All_Types) =>
   654               else
   654              Native (Higher_Order THF_With_Choice, poly, All_Types)
   655                 raise Same.SAME
   655            | _ => raise Same.SAME)
   656             | (poly as Raw_Polymorphic _, All_Types) =>
   656         | ("guards", (SOME poly, _)) =>
   657               Native (Higher_Order THF_With_Choice, poly, All_Types)
   657           if (poly = Mangled_Monomorphic andalso
   658             | _ => raise Same.SAME)
   658               level = Undercover_Types) orelse
   659          | ("guards", (SOME poly, _)) =>
   659              poly = Type_Class_Polymorphic then
   660            if (poly = Mangled_Monomorphic andalso
   660             raise Same.SAME
   661                level = Undercover_Types) orelse
   661           else
   662               poly = Type_Class_Polymorphic then
   662             Guards (poly, level)
   663              raise Same.SAME
   663         | ("tags", (SOME poly, _)) =>
   664            else
   664           if (poly = Mangled_Monomorphic andalso
   665              Guards (poly, level)
   665               level = Undercover_Types) orelse
   666          | ("tags", (SOME poly, _)) =>
   666              poly = Type_Class_Polymorphic then
   667            if (poly = Mangled_Monomorphic andalso
   667             raise Same.SAME
   668                level = Undercover_Types) orelse
   668           else
   669               poly = Type_Class_Polymorphic then
   669             Tags (poly, level)
   670              raise Same.SAME
   670         | ("args", (SOME poly, All_Types (* naja *))) =>
   671            else
   671           if poly = Type_Class_Polymorphic then raise Same.SAME
   672              Tags (poly, level)
   672           else Guards (poly, Const_Types Without_Ctr_Optim)
   673          | ("args", (SOME poly, All_Types (* naja *))) =>
   673         | ("args", (SOME poly, Nonmono_Types (_, Uniform) (* naja *))) =>
   674            if poly = Type_Class_Polymorphic then raise Same.SAME
   674           if poly = Mangled_Monomorphic orelse
   675            else Guards (poly, Const_Types Without_Ctr_Optim)
   675              poly = Type_Class_Polymorphic then
   676          | ("args", (SOME poly, Nonmono_Types (_, Uniform) (* naja *))) =>
   676             raise Same.SAME
   677            if poly = Mangled_Monomorphic orelse
   677           else
   678               poly = Type_Class_Polymorphic then
   678             Guards (poly, Const_Types With_Ctr_Optim)
   679              raise Same.SAME
   679         | ("erased", (NONE, All_Types (* naja *))) =>
   680            else
   680           Guards (Raw_Polymorphic With_Phantom_Type_Vars, No_Types)
   681              Guards (poly, Const_Types With_Ctr_Optim)
   681         | _ => raise Same.SAME))
   682          | ("erased", (NONE, All_Types (* naja *))) =>
       
   683            Guards (Raw_Polymorphic With_Phantom_Type_Vars, No_Types)
       
   684          | _ => raise Same.SAME)
       
   685   handle Same.SAME => error ("Unknown type encoding: " ^ quote s ^ ".")
   682   handle Same.SAME => error ("Unknown type encoding: " ^ quote s ^ ".")
   686 
   683 
   687 fun adjust_order THF_Without_Choice (Higher_Order _) =
   684 fun adjust_order THF_Without_Choice (Higher_Order _) =
   688     Higher_Order THF_Without_Choice
   685     Higher_Order THF_Without_Choice
   689   | adjust_order _ type_enc = type_enc
   686   | adjust_order _ type_enc = type_enc
   709   | adjust_type_enc CNF_UEQ (type_enc as Guards stuff) =
   706   | adjust_type_enc CNF_UEQ (type_enc as Guards stuff) =
   710     (if is_type_enc_sound type_enc then Tags else Guards) stuff
   707     (if is_type_enc_sound type_enc then Tags else Guards) stuff
   711   | adjust_type_enc _ type_enc = type_enc
   708   | adjust_type_enc _ type_enc = type_enc
   712 
   709 
   713 fun is_lambda_free t =
   710 fun is_lambda_free t =
   714   case t of
   711   (case t of
   715     @{const Not} $ t1 => is_lambda_free t1
   712     @{const Not} $ t1 => is_lambda_free t1
   716   | Const (@{const_name All}, _) $ Abs (_, _, t') => is_lambda_free t'
   713   | Const (@{const_name All}, _) $ Abs (_, _, t') => is_lambda_free t'
   717   | Const (@{const_name All}, _) $ t1 => is_lambda_free t1
   714   | Const (@{const_name All}, _) $ t1 => is_lambda_free t1
   718   | Const (@{const_name Ex}, _) $ Abs (_, _, t') => is_lambda_free t'
   715   | Const (@{const_name Ex}, _) $ Abs (_, _, t') => is_lambda_free t'
   719   | Const (@{const_name Ex}, _) $ t1 => is_lambda_free t1
   716   | Const (@{const_name Ex}, _) $ t1 => is_lambda_free t1
   720   | @{const HOL.conj} $ t1 $ t2 => is_lambda_free t1 andalso is_lambda_free t2
   717   | @{const HOL.conj} $ t1 $ t2 => is_lambda_free t1 andalso is_lambda_free t2
   721   | @{const HOL.disj} $ t1 $ t2 => is_lambda_free t1 andalso is_lambda_free t2
   718   | @{const HOL.disj} $ t1 $ t2 => is_lambda_free t1 andalso is_lambda_free t2
   722   | @{const HOL.implies} $ t1 $ t2 => is_lambda_free t1 andalso is_lambda_free t2
   719   | @{const HOL.implies} $ t1 $ t2 => is_lambda_free t1 andalso is_lambda_free t2
   723   | Const (@{const_name HOL.eq}, Type (_, [@{typ bool}, _])) $ t1 $ t2 =>
   720   | Const (@{const_name HOL.eq}, Type (_, [@{typ bool}, _])) $ t1 $ t2 =>
   724     is_lambda_free t1 andalso is_lambda_free t2
   721     is_lambda_free t1 andalso is_lambda_free t2
   725   | _ => not (exists_subterm (fn Abs _ => true | _ => false) t)
   722   | _ => not (exists_subterm (fn Abs _ => true | _ => false) t))
   726 
   723 
   727 fun simple_translate_lambdas do_lambdas ctxt t =
   724 fun simple_translate_lambdas do_lambdas ctxt t =
   728   if is_lambda_free t then
   725   if is_lambda_free t then
   729     t
   726     t
   730   else
   727   else
   731     let
   728     let
   732       fun trans Ts t =
   729       fun trans Ts t =
   733         case t of
   730         (case t of
   734           @{const Not} $ t1 => @{const Not} $ trans Ts t1
   731           @{const Not} $ t1 => @{const Not} $ trans Ts t1
   735         | (t0 as Const (@{const_name All}, _)) $ Abs (s, T, t') =>
   732         | (t0 as Const (@{const_name All}, _)) $ Abs (s, T, t') =>
   736           t0 $ Abs (s, T, trans (T :: Ts) t')
   733           t0 $ Abs (s, T, trans (T :: Ts) t')
   737         | (t0 as Const (@{const_name All}, _)) $ t1 =>
   734         | (t0 as Const (@{const_name All}, _)) $ t1 => trans Ts (t0 $ eta_expand Ts t1 1)
   738           trans Ts (t0 $ eta_expand Ts t1 1)
       
   739         | (t0 as Const (@{const_name Ex}, _)) $ Abs (s, T, t') =>
   735         | (t0 as Const (@{const_name Ex}, _)) $ Abs (s, T, t') =>
   740           t0 $ Abs (s, T, trans (T :: Ts) t')
   736           t0 $ Abs (s, T, trans (T :: Ts) t')
   741         | (t0 as Const (@{const_name Ex}, _)) $ t1 =>
   737         | (t0 as Const (@{const_name Ex}, _)) $ t1 => trans Ts (t0 $ eta_expand Ts t1 1)
   742           trans Ts (t0 $ eta_expand Ts t1 1)
   738         | (t0 as @{const HOL.conj}) $ t1 $ t2 => t0 $ trans Ts t1 $ trans Ts t2
   743         | (t0 as @{const HOL.conj}) $ t1 $ t2 =>
   739         | (t0 as @{const HOL.disj}) $ t1 $ t2 => t0 $ trans Ts t1 $ trans Ts t2
   744           t0 $ trans Ts t1 $ trans Ts t2
   740         | (t0 as @{const HOL.implies}) $ t1 $ t2 => t0 $ trans Ts t1 $ trans Ts t2
   745         | (t0 as @{const HOL.disj}) $ t1 $ t2 =>
   741         | (t0 as Const (@{const_name HOL.eq}, Type (_, [@{typ bool}, _]))) $ t1 $ t2 =>
   746           t0 $ trans Ts t1 $ trans Ts t2
       
   747         | (t0 as @{const HOL.implies}) $ t1 $ t2 =>
       
   748           t0 $ trans Ts t1 $ trans Ts t2
       
   749         | (t0 as Const (@{const_name HOL.eq}, Type (_, [@{typ bool}, _])))
       
   750             $ t1 $ t2 =>
       
   751           t0 $ trans Ts t1 $ trans Ts t2
   742           t0 $ trans Ts t1 $ trans Ts t2
   752         | _ =>
   743         | _ =>
   753           if not (exists_subterm (fn Abs _ => true | _ => false) t) then t
   744           if not (exists_subterm (fn Abs _ => true | _ => false) t) then t
   754           else t |> Envir.eta_contract |> do_lambdas ctxt Ts
   745           else t |> Envir.eta_contract |> do_lambdas ctxt Ts)
   755       val (t, ctxt') = Variable.import_terms true [t] ctxt |>> the_single
   746       val (t, ctxt') = Variable.import_terms true [t] ctxt |>> the_single
   756     in t |> trans [] |> singleton (Variable.export_terms ctxt' ctxt) end
   747     in t |> trans [] |> singleton (Variable.export_terms ctxt' ctxt) end
   757 
   748 
   758 fun do_cheaply_conceal_lambdas Ts (t1 $ t2) =
   749 fun do_cheaply_conceal_lambdas Ts (t1 $ t2) =
   759     do_cheaply_conceal_lambdas Ts t1
   750     do_cheaply_conceal_lambdas Ts t1
   851 
   842 
   852 fun filter_type_args thy ctrss type_enc s ary T_args =
   843 fun filter_type_args thy ctrss type_enc s ary T_args =
   853   let val poly = polymorphism_of_type_enc type_enc in
   844   let val poly = polymorphism_of_type_enc type_enc in
   854     if s = type_tag_name then (* FIXME: why not "type_guard_name" as well? *)
   845     if s = type_tag_name then (* FIXME: why not "type_guard_name" as well? *)
   855       T_args
   846       T_args
   856     else case type_enc of
   847     else
   857       Native (_, Raw_Polymorphic _, _) => T_args
   848       (case type_enc of
   858     | Native (_, Type_Class_Polymorphic, _) => T_args
   849         Native (_, Raw_Polymorphic _, _) => T_args
   859     | _ =>
   850       | Native (_, Type_Class_Polymorphic, _) => T_args
   860       let
   851       | _ =>
   861         fun gen_type_args _ _ [] = []
   852         let
   862           | gen_type_args keep strip_ty T_args =
   853           fun gen_type_args _ _ [] = []
   863             let
   854             | gen_type_args keep strip_ty T_args =
   864               val U = robust_const_type thy s
   855               let
   865               val (binder_Us, body_U) = strip_ty U
   856                 val U = robust_const_type thy s
   866               val in_U_vars = fold Term.add_tvarsT binder_Us []
   857                 val (binder_Us, body_U) = strip_ty U
   867               val out_U_vars = Term.add_tvarsT body_U []
   858                 val in_U_vars = fold Term.add_tvarsT binder_Us []
   868               fun filt (U_var, T) =
   859                 val out_U_vars = Term.add_tvarsT body_U []
   869                 if keep (member (op =) in_U_vars U_var,
   860                 fun filt (U_var, T) =
   870                          member (op =) out_U_vars U_var) then
   861                   if keep (member (op =) in_U_vars U_var,
   871                   T
   862                            member (op =) out_U_vars U_var) then
   872                 else
   863                     T
   873                   dummyT
   864                   else
   874               val U_args = (s, U) |> robust_const_type_args thy
   865                     dummyT
   875             in map (filt o apfst dest_TVar) (U_args ~~ T_args) end
   866                 val U_args = (s, U) |> robust_const_type_args thy
   876             handle TYPE _ => T_args
   867               in map (filt o apfst dest_TVar) (U_args ~~ T_args) end
   877         fun is_always_ctr (s', T') =
   868               handle TYPE _ => T_args
   878           s' = s andalso type_equiv thy (T', robust_const_type thy s')
   869           fun is_always_ctr (s', T') =
   879         val noninfer_type_args = gen_type_args (not o fst) (chop_fun ary)
   870             s' = s andalso type_equiv thy (T', robust_const_type thy s')
   880         val ctr_infer_type_args = gen_type_args fst strip_type
   871           val noninfer_type_args = gen_type_args (not o fst) (chop_fun ary)
   881         val level = level_of_type_enc type_enc
   872           val ctr_infer_type_args = gen_type_args fst strip_type
   882       in
   873           val level = level_of_type_enc type_enc
   883         if level = No_Types orelse s = @{const_name HOL.eq} orelse
   874         in
   884            (case level of Const_Types _ => s = app_op_name | _ => false) then
   875           if level = No_Types orelse s = @{const_name HOL.eq} orelse
   885           []
   876              (case level of Const_Types _ => s = app_op_name | _ => false) then
   886         else if poly = Mangled_Monomorphic then
   877             []
   887           T_args
   878           else if poly = Mangled_Monomorphic then
   888         else if level = All_Types then
   879             T_args
   889           case type_enc of
   880           else if level = All_Types then
   890             Guards _ => noninfer_type_args T_args
   881             (case type_enc of
   891           | Tags _ => []
   882               Guards _ => noninfer_type_args T_args
   892         else if level = Undercover_Types then
   883             | Tags _ => [])
   893           noninfer_type_args T_args
   884           else if level = Undercover_Types then
   894         else if level <> Const_Types Without_Ctr_Optim andalso
   885             noninfer_type_args T_args
   895                 exists (exists is_always_ctr) ctrss then
   886           else if level <> Const_Types Without_Ctr_Optim andalso
   896           ctr_infer_type_args T_args
   887                   exists (exists is_always_ctr) ctrss then
   897         else
   888             ctr_infer_type_args T_args
   898           T_args
   889           else
   899       end
   890             T_args
       
   891         end)
   900   end
   892   end
   901 
   893 
   902 val fused_infinite_type_name = "ATP.fused_inf" (* shouldn't clash *)
   894 val fused_infinite_type_name = "ATP.fused_inf" (* shouldn't clash *)
   903 val fused_infinite_type = Type (fused_infinite_type_name, [])
   895 val fused_infinite_type = Type (fused_infinite_type_name, [])
   904 
   896 
  1043   | add_iterm_vars bounds (IAbs (_, tm)) = add_iterm_vars bounds tm
  1035   | add_iterm_vars bounds (IAbs (_, tm)) = add_iterm_vars bounds tm
  1044 
  1036 
  1045 fun aliased_uncurried ary (s, s') =
  1037 fun aliased_uncurried ary (s, s') =
  1046   (s ^ ascii_of_uncurried_alias_sep ^ string_of_int ary, s' ^ string_of_int ary)
  1038   (s ^ ascii_of_uncurried_alias_sep ^ string_of_int ary, s' ^ string_of_int ary)
  1047 fun unaliased_uncurried (s, s') =
  1039 fun unaliased_uncurried (s, s') =
  1048   case space_explode uncurried_alias_sep s of
  1040   (case space_explode uncurried_alias_sep s of
  1049     [_] => (s, s')
  1041     [_] => (s, s')
  1050   | [s1, s2] => (s1, unsuffix s2 s')
  1042   | [s1, s2] => (s1, unsuffix s2 s')
  1051   | _ => raise Fail "ill-formed explicit application alias"
  1043   | _ => raise Fail "ill-formed explicit application alias")
  1052 
  1044 
  1053 fun raw_mangled_const_name type_name ty_args (s, s') =
  1045 fun raw_mangled_const_name type_name ty_args (s, s') =
  1054   let
  1046   let
  1055     fun type_suffix f g =
  1047     fun type_suffix f g =
  1056       fold_rev (curry (op ^) o g o prefix mangled_type_sep o type_name f)
  1048       fold_rev (curry (op ^) o g o prefix mangled_type_sep o type_name f)
  1104       | tweak_ho_quant _ _ _ = raise Fail "unexpected type for quantifier"
  1096       | tweak_ho_quant _ _ _ = raise Fail "unexpected type for quantifier"
  1105     fun intro top_level args (IApp (tm1, tm2)) =
  1097     fun intro top_level args (IApp (tm1, tm2)) =
  1106         IApp (intro top_level (tm2 :: args) tm1, intro false [] tm2)
  1098         IApp (intro top_level (tm2 :: args) tm1, intro false [] tm2)
  1107       | intro top_level args (IConst (name as (s, _), T, T_args)) =
  1099       | intro top_level args (IConst (name as (s, _), T, T_args)) =
  1108         (case proxify_const s of
  1100         (case proxify_const s of
  1109            SOME proxy_base =>
  1101           SOME proxy_base =>
  1110            if top_level orelse is_type_enc_higher_order type_enc then
  1102           if top_level orelse is_type_enc_higher_order type_enc then
  1111              case (top_level, s) of
  1103             (case (top_level, s) of
  1112                (_, "c_False") => IConst (`I tptp_false, T, [])
  1104               (_, "c_False") => IConst (`I tptp_false, T, [])
  1113              | (_, "c_True") => IConst (`I tptp_true, T, [])
  1105             | (_, "c_True") => IConst (`I tptp_true, T, [])
  1114              | (false, "c_Not") => IConst (`I tptp_not, T, [])
  1106             | (false, "c_Not") => IConst (`I tptp_not, T, [])
  1115              | (false, "c_conj") => IConst (`I tptp_and, T, [])
  1107             | (false, "c_conj") => IConst (`I tptp_and, T, [])
  1116              | (false, "c_disj") => IConst (`I tptp_or, T, [])
  1108             | (false, "c_disj") => IConst (`I tptp_or, T, [])
  1117              | (false, "c_implies") => IConst (`I tptp_implies, T, [])
  1109             | (false, "c_implies") => IConst (`I tptp_implies, T, [])
  1118              | (false, "c_All") => tweak_ho_quant tptp_ho_forall T args
  1110             | (false, "c_All") => tweak_ho_quant tptp_ho_forall T args
  1119              | (false, "c_Ex") => tweak_ho_quant tptp_ho_exists T args
  1111             | (false, "c_Ex") => tweak_ho_quant tptp_ho_exists T args
  1120              | (false, s) =>
  1112             | (false, s) =>
  1121                if is_tptp_equal s then
  1113               if is_tptp_equal s then
  1122                  if length args = 2 then
  1114                 if length args = 2 then
  1123                    IConst (`I tptp_equal, T, [])
  1115                   IConst (`I tptp_equal, T, [])
  1124                  else
  1116                 else
  1125                    (* Eta-expand partially applied THF equality, because the
  1117                   (* Eta-expand partially applied THF equality, because the
  1126                       LEO-II and Satallax parsers complain about not being able to
  1118                      LEO-II and Satallax parsers complain about not being able to
  1127                       infer the type of "=". *)
  1119                      infer the type of "=". *)
  1128                    let val i_T = domain_type T in
  1120                   let val i_T = domain_type T in
  1129                      IAbs ((`I "Y", i_T),
  1121                     IAbs ((`I "Y", i_T),
  1130                            IAbs ((`I "Z", i_T),
  1122                           IAbs ((`I "Z", i_T),
  1131                                  IApp (IApp (IConst (`I tptp_equal, T, []),
  1123                                 IApp (IApp (IConst (`I tptp_equal, T, []),
  1132                                              IConst (`I "Y", i_T, [])),
  1124                                             IConst (`I "Y", i_T, [])),
  1133                                        IConst (`I "Z", i_T, []))))
  1125                                       IConst (`I "Z", i_T, []))))
  1134                    end
  1126                   end
  1135                else
  1127               else
  1136                  IConst (name, T, [])
  1128                 IConst (name, T, [])
  1137              | _ => IConst (name, T, [])
  1129             | _ => IConst (name, T, []))
  1138            else
  1130           else
  1139              IConst (proxy_base |>> prefix const_prefix, T, T_args)
  1131             IConst (proxy_base |>> prefix const_prefix, T, T_args)
  1140           | NONE => if s = tptp_choice then tweak_ho_quant tptp_choice T args
  1132          | NONE => if s = tptp_choice then tweak_ho_quant tptp_choice T args
  1141                     else IConst (name, T, T_args))
  1133                    else IConst (name, T, T_args))
  1142       | intro _ _ (IAbs (bound, tm)) = IAbs (bound, intro false [] tm)
  1134       | intro _ _ (IAbs (bound, tm)) = IAbs (bound, intro false [] tm)
  1143       | intro _ _ tm = tm
  1135       | intro _ _ tm = tm
  1144   in intro true [] end
  1136   in intro true [] end
  1145 
  1137 
  1146 fun mangle_type_args_in_const type_enc (name as (s, _)) T_args =
  1138 fun mangle_type_args_in_const type_enc (name as (s, _)) T_args =
  1162   else
  1154   else
  1163     I
  1155     I
  1164 
  1156 
  1165 fun filter_type_args_in_const _ _ _ _ _ [] = []
  1157 fun filter_type_args_in_const _ _ _ _ _ [] = []
  1166   | filter_type_args_in_const thy ctrss type_enc ary s T_args =
  1158   | filter_type_args_in_const thy ctrss type_enc ary s T_args =
  1167     case unprefix_and_unascii const_prefix s of
  1159     (case unprefix_and_unascii const_prefix s of
  1168       NONE =>
  1160       NONE => if level_of_type_enc type_enc = No_Types orelse s = tptp_choice then [] else T_args
  1169       if level_of_type_enc type_enc = No_Types orelse s = tptp_choice then []
  1161     | SOME s'' => filter_type_args thy ctrss type_enc (unmangled_invert_const s'') ary T_args)
  1170       else T_args
       
  1171     | SOME s'' =>
       
  1172       filter_type_args thy ctrss type_enc (unmangled_invert_const s'') ary
       
  1173                        T_args
       
  1174 
  1162 
  1175 fun filter_type_args_in_iterm thy ctrss type_enc =
  1163 fun filter_type_args_in_iterm thy ctrss type_enc =
  1176   let
  1164   let
  1177     fun filt ary (IApp (tm1, tm2)) = IApp (filt (ary + 1) tm1, filt 0 tm2)
  1165     fun filt ary (IApp (tm1, tm2)) = IApp (filt (ary + 1) tm1, filt 0 tm2)
  1178       | filt ary (IConst (name as (s, _), T, T_args)) =
  1166       | filt ary (IConst (name as (s, _), T, T_args)) =
  1205         ##> union (op =) (atomic_types_of T)
  1193         ##> union (op =) (atomic_types_of T)
  1206       end
  1194       end
  1207     and do_conn bs c pos1 t1 pos2 t2 =
  1195     and do_conn bs c pos1 t1 pos2 t2 =
  1208       do_formula bs pos1 t1 ##>> do_formula bs pos2 t2 #>> uncurry (mk_aconn c)
  1196       do_formula bs pos1 t1 ##>> do_formula bs pos2 t2 #>> uncurry (mk_aconn c)
  1209     and do_formula bs pos t =
  1197     and do_formula bs pos t =
  1210       case t of
  1198       (case t of
  1211         @{const Trueprop} $ t1 => do_formula bs pos t1
  1199         @{const Trueprop} $ t1 => do_formula bs pos t1
  1212       | @{const Not} $ t1 => do_formula bs (Option.map not pos) t1 #>> mk_anot
  1200       | @{const Not} $ t1 => do_formula bs (Option.map not pos) t1 #>> mk_anot
  1213       | Const (@{const_name All}, _) $ Abs (s, T, t') =>
  1201       | Const (@{const_name All}, _) $ Abs (s, T, t') => do_quant bs AForall pos s T t'
  1214         do_quant bs AForall pos s T t'
       
  1215       | (t0 as Const (@{const_name All}, _)) $ t1 =>
  1202       | (t0 as Const (@{const_name All}, _)) $ t1 =>
  1216         do_formula bs pos (t0 $ eta_expand (map (snd o snd) bs) t1 1)
  1203         do_formula bs pos (t0 $ eta_expand (map (snd o snd) bs) t1 1)
  1217       | Const (@{const_name Ex}, _) $ Abs (s, T, t') =>
  1204       | Const (@{const_name Ex}, _) $ Abs (s, T, t') => do_quant bs AExists pos s T t'
  1218         do_quant bs AExists pos s T t'
       
  1219       | (t0 as Const (@{const_name Ex}, _)) $ t1 =>
  1205       | (t0 as Const (@{const_name Ex}, _)) $ t1 =>
  1220         do_formula bs pos (t0 $ eta_expand (map (snd o snd) bs) t1 1)
  1206         do_formula bs pos (t0 $ eta_expand (map (snd o snd) bs) t1 1)
  1221       | @{const HOL.conj} $ t1 $ t2 => do_conn bs AAnd pos t1 pos t2
  1207       | @{const HOL.conj} $ t1 $ t2 => do_conn bs AAnd pos t1 pos t2
  1222       | @{const HOL.disj} $ t1 $ t2 => do_conn bs AOr pos t1 pos t2
  1208       | @{const HOL.disj} $ t1 $ t2 => do_conn bs AOr pos t1 pos t2
  1223       | @{const HOL.implies} $ t1 $ t2 =>
  1209       | @{const HOL.implies} $ t1 $ t2 => do_conn bs AImplies (Option.map not pos) t1 pos t2
  1224         do_conn bs AImplies (Option.map not pos) t1 pos t2
       
  1225       | Const (@{const_name HOL.eq}, Type (_, [@{typ bool}, _])) $ t1 $ t2 =>
  1210       | Const (@{const_name HOL.eq}, Type (_, [@{typ bool}, _])) $ t1 $ t2 =>
  1226         if iff_for_eq then do_conn bs AIff NONE t1 NONE t2 else do_term bs t
  1211         if iff_for_eq then do_conn bs AIff NONE t1 NONE t2 else do_term bs t
  1227       | _ => do_term bs t
  1212       | _ => do_term bs t)
  1228   in do_formula [] end
  1213   in do_formula [] end
  1229 
  1214 
  1230 fun presimplify_term ctxt t =
  1215 fun presimplify_term ctxt t =
  1231   if exists_Const (member (op =) Meson.presimplified_consts o fst) t then
  1216   if exists_Const (member (op =) Meson.presimplified_consts o fst) t then
  1232     t |> Skip_Proof.make_thm (Proof_Context.theory_of ctxt)
  1217     t |> Skip_Proof.make_thm (Proof_Context.theory_of ctxt)
  1309          is_legitimate_tptp_def t then
  1294          is_legitimate_tptp_def t then
  1310         Definition
  1295         Definition
  1311       else
  1296       else
  1312         Axiom
  1297         Axiom
  1313   in
  1298   in
  1314     case t |> make_formula ctxt format type_enc iff_for_eq name stature role of
  1299     (case t |> make_formula ctxt format type_enc iff_for_eq name stature role of
  1315       formula as {iformula = AAtom (IConst ((s, _), _, _)), ...} =>
  1300       formula as {iformula = AAtom (IConst ((s, _), _, _)), ...} =>
  1316       if s = tptp_true then NONE else SOME formula
  1301       if s = tptp_true then NONE else SOME formula
  1317     | formula => SOME formula
  1302     | formula => SOME formula)
  1318   end
  1303   end
  1319 
  1304 
  1320 fun make_conjecture ctxt format type_enc =
  1305 fun make_conjecture ctxt format type_enc =
  1321   map (fn ((name, stature), (role, t)) =>
  1306   map (fn ((name, stature), (role, t)) =>
  1322           let
  1307           let
  1335 
  1320 
  1336 (** Finite and infinite type inference **)
  1321 (** Finite and infinite type inference **)
  1337 
  1322 
  1338 fun tvar_footprint thy s ary =
  1323 fun tvar_footprint thy s ary =
  1339   (case unprefix_and_unascii const_prefix s of
  1324   (case unprefix_and_unascii const_prefix s of
  1340      SOME s =>
  1325     SOME s =>
  1341      let fun tvars_of T = [] |> Term.add_tvarsT T |> map fst in
  1326     let fun tvars_of T = [] |> Term.add_tvarsT T |> map fst in
  1342        s |> unmangled_invert_const |> robust_const_type thy |> chop_fun ary
  1327       s |> unmangled_invert_const |> robust_const_type thy |> chop_fun ary |> fst |> map tvars_of
  1343          |> fst |> map tvars_of
  1328     end
  1344      end
  1329   | NONE => [])
  1345    | NONE => [])
       
  1346   handle TYPE _ => []
  1330   handle TYPE _ => []
  1347 
  1331 
  1348 fun type_arg_cover thy pos s ary =
  1332 fun type_arg_cover thy pos s ary =
  1349   if is_tptp_equal s then
  1333   if is_tptp_equal s then
  1350     if pos = SOME false then [] else 0 upto ary - 1
  1334     if pos = SOME false then [] else 0 upto ary - 1
  1416   Elsewhere
  1400   Elsewhere
  1417 
  1401 
  1418 fun should_tag_with_type _ _ _ (Top_Level _) _ _ = false
  1402 fun should_tag_with_type _ _ _ (Top_Level _) _ _ = false
  1419   | should_tag_with_type ctxt mono (Tags (_, level)) site u T =
  1403   | should_tag_with_type ctxt mono (Tags (_, level)) site u T =
  1420     let val thy = Proof_Context.theory_of ctxt in
  1404     let val thy = Proof_Context.theory_of ctxt in
  1421       case level of
  1405       (case level of
  1422         Nonmono_Types (_, Non_Uniform) =>
  1406         Nonmono_Types (_, Non_Uniform) =>
  1423         (case (site, is_maybe_universal_var u) of
  1407         (case (site, is_maybe_universal_var u) of
  1424            (Eq_Arg pos, true) =>
  1408           (Eq_Arg pos, true) =>
  1425            (pos <> SOME false orelse tag_neg_vars) andalso
  1409           (pos <> SOME false orelse tag_neg_vars) andalso should_encode_type ctxt mono level T
  1426            should_encode_type ctxt mono level T
  1410         | _ => false)
  1427          | _ => false)
       
  1428       | Undercover_Types =>
  1411       | Undercover_Types =>
  1429         (case (site, is_maybe_universal_var u) of
  1412         (case (site, is_maybe_universal_var u) of
  1430            (Eq_Arg pos, true) => pos <> SOME false
  1413           (Eq_Arg pos, true) => pos <> SOME false
  1431          | (Arg (s, j, ary), true) =>
  1414         | (Arg (s, j, ary), true) => member (op =) (type_arg_cover thy NONE s ary) j
  1432            member (op =) (type_arg_cover thy NONE s ary) j
  1415         | _ => false)
  1433          | _ => false)
  1416       | _ => should_encode_type ctxt mono level T)
  1434       | _ => should_encode_type ctxt mono level T
       
  1435     end
  1417     end
  1436   | should_tag_with_type _ _ _ _ _ _ = false
  1418   | should_tag_with_type _ _ _ _ _ _ = false
  1437 
  1419 
  1438 fun fused_type ctxt mono level =
  1420 fun fused_type ctxt mono level =
  1439   let
  1421   let
  1450   {pred_sym : bool, min_ary : int, max_ary : int, types : typ list,
  1432   {pred_sym : bool, min_ary : int, max_ary : int, types : typ list,
  1451    in_conj : bool}
  1433    in_conj : bool}
  1452 
  1434 
  1453 fun default_sym_tab_entries type_enc =
  1435 fun default_sym_tab_entries type_enc =
  1454   (make_fixed_const NONE @{const_name undefined},
  1436   (make_fixed_const NONE @{const_name undefined},
  1455        {pred_sym = false, min_ary = 0, max_ary = 0, types = [],
  1437      {pred_sym = false, min_ary = 0, max_ary = 0, types = [], in_conj = false}) ::
  1456         in_conj = false}) ::
       
  1457   ([tptp_false, tptp_true]
  1438   ([tptp_false, tptp_true]
  1458    |> map (rpair {pred_sym = true, min_ary = 0, max_ary = 0, types = [],
  1439    |> map (rpair {pred_sym = true, min_ary = 0, max_ary = 0, types = [], in_conj = false})) @
  1459                   in_conj = false})) @
       
  1460   ([tptp_equal, tptp_old_equal]
  1440   ([tptp_equal, tptp_old_equal]
  1461    |> map (rpair {pred_sym = true, min_ary = 2, max_ary = 2, types = [],
  1441    |> map (rpair {pred_sym = true, min_ary = 2, max_ary = 2, types = [], in_conj = false}))
  1462                   in_conj = false}))
       
  1463   |> not (is_type_enc_higher_order type_enc)
  1442   |> not (is_type_enc_higher_order type_enc)
  1464      ? cons (prefixed_predicator_name,
  1443     ? cons (prefixed_predicator_name,
  1465              {pred_sym = true, min_ary = 1, max_ary = 1, types = [],
  1444       {pred_sym = true, min_ary = 1, max_ary = 1, types = [], in_conj = false})
  1466               in_conj = false})
       
  1467 
  1445 
  1468 datatype app_op_level =
  1446 datatype app_op_level =
  1469   Min_App_Op |
  1447   Min_App_Op |
  1470   Sufficient_App_Op |
  1448   Sufficient_App_Op |
  1471   Sufficient_App_Op_And_Predicator |
  1449   Sufficient_App_Op_And_Predicator |
  1475   let
  1453   let
  1476     val thy = Proof_Context.theory_of ctxt
  1454     val thy = Proof_Context.theory_of ctxt
  1477     fun consider_var_ary const_T var_T max_ary =
  1455     fun consider_var_ary const_T var_T max_ary =
  1478       let
  1456       let
  1479         fun iter ary T =
  1457         fun iter ary T =
  1480           if ary = max_ary orelse type_instance thy var_T T orelse
  1458           if ary = max_ary orelse type_instance thy var_T T orelse type_instance thy T var_T then
  1481              type_instance thy T var_T then
       
  1482             ary
  1459             ary
  1483           else
  1460           else
  1484             iter (ary + 1) (range_type T)
  1461             iter (ary + 1) (range_type T)
  1485       in iter 0 const_T end
  1462       in iter 0 const_T end
  1486     fun add_universal_var T (accum as ((bool_vars, fun_var_Ts), sym_tab)) =
  1463     fun add_universal_var T (accum as ((bool_vars, fun_var_Ts), sym_tab)) =
  1488          (app_op_level = Sufficient_App_Op_And_Predicator andalso
  1465          (app_op_level = Sufficient_App_Op_And_Predicator andalso
  1489           (can dest_funT T orelse T = @{typ bool})) then
  1466           (can dest_funT T orelse T = @{typ bool})) then
  1490         let
  1467         let
  1491           val bool_vars' =
  1468           val bool_vars' =
  1492             bool_vars orelse
  1469             bool_vars orelse
  1493             (app_op_level = Sufficient_App_Op_And_Predicator andalso
  1470             (app_op_level = Sufficient_App_Op_And_Predicator andalso body_type T = @{typ bool})
  1494              body_type T = @{typ bool})
       
  1495           fun repair_min_ary {pred_sym, min_ary, max_ary, types, in_conj} =
  1471           fun repair_min_ary {pred_sym, min_ary, max_ary, types, in_conj} =
  1496             {pred_sym = pred_sym andalso not bool_vars',
  1472             {pred_sym = pred_sym andalso not bool_vars',
  1497              min_ary = fold (fn T' => consider_var_ary T' T) types min_ary,
  1473              min_ary = fold (fn T' => consider_var_ary T' T) types min_ary,
  1498              max_ary = max_ary, types = types, in_conj = in_conj}
  1474              max_ary = max_ary, types = types, in_conj = in_conj}
  1499           val fun_var_Ts' =
  1475           val fun_var_Ts' = fun_var_Ts |> can dest_funT T ? insert_type thy I T
  1500             fun_var_Ts |> can dest_funT T ? insert_type thy I T
       
  1501         in
  1476         in
  1502           if bool_vars' = bool_vars andalso
  1477           if bool_vars' = bool_vars andalso pointer_eq (fun_var_Ts', fun_var_Ts) then accum
  1503              pointer_eq (fun_var_Ts', fun_var_Ts) then
  1478           else ((bool_vars', fun_var_Ts'), Symtab.map (K repair_min_ary) sym_tab)
  1504             accum
       
  1505           else
       
  1506             ((bool_vars', fun_var_Ts'), Symtab.map (K repair_min_ary) sym_tab)
       
  1507         end
  1479         end
  1508       else
  1480       else
  1509         accum
  1481         accum
  1510     fun add_iterm_syms top_level tm
  1482     fun add_iterm_syms top_level tm (accum as ((bool_vars, fun_var_Ts), sym_tab)) =
  1511                        (accum as ((bool_vars, fun_var_Ts), sym_tab)) =
       
  1512       let val (head, args) = strip_iterm_comb tm in
  1483       let val (head, args) = strip_iterm_comb tm in
  1513         (case head of
  1484         (case head of
  1514            IConst ((s, _), T, _) =>
  1485           IConst ((s, _), T, _) =>
  1515            if is_maybe_universal_name s then
  1486           if is_maybe_universal_name s then
  1516              add_universal_var T accum
  1487             add_universal_var T accum
  1517            else if String.isPrefix exist_bound_var_prefix s then
  1488           else if String.isPrefix exist_bound_var_prefix s then
  1518              accum
  1489             accum
  1519            else
  1490           else
  1520              let val ary = length args in
  1491             let val ary = length args in
  1521                ((bool_vars, fun_var_Ts),
  1492               ((bool_vars, fun_var_Ts),
  1522                 case Symtab.lookup sym_tab s of
  1493                (case Symtab.lookup sym_tab s of
  1523                   SOME {pred_sym, min_ary, max_ary, types, in_conj} =>
  1494                  SOME {pred_sym, min_ary, max_ary, types, in_conj} =>
  1524                   let
  1495                  let
  1525                     val pred_sym =
  1496                    val pred_sym = pred_sym andalso top_level andalso not bool_vars
  1526                       pred_sym andalso top_level andalso not bool_vars
  1497                    val types' = types |> insert_type thy I T
  1527                     val types' = types |> insert_type thy I T
  1498                    val in_conj = in_conj orelse conj_fact
  1528                     val in_conj = in_conj orelse conj_fact
  1499                    val min_ary =
  1529                     val min_ary =
  1500                      if (app_op_level = Sufficient_App_Op orelse
  1530                       if (app_op_level = Sufficient_App_Op orelse
  1501                          app_op_level = Sufficient_App_Op_And_Predicator)
  1531                           app_op_level = Sufficient_App_Op_And_Predicator)
  1502                         andalso not (pointer_eq (types', types)) then
  1532                          andalso not (pointer_eq (types', types)) then
  1503                        fold (consider_var_ary T) fun_var_Ts min_ary
  1533                         fold (consider_var_ary T) fun_var_Ts min_ary
  1504                      else
  1534                       else
  1505                        min_ary
  1535                         min_ary
  1506                  in
  1536                   in
  1507                    Symtab.update (s, {pred_sym = pred_sym, min_ary = Int.min (ary, min_ary),
  1537                     Symtab.update (s, {pred_sym = pred_sym,
  1508                      max_ary = Int.max (ary, max_ary), types = types', in_conj = in_conj}) sym_tab
  1538                                        min_ary = Int.min (ary, min_ary),
  1509                  end
  1539                                        max_ary = Int.max (ary, max_ary),
  1510                | NONE =>
  1540                                        types = types', in_conj = in_conj})
  1511                  let
  1541                                   sym_tab
  1512                    val max_ary =
  1542                   end
  1513                      (case unprefix_and_unascii const_prefix s of
  1543                 | NONE =>
  1514                        SOME s =>
  1544                   let
  1515                        (if String.isSubstring uncurried_alias_sep s then
  1545                     val max_ary =
  1516                           ary
  1546                       case unprefix_and_unascii const_prefix s of
  1517                         else
  1547                         SOME s =>
  1518                           (case try (ary_of o robust_const_type thy o unmangled_invert_const) s of
  1548                         (if String.isSubstring uncurried_alias_sep s then
  1519                             SOME ary0 => Int.min (ary0, ary)
  1549                            ary
  1520                           | NONE => ary))
  1550                          else case try (ary_of o robust_const_type thy
  1521                      | NONE => ary)
  1551                                         o unmangled_invert_const) s of
  1522                    val pred_sym = top_level andalso max_ary = ary andalso not bool_vars
  1552                            SOME ary0 => Int.min (ary0, ary)
  1523                    val min_ary =
  1553                          | NONE => ary)
  1524                      (case app_op_level of
  1554                       | NONE => ary
  1525                        Min_App_Op => max_ary
  1555                     val pred_sym = top_level andalso max_ary = ary andalso not bool_vars
  1526                      | Full_App_Op_And_Predicator => 0
  1556                     val min_ary =
  1527                      | _ => fold (consider_var_ary T) fun_var_Ts max_ary)
  1557                       case app_op_level of
  1528                  in
  1558                         Min_App_Op => max_ary
  1529                    Symtab.update_new (s, {pred_sym = pred_sym, min_ary = min_ary, max_ary = max_ary,
  1559                       | Full_App_Op_And_Predicator => 0
  1530                      types = [T], in_conj = conj_fact}) sym_tab
  1560                       | _ => fold (consider_var_ary T) fun_var_Ts max_ary
  1531                  end))
  1561                   in
  1532             end
  1562                     Symtab.update_new (s,
  1533         | IVar (_, T) => add_universal_var T accum
  1563                         {pred_sym = pred_sym, min_ary = min_ary,
  1534         | IAbs ((_, T), tm) => accum |> add_universal_var T |> add_iterm_syms false tm
  1564                          max_ary = max_ary, types = [T], in_conj = conj_fact})
  1535         | _ => accum)
  1565                         sym_tab
       
  1566                   end)
       
  1567              end
       
  1568          | IVar (_, T) => add_universal_var T accum
       
  1569          | IAbs ((_, T), tm) =>
       
  1570            accum |> add_universal_var T |> add_iterm_syms false tm
       
  1571          | _ => accum)
       
  1572         |> fold (add_iterm_syms false) args
  1536         |> fold (add_iterm_syms false) args
  1573       end
  1537       end
  1574   in add_iterm_syms end
  1538   in add_iterm_syms end
  1575 
  1539 
  1576 fun sym_table_of_facts ctxt type_enc app_op_level conjs facts =
  1540 fun sym_table_of_facts ctxt type_enc app_op_level conjs facts =
  1577   let
  1541   let
  1578     fun add_iterm_syms conj_fact =
  1542     fun add_iterm_syms conj_fact = add_iterm_syms_to_sym_table ctxt app_op_level conj_fact true
  1579       add_iterm_syms_to_sym_table ctxt app_op_level conj_fact true
  1543     fun add_fact_syms conj_fact = ifact_lift (formula_fold NONE (K (add_iterm_syms conj_fact)))
  1580     fun add_fact_syms conj_fact =
       
  1581       ifact_lift (formula_fold NONE (K (add_iterm_syms conj_fact)))
       
  1582   in
  1544   in
  1583     ((false, []), Symtab.empty)
  1545     ((false, []), Symtab.empty)
  1584     |> fold (add_fact_syms true) conjs
  1546     |> fold (add_fact_syms true) conjs
  1585     |> fold (add_fact_syms false) facts
  1547     |> fold (add_fact_syms false) facts
  1586     ||> fold Symtab.update (default_sym_tab_entries type_enc)
  1548     ||> fold Symtab.update (default_sym_tab_entries type_enc)
  1587   end
  1549   end
  1588 
  1550 
  1589 fun min_ary_of sym_tab s =
  1551 fun min_ary_of sym_tab s =
  1590   case Symtab.lookup sym_tab s of
  1552   (case Symtab.lookup sym_tab s of
  1591     SOME ({min_ary, ...} : sym_info) => min_ary
  1553     SOME ({min_ary, ...} : sym_info) => min_ary
  1592   | NONE =>
  1554   | NONE =>
  1593     case unprefix_and_unascii const_prefix s of
  1555     (case unprefix_and_unascii const_prefix s of
  1594       SOME s =>
  1556       SOME s =>
  1595       let val s = s |> unmangled_invert_const in
  1557       let val s = s |> unmangled_invert_const in
  1596         if s = predicator_name then 1
  1558         if s = predicator_name then 1
  1597         else if s = app_op_name then 2
  1559         else if s = app_op_name then 2
  1598         else if s = type_guard_name then 1
  1560         else if s = type_guard_name then 1
  1599         else 0
  1561         else 0
  1600       end
  1562       end
  1601     | NONE => 0
  1563     | NONE => 0))
  1602 
  1564 
  1603 (* True if the constant ever appears outside of the top-level position in
  1565 (* True if the constant ever appears outside of the top-level position in
  1604    literals, or if it appears with different arities (e.g., because of different
  1566    literals, or if it appears with different arities (e.g., because of different
  1605    type instantiations). If false, the constant always receives all of its
  1567    type instantiations). If false, the constant always receives all of its
  1606    arguments and is used as a predicate. *)
  1568    arguments and is used as a predicate. *)
  1607 fun is_pred_sym sym_tab s =
  1569 fun is_pred_sym sym_tab s =
  1608   case Symtab.lookup sym_tab s of
  1570   (case Symtab.lookup sym_tab s of
  1609     SOME ({pred_sym, min_ary, max_ary, ...} : sym_info) =>
  1571     SOME ({pred_sym, min_ary, max_ary, ...} : sym_info) => pred_sym andalso min_ary = max_ary
  1610     pred_sym andalso min_ary = max_ary
  1572   | NONE => false)
  1611   | NONE => false
  1573 
  1612 
  1574 val fTrue_iconst = IConst ((const_prefix ^ "fTrue", @{const_name ATP.fTrue}), @{typ bool}, [])
  1613 val fTrue_iconst =
  1575 val predicator_iconst = IConst (`(make_fixed_const NONE) predicator_name, @{typ "bool => bool"}, [])
  1614   IConst ((const_prefix ^ "fTrue", @{const_name ATP.fTrue}), @{typ bool}, [])
       
  1615 val predicator_iconst =
       
  1616   IConst (`(make_fixed_const NONE) predicator_name, @{typ "bool => bool"}, [])
       
  1617 
  1576 
  1618 fun predicatify completish tm =
  1577 fun predicatify completish tm =
  1619   if completish then
  1578   if completish then
  1620     IApp (IApp (IConst (`I tptp_equal, @{typ "bool => bool => bool"}, []), tm),
  1579     IApp (IApp (IConst (`I tptp_equal, @{typ "bool => bool => bool"}, []), tm), fTrue_iconst)
  1621           fTrue_iconst)
       
  1622   else
  1580   else
  1623     IApp (predicator_iconst, tm)
  1581     IApp (predicator_iconst, tm)
  1624 
  1582 
  1625 val app_op = `(make_fixed_const NONE) app_op_name
  1583 val app_op = `(make_fixed_const NONE) app_op_name
  1626 
  1584 
  1639   let
  1597   let
  1640     fun do_app arg head = mk_app_op type_enc head arg
  1598     fun do_app arg head = mk_app_op type_enc head arg
  1641     fun list_app_ops (head, args) = fold do_app args head
  1599     fun list_app_ops (head, args) = fold do_app args head
  1642     fun introduce_app_ops tm =
  1600     fun introduce_app_ops tm =
  1643       let val (head, args) = tm |> strip_iterm_comb ||> map introduce_app_ops in
  1601       let val (head, args) = tm |> strip_iterm_comb ||> map introduce_app_ops in
  1644         case head of
  1602         (case head of
  1645           IConst (name as (s, _), T, T_args) =>
  1603           IConst (name as (s, _), T, T_args) =>
  1646           let
  1604           let
  1647             val min_ary = min_ary_of sym_tab s
  1605             val min_ary = min_ary_of sym_tab s
  1648             val ary =
  1606             val ary =
  1649               if uncurried_aliases andalso String.isPrefix const_prefix s then
  1607               if uncurried_aliases andalso String.isPrefix const_prefix s then
  1654                      arguments than its signature (even though such concrete
  1612                      arguments than its signature (even though such concrete
  1655                      instances, where a type variable is instantiated by a
  1613                      instances, where a type variable is instantiated by a
  1656                      function type, are possible.) *)
  1614                      function type, are possible.) *)
  1657                   val official_ary =
  1615                   val official_ary =
  1658                     if is_type_enc_polymorphic type_enc then
  1616                     if is_type_enc_polymorphic type_enc then
  1659                       case unprefix_and_unascii const_prefix s of
  1617                       (case unprefix_and_unascii const_prefix s of
  1660                         SOME s' =>
  1618                         SOME s' =>
  1661                         (case try (ary_of o robust_const_type thy)
  1619                         (case try (ary_of o robust_const_type thy) (invert_const s') of
  1662                                   (invert_const s') of
  1620                           SOME ary => ary
  1663                            SOME ary => ary
  1621                         | NONE => min_ary)
  1664                          | NONE => min_ary)
  1622                       | NONE => min_ary)
  1665                       | NONE => min_ary
       
  1666                     else
  1623                     else
  1667                       1000000000 (* irrealistically big arity *)
  1624                       1000000000 (* irrealistically big arity *)
  1668                 in Int.min (ary, official_ary) end
  1625                 in Int.min (ary, official_ary) end
  1669               else
  1626               else
  1670                 min_ary
  1627                 min_ary
  1671             val head =
  1628             val head =
  1672               if ary = min_ary then head
  1629               if ary = min_ary then head else IConst (aliased_uncurried ary name, T, T_args)
  1673               else IConst (aliased_uncurried ary name, T, T_args)
       
  1674           in
  1630           in
  1675             args |> chop ary |>> list_app head |> list_app_ops
  1631             args |> chop ary |>> list_app head |> list_app_ops
  1676           end
  1632           end
  1677         | _ => list_app_ops (head, args)
  1633         | _ => list_app_ops (head, args))
  1678       end
  1634       end
  1679     fun introduce_predicators tm =
  1635     fun introduce_predicators tm =
  1680       case strip_iterm_comb tm of
  1636       (case strip_iterm_comb tm of
  1681         (IConst ((s, _), _, _), _) =>
  1637         (IConst ((s, _), _, _), _) =>
  1682         if is_pred_sym sym_tab s then tm else predicatify completish tm
  1638         if is_pred_sym sym_tab s then tm else predicatify completish tm
  1683       | _ => predicatify completish tm
  1639       | _ => predicatify completish tm)
  1684     val do_iterm =
  1640     val do_iterm =
  1685       not (is_type_enc_higher_order type_enc)
  1641       not (is_type_enc_higher_order type_enc) ? (introduce_app_ops #> introduce_predicators)
  1686       ? (introduce_app_ops #> introduce_predicators)
       
  1687       #> filter_type_args_in_iterm thy ctrss type_enc
  1642       #> filter_type_args_in_iterm thy ctrss type_enc
  1688   in update_iformula (formula_map do_iterm) end
  1643   in update_iformula (formula_map do_iterm) end
  1689 
  1644 
  1690 (** Helper facts **)
  1645 (** Helper facts **)
  1691 
  1646 
  1713   [(("fNot", false),
  1668   [(("fNot", false),
  1714     @{thms fNot_def [THEN Meson.iff_to_disjD, THEN conjunct1]
  1669     @{thms fNot_def [THEN Meson.iff_to_disjD, THEN conjunct1]
  1715            fNot_def [THEN Meson.iff_to_disjD, THEN conjunct2]}
  1670            fNot_def [THEN Meson.iff_to_disjD, THEN conjunct2]}
  1716     |> map (pair Non_Rec_Def)),
  1671     |> map (pair Non_Rec_Def)),
  1717    (("fconj", false),
  1672    (("fconj", false),
  1718     @{lemma "~ P | ~ Q | fconj P Q" "~ fconj P Q | P" "~ fconj P Q | Q"
  1673     @{lemma "~ P | ~ Q | fconj P Q" "~ fconj P Q | P" "~ fconj P Q | Q" by (unfold fconj_def) fast+}
  1719         by (unfold fconj_def) fast+}
       
  1720     |> map (pair General)),
  1674     |> map (pair General)),
  1721    (("fdisj", false),
  1675    (("fdisj", false),
  1722     @{lemma "~ P | fdisj P Q" "~ Q | fdisj P Q" "~ fdisj P Q | P | Q"
  1676     @{lemma "~ P | fdisj P Q" "~ Q | fdisj P Q" "~ fdisj P Q | P | Q" by (unfold fdisj_def) fast+}
  1723         by (unfold fdisj_def) fast+}
       
  1724     |> map (pair General)),
  1677     |> map (pair General)),
  1725    (("fimplies", false),
  1678    (("fimplies", false),
  1726     @{lemma "P | fimplies P Q" "~ Q | fimplies P Q" "~ fimplies P Q | ~ P | Q"
  1679     @{lemma "P | fimplies P Q" "~ Q | fimplies P Q" "~ fimplies P Q | ~ P | Q"
  1727         by (unfold fimplies_def) fast+}
  1680         by (unfold fimplies_def) fast+}
  1728     |> map (pair General)),
  1681     |> map (pair General)),
  1733     @{thms fequal_def [THEN Meson.iff_to_disjD, THEN conjunct1]
  1686     @{thms fequal_def [THEN Meson.iff_to_disjD, THEN conjunct1]
  1734            fequal_def [THEN Meson.iff_to_disjD, THEN conjunct2]}
  1687            fequal_def [THEN Meson.iff_to_disjD, THEN conjunct2]}
  1735     |> map (pair General)),
  1688     |> map (pair General)),
  1736    (* Partial characterization of "fAll" and "fEx". A complete characterization
  1689    (* Partial characterization of "fAll" and "fEx". A complete characterization
  1737       would require the axiom of choice for replay with Metis. *)
  1690       would require the axiom of choice for replay with Metis. *)
  1738    (("fAll", false),
  1691    (("fAll", false), [(General, @{lemma "~ fAll P | P x" by (auto simp: fAll_def)})]),
  1739     [(General, @{lemma "~ fAll P | P x" by (auto simp: fAll_def)})]),
  1692    (("fEx", false), [(General, @{lemma "~ P x | fEx P" by (auto simp: fEx_def)})])]
  1740    (("fEx", false),
       
  1741     [(General, @{lemma "~ P x | fEx P" by (auto simp: fEx_def)})])]
       
  1742   |> map (apsnd (map (apsnd zero_var_indexes)))
  1693   |> map (apsnd (map (apsnd zero_var_indexes)))
  1743 
  1694 
  1744 val completish_helper_table =
  1695 val completish_helper_table =
  1745   helper_table @
  1696   helper_table @
  1746   [((predicator_name, true),
  1697   [((predicator_name, true),
  1747     @{thms True_or_False fTrue_ne_fFalse} |> map (pair General)),
  1698     @{thms True_or_False fTrue_ne_fFalse} |> map (pair General)),
  1748    ((app_op_name, true),
  1699    ((app_op_name, true),
  1749     [(General, @{lemma "EX x. ~ f x = g x | f = g" by blast}),
  1700     [(General, @{lemma "EX x. ~ f x = g x | f = g" by blast}),
  1750      (General, @{lemma "EX p. (p x <-> p y) --> x = y" by blast})]),
  1701      (General, @{lemma "EX p. (p x <-> p y) --> x = y" by blast})]),
  1751    (("fconj", false),
  1702    (("fconj", false), @{thms fconj_table fconj_laws fdisj_laws} |> map (pair Non_Rec_Def)),
  1752     @{thms fconj_table fconj_laws fdisj_laws} |> map (pair Non_Rec_Def)),
  1703    (("fdisj", false), @{thms fdisj_table fconj_laws fdisj_laws} |> map (pair Non_Rec_Def)),
  1753    (("fdisj", false),
       
  1754     @{thms fdisj_table fconj_laws fdisj_laws} |> map (pair Non_Rec_Def)),
       
  1755    (("fimplies", false),
  1704    (("fimplies", false),
  1756     @{thms fimplies_table fconj_laws fdisj_laws fimplies_laws}
  1705     @{thms fimplies_table fconj_laws fdisj_laws fimplies_laws}
  1757     |> map (pair Non_Rec_Def)),
  1706     |> map (pair Non_Rec_Def)),
  1758    (("fequal", false),
  1707    (("fequal", false),
  1759     (@{thms fequal_table} |> map (pair Non_Rec_Def)) @
  1708     (@{thms fequal_table} |> map (pair Non_Rec_Def)) @
  1760     (@{thms fequal_laws} |> map (pair General))),
  1709     (@{thms fequal_laws} |> map (pair General))),
  1761    (("fAll", false),
  1710    (("fAll", false), @{thms fAll_table fComp_law fAll_law fEx_law} |> map (pair Non_Rec_Def)),
  1762     @{thms fAll_table fComp_law fAll_law fEx_law} |> map (pair Non_Rec_Def)),
  1711    (("fEx", false), @{thms fEx_table fComp_law fAll_law fEx_law} |> map (pair Non_Rec_Def))]
  1763    (("fEx", false),
       
  1764     @{thms fEx_table fComp_law fAll_law fEx_law} |> map (pair Non_Rec_Def))]
       
  1765   |> map (apsnd (map (apsnd zero_var_indexes)))
  1712   |> map (apsnd (map (apsnd zero_var_indexes)))
  1766 
  1713 
  1767 fun bound_tvars type_enc sorts Ts =
  1714 fun bound_tvars type_enc sorts Ts =
  1768   (case filter is_TVar Ts of
  1715   (case filter is_TVar Ts of
  1769     [] => I
  1716     [] => I
  1794 fun rank_of_fact_num n j = min_rank + (max_rank - min_rank) * j div n
  1741 fun rank_of_fact_num n j = min_rank + (max_rank - min_rank) * j div n
  1795 
  1742 
  1796 val type_tag = `(make_fixed_const NONE) type_tag_name
  1743 val type_tag = `(make_fixed_const NONE) type_tag_name
  1797 
  1744 
  1798 fun could_specialize_helpers type_enc =
  1745 fun could_specialize_helpers type_enc =
  1799   not (is_type_enc_polymorphic type_enc) andalso
  1746   not (is_type_enc_polymorphic type_enc) andalso level_of_type_enc type_enc <> No_Types
  1800   level_of_type_enc type_enc <> No_Types
       
  1801 
  1747 
  1802 fun should_specialize_helper type_enc t =
  1748 fun should_specialize_helper type_enc t =
  1803   could_specialize_helpers type_enc andalso
  1749   could_specialize_helpers type_enc andalso not (null (Term.hidden_polymorphism t))
  1804   not (null (Term.hidden_polymorphism t))
       
  1805 
  1750 
  1806 fun add_helper_facts_of_sym ctxt format type_enc completish
  1751 fun add_helper_facts_of_sym ctxt format type_enc completish
  1807                             (s, {types, ...} : sym_info) =
  1752                             (s, {types, ...} : sym_info) =
  1808   case unprefix_and_unascii const_prefix s of
  1753   (case unprefix_and_unascii const_prefix s of
  1809     SOME mangled_s =>
  1754     SOME mangled_s =>
  1810     let
  1755     let
  1811       val thy = Proof_Context.theory_of ctxt
  1756       val thy = Proof_Context.theory_of ctxt
  1812       val unmangled_s = mangled_s |> unmangled_const_name |> hd
  1757       val unmangled_s = mangled_s |> unmangled_const_name |> hd
  1813       fun dub needs_sound j k =
  1758       fun dub needs_sound j k =
  1814         ascii_of unmangled_s ^ "_" ^ string_of_int j ^ "_" ^ string_of_int k ^
  1759         ascii_of unmangled_s ^ "_" ^ string_of_int j ^ "_" ^ string_of_int k ^
  1815         (if mangled_s = unmangled_s then "" else "_" ^ ascii_of mangled_s) ^
  1760         (if mangled_s = unmangled_s then "" else "_" ^ ascii_of mangled_s) ^
  1816         (if needs_sound then typed_helper_suffix else untyped_helper_suffix)
  1761         (if needs_sound then typed_helper_suffix else untyped_helper_suffix)
  1817       fun specialize_helper t T =
  1762       fun specialize_helper t T =
  1818         if unmangled_s = app_op_name then
  1763         if unmangled_s = app_op_name then
  1819           let
  1764           let val tyenv = Sign.typ_match thy (alpha_to_beta, domain_type T) Vartab.empty in
  1820             val tyenv =
  1765             monomorphic_term tyenv t
  1821               Sign.typ_match thy (alpha_to_beta, domain_type T) Vartab.empty
  1766           end
  1822           in monomorphic_term tyenv t end
       
  1823         else
  1767         else
  1824           specialize_type thy (invert_const unmangled_s, T) t
  1768           specialize_type thy (invert_const unmangled_s, T) t
  1825       fun dub_and_inst needs_sound ((status, t), j) =
  1769       fun dub_and_inst needs_sound ((status, t), j) =
  1826         (if should_specialize_helper type_enc t then
  1770         (if should_specialize_helper type_enc t then
  1827            map_filter (try (specialize_helper t)) types
  1771            map_filter (try (specialize_helper t)) types
  1843                  |> maps (dub_and_inst needs_sound o apfst (apsnd prop_of))
  1787                  |> maps (dub_and_inst needs_sound o apfst (apsnd prop_of))
  1844                  |> make_facts
  1788                  |> make_facts
  1845                  |> union (op = o pairself #iformula))
  1789                  |> union (op = o pairself #iformula))
  1846            (if completish then completish_helper_table else helper_table)
  1790            (if completish then completish_helper_table else helper_table)
  1847     end
  1791     end
  1848   | NONE => I
  1792   | NONE => I)
  1849 fun helper_facts_of_sym_table ctxt format type_enc completish sym_tab =
  1793 fun helper_facts_of_sym_table ctxt format type_enc completish sym_tab =
  1850   Symtab.fold_rev (add_helper_facts_of_sym ctxt format type_enc completish)
  1794   Symtab.fold_rev (add_helper_facts_of_sym ctxt format type_enc completish)
  1851                   sym_tab []
  1795                   sym_tab []
  1852 
  1796 
  1853 (***************************************************************)
  1797 (***************************************************************)
  1906     lift_lams_part_1 ctxt type_enc
  1850     lift_lams_part_1 ctxt type_enc
  1907     ##> maps (fn t => [t, introduce_combinators ctxt (intentionalize_def t)])
  1851     ##> maps (fn t => [t, introduce_combinators ctxt (intentionalize_def t)])
  1908     #> lift_lams_part_2 ctxt
  1852     #> lift_lams_part_2 ctxt
  1909   else if lam_trans = combs_or_liftingN then
  1853   else if lam_trans = combs_or_liftingN then
  1910     lift_lams_part_1 ctxt type_enc
  1854     lift_lams_part_1 ctxt type_enc
  1911     ##> map (fn t => case head_of (strip_qnt_body @{const_name All} t) of
  1855     ##> map (fn t => (case head_of (strip_qnt_body @{const_name All} t) of
  1912                        @{term "op =::bool => bool => bool"} => t
  1856                        @{term "op =::bool => bool => bool"} => t
  1913                      | _ => introduce_combinators ctxt (intentionalize_def t))
  1857                      | _ => introduce_combinators ctxt (intentionalize_def t)))
  1914     #> lift_lams_part_2 ctxt
  1858     #> lift_lams_part_2 ctxt
  1915   else if lam_trans = keep_lamsN then
  1859   else if lam_trans = keep_lamsN then
  1916     map (Envir.eta_contract) #> rpair []
  1860     map (Envir.eta_contract) #> rpair []
  1917   else
  1861   else
  1918     error ("Unknown lambda translation scheme: " ^ quote lam_trans ^ ".")
  1862     error ("Unknown lambda translation scheme: " ^ quote lam_trans ^ ".")
  1922     fun add_consts (IApp (t, u)) = fold add_consts [t, u]
  1866     fun add_consts (IApp (t, u)) = fold add_consts [t, u]
  1923       | add_consts (IAbs (_, t)) = add_consts t
  1867       | add_consts (IAbs (_, t)) = add_consts t
  1924       | add_consts (IConst (name, _, _)) = insert (op =) name
  1868       | add_consts (IConst (name, _, _)) = insert (op =) name
  1925       | add_consts (IVar _) = I
  1869       | add_consts (IVar _) = I
  1926     fun consts_of_hs l_or_r ({iformula, ...} : ifact) =
  1870     fun consts_of_hs l_or_r ({iformula, ...} : ifact) =
  1927       case iformula of
  1871       (case iformula of
  1928         AAtom (IApp (IApp (IConst _, t), u)) => add_consts (l_or_r (t, u)) []
  1872         AAtom (IApp (IApp (IConst _, t), u)) => add_consts (l_or_r (t, u)) []
  1929       | _ => []
  1873       | _ => [])
  1930     (* Quadratic, but usually OK. *)
  1874     (* Quadratic, but usually OK. *)
  1931     fun reorder [] [] = []
  1875     fun reorder [] [] = []
  1932       | reorder (fact :: skipped) [] =
  1876       | reorder (fact :: skipped) [] =
  1933         fact :: reorder [] skipped (* break cycle *)
  1877         fact :: reorder [] skipped (* break cycle *)
  1934       | reorder skipped (fact :: facts) =
  1878       | reorder skipped (fact :: facts) =
  1949                        concl_t facts =
  1893                        concl_t facts =
  1950   let
  1894   let
  1951     val thy = Proof_Context.theory_of ctxt
  1895     val thy = Proof_Context.theory_of ctxt
  1952     val trans_lams = trans_lams_of_string ctxt type_enc lam_trans
  1896     val trans_lams = trans_lams_of_string ctxt type_enc lam_trans
  1953     val fact_ts = facts |> map snd
  1897     val fact_ts = facts |> map snd
  1954     (* Remove existing facts from the conjecture, as this can dramatically
  1898     (* Remove existing facts from the conjecture, as this can dramatically boost an ATP's
  1955        boost an ATP's performance (for some reason). *)
  1899        performance (for some reason). *)
  1956     val hyp_ts =
  1900     val hyp_ts = hyp_ts |> map (fn t => if member (op aconv) fact_ts t then @{prop True} else t)
  1957       hyp_ts
       
  1958       |> map (fn t => if member (op aconv) fact_ts t then @{prop True} else t)
       
  1959     val facts = facts |> map (apsnd (pair Axiom))
  1901     val facts = facts |> map (apsnd (pair Axiom))
  1960     val conjs =
  1902     val conjs =
  1961       map (pair prem_role) hyp_ts @ [(Conjecture, s_not_prop concl_t)]
  1903       map (pair prem_role) hyp_ts @ [(Conjecture, s_not_prop concl_t)]
  1962       |> map (apsnd freeze_term)
  1904       |> map (apsnd freeze_term)
  1963       |> map2 (pair o rpair (Local, General) o string_of_int)
  1905       |> map2 (pair o rpair (Local, General) o string_of_int) (0 upto length hyp_ts)
  1964               (0 upto length hyp_ts)
       
  1965     val ((conjs, facts), lam_facts) =
  1906     val ((conjs, facts), lam_facts) =
  1966       (conjs, facts)
  1907       (conjs, facts)
  1967       |> presimp ? pairself (map (apsnd (apsnd (presimp_prop ctxt type_enc))))
  1908       |> presimp ? pairself (map (apsnd (apsnd (presimp_prop ctxt type_enc))))
  1968       |> (if lam_trans = no_lamsN then
  1909       |> (if lam_trans = no_lamsN then
  1969             rpair []
  1910             rpair []
  1976             |> pull_and_reorder_definitions
  1917             |> pull_and_reorder_definitions
  1977     val facts =
  1918     val facts =
  1978       facts |> map_filter (fn (name, (_, t)) =>
  1919       facts |> map_filter (fn (name, (_, t)) =>
  1979                               make_fact ctxt format type_enc true (name, t))
  1920                               make_fact ctxt format type_enc true (name, t))
  1980             |> pull_and_reorder_definitions
  1921             |> pull_and_reorder_definitions
  1981     val fact_names =
  1922     val fact_names = facts |> map (fn {name, stature, ...} : ifact => (name, stature))
  1982       facts |> map (fn {name, stature, ...} : ifact => (name, stature))
       
  1983     val lifted = lam_facts |> map (extract_lambda_def o snd o snd)
  1923     val lifted = lam_facts |> map (extract_lambda_def o snd o snd)
  1984     val lam_facts =
  1924     val lam_facts = lam_facts |> map_filter (make_fact ctxt format type_enc true o apsnd snd)
  1985       lam_facts |> map_filter (make_fact ctxt format type_enc true o apsnd snd)
       
  1986     val all_ts = concl_t :: hyp_ts @ fact_ts
  1925     val all_ts = concl_t :: hyp_ts @ fact_ts
  1987     val subs = tfree_classes_of_terms all_ts
  1926     val subs = tfree_classes_of_terms all_ts
  1988     val supers = tvar_classes_of_terms all_ts
  1927     val supers = tvar_classes_of_terms all_ts
  1989     val tycons = type_ctrs_of_terms thy all_ts
  1928     val tycons = type_ctrs_of_terms thy all_ts
  1990     val (supers, tcon_clauses) =
  1929     val (supers, tcon_clauses) =
  2002   IApp (IConst (type_guard, T --> @{typ bool}, [T])
  1941   IApp (IConst (type_guard, T --> @{typ bool}, [T])
  2003         |> mangle_type_args_in_iterm type_enc, tm)
  1942         |> mangle_type_args_in_iterm type_enc, tm)
  2004 
  1943 
  2005 fun is_var_positively_naked_in_term _ (SOME false) _ accum = accum
  1944 fun is_var_positively_naked_in_term _ (SOME false) _ accum = accum
  2006   | is_var_positively_naked_in_term name _ (ATerm (((s, _), _), tms)) accum =
  1945   | is_var_positively_naked_in_term name _ (ATerm (((s, _), _), tms)) accum =
  2007     accum orelse
  1946     accum orelse (is_tptp_equal s andalso member (op =) tms (ATerm ((name, []), [])))
  2008     (is_tptp_equal s andalso member (op =) tms (ATerm ((name, []), [])))
       
  2009   | is_var_positively_naked_in_term _ _ _ _ = true
  1947   | is_var_positively_naked_in_term _ _ _ _ = true
  2010 
  1948 
  2011 fun is_var_undercover_in_term thy name pos tm accum =
  1949 fun is_var_undercover_in_term thy name pos tm accum =
  2012   accum orelse
  1950   accum orelse
  2013   let
  1951   let
  2025       | is_undercover _ = true
  1963       | is_undercover _ = true
  2026   in is_undercover tm end
  1964   in is_undercover tm end
  2027 
  1965 
  2028 fun should_guard_var_in_formula thy level pos phi (SOME true) name =
  1966 fun should_guard_var_in_formula thy level pos phi (SOME true) name =
  2029     (case level of
  1967     (case level of
  2030        All_Types => true
  1968       All_Types => true
  2031      | Undercover_Types =>
  1969     | Undercover_Types => formula_fold pos (is_var_undercover_in_term thy name) phi false
  2032        formula_fold pos (is_var_undercover_in_term thy name) phi false
  1970     | Nonmono_Types (_, Uniform) => true
  2033      | Nonmono_Types (_, Uniform) => true
  1971     | Nonmono_Types (_, Non_Uniform) =>
  2034      | Nonmono_Types (_, Non_Uniform) =>
  1972       formula_fold pos (is_var_positively_naked_in_term name) phi false
  2035        formula_fold pos (is_var_positively_naked_in_term name) phi false
  1973     | _ => false)
  2036      | _ => false)
       
  2037   | should_guard_var_in_formula _ _ _ _ _ _ = true
  1974   | should_guard_var_in_formula _ _ _ _ _ _ = true
  2038 
  1975 
  2039 fun always_guard_var_in_formula _ _ _ _ _ _ = true
  1976 fun always_guard_var_in_formula _ _ _ _ _ _ = true
  2040 
  1977 
  2041 fun should_generate_tag_bound_decl _ _ _ (SOME true) _ = false
  1978 fun should_generate_tag_bound_decl _ _ _ (SOME true) _ = false
  2064 and atp_term_of_iterm ctxt mono type_enc pos =
  2001 and atp_term_of_iterm ctxt mono type_enc pos =
  2065   let
  2002   let
  2066     fun term site u =
  2003     fun term site u =
  2067       let
  2004       let
  2068         val (head, args) = strip_iterm_comb u
  2005         val (head, args) = strip_iterm_comb u
  2069         val pos =
  2006         val pos = (case site of Top_Level pos => pos | Eq_Arg pos => pos | _ => NONE)
  2070           case site of
       
  2071             Top_Level pos => pos
       
  2072           | Eq_Arg pos => pos
       
  2073           | _ => NONE
       
  2074         val T = ityp_of u
  2007         val T = ityp_of u
  2075         val t =
  2008         val t =
  2076           case head of
  2009           (case head of
  2077             IConst (name as (s, _), _, T_args) =>
  2010             IConst (name as (s, _), _, T_args) =>
  2078             let
  2011             let
  2079               val ary = length args
  2012               val ary = length args
  2080               fun arg_site j =
  2013               fun arg_site j = if is_tptp_equal s then Eq_Arg pos else Arg (s, j, ary)
  2081                 if is_tptp_equal s then Eq_Arg pos else Arg (s, j, ary)
       
  2082             in
  2014             in
  2083               map2 (fn j => term (arg_site j)) (0 upto ary - 1) args
  2015               map2 (fn j => term (arg_site j)) (0 upto ary - 1) args
  2084               |> mk_aterm type_enc name T_args
  2016               |> mk_aterm type_enc name T_args
  2085             end
  2017             end
  2086           | IVar (name, _) =>
  2018           | IVar (name, _) =>
  2089             if is_type_enc_higher_order type_enc then
  2021             if is_type_enc_higher_order type_enc then
  2090               AAbs (((name, native_atp_type_of_typ type_enc true 0 T), (* FIXME? why "true"? *)
  2022               AAbs (((name, native_atp_type_of_typ type_enc true 0 T), (* FIXME? why "true"? *)
  2091                      term Elsewhere tm), map (term Elsewhere) args)
  2023                      term Elsewhere tm), map (term Elsewhere) args)
  2092             else
  2024             else
  2093               raise Fail "unexpected lambda-abstraction"
  2025               raise Fail "unexpected lambda-abstraction"
  2094           | IApp _ => raise Fail "impossible \"IApp\""
  2026           | IApp _ => raise Fail "impossible \"IApp\"")
  2095         val tag = should_tag_with_type ctxt mono type_enc site u T
  2027         val tag = should_tag_with_type ctxt mono type_enc site u T
  2096       in t |> tag ? tag_with_type ctxt mono type_enc pos T end
  2028       in t |> tag ? tag_with_type ctxt mono type_enc pos T end
  2097   in term (Top_Level pos) end
  2029   in term (Top_Level pos) end
  2098 and formula_of_iformula ctxt mono type_enc should_guard_var =
  2030 and formula_of_iformula ctxt mono type_enc should_guard_var =
  2099   let
  2031   let
  2225                    else
  2157                    else
  2226                      bool_atype))
  2158                      bool_atype))
  2227   end
  2159   end
  2228 
  2160 
  2229 fun decl_lines_of_classes type_enc =
  2161 fun decl_lines_of_classes type_enc =
  2230   case type_enc of
  2162   (case type_enc of
  2231     Native (_, Raw_Polymorphic phantoms, _) =>
  2163     Native (_, Raw_Polymorphic phantoms, _) => map (decl_line_of_class phantoms)
  2232     map (decl_line_of_class phantoms)
  2164   | _ => K [])
  2233   | _ => K []
       
  2234 
  2165 
  2235 fun sym_decl_table_of_facts thy type_enc sym_tab (conjs, facts, extra_tms) =
  2166 fun sym_decl_table_of_facts thy type_enc sym_tab (conjs, facts, extra_tms) =
  2236   let
  2167   let
  2237     fun add_iterm_syms tm =
  2168     fun add_iterm_syms tm =
  2238       let val (head, args) = strip_iterm_comb tm in
  2169       let val (head, args) = strip_iterm_comb tm in
  2239         (case head of
  2170         (case head of
  2240            IConst ((s, s'), T, T_args) =>
  2171           IConst ((s, s'), T, T_args) =>
  2241            let
  2172           let
  2242              val (pred_sym, in_conj) =
  2173             val (pred_sym, in_conj) =
  2243                case Symtab.lookup sym_tab s of
  2174               (case Symtab.lookup sym_tab s of
  2244                  SOME ({pred_sym, in_conj, ...} : sym_info) =>
  2175                 SOME ({pred_sym, in_conj, ...} : sym_info) => (pred_sym, in_conj)
  2245                  (pred_sym, in_conj)
  2176               | NONE => (false, false))
  2246                | NONE => (false, false)
  2177             val decl_sym =
  2247              val decl_sym =
  2178               (case type_enc of Guards _ => not pred_sym | _ => true) andalso is_tptp_user_symbol s
  2248                (case type_enc of
  2179           in
  2249                   Guards _ => not pred_sym
  2180             if decl_sym then
  2250                 | _ => true) andalso
  2181               Symtab.map_default (s, [])
  2251                is_tptp_user_symbol s
  2182                 (insert_type thy #3 (s', T_args, T, pred_sym, length args, in_conj))
  2252            in
  2183             else
  2253              if decl_sym then
  2184               I
  2254                Symtab.map_default (s, [])
  2185           end
  2255                    (insert_type thy #3 (s', T_args, T, pred_sym, length args,
  2186         | IAbs (_, tm) => add_iterm_syms tm
  2256                                         in_conj))
  2187         | _ => I)
  2257              else
       
  2258                I
       
  2259            end
       
  2260          | IAbs (_, tm) => add_iterm_syms tm
       
  2261          | _ => I)
       
  2262         #> fold add_iterm_syms args
  2188         #> fold add_iterm_syms args
  2263       end
  2189       end
  2264     val add_fact_syms = ifact_lift (formula_fold NONE (K add_iterm_syms))
  2190     val add_fact_syms = ifact_lift (formula_fold NONE (K add_iterm_syms))
  2265     fun add_formula_var_types (ATyQuant (_, _, phi)) = add_formula_var_types phi
  2191     fun add_formula_var_types (ATyQuant (_, _, phi)) = add_formula_var_types phi
  2266       | add_formula_var_types (AQuant (_, xs, phi)) =
  2192       | add_formula_var_types (AQuant (_, xs, phi)) =
  2301   end
  2227   end
  2302 
  2228 
  2303 (* We add "bool" in case the helper "True_or_False" is included later. *)
  2229 (* We add "bool" in case the helper "True_or_False" is included later. *)
  2304 fun default_mono level completish =
  2230 fun default_mono level completish =
  2305   {maybe_finite_Ts = [@{typ bool}],
  2231   {maybe_finite_Ts = [@{typ bool}],
  2306    surely_infinite_Ts =
  2232    surely_infinite_Ts = (case level of Nonmono_Types (Strict, _) => [] | _ => known_infinite_types),
  2307      case level of
       
  2308        Nonmono_Types (Strict, _) => []
       
  2309      | _ => known_infinite_types,
       
  2310    maybe_nonmono_Ts = [if completish then tvar_a else @{typ bool}]}
  2233    maybe_nonmono_Ts = [if completish then tvar_a else @{typ bool}]}
  2311 
  2234 
  2312 (* This inference is described in section 4 of Blanchette et al., "Encoding
  2235 (* This inference is described in section 4 of Blanchette et al., "Encoding
  2313    monomorphic and polymorphic types", TACAS 2013. *)
  2236    monomorphic and polymorphic types", TACAS 2013. *)
  2314 fun add_iterm_mononotonicity_info _ _ (SOME false) _ mono = mono
  2237 fun add_iterm_mononotonicity_info _ _ (SOME false) _ mono = mono
  2315   | add_iterm_mononotonicity_info ctxt level _
  2238   | add_iterm_mononotonicity_info ctxt level _
  2316         (IApp (IApp (IConst ((s, _), Type (_, [T, _]), _), tm1), tm2))
  2239         (IApp (IApp (IConst ((s, _), Type (_, [T, _]), _), tm1), tm2))
  2317         (mono as {maybe_finite_Ts, surely_infinite_Ts, maybe_nonmono_Ts}) =
  2240         (mono as {maybe_finite_Ts, surely_infinite_Ts, maybe_nonmono_Ts}) =
  2318     let val thy = Proof_Context.theory_of ctxt in
  2241     let val thy = Proof_Context.theory_of ctxt in
  2319       if is_tptp_equal s andalso exists is_maybe_universal_var [tm1, tm2] then
  2242       if is_tptp_equal s andalso exists is_maybe_universal_var [tm1, tm2] then
  2320         case level of
  2243         (case level of
  2321           Nonmono_Types (strictness, _) =>
  2244           Nonmono_Types (strictness, _) =>
  2322           if exists (type_instance thy T) surely_infinite_Ts orelse
  2245           if exists (type_instance thy T) surely_infinite_Ts orelse
  2323              member (type_equiv thy) maybe_finite_Ts T then
  2246              member (type_equiv thy) maybe_finite_Ts T then
  2324             mono
  2247             mono
  2325           else if is_type_kind_of_surely_infinite ctxt strictness
  2248           else if is_type_kind_of_surely_infinite ctxt strictness
  2329              maybe_nonmono_Ts = maybe_nonmono_Ts}
  2252              maybe_nonmono_Ts = maybe_nonmono_Ts}
  2330           else
  2253           else
  2331             {maybe_finite_Ts = maybe_finite_Ts |> insert (type_equiv thy) T,
  2254             {maybe_finite_Ts = maybe_finite_Ts |> insert (type_equiv thy) T,
  2332              surely_infinite_Ts = surely_infinite_Ts,
  2255              surely_infinite_Ts = surely_infinite_Ts,
  2333              maybe_nonmono_Ts = maybe_nonmono_Ts |> insert_type thy I T}
  2256              maybe_nonmono_Ts = maybe_nonmono_Ts |> insert_type thy I T}
  2334         | _ => mono
  2257         | _ => mono)
  2335       else
  2258       else
  2336         mono
  2259         mono
  2337     end
  2260     end
  2338   | add_iterm_mononotonicity_info _ _ _ _ mono = mono
  2261   | add_iterm_mononotonicity_info _ _ _ _ mono = mono
  2339 fun add_fact_mononotonicity_info ctxt level ({role, iformula, ...} : ifact) =
  2262 fun add_fact_mononotonicity_info ctxt level ({role, iformula, ...} : ifact) =
  2388                   (tag_with_type ctxt mono type_enc NONE T x_var) x_var,
  2311                   (tag_with_type ctxt mono type_enc NONE T x_var) x_var,
  2389              NONE, isabelle_info non_rec_defN helper_rank)
  2312              NONE, isabelle_info non_rec_defN helper_rank)
  2390   end
  2313   end
  2391 
  2314 
  2392 fun lines_of_mono_types ctxt mono type_enc =
  2315 fun lines_of_mono_types ctxt mono type_enc =
  2393   case type_enc of
  2316   (case type_enc of
  2394     Native _ => K []
  2317     Native _ => K []
  2395   | Guards _ => map (line_of_guards_mono_type ctxt mono type_enc)
  2318   | Guards _ => map (line_of_guards_mono_type ctxt mono type_enc)
  2396   | Tags _ => map (line_of_tags_mono_type ctxt mono type_enc)
  2319   | Tags _ => map (line_of_tags_mono_type ctxt mono type_enc))
  2397 
  2320 
  2398 fun decl_line_of_sym ctxt mono type_enc s (s', T_args, T, pred_sym, ary, _) =
  2321 fun decl_line_of_sym ctxt mono type_enc s (s', T_args, T, pred_sym, ary, _) =
  2399   let
  2322   let
  2400     val thy = Proof_Context.theory_of ctxt
  2323     val thy = Proof_Context.theory_of ctxt
  2401     val (T, T_args) =
  2324     val (T, T_args) =
  2402       if null T_args then
  2325       if null T_args then
  2403         (T, [])
  2326         (T, [])
  2404       else case unprefix_and_unascii const_prefix s of
  2327       else
  2405         SOME s' =>
  2328         (case unprefix_and_unascii const_prefix s of
  2406         let
  2329           SOME s' =>
  2407           val s' = s' |> unmangled_invert_const
  2330           let
  2408           val T = s' |> robust_const_type thy
  2331             val s' = s' |> unmangled_invert_const
  2409         in (T, robust_const_type_args thy (s', T)) end
  2332             val T = s' |> robust_const_type thy
  2410       | NONE => raise Fail "unexpected type arguments"
  2333           in (T, robust_const_type_args thy (s', T)) end
       
  2334         | NONE => raise Fail "unexpected type arguments")
  2411   in
  2335   in
  2412     Sym_Decl (sym_decl_prefix ^ s, (s, s'),
  2336     Sym_Decl (sym_decl_prefix ^ s, (s, s'),
  2413               T |> fused_type ctxt mono (level_of_type_enc type_enc) ary
  2337               T |> fused_type ctxt mono (level_of_type_enc type_enc) ary
  2414                 |> native_atp_type_of_typ type_enc pred_sym ary
  2338                 |> native_atp_type_of_typ type_enc pred_sym ary
  2415                 |> not (null T_args)
  2339                 |> not (null T_args)
  2426     val (arg_Ts, res_T) = chop_fun ary T
  2350     val (arg_Ts, res_T) = chop_fun ary T
  2427     val bound_names = 1 upto ary |> map (`I o make_bound_var o string_of_int)
  2351     val bound_names = 1 upto ary |> map (`I o make_bound_var o string_of_int)
  2428     val bounds =
  2352     val bounds =
  2429       bound_names ~~ arg_Ts |> map (fn (name, T) => IConst (name, T, []))
  2353       bound_names ~~ arg_Ts |> map (fn (name, T) => IConst (name, T, []))
  2430     val bound_Ts =
  2354     val bound_Ts =
  2431       case level_of_type_enc type_enc of
  2355       (case level_of_type_enc type_enc of
  2432         All_Types => if null T_args then replicate ary NONE else map SOME arg_Ts
  2356         All_Types => if null T_args then replicate ary NONE else map SOME arg_Ts
  2433       | Undercover_Types =>
  2357       | Undercover_Types =>
  2434         let val cover = type_arg_cover thy NONE s ary in
  2358         let val cover = type_arg_cover thy NONE s ary in
  2435           map2 (fn j => if member (op =) cover j then SOME else K NONE)
  2359           map2 (fn j => if member (op =) cover j then SOME else K NONE) (0 upto ary - 1) arg_Ts
  2436                (0 upto ary - 1) arg_Ts
       
  2437         end
  2360         end
  2438       | _ => replicate ary NONE
  2361       | _ => replicate ary NONE)
  2439   in
  2362   in
  2440     Formula ((guards_sym_formula_prefix ^ s ^
  2363     Formula ((guards_sym_formula_prefix ^ s ^
  2441               (if n > 1 then "_" ^ string_of_int j else ""), ""),
  2364               (if n > 1 then "_" ^ string_of_int j else ""), ""),
  2442              role,
  2365              role,
  2443              IConst ((s, s'), T, T_args)
  2366              IConst ((s, s'), T, T_args)
  2485 
  2408 
  2486 fun result_type_of_decl (_, _, T, _, ary, _) = chop_fun ary T |> snd
  2409 fun result_type_of_decl (_, _, T, _, ary, _) = chop_fun ary T |> snd
  2487 
  2410 
  2488 fun rationalize_decls thy (decls as decl :: (decls' as _ :: _)) =
  2411 fun rationalize_decls thy (decls as decl :: (decls' as _ :: _)) =
  2489     let
  2412     let
  2490       val T = result_type_of_decl decl
  2413       val T = result_type_of_decl decl |> map_type_tvar (fn (z, _) => TVar (z, HOLogic.typeS))
  2491               |> map_type_tvar (fn (z, _) => TVar (z, HOLogic.typeS))
       
  2492     in
  2414     in
  2493       if forall (type_generalization thy T o result_type_of_decl) decls' then
  2415       if forall (type_generalization thy T o result_type_of_decl) decls' then [decl]
  2494         [decl]
  2416       else decls
  2495       else
       
  2496         decls
       
  2497     end
  2417     end
  2498   | rationalize_decls _ decls = decls
  2418   | rationalize_decls _ decls = decls
  2499 
  2419 
  2500 fun lines_of_sym_decls ctxt mono type_enc (s, decls) =
  2420 fun lines_of_sym_decls ctxt mono type_enc (s, decls) =
  2501   case type_enc of
  2421   (case type_enc of
  2502     Native _ => [decl_line_of_sym ctxt mono type_enc s (hd decls)]
  2422     Native _ => [decl_line_of_sym ctxt mono type_enc s (hd decls)]
  2503   | Guards (_, level) =>
  2423   | Guards (_, level) =>
  2504     let
  2424     let
  2505       val thy = Proof_Context.theory_of ctxt
  2425       val thy = Proof_Context.theory_of ctxt
  2506       val decls = decls |> rationalize_decls thy
  2426       val decls = decls |> rationalize_decls thy
  2507       val n = length decls
  2427       val n = length decls
  2508       val decls =
  2428       val decls = decls |> filter (should_encode_type ctxt mono level o result_type_of_decl)
  2509         decls |> filter (should_encode_type ctxt mono level
       
  2510                          o result_type_of_decl)
       
  2511     in
  2429     in
  2512       (0 upto length decls - 1, decls)
  2430       (0 upto length decls - 1, decls) |-> map2 (line_of_guards_sym_decl ctxt mono type_enc n s)
  2513       |-> map2 (line_of_guards_sym_decl ctxt mono type_enc n s)
       
  2514     end
  2431     end
  2515   | Tags (_, level) =>
  2432   | Tags (_, level) =>
  2516     if is_type_level_uniform level then
  2433     if is_type_level_uniform level then
  2517       []
  2434       []
  2518     else
  2435     else
  2519       let val n = length decls in
  2436       let val n = length decls in
  2520         (0 upto n - 1 ~~ decls)
  2437         (0 upto n - 1 ~~ decls) |> maps (lines_of_tags_sym_decl ctxt mono type_enc n s)
  2521         |> maps (lines_of_tags_sym_decl ctxt mono type_enc n s)
  2438       end)
  2522       end
       
  2523 
  2439 
  2524 fun lines_of_sym_decl_table ctxt mono type_enc mono_Ts sym_decl_tab =
  2440 fun lines_of_sym_decl_table ctxt mono type_enc mono_Ts sym_decl_tab =
  2525   let
  2441   let
  2526     val syms = sym_decl_tab |> Symtab.dest |> sort_wrt fst
  2442     val syms = sym_decl_tab |> Symtab.dest |> sort_wrt fst
  2527     val mono_lines = lines_of_mono_types ctxt mono type_enc mono_Ts
  2443     val mono_lines = lines_of_mono_types ctxt mono type_enc mono_Ts
  2535         val thy = Proof_Context.theory_of ctxt
  2451         val thy = Proof_Context.theory_of ctxt
  2536         fun do_ctr (s, T) =
  2452         fun do_ctr (s, T) =
  2537           let
  2453           let
  2538             val s' = make_fixed_const (SOME type_enc) s
  2454             val s' = make_fixed_const (SOME type_enc) s
  2539             val ary = ary_of T
  2455             val ary = ary_of T
  2540             fun mk name =
  2456             fun mk name = mk_aterm type_enc name (robust_const_type_args thy (s, T)) []
  2541               mk_aterm type_enc name (robust_const_type_args thy (s, T)) []
       
  2542           in
  2457           in
  2543             case Symtab.lookup sym_tab s' of
  2458             (case Symtab.lookup sym_tab s' of
  2544               NONE => NONE
  2459               NONE => NONE
  2545             | SOME ({min_ary, ...} : sym_info) =>
  2460             | SOME ({min_ary, ...} : sym_info) =>
  2546               if ary = min_ary then
  2461               if ary = min_ary then SOME (mk (s', s))
  2547                 SOME (mk (s', s))
  2462               else if uncurried_aliases then SOME (mk (aliased_uncurried ary (s', s)))
  2548               else if uncurried_aliases then
  2463               else NONE)
  2549                 SOME (mk (aliased_uncurried ary (s', s)))
       
  2550               else
       
  2551                 NONE
       
  2552           end
  2464           end
  2553         fun datatype_of_ctrs (ctrs as (_, T1) :: _) =
  2465         fun datatype_of_ctrs (ctrs as (_, T1) :: _) =
  2554           let val ctrs' = map do_ctr ctrs in
  2466           let val ctrs' = map do_ctr ctrs in
  2555             if forall is_some ctrs' then
  2467             if forall is_some ctrs' then
  2556               SOME (native_atp_type_of_typ type_enc false 0 (body_type T1), map_filter I ctrs')
  2468               SOME (native_atp_type_of_typ type_enc false 0 (body_type T1), map_filter I ctrs')
  2577     fun do_alias ary =
  2489     fun do_alias ary =
  2578       let
  2490       let
  2579         val thy = Proof_Context.theory_of ctxt
  2491         val thy = Proof_Context.theory_of ctxt
  2580         val (role, maybe_negate) = honor_conj_sym_role in_conj
  2492         val (role, maybe_negate) = honor_conj_sym_role in_conj
  2581         val base_name = base_s0 |> `(make_fixed_const (SOME type_enc))
  2493         val base_name = base_s0 |> `(make_fixed_const (SOME type_enc))
  2582         val T = case types of [T] => T | _ => robust_const_type thy base_s0
  2494         val T = (case types of [T] => T | _ => robust_const_type thy base_s0)
  2583         val T_args = robust_const_type_args thy (base_s0, T)
  2495         val T_args = robust_const_type_args thy (base_s0, T)
  2584         val (base_name as (base_s, _), T_args) =
  2496         val (base_name as (base_s, _), T_args) =
  2585           mangle_type_args_in_const type_enc base_name T_args
  2497           mangle_type_args_in_const type_enc base_name T_args
  2586         val base_ary = min_ary_of sym_tab0 base_s
  2498         val base_ary = min_ary_of sym_tab0 base_s
  2587         fun do_const name = IConst (name, T, T_args)
  2499         fun do_const name = IConst (name, T, T_args)
  2602         val tm2 =
  2514         val tm2 =
  2603           list_app (do_const name2) (first_bounds @ [last_bound])
  2515           list_app (do_const name2) (first_bounds @ [last_bound])
  2604           |> filter_ty_args
  2516           |> filter_ty_args
  2605         val do_bound_type = do_bound_type ctxt mono type_enc
  2517         val do_bound_type = do_bound_type ctxt mono type_enc
  2606         val eq =
  2518         val eq =
  2607           eq_formula type_enc (atomic_types_of T)
  2519           eq_formula type_enc (atomic_types_of T) (map (apsnd do_bound_type) bounds) false
  2608                      (map (apsnd do_bound_type) bounds) false
  2520             (atp_term_of tm1) (atp_term_of tm2)
  2609                      (atp_term_of tm1) (atp_term_of tm2)
       
  2610       in
  2521       in
  2611         ([tm1, tm2],
  2522         ([tm1, tm2],
  2612          [Formula ((uncurried_alias_eq_prefix ^ s2, ""), role,
  2523          [Formula ((uncurried_alias_eq_prefix ^ s2, ""), role, eq |> maybe_negate, NONE,
  2613                    eq |> maybe_negate, NONE,
  2524             isabelle_info non_rec_defN helper_rank)])
  2614                    isabelle_info non_rec_defN helper_rank)])
       
  2615         |> (if ary - 1 = base_ary orelse Symtab.defined sym_tab s1 then I
  2525         |> (if ary - 1 = base_ary orelse Symtab.defined sym_tab s1 then I
  2616             else pair_append (do_alias (ary - 1)))
  2526             else pair_append (do_alias (ary - 1)))
  2617       end
  2527       end
  2618   in do_alias end
  2528   in do_alias end
  2619 fun uncurried_alias_lines_of_sym ctxt ctrss mono type_enc sym_tab0 sym_tab
  2529 fun uncurried_alias_lines_of_sym ctxt ctrss mono type_enc sym_tab0 sym_tab
  2620         (s, {min_ary, types, in_conj, ...} : sym_info) =
  2530         (s, {min_ary, types, in_conj, ...} : sym_info) =
  2621   case unprefix_and_unascii const_prefix s of
  2531   (case unprefix_and_unascii const_prefix s of
  2622     SOME mangled_s =>
  2532     SOME mangled_s =>
  2623     if String.isSubstring uncurried_alias_sep mangled_s then
  2533     if String.isSubstring uncurried_alias_sep mangled_s then
  2624       let
  2534       let val base_s0 = mangled_s |> unmangled_invert_const in
  2625         val base_s0 = mangled_s |> unmangled_invert_const
  2535         do_uncurried_alias_lines_of_sym ctxt ctrss mono type_enc sym_tab0 sym_tab base_s0 types
  2626       in
  2536           in_conj min_ary
  2627         do_uncurried_alias_lines_of_sym ctxt ctrss mono type_enc sym_tab0
       
  2628                                         sym_tab base_s0 types in_conj min_ary
       
  2629       end
  2537       end
  2630     else
  2538     else
  2631       ([], [])
  2539       ([], [])
  2632   | NONE => ([], [])
  2540   | NONE => ([], []))
  2633 fun uncurried_alias_lines_of_sym_table ctxt ctrss mono type_enc
  2541 fun uncurried_alias_lines_of_sym_table ctxt ctrss mono type_enc uncurried_aliases sym_tab0 sym_tab =
  2634                                        uncurried_aliases sym_tab0 sym_tab =
       
  2635   ([], [])
  2542   ([], [])
  2636   |> uncurried_aliases
  2543   |> uncurried_aliases
  2637      ? Symtab.fold_rev
  2544     ? Symtab.fold_rev
  2638            (pair_append
  2545         (pair_append o uncurried_alias_lines_of_sym ctxt ctrss mono type_enc sym_tab0 sym_tab)
  2639             o uncurried_alias_lines_of_sym ctxt ctrss mono type_enc sym_tab0
  2546         sym_tab
  2640                                            sym_tab) sym_tab
       
  2641 
  2547 
  2642 val implicit_declsN = "Could-be-implicit typings"
  2548 val implicit_declsN = "Could-be-implicit typings"
  2643 val explicit_declsN = "Explicit typings"
  2549 val explicit_declsN = "Explicit typings"
  2644 val uncurried_alias_eqsN = "Uncurried aliases"
  2550 val uncurried_alias_eqsN = "Uncurried aliases"
  2645 val factsN = "Relevant facts"
  2551 val factsN = "Relevant facts"
  2907       | add_term_deps head (AAbs ((_, tm), args)) =
  2813       | add_term_deps head (AAbs ((_, tm), args)) =
  2908         add_term_deps head tm #> fold (add_term_deps head) args
  2814         add_term_deps head tm #> fold (add_term_deps head) args
  2909     fun add_intro_deps pred (Formula (_, role, phi, _, info)) =
  2815     fun add_intro_deps pred (Formula (_, role, phi, _, info)) =
  2910         if pred (role, info) then
  2816         if pred (role, info) then
  2911           let val (hyps, concl) = strip_ahorn_etc phi in
  2817           let val (hyps, concl) = strip_ahorn_etc phi in
  2912             case make_head_roll concl of
  2818             (case make_head_roll concl of
  2913               (head, args as _ :: _) => fold (add_term_deps head) (hyps @ args)
  2819               (head, args as _ :: _) => fold (add_term_deps head) (hyps @ args)
  2914             | _ => I
  2820             | _ => I)
  2915           end
  2821           end
  2916         else
  2822         else
  2917           I
  2823           I
  2918       | add_intro_deps _ _ = I
  2824       | add_intro_deps _ _ = I
  2919     fun add_atom_eq_deps (SOME true) (ATerm ((s, _), [lhs as _, rhs])) =
  2825     fun add_atom_eq_deps (SOME true) (ATerm ((s, _), [lhs as _, rhs])) =
  2920         if is_tptp_equal s then
  2826         if is_tptp_equal s then
  2921           case make_head_roll lhs of
  2827           (case make_head_roll lhs of
  2922             (head, args as _ :: _) => fold (add_term_deps head) (rhs :: args)
  2828             (head, args as _ :: _) => fold (add_term_deps head) (rhs :: args)
  2923           | _ => I
  2829           | _ => I)
  2924         else
  2830         else
  2925           I
  2831           I
  2926       | add_atom_eq_deps _ _ = I
  2832       | add_atom_eq_deps _ _ = I
  2927     fun add_eq_deps pred (Formula (_, role, phi, _, info)) =
  2833     fun add_eq_deps pred (Formula (_, role, phi, _, info)) =
  2928         if pred (role, info) then
  2834         if pred (role, info) then
  2929           case strip_iff_etc phi of
  2835           (case strip_iff_etc phi of
  2930             ([lhs], rhs) =>
  2836             ([lhs], rhs) =>
  2931             (case make_head_roll lhs of
  2837             (case make_head_roll lhs of
  2932                (head, args as _ :: _) => fold (add_term_deps head) (rhs @ args)
  2838               (head, args as _ :: _) => fold (add_term_deps head) (rhs @ args)
  2933              | _ => I)
  2839             | _ => I)
  2934           | _ => formula_fold (SOME (role <> Conjecture)) add_atom_eq_deps phi
  2840           | _ => formula_fold (SOME (role <> Conjecture)) add_atom_eq_deps phi)
  2935         else
  2841         else
  2936           I
  2842           I
  2937       | add_eq_deps _ _ = I
  2843       | add_eq_deps _ _ = I
  2938     fun has_status status (_, info) = extract_isabelle_status info = SOME status
  2844     fun has_status status (_, info) = extract_isabelle_status info = SOME status
  2939     fun is_conj (role, _) = (role = Conjecture orelse role = Hypothesis)
  2845     fun is_conj (role, _) = (role = Conjecture orelse role = Hypothesis)