src/HOL/Tools/recdef_package.ML
author wenzelm
Wed Sep 06 19:01:37 2000 +0200 (2000-09-06)
changeset 9879 a1fcaf2d080d
parent 9876 a069795f1060
child 9893 93d2fde0306c
permissions -rw-r--r--
make SML/NJ happy;
wenzelm@6429
     1
(*  Title:      HOL/Tools/recdef_package.ML
wenzelm@6429
     2
    ID:         $Id$
wenzelm@6429
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@9876
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
wenzelm@6429
     5
wenzelm@6429
     6
Wrapper module for Konrad Slind's TFL package.
wenzelm@6429
     7
*)
wenzelm@6429
     8
wenzelm@6429
     9
signature RECDEF_PACKAGE =
wenzelm@6429
    10
sig
wenzelm@6429
    11
  val quiet_mode: bool ref
wenzelm@6439
    12
  val print_recdefs: theory -> unit
wenzelm@8657
    13
  val get_recdef: theory -> string
wenzelm@8657
    14
    -> {simps: thm list, rules: thm list list, induct: thm, tcs: term list} option
wenzelm@9859
    15
  val simp_add_global: theory attribute
wenzelm@9859
    16
  val simp_del_global: theory attribute
wenzelm@9859
    17
  val cong_add_global: theory attribute
wenzelm@9859
    18
  val cong_del_global: theory attribute
wenzelm@9859
    19
  val wf_add_global: theory attribute
wenzelm@9859
    20
  val wf_del_global: theory attribute
wenzelm@9859
    21
  val simp_add_local: Proof.context attribute
wenzelm@9859
    22
  val simp_del_local: Proof.context attribute
wenzelm@9859
    23
  val cong_add_local: Proof.context attribute
wenzelm@9859
    24
  val cong_del_local: Proof.context attribute
wenzelm@9859
    25
  val wf_add_local: Proof.context attribute
wenzelm@9859
    26
  val wf_del_local: Proof.context attribute
wenzelm@9859
    27
  val add_recdef: xstring -> string -> ((bstring * string) * Args.src list) list ->
wenzelm@9859
    28
    Args.src option -> theory -> theory
wenzelm@9859
    29
      * {simps: thm list, rules: thm list list, induct: thm, tcs: term list}
wenzelm@9859
    30
  val add_recdef_i: xstring -> term -> ((bstring * term) * theory attribute list) list ->
wenzelm@9859
    31
    theory -> theory * {simps: thm list, rules: thm list list, induct: thm, tcs: term list}
wenzelm@9859
    32
  val add_recdef_old: xstring -> string -> ((bstring * string) * Args.src list) list ->
wenzelm@9859
    33
    simpset * thm list -> theory ->
wenzelm@9859
    34
    theory * {simps: thm list, rules: thm list list, induct: thm, tcs: term list}
wenzelm@6557
    35
  val defer_recdef: xstring -> string list -> (xstring * Args.src list) list
wenzelm@6557
    36
    -> theory -> theory * {induct_rules: thm}
wenzelm@6557
    37
  val defer_recdef_i: xstring -> term list -> (thm * theory attribute list) list
wenzelm@6557
    38
    -> theory -> theory * {induct_rules: thm}
wenzelm@6439
    39
  val setup: (theory -> theory) list
wenzelm@6429
    40
end;
wenzelm@6429
    41
wenzelm@6429
    42
structure RecdefPackage: RECDEF_PACKAGE =
wenzelm@6429
    43
struct
wenzelm@6429
    44
wenzelm@9859
    45
wenzelm@6429
    46
val quiet_mode = Tfl.quiet_mode;
wenzelm@6429
    47
val message = Tfl.message;
wenzelm@6429
    48
wenzelm@6429
    49
wenzelm@9859
    50
(** recdef hints **)
wenzelm@6439
    51
wenzelm@9859
    52
(* type hints *)
wenzelm@9859
    53
wenzelm@9859
    54
type hints = {simps: thm list, congs: (string * thm) list, wfs: thm list};
wenzelm@9859
    55
wenzelm@9859
    56
fun mk_hints (simps, congs, wfs) = {simps = simps, congs = congs, wfs = wfs}: hints;
wenzelm@9859
    57
