src/HOL/Tools/Sledgehammer/sledgehammer_atp_translate.ML
changeset 42690 4d29b4785f43
parent 42689 e38590764c34
child 42691 6efda6167e5d
equal deleted inserted replaced
42689:e38590764c34 42690:4d29b4785f43
   635              |>> list_app head
   635              |>> list_app head
   636              |-> list_explicit_app
   636              |-> list_explicit_app
   637       | (head, args) => list_explicit_app head (map aux args)
   637       | (head, args) => list_explicit_app head (map aux args)
   638   in aux end
   638   in aux end
   639 
   639 
   640 fun impose_type_arg_policy_in_combterm ctxt nonmono_Ts type_sys =
   640 fun impose_type_arg_policy_in_combterm type_sys =
   641   let
   641   let
   642     fun aux (CombApp tmp) = CombApp (pairself aux tmp)
   642     fun aux (CombApp tmp) = CombApp (pairself aux tmp)
   643       | aux (CombConst (name as (s, _), T, T_args)) =
   643       | aux (CombConst (name as (s, _), T, T_args)) =
   644         let
   644         (case strip_prefix_and_unascii const_prefix s of
   645           val level = level_of_type_sys type_sys
   645            NONE => (name, T_args)
   646           val (T, T_args) =
   646          | SOME s'' =>
   647             (* avoid needless identical homogenized versions of "hAPP" *)
   647            let val s'' = invert_const s'' in
   648             if s = const_prefix ^ explicit_app_base then
   648              case type_arg_policy type_sys s'' of
   649               T_args |> map (homogenized_type ctxt nonmono_Ts level)
   649                No_Type_Args => (name, [])
   650                      |> (fn Ts => let val T = hd Ts --> nth Ts 1 in
   650              | Explicit_Type_Args => (name, T_args)
   651                                     (T --> T, Ts)
   651              | Mangled_Type_Args => (mangled_const_name T_args name, [])
   652                                   end)
   652            end)
   653             else
   653         |> (fn (name, T_args) => CombConst (name, T, T_args))
   654               (T, T_args)
       
   655         in
       
   656           (case strip_prefix_and_unascii const_prefix s of
       
   657              NONE => (name, T_args)
       
   658            | SOME s'' =>
       
   659              let val s'' = invert_const s'' in
       
   660                case type_arg_policy type_sys s'' of
       
   661                  No_Type_Args => (name, [])
       
   662                | Explicit_Type_Args => (name, T_args)
       
   663                | Mangled_Type_Args => (mangled_const_name T_args name, [])
       
   664              end)
       
   665           |> (fn (name, T_args) => CombConst (name, T, T_args))
       
   666         end
       
   667       | aux tm = tm
   654       | aux tm = tm
   668   in aux end
   655   in aux end
   669 
   656 
   670 fun repair_combterm ctxt nonmono_Ts type_sys sym_tab =
   657 fun repair_combterm type_sys sym_tab =
   671   introduce_explicit_apps_in_combterm sym_tab
   658   introduce_explicit_apps_in_combterm sym_tab
   672   #> introduce_predicators_in_combterm sym_tab
   659   #> introduce_predicators_in_combterm sym_tab
   673   #> impose_type_arg_policy_in_combterm ctxt nonmono_Ts type_sys
   660   #> impose_type_arg_policy_in_combterm type_sys
   674 fun repair_fact ctxt nonmono_Ts type_sys sym_tab =
   661 fun repair_fact type_sys sym_tab =
   675   update_combformula (formula_map
   662   update_combformula (formula_map (repair_combterm type_sys sym_tab))
   676       (repair_combterm ctxt nonmono_Ts type_sys sym_tab))
       
   677 
   663 
   678 (** Helper facts **)
   664 (** Helper facts **)
   679 
   665 
   680 fun ti_ti_helper_fact () =
   666 fun ti_ti_helper_fact () =
   681   let
   667   let
   755   | fo_literal_from_type_literal (TyLitFree (class, name)) =
   741   | fo_literal_from_type_literal (TyLitFree (class, name)) =
   756     (true, ATerm (class, [ATerm (name, [])]))
   742     (true, ATerm (class, [ATerm (name, [])]))
   757 
   743 
   758 fun formula_from_fo_literal (pos, t) = AAtom t |> not pos ? mk_anot
   744 fun formula_from_fo_literal (pos, t) = AAtom t |> not pos ? mk_anot
   759 
   745 
   760 fun type_pred_combatom ctxt nonmono_Ts type_sys T tm =
   746 fun type_pred_combatom type_sys T tm =
   761   CombApp (CombConst (`make_fixed_const type_pred_base, T --> @{typ bool}, [T]),
   747   CombApp (CombConst (`make_fixed_const type_pred_base, T --> @{typ bool}, [T]),
   762            tm)
   748            tm)
   763   |> impose_type_arg_policy_in_combterm ctxt nonmono_Ts type_sys
   749   |> impose_type_arg_policy_in_combterm type_sys
   764   |> AAtom
   750   |> AAtom
   765 
   751 
   766 fun formula_from_combformula ctxt nonmono_Ts type_sys =
   752 fun formula_from_combformula ctxt nonmono_Ts type_sys =
   767   let
   753   let
   768     fun tag_with_type type_sys T tm =
   754     fun tag_with_type type_sys T tm =
   769       CombConst (`make_fixed_const type_tag_name, T --> T, [T])
   755       CombConst (`make_fixed_const type_tag_name, T --> T, [T])
   770       |> impose_type_arg_policy_in_combterm ctxt nonmono_Ts type_sys
   756       |> impose_type_arg_policy_in_combterm type_sys
   771       |> do_term true
   757       |> do_term true
   772       |> (fn ATerm (s, tms) => ATerm (s, tms @ [tm]))
   758       |> (fn ATerm (s, tms) => ATerm (s, tms @ [tm]))
   773     and do_term top_level u =
   759     and do_term top_level u =
   774       let
   760       let
   775         val (head, args) = strip_combterm_comb u
   761         val (head, args) = strip_combterm_comb u
   793         Simple level =>
   779         Simple level =>
   794         SOME o mangled_type_name o homogenized_type ctxt nonmono_Ts level
   780         SOME o mangled_type_name o homogenized_type ctxt nonmono_Ts level
   795       | _ => K NONE
   781       | _ => K NONE
   796     fun do_out_of_bound_type (s, T) =
   782     fun do_out_of_bound_type (s, T) =
   797       if should_predicate_on_type ctxt nonmono_Ts type_sys T then
   783       if should_predicate_on_type ctxt nonmono_Ts type_sys T then
   798         type_pred_combatom ctxt nonmono_Ts type_sys T (CombVar (s, T))
   784         type_pred_combatom type_sys T (CombVar (s, T))
   799         |> do_formula |> SOME
   785         |> do_formula |> SOME
   800       else
   786       else
   801         NONE
   787         NONE
   802     and do_formula (AQuant (q, xs, phi)) =
   788     and do_formula (AQuant (q, xs, phi)) =
   803         AQuant (q, xs |> map (apsnd (fn NONE => NONE
   789         AQuant (q, xs |> map (apsnd (fn NONE => NONE
   976   in
   962   in
   977     Formula (sym_decl_prefix ^ s ^
   963     Formula (sym_decl_prefix ^ s ^
   978              (if n > 1 then "_" ^ string_of_int j else ""), Axiom,
   964              (if n > 1 then "_" ^ string_of_int j else ""), Axiom,
   979              CombConst ((s, s'), T, T_args)
   965              CombConst ((s, s'), T, T_args)
   980              |> fold (curry (CombApp o swap)) bound_tms
   966              |> fold (curry (CombApp o swap)) bound_tms
   981              |> type_pred_combatom ctxt nonmono_Ts type_sys res_T
   967              |> type_pred_combatom type_sys res_T
   982              |> mk_aquant AForall (bound_names ~~ bound_Ts)
   968              |> mk_aquant AForall (bound_names ~~ bound_Ts)
   983              |> formula_from_combformula ctxt nonmono_Ts type_sys
   969              |> formula_from_combformula ctxt nonmono_Ts type_sys
   984              |> close_formula_universally,
   970              |> close_formula_universally,
   985              NONE, NONE)
   971              NONE, NONE)
   986   end
   972   end
  1051     val (fact_names, (conjs, facts, class_rel_clauses, arity_clauses)) =
  1037     val (fact_names, (conjs, facts, class_rel_clauses, arity_clauses)) =
  1052       translate_formulas ctxt type_sys hyp_ts concl_t facts
  1038       translate_formulas ctxt type_sys hyp_ts concl_t facts
  1053     val sym_tab = conjs @ facts |> sym_table_for_facts explicit_apply
  1039     val sym_tab = conjs @ facts |> sym_table_for_facts explicit_apply
  1054     val nonmono_Ts =
  1040     val nonmono_Ts =
  1055       [] |> fold (add_nonmonotonic_types_for_facts ctxt type_sys) [facts, conjs]
  1041       [] |> fold (add_nonmonotonic_types_for_facts ctxt type_sys) [facts, conjs]
  1056     val repair = repair_fact ctxt nonmono_Ts type_sys sym_tab
  1042     val repair = repair_fact type_sys sym_tab
  1057     val (conjs, facts) = (conjs, facts) |> pairself (map repair)
  1043     val (conjs, facts) = (conjs, facts) |> pairself (map repair)
  1058     val repaired_sym_tab = conjs @ facts |> sym_table_for_facts false
  1044     val repaired_sym_tab = conjs @ facts |> sym_table_for_facts false
  1059     val helpers =
  1045     val helpers =
  1060       repaired_sym_tab |> helper_facts_for_sym_table ctxt type_sys |> map repair
  1046       repaired_sym_tab |> helper_facts_for_sym_table ctxt type_sys |> map repair
  1061     val sym_decl_lines =
  1047     val sym_decl_lines =