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