src/HOL/Tools/Sledgehammer/sledgehammer_atp_translate.ML
changeset 42539 f6ba908b8b27
parent 42538 9e3e45636459
child 42540 77d9915e6a11
equal deleted inserted replaced
42538:9e3e45636459 42539:f6ba908b8b27
    19     Mangled of bool |
    19     Mangled of bool |
    20     No_Types
    20     No_Types
    21 
    21 
    22   val fact_prefix : string
    22   val fact_prefix : string
    23   val conjecture_prefix : string
    23   val conjecture_prefix : string
       
    24   val boolify_name : string
       
    25   val explicit_app_name : string
    24   val is_type_system_sound : type_system -> bool
    26   val is_type_system_sound : type_system -> bool
    25   val type_system_types_dangerous_types : type_system -> bool
    27   val type_system_types_dangerous_types : type_system -> bool
    26   val num_atp_type_args : theory -> type_system -> string -> int
    28   val num_atp_type_args : theory -> type_system -> string -> int
    27   val translate_atp_fact :
    29   val translate_atp_fact :
    28     Proof.context -> bool -> (string * 'a) * thm
    30     Proof.context -> bool -> (string * 'a) * thm
    48 val type_decl_prefix = "type_"
    50 val type_decl_prefix = "type_"
    49 val class_rel_clause_prefix = "clrel_";
    51 val class_rel_clause_prefix = "clrel_";
    50 val arity_clause_prefix = "arity_"
    52 val arity_clause_prefix = "arity_"
    51 val tfree_prefix = "tfree_"
    53 val tfree_prefix = "tfree_"
    52 
    54 
       
    55 val boolify_name = "hBOOL"
       
    56 val explicit_app_name = "hAPP"
    53 val is_base = "is"
    57 val is_base = "is"
    54 val type_prefix = "ty_"
    58 val type_prefix = "ty_"
    55 
    59 
    56 fun make_type ty = type_prefix ^ ascii_of ty
    60 fun make_type ty = type_prefix ^ ascii_of ty
    57 
    61 
   626   | consider_problem_line_syms (Formula (_, _, _, phi, _, _)) =
   630   | consider_problem_line_syms (Formula (_, _, _, phi, _, _)) =
   627     consider_formula_syms phi
   631     consider_formula_syms phi
   628 fun consider_problem_syms problem =
   632 fun consider_problem_syms problem =
   629   fold (fold consider_problem_line_syms o snd) problem
   633   fold (fold consider_problem_line_syms o snd) problem
   630 
   634 
   631 (* needed for helper facts if the problem otherwise does not involve equality *)
   635 (* The "equal" entry is needed for helper facts if the problem otherwise does
   632 val equal_entry = ("equal", {min_arity = 2, max_arity = 2, fun_sym = false})
   636    not involve equality. *)
       
   637 val default_entries =
       
   638   [("equal", {min_arity = 2, max_arity = 2, fun_sym = false})]
   633 
   639 
   634 fun sym_table_for_problem explicit_apply problem =
   640 fun sym_table_for_problem explicit_apply problem =
   635   if explicit_apply then
   641   if explicit_apply then
   636     NONE
   642     NONE
   637   else
   643   else
   638     SOME (Symtab.empty |> Symtab.default equal_entry
   644     SOME (Symtab.empty |> fold Symtab.default default_entries
   639                        |> consider_problem_syms problem)
   645                        |> consider_problem_syms problem)
   640 
   646 
   641 fun min_arity_of thy type_sys NONE s =
   647 fun min_arity_of thy type_sys NONE s =
   642     (if s = "equal" orelse s = type_tag_name orelse
   648     (if s = "equal" orelse s = type_tag_name orelse
   643         String.isPrefix type_const_prefix s orelse
   649         String.isPrefix type_const_prefix s orelse
   656     if s = type_tag_name then SOME ty else NONE
   662     if s = type_tag_name then SOME ty else NONE
   657   | full_type_of _ = NONE
   663   | full_type_of _ = NONE
   658 
   664 
   659 fun list_hAPP_rev _ t1 [] = t1
   665 fun list_hAPP_rev _ t1 [] = t1
   660   | list_hAPP_rev NONE t1 (t2 :: ts2) =
   666   | list_hAPP_rev NONE t1 (t2 :: ts2) =
   661     ATerm (`I "hAPP", [list_hAPP_rev NONE t1 ts2, t2])
   667     ATerm (`I explicit_app_name, [list_hAPP_rev NONE t1 ts2, t2])
   662   | list_hAPP_rev (SOME ty) t1 (t2 :: ts2) =
   668   | list_hAPP_rev (SOME ty) t1 (t2 :: ts2) =
   663     case full_type_of t2 of
   669     case full_type_of t2 of
   664       SOME ty2 =>
   670       SOME ty2 =>
   665       let val ty' = ATerm (`make_fixed_type_const @{type_name fun},
   671       let val ty' = ATerm (`make_fixed_type_const @{type_name fun},
   666                            [ty2, ty]) in
   672                            [ty2, ty]) in
   667         ATerm (`I "hAPP",
   673         ATerm (`I explicit_app_name,
   668                [tag_with_type ty' (list_hAPP_rev (SOME ty') t1 ts2), t2])
   674                [tag_with_type ty' (list_hAPP_rev (SOME ty') t1 ts2), t2])
   669       end
   675       end
   670     | NONE => list_hAPP_rev NONE t1 (t2 :: ts2)
   676     | NONE => list_hAPP_rev NONE t1 (t2 :: ts2)
   671 
   677 
   672 fun repair_applications_in_term thy type_sys sym_tab =
   678 fun repair_applications_in_term thy type_sys sym_tab =
   681           val ts = map (aux NONE) ts
   687           val ts = map (aux NONE) ts
   682           val (ts1, ts2) = chop (min_arity_of thy type_sys sym_tab s) ts
   688           val (ts1, ts2) = chop (min_arity_of thy type_sys sym_tab s) ts
   683         in list_hAPP_rev opt_ty (ATerm (name, ts1)) (rev ts2) end
   689         in list_hAPP_rev opt_ty (ATerm (name, ts1)) (rev ts2) end
   684   in aux NONE end
   690   in aux NONE end
   685 
   691 
   686 fun boolify t = ATerm (`I "hBOOL", [t])
   692 fun boolify t = ATerm (`I boolify_name, [t])
   687 
   693 
   688 (* True if the constant ever appears outside of the top-level position in
   694 (* True if the constant ever appears outside of the top-level position in
   689    literals, or if it appears with different arities (e.g., because of different
   695    literals, or if it appears with different arities (e.g., because of different
   690    type instantiations). If false, the constant always receives all of its
   696    type instantiations). If false, the constant always receives all of its
   691    arguments and is used as a predicate. *)
   697    arguments and is used as a predicate. *)
   784                NONE, NONE)
   790                NONE, NONE)
   785     end
   791     end
   786 fun type_decl_lines_for_const ctxt type_sys sym_tab (s, xs) =
   792 fun type_decl_lines_for_const ctxt type_sys sym_tab (s, xs) =
   787   map (type_decl_line_for_const_entry ctxt type_sys sym_tab s) xs
   793   map (type_decl_line_for_const_entry ctxt type_sys sym_tab s) xs
   788 
   794 
       
   795 fun add_extra_type_decl_lines Many_Typed =
       
   796     cons (Type_Decl (type_decl_prefix ^ boolify_name, `I boolify_name,
       
   797                      [mangled_combtyp (combtyp_from_typ @{typ bool})],
       
   798                      `I tff_bool_type))
       
   799   | add_extra_type_decl_lines _ = I
       
   800 
   789 val factsN = "Relevant facts"
   801 val factsN = "Relevant facts"
   790 val class_relsN = "Class relationships"
   802 val class_relsN = "Class relationships"
   791 val aritiesN = "Arity declarations"
   803 val aritiesN = "Arity declarations"
   792 val helpersN = "Helper facts"
   804 val helpersN = "Helper facts"
   793 val type_declsN = "Type declarations"
   805 val type_declsN = "Type declarations"
   824               |> get_helper_facts ctxt type_sys
   836               |> get_helper_facts ctxt type_sys
   825     val const_tab = const_table_for_facts type_sys sym_tab (conjs @ facts)
   837     val const_tab = const_table_for_facts type_sys sym_tab (conjs @ facts)
   826     val type_decl_lines =
   838     val type_decl_lines =
   827       Symtab.fold_rev (append o type_decl_lines_for_const ctxt type_sys sym_tab)
   839       Symtab.fold_rev (append o type_decl_lines_for_const ctxt type_sys sym_tab)
   828                       const_tab []
   840                       const_tab []
       
   841       |> add_extra_type_decl_lines type_sys
   829     val helper_lines =
   842     val helper_lines =
   830       helper_facts
   843       helper_facts
   831       |>> map (pair 0
   844       |>> map (pair 0
   832                #> problem_line_for_fact ctxt helper_prefix type_sys
   845                #> problem_line_for_fact ctxt helper_prefix type_sys
   833                #> repair_problem_line thy type_sys sym_tab)
   846                #> repair_problem_line thy type_sys sym_tab)