fun map_hints f ({simps, congs, wfs}: hints) = mk_hints (f (simps, congs, wfs));
wenzelm@9859
    58
wenzelm@9859
    59
fun map_simps f = map_hints (fn (simps, congs, wfs) => (f simps, congs, wfs));
wenzelm@9859
    60
fun map_congs f = map_hints (fn (simps, congs, wfs) => (simps, f congs, wfs));
wenzelm@9859
    61
fun map_wfs f = map_hints (fn (simps, congs, wfs) => (simps, congs, f wfs));
wenzelm@9859
    62
wenzelm@9859
    63
fun pretty_hints ({simps, congs, wfs}: hints) =
wenzelm@9859
    64
 [Pretty.big_list "recdef simp hints:" (map Display.pretty_thm simps),
wenzelm@9859
    65
  Pretty.big_list "recdef cong hints:" (map Display.pretty_thm (map #2 congs)),
wenzelm@9859
    66
  Pretty.big_list "recdef wf hints:" (map Display.pretty_thm wfs)];
wenzelm@9859
    67
wenzelm@9859
    68
wenzelm@9859
    69
(* congruence rules *)
wenzelm@9859
    70
wenzelm@9859
    71
local
wenzelm@9859
    72
wenzelm@9859
    73
val cong_head =
wenzelm@9859
    74
  fst o Term.dest_Const o Term.head_of o fst o Logic.dest_equals o Thm.concl_of;
wenzelm@6439
    75
wenzelm@9859
    76
fun prep_cong raw_thm =
wenzelm@9859
    77
  let val thm = safe_mk_meta_eq raw_thm in (cong_head thm, thm) end;
wenzelm@9859
    78
wenzelm@9859
    79
in
wenzelm@9859
    80
wenzelm@9859
    81
fun add_cong raw_thm congs =
wenzelm@9859
    82
  let val (c, thm) = prep_cong raw_thm
wenzelm@9859
    83
  in overwrite_warn (congs, (c, thm)) ("Overwriting recdef congruence rule for " ^ quote c) end;
wenzelm@9859
    84
wenzelm@9859
    85
fun del_cong raw_thm congs =
wenzelm@9859
    86
  let
wenzelm@9859
    87
    val (c, thm) = prep_cong raw_thm;
wenzelm@9859
    88
    val (del, rest) = Library.partition (Library.equal c o fst) congs;
wenzelm@9859
    89
  in if null del then (warning ("No recdef congruence rule for " ^ quote c); congs) else rest end;
wenzelm@9859
    90
wenzelm@9859
    91
val add_congs = curry (foldr (uncurry add_cong));
wenzelm@9859
    92
wenzelm@9859
    93
end;
wenzelm@9859
    94
wenzelm@9859
    95
wenzelm@9859
    96
wenzelm@9859
    97
(** global and local recdef data **)
wenzelm@9859
    98
wenzelm@9859
    99
(* theory data kind 'HOL/recdef' *)
wenzelm@6439
   100
wenzelm@8657
   101
type recdef_info = {simps: thm list, rules: thm list list, induct: thm, tcs: term list};
wenzelm@6439
   102
wenzelm@9859
   103
structure GlobalRecdefArgs =
wenzelm@6439
   104
struct
wenzelm@6439
   105
  val name = "HOL/recdef";
wenzelm@9859
   106
  type T = recdef_info Symtab.table * hints;
wenzelm@6439
   107
wenzelm@9879
   108
  val empty = (Symtab.empty, mk_hints ([], [], [])): T;
wenzelm@6557
   109
  val copy = I;
wenzelm@6439
   110
  val prep_ext = I;
wenzelm@9859
   111
  fun merge
wenzelm@9859
   112
   ((tab1, {simps = simps1, congs = congs1, wfs = wfs1}),
wenzelm@9859
   113
    (tab2, {simps = simps2, congs = congs2, wfs = wfs2})) =
wenzelm@9859
   114
      (Symtab.merge (K true) (tab1, tab2),
wenzelm@9859
   115
        mk_hints (Drule.merge_rules (simps1, simps2),
wenzelm@9859
   116
          Library.merge_alists congs1 congs2,
wenzelm@9859
   117
          Drule.merge_rules (wfs1, wfs2)));
wenzelm@6439
   118
wenzelm@9859
   119
  fun print sg (tab, hints) =
wenzelm@9859
   120
   (Pretty.strs ("recdefs:" :: map #1 (Sign.cond_extern_table sg Sign.constK tab)) ::
wenzelm@9859
   121
     pretty_hints hints) |> Pretty.chunks |> Pretty.writeln;
wenzelm@6439
   122
end;
wenzelm@6439
   123
wenzelm@9859
   124
structure GlobalRecdefData = TheoryDataFun(GlobalRecdefArgs);
wenzelm@9859
   125
val print_recdefs = GlobalRecdefData.print;
wenzelm@6429
   126
wenzelm@6439
   127
wenzelm@9859
   128
fun get_recdef thy name = Symtab.lookup (#1 (GlobalRecdefData.get thy), name);
wenzelm@6439
   129
wenzelm@6439
   130
fun put_recdef name info thy =
wenzelm@6429
   131
  let
wenzelm@9859
   132
    val (tab, hints) = GlobalRecdefData.get thy;
wenzelm@9859
   133
    val tab' = Symtab.update_new ((name, info), tab)
wenzelm@6439
   134
      handle Symtab.DUP _ => error ("Duplicate recursive function definition " ^ quote name);
wenzelm@9859
   135
  in GlobalRecdefData.put (tab', hints) thy end;
wenzelm@9859
   136
wenzelm@9859
   137
val get_global_hints = #2 o GlobalRecdefData.get;
wenzelm@9859
   138
val map_global_hints = GlobalRecdefData.map o apsnd;
wenzelm@9859
   139
wenzelm@9859
   140
wenzelm@9859
   141
(* proof data kind 'HOL/recdef' *)
wenzelm@9859
   142
wenzelm@9859
   143
structure LocalRecdefArgs =
wenzelm@9859
   144
struct
wenzelm@9859
   145
  val name = "HOL/recdef";
wenzelm@9859
   146
  type T = hints;
wenzelm@9859
   147
  val init = get_global_hints;
wenzelm@9859
   148
  fun print _ hints = pretty_hints hints |> Pretty.chunks |> Pretty.writeln;
wenzelm@9859
   149
end;
wenzelm@9859
   150
wenzelm@9859
   151
structure LocalRecdefData = ProofDataFun(LocalRecdefArgs);
wenzelm@9859
   152
val get_local_hints = LocalRecdefData.get;
wenzelm@9859
   153
val map_local_hints = LocalRecdefData.map;
wenzelm@9859
   154
wenzelm@9859
   155
wenzelm@9859
   156
(* attributes *)
wenzelm@9859
   157
wenzelm@9859
   158
local
wenzelm@9859
   159
wenzelm@9859
   160
fun global_local f g =
wenzelm@9859
   161
 (fn (thy, thm) => (map_global_hints (f (g thm)) thy, thm),
wenzelm@9859
   162
  fn (ctxt, thm) => (map_local_hints (f (g thm)) ctxt, thm));
wenzelm@9859
   163
wenzelm@9859
   164
fun mk_attr (add1, add2) (del1, del2) =
wenzelm@9859
   165
 (Attrib.add_del_args add1 del1, Attrib.add_del_args add2 del2);
wenzelm@9859
   166
wenzelm@9859
   167
in
wenzelm@9859
   168
wenzelm@9859
   169
val (simp_add_global, simp_add_local) = global_local map_simps (Drule.add_rules o single);
wenzelm@9859
   170
val (simp_del_global, simp_del_local) = global_local map_simps (Drule.del_rules o single);
wenzelm@9859
   171
val (cong_add_global, cong_add_local) = global_local map_congs add_cong;
wenzelm@9859
   172
val (cong_del_global, cong_del_local) = global_local map_congs del_cong;
wenzelm@9859
   173
val (wf_add_global, wf_add_local) = global_local map_wfs (Drule.add_rules o single);
wenzelm@9859
   174
val (wf_del_global, wf_del_local) = global_local map_wfs (Drule.del_rules o single);
wenzelm@9859
   175
wenzelm@9859
   176
val simp_attr = mk_attr (simp_add_global, simp_add_local) (simp_del_global, simp_del_local);
wenzelm@9859
   177
val cong_attr = mk_attr (cong_add_global, cong_add_local) (cong_del_global, cong_del_local);
wenzelm@9859
   178
val wf_attr = mk_attr (wf_add_global, wf_add_local) (wf_del_global, wf_del_local);
wenzelm@9859
   179
wenzelm@9859
   180
end;
wenzelm@9859
   181
wenzelm@9859
   182
wenzelm@9859
   183
wenzelm@9859
   184
(** prepare_hints(_i) **)
wenzelm@9859
   185
wenzelm@9859
   186
local
wenzelm@9859
   187
wenzelm@9859
   188
val simpN = "simp";
wenzelm@9859
   189
val congN = "cong";
wenzelm@9859
   190
val wfN = "wf";
wenzelm@9859
   191
val addN = "add";
wenzelm@9859
   192
val delN = "del";
wenzelm@9859
   193
wenzelm@9859
   194
val recdef_modifiers =
wenzelm@9859
   195
 [Args.$$$ simpN -- Args.colon >> K ((I, simp_add_local): Method.modifier),
wenzelm@9859
   196
  Args.$$$ simpN -- Args.$$$ addN -- Args.colon >> K (I, simp_add_local),
wenzelm@9859
   197
  Args.$$$ simpN -- Args.$$$ delN -- Args.colon >> K (I, simp_del_local),
wenzelm@9859
   198
  Args.$$$ congN -- Args.colon >> K (I, cong_add_local),
wenzelm@9859
   199
  Args.$$$ congN -- Args.$$$ addN -- Args.colon >> K (I, cong_add_local),
wenzelm@9859
   200
  Args.$$$ congN -- Args.$$$ delN -- Args.colon >> K (I, cong_del_local),
wenzelm@9859
   201
  Args.$$$ wfN -- Args.colon >> K (I, wf_add_local),
wenzelm@9859
   202
  Args.$$$ wfN -- Args.$$$ addN -- Args.colon >> K (I, wf_add_local),
wenzelm@9859
   203
  Args.$$$ wfN -- Args.$$$ delN -- Args.colon >> K (I, wf_del_local)];
wenzelm@9859
   204
wenzelm@9859
   205
val modifiers =  (* FIXME include 'simp cong' (!?) *)
wenzelm@9859
   206
  recdef_modifiers @ Splitter.split_modifiers @ Classical.cla_modifiers @ Clasimp.iff_modifiers;
wenzelm@9859
   207
wenzelm@9859
   208
in
wenzelm@9859
   209
wenzelm@9859
   210
fun prepare_hints thy opt_src =
wenzelm@9859
   211
  let
wenzelm@9859
   212
    val ctxt0 = ProofContext.init thy;
wenzelm@9859
   213
    val ctxt =
wenzelm@9859
   214
      (case opt_src of
wenzelm@9859
   215
        None => ctxt0
wenzelm@9859
   216
      | Some src => Method.only_sectioned_args modifiers I src ctxt0);
wenzelm@9859
   217
    val {simps, congs, wfs} = get_local_hints ctxt;
wenzelm@9859
   218
    val cs = Classical.get_local_claset ctxt;
wenzelm@9859
   219
    val ss = Simplifier.get_local_simpset ctxt addsimps simps;
wenzelm@9859
   220
  in (cs, ss, map #2 congs, wfs) end;
wenzelm@9859
   221
wenzelm@9859
   222
fun prepare_hints_i thy () =
wenzelm@9859
   223
  let val {simps, congs, wfs} = get_global_hints thy
wenzelm@9859
   224
  in (Classical.claset_of thy, Simplifier.simpset_of thy addsimps simps, map #2 congs, wfs) end;
wenzelm@9859
   225
wenzelm@9859
   226
end;
wenzelm@6439
   227
wenzelm@6439
   228
wenzelm@6439
   229
wenzelm@6439
   230
(** add_recdef(_i) **)
wenzelm@6439
   231
wenzelm@6557
   232
fun requires_recdef thy = Theory.requires thy "Recdef" "recursive functions";
wenzelm@6557
   233
wenzelm@9859
   234
fun gen_add_recdef tfl_fn prep_att prep_hints raw_name R eq_srcs hints thy =
wenzelm@6439
   235
  let
wenzelm@9859
   236
    val _ = requires_recdef thy;
wenzelm@9859
   237
wenzelm@6439
   238
    val name = Sign.intern_const (Theory.sign_of thy) raw_name;
wenzelm@6439
   239
    val bname = Sign.base_name name;
wenzelm@6429
   240
    val _ = message ("Defining recursive function " ^ quote name ^ " ...");
wenzelm@6429
   241
wenzelm@8657
   242
    val ((eq_names, eqs), raw_eq_atts) = apfst split_list (split_list eq_srcs);
wenzelm@8657
   243
    val eq_atts = map (map (prep_att thy)) raw_eq_atts;
wenzelm@8657
   244
wenzelm@9859
   245
    val (cs, ss, congs, wfs) = prep_hints thy hints;
wenzelm@9859
   246
    val (thy, {rules = rules_idx, induct, tcs}) = tfl_fn thy cs ss congs wfs name R eqs;
wenzelm@8657
   247
    val rules = map (map #1) (Library.partition_eq Library.eq_snd rules_idx);
wenzelm@8657
   248
    val simp_att = if null tcs then [Simplifier.simp_add_global] else [];
wenzelm@8657
   249
wenzelm@8430
   250
    val case_numbers = map Library.string_of_int (1 upto Thm.nprems_of induct);
wenzelm@8657
   251
    val (thy, (simps' :: rules', [induct'])) =
wenzelm@7798
   252
      thy
wenzelm@6439
   253
      |> Theory.add_path bname
wenzelm@8657
   254
      |> PureThy.add_thmss ((("simps", flat rules), simp_att) :: ((eq_names ~~ rules) ~~ eq_atts))
wenzelm@8430
   255
      |>>> PureThy.add_thms [(("induct", induct), [RuleCases.case_names case_numbers])];
wenzelm@8657
   256
    val result = {simps = simps', rules = rules', induct = induct', tcs = tcs};
wenzelm@7798
   257
    val thy =
wenzelm@7798
   258
      thy
wenzelm@6439
   259
      |> put_recdef name result
wenzelm@6429
   260
      |> Theory.parent_path;
wenzelm@7798
   261
  in (thy, result) end;
wenzelm@6429
   262
wenzelm@9859
   263
val add_recdef = gen_add_recdef Tfl.define Attrib.global_attribute prepare_hints;
wenzelm@9859
   264
fun add_recdef_i x y z = gen_add_recdef Tfl.define_i (K I) prepare_hints_i x y z ();
wenzelm@9859
   265
wenzelm@9859
   266
wenzelm@9859
   267
(* add_recdef_old -- legacy interface *)
wenzelm@9859
   268
wenzelm@9859
   269
fun prepare_hints_old thy (ss, thms) =
wenzelm@9859
   270
  let val {simps, congs, wfs} = get_global_hints thy
wenzelm@9859
   271
  in (Classical.claset_of thy, ss addsimps simps, map #2 (add_congs thms congs), wfs) end;
wenzelm@9859
   272
wenzelm@9859
   273
val add_recdef_old = gen_add_recdef Tfl.define Attrib.global_attribute prepare_hints_old;
wenzelm@6429
   274
wenzelm@6429
   275
wenzelm@6557
   276
wenzelm@6557
   277
(** defer_recdef(_i) **)
wenzelm@6557
   278
wenzelm@6557
   279
fun gen_defer_recdef tfl_fn app_thms raw_name eqs raw_congs thy =
wenzelm@6557
   280
  let
wenzelm@6557
   281
    val name = Sign.intern_const (Theory.sign_of thy) raw_name;
wenzelm@6557
   282
    val bname = Sign.base_name name;
wenzelm@6557
   283
wenzelm@6557
   284
    val _ = requires_recdef thy;
wenzelm@6557
   285
    val _ = message ("Deferred recursive function " ^ quote name ^ " ...");
wenzelm@6557
   286
wenzelm@6557
   287
    val (thy1, congs) = thy |> app_thms raw_congs;
wenzelm@9859
   288
    val (thy2, induct_rules) = tfl_fn thy1 congs name eqs;
wenzelm@8430
   289
    val (thy3, [induct_rules']) =
wenzelm@6557
   290
      thy2
wenzelm@6557
   291
      |> Theory.add_path bname
wenzelm@6557
   292
      |> PureThy.add_thms [(("induct_rules", induct_rules), [])]
wenzelm@8430
   293
      |>> Theory.parent_path;
wenzelm@8430
   294
  in (thy3, {induct_rules = induct_rules'}) end;
wenzelm@6557
   295
wenzelm@6557
   296
val defer_recdef = gen_defer_recdef Tfl.defer IsarThy.apply_theorems;
wenzelm@6557
   297
val defer_recdef_i = gen_defer_recdef Tfl.defer_i IsarThy.apply_theorems_i;
wenzelm@6557
   298
wenzelm@6557
   299
wenzelm@6557
   300
wenzelm@6439
   301
(** package setup **)
wenzelm@6439
   302
wenzelm@6439
   303
(* setup theory *)
wenzelm@6439
   304
wenzelm@9859
   305
val setup =
wenzelm@9859
   306
 [GlobalRecdefData.init, LocalRecdefData.init,
wenzelm@9859
   307
  Attrib.add_attributes
wenzelm@9859
   308
   [("recdef_simp", simp_attr, "declare recdef simp rule"),
wenzelm@9859
   309
    ("recdef_cong", cong_attr, "declare recdef cong rule"),
wenzelm@9859
   310
    ("recdef_wf", wf_attr, "declare recdef wf rule")]];
wenzelm@6439
   311
wenzelm@6439
   312
wenzelm@6429
   313
(* outer syntax *)
wenzelm@6429
   314
wenzelm@6723
   315
local structure P = OuterParse and K = OuterSyntax.Keyword in
wenzelm@6429
   316
wenzelm@9859
   317
val hints =
wenzelm@9859
   318
  P.$$$ "(" |-- P.!!! (P.position (P.$$$ "hints" -- P.arguments) --| P.$$$ ")") >> Args.src;
wenzelm@9859
   319
wenzelm@6429
   320
val recdef_decl =
wenzelm@9859
   321
  P.name -- P.term -- Scan.repeat1 (P.opt_thm_name ":" -- P.prop --| P.marg_comment)
wenzelm@9859
   322
    -- Scan.option hints
wenzelm@9859
   323
  >> (fn (((f, R), eqs), src) => #1 o add_recdef f R (map P.triple_swap eqs) src);
wenzelm@6429
   324
wenzelm@6429
   325
val recdefP =
wenzelm@6723
   326
  OuterSyntax.command "recdef" "define general recursive functions (TFL)" K.thy_decl
wenzelm@6429
   327
    (recdef_decl >> Toplevel.theory);
wenzelm@6429
   328
wenzelm@6557
   329
wenzelm@6557
   330
val defer_recdef_decl =
wenzelm@8657
   331
  P.name -- Scan.repeat1 P.prop --
wenzelm@6723
   332
  Scan.optional (P.$$$ "(" |-- P.$$$ "congs" |-- P.!!! (P.xthms1 --| P.$$$ ")")) []
wenzelm@6557
   333
  >> (fn ((f, eqs), congs) => #1 o defer_recdef f eqs congs);
wenzelm@6557
   334
wenzelm@6557
   335
val defer_recdefP =
wenzelm@6723
   336
  OuterSyntax.command "defer_recdef" "defer general recursive functions (TFL)" K.thy_decl
wenzelm@6557
   337
    (defer_recdef_decl >> Toplevel.theory);
wenzelm@6557
   338
wenzelm@9859
   339
val _ = OuterSyntax.add_keywords ["hints"];
wenzelm@6557
   340
val _ = OuterSyntax.add_parsers [recdefP, defer_recdefP];
wenzelm@6429
   341
wenzelm@6429
   342
end;
wenzelm@6429
   343
wenzelm@6429
   344
wenzelm@6429
   345
end;