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