src/HOL/Tools/ATP/atp_problem_generate.ML
changeset 46409 d4754183ccce
parent 46406 0e490b9e8422
child 46410 78ff6a886b50
equal deleted inserted replaced
46408:2520cd337056 46409:d4754183ccce
   101   val trans_lams_from_string :
   101   val trans_lams_from_string :
   102     Proof.context -> type_enc -> string -> term list -> term list * term list
   102     Proof.context -> type_enc -> string -> term list -> term list * term list
   103   val factsN : string
   103   val factsN : string
   104   val prepare_atp_problem :
   104   val prepare_atp_problem :
   105     Proof.context -> atp_format -> formula_kind -> formula_kind -> type_enc
   105     Proof.context -> atp_format -> formula_kind -> formula_kind -> type_enc
   106     -> bool -> string -> bool -> bool -> term list -> term
   106     -> bool -> string -> bool -> bool -> bool -> term list -> term
   107     -> ((string * stature) * term) list
   107     -> ((string * stature) * term) list
   108     -> string problem * string Symtab.table * (string * stature) list vector
   108     -> string problem * string Symtab.table * (string * stature) list vector
   109        * (string * term) list * int Symtab.table
   109        * (string * term) list * int Symtab.table
   110   val atp_problem_weights : string problem -> (string * real) list
   110   val atp_problem_weights : string problem -> (string * real) list
   111 end;
   111 end;
   164 
   164 
   165 val type_decl_prefix = "ty_"
   165 val type_decl_prefix = "ty_"
   166 val sym_decl_prefix = "sy_"
   166 val sym_decl_prefix = "sy_"
   167 val guards_sym_formula_prefix = "gsy_"
   167 val guards_sym_formula_prefix = "gsy_"
   168 val tags_sym_formula_prefix = "tsy_"
   168 val tags_sym_formula_prefix = "tsy_"
   169 val app_op_alias_eq_prefix = "aa_"
   169 val uncurried_alias_eq_prefix = "unc_"
   170 val fact_prefix = "fact_"
   170 val fact_prefix = "fact_"
   171 val conjecture_prefix = "conj_"
   171 val conjecture_prefix = "conj_"
   172 val helper_prefix = "help_"
   172 val helper_prefix = "help_"
   173 val class_rel_clause_prefix = "clar_"
   173 val class_rel_clause_prefix = "clar_"
   174 val arity_clause_prefix = "arity_"
   174 val arity_clause_prefix = "arity_"
   873 
   873 
   874 fun ho_term_for_type_arg format type_enc T =
   874 fun ho_term_for_type_arg format type_enc T =
   875   if T = dummyT then NONE else SOME (ho_term_from_typ format type_enc T)
   875   if T = dummyT then NONE else SOME (ho_term_from_typ format type_enc T)
   876 
   876 
   877 (* This shouldn't clash with anything else. *)
   877 (* This shouldn't clash with anything else. *)
   878 val app_op_alias_sep = "\000"
   878 val uncurried_alias_sep = "\000"
   879 val mangled_type_sep = "\001"
   879 val mangled_type_sep = "\001"
   880 
   880 
   881 val ascii_of_app_op_alias_sep = ascii_of app_op_alias_sep
   881 val ascii_of_uncurried_alias_sep = ascii_of uncurried_alias_sep
   882 
   882 
   883 fun generic_mangled_type_name f (ATerm (name, [])) = f name
   883 fun generic_mangled_type_name f (ATerm (name, [])) = f name
   884   | generic_mangled_type_name f (ATerm (name, tys)) =
   884   | generic_mangled_type_name f (ATerm (name, tys)) =
   885     f name ^ "(" ^ space_implode "," (map (generic_mangled_type_name f) tys)
   885     f name ^ "(" ^ space_implode "," (map (generic_mangled_type_name f) tys)
   886     ^ ")"
   886     ^ ")"
   917 
   917 
   918 fun ho_type_from_typ format type_enc pred_sym ary =
   918 fun ho_type_from_typ format type_enc pred_sym ary =
   919   ho_type_from_ho_term type_enc pred_sym ary
   919   ho_type_from_ho_term type_enc pred_sym ary
   920   o ho_term_from_typ format type_enc
   920   o ho_term_from_typ format type_enc
   921 
   921 
   922 fun aliased_app_op ary (s, s') =
   922 fun aliased_uncurried ary (s, s') =
   923   (s ^ ascii_of_app_op_alias_sep ^ string_of_int ary, s' ^ string_of_int ary)
   923   (s ^ ascii_of_uncurried_alias_sep ^ string_of_int ary, s' ^ string_of_int ary)
   924 fun unaliased_app_op (s, s') =
   924 fun unaliased_uncurried (s, s') =
   925   case space_explode app_op_alias_sep s of
   925   case space_explode uncurried_alias_sep s of
   926     [_] => (s, s')
   926     [_] => (s, s')
   927   | [s1, s2] => (s1, unsuffix s2 s')
   927   | [s1, s2] => (s1, unsuffix s2 s')
   928   | _ => raise Fail "ill-formed explicit application alias"
   928   | _ => raise Fail "ill-formed explicit application alias"
   929 
   929 
   930 fun raw_mangled_const_name type_name ty_args (s, s') =
   930 fun raw_mangled_const_name type_name ty_args (s, s') =
   953            (Scan.error (!! (fn _ => raise Fail ("unrecognized mangled type " ^
   953            (Scan.error (!! (fn _ => raise Fail ("unrecognized mangled type " ^
   954                                                 quote s)) parse_mangled_type))
   954                                                 quote s)) parse_mangled_type))
   955     |> fst
   955     |> fst
   956 
   956 
   957 fun unmangled_const_name s =
   957 fun unmangled_const_name s =
   958   (s, s) |> unaliased_app_op |> fst |> space_explode mangled_type_sep
   958   (s, s) |> unaliased_uncurried |> fst |> space_explode mangled_type_sep
   959 fun unmangled_const s =
   959 fun unmangled_const s =
   960   let val ss = unmangled_const_name s in
   960   let val ss = unmangled_const_name s in
   961     (hd ss, map unmangled_type (tl ss))
   961     (hd ss, map unmangled_type (tl ss))
   962   end
   962   end
   963 
   963 
  1531       IConst (app_op, head_T --> head_T, [arg_T, res_T])
  1531       IConst (app_op, head_T --> head_T, [arg_T, res_T])
  1532       |> mangle_type_args_in_iterm format type_enc
  1532       |> mangle_type_args_in_iterm format type_enc
  1533   in list_app app [head, arg] end
  1533   in list_app app [head, arg] end
  1534 
  1534 
  1535 fun firstorderize_fact thy monom_constrs format type_enc sym_tab
  1535 fun firstorderize_fact thy monom_constrs format type_enc sym_tab
  1536                        app_op_aliases =
  1536                        uncurried_aliases =
  1537   let
  1537   let
  1538     fun do_app arg head = do_app_op format type_enc head arg
  1538     fun do_app arg head = do_app_op format type_enc head arg
  1539     fun list_app_ops head args = fold do_app args head
  1539     fun list_app_ops head args = fold do_app args head
  1540     fun introduce_app_ops tm =
  1540     fun introduce_app_ops tm =
  1541       let val (head, args) = tm |> strip_iterm_comb ||> map introduce_app_ops in
  1541       let val (head, args) = tm |> strip_iterm_comb ||> map introduce_app_ops in
  1542         case head of
  1542         case head of
  1543           IConst (name as (s, _), T, T_args) =>
  1543           IConst (name as (s, _), T, T_args) =>
  1544           if app_op_aliases andalso String.isPrefix const_prefix s then
  1544           if uncurried_aliases andalso String.isPrefix const_prefix s then
  1545             let
  1545             let
  1546               val ary = length args
  1546               val ary = length args
  1547               val name = name |> ary > min_ary_of sym_tab s ? aliased_app_op ary
  1547               val name = name |> ary > min_ary_of sym_tab s
       
  1548                                  ? aliased_uncurried ary
  1548             in list_app (IConst (name, T, T_args)) args end
  1549             in list_app (IConst (name, T, T_args)) args end
  1549           else
  1550           else
  1550             args |> chop (min_ary_of sym_tab s)
  1551             args |> chop (min_ary_of sym_tab s)
  1551                  |>> list_app head |-> list_app_ops
  1552                  |>> list_app head |-> list_app_ops
  1552         | _ => list_app_ops head args
  1553         | _ => list_app_ops head args
  2391                syms []
  2392                syms []
  2392   in mono_lines @ decl_lines end
  2393   in mono_lines @ decl_lines end
  2393 
  2394 
  2394 fun pair_append (xs1, xs2) (ys1, ys2) = (xs1 @ ys1, xs2 @ ys2)
  2395 fun pair_append (xs1, xs2) (ys1, ys2) = (xs1 @ ys1, xs2 @ ys2)
  2395 
  2396 
  2396 fun do_app_op_alias_lines_for_sym ctxt monom_constrs format conj_sym_kind mono
  2397 fun do_uncurried_alias_lines_for_sym ctxt monom_constrs format conj_sym_kind
  2397         type_enc sym_tab0 sym_tab base_s0 types in_conj =
  2398         mono type_enc sym_tab0 sym_tab base_s0 types in_conj =
  2398   let
  2399   let
  2399     fun do_alias ary =
  2400     fun do_alias ary =
  2400       let
  2401       let
  2401         val thy = Proof_Context.theory_of ctxt
  2402         val thy = Proof_Context.theory_of ctxt
  2402         val (kind, maybe_negate) = honor_conj_sym_kind in_conj conj_sym_kind
  2403         val (kind, maybe_negate) = honor_conj_sym_kind in_conj conj_sym_kind
  2410           T_args |> filter_type_args_in_const thy monom_constrs type_enc
  2411           T_args |> filter_type_args_in_const thy monom_constrs type_enc
  2411                                               base_ary base_s
  2412                                               base_ary base_s
  2412         fun do_const name = IConst (name, T, T_args)
  2413         fun do_const name = IConst (name, T, T_args)
  2413         val do_term = ho_term_from_iterm ctxt format mono type_enc (SOME true)
  2414         val do_term = ho_term_from_iterm ctxt format mono type_enc (SOME true)
  2414         val name1 as (s1, _) =
  2415         val name1 as (s1, _) =
  2415           base_name |> ary - 1 > base_ary ? aliased_app_op (ary - 1)
  2416           base_name |> ary - 1 > base_ary ? aliased_uncurried (ary - 1)
  2416         val name2 as (s2, _) = base_name |> aliased_app_op ary
  2417         val name2 as (s2, _) = base_name |> aliased_uncurried ary
  2417         val (arg_Ts, _) = chop_fun ary T
  2418         val (arg_Ts, _) = chop_fun ary T
  2418         val bound_names =
  2419         val bound_names =
  2419           1 upto ary |> map (`I o make_bound_var o string_of_int)
  2420           1 upto ary |> map (`I o make_bound_var o string_of_int)
  2420         val bounds = bound_names ~~ arg_Ts
  2421         val bounds = bound_names ~~ arg_Ts
  2421         val (first_bounds, last_bound) =
  2422         val (first_bounds, last_bound) =
  2429           eq_formula type_enc (atomic_types_of T)
  2430           eq_formula type_enc (atomic_types_of T)
  2430                      (map (apsnd do_bound_type) bounds) false
  2431                      (map (apsnd do_bound_type) bounds) false
  2431                      (do_term tm1) (do_term tm2)
  2432                      (do_term tm1) (do_term tm2)
  2432       in
  2433       in
  2433         ([tm1, tm2],
  2434         ([tm1, tm2],
  2434          [Formula (app_op_alias_eq_prefix ^ s2, kind, eq |> maybe_negate, NONE,
  2435          [Formula (uncurried_alias_eq_prefix ^ s2, kind, eq |> maybe_negate,
  2435                    isabelle_info format spec_eqN helper_rank)])
  2436                    NONE, isabelle_info format spec_eqN helper_rank)])
  2436         |> (if ary - 1 = base_ary orelse Symtab.defined sym_tab s1 then I
  2437         |> (if ary - 1 = base_ary orelse Symtab.defined sym_tab s1 then I
  2437             else pair_append (do_alias (ary - 1)))
  2438             else pair_append (do_alias (ary - 1)))
  2438       end
  2439       end
  2439   in do_alias end
  2440   in do_alias end
  2440 fun app_op_alias_lines_for_sym ctxt monom_constrs format conj_sym_kind mono
  2441 fun uncurried_alias_lines_for_sym ctxt monom_constrs format conj_sym_kind mono
  2441                                type_enc sym_tab0 sym_tab
  2442         type_enc sym_tab0 sym_tab
  2442                                (s, {min_ary, types, in_conj, ...} : sym_info) =
  2443         (s, {min_ary, types, in_conj, ...} : sym_info) =
  2443   case unprefix_and_unascii const_prefix s of
  2444   case unprefix_and_unascii const_prefix s of
  2444     SOME mangled_s =>
  2445     SOME mangled_s =>
  2445     if String.isSubstring app_op_alias_sep mangled_s then
  2446     if String.isSubstring uncurried_alias_sep mangled_s then
  2446       let
  2447       let
  2447         val base_s0 = mangled_s |> unmangled_const_name |> hd |> invert_const
  2448         val base_s0 = mangled_s |> unmangled_const_name |> hd |> invert_const
  2448       in
  2449       in
  2449         do_app_op_alias_lines_for_sym ctxt monom_constrs format conj_sym_kind
  2450         do_uncurried_alias_lines_for_sym ctxt monom_constrs format conj_sym_kind
  2450             mono type_enc sym_tab0 sym_tab base_s0 types in_conj min_ary
  2451             mono type_enc sym_tab0 sym_tab base_s0 types in_conj min_ary
  2451       end
  2452       end
  2452     else
  2453     else
  2453       ([], [])
  2454       ([], [])
  2454   | NONE => ([], [])
  2455   | NONE => ([], [])
  2455 fun app_op_alias_lines_for_sym_table ctxt monom_constrs format conj_sym_kind
  2456 fun uncurried_alias_lines_for_sym_table ctxt monom_constrs format conj_sym_kind
  2456         mono type_enc app_op_aliases sym_tab0 sym_tab =
  2457         mono type_enc uncurried_aliases sym_tab0 sym_tab =
  2457   ([], [])
  2458   ([], [])
  2458   |> app_op_aliases
  2459   |> uncurried_aliases
  2459      ? Symtab.fold_rev
  2460      ? Symtab.fold_rev
  2460            (pair_append
  2461            (pair_append
  2461             o app_op_alias_lines_for_sym ctxt monom_constrs format conj_sym_kind
  2462             o uncurried_alias_lines_for_sym ctxt monom_constrs format
  2462                   mono type_enc sym_tab0 sym_tab) sym_tab
  2463                   conj_sym_kind mono type_enc sym_tab0 sym_tab) sym_tab
  2463 
  2464 
  2464 fun needs_type_tag_idempotence ctxt (Tags (poly, level)) =
  2465 fun needs_type_tag_idempotence ctxt (Tags (poly, level)) =
  2465     Config.get ctxt type_tag_idempotence andalso
  2466     Config.get ctxt type_tag_idempotence andalso
  2466     is_type_level_monotonicity_based level andalso
  2467     is_type_level_monotonicity_based level andalso
  2467     poly <> Mangled_Monomorphic
  2468     poly <> Mangled_Monomorphic
  2468   | needs_type_tag_idempotence _ _ = false
  2469   | needs_type_tag_idempotence _ _ = false
  2469 
  2470 
  2470 val implicit_declsN = "Should-be-implicit typings"
  2471 val implicit_declsN = "Should-be-implicit typings"
  2471 val explicit_declsN = "Explicit typings"
  2472 val explicit_declsN = "Explicit typings"
  2472 val app_op_alias_eqsN = "Application aliases"
  2473 val uncurried_alias_eqsN = "Uncurried aliases"
  2473 val factsN = "Relevant facts"
  2474 val factsN = "Relevant facts"
  2474 val class_relsN = "Class relationships"
  2475 val class_relsN = "Class relationships"
  2475 val aritiesN = "Arities"
  2476 val aritiesN = "Arities"
  2476 val helpersN = "Helper facts"
  2477 val helpersN = "Helper facts"
  2477 val conjsN = "Conjectures"
  2478 val conjsN = "Conjectures"
  2552   |> pairself (map fst)
  2553   |> pairself (map fst)
  2553 
  2554 
  2554 val app_op_threshold = 50
  2555 val app_op_threshold = 50
  2555 
  2556 
  2556 fun prepare_atp_problem ctxt format conj_sym_kind prem_kind type_enc exporter
  2557 fun prepare_atp_problem ctxt format conj_sym_kind prem_kind type_enc exporter
  2557                         lam_trans readable_names preproc hyp_ts concl_t facts =
  2558                         lam_trans uncurried_aliases readable_names preproc
       
  2559                         hyp_ts concl_t facts =
  2558   let
  2560   let
  2559     val thy = Proof_Context.theory_of ctxt
  2561     val thy = Proof_Context.theory_of ctxt
  2560     val type_enc = type_enc |> adjust_type_enc format
  2562     val type_enc = type_enc |> adjust_type_enc format
  2561     (* Forcing explicit applications is expensive for polymorphic encodings,
  2563     (* Forcing explicit applications is expensive for polymorphic encodings,
  2562        because it takes only one existential variable ranging over "'a => 'b" to
  2564        because it takes only one existential variable ranging over "'a => 'b" to
  2566       if polymorphism_of_type_enc type_enc = Polymorphic andalso
  2568       if polymorphism_of_type_enc type_enc = Polymorphic andalso
  2567          length facts >= app_op_threshold then
  2569          length facts >= app_op_threshold then
  2568         Incomplete_Apply
  2570         Incomplete_Apply
  2569       else
  2571       else
  2570         Sufficient_Apply
  2572         Sufficient_Apply
  2571     val app_op_aliases = (format = DFG DFG_Sorted)
       
  2572     val lam_trans =
  2573     val lam_trans =
  2573       if lam_trans = keep_lamsN andalso
  2574       if lam_trans = keep_lamsN andalso
  2574          not (is_type_enc_higher_order type_enc) then
  2575          not (is_type_enc_higher_order type_enc) then
  2575         error ("Lambda translation scheme incompatible with first-order \
  2576         error ("Lambda translation scheme incompatible with first-order \
  2576                \encoding.")
  2577                \encoding.")
  2585     val (polym_constrs, monom_constrs) =
  2586     val (polym_constrs, monom_constrs) =
  2586       all_constrs_of_polymorphic_datatypes thy
  2587       all_constrs_of_polymorphic_datatypes thy
  2587       |>> map (make_fixed_const (SOME format))
  2588       |>> map (make_fixed_const (SOME format))
  2588     fun firstorderize in_helper =
  2589     fun firstorderize in_helper =
  2589       firstorderize_fact thy monom_constrs format type_enc sym_tab0
  2590       firstorderize_fact thy monom_constrs format type_enc sym_tab0
  2590                          (app_op_aliases andalso not in_helper)
  2591                          (uncurried_aliases andalso not in_helper)
  2591     val (conjs, facts) = (conjs, facts) |> pairself (map (firstorderize false))
  2592     val (conjs, facts) = (conjs, facts) |> pairself (map (firstorderize false))
  2592     val sym_tab = sym_table_for_facts ctxt type_enc Incomplete_Apply conjs facts
  2593     val sym_tab = sym_table_for_facts ctxt type_enc Incomplete_Apply conjs facts
  2593     val helpers =
  2594     val helpers =
  2594       sym_tab |> helper_facts_for_sym_table ctxt format type_enc
  2595       sym_tab |> helper_facts_for_sym_table ctxt format type_enc
  2595               |> map (firstorderize true)
  2596               |> map (firstorderize true)
  2596     val mono_Ts =
  2597     val mono_Ts =
  2597       helpers @ conjs @ facts |> monotonic_types_for_facts ctxt mono type_enc
  2598       helpers @ conjs @ facts |> monotonic_types_for_facts ctxt mono type_enc
  2598     val class_decl_lines = decl_lines_for_classes type_enc classes
  2599     val class_decl_lines = decl_lines_for_classes type_enc classes
  2599     val (app_op_alias_eq_tms, app_op_alias_eq_lines) =
  2600     val (uncurried_alias_eq_tms, uncurried_alias_eq_lines) =
  2600       app_op_alias_lines_for_sym_table ctxt monom_constrs format conj_sym_kind
  2601       uncurried_alias_lines_for_sym_table ctxt monom_constrs format
  2601           mono type_enc app_op_aliases sym_tab0 sym_tab
  2602           conj_sym_kind mono type_enc uncurried_aliases sym_tab0 sym_tab
  2602     val sym_decl_lines =
  2603     val sym_decl_lines =
  2603       (conjs, helpers @ facts, app_op_alias_eq_tms)
  2604       (conjs, helpers @ facts, uncurried_alias_eq_tms)
  2604       |> sym_decl_table_for_facts ctxt format type_enc sym_tab
  2605       |> sym_decl_table_for_facts ctxt format type_enc sym_tab
  2605       |> problem_lines_for_sym_decl_table ctxt format conj_sym_kind mono
  2606       |> problem_lines_for_sym_decl_table ctxt format conj_sym_kind mono
  2606                                                type_enc mono_Ts
  2607                                                type_enc mono_Ts
  2607     val num_facts = length facts
  2608     val num_facts = length facts
  2608     val fact_lines =
  2609     val fact_lines =
  2620             I)
  2621             I)
  2621     (* Reordering these might confuse the proof reconstruction code or the SPASS
  2622     (* Reordering these might confuse the proof reconstruction code or the SPASS
  2622        FLOTTER hack. *)
  2623        FLOTTER hack. *)
  2623     val problem =
  2624     val problem =
  2624       [(explicit_declsN, class_decl_lines @ sym_decl_lines),
  2625       [(explicit_declsN, class_decl_lines @ sym_decl_lines),
  2625        (app_op_alias_eqsN, app_op_alias_eq_lines),
  2626        (uncurried_alias_eqsN, uncurried_alias_eq_lines),
  2626        (factsN, fact_lines),
  2627        (factsN, fact_lines),
  2627        (class_relsN,
  2628        (class_relsN,
  2628         map (formula_line_for_class_rel_clause format type_enc)
  2629         map (formula_line_for_class_rel_clause format type_enc)
  2629             class_rel_clauses),
  2630             class_rel_clauses),
  2630        (aritiesN,
  2631        (aritiesN,