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