src/HOL/Tools/ATP/atp_translate.ML
changeset 44860 eb763b3ff9ed
parent 44858 2850b7dc27a4
child 44868 578460971517
equal deleted inserted replaced
44859:def89b8c6948 44860:eb763b3ff9ed
    26     No_Types
    26     No_Types
    27   datatype type_heaviness = Heavyweight | Lightweight
    27   datatype type_heaviness = Heavyweight | Lightweight
    28 
    28 
    29   datatype type_enc =
    29   datatype type_enc =
    30     Simple_Types of order * type_level |
    30     Simple_Types of order * type_level |
    31     Preds of polymorphism * type_level * type_heaviness |
    31     Guards of polymorphism * type_level * type_heaviness |
    32     Tags of polymorphism * type_level * type_heaviness
    32     Tags of polymorphism * type_level * type_heaviness
    33 
    33 
    34   val bound_var_prefix : string
    34   val bound_var_prefix : string
    35   val schematic_var_prefix : string
    35   val schematic_var_prefix : string
    36   val fixed_var_prefix : string
    36   val fixed_var_prefix : string
    43   val skolem_const_prefix : string
    43   val skolem_const_prefix : string
    44   val old_skolem_const_prefix : string
    44   val old_skolem_const_prefix : string
    45   val new_skolem_const_prefix : string
    45   val new_skolem_const_prefix : string
    46   val type_decl_prefix : string
    46   val type_decl_prefix : string
    47   val sym_decl_prefix : string
    47   val sym_decl_prefix : string
    48   val preds_sym_formula_prefix : string
    48   val guards_sym_formula_prefix : string
    49   val lightweight_tags_sym_formula_prefix : string
    49   val lightweight_tags_sym_formula_prefix : string
    50   val fact_prefix : string
    50   val fact_prefix : string
    51   val conjecture_prefix : string
    51   val conjecture_prefix : string
    52   val helper_prefix : string
    52   val helper_prefix : string
    53   val class_rel_clause_prefix : string
    53   val class_rel_clause_prefix : string
   131 val old_skolem_const_prefix = skolem_const_prefix ^ "o"
   131 val old_skolem_const_prefix = skolem_const_prefix ^ "o"
   132 val new_skolem_const_prefix = skolem_const_prefix ^ "n"
   132 val new_skolem_const_prefix = skolem_const_prefix ^ "n"
   133 
   133 
   134 val type_decl_prefix = "ty_"
   134 val type_decl_prefix = "ty_"
   135 val sym_decl_prefix = "sy_"
   135 val sym_decl_prefix = "sy_"
   136 val preds_sym_formula_prefix = "psy_"
   136 val guards_sym_formula_prefix = "gsy_"
   137 val lightweight_tags_sym_formula_prefix = "tsy_"
   137 val lightweight_tags_sym_formula_prefix = "tsy_"
   138 val fact_prefix = "fact_"
   138 val fact_prefix = "fact_"
   139 val conjecture_prefix = "conj_"
   139 val conjecture_prefix = "conj_"
   140 val helper_prefix = "help_"
   140 val helper_prefix = "help_"
   141 val class_rel_clause_prefix = "clar_"
   141 val class_rel_clause_prefix = "clar_"
   516   No_Types
   516   No_Types
   517 datatype type_heaviness = Heavyweight | Lightweight
   517 datatype type_heaviness = Heavyweight | Lightweight
   518 
   518 
   519 datatype type_enc =
   519 datatype type_enc =
   520   Simple_Types of order * type_level |
   520   Simple_Types of order * type_level |
   521   Preds of polymorphism * type_level * type_heaviness |
   521   Guards of polymorphism * type_level * type_heaviness |
   522   Tags of polymorphism * type_level * type_heaviness
   522   Tags of polymorphism * type_level * type_heaviness
   523 
   523 
   524 fun try_unsuffixes ss s =
   524 fun try_unsuffixes ss s =
   525   fold (fn s' => fn NONE => try (unsuffix s') s | some => some) ss NONE
   525   fold (fn s' => fn NONE => try (unsuffix s') s | some => some) ss NONE
   526 
   526 
   552            ("simple", (NONE, _, Lightweight)) =>
   552            ("simple", (NONE, _, Lightweight)) =>
   553            Simple_Types (First_Order, level)
   553            Simple_Types (First_Order, level)
   554          | ("simple_higher", (NONE, _, Lightweight)) =>
   554          | ("simple_higher", (NONE, _, Lightweight)) =>
   555            if level = Noninf_Nonmono_Types then raise Same.SAME
   555            if level = Noninf_Nonmono_Types then raise Same.SAME
   556            else Simple_Types (Higher_Order, level)
   556            else Simple_Types (Higher_Order, level)
   557          | ("preds", (SOME poly, _, _)) => Preds (poly, level, heaviness)
   557          | ("guards", (SOME poly, _, _)) => Guards (poly, level, heaviness)
   558          | ("tags", (SOME Polymorphic, _, _)) =>
   558          | ("tags", (SOME Polymorphic, _, _)) =>
   559            Tags (Polymorphic, level, heaviness)
   559            Tags (Polymorphic, level, heaviness)
   560          | ("tags", (SOME poly, _, _)) => Tags (poly, level, heaviness)
   560          | ("tags", (SOME poly, _, _)) => Tags (poly, level, heaviness)
   561          | ("args", (SOME poly, All_Types (* naja *), Lightweight)) =>
   561          | ("args", (SOME poly, All_Types (* naja *), Lightweight)) =>
   562            Preds (poly, Const_Arg_Types, Lightweight)
   562            Guards (poly, Const_Arg_Types, Lightweight)
   563          | ("erased", (NONE, All_Types (* naja *), Lightweight)) =>
   563          | ("erased", (NONE, All_Types (* naja *), Lightweight)) =>
   564            Preds (Polymorphic, No_Types, Lightweight)
   564            Guards (Polymorphic, No_Types, Lightweight)
   565          | _ => raise Same.SAME)
   565          | _ => raise Same.SAME)
   566   handle Same.SAME => error ("Unknown type system: " ^ quote s ^ ".")
   566   handle Same.SAME => error ("Unknown type system: " ^ quote s ^ ".")
   567 
   567 
   568 fun is_type_enc_higher_order (Simple_Types (Higher_Order, _)) = true
   568 fun is_type_enc_higher_order (Simple_Types (Higher_Order, _)) = true
   569   | is_type_enc_higher_order _ = false
   569   | is_type_enc_higher_order _ = false
   570 
   570 
   571 fun polymorphism_of_type_enc (Simple_Types _) = Mangled_Monomorphic
   571 fun polymorphism_of_type_enc (Simple_Types _) = Mangled_Monomorphic
   572   | polymorphism_of_type_enc (Preds (poly, _, _)) = poly
   572   | polymorphism_of_type_enc (Guards (poly, _, _)) = poly
   573   | polymorphism_of_type_enc (Tags (poly, _, _)) = poly
   573   | polymorphism_of_type_enc (Tags (poly, _, _)) = poly
   574 
   574 
   575 fun level_of_type_enc (Simple_Types (_, level)) = level
   575 fun level_of_type_enc (Simple_Types (_, level)) = level
   576   | level_of_type_enc (Preds (_, level, _)) = level
   576   | level_of_type_enc (Guards (_, level, _)) = level
   577   | level_of_type_enc (Tags (_, level, _)) = level
   577   | level_of_type_enc (Tags (_, level, _)) = level
   578 
   578 
   579 fun heaviness_of_type_enc (Simple_Types _) = Heavyweight
   579 fun heaviness_of_type_enc (Simple_Types _) = Heavyweight
   580   | heaviness_of_type_enc (Preds (_, _, heaviness)) = heaviness
   580   | heaviness_of_type_enc (Guards (_, _, heaviness)) = heaviness
   581   | heaviness_of_type_enc (Tags (_, _, heaviness)) = heaviness
   581   | heaviness_of_type_enc (Tags (_, _, heaviness)) = heaviness
   582 
   582 
   583 fun is_type_level_virtually_sound level =
   583 fun is_type_level_virtually_sound level =
   584   level = All_Types orelse level = Noninf_Nonmono_Types
   584   level = All_Types orelse level = Noninf_Nonmono_Types
   585 val is_type_enc_virtually_sound =
   585 val is_type_enc_virtually_sound =
   593     if member (op =) formats THF then
   593     if member (op =) formats THF then
   594       (THF, Simple_Types (order, level))
   594       (THF, Simple_Types (order, level))
   595     else if member (op =) formats TFF then
   595     else if member (op =) formats TFF then
   596       (TFF, Simple_Types (First_Order, level))
   596       (TFF, Simple_Types (First_Order, level))
   597     else
   597     else
   598       choose_format formats (Preds (Mangled_Monomorphic, level, Heavyweight))
   598       choose_format formats (Guards (Mangled_Monomorphic, level, Heavyweight))
   599   | choose_format formats type_enc =
   599   | choose_format formats type_enc =
   600     (case hd formats of
   600     (case hd formats of
   601        CNF_UEQ =>
   601        CNF_UEQ =>
   602        (CNF_UEQ, case type_enc of
   602        (CNF_UEQ, case type_enc of
   603                    Preds stuff =>
   603                    Guards stuff =>
   604                    (if is_type_enc_fairly_sound type_enc then Tags else Preds)
   604                    (if is_type_enc_fairly_sound type_enc then Tags else Guards)
   605                        stuff
   605                        stuff
   606                  | _ => type_enc)
   606                  | _ => type_enc)
   607      | format => (format, type_enc))
   607      | format => (format, type_enc))
   608 
   608 
   609 type translated_formula =
   609 type translated_formula =
  1033   | should_encode_type _ _ All_Types _ = true
  1033   | should_encode_type _ _ All_Types _ = true
  1034   | should_encode_type ctxt _ Fin_Nonmono_Types T =
  1034   | should_encode_type ctxt _ Fin_Nonmono_Types T =
  1035     is_type_surely_finite ctxt false T
  1035     is_type_surely_finite ctxt false T
  1036   | should_encode_type _ _ _ _ = false
  1036   | should_encode_type _ _ _ _ = false
  1037 
  1037 
  1038 fun should_predicate_on_type ctxt nonmono_Ts (Preds (_, level, heaviness))
  1038 fun should_predicate_on_type ctxt nonmono_Ts (Guards (_, level, heaviness))
  1039                              should_predicate_on_var T =
  1039                              should_predicate_on_var T =
  1040     (heaviness = Heavyweight orelse should_predicate_on_var ()) andalso
  1040     (heaviness = Heavyweight orelse should_predicate_on_var ()) andalso
  1041     should_encode_type ctxt nonmono_Ts level T
  1041     should_encode_type ctxt nonmono_Ts level T
  1042   | should_predicate_on_type _ _ _ _ _ = false
  1042   | should_predicate_on_type _ _ _ _ _ = false
  1043 
  1043 
  1754     Decl (sym_decl_prefix ^ s, (s, s'),
  1754     Decl (sym_decl_prefix ^ s, (s, s'),
  1755           (T_arg_Ts ---> (T |> homogenized_type ctxt nonmono_Ts level ary))
  1755           (T_arg_Ts ---> (T |> homogenized_type ctxt nonmono_Ts level ary))
  1756           |> ho_type_from_typ format type_enc pred_sym (length T_arg_Ts + ary))
  1756           |> ho_type_from_typ format type_enc pred_sym (length T_arg_Ts + ary))
  1757   end
  1757   end
  1758 
  1758 
  1759 fun formula_line_for_preds_sym_decl ctxt format conj_sym_kind nonmono_Ts
  1759 fun formula_line_for_guards_sym_decl ctxt format conj_sym_kind nonmono_Ts
  1760         poly_nonmono_Ts type_enc n s j (s', T_args, T, _, ary, in_conj) =
  1760         poly_nonmono_Ts type_enc n s j (s', T_args, T, _, ary, in_conj) =
  1761   let
  1761   let
  1762     val (kind, maybe_negate) =
  1762     val (kind, maybe_negate) =
  1763       if in_conj then (conj_sym_kind, conj_sym_kind = Conjecture ? mk_anot)
  1763       if in_conj then (conj_sym_kind, conj_sym_kind = Conjecture ? mk_anot)
  1764       else (Axiom, I)
  1764       else (Axiom, I)
  1773       sym_needs_arg_types orelse
  1773       sym_needs_arg_types orelse
  1774       not (should_predicate_on_type ctxt nonmono_Ts type_enc (K false) T)
  1774       not (should_predicate_on_type ctxt nonmono_Ts type_enc (K false) T)
  1775     val bound_Ts =
  1775     val bound_Ts =
  1776       arg_Ts |> map (fn T => if should_keep_arg_type T then SOME T else NONE)
  1776       arg_Ts |> map (fn T => if should_keep_arg_type T then SOME T else NONE)
  1777   in
  1777   in
  1778     Formula (preds_sym_formula_prefix ^ s ^
  1778     Formula (guards_sym_formula_prefix ^ s ^
  1779              (if n > 1 then "_" ^ string_of_int j else ""), kind,
  1779              (if n > 1 then "_" ^ string_of_int j else ""), kind,
  1780              IConst ((s, s'), T, T_args)
  1780              IConst ((s, s'), T, T_args)
  1781              |> fold (curry (IApp o swap)) bounds
  1781              |> fold (curry (IApp o swap)) bounds
  1782              |> type_pred_iterm ctxt format type_enc res_T
  1782              |> type_pred_iterm ctxt format type_enc res_T
  1783              |> AAtom |> mk_aquant AForall (bound_names ~~ bound_Ts)
  1783              |> AAtom |> mk_aquant AForall (bound_names ~~ bound_Ts)
  1849 fun problem_lines_for_sym_decls ctxt format conj_sym_kind nonmono_Ts
  1849 fun problem_lines_for_sym_decls ctxt format conj_sym_kind nonmono_Ts
  1850                                 poly_nonmono_Ts type_enc (s, decls) =
  1850                                 poly_nonmono_Ts type_enc (s, decls) =
  1851   case type_enc of
  1851   case type_enc of
  1852     Simple_Types _ =>
  1852     Simple_Types _ =>
  1853     decls |> map (decl_line_for_sym ctxt format nonmono_Ts type_enc s)
  1853     decls |> map (decl_line_for_sym ctxt format nonmono_Ts type_enc s)
  1854   | Preds _ =>
  1854   | Guards _ =>
  1855     let
  1855     let
  1856       val decls =
  1856       val decls =
  1857         case decls of
  1857         case decls of
  1858           decl :: (decls' as _ :: _) =>
  1858           decl :: (decls' as _ :: _) =>
  1859           let val T = result_type_of_decl decl in
  1859           let val T = result_type_of_decl decl in
  1869         decls |> filter (should_predicate_on_type ctxt poly_nonmono_Ts type_enc
  1869         decls |> filter (should_predicate_on_type ctxt poly_nonmono_Ts type_enc
  1870                                                   (K true)
  1870                                                   (K true)
  1871                          o result_type_of_decl)
  1871                          o result_type_of_decl)
  1872     in
  1872     in
  1873       (0 upto length decls - 1, decls)
  1873       (0 upto length decls - 1, decls)
  1874       |-> map2 (formula_line_for_preds_sym_decl ctxt format conj_sym_kind
  1874       |-> map2 (formula_line_for_guards_sym_decl ctxt format conj_sym_kind
  1875                    nonmono_Ts poly_nonmono_Ts type_enc n s)
  1875                    nonmono_Ts poly_nonmono_Ts type_enc n s)
  1876     end
  1876     end
  1877   | Tags (_, _, heaviness) =>
  1877   | Tags (_, _, heaviness) =>
  1878     (case heaviness of
  1878     (case heaviness of
  1879        Heavyweight => []
  1879        Heavyweight => []