src/HOL/Tools/ATP/atp_translate.ML
changeset 45947 7eccf8147f57
parent 45946 428db0387f32
child 45948 f88f502d635f
equal deleted inserted replaced
45946:428db0387f32 45947:7eccf8147f57
  1315     String.isPrefix bound_var_prefix s orelse
  1315     String.isPrefix bound_var_prefix s orelse
  1316     String.isPrefix all_bound_var_prefix s
  1316     String.isPrefix all_bound_var_prefix s
  1317   | is_maybe_universal_var (IVar _) = true
  1317   | is_maybe_universal_var (IVar _) = true
  1318   | is_maybe_universal_var _ = false
  1318   | is_maybe_universal_var _ = false
  1319 
  1319 
  1320 datatype tag_site =
  1320 datatype site =
  1321   Top_Level of bool option |
  1321   Top_Level of bool option |
  1322   Eq_Arg of bool option |
  1322   Eq_Arg of bool option |
  1323   Arg of string * int * int |
  1323   Arg of string * int * int |
  1324   Elsewhere
  1324   Elsewhere
  1325 
  1325 
  1326 fun should_tag_with_type _ _ _ [Top_Level _] _ _ = false
  1326 fun should_tag_with_type _ _ _ _ [Top_Level _] _ _ = false
  1327   | should_tag_with_type ctxt mono (Tags (_, level)) sites u T =
  1327   | should_tag_with_type ctxt polym_constrs mono (Tags (_, level)) sites u T =
  1328     (case granularity_of_type_level level of
  1328     (case granularity_of_type_level level of
  1329        All_Vars => should_encode_type ctxt mono level T
  1329        All_Vars => should_encode_type ctxt mono level T
  1330      | grain =>
  1330      | grain =>
  1331        case (hd sites, is_maybe_universal_var u) of
  1331        case (sites, is_maybe_universal_var u) of
  1332          (Eq_Arg _, true) => should_encode_type ctxt mono level T
  1332          (Eq_Arg _ :: _, true) => should_encode_type ctxt mono level T
  1333        | (Arg (s, j, ary), true) =>
  1333        | (Arg (s, j, ary) :: site0 :: _, true) =>
  1334          let val thy = Proof_Context.theory_of ctxt in
  1334          grain = Ghost_Type_Arg_Vars andalso
  1335            grain = Ghost_Type_Arg_Vars andalso
  1335          let
  1336            member (op =) (ghost_type_args thy s ary) j
  1336            val thy = Proof_Context.theory_of ctxt
       
  1337            fun is_ghost_type_arg s j ary =
       
  1338              member (op =) (ghost_type_args thy s ary) j
       
  1339            fun is_ghost_site (Arg (s, j, ary)) = is_ghost_type_arg s j ary
       
  1340              | is_ghost_site _ = true
       
  1341          in
       
  1342            is_ghost_type_arg s j ary andalso
       
  1343            (not (member (op =) polym_constrs s) orelse is_ghost_site site0)
  1337          end
  1344          end
  1338        | _ => false)
  1345        | _ => false)
  1339   | should_tag_with_type _ _ _ _ _ _ = false
  1346   | should_tag_with_type _ _ _ _ _ _ _ = false
  1340 
  1347 
  1341 fun fused_type ctxt mono level =
  1348 fun fused_type ctxt mono level =
  1342   let
  1349   let
  1343     val should_encode = should_encode_type ctxt mono level
  1350     val should_encode = should_encode_type ctxt mono level
  1344     fun fuse 0 T = if should_encode T then T else fused_infinite_type
  1351     fun fuse 0 T = if should_encode T then T else fused_infinite_type
  1801   | should_generate_tag_bound_decl _ _ _ _ _ = false
  1808   | should_generate_tag_bound_decl _ _ _ _ _ = false
  1802 
  1809 
  1803 fun mk_aterm format type_enc name T_args args =
  1810 fun mk_aterm format type_enc name T_args args =
  1804   ATerm (name, map_filter (ho_term_for_type_arg format type_enc) T_args @ args)
  1811   ATerm (name, map_filter (ho_term_for_type_arg format type_enc) T_args @ args)
  1805 
  1812 
  1806 fun tag_with_type ctxt format mono type_enc pos T tm =
  1813 fun tag_with_type ctxt polym_constrs format mono type_enc pos T tm =
  1807   IConst (type_tag, T --> T, [T])
  1814   IConst (type_tag, T --> T, [T])
  1808   |> mangle_type_args_in_iterm format type_enc
  1815   |> mangle_type_args_in_iterm format type_enc
  1809   |> ho_term_from_iterm ctxt format mono type_enc pos
  1816   |> ho_term_from_iterm ctxt polym_constrs format mono type_enc pos
  1810   |> (fn ATerm (s, tms) => ATerm (s, tms @ [tm])
  1817   |> (fn ATerm (s, tms) => ATerm (s, tms @ [tm])
  1811        | _ => raise Fail "unexpected lambda-abstraction")
  1818        | _ => raise Fail "unexpected lambda-abstraction")
  1812 and ho_term_from_iterm ctxt format mono type_enc =
  1819 and ho_term_from_iterm ctxt polym_constrs format mono type_enc =
  1813   let
  1820   let
  1814     fun term sites u =
  1821     fun term sites u =
  1815       let
  1822       let
  1816         val (head, args) = strip_iterm_comb u
  1823         val (head, args) = strip_iterm_comb u
  1817         val pos =
  1824         val pos =
  1837             AAbs ((name, ho_type_from_typ format type_enc true 0 T),
  1844             AAbs ((name, ho_type_from_typ format type_enc true 0 T),
  1838                   term (Elsewhere :: sites) tm)
  1845                   term (Elsewhere :: sites) tm)
  1839           | IApp _ => raise Fail "impossible \"IApp\""
  1846           | IApp _ => raise Fail "impossible \"IApp\""
  1840         val T = ityp_of u
  1847         val T = ityp_of u
  1841       in
  1848       in
  1842         t |> (if should_tag_with_type ctxt mono type_enc sites u T then
  1849         if should_tag_with_type ctxt polym_constrs mono type_enc sites u T then
  1843                 tag_with_type ctxt format mono type_enc pos T
  1850           tag_with_type ctxt polym_constrs format mono type_enc pos T t
  1844               else
  1851         else
  1845                 I)
  1852           t
  1846       end
  1853       end
  1847   in term o single o Top_Level end
  1854   in term o single o Top_Level end
  1848 and formula_from_iformula ctxt format mono type_enc should_guard_var =
  1855 and formula_from_iformula ctxt polym_constrs format mono type_enc
       
  1856                           should_guard_var =
  1849   let
  1857   let
  1850     val thy = Proof_Context.theory_of ctxt
  1858     val thy = Proof_Context.theory_of ctxt
  1851     val level = level_of_type_enc type_enc
  1859     val level = level_of_type_enc type_enc
  1852     val do_term = ho_term_from_iterm ctxt format mono type_enc
  1860     val do_term = ho_term_from_iterm ctxt polym_constrs format mono type_enc
  1853     val do_bound_type =
  1861     val do_bound_type =
  1854       case type_enc of
  1862       case type_enc of
  1855         Simple_Types _ => fused_type ctxt mono level 0
  1863         Simple_Types _ => fused_type ctxt mono level 0
  1856         #> ho_type_from_typ format type_enc false 0 #> SOME
  1864         #> ho_type_from_typ format type_enc false 0 #> SOME
  1857       | _ => K NONE
  1865       | _ => K NONE
  1862         |> type_guard_iterm format type_enc T
  1870         |> type_guard_iterm format type_enc T
  1863         |> do_term pos |> AAtom |> SOME
  1871         |> do_term pos |> AAtom |> SOME
  1864       else if should_generate_tag_bound_decl ctxt mono type_enc universal T then
  1872       else if should_generate_tag_bound_decl ctxt mono type_enc universal T then
  1865         let
  1873         let
  1866           val var = ATerm (name, [])
  1874           val var = ATerm (name, [])
  1867           val tagged_var = var |> tag_with_type ctxt format mono type_enc pos T
  1875           val tagged_var = tag_with_type ctxt [] format mono type_enc pos T var
  1868         in SOME (AAtom (ATerm (`I tptp_equal, [tagged_var, var]))) end
  1876         in SOME (AAtom (ATerm (`I tptp_equal, [tagged_var, var]))) end
  1869       else
  1877       else
  1870         NONE
  1878         NONE
  1871     fun do_formula pos (AQuant (q, xs, phi)) =
  1879     fun do_formula pos (AQuant (q, xs, phi)) =
  1872         let
  1880         let
  1888   in do_formula end
  1896   in do_formula end
  1889 
  1897 
  1890 (* Each fact is given a unique fact number to avoid name clashes (e.g., because
  1898 (* Each fact is given a unique fact number to avoid name clashes (e.g., because
  1891    of monomorphization). The TPTP explicitly forbids name clashes, and some of
  1899    of monomorphization). The TPTP explicitly forbids name clashes, and some of
  1892    the remote provers might care. *)
  1900    the remote provers might care. *)
  1893 fun formula_line_for_fact ctxt format prefix encode freshen pos mono type_enc
  1901 fun formula_line_for_fact ctxt polym_constrs format prefix encode freshen pos
  1894                           (j, {name, locality, kind, iformula, atomic_types}) =
  1902         mono type_enc (j, {name, locality, kind, iformula, atomic_types}) =
  1895   (prefix ^ (if freshen then string_of_int j ^ "_" else "") ^ encode name, kind,
  1903   (prefix ^ (if freshen then string_of_int j ^ "_" else "") ^ encode name, kind,
  1896    iformula
  1904    iformula
  1897    |> formula_from_iformula ctxt format mono type_enc
  1905    |> formula_from_iformula ctxt polym_constrs format mono type_enc
  1898                             should_guard_var_in_formula
  1906           should_guard_var_in_formula (if pos then SOME true else NONE)
  1899                             (if pos then SOME true else NONE)
       
  1900    |> close_formula_universally
  1907    |> close_formula_universally
  1901    |> bound_tvars type_enc true atomic_types,
  1908    |> bound_tvars type_enc true atomic_types,
  1902    NONE,
  1909    NONE,
  1903    case locality of
  1910    case locality of
  1904      Intro => isabelle_info format introN
  1911      Intro => isabelle_info format introN
  1930                     (formula_from_arity_atom type_enc concl_atom)
  1937                     (formula_from_arity_atom type_enc concl_atom)
  1931            |> mk_aquant AForall
  1938            |> mk_aquant AForall
  1932                   (map (rpair (atype_of_type_vars type_enc)) (#3 concl_atom)),
  1939                   (map (rpair (atype_of_type_vars type_enc)) (#3 concl_atom)),
  1933            isabelle_info format introN, NONE)
  1940            isabelle_info format introN, NONE)
  1934 
  1941 
  1935 fun formula_line_for_conjecture ctxt format mono type_enc
  1942 fun formula_line_for_conjecture ctxt polym_constrs format mono type_enc
  1936         ({name, kind, iformula, atomic_types, ...} : translated_formula) =
  1943         ({name, kind, iformula, atomic_types, ...} : translated_formula) =
  1937   Formula (conjecture_prefix ^ name, kind,
  1944   Formula (conjecture_prefix ^ name, kind,
  1938            iformula
  1945            iformula
  1939            |> formula_from_iformula ctxt format mono type_enc
  1946            |> formula_from_iformula ctxt polym_constrs format mono type_enc
  1940                   should_guard_var_in_formula (SOME false)
  1947                   should_guard_var_in_formula (SOME false)
  1941            |> close_formula_universally
  1948            |> close_formula_universally
  1942            |> bound_tvars type_enc true atomic_types, NONE, NONE)
  1949            |> bound_tvars type_enc true atomic_types, NONE, NONE)
  1943 
  1950 
  1944 fun formula_line_for_free_type j phi =
  1951 fun formula_line_for_free_type j phi =
  2131            ascii_of (mangled_type format type_enc T),
  2138            ascii_of (mangled_type format type_enc T),
  2132            Axiom,
  2139            Axiom,
  2133            IConst (`make_bound_var "X", T, [])
  2140            IConst (`make_bound_var "X", T, [])
  2134            |> type_guard_iterm format type_enc T
  2141            |> type_guard_iterm format type_enc T
  2135            |> AAtom
  2142            |> AAtom
  2136            |> formula_from_iformula ctxt format mono type_enc
  2143            |> formula_from_iformula ctxt [] format mono type_enc
  2137                                     (K (K (K (K (K (K true)))))) (SOME true)
  2144                                     (K (K (K (K (K (K true)))))) (SOME true)
  2138            |> close_formula_universally
  2145            |> close_formula_universally
  2139            |> bound_tvars type_enc true (atomic_types_of T),
  2146            |> bound_tvars type_enc true (atomic_types_of T),
  2140            isabelle_info format introN, NONE)
  2147            isabelle_info format introN, NONE)
  2141 
  2148 
  2143   let val x_var = ATerm (`make_bound_var "X", []) in
  2150   let val x_var = ATerm (`make_bound_var "X", []) in
  2144     Formula (tags_sym_formula_prefix ^
  2151     Formula (tags_sym_formula_prefix ^
  2145              ascii_of (mangled_type format type_enc T),
  2152              ascii_of (mangled_type format type_enc T),
  2146              Axiom,
  2153              Axiom,
  2147              eq_formula type_enc (atomic_types_of T) false
  2154              eq_formula type_enc (atomic_types_of T) false
  2148                         (tag_with_type ctxt format mono type_enc NONE T x_var)
  2155                   (tag_with_type ctxt [] format mono type_enc NONE T x_var)
  2149                         x_var,
  2156                   x_var,
  2150              isabelle_info format simpN, NONE)
  2157              isabelle_info format simpN, NONE)
  2151   end
  2158   end
  2152 
  2159 
  2153 fun problem_lines_for_mono_types ctxt format mono type_enc Ts =
  2160 fun problem_lines_for_mono_types ctxt format mono type_enc Ts =
  2154   case type_enc of
  2161   case type_enc of
  2209              (if n > 1 then "_" ^ string_of_int j else ""), kind,
  2216              (if n > 1 then "_" ^ string_of_int j else ""), kind,
  2210              IConst ((s, s'), T, T_args)
  2217              IConst ((s, s'), T, T_args)
  2211              |> fold (curry (IApp o swap)) bounds
  2218              |> fold (curry (IApp o swap)) bounds
  2212              |> type_guard_iterm format type_enc res_T
  2219              |> type_guard_iterm format type_enc res_T
  2213              |> AAtom |> mk_aquant AForall (bound_names ~~ bound_Ts)
  2220              |> AAtom |> mk_aquant AForall (bound_names ~~ bound_Ts)
  2214              |> formula_from_iformula ctxt format mono type_enc
  2221              |> formula_from_iformula ctxt [] format mono type_enc
  2215                                       (K (K (K (K (K (K true)))))) (SOME true)
  2222                                       (K (K (K (K (K (K true)))))) (SOME true)
  2216              |> close_formula_universally
  2223              |> close_formula_universally
  2217              |> bound_tvars type_enc (n > 1) (atomic_types_of T)
  2224              |> bound_tvars type_enc (n > 1) (atomic_types_of T)
  2218              |> maybe_negate,
  2225              |> maybe_negate,
  2219              isabelle_info format introN, NONE)
  2226              isabelle_info format introN, NONE)
  2235     val bound_names = 1 upto ary |> map (`I o make_bound_var o string_of_int)
  2242     val bound_names = 1 upto ary |> map (`I o make_bound_var o string_of_int)
  2236     val bounds = bound_names |> map (fn name => ATerm (name, []))
  2243     val bounds = bound_names |> map (fn name => ATerm (name, []))
  2237     val cst = mk_aterm format type_enc (s, s') T_args
  2244     val cst = mk_aterm format type_enc (s, s') T_args
  2238     val eq = maybe_negate oo eq_formula type_enc (atomic_types_of T) pred_sym
  2245     val eq = maybe_negate oo eq_formula type_enc (atomic_types_of T) pred_sym
  2239     val should_encode = should_encode_type ctxt mono level
  2246     val should_encode = should_encode_type ctxt mono level
  2240     val tag_with = tag_with_type ctxt format mono type_enc NONE
  2247     val tag_with = tag_with_type ctxt [] format mono type_enc NONE
  2241     val add_formula_for_res =
  2248     val add_formula_for_res =
  2242       if should_encode res_T then
  2249       if should_encode res_T then
  2243         let
  2250         let
  2244           val tagged_bounds =
  2251           val tagged_bounds =
  2245             if grain = Ghost_Type_Arg_Vars then
  2252             if grain = Ghost_Type_Arg_Vars then
  2322     val syms = sym_decl_tab |> Symtab.dest |> sort_wrt fst
  2329     val syms = sym_decl_tab |> Symtab.dest |> sort_wrt fst
  2323     val mono_lines =
  2330     val mono_lines =
  2324       problem_lines_for_mono_types ctxt format mono type_enc mono_Ts
  2331       problem_lines_for_mono_types ctxt format mono type_enc mono_Ts
  2325     val decl_lines =
  2332     val decl_lines =
  2326       fold_rev (append o problem_lines_for_sym_decls ctxt format conj_sym_kind
  2333       fold_rev (append o problem_lines_for_sym_decls ctxt format conj_sym_kind
  2327                                                      mono type_enc)
  2334                              mono type_enc)
  2328                syms []
  2335                syms []
  2329   in mono_lines @ decl_lines end
  2336   in mono_lines @ decl_lines end
  2330 
  2337 
  2331 fun needs_type_tag_idempotence ctxt (Tags (poly, level)) =
  2338 fun needs_type_tag_idempotence ctxt (Tags (poly, level)) =
  2332     Config.get ctxt type_tag_idempotence andalso
  2339     Config.get ctxt type_tag_idempotence andalso
  2405   in ex end
  2412   in ex end
  2406 
  2413 
  2407 fun is_poly_constr (_, Us) =
  2414 fun is_poly_constr (_, Us) =
  2408   exists (exists_subdtype (fn Datatype.DtTFree _ => true | _ => false)) Us
  2415   exists (exists_subdtype (fn Datatype.DtTFree _ => true | _ => false)) Us
  2409 
  2416 
  2410 fun all_monomorphic_constrs_of_polymorphic_datatypes thy =
  2417 fun all_constrs_of_polymorphic_datatypes thy =
  2411   Symtab.fold (snd
  2418   Symtab.fold (snd
  2412                #> #descr
  2419                #> #descr
  2413                #> maps (snd #> #3)
  2420                #> maps (snd #> #3)
  2414                #> (fn cs => exists is_poly_constr cs ? append cs))
  2421                #> (fn cs => exists is_poly_constr cs ? append cs))
  2415               (Datatype.get_all thy) []
  2422               (Datatype.get_all thy) []
  2416   |> filter_out is_poly_constr
  2423   |> List.partition is_poly_constr
  2417   |> map fst
  2424   |> pairself (map fst)
  2418 
  2425 
  2419 (* Forcing explicit applications is expensive for polymorphic encodings, because
  2426 (* Forcing explicit applications is expensive for polymorphic encodings, because
  2420    it takes only one existential variable ranging over "'a => 'b" to ruin
  2427    it takes only one existential variable ranging over "'a => 'b" to ruin
  2421    everything. Hence we do it only if there are few facts (is normally the case
  2428    everything. Hence we do it only if there are few facts (is normally the case
  2422    for "metis" and the minimizer. *)
  2429    for "metis" and the minimizer. *)
  2444          lifted) =
  2451          lifted) =
  2445       translate_formulas ctxt format prem_kind type_enc lam_trans preproc hyp_ts
  2452       translate_formulas ctxt format prem_kind type_enc lam_trans preproc hyp_ts
  2446                          concl_t facts
  2453                          concl_t facts
  2447     val sym_tab = sym_table_for_facts ctxt type_enc explicit_apply conjs facts
  2454     val sym_tab = sym_table_for_facts ctxt type_enc explicit_apply conjs facts
  2448     val mono = conjs @ facts |> mononotonicity_info_for_facts ctxt type_enc
  2455     val mono = conjs @ facts |> mononotonicity_info_for_facts ctxt type_enc
  2449     val monom_constrs = all_monomorphic_constrs_of_polymorphic_datatypes thy
  2456     val (polym_constrs, monom_constrs) =
       
  2457       all_constrs_of_polymorphic_datatypes thy
       
  2458       |>> map (make_fixed_const (SOME format))
  2450     val firstorderize =
  2459     val firstorderize =
  2451       firstorderize_fact thy monom_constrs format type_enc sym_tab
  2460       firstorderize_fact thy monom_constrs format type_enc sym_tab
  2452     val (conjs, facts) = (conjs, facts) |> pairself (map firstorderize)
  2461     val (conjs, facts) = (conjs, facts) |> pairself (map firstorderize)
  2453     val sym_tab = sym_table_for_facts ctxt type_enc (SOME false) conjs facts
  2462     val sym_tab = sym_table_for_facts ctxt type_enc (SOME false) conjs facts
  2454     val helpers =
  2463     val helpers =
  2462       |> sym_decl_table_for_facts ctxt format type_enc sym_tab
  2471       |> sym_decl_table_for_facts ctxt format type_enc sym_tab
  2463       |> problem_lines_for_sym_decl_table ctxt format conj_sym_kind mono
  2472       |> problem_lines_for_sym_decl_table ctxt format conj_sym_kind mono
  2464                                                type_enc mono_Ts
  2473                                                type_enc mono_Ts
  2465     val helper_lines =
  2474     val helper_lines =
  2466       0 upto length helpers - 1 ~~ helpers
  2475       0 upto length helpers - 1 ~~ helpers
  2467       |> map (formula_line_for_fact ctxt format helper_prefix I false true mono
  2476       |> map (formula_line_for_fact ctxt polym_constrs format helper_prefix I
  2468                                     type_enc)
  2477                                     false true mono type_enc)
  2469       |> (if needs_type_tag_idempotence ctxt type_enc then
  2478       |> (if needs_type_tag_idempotence ctxt type_enc then
  2470             cons (type_tag_idempotence_fact format type_enc)
  2479             cons (type_tag_idempotence_fact format type_enc)
  2471           else
  2480           else
  2472             I)
  2481             I)
  2473     (* Reordering these might confuse the proof reconstruction code or the SPASS
  2482     (* Reordering these might confuse the proof reconstruction code or the SPASS
  2474        FLOTTER hack. *)
  2483        FLOTTER hack. *)
  2475     val problem =
  2484     val problem =
  2476       [(explicit_declsN, class_decl_lines @ sym_decl_lines),
  2485       [(explicit_declsN, class_decl_lines @ sym_decl_lines),
  2477        (factsN,
  2486        (factsN,
  2478         map (formula_line_for_fact ctxt format fact_prefix ascii_of
  2487         map (formula_line_for_fact ctxt polym_constrs format fact_prefix
  2479                                    (not exporter) (not exporter) mono type_enc)
  2488                  ascii_of (not exporter) (not exporter) mono type_enc)
  2480             (0 upto length facts - 1 ~~ facts)),
  2489             (0 upto length facts - 1 ~~ facts)),
  2481        (class_relsN,
  2490        (class_relsN,
  2482         map (formula_line_for_class_rel_clause format type_enc)
  2491         map (formula_line_for_class_rel_clause format type_enc)
  2483             class_rel_clauses),
  2492             class_rel_clauses),
  2484        (aritiesN,
  2493        (aritiesN,
  2485         map (formula_line_for_arity_clause format type_enc) arity_clauses),
  2494         map (formula_line_for_arity_clause format type_enc) arity_clauses),
  2486        (helpersN, helper_lines),
  2495        (helpersN, helper_lines),
  2487        (conjsN,
  2496        (conjsN,
  2488         map (formula_line_for_conjecture ctxt format mono type_enc) conjs),
  2497         map (formula_line_for_conjecture ctxt polym_constrs format mono
       
  2498                                          type_enc) conjs),
  2489        (free_typesN, formula_lines_for_free_types type_enc (facts @ conjs))]
  2499        (free_typesN, formula_lines_for_free_types type_enc (facts @ conjs))]
  2490     val problem =
  2500     val problem =
  2491       problem
  2501       problem
  2492       |> (case format of
  2502       |> (case format of
  2493             CNF => ensure_cnf_problem
  2503             CNF => ensure_cnf_problem