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