src/HOL/Tools/recdef_package.ML
author wenzelm
Sat Jan 21 23:02:14 2006 +0100 (2006-01-21)
changeset 18728 6790126ab5f6
parent 18708 4b3dadb4fe33
child 18799 f137c5e971f5
permissions -rw-r--r--
simplified type attribute;
     1 (*  Title:      HOL/Tools/recdef_package.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4 
     5 Wrapper module for Konrad Slind's TFL package.
     6 *)
     7 
     8 signature RECDEF_PACKAGE =
     9 sig
    10   val quiet_mode: bool ref
    11   val print_recdefs: theory -> unit
    12   val get_recdef: theory -> string
    13     -> {simps: thm list, rules: thm list list, induct: thm, tcs: term list} option
    14   val simp_add: attribute
    15   val simp_del: attribute
    16   val cong_add: attribute
    17   val cong_del: attribute
    18   val wf_add: attribute
    19   val wf_del: attribute
    20   val add_recdef: bool -> xstring -> string -> ((bstring * string) * Attrib.src list) list ->
    21     Attrib.src option -> theory -> theory
    22       * {simps: thm list, rules: thm list list, induct: thm, tcs: term list}
    23   val add_recdef_i: bool -> xstring -> term -> ((bstring * term) * attribute list) list ->
    24     theory -> theory * {simps: thm list, rules: thm list list, induct: thm, tcs: term list}
    25   val defer_recdef: xstring -> string list -> (thmref * Attrib.src list) list
    26     -> theory -> theory * {induct_rules: thm}
    27   val defer_recdef_i: xstring -> term list -> (thm list * attribute list) list
    28     -> theory -> theory * {induct_rules: thm}
    29   val recdef_tc: bstring * Attrib.src list -> xstring -> int option -> theory -> Proof.state
    30   val recdef_tc_i: bstring * attribute list -> string -> int option -> theory -> Proof.state
    31   val setup: theory -> theory
    32 end;
    33 
    34 structure RecdefPackage: RECDEF_PACKAGE =
    35 struct
    36 
    37 val quiet_mode = Tfl.quiet_mode;
    38 val message = Tfl.message;
    39 
    40 
    41 (** recdef hints **)
    42 
    43 (* type hints *)
    44 
    45 type hints = {simps: thm list, congs: (string * thm) list, wfs: thm list};
    46 
    47 fun mk_hints (simps, congs, wfs) = {simps = simps, congs = congs, wfs = wfs}: hints;
    48 fun map_hints f ({simps, congs, wfs}: hints) = mk_hints (f (simps, congs, wfs));
    49 
    50 fun map_simps f = map_hints (fn (simps, congs, wfs) => (f simps, congs, wfs));
    51 fun map_congs f = map_hints (fn (simps, congs, wfs) => (simps, f congs, wfs));
    52 fun map_wfs f = map_hints (fn (simps, congs, wfs) => (simps, congs, f wfs));
    53 
    54 fun pretty_hints ({simps, congs, wfs}: hints) =
    55  [Pretty.big_list "recdef simp hints:" (map Display.pretty_thm simps),
    56   Pretty.big_list "recdef cong hints:" (map Display.pretty_thm (map #2 congs)),
    57   Pretty.big_list "recdef wf hints:" (map Display.pretty_thm wfs)];
    58 
    59 
    60 (* congruence rules *)
    61 
    62 local
    63 
    64 val cong_head =
    65   fst o Term.dest_Const o Term.head_of o fst o Logic.dest_equals o Thm.concl_of;
    66 
    67 fun prep_cong raw_thm =
    68   let val thm = safe_mk_meta_eq raw_thm in (cong_head thm, thm) end;
    69 
    70 in
    71 
    72 fun add_cong raw_thm congs =
    73   let val (c, thm) = prep_cong raw_thm
    74   in overwrite_warn (congs, (c, thm)) ("Overwriting recdef congruence rule for " ^ quote c) end;
    75 
    76 fun del_cong raw_thm congs =
    77   let
    78     val (c, thm) = prep_cong raw_thm;
    79     val (del, rest) = Library.partition (Library.equal c o fst) congs;
    80   in if null del then (warning ("No recdef congruence rule for " ^ quote c); congs) else rest end;
    81 
    82 val add_congs = foldr (uncurry add_cong);
    83 
    84 end;
    85 
    86 
    87 
    88 (** global and local recdef data **)
    89 
    90 (* theory data *)
    91 
    92 type recdef_info = {simps: thm list, rules: thm list list, induct: thm, tcs: term list};
    93 
    94 structure GlobalRecdefData = TheoryDataFun
    95 (struct
    96   val name = "HOL/recdef";
    97   type T = recdef_info Symtab.table * hints;
    98 
    99   val empty = (Symtab.empty, mk_hints ([], [], [])): T;
   100   val copy = I;
   101   val extend = I;
   102   fun merge _
   103    ((tab1, {simps = simps1, congs = congs1, wfs = wfs1}),
   104     (tab2, {simps = simps2, congs = congs2, wfs = wfs2})) : T =
   105       (Symtab.merge (K true) (tab1, tab2),
   106         mk_hints (Drule.merge_rules (simps1, simps2),
   107           Library.merge_alists congs1 congs2,
   108           Drule.merge_rules (wfs1, wfs2)));
   109 
   110   fun print thy (tab, hints) =
   111     (Pretty.strs ("recdefs:" :: map #1 (NameSpace.extern_table (Sign.const_space thy, tab))) ::
   112       pretty_hints hints) |> Pretty.chunks |> Pretty.writeln;
   113 end);
   114 
   115 val print_recdefs = GlobalRecdefData.print;
   116 
   117 
   118 val get_recdef = Symtab.lookup o #1 o GlobalRecdefData.get;
   119 
   120 fun put_recdef name info thy =
   121   let
   122     val (tab, hints) = GlobalRecdefData.get thy;
   123     val tab' = Symtab.update_new (name, info) tab
   124       handle Symtab.DUP _ => error ("Duplicate recursive function definition " ^ quote name);
   125   in GlobalRecdefData.put (tab', hints) thy end;
   126 
   127 val get_global_hints = #2 o GlobalRecdefData.get;
   128 val map_global_hints = GlobalRecdefData.map o apsnd;
   129 
   130 
   131 (* proof data *)
   132 
   133 structure LocalRecdefData = ProofDataFun
   134 (struct
   135   val name = "HOL/recdef";
   136   type T = hints;
   137   val init = get_global_hints;
   138   fun print _ hints = pretty_hints hints |> Pretty.chunks |> Pretty.writeln;
   139 end);
   140 
   141 val get_local_hints = LocalRecdefData.get;
   142 val map_local_hints = LocalRecdefData.map;
   143 
   144 
   145 (* attributes *)
   146 
   147 fun map_hints f (Context.Theory thy) = Context.Theory (map_global_hints f thy)
   148   | map_hints f (Context.Proof ctxt) = Context.Proof (map_local_hints f ctxt);
   149 
   150 fun attrib f = Thm.declaration_attribute (map_hints o f);
   151 
   152 val simp_add = attrib (map_simps o Drule.add_rule);
   153 val simp_del = attrib (map_simps o Drule.del_rule);
   154 val cong_add = attrib (map_congs o add_cong);
   155 val cong_del = attrib (map_congs o del_cong);
   156 val wf_add = attrib (map_wfs o Drule.add_rule);
   157 val wf_del = attrib (map_wfs o Drule.del_rule);
   158 
   159 
   160 (* modifiers *)
   161 
   162 val recdef_simpN = "recdef_simp";
   163 val recdef_congN = "recdef_cong";
   164 val recdef_wfN = "recdef_wf";
   165 
   166 val recdef_modifiers =
   167  [Args.$$$ recdef_simpN -- Args.colon >> K ((I, simp_add): Method.modifier),
   168   Args.$$$ recdef_simpN -- Args.add -- Args.colon >> K (I, simp_add),
   169   Args.$$$ recdef_simpN -- Args.del -- Args.colon >> K (I, simp_del),
   170   Args.$$$ recdef_congN -- Args.colon >> K (I, cong_add),
   171   Args.$$$ recdef_congN -- Args.add -- Args.colon >> K (I, cong_add),
   172   Args.$$$ recdef_congN -- Args.del -- Args.colon >> K (I, cong_del),
   173   Args.$$$ recdef_wfN -- Args.colon >> K (I, wf_add),
   174   Args.$$$ recdef_wfN -- Args.add -- Args.colon >> K (I, wf_add),
   175   Args.$$$ recdef_wfN -- Args.del -- Args.colon >> K (I, wf_del)] @
   176   Clasimp.clasimp_modifiers;
   177 
   178 
   179 
   180 (** prepare_hints(_i) **)
   181 
   182 fun prepare_hints thy opt_src =
   183   let
   184     val ctxt0 = ProofContext.init thy;
   185     val ctxt =
   186       (case opt_src of
   187         NONE => ctxt0
   188       | SOME src => Method.only_sectioned_args recdef_modifiers I src ctxt0);
   189     val {simps, congs, wfs} = get_local_hints ctxt;
   190     val cs = local_claset_of ctxt;
   191     val ss = local_simpset_of ctxt addsimps simps;
   192   in (cs, ss, map #2 congs, wfs) end;
   193 
   194 fun prepare_hints_i thy () =
   195   let
   196     val ctxt0 = ProofContext.init thy;
   197     val {simps, congs, wfs} = get_global_hints thy;
   198   in (local_claset_of ctxt0, local_simpset_of ctxt0 addsimps simps, map #2 congs, wfs) end;
   199 
   200 
   201 
   202 (** add_recdef(_i) **)
   203 
   204 fun requires_recdef thy = Theory.requires thy "Recdef" "recursive functions";
   205 
   206 fun gen_add_recdef tfl_fn prep_att prep_hints not_permissive raw_name R eq_srcs hints thy =
   207   let
   208     val _ = requires_recdef thy;
   209 
   210     val name = Sign.intern_const thy raw_name;
   211     val bname = Sign.base_name name;
   212     val _ = message ("Defining recursive function " ^ quote name ^ " ...");
   213 
   214     val ((eq_names, eqs), raw_eq_atts) = apfst split_list (split_list eq_srcs);
   215     val eq_atts = map (map (prep_att thy)) raw_eq_atts;
   216 
   217     val (cs, ss, congs, wfs) = prep_hints thy hints;
   218     (*We must remove imp_cong to prevent looping when the induction rule
   219       is simplified. Many induction rules have nested implications that would
   220       give rise to looping conditional rewriting.*)
   221     val (thy, {rules = rules_idx, induct, tcs}) =
   222         tfl_fn not_permissive thy cs (ss delcongs [imp_cong])
   223                congs wfs name R eqs;
   224     val rules = map (map #1) (Library.partition_eq (Library.eq_snd (op =)) rules_idx);
   225     val simp_att = if null tcs then [Simplifier.simp_add, RecfunCodegen.add NONE] else [];
   226 
   227     val ((simps' :: rules', [induct']), thy) =
   228       thy
   229       |> Theory.add_path bname
   230       |> PureThy.add_thmss
   231         ((("simps", List.concat rules), simp_att) :: ((eq_names ~~ rules) ~~ eq_atts))
   232       ||>> PureThy.add_thms [(("induct", induct), [])];
   233     val result = {simps = simps', rules = rules', induct = induct', tcs = tcs};
   234     val thy =
   235       thy
   236       |> put_recdef name result
   237       |> Theory.parent_path;
   238   in (thy, result) end;
   239 
   240 val add_recdef = gen_add_recdef Tfl.define Attrib.attribute prepare_hints;
   241 fun add_recdef_i x y z w = gen_add_recdef Tfl.define_i (K I) prepare_hints_i x y z w ();
   242 
   243 
   244 
   245 (** defer_recdef(_i) **)
   246 
   247 fun gen_defer_recdef tfl_fn app_thms raw_name eqs raw_congs thy =
   248   let
   249     val name = Sign.intern_const thy raw_name;
   250     val bname = Sign.base_name name;
   251 
   252     val _ = requires_recdef thy;
   253     val _ = message ("Deferred recursive function " ^ quote name ^ " ...");
   254 
   255     val (congs, thy1) = thy |> app_thms raw_congs;
   256     val (thy2, induct_rules) = tfl_fn thy1 congs name eqs;
   257     val ([induct_rules'], thy3) =
   258       thy2
   259       |> Theory.add_path bname
   260       |> PureThy.add_thms [(("induct_rules", induct_rules), [])]
   261       ||> Theory.parent_path;
   262   in (thy3, {induct_rules = induct_rules'}) end;
   263 
   264 val defer_recdef = gen_defer_recdef Tfl.defer IsarThy.apply_theorems;
   265 val defer_recdef_i = gen_defer_recdef Tfl.defer_i IsarThy.apply_theorems_i;
   266 
   267 
   268 
   269 (** recdef_tc(_i) **)
   270 
   271 fun gen_recdef_tc prep_att prep_name (bname, raw_atts) raw_name opt_i thy =
   272   let
   273     val name = prep_name thy raw_name;
   274     val atts = map (prep_att thy) raw_atts;
   275     val tcs =
   276       (case get_recdef thy name of
   277         NONE => error ("No recdef definition of constant: " ^ quote name)
   278       | SOME {tcs, ...} => tcs);
   279     val i = getOpt (opt_i, 1);
   280     val tc = List.nth (tcs, i - 1) handle Subscript =>
   281       error ("No termination condition #" ^ string_of_int i ^
   282         " in recdef definition of " ^ quote name);
   283   in IsarThy.theorem_i Drule.internalK (bname, atts) (HOLogic.mk_Trueprop tc, ([], [])) thy end;
   284 
   285 val recdef_tc = gen_recdef_tc Attrib.attribute Sign.intern_const;
   286 val recdef_tc_i = gen_recdef_tc (K I) (K I);
   287 
   288 
   289 
   290 (** package setup **)
   291 
   292 (* setup theory *)
   293 
   294 val setup =
   295   GlobalRecdefData.init #>
   296   LocalRecdefData.init #>
   297   Attrib.add_attributes
   298    [(recdef_simpN, Attrib.add_del_args simp_add simp_del, "declaration of recdef simp rule"),
   299     (recdef_congN, Attrib.add_del_args cong_add cong_del, "declaration of recdef cong rule"),
   300     (recdef_wfN, Attrib.add_del_args wf_add wf_del, "declaration of recdef wf rule")];
   301 
   302 
   303 (* outer syntax *)
   304 
   305 local structure P = OuterParse and K = OuterKeyword in
   306 
   307 val hints =
   308   P.$$$ "(" |-- P.!!! (P.position (P.$$$ "hints" -- P.arguments) --| P.$$$ ")") >> Args.src;
   309 
   310 val recdef_decl =
   311   Scan.optional (P.$$$ "(" -- P.!!! (P.$$$ "permissive" -- P.$$$ ")") >> K false) true --
   312   P.name -- P.term -- Scan.repeat1 (P.opt_thm_name ":" -- P.prop) -- Scan.option hints
   313   >> (fn ((((p, f), R), eqs), src) => #1 o add_recdef p f R (map P.triple_swap eqs) src);
   314 
   315 val recdefP =
   316   OuterSyntax.command "recdef" "define general recursive functions (TFL)" K.thy_decl
   317     (recdef_decl >> Toplevel.theory);
   318 
   319 
   320 val defer_recdef_decl =
   321   P.name -- Scan.repeat1 P.prop --
   322   Scan.optional (P.$$$ "(" |-- P.$$$ "congs" |-- P.!!! (P.xthms1 --| P.$$$ ")")) []
   323   >> (fn ((f, eqs), congs) => #1 o defer_recdef f eqs congs);
   324 
   325 val defer_recdefP =
   326   OuterSyntax.command "defer_recdef" "defer general recursive functions (TFL)" K.thy_decl
   327     (defer_recdef_decl >> Toplevel.theory);
   328 
   329 val recdef_tcP =
   330   OuterSyntax.command "recdef_tc" "recommence proof of termination condition (TFL)" K.thy_goal
   331     (P.opt_thm_name ":" -- P.xname -- Scan.option (P.$$$ "(" |-- P.nat --| P.$$$ ")")
   332       >> (fn ((thm_name, name), i) =>
   333         Toplevel.print o Toplevel.theory_to_proof (recdef_tc thm_name name i)));
   334 
   335 
   336 val _ = OuterSyntax.add_keywords ["permissive", "congs", "hints"];
   337 val _ = OuterSyntax.add_parsers [recdefP, defer_recdefP, recdef_tcP];
   338 
   339 end;
   340 
   341 end;