src/HOL/Tools/ATP/atp_translate.ML
changeset 44491 de026aecab9b
parent 44490 e096b1effbbb
child 44493 a867ebb12209
equal deleted inserted replaced
44490:e096b1effbbb 44491:de026aecab9b
    17 
    17 
    18   datatype locality =
    18   datatype locality =
    19     General | Helper | Extensionality | Intro | Elim | Simp | Local | Assum |
    19     General | Helper | Extensionality | Intro | Elim | Simp | Local | Assum |
    20     Chained
    20     Chained
    21 
    21 
       
    22   datatype order = First_Order | Higher_Order
    22   datatype polymorphism = Polymorphic | Monomorphic | Mangled_Monomorphic
    23   datatype polymorphism = Polymorphic | Monomorphic | Mangled_Monomorphic
    23   datatype type_level =
    24   datatype type_level =
    24     All_Types | Noninf_Nonmono_Types | Fin_Nonmono_Types | Const_Arg_Types |
    25     All_Types | Noninf_Nonmono_Types | Fin_Nonmono_Types | Const_Arg_Types |
    25     No_Types
    26     No_Types
    26   datatype type_heaviness = Heavyweight | Lightweight
    27   datatype type_heaviness = Heavyweight | Lightweight
    27 
    28 
    28   datatype type_sys =
    29   datatype type_sys =
    29     Simple_Types of type_level |
    30     Simple_Types of order * type_level |
    30     Preds of polymorphism * type_level * type_heaviness |
    31     Preds of polymorphism * type_level * type_heaviness |
    31     Tags of polymorphism * type_level * type_heaviness
    32     Tags of polymorphism * type_level * type_heaviness
    32 
    33 
    33   val bound_var_prefix : string
    34   val bound_var_prefix : string
    34   val schematic_var_prefix : string
    35   val schematic_var_prefix : string
   499 fun is_locality_global Local = false
   500 fun is_locality_global Local = false
   500   | is_locality_global Assum = false
   501   | is_locality_global Assum = false
   501   | is_locality_global Chained = false
   502   | is_locality_global Chained = false
   502   | is_locality_global _ = true
   503   | is_locality_global _ = true
   503 
   504 
       
   505 datatype order = First_Order | Higher_Order
   504 datatype polymorphism = Polymorphic | Monomorphic | Mangled_Monomorphic
   506 datatype polymorphism = Polymorphic | Monomorphic | Mangled_Monomorphic
   505 datatype type_level =
   507 datatype type_level =
   506   All_Types | Noninf_Nonmono_Types | Fin_Nonmono_Types | Const_Arg_Types |
   508   All_Types | Noninf_Nonmono_Types | Fin_Nonmono_Types | Const_Arg_Types |
   507   No_Types
   509   No_Types
   508 datatype type_heaviness = Heavyweight | Lightweight
   510 datatype type_heaviness = Heavyweight | Lightweight
   509 
   511 
   510 datatype type_sys =
   512 datatype type_sys =
   511   Simple_Types of type_level |
   513   Simple_Types of order * type_level |
   512   Preds of polymorphism * type_level * type_heaviness |
   514   Preds of polymorphism * type_level * type_heaviness |
   513   Tags of polymorphism * type_level * type_heaviness
   515   Tags of polymorphism * type_level * type_heaviness
   514 
   516 
   515 fun try_unsuffixes ss s =
   517 fun try_unsuffixes ss s =
   516   fold (fn s' => fn NONE => try (unsuffix s') s | some => some) ss NONE
   518   fold (fn s' => fn NONE => try (unsuffix s') s | some => some) ss NONE
   524      | NONE =>
   526      | NONE =>
   525        case try (unprefix "mangled_") s of
   527        case try (unprefix "mangled_") s of
   526          SOME s => (SOME Mangled_Monomorphic, s)
   528          SOME s => (SOME Mangled_Monomorphic, s)
   527        | NONE => (NONE, s))
   529        | NONE => (NONE, s))
   528   ||> (fn s =>
   530   ||> (fn s =>
   529           (* "_query" and "_bang" are for the ASCII-challenged Mirabelle. *)
   531           (* "_query" and "_bang" are for the ASCII-challenged Metis and
       
   532              Mirabelle. *)
   530           case try_unsuffixes ["?", "_query"] s of
   533           case try_unsuffixes ["?", "_query"] s of
   531             SOME s => (Noninf_Nonmono_Types, s)
   534             SOME s => (Noninf_Nonmono_Types, s)
   532           | NONE =>
   535           | NONE =>
   533             case try_unsuffixes ["!", "_bang"] s of
   536             case try_unsuffixes ["!", "_bang"] s of
   534               SOME s => (Fin_Nonmono_Types, s)
   537               SOME s => (Fin_Nonmono_Types, s)
   537                 case try (unsuffix "_heavy") s of
   540                 case try (unsuffix "_heavy") s of
   538                   SOME s => (Heavyweight, s)
   541                   SOME s => (Heavyweight, s)
   539                 | NONE => (Lightweight, s))
   542                 | NONE => (Lightweight, s))
   540   |> (fn (poly, (level, (heaviness, core))) =>
   543   |> (fn (poly, (level, (heaviness, core))) =>
   541          case (core, (poly, level, heaviness)) of
   544          case (core, (poly, level, heaviness)) of
   542            ("simple", (NONE, _, Lightweight)) => Simple_Types level
   545            ("simple", (NONE, _, Lightweight)) =>
       
   546            Simple_Types (First_Order, level)
       
   547          | ("simple_higher", (NONE, _, Lightweight)) =>
       
   548            Simple_Types (Higher_Order, level)
   543          | ("preds", (SOME poly, _, _)) => Preds (poly, level, heaviness)
   549          | ("preds", (SOME poly, _, _)) => Preds (poly, level, heaviness)
   544          | ("tags", (SOME Polymorphic, _, _)) =>
   550          | ("tags", (SOME Polymorphic, _, _)) =>
   545            Tags (Polymorphic, level, heaviness)
   551            Tags (Polymorphic, level, heaviness)
   546          | ("tags", (SOME poly, _, _)) => Tags (poly, level, heaviness)
   552          | ("tags", (SOME poly, _, _)) => Tags (poly, level, heaviness)
   547          | ("args", (SOME poly, All_Types (* naja *), Lightweight)) =>
   553          | ("args", (SOME poly, All_Types (* naja *), Lightweight)) =>
   549          | ("erased", (NONE, All_Types (* naja *), Lightweight)) =>
   555          | ("erased", (NONE, All_Types (* naja *), Lightweight)) =>
   550            Preds (Polymorphic, No_Types, Lightweight)
   556            Preds (Polymorphic, No_Types, Lightweight)
   551          | _ => raise Same.SAME)
   557          | _ => raise Same.SAME)
   552   handle Same.SAME => error ("Unknown type system: " ^ quote s ^ ".")
   558   handle Same.SAME => error ("Unknown type system: " ^ quote s ^ ".")
   553 
   559 
       
   560 fun is_type_sys_higher_order (Simple_Types (Higher_Order, _)) = true
       
   561   | is_type_sys_higher_order _ = false
       
   562 
   554 fun polymorphism_of_type_sys (Simple_Types _) = Mangled_Monomorphic
   563 fun polymorphism_of_type_sys (Simple_Types _) = Mangled_Monomorphic
   555   | polymorphism_of_type_sys (Preds (poly, _, _)) = poly
   564   | polymorphism_of_type_sys (Preds (poly, _, _)) = poly
   556   | polymorphism_of_type_sys (Tags (poly, _, _)) = poly
   565   | polymorphism_of_type_sys (Tags (poly, _, _)) = poly
   557 
   566 
   558 fun level_of_type_sys (Simple_Types level) = level
   567 fun level_of_type_sys (Simple_Types (_, level)) = level
   559   | level_of_type_sys (Preds (_, level, _)) = level
   568   | level_of_type_sys (Preds (_, level, _)) = level
   560   | level_of_type_sys (Tags (_, level, _)) = level
   569   | level_of_type_sys (Tags (_, level, _)) = level
   561 
   570 
   562 fun heaviness_of_type_sys (Simple_Types _) = Heavyweight
   571 fun heaviness_of_type_sys (Simple_Types _) = Heavyweight
   563   | heaviness_of_type_sys (Preds (_, _, heaviness)) = heaviness
   572   | heaviness_of_type_sys (Preds (_, _, heaviness)) = heaviness
   570 
   579 
   571 fun is_type_level_fairly_sound level =
   580 fun is_type_level_fairly_sound level =
   572   is_type_level_virtually_sound level orelse level = Fin_Nonmono_Types
   581   is_type_level_virtually_sound level orelse level = Fin_Nonmono_Types
   573 val is_type_sys_fairly_sound = is_type_level_fairly_sound o level_of_type_sys
   582 val is_type_sys_fairly_sound = is_type_level_fairly_sound o level_of_type_sys
   574 
   583 
   575 fun is_setting_higher_order THF (Simple_Types _) = true
   584 fun choose_format formats (Simple_Types (order, level)) =
   576   | is_setting_higher_order _ _ = false
   585     if member (op =) formats THF then
   577 
   586       (THF, Simple_Types (order, level))
   578 fun choose_format formats (Simple_Types level) =
   587     else if member (op =) formats TFF then
   579     if member (op =) formats THF then (THF, Simple_Types level)
   588       (TFF, Simple_Types (First_Order, level))
   580     else if member (op =) formats TFF then (TFF, Simple_Types level)
   589     else
   581     else choose_format formats (Preds (Mangled_Monomorphic, level, Heavyweight))
   590       choose_format formats (Preds (Mangled_Monomorphic, level, Heavyweight))
   582   | choose_format formats type_sys =
   591   | choose_format formats type_sys =
   583     (case hd formats of
   592     (case hd formats of
   584        CNF_UEQ =>
   593        CNF_UEQ =>
   585        (CNF_UEQ, case type_sys of
   594        (CNF_UEQ, case type_sys of
   586                    Preds stuff =>
   595                    Preds stuff =>
   697 val homo_infinite_type = Type (homo_infinite_type_name, [])
   706 val homo_infinite_type = Type (homo_infinite_type_name, [])
   698 
   707 
   699 fun fo_term_from_typ format type_sys =
   708 fun fo_term_from_typ format type_sys =
   700   let
   709   let
   701     fun term (Type (s, Ts)) =
   710     fun term (Type (s, Ts)) =
   702       ATerm (case (is_setting_higher_order format type_sys, s) of
   711       ATerm (case (is_type_sys_higher_order type_sys, s) of
   703                (true, @{type_name bool}) => `I tptp_bool_type
   712                (true, @{type_name bool}) => `I tptp_bool_type
   704              | (true, @{type_name fun}) => `I tptp_fun_type
   713              | (true, @{type_name fun}) => `I tptp_fun_type
   705              | _ => if s = homo_infinite_type_name andalso
   714              | _ => if s = homo_infinite_type_name andalso
   706                        (format = TFF orelse format = THF) then
   715                        (format = TFF orelse format = THF) then
   707                       `I tptp_individual_type
   716                       `I tptp_individual_type
   731      s = tptp_individual_type then
   740      s = tptp_individual_type then
   732     s
   741     s
   733   else
   742   else
   734     simple_type_prefix ^ ascii_of s
   743     simple_type_prefix ^ ascii_of s
   735 
   744 
   736 fun ho_type_from_fo_term format type_sys pred_sym ary =
   745 fun ho_type_from_fo_term type_sys pred_sym ary =
   737   let
   746   let
   738     fun to_atype ty =
   747     fun to_atype ty =
   739       AType ((make_simple_type (generic_mangled_type_name fst ty),
   748       AType ((make_simple_type (generic_mangled_type_name fst ty),
   740               generic_mangled_type_name snd ty))
   749               generic_mangled_type_name snd ty))
   741     fun to_afun f1 f2 tys = AFun (f1 (hd tys), f2 (nth tys 1))
   750     fun to_afun f1 f2 tys = AFun (f1 (hd tys), f2 (nth tys 1))
   742     fun to_fo 0 ty = if pred_sym then bool_atype else to_atype ty
   751     fun to_fo 0 ty = if pred_sym then bool_atype else to_atype ty
   743       | to_fo ary (ATerm (_, tys)) = to_afun to_atype (to_fo (ary - 1)) tys
   752       | to_fo ary (ATerm (_, tys)) = to_afun to_atype (to_fo (ary - 1)) tys
   744     fun to_ho (ty as ATerm ((s, _), tys)) =
   753     fun to_ho (ty as ATerm ((s, _), tys)) =
   745       if s = tptp_fun_type then to_afun to_ho to_ho tys else to_atype ty
   754       if s = tptp_fun_type then to_afun to_ho to_ho tys else to_atype ty
   746   in if is_setting_higher_order format type_sys then to_ho else to_fo ary end
   755   in if is_type_sys_higher_order type_sys then to_ho else to_fo ary end
   747 
   756 
   748 fun mangled_type format type_sys pred_sym ary =
   757 fun mangled_type format type_sys pred_sym ary =
   749   ho_type_from_fo_term format type_sys pred_sym ary
   758   ho_type_from_fo_term type_sys pred_sym ary
   750   o fo_term_from_typ format type_sys
   759   o fo_term_from_typ format type_sys
   751 
   760 
   752 fun mangled_const_name format type_sys T_args (s, s') =
   761 fun mangled_const_name format type_sys T_args (s, s') =
   753   let
   762   let
   754     val ty_args = T_args |> map_filter (fo_term_for_type_arg format type_sys)
   763     val ty_args = T_args |> map_filter (fo_term_for_type_arg format type_sys)
   778 fun unmangled_const s =
   787 fun unmangled_const s =
   779   let val ss = space_explode mangled_type_sep s in
   788   let val ss = space_explode mangled_type_sep s in
   780     (hd ss, map unmangled_type (tl ss))
   789     (hd ss, map unmangled_type (tl ss))
   781   end
   790   end
   782 
   791 
   783 fun introduce_proxies format type_sys =
   792 fun introduce_proxies type_sys =
   784   let
   793   let
   785     fun intro top_level (CombApp (tm1, tm2)) =
   794     fun intro top_level (CombApp (tm1, tm2)) =
   786         CombApp (intro top_level tm1, intro false tm2)
   795         CombApp (intro top_level tm1, intro false tm2)
   787       | intro top_level (CombConst (name as (s, _), T, T_args)) =
   796       | intro top_level (CombConst (name as (s, _), T, T_args)) =
   788         (case proxify_const s of
   797         (case proxify_const s of
   789            SOME proxy_base =>
   798            SOME proxy_base =>
   790            if top_level orelse is_setting_higher_order format type_sys then
   799            if top_level orelse is_type_sys_higher_order type_sys then
   791              case (top_level, s) of
   800              case (top_level, s) of
   792                (_, "c_False") => (`I tptp_false, [])
   801                (_, "c_False") => (`I tptp_false, [])
   793              | (_, "c_True") => (`I tptp_true, [])
   802              | (_, "c_True") => (`I tptp_true, [])
   794              | (false, "c_Not") => (`I tptp_not, [])
   803              | (false, "c_Not") => (`I tptp_not, [])
   795              | (false, "c_conj") => (`I tptp_and, [])
   804              | (false, "c_conj") => (`I tptp_and, [])
   804           | NONE => (name, T_args))
   813           | NONE => (name, T_args))
   805         |> (fn (name, T_args) => CombConst (name, T, T_args))
   814         |> (fn (name, T_args) => CombConst (name, T, T_args))
   806       | intro _ tm = tm
   815       | intro _ tm = tm
   807   in intro true end
   816   in intro true end
   808 
   817 
   809 fun combformula_from_prop thy format type_sys eq_as_iff =
   818 fun combformula_from_prop thy type_sys eq_as_iff =
   810   let
   819   let
   811     fun do_term bs t atomic_types =
   820     fun do_term bs t atomic_types =
   812       combterm_from_term thy bs (Envir.eta_contract t)
   821       combterm_from_term thy bs (Envir.eta_contract t)
   813       |>> (introduce_proxies format type_sys #> AAtom)
   822       |>> (introduce_proxies type_sys #> AAtom)
   814       ||> union (op =) atomic_types
   823       ||> union (op =) atomic_types
   815     fun do_quant bs q s T t' =
   824     fun do_quant bs q s T t' =
   816       let val s = singleton (Name.variant_list (map fst bs)) s in
   825       let val s = singleton (Name.variant_list (map fst bs)) s in
   817         do_formula ((s, T) :: bs) t'
   826         do_formula ((s, T) :: bs) t'
   818         #>> mk_aquant q [(`make_bound_var s, SOME T)]
   827         #>> mk_aquant q [(`make_bound_var s, SOME T)]
   929       |> perhaps (try (HOLogic.dest_Trueprop))
   938       |> perhaps (try (HOLogic.dest_Trueprop))
   930       |> introduce_combinators_in_term ctxt kind
   939       |> introduce_combinators_in_term ctxt kind
   931   end
   940   end
   932 
   941 
   933 (* making fact and conjecture formulas *)
   942 (* making fact and conjecture formulas *)
   934 fun make_formula thy format type_sys eq_as_iff name loc kind t =
   943 fun make_formula thy type_sys eq_as_iff name loc kind t =
   935   let
   944   let
   936     val (combformula, atomic_types) =
   945     val (combformula, atomic_types) =
   937       combformula_from_prop thy format type_sys eq_as_iff t []
   946       combformula_from_prop thy type_sys eq_as_iff t []
   938   in
   947   in
   939     {name = name, locality = loc, kind = kind, combformula = combformula,
   948     {name = name, locality = loc, kind = kind, combformula = combformula,
   940      atomic_types = atomic_types}
   949      atomic_types = atomic_types}
   941   end
   950   end
   942 
   951 
   943 fun make_fact ctxt format type_sys eq_as_iff preproc presimp_consts
   952 fun make_fact ctxt format type_sys eq_as_iff preproc presimp_consts
   944               ((name, loc), t) =
   953               ((name, loc), t) =
   945   let val thy = Proof_Context.theory_of ctxt in
   954   let val thy = Proof_Context.theory_of ctxt in
   946     case t |> preproc ? preprocess_prop ctxt presimp_consts Axiom
   955     case t |> preproc ? preprocess_prop ctxt presimp_consts Axiom
   947            |> make_formula thy format type_sys (eq_as_iff andalso format <> CNF)
   956            |> make_formula thy type_sys (eq_as_iff andalso format <> CNF) name
   948                            name loc Axiom of
   957                            loc Axiom of
   949       formula as {combformula = AAtom (CombConst ((s, _), _, _)), ...} =>
   958       formula as {combformula = AAtom (CombConst ((s, _), _, _)), ...} =>
   950       if s = tptp_true then NONE else SOME formula
   959       if s = tptp_true then NONE else SOME formula
   951     | formula => SOME formula
   960     | formula => SOME formula
   952   end
   961   end
   953 
   962 
   966                     if prem_kind = Conjecture then update_combformula mk_anot
   975                     if prem_kind = Conjecture then update_combformula mk_anot
   967                     else I)
   976                     else I)
   968               in
   977               in
   969                 t |> preproc ?
   978                 t |> preproc ?
   970                      (preprocess_prop ctxt presimp_consts kind #> freeze_term)
   979                      (preprocess_prop ctxt presimp_consts kind #> freeze_term)
   971                   |> make_formula thy format type_sys (format <> CNF)
   980                   |> make_formula thy type_sys (format <> CNF) (string_of_int j)
   972                                   (string_of_int j) Local kind
   981                                   Local kind
   973                   |> maybe_negate
   982                   |> maybe_negate
   974               end)
   983               end)
   975          (0 upto last) ts
   984          (0 upto last) ts
   976   end
   985   end
   977 
   986 
  1247         |> (fn (name, T_args) => CombConst (name, T, T_args))
  1256         |> (fn (name, T_args) => CombConst (name, T, T_args))
  1248       | aux _ tm = tm
  1257       | aux _ tm = tm
  1249   in aux 0 end
  1258   in aux 0 end
  1250 
  1259 
  1251 fun repair_combterm ctxt format type_sys sym_tab =
  1260 fun repair_combterm ctxt format type_sys sym_tab =
  1252   not (is_setting_higher_order format type_sys)
  1261   not (is_type_sys_higher_order type_sys)
  1253   ? (introduce_explicit_apps_in_combterm sym_tab
  1262   ? (introduce_explicit_apps_in_combterm sym_tab
  1254      #> introduce_predicators_in_combterm sym_tab)
  1263      #> introduce_predicators_in_combterm sym_tab)
  1255   #> enforce_type_arg_policy_in_combterm ctxt format type_sys
  1264   #> enforce_type_arg_policy_in_combterm ctxt format type_sys
  1256 fun repair_fact ctxt format type_sys sym_tab =
  1265 fun repair_fact ctxt format type_sys sym_tab =
  1257   update_combformula (formula_map
  1266   update_combformula (formula_map
  1475   let
  1484   let
  1476     fun do_term pos =
  1485     fun do_term pos =
  1477       term_from_combterm ctxt format nonmono_Ts type_sys (Top_Level pos)
  1486       term_from_combterm ctxt format nonmono_Ts type_sys (Top_Level pos)
  1478     val do_bound_type =
  1487     val do_bound_type =
  1479       case type_sys of
  1488       case type_sys of
  1480         Simple_Types level =>
  1489         Simple_Types (_, level) =>
  1481         homogenized_type ctxt nonmono_Ts level 0
  1490         homogenized_type ctxt nonmono_Ts level 0
  1482         #> mangled_type format type_sys false 0 #> SOME
  1491         #> mangled_type format type_sys false 0 #> SOME
  1483       | _ => K NONE
  1492       | _ => K NONE
  1484     fun do_out_of_bound_type pos phi universal (name, T) =
  1493     fun do_out_of_bound_type pos phi universal (name, T) =
  1485       if should_predicate_on_type ctxt nonmono_Ts type_sys
  1494       if should_predicate_on_type ctxt nonmono_Ts type_sys
  1647 fun decl_line_for_sym ctxt format nonmono_Ts type_sys s
  1656 fun decl_line_for_sym ctxt format nonmono_Ts type_sys s
  1648                       (s', T_args, T, pred_sym, ary, _) =
  1657                       (s', T_args, T, pred_sym, ary, _) =
  1649   let
  1658   let
  1650     val (T_arg_Ts, level) =
  1659     val (T_arg_Ts, level) =
  1651       case type_sys of
  1660       case type_sys of
  1652         Simple_Types level => ([], level)
  1661         Simple_Types (_, level) => ([], level)
  1653       | _ => (replicate (length T_args) homo_infinite_type, No_Types)
  1662       | _ => (replicate (length T_args) homo_infinite_type, No_Types)
  1654   in
  1663   in
  1655     Decl (sym_decl_prefix ^ s, (s, s'),
  1664     Decl (sym_decl_prefix ^ s, (s, s'),
  1656           (T_arg_Ts ---> (T |> homogenized_type ctxt nonmono_Ts level ary))
  1665           (T_arg_Ts ---> (T |> homogenized_type ctxt nonmono_Ts level ary))
  1657           |> mangled_type format type_sys pred_sym (length T_arg_Ts + ary))
  1666           |> mangled_type format type_sys pred_sym (length T_arg_Ts + ary))