2015 | string_of_status Rec_Def = rec_defN |
2016 | string_of_status Rec_Def = rec_defN |
2016 |
2017 |
2017 (* Each fact is given a unique fact number to avoid name clashes (e.g., because |
2018 (* Each fact is given a unique fact number to avoid name clashes (e.g., because |
2018 of monomorphization). The TPTP forbids name clashes, and some of the remote |
2019 of monomorphization). The TPTP forbids name clashes, and some of the remote |
2019 provers might care. *) |
2020 provers might care. *) |
2020 fun line_of_fact ctxt prefix encode alt freshen pos mono type_enc rank |
2021 fun line_of_fact ctxt generate_info prefix encode alt freshen pos mono type_enc rank |
2021 (j, {name, stature = (_, status), role, iformula, atomic_types}) = |
2022 (j, {name, stature = (_, status), role, iformula, atomic_types}) = |
2022 Formula ((prefix ^ (if freshen then string_of_int j ^ "_" else "") ^ |
2023 Formula ((prefix ^ (if freshen then string_of_int j ^ "_" else "") ^ |
2023 encode name, alt name), |
2024 encode name, alt name), |
2024 role, |
2025 role, |
2025 iformula |
2026 iformula |
2026 |> formula_of_iformula ctxt mono type_enc |
2027 |> formula_of_iformula ctxt mono type_enc |
2027 should_guard_var_in_formula (if pos then SOME true else NONE) |
2028 should_guard_var_in_formula (if pos then SOME true else NONE) |
2028 |> close_formula_universally |
2029 |> close_formula_universally |
2029 |> bound_tvars type_enc true atomic_types, |
2030 |> bound_tvars type_enc true atomic_types, |
2030 NONE, isabelle_info (string_of_status status) (rank j)) |
2031 NONE, isabelle_info generate_info (string_of_status status) (rank j)) |
2031 |
2032 |
2032 fun lines_of_subclass type_enc sub super = |
2033 fun lines_of_subclass generate_info type_enc sub super = |
2033 Formula ((subclass_prefix ^ ascii_of sub ^ "___" ^ ascii_of super, ""), Axiom, |
2034 Formula ((subclass_prefix ^ ascii_of sub ^ "___" ^ ascii_of super, ""), Axiom, |
2034 AConn (AImplies, |
2035 AConn (AImplies, |
2035 [sub, super] |> map (fn s => class_atom type_enc (s, tvar_a))) |
2036 [sub, super] |> map (fn s => class_atom type_enc (s, tvar_a))) |
2036 |> bound_tvars type_enc false [tvar_a], |
2037 |> bound_tvars type_enc false [tvar_a], |
2037 NONE, isabelle_info inductiveN helper_rank) |
2038 NONE, isabelle_info generate_info inductiveN helper_rank) |
2038 |
2039 |
2039 fun lines_of_subclass_pair type_enc (sub, supers) = |
2040 fun lines_of_subclass_pair generate_info type_enc (sub, supers) = |
2040 if polymorphism_of_type_enc type_enc = Type_Class_Polymorphic then |
2041 if polymorphism_of_type_enc type_enc = Type_Class_Polymorphic then |
2041 [Class_Decl (class_decl_prefix ^ ascii_of sub, `make_class sub, |
2042 [Class_Decl (class_decl_prefix ^ ascii_of sub, `make_class sub, map (`make_class) supers)] |
2042 map (`make_class) supers)] |
|
2043 else |
2043 else |
2044 map (lines_of_subclass type_enc sub) supers |
2044 map (lines_of_subclass generate_info type_enc sub) supers |
2045 |
2045 |
2046 fun line_of_tcon_clause type_enc (name, prems, (cl, T)) = |
2046 fun line_of_tcon_clause generate_info type_enc (name, prems, (cl, T)) = |
2047 if polymorphism_of_type_enc type_enc = Type_Class_Polymorphic then |
2047 if polymorphism_of_type_enc type_enc = Type_Class_Polymorphic then |
2048 Class_Memb (class_memb_prefix ^ name, |
2048 Class_Memb (class_memb_prefix ^ name, |
2049 map (fn (cls, T) => (T |> dest_TVar |> tvar_name, map (`make_class) cls)) prems, |
2049 map (fn (cls, T) => (T |> dest_TVar |> tvar_name, map (`make_class) cls)) prems, |
2050 native_atp_type_of_typ type_enc false 0 T, `make_class cl) |
2050 native_atp_type_of_typ type_enc false 0 T, `make_class cl) |
2051 else |
2051 else |
2052 Formula ((tcon_clause_prefix ^ name, ""), Axiom, |
2052 Formula ((tcon_clause_prefix ^ name, ""), Axiom, |
2053 mk_ahorn (maps (class_atoms type_enc) prems) |
2053 mk_ahorn (maps (class_atoms type_enc) prems) |
2054 (class_atom type_enc (cl, T)) |
2054 (class_atom type_enc (cl, T)) |
2055 |> bound_tvars type_enc true (snd (dest_Type T)), |
2055 |> bound_tvars type_enc true (snd (dest_Type T)), |
2056 NONE, isabelle_info inductiveN helper_rank) |
2056 NONE, isabelle_info generate_info inductiveN helper_rank) |
2057 |
2057 |
2058 fun line_of_conjecture ctxt mono type_enc |
2058 fun line_of_conjecture ctxt mono type_enc ({name, role, iformula, atomic_types, ...} : ifact) = |
2059 ({name, role, iformula, atomic_types, ...} : ifact) = |
|
2060 Formula ((conjecture_prefix ^ name, ""), role, |
2059 Formula ((conjecture_prefix ^ name, ""), role, |
2061 iformula |
2060 iformula |
2062 |> formula_of_iformula ctxt mono type_enc |
2061 |> formula_of_iformula ctxt mono type_enc should_guard_var_in_formula (SOME false) |
2063 should_guard_var_in_formula (SOME false) |
|
2064 |> close_formula_universally |
2062 |> close_formula_universally |
2065 |> bound_tvars type_enc true atomic_types, NONE, []) |
2063 |> bound_tvars type_enc true atomic_types, NONE, []) |
2066 |
2064 |
2067 fun lines_of_free_types type_enc (facts : ifact list) = |
2065 fun lines_of_free_types type_enc (facts : ifact list) = |
2068 if is_type_enc_polymorphic type_enc then |
2066 if is_type_enc_polymorphic type_enc then |
2228 [] |> (is_type_enc_polymorphic type_enc andalso |
2226 [] |> (is_type_enc_polymorphic type_enc andalso |
2229 is_type_level_monotonicity_based level) |
2227 is_type_level_monotonicity_based level) |
2230 ? fold (add_fact_monotonic_types ctxt mono type_enc) facts |
2228 ? fold (add_fact_monotonic_types ctxt mono type_enc) facts |
2231 end |
2229 end |
2232 |
2230 |
2233 fun line_of_guards_mono_type ctxt mono type_enc T = |
2231 fun line_of_guards_mono_type ctxt generate_info mono type_enc T = |
2234 Formula ((guards_sym_formula_prefix ^ ascii_of (mangled_type type_enc T), ""), |
2232 Formula ((guards_sym_formula_prefix ^ ascii_of (mangled_type type_enc T), ""), |
2235 Axiom, |
2233 Axiom, |
2236 IConst (`make_bound_var "X", T, []) |
2234 IConst (`make_bound_var "X", T, []) |
2237 |> type_guard_iterm type_enc T |
2235 |> type_guard_iterm type_enc T |
2238 |> AAtom |
2236 |> AAtom |
2239 |> formula_of_iformula ctxt mono type_enc always_guard_var_in_formula |
2237 |> formula_of_iformula ctxt mono type_enc always_guard_var_in_formula |
2240 (SOME true) |
2238 (SOME true) |
2241 |> close_formula_universally |
2239 |> close_formula_universally |
2242 |> bound_tvars type_enc true (atomic_types_of T), |
2240 |> bound_tvars type_enc true (atomic_types_of T), |
2243 NONE, isabelle_info inductiveN helper_rank) |
2241 NONE, isabelle_info generate_info inductiveN helper_rank) |
2244 |
2242 |
2245 fun line_of_tags_mono_type ctxt mono type_enc T = |
2243 fun line_of_tags_mono_type ctxt generate_info mono type_enc T = |
2246 let val x_var = ATerm ((`make_bound_var "X", []), []) in |
2244 let val x_var = ATerm ((`make_bound_var "X", []), []) in |
2247 Formula ((tags_sym_formula_prefix ^ ascii_of (mangled_type type_enc T), ""), Axiom, |
2245 Formula ((tags_sym_formula_prefix ^ ascii_of (mangled_type type_enc T), ""), Axiom, |
2248 eq_formula type_enc (atomic_types_of T) [] false |
2246 eq_formula type_enc (atomic_types_of T) [] false |
2249 (tag_with_type ctxt mono type_enc NONE T x_var) x_var, |
2247 (tag_with_type ctxt mono type_enc NONE T x_var) x_var, |
2250 NONE, isabelle_info non_rec_defN helper_rank) |
2248 NONE, isabelle_info generate_info non_rec_defN helper_rank) |
2251 end |
2249 end |
2252 |
2250 |
2253 fun lines_of_mono_types ctxt mono type_enc = |
2251 fun lines_of_mono_types ctxt generate_info mono type_enc = |
2254 (case type_enc of |
2252 (case type_enc of |
2255 Native _ => K [] |
2253 Native _ => K [] |
2256 | Guards _ => map (line_of_guards_mono_type ctxt mono type_enc) |
2254 | Guards _ => map (line_of_guards_mono_type ctxt generate_info mono type_enc) |
2257 | Tags _ => map (line_of_tags_mono_type ctxt mono type_enc)) |
2255 | Tags _ => map (line_of_tags_mono_type ctxt generate_info mono type_enc)) |
2258 |
2256 |
2259 fun decl_line_of_sym ctxt mono type_enc s (s', T_args, T, pred_sym, ary, _) = |
2257 fun decl_line_of_sym ctxt mono type_enc s (s', T_args, T, pred_sym, ary, _) = |
2260 let |
2258 let |
2261 val thy = Proof_Context.theory_of ctxt |
2259 val thy = Proof_Context.theory_of ctxt |
2262 val (T, T_args) = |
2260 val (T, T_args) = |
2308 |> formula_of_iformula ctxt mono type_enc |
2306 |> formula_of_iformula ctxt mono type_enc |
2309 always_guard_var_in_formula (SOME true) |
2307 always_guard_var_in_formula (SOME true) |
2310 |> close_formula_universally |
2308 |> close_formula_universally |
2311 |> bound_tvars type_enc (n > 1) (atomic_types_of T) |
2309 |> bound_tvars type_enc (n > 1) (atomic_types_of T) |
2312 |> maybe_negate, |
2310 |> maybe_negate, |
2313 NONE, isabelle_info inductiveN helper_rank) |
2311 NONE, isabelle_info generate_info inductiveN helper_rank) |
2314 end |
2312 end |
2315 |
2313 |
2316 fun lines_of_tags_sym_decl ctxt mono type_enc n s |
2314 fun lines_of_tags_sym_decl ctxt generate_info mono type_enc n s |
2317 (j, (s', T_args, T, pred_sym, ary, in_conj)) = |
2315 (j, (s', T_args, T, pred_sym, ary, in_conj)) = |
2318 let |
2316 let |
2319 val thy = Proof_Context.theory_of ctxt |
2317 val thy = Proof_Context.theory_of ctxt |
2320 val level = level_of_type_enc type_enc |
2318 val level = level_of_type_enc type_enc |
2321 val ident = |
2319 val ident = |
2322 tags_sym_formula_prefix ^ s ^ |
2320 tags_sym_formula_prefix ^ s ^ |
2353 if forall (type_generalization thy T o result_type_of_decl) decls' then [decl] |
2351 if forall (type_generalization thy T o result_type_of_decl) decls' then [decl] |
2354 else decls |
2352 else decls |
2355 end |
2353 end |
2356 | rationalize_decls _ decls = decls |
2354 | rationalize_decls _ decls = decls |
2357 |
2355 |
2358 fun lines_of_sym_decls ctxt mono type_enc (s, decls) = |
2356 fun lines_of_sym_decls ctxt generate_info mono type_enc (s, decls) = |
2359 (case type_enc of |
2357 (case type_enc of |
2360 Native _ => [decl_line_of_sym ctxt mono type_enc s (hd decls)] |
2358 Native _ => [decl_line_of_sym ctxt mono type_enc s (hd decls)] |
2361 | Guards (_, level) => |
2359 | Guards (_, level) => |
2362 let |
2360 let |
2363 val thy = Proof_Context.theory_of ctxt |
2361 val thy = Proof_Context.theory_of ctxt |
2364 val decls = decls |> rationalize_decls thy |
2362 val decls = decls |> rationalize_decls thy |
2365 val n = length decls |
2363 val n = length decls |
2366 val decls = decls |> filter (should_encode_type ctxt mono level o result_type_of_decl) |
2364 val decls = decls |> filter (should_encode_type ctxt mono level o result_type_of_decl) |
2367 in |
2365 in |
2368 (0 upto length decls - 1, decls) |-> map2 (line_of_guards_sym_decl ctxt mono type_enc n s) |
2366 (0 upto length decls - 1, decls) |
|
2367 |-> map2 (line_of_guards_sym_decl ctxt generate_info mono type_enc n s) |
2369 end |
2368 end |
2370 | Tags (_, level) => |
2369 | Tags (_, level) => |
2371 if is_type_level_uniform level then |
2370 if is_type_level_uniform level then |
2372 [] |
2371 [] |
2373 else |
2372 else |
2374 let val n = length decls in |
2373 let val n = length decls in |
2375 (0 upto n - 1 ~~ decls) |> maps (lines_of_tags_sym_decl ctxt mono type_enc n s) |
2374 (0 upto n - 1 ~~ decls) |
|
2375 |> maps (lines_of_tags_sym_decl ctxt generate_info mono type_enc n s) |
2376 end) |
2376 end) |
2377 |
2377 |
2378 fun lines_of_sym_decl_table ctxt mono type_enc mono_Ts sym_decl_tab = |
2378 fun lines_of_sym_decl_table ctxt generate_info mono type_enc mono_Ts sym_decl_tab = |
2379 let |
2379 let |
2380 val syms = sym_decl_tab |> Symtab.dest |> sort_by fst |
2380 val syms = sym_decl_tab |> Symtab.dest |> sort_by fst |
2381 val mono_lines = lines_of_mono_types ctxt mono type_enc mono_Ts |
2381 val mono_lines = lines_of_mono_types ctxt generate_info mono type_enc mono_Ts |
2382 val decl_lines = maps (lines_of_sym_decls ctxt mono type_enc) syms |
2382 val decl_lines = maps (lines_of_sym_decls ctxt generate_info mono type_enc) syms |
2383 in mono_lines @ decl_lines end |
2383 in mono_lines @ decl_lines end |
2384 |
2384 |
2385 fun datatypes_of_sym_table ctxt ctrss (DFG Polymorphic) (type_enc as Native _) uncurried_aliases |
2385 fun datatypes_of_sym_table ctxt ctrss (DFG Polymorphic) (type_enc as Native _) uncurried_aliases |
2386 sym_tab = |
2386 sym_tab = |
2387 if is_type_enc_polymorphic type_enc then |
2387 if is_type_enc_polymorphic type_enc then |
2462 eq_formula type_enc (atomic_types_of T) (map (apsnd do_bound_type) bounds) false |
2462 eq_formula type_enc (atomic_types_of T) (map (apsnd do_bound_type) bounds) false |
2463 (atp_term_of tm1) (atp_term_of tm2) |
2463 (atp_term_of tm1) (atp_term_of tm2) |
2464 in |
2464 in |
2465 ([tm1, tm2], |
2465 ([tm1, tm2], |
2466 [Formula ((uncurried_alias_eq_prefix ^ s2, ""), role, eq |> maybe_negate, NONE, |
2466 [Formula ((uncurried_alias_eq_prefix ^ s2, ""), role, eq |> maybe_negate, NONE, |
2467 isabelle_info non_rec_defN helper_rank)]) |
2467 isabelle_info generate_info non_rec_defN helper_rank)]) |
2468 |> (if ary - 1 = base_ary orelse Symtab.defined sym_tab s1 then I |
2468 |> (if ary - 1 = base_ary orelse Symtab.defined sym_tab s1 then I |
2469 else pair_append (do_alias (ary - 1))) |
2469 else pair_append (do_alias (ary - 1))) |
2470 end |
2470 end |
2471 in do_alias end |
2471 in do_alias end |
2472 |
2472 |
2473 fun uncurried_alias_lines_of_sym ctxt ctrss mono type_enc sym_tab0 sym_tab |
2473 fun uncurried_alias_lines_of_sym ctxt generate_info ctrss mono type_enc sym_tab0 sym_tab |
2474 (s, {min_ary, types, in_conj, ...} : sym_info) = |
2474 (s, {min_ary, types, in_conj, ...} : sym_info) = |
2475 (case unprefix_and_unascii const_prefix s of |
2475 (case unprefix_and_unascii const_prefix s of |
2476 SOME mangled_s => |
2476 SOME mangled_s => |
2477 if String.isSubstring uncurried_alias_sep mangled_s then |
2477 if String.isSubstring uncurried_alias_sep mangled_s then |
2478 let val base_s0 = mangled_s |> unmangled_invert_const in |
2478 let val base_s0 = mangled_s |> unmangled_invert_const in |
2479 do_uncurried_alias_lines_of_sym ctxt ctrss mono type_enc sym_tab0 sym_tab base_s0 types |
2479 do_uncurried_alias_lines_of_sym ctxt generate_info ctrss mono type_enc sym_tab0 sym_tab |
2480 in_conj min_ary |
2480 base_s0 types in_conj min_ary |
2481 end |
2481 end |
2482 else |
2482 else |
2483 ([], []) |
2483 ([], []) |
2484 | NONE => ([], [])) |
2484 | NONE => ([], [])) |
2485 |
2485 |
2486 fun uncurried_alias_lines_of_sym_table ctxt ctrss mono type_enc uncurried_aliases sym_tab0 sym_tab = |
2486 fun uncurried_alias_lines_of_sym_table ctxt generate_info ctrss mono type_enc uncurried_aliases |
|
2487 sym_tab0 sym_tab = |
2487 ([], []) |
2488 ([], []) |
2488 |> uncurried_aliases |
2489 |> uncurried_aliases |
2489 ? Symtab.fold_rev |
2490 ? Symtab.fold_rev (pair_append |
2490 (pair_append o uncurried_alias_lines_of_sym ctxt ctrss mono type_enc sym_tab0 sym_tab) |
2491 o uncurried_alias_lines_of_sym ctxt generate_info ctrss mono type_enc sym_tab0 sym_tab) |
2491 sym_tab |
2492 sym_tab |
2492 |
2493 |
2493 val implicit_declsN = "Could-be-implicit typings" |
2494 val implicit_declsN = "Could-be-implicit typings" |
2494 val explicit_declsN = "Explicit typings" |
2495 val explicit_declsN = "Explicit typings" |
2495 val uncurried_alias_eqsN = "Uncurried aliases" |
2496 val uncurried_alias_eqsN = "Uncurried aliases" |
2496 val factsN = "Relevant facts" |
2497 val factsN = "Relevant facts" |
2608 val datatypes = datatypes_of_sym_table ctxt ctrss format type_enc uncurried_aliases sym_tab |
2610 val datatypes = datatypes_of_sym_table ctxt ctrss format type_enc uncurried_aliases sym_tab |
2609 val class_decl_lines = decl_lines_of_classes type_enc classes |
2611 val class_decl_lines = decl_lines_of_classes type_enc classes |
2610 val sym_decl_lines = |
2612 val sym_decl_lines = |
2611 (conjs, helpers @ facts, uncurried_alias_eq_tms) |
2613 (conjs, helpers @ facts, uncurried_alias_eq_tms) |
2612 |> sym_decl_table_of_facts thy type_enc sym_tab |
2614 |> sym_decl_table_of_facts thy type_enc sym_tab |
2613 |> lines_of_sym_decl_table ctxt mono type_enc mono_Ts |
2615 |> lines_of_sym_decl_table ctxt generate_info mono type_enc mono_Ts |
2614 val datatype_decl_lines = map decl_line_of_datatype datatypes |
2616 val datatype_decl_lines = map decl_line_of_datatype datatypes |
2615 val decl_lines = class_decl_lines @ sym_decl_lines @ datatype_decl_lines |
2617 val decl_lines = class_decl_lines @ sym_decl_lines @ datatype_decl_lines |
2616 val num_facts = length facts |
2618 val num_facts = length facts |
2617 val freshen = mode <> Exporter andalso mode <> Translator |
2619 val freshen = mode <> Exporter andalso mode <> Translator |
2618 val pos = mode <> Exporter |
2620 val pos = mode <> Exporter |
2619 val rank_of = rank_of_fact_num num_facts |
2621 val rank_of = rank_of_fact_num num_facts |
2620 val fact_lines = |
2622 val fact_lines = |
2621 map (line_of_fact ctxt fact_prefix ascii_of I freshen pos mono type_enc rank_of) |
2623 map (line_of_fact ctxt generate_info fact_prefix ascii_of I freshen pos mono type_enc rank_of) |
2622 (0 upto num_facts - 1 ~~ facts) |
2624 (0 upto num_facts - 1 ~~ facts) |
2623 val subclass_lines = maps (lines_of_subclass_pair type_enc) subclass_pairs |
2625 val subclass_lines = maps (lines_of_subclass_pair generate_info type_enc) subclass_pairs |
2624 val tcon_lines = map (line_of_tcon_clause type_enc) tcon_clauses |
2626 val tcon_lines = map (line_of_tcon_clause generate_info type_enc) tcon_clauses |
2625 val helper_lines = |
2627 val helper_lines = |
2626 0 upto length helpers - 1 ~~ helpers |
2628 0 upto length helpers - 1 ~~ helpers |
2627 |> map (line_of_fact ctxt helper_prefix I (K "") false true mono type_enc (K default_rank)) |
2629 |> map (line_of_fact ctxt generate_info helper_prefix I (K "") false true mono type_enc |
|
2630 (K default_rank)) |
2628 val free_type_lines = lines_of_free_types type_enc (facts @ conjs) |
2631 val free_type_lines = lines_of_free_types type_enc (facts @ conjs) |
2629 val conj_lines = map (line_of_conjecture ctxt mono type_enc) conjs |
2632 val conj_lines = map (line_of_conjecture ctxt mono type_enc) conjs |
2630 (* Reordering these might confuse the proof reconstruction code. *) |
2633 (* Reordering these might confuse the proof reconstruction code. *) |
2631 val problem = |
2634 val problem = |
2632 [(explicit_declsN, decl_lines), |
2635 [(explicit_declsN, decl_lines), |