src/HOL/Tools/recdef_package.ML
author paulson
Tue May 22 17:56:06 2007 +0200 (2007-05-22)
changeset 23075 69e30a7e8880
parent 22846 fb79144af9a3
child 24039 273698405054
permissions -rw-r--r--
Some hacks for SPASS format
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@8657
    11
  val get_recdef: theory -> string
wenzelm@8657
    12
    -> {simps: thm list, rules: thm list list, induct: thm, tcs: term list} option
wenzelm@21505
    13
  val get_hints: Proof.context -> {simps: thm list, congs: (string * thm) list, wfs: thm list}
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@21351
    29
  val recdef_tc: bstring * Attrib.src list -> xstring -> int option -> local_theory -> Proof.state
wenzelm@21351
    30
  val recdef_tc_i: bstring * Attrib.src list -> string -> int option -> local_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),
haftmann@21098
    56
  Pretty.big_list "recdef cong hints:" (map Display.pretty_thm (map snd 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 =
haftmann@21098
    73
  let
haftmann@21098
    74
    val (c, thm) = prep_cong raw_thm;
haftmann@21098
    75
    val _ = if AList.defined (op =) congs c
haftmann@21098
    76
      then warning ("Overwriting recdef congruence rule for " ^ quote c)
haftmann@21098
    77
      else ();
haftmann@21098
    78
  in AList.update (op =) (c, thm) congs 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;
haftmann@21098
    83
    val _ = if AList.defined (op =) congs c
haftmann@21098
    84
      then ()
haftmann@21098
    85
      else warning ("No recdef congruence rule for " ^ quote c);
haftmann@21098
    86
  in AList.delete (op =) c congs end;
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@22846
    99
(
wenzelm@9859
   100
  type T = recdef_info Symtab.table * hints;
wenzelm@9879
   101
  val empty = (Symtab.empty, mk_hints ([], [], [])): T;
wenzelm@6557
   102
  val copy = I;
wenzelm@16458
   103
  val extend = I;
wenzelm@16458
   104
  fun merge _
wenzelm@9859
   105
   ((tab1, {simps = simps1, congs = congs1, wfs = wfs1}),
wenzelm@16458
   106
    (tab2, {simps = simps2, congs = congs2, wfs = wfs2})) : T =
wenzelm@9859
   107
      (Symtab.merge (K true) (tab1, tab2),
wenzelm@9859
   108
        mk_hints (Drule.merge_rules (simps1, simps2),
wenzelm@22360
   109
          AList.merge (op =) Thm.eq_thm (congs1, congs2),
wenzelm@9859
   110
          Drule.merge_rules (wfs1, wfs2)));
wenzelm@22846
   111
);
wenzelm@6439
   112
wenzelm@17412
   113
val get_recdef = Symtab.lookup o #1 o GlobalRecdefData.get;
wenzelm@6439
   114
wenzelm@6439
   115
fun put_recdef name info thy =
wenzelm@6429
   116
  let
wenzelm@9859
   117
    val (tab, hints) = GlobalRecdefData.get thy;
wenzelm@17412
   118
    val tab' = Symtab.update_new (name, info) tab
wenzelm@6439
   119
      handle Symtab.DUP _ => error ("Duplicate recursive function definition " ^ quote name);
wenzelm@9859
   120
  in GlobalRecdefData.put (tab', hints) thy end;
wenzelm@9859
   121
wenzelm@9859
   122
val get_global_hints = #2 o GlobalRecdefData.get;
wenzelm@9859
   123
wenzelm@9859
   124
wenzelm@17920
   125
(* proof data *)
wenzelm@9859
   126
wenzelm@16458
   127
structure LocalRecdefData = ProofDataFun
wenzelm@22846
   128
(
wenzelm@9859
   129
  type T = hints;
wenzelm@9859
   130
  val init = get_global_hints;
wenzelm@22846
   131
);
wenzelm@9859
   132
wenzelm@21505
   133
val get_hints = LocalRecdefData.get;
wenzelm@21505
   134
fun map_hints f = Context.mapping (GlobalRecdefData.map (apsnd f)) (LocalRecdefData.map f);
wenzelm@9859
   135
wenzelm@20291
   136
wenzelm@20291
   137
(* attributes *)
wenzelm@20291
   138
wenzelm@18728
   139
fun attrib f = Thm.declaration_attribute (map_hints o f);
wenzelm@9859
   140
wenzelm@18688
   141
val simp_add = attrib (map_simps o Drule.add_rule);
wenzelm@18688
   142
val simp_del = attrib (map_simps o Drule.del_rule);
wenzelm@18688
   143
val cong_add = attrib (map_congs o add_cong);
wenzelm@18688
   144
val cong_del = attrib (map_congs o del_cong);
wenzelm@18688
   145
val wf_add = attrib (map_wfs o Drule.add_rule);
wenzelm@18688
   146
val wf_del = attrib (map_wfs o Drule.del_rule);
wenzelm@9859
   147
wenzelm@9859
   148
wenzelm@9949
   149
(* modifiers *)
wenzelm@9859
   150
wenzelm@9949
   151
val recdef_simpN = "recdef_simp";
wenzelm@9949
   152
val recdef_congN = "recdef_cong";
wenzelm@9949
   153
val recdef_wfN = "recdef_wf";
wenzelm@9859
   154
wenzelm@9859
   155
val recdef_modifiers =
wenzelm@18728
   156
 [Args.$$$ recdef_simpN -- Args.colon >> K ((I, simp_add): Method.modifier),
wenzelm@18728
   157
  Args.$$$ recdef_simpN -- Args.add -- Args.colon >> K (I, simp_add),
wenzelm@18728
   158
  Args.$$$ recdef_simpN -- Args.del -- Args.colon >> K (I, simp_del),
wenzelm@18728
   159
  Args.$$$ recdef_congN -- Args.colon >> K (I, cong_add),
wenzelm@18728
   160
  Args.$$$ recdef_congN -- Args.add -- Args.colon >> K (I, cong_add),
wenzelm@18728
   161
  Args.$$$ recdef_congN -- Args.del -- Args.colon >> K (I, cong_del),
wenzelm@18728
   162
  Args.$$$ recdef_wfN -- Args.colon >> K (I, wf_add),
wenzelm@18728
   163
  Args.$$$ recdef_wfN -- Args.add -- Args.colon >> K (I, wf_add),
wenzelm@18728
   164
  Args.$$$ recdef_wfN -- Args.del -- Args.colon >> K (I, wf_del)] @
wenzelm@9949
   165
  Clasimp.clasimp_modifiers;
wenzelm@9859
   166
wenzelm@9949
   167
wenzelm@9859
   168
wenzelm@9949
   169
(** prepare_hints(_i) **)
wenzelm@9859
   170
wenzelm@9859
   171
fun prepare_hints thy opt_src =
wenzelm@9859
   172
  let
wenzelm@9859
   173
    val ctxt0 = ProofContext.init thy;
wenzelm@9859
   174
    val ctxt =
wenzelm@9859
   175
      (case opt_src of
skalberg@15531
   176
        NONE => ctxt0
skalberg@15531
   177
      | SOME src => Method.only_sectioned_args recdef_modifiers I src ctxt0);
wenzelm@21505
   178
    val {simps, congs, wfs} = get_hints ctxt;
wenzelm@15032
   179
    val cs = local_claset_of ctxt;
wenzelm@15032
   180
    val ss = local_simpset_of ctxt addsimps simps;
haftmann@21098
   181
  in (cs, ss, rev (map snd congs), wfs) end;
wenzelm@9859
   182
wenzelm@9859
   183
fun prepare_hints_i thy () =
wenzelm@15032
   184
  let
wenzelm@15032
   185
    val ctxt0 = ProofContext.init thy;
wenzelm@15032
   186
    val {simps, congs, wfs} = get_global_hints thy;
haftmann@21098
   187
  in (local_claset_of ctxt0, local_simpset_of ctxt0 addsimps simps, rev (map snd congs), wfs) end;
wenzelm@9859
   188
wenzelm@6439
   189
wenzelm@6439
   190
wenzelm@6439
   191
(** add_recdef(_i) **)
wenzelm@6439
   192
wenzelm@6557
   193
fun requires_recdef thy = Theory.requires thy "Recdef" "recursive functions";
wenzelm@6557
   194
wenzelm@17920
   195
fun gen_add_recdef tfl_fn prep_att prep_hints not_permissive raw_name R eq_srcs hints thy =
wenzelm@6439
   196
  let
wenzelm@9859
   197
    val _ = requires_recdef thy;
wenzelm@9859
   198
wenzelm@16458
   199
    val name = Sign.intern_const thy raw_name;
wenzelm@6439
   200
    val bname = Sign.base_name name;
wenzelm@6429
   201
    val _ = message ("Defining recursive function " ^ quote name ^ " ...");
wenzelm@6429
   202
wenzelm@8657
   203
    val ((eq_names, eqs), raw_eq_atts) = apfst split_list (split_list eq_srcs);
wenzelm@8657
   204
    val eq_atts = map (map (prep_att thy)) raw_eq_atts;
wenzelm@8657
   205
wenzelm@9859
   206
    val (cs, ss, congs, wfs) = prep_hints thy hints;
paulson@14241
   207
    (*We must remove imp_cong to prevent looping when the induction rule
paulson@14241
   208
      is simplified. Many induction rules have nested implications that would
paulson@14241
   209
      give rise to looping conditional rewriting.*)
paulson@14241
   210
    val (thy, {rules = rules_idx, induct, tcs}) =
wenzelm@17920
   211
        tfl_fn not_permissive thy cs (ss delcongs [imp_cong])
paulson@14241
   212
               congs wfs name R eqs;
haftmann@21098
   213
    val rules = (map o map) fst (partition_eq (eq_snd (op = : int * int -> bool)) rules_idx);
wenzelm@18728
   214
    val simp_att = if null tcs then [Simplifier.simp_add, RecfunCodegen.add NONE] else [];
wenzelm@8657
   215
haftmann@18377
   216
    val ((simps' :: rules', [induct']), thy) =
wenzelm@7798
   217
      thy
wenzelm@6439
   218
      |> Theory.add_path bname
wenzelm@18688
   219
      |> PureThy.add_thmss
wenzelm@18688
   220
        ((("simps", List.concat rules), simp_att) :: ((eq_names ~~ rules) ~~ eq_atts))
haftmann@18377
   221
      ||>> PureThy.add_thms [(("induct", induct), [])];
wenzelm@8657
   222
    val result = {simps = simps', rules = rules', induct = induct', tcs = tcs};
wenzelm@7798
   223
    val thy =
wenzelm@7798
   224
      thy
wenzelm@6439
   225
      |> put_recdef name result
wenzelm@6429
   226
      |> Theory.parent_path;
wenzelm@7798
   227
  in (thy, result) end;
wenzelm@6429
   228
wenzelm@18728
   229
val add_recdef = gen_add_recdef Tfl.define Attrib.attribute prepare_hints;
wenzelm@11629
   230
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
   231
wenzelm@9859
   232
wenzelm@6557
   233
wenzelm@6557
   234
(** defer_recdef(_i) **)
wenzelm@6557
   235
wenzelm@6557
   236
fun gen_defer_recdef tfl_fn app_thms raw_name eqs raw_congs thy =
wenzelm@6557
   237
  let
wenzelm@16458
   238
    val name = Sign.intern_const thy raw_name;
wenzelm@6557
   239
    val bname = Sign.base_name name;
wenzelm@6557
   240
wenzelm@6557
   241
    val _ = requires_recdef thy;
wenzelm@6557
   242
    val _ = message ("Deferred recursive function " ^ quote name ^ " ...");
wenzelm@6557
   243
haftmann@18418
   244
    val (congs, thy1) = thy |> app_thms raw_congs;
wenzelm@9859
   245
    val (thy2, induct_rules) = tfl_fn thy1 congs name eqs;
haftmann@18377
   246
    val ([induct_rules'], thy3) =
wenzelm@6557
   247
      thy2
wenzelm@6557
   248
      |> Theory.add_path bname
wenzelm@6557
   249
      |> PureThy.add_thms [(("induct_rules", induct_rules), [])]
haftmann@18377
   250
      ||> Theory.parent_path;
wenzelm@8430
   251
  in (thy3, {induct_rules = induct_rules'}) end;
wenzelm@6557
   252
wenzelm@21351
   253
val defer_recdef = gen_defer_recdef Tfl.defer IsarCmd.apply_theorems;
wenzelm@21351
   254
val defer_recdef_i = gen_defer_recdef Tfl.defer_i IsarCmd.apply_theorems_i;
wenzelm@6557
   255
wenzelm@6557
   256
wenzelm@6557
   257
wenzelm@10775
   258
(** recdef_tc(_i) **)
wenzelm@10775
   259
wenzelm@21351
   260
fun gen_recdef_tc prep_att prep_name (bname, raw_atts) raw_name opt_i lthy =
wenzelm@10775
   261
  let
wenzelm@21351
   262
    val thy = ProofContext.theory_of lthy;
wenzelm@16458
   263
    val name = prep_name thy raw_name;
wenzelm@10775
   264
    val atts = map (prep_att thy) raw_atts;
wenzelm@10775
   265
    val tcs =
wenzelm@10775
   266
      (case get_recdef thy name of
skalberg@15531
   267
        NONE => error ("No recdef definition of constant: " ^ quote name)
skalberg@15531
   268
      | SOME {tcs, ...} => tcs);
wenzelm@21351
   269
    val i = the_default 1 opt_i;
wenzelm@21351
   270
    val tc = nth tcs (i - 1) handle Subscript =>
wenzelm@10775
   271
      error ("No termination condition #" ^ string_of_int i ^
wenzelm@10775
   272
        " in recdef definition of " ^ quote name);
wenzelm@21351
   273
  in
wenzelm@21434
   274
    Specification.theorem_i Thm.internalK NONE (K I) (bname, atts)
wenzelm@21351
   275
      [] (Element.Shows [(("", []), [(HOLogic.mk_Trueprop tc, [])])]) lthy
wenzelm@21351
   276
  end;
wenzelm@10775
   277
wenzelm@21351
   278
val recdef_tc = gen_recdef_tc Attrib.intern_src Sign.intern_const;
wenzelm@10775
   279
val recdef_tc_i = gen_recdef_tc (K I) (K I);
wenzelm@10775
   280
wenzelm@10775
   281
wenzelm@10775
   282
wenzelm@6439
   283
(** package setup **)
wenzelm@6439
   284
wenzelm@6439
   285
(* setup theory *)
wenzelm@6439
   286
wenzelm@9859
   287
val setup =
wenzelm@9859
   288
  Attrib.add_attributes
wenzelm@18728
   289
   [(recdef_simpN, Attrib.add_del_args simp_add simp_del, "declaration of recdef simp rule"),
krauss@19770
   290
    (recdef_congN, Attrib.add_del_args cong_add cong_del, "declaration of recdef cong rule"),
wenzelm@18728
   291
    (recdef_wfN, Attrib.add_del_args wf_add wf_del, "declaration of recdef wf rule")];
wenzelm@6439
   292
wenzelm@6439
   293
wenzelm@6429
   294
(* outer syntax *)
wenzelm@6429
   295
wenzelm@17057
   296
local structure P = OuterParse and K = OuterKeyword in
wenzelm@6429
   297
wenzelm@9859
   298
val hints =
wenzelm@9859
   299
  P.$$$ "(" |-- P.!!! (P.position (P.$$$ "hints" -- P.arguments) --| P.$$$ ")") >> Args.src;
wenzelm@9859
   300
wenzelm@6429
   301
val recdef_decl =
wenzelm@11629
   302
  Scan.optional (P.$$$ "(" -- P.!!! (P.$$$ "permissive" -- P.$$$ ")") >> K false) true --
wenzelm@22101
   303
  P.name -- P.term -- Scan.repeat1 (SpecParse.opt_thm_name ":" -- P.prop) -- Scan.option hints
wenzelm@11629
   304
  >> (fn ((((p, f), R), eqs), src) => #1 o add_recdef p f R (map P.triple_swap eqs) src);
wenzelm@6429
   305
wenzelm@6429
   306
val recdefP =
wenzelm@6723
   307
  OuterSyntax.command "recdef" "define general recursive functions (TFL)" K.thy_decl
wenzelm@6429
   308
    (recdef_decl >> Toplevel.theory);
wenzelm@6429
   309
wenzelm@6557
   310
wenzelm@6557
   311
val defer_recdef_decl =
wenzelm@8657
   312
  P.name -- Scan.repeat1 P.prop --
wenzelm@22101
   313
  Scan.optional (P.$$$ "(" |-- P.$$$ "congs" |-- P.!!! (SpecParse.xthms1 --| P.$$$ ")")) []
wenzelm@6557
   314
  >> (fn ((f, eqs), congs) => #1 o defer_recdef f eqs congs);
wenzelm@6557
   315
wenzelm@6557
   316
val defer_recdefP =
wenzelm@6723
   317
  OuterSyntax.command "defer_recdef" "defer general recursive functions (TFL)" K.thy_decl
wenzelm@6557
   318
    (defer_recdef_decl >> Toplevel.theory);
wenzelm@6557
   319
wenzelm@10775
   320
val recdef_tcP =
wenzelm@10775
   321
  OuterSyntax.command "recdef_tc" "recommence proof of termination condition (TFL)" K.thy_goal
wenzelm@22101
   322
    (P.opt_target --
wenzelm@22101
   323
      SpecParse.opt_thm_name ":" -- P.xname -- Scan.option (P.$$$ "(" |-- P.nat --| P.$$$ ")")
wenzelm@21351
   324
      >> (fn (((loc, thm_name), name), i) =>
wenzelm@21351
   325
        Toplevel.print o Toplevel.local_theory_to_proof loc (recdef_tc thm_name name i)));
wenzelm@10775
   326
wenzelm@10775
   327
wenzelm@11629
   328
val _ = OuterSyntax.add_keywords ["permissive", "congs", "hints"];
wenzelm@10775
   329
val _ = OuterSyntax.add_parsers [recdefP, defer_recdefP, recdef_tcP];
wenzelm@6429
   330
wenzelm@6429
   331
end;
wenzelm@6429
   332
wenzelm@6429
   333
end;