src/HOL/Tools/Ctr_Sugar/ctr_sugar.ML
changeset 58675 69571f0a93df
parent 58665 50b229a5a097
child 58685 6a75a4c24339
equal deleted inserted replaced
58674:eb98d1971d2a 58675:69571f0a93df
     5 Wrapping existing freely generated type's constructors.
     5 Wrapping existing freely generated type's constructors.
     6 *)
     6 *)
     7 
     7 
     8 signature CTR_SUGAR =
     8 signature CTR_SUGAR =
     9 sig
     9 sig
       
    10 
       
    11   datatype ctr_sugar_kind = Datatype | Codatatype | Record | Unknown
       
    12 
    10   type ctr_sugar =
    13   type ctr_sugar =
    11     {T: typ,
    14     {kind: ctr_sugar_kind,
       
    15      T: typ,
    12      ctrs: term list,
    16      ctrs: term list,
    13      casex: term,
    17      casex: term,
    14      discs: term list,
    18      discs: term list,
    15      selss: term list list,
    19      selss: term list list,
    16      exhaust: thm,
    20      exhaust: thm,
    71 
    75 
    72   type ctr_options = (string -> bool) * bool
    76   type ctr_options = (string -> bool) * bool
    73   type ctr_options_cmd = (Proof.context -> string -> bool) * bool
    77   type ctr_options_cmd = (Proof.context -> string -> bool) * bool
    74 
    78 
    75   val fake_local_theory_for_sel_defaults: (binding * typ) list -> Proof.context -> Proof.context
    79   val fake_local_theory_for_sel_defaults: (binding * typ) list -> Proof.context -> Proof.context
    76   val free_constructors: ({prems: thm list, context: Proof.context} -> tactic) list list ->
    80   val free_constructors: ctr_sugar_kind ->
       
    81     ({prems: thm list, context: Proof.context} -> tactic) list list ->
    77     ((ctr_options * binding) * (term, binding) ctr_spec list) * term list -> local_theory ->
    82     ((ctr_options * binding) * (term, binding) ctr_spec list) * term list -> local_theory ->
    78     ctr_sugar * local_theory
    83     ctr_sugar * local_theory
    79   val default_ctr_options: ctr_options
    84   val default_ctr_options: ctr_options
    80   val default_ctr_options_cmd: ctr_options_cmd
    85   val default_ctr_options_cmd: ctr_options_cmd
    81   val parse_bound_term: (binding * string) parser
    86   val parse_bound_term: (binding * string) parser
    89 
    94 
    90 open Ctr_Sugar_Util
    95 open Ctr_Sugar_Util
    91 open Ctr_Sugar_Tactics
    96 open Ctr_Sugar_Tactics
    92 open Ctr_Sugar_Code
    97 open Ctr_Sugar_Code
    93 
    98 
       
    99 datatype ctr_sugar_kind = Datatype | Codatatype | Record | Unknown;
       
   100 
    94 type ctr_sugar =
   101 type ctr_sugar =
    95   {T: typ,
   102   {kind: ctr_sugar_kind,
       
   103    T: typ,
    96    ctrs: term list,
   104    ctrs: term list,
    97    casex: term,
   105    casex: term,
    98    discs: term list,
   106    discs: term list,
    99    selss: term list list,
   107    selss: term list list,
   100    exhaust: thm,
   108    exhaust: thm,
   118    expands: thm list,
   126    expands: thm list,
   119    split_sels: thm list,
   127    split_sels: thm list,
   120    split_sel_asms: thm list,
   128    split_sel_asms: thm list,
   121    case_eq_ifs: thm list};
   129    case_eq_ifs: thm list};
   122 
   130 
   123 fun morph_ctr_sugar phi {T, ctrs, casex, discs, selss, exhaust, nchotomy, injects, distincts,
   131 fun morph_ctr_sugar phi {kind, T, ctrs, casex, discs, selss, exhaust, nchotomy, injects, distincts,
   124     case_thms, case_cong, case_cong_weak, split, split_asm, disc_defs, disc_thmss, discIs, sel_defs,
   132     case_thms, case_cong, case_cong_weak, split, split_asm, disc_defs, disc_thmss, discIs, sel_defs,
   125     sel_thmss, distinct_discsss, exhaust_discs, exhaust_sels, collapses, expands, split_sels,
   133     sel_thmss, distinct_discsss, exhaust_discs, exhaust_sels, collapses, expands, split_sels,
   126     split_sel_asms, case_eq_ifs} =
   134     split_sel_asms, case_eq_ifs} =
   127   {T = Morphism.typ phi T,
   135   {kind = kind,
       
   136    T = Morphism.typ phi T,
   128    ctrs = map (Morphism.term phi) ctrs,
   137    ctrs = map (Morphism.term phi) ctrs,
   129    casex = Morphism.term phi casex,
   138    casex = Morphism.term phi casex,
   130    discs = map (Morphism.term phi) discs,
   139    discs = map (Morphism.term phi) discs,
   131    selss = map (map (Morphism.term phi)) selss,
   140    selss = map (map (Morphism.term phi)) selss,
   132    exhaust = Morphism.thm phi exhaust,
   141    exhaust = Morphism.thm phi exhaust,
   358 fun ctr_of_ctr_spec ((_, ctr), _) = ctr;
   367 fun ctr_of_ctr_spec ((_, ctr), _) = ctr;
   359 fun args_of_ctr_spec (_, args) = args;
   368 fun args_of_ctr_spec (_, args) = args;
   360 
   369 
   361 val code_plugin = Plugin_Name.declare_setup @{binding code};
   370 val code_plugin = Plugin_Name.declare_setup @{binding code};
   362 
   371 
   363 fun prepare_free_constructors prep_plugins prep_term
   372 fun prepare_free_constructors kind prep_plugins prep_term
   364     ((((raw_plugins, discs_sels), raw_case_binding), ctr_specs),
   373     ((((raw_plugins, discs_sels), raw_case_binding), ctr_specs),
   365       sel_default_eqs) no_defs_lthy =
   374       sel_default_eqs) no_defs_lthy =
   366   let
   375   let
   367     val plugins = prep_plugins no_defs_lthy raw_plugins;
   376     val plugins = prep_plugins no_defs_lthy raw_plugins;
   368 
   377 
  1035           |> Local_Theory.notes (anonymous_notes @ notes)
  1044           |> Local_Theory.notes (anonymous_notes @ notes)
  1036           (* for "datatype_realizer.ML": *)
  1045           (* for "datatype_realizer.ML": *)
  1037           |>> name_noted_thms fcT_name exhaustN;
  1046           |>> name_noted_thms fcT_name exhaustN;
  1038 
  1047 
  1039         val ctr_sugar =
  1048         val ctr_sugar =
  1040           {T = fcT, ctrs = ctrs, casex = casex, discs = discs, selss = selss, exhaust = exhaust_thm,
  1049           {kind = kind, T = fcT, ctrs = ctrs, casex = casex, discs = discs, selss = selss,
  1041            nchotomy = nchotomy_thm, injects = inject_thms, distincts = distinct_thms,
  1050            exhaust = exhaust_thm, nchotomy = nchotomy_thm, injects = inject_thms,
  1042            case_thms = case_thms, case_cong = case_cong_thm, case_cong_weak = case_cong_weak_thm,
  1051            distincts = distinct_thms, case_thms = case_thms, case_cong = case_cong_thm,
  1043            split = split_thm, split_asm = split_asm_thm, disc_defs = disc_defs,
  1052            case_cong_weak = case_cong_weak_thm, split = split_thm, split_asm = split_asm_thm,
  1044            disc_thmss = disc_thmss, discIs = discI_thms, sel_defs = sel_defs, sel_thmss = sel_thmss,
  1053            disc_defs = disc_defs, disc_thmss = disc_thmss, discIs = discI_thms, sel_defs = sel_defs,
  1045            distinct_discsss = distinct_disc_thmsss, exhaust_discs = exhaust_disc_thms,
  1054            sel_thmss = sel_thmss, distinct_discsss = distinct_disc_thmsss,
  1046            exhaust_sels = exhaust_sel_thms, collapses = all_collapse_thms, expands = expand_thms,
  1055            exhaust_discs = exhaust_disc_thms, exhaust_sels = exhaust_sel_thms,
  1047            split_sels = split_sel_thms, split_sel_asms = split_sel_asm_thms,
  1056            collapses = all_collapse_thms, expands = expand_thms, split_sels = split_sel_thms,
  1048            case_eq_ifs = case_eq_if_thms}
  1057            split_sel_asms = split_sel_asm_thms, case_eq_ifs = case_eq_if_thms}
  1049           |> morph_ctr_sugar (substitute_noted_thm noted);
  1058           |> morph_ctr_sugar (substitute_noted_thm noted);
  1050       in
  1059       in
  1051         (ctr_sugar, lthy' |> register_ctr_sugar plugins ctr_sugar)
  1060         (ctr_sugar, lthy' |> register_ctr_sugar plugins ctr_sugar)
  1052       end;
  1061       end;
  1053   in
  1062   in
  1054     (goalss, after_qed, lthy')
  1063     (goalss, after_qed, lthy')
  1055   end;
  1064   end;
  1056 
  1065 
  1057 fun free_constructors tacss = (fn (goalss, after_qed, lthy) =>
  1066 fun free_constructors kind tacss = (fn (goalss, after_qed, lthy) =>
  1058   map2 (map2 (Thm.close_derivation oo Goal.prove_sorry lthy [] [])) goalss tacss
  1067   map2 (map2 (Thm.close_derivation oo Goal.prove_sorry lthy [] [])) goalss tacss
  1059   |> (fn thms => after_qed thms lthy)) oo prepare_free_constructors (K I) (K I);
  1068   |> (fn thms => after_qed thms lthy)) oo prepare_free_constructors kind (K I) (K I);
  1060 
  1069 
  1061 val free_constructors_cmd = (fn (goalss, after_qed, lthy) =>
  1070 fun free_constructors_cmd kind = (fn (goalss, after_qed, lthy) =>
  1062   Proof.theorem NONE (snd oo after_qed) (map (map (rpair [])) goalss) lthy) oo
  1071   Proof.theorem NONE (snd oo after_qed) (map (map (rpair [])) goalss) lthy) oo
  1063   prepare_free_constructors Plugin_Name.make_filter Syntax.read_term;
  1072   prepare_free_constructors kind Plugin_Name.make_filter Syntax.read_term;
  1064 
  1073 
  1065 val parse_bound_term = Parse.binding --| @{keyword ":"} -- Parse.term;
  1074 val parse_bound_term = Parse.binding --| @{keyword ":"} -- Parse.term;
  1066 
  1075 
  1067 type ctr_options = Plugin_Name.filter * bool;
  1076 type ctr_options = Plugin_Name.filter * bool;
  1068 type ctr_options_cmd = (Proof.context -> Plugin_Name.filter) * bool;
  1077 type ctr_options_cmd = (Proof.context -> Plugin_Name.filter) * bool;
  1087 val _ =
  1096 val _ =
  1088   Outer_Syntax.local_theory_to_proof @{command_spec "free_constructors"}
  1097   Outer_Syntax.local_theory_to_proof @{command_spec "free_constructors"}
  1089     "register an existing freely generated type's constructors"
  1098     "register an existing freely generated type's constructors"
  1090     (parse_ctr_options -- Parse.binding --| @{keyword "for"} -- parse_ctr_specs
  1099     (parse_ctr_options -- Parse.binding --| @{keyword "for"} -- parse_ctr_specs
  1091        -- parse_sel_default_eqs
  1100        -- parse_sel_default_eqs
  1092      >> free_constructors_cmd);
  1101      >> free_constructors_cmd Unknown);
  1093 
  1102 
  1094 end;
  1103 end;