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