src/HOL/Tools/recdef_package.ML
changeset 16458 4c6fd0c01d28
parent 16364 dc9f7066d80a
child 16646 666774b0d1b0
equal deleted inserted replaced
16457:e0f22edf38a5 16458:4c6fd0c01d28
   101 
   101 
   102 (* theory data kind 'HOL/recdef' *)
   102 (* theory data kind 'HOL/recdef' *)
   103 
   103 
   104 type recdef_info = {simps: thm list, rules: thm list list, induct: thm, tcs: term list};
   104 type recdef_info = {simps: thm list, rules: thm list list, induct: thm, tcs: term list};
   105 
   105 
   106 structure GlobalRecdefArgs =
   106 structure GlobalRecdefData = TheoryDataFun
   107 struct
   107 (struct
   108   val name = "HOL/recdef";
   108   val name = "HOL/recdef";
   109   type T = recdef_info Symtab.table * hints;
   109   type T = recdef_info Symtab.table * hints;
   110 
   110 
   111   val empty = (Symtab.empty, mk_hints ([], [], [])): T;
   111   val empty = (Symtab.empty, mk_hints ([], [], [])): T;
   112   val copy = I;
   112   val copy = I;
   113   val prep_ext = I;
   113   val extend = I;
   114   fun merge
   114   fun merge _
   115    ((tab1, {simps = simps1, congs = congs1, wfs = wfs1}),
   115    ((tab1, {simps = simps1, congs = congs1, wfs = wfs1}),
   116     (tab2, {simps = simps2, congs = congs2, wfs = wfs2})) =
   116     (tab2, {simps = simps2, congs = congs2, wfs = wfs2})) : T =
   117       (Symtab.merge (K true) (tab1, tab2),
   117       (Symtab.merge (K true) (tab1, tab2),
   118         mk_hints (Drule.merge_rules (simps1, simps2),
   118         mk_hints (Drule.merge_rules (simps1, simps2),
   119           Library.merge_alists congs1 congs2,
   119           Library.merge_alists congs1 congs2,
   120           Drule.merge_rules (wfs1, wfs2)));
   120           Drule.merge_rules (wfs1, wfs2)));
   121 
   121 
   122   fun print sg (tab, hints) =
   122   fun print thy (tab, hints) =
   123     (Pretty.strs ("recdefs:" :: map #1 (NameSpace.extern_table (Sign.const_space sg, tab))) ::
   123     (Pretty.strs ("recdefs:" :: map #1 (NameSpace.extern_table (Sign.const_space thy, tab))) ::
   124       pretty_hints hints) |> Pretty.chunks |> Pretty.writeln;
   124       pretty_hints hints) |> Pretty.chunks |> Pretty.writeln;
   125 end;
   125 end);
   126 
   126 
   127 structure GlobalRecdefData = TheoryDataFun(GlobalRecdefArgs);
       
   128 val print_recdefs = GlobalRecdefData.print;
   127 val print_recdefs = GlobalRecdefData.print;
   129 
   128 
   130 
   129 
   131 fun get_recdef thy name = Symtab.lookup (#1 (GlobalRecdefData.get thy), name);
   130 fun get_recdef thy name = Symtab.lookup (#1 (GlobalRecdefData.get thy), name);
   132 
   131 
   141 val map_global_hints = GlobalRecdefData.map o apsnd;
   140 val map_global_hints = GlobalRecdefData.map o apsnd;
   142 
   141 
   143 
   142 
   144 (* proof data kind 'HOL/recdef' *)
   143 (* proof data kind 'HOL/recdef' *)
   145 
   144 
   146 structure LocalRecdefArgs =
   145 structure LocalRecdefData = ProofDataFun
   147 struct
   146 (struct
   148   val name = "HOL/recdef";
   147   val name = "HOL/recdef";
   149   type T = hints;
   148   type T = hints;
   150   val init = get_global_hints;
   149   val init = get_global_hints;
   151   fun print _ hints = pretty_hints hints |> Pretty.chunks |> Pretty.writeln;
   150   fun print _ hints = pretty_hints hints |> Pretty.chunks |> Pretty.writeln;
   152 end;
   151 end);
   153 
   152 
   154 structure LocalRecdefData = ProofDataFun(LocalRecdefArgs);
       
   155 val get_local_hints = LocalRecdefData.get;
   153 val get_local_hints = LocalRecdefData.get;
   156 val map_local_hints = LocalRecdefData.map;
   154 val map_local_hints = LocalRecdefData.map;
   157 
   155 
   158 
   156 
   159 (* attributes *)
   157 (* attributes *)
   232 (*"strict" is true iff (permissive) has been omitted*)
   230 (*"strict" is true iff (permissive) has been omitted*)
   233 fun gen_add_recdef tfl_fn prep_att prep_hints strict raw_name R eq_srcs hints thy =
   231 fun gen_add_recdef tfl_fn prep_att prep_hints strict raw_name R eq_srcs hints thy =
   234   let
   232   let
   235     val _ = requires_recdef thy;
   233     val _ = requires_recdef thy;
   236 
   234 
   237     val name = Sign.intern_const (Theory.sign_of thy) raw_name;
   235     val name = Sign.intern_const thy raw_name;
   238     val bname = Sign.base_name name;
   236     val bname = Sign.base_name name;
   239     val _ = message ("Defining recursive function " ^ quote name ^ " ...");
   237     val _ = message ("Defining recursive function " ^ quote name ^ " ...");
   240 
   238 
   241     val ((eq_names, eqs), raw_eq_atts) = apfst split_list (split_list eq_srcs);
   239     val ((eq_names, eqs), raw_eq_atts) = apfst split_list (split_list eq_srcs);
   242     val eq_atts = map (map (prep_att thy)) raw_eq_atts;
   240     val eq_atts = map (map (prep_att thy)) raw_eq_atts;
   280 
   278 
   281 (** defer_recdef(_i) **)
   279 (** defer_recdef(_i) **)
   282 
   280 
   283 fun gen_defer_recdef tfl_fn app_thms raw_name eqs raw_congs thy =
   281 fun gen_defer_recdef tfl_fn app_thms raw_name eqs raw_congs thy =
   284   let
   282   let
   285     val name = Sign.intern_const (Theory.sign_of thy) raw_name;
   283     val name = Sign.intern_const thy raw_name;
   286     val bname = Sign.base_name name;
   284     val bname = Sign.base_name name;
   287 
   285 
   288     val _ = requires_recdef thy;
   286     val _ = requires_recdef thy;
   289     val _ = message ("Deferred recursive function " ^ quote name ^ " ...");
   287     val _ = message ("Deferred recursive function " ^ quote name ^ " ...");
   290 
   288 
   304 
   302 
   305 (** recdef_tc(_i) **)
   303 (** recdef_tc(_i) **)
   306 
   304 
   307 fun gen_recdef_tc prep_att prep_name (bname, raw_atts) raw_name opt_i int thy =
   305 fun gen_recdef_tc prep_att prep_name (bname, raw_atts) raw_name opt_i int thy =
   308   let
   306   let
   309     val name = prep_name (Theory.sign_of thy) raw_name;
   307     val name = prep_name thy raw_name;
   310     val atts = map (prep_att thy) raw_atts;
   308     val atts = map (prep_att thy) raw_atts;
   311     val tcs =
   309     val tcs =
   312       (case get_recdef thy name of
   310       (case get_recdef thy name of
   313         NONE => error ("No recdef definition of constant: " ^ quote name)
   311         NONE => error ("No recdef definition of constant: " ^ quote name)
   314       | SOME {tcs, ...} => tcs);
   312       | SOME {tcs, ...} => tcs);
   378 val _ = OuterSyntax.add_keywords ["permissive", "congs", "hints"];
   376 val _ = OuterSyntax.add_keywords ["permissive", "congs", "hints"];
   379 val _ = OuterSyntax.add_parsers [recdefP, defer_recdefP, recdef_tcP];
   377 val _ = OuterSyntax.add_parsers [recdefP, defer_recdefP, recdef_tcP];
   380 
   378 
   381 end;
   379 end;
   382 
   380 
   383 
   381 end;
   384 end;
       
   385