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 |
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 = |
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) |
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 | |
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 |
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 |
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 |
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 |
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) |
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 |
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) |