src/HOL/Tools/recdef_package.ML
author paulson
Fri Nov 24 16:38:42 2006 +0100 (2006-11-24)
changeset 21513 9e9fff87dc6c
parent 21505 13d4dba99337
child 22101 6d13239d5f52
permissions -rw-r--r--
Conversion of "equal" to "=" for TSTP format; big tidy-up
     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 get_hints: Proof.context -> {simps: thm list, congs: (string * thm) list, wfs: thm list}
    15   val simp_add: attribute
    16   val simp_del: attribute
    17   val cong_add: attribute
    18   val cong_del: attribute
    19   val wf_add: attribute
    20   val wf_del: attribute
    21   val add_recdef: bool -> xstring -> string -> ((bstring * string) * Attrib.src list) list ->
    22     Attrib.src option -> theory -> theory
    23       * {simps: thm list, rules: thm list list, induct: thm, tcs: term list}
    24   val add_recdef_i: bool -> xstring -> term -> ((bstring * term) * attribute list) list ->
    25     theory -> theory * {simps: thm list, rules: thm list list, induct: thm, tcs: term list}
    26   val defer_recdef: xstring -> string list -> (thmref * Attrib.src list) list
    27     -> theory -> theory * {induct_rules: thm}
    28   val defer_recdef_i: xstring -> term list -> (thm list * attribute list) list
    29     -> theory -> theory * {induct_rules: thm}
    30   val recdef_tc: bstring * Attrib.src list -> xstring -> int option -> local_theory -> Proof.state
    31   val recdef_tc_i: bstring * Attrib.src list -> string -> int option -> local_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 snd 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
    75     val (c, thm) = prep_cong raw_thm;
    76     val _ = if AList.defined (op =) congs c
    77       then warning ("Overwriting recdef congruence rule for " ^ quote c)
    78       else ();
    79   in AList.update (op =) (c, thm) congs end;
    80 
    81 fun del_cong raw_thm congs =
    82   let
    83     val (c, thm) = prep_cong raw_thm;
    84     val _ = if AList.defined (op =) congs c
    85       then ()
    86       else warning ("No recdef congruence rule for " ^ quote c);
    87   in AList.delete (op =) c congs end;
    88 
    89 end;
    90 
    91 
    92 
    93 (** global and local recdef data **)
    94 
    95 (* theory data *)
    96 
    97 type recdef_info = {simps: thm list, rules: thm list list, induct: thm, tcs: term list};
    98 
    99 structure GlobalRecdefData = TheoryDataFun
   100 (struct
   101   val name = "HOL/recdef";
   102   type T = recdef_info Symtab.table * hints;
   103 
   104   val empty = (Symtab.empty, mk_hints ([], [], [])): T;
   105   val copy = I;
   106   val extend = I;
   107   fun merge _
   108    ((tab1, {simps = simps1, congs = congs1, wfs = wfs1}),
   109     (tab2, {simps = simps2, congs = congs2, wfs = wfs2})) : T =
   110       (Symtab.merge (K true) (tab1, tab2),
   111         mk_hints (Drule.merge_rules (simps1, simps2),
   112           AList.merge (op =) eq_thm (congs1, congs2),
   113           Drule.merge_rules (wfs1, wfs2)));
   114 
   115   fun print thy (tab, hints) =
   116     (Pretty.strs ("recdefs:" :: map #1 (NameSpace.extern_table (Sign.const_space thy, tab))) ::
   117       pretty_hints hints) |> Pretty.chunks |> Pretty.writeln;
   118 end);
   119 
   120 val print_recdefs = GlobalRecdefData.print;
   121 
   122 
   123 val get_recdef = Symtab.lookup o #1 o GlobalRecdefData.get;
   124 
   125 fun put_recdef name info thy =
   126   let
   127     val (tab, hints) = GlobalRecdefData.get thy;
   128     val tab' = Symtab.update_new (name, info) tab
   129       handle Symtab.DUP _ => error ("Duplicate recursive function definition " ^ quote name);
   130   in GlobalRecdefData.put (tab', hints) thy end;
   131 
   132 val get_global_hints = #2 o GlobalRecdefData.get;
   133 
   134 
   135 (* proof data *)
   136 
   137 structure LocalRecdefData = ProofDataFun
   138 (struct
   139   val name = "HOL/recdef";
   140   type T = hints;
   141   val init = get_global_hints;
   142   fun print _ hints = pretty_hints hints |> Pretty.chunks |> Pretty.writeln;
   143 end);
   144 
   145 val get_hints = LocalRecdefData.get;
   146 fun map_hints f = Context.mapping (GlobalRecdefData.map (apsnd f)) (LocalRecdefData.map f);
   147 
   148 
   149 (* attributes *)
   150 
   151 fun attrib f = Thm.declaration_attribute (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, simp_add): Method.modifier),
   169   Args.$$$ recdef_simpN -- Args.add -- Args.colon >> K (I, simp_add),
   170   Args.$$$ recdef_simpN -- Args.del -- Args.colon >> K (I, simp_del),
   171   Args.$$$ recdef_congN -- Args.colon >> K (I, cong_add),
   172   Args.$$$ recdef_congN -- Args.add -- Args.colon >> K (I, cong_add),
   173   Args.$$$ recdef_congN -- Args.del -- Args.colon >> K (I, cong_del),
   174   Args.$$$ recdef_wfN -- Args.colon >> K (I, wf_add),
   175   Args.$$$ recdef_wfN -- Args.add -- Args.colon >> K (I, wf_add),
   176   Args.$$$ recdef_wfN -- Args.del -- Args.colon >> K (I, 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_hints ctxt;
   191     val cs = local_claset_of ctxt;
   192     val ss = local_simpset_of ctxt addsimps simps;
   193   in (cs, ss, rev (map snd 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, rev (map snd 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 o map) fst (partition_eq (eq_snd (op = : int * int -> bool)) rules_idx);
   226     val simp_att = if null tcs then [Simplifier.simp_add, RecfunCodegen.add NONE] else [];
   227 
   228     val ((simps' :: rules', [induct']), thy) =
   229       thy
   230       |> Theory.add_path bname
   231       |> PureThy.add_thmss
   232         ((("simps", List.concat rules), simp_att) :: ((eq_names ~~ rules) ~~ eq_atts))
   233       ||>> PureThy.add_thms [(("induct", induct), [])];
   234     val result = {simps = simps', rules = rules', induct = induct', tcs = tcs};
   235     val thy =
   236       thy
   237       |> put_recdef name result
   238       |> Theory.parent_path;
   239   in (thy, result) end;
   240 
   241 val add_recdef = gen_add_recdef Tfl.define Attrib.attribute prepare_hints;
   242 fun add_recdef_i x y z w = gen_add_recdef Tfl.define_i (K I) prepare_hints_i x y z w ();
   243 
   244 
   245 
   246 (** defer_recdef(_i) **)
   247 
   248 fun gen_defer_recdef tfl_fn app_thms raw_name eqs raw_congs thy =
   249   let
   250     val name = Sign.intern_const thy raw_name;
   251     val bname = Sign.base_name name;
   252 
   253     val _ = requires_recdef thy;
   254     val _ = message ("Deferred recursive function " ^ quote name ^ " ...");
   255 
   256     val (congs, thy1) = thy |> app_thms raw_congs;
   257     val (thy2, induct_rules) = tfl_fn thy1 congs name eqs;
   258     val ([induct_rules'], thy3) =
   259       thy2
   260       |> Theory.add_path bname
   261       |> PureThy.add_thms [(("induct_rules", induct_rules), [])]
   262       ||> Theory.parent_path;
   263   in (thy3, {induct_rules = induct_rules'}) end;
   264 
   265 val defer_recdef = gen_defer_recdef Tfl.defer IsarCmd.apply_theorems;
   266 val defer_recdef_i = gen_defer_recdef Tfl.defer_i IsarCmd.apply_theorems_i;
   267 
   268 
   269 
   270 (** recdef_tc(_i) **)
   271 
   272 fun gen_recdef_tc prep_att prep_name (bname, raw_atts) raw_name opt_i lthy =
   273   let
   274     val thy = ProofContext.theory_of lthy;
   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 = the_default 1 opt_i;
   282     val tc = nth tcs (i - 1) handle Subscript =>
   283       error ("No termination condition #" ^ string_of_int i ^
   284         " in recdef definition of " ^ quote name);
   285   in
   286     Specification.theorem_i Thm.internalK NONE (K I) (bname, atts)
   287       [] (Element.Shows [(("", []), [(HOLogic.mk_Trueprop tc, [])])]) lthy
   288   end;
   289 
   290 val recdef_tc = gen_recdef_tc Attrib.intern_src Sign.intern_const;
   291 val recdef_tc_i = gen_recdef_tc (K I) (K I);
   292 
   293 
   294 
   295 (** package setup **)
   296 
   297 (* setup theory *)
   298 
   299 val setup =
   300   GlobalRecdefData.init #>
   301   LocalRecdefData.init #>
   302   Attrib.add_attributes
   303    [(recdef_simpN, Attrib.add_del_args simp_add simp_del, "declaration of recdef simp rule"),
   304     (recdef_congN, Attrib.add_del_args cong_add cong_del, "declaration of recdef cong rule"),
   305     (recdef_wfN, Attrib.add_del_args wf_add wf_del, "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_locale_target --
   337       P.opt_thm_name ":" -- P.xname -- Scan.option (P.$$$ "(" |-- P.nat --| P.$$$ ")")
   338       >> (fn (((loc, thm_name), name), i) =>
   339         Toplevel.print o Toplevel.local_theory_to_proof loc (recdef_tc thm_name name i)));
   340 
   341 
   342 val _ = OuterSyntax.add_keywords ["permissive", "congs", "hints"];
   343 val _ = OuterSyntax.add_parsers [recdefP, defer_recdefP, recdef_tcP];
   344 
   345 end;
   346 
   347 end;