src/HOL/Tools/recdef.ML
author bulwahn
Fri Mar 12 12:14:30 2010 +0100 (2010-03-12)
changeset 35756 cfde251d03a5
parent 35690 863bee3a9153
child 36610 bafd82950e24
permissions -rw-r--r--
refining and adding Spec_Rules to definitional packages old_primrec, primrec, recdef, size and function
haftmann@31723
     1
(*  Title:      HOL/Tools/recdef.ML
wenzelm@6429
     2
    Author:     Markus Wenzel, TU Muenchen
wenzelm@6429
     3
wenzelm@6429
     4
Wrapper module for Konrad Slind's TFL package.
wenzelm@6429
     5
*)
wenzelm@6429
     6
haftmann@31723
     7
signature RECDEF =
wenzelm@6429
     8
sig
wenzelm@8657
     9
  val get_recdef: theory -> string
bulwahn@35756
    10
    -> {lhs: term, simps: thm list, rules: thm list list, induct: thm, tcs: term list} option
wenzelm@21505
    11
  val get_hints: Proof.context -> {simps: thm list, congs: (string * thm) list, wfs: thm list}
wenzelm@18728
    12
  val simp_add: attribute
wenzelm@18728
    13
  val simp_del: attribute
wenzelm@18728
    14
  val cong_add: attribute
wenzelm@18728
    15
  val cong_del: attribute
wenzelm@18728
    16
  val wf_add: attribute
wenzelm@18728
    17
  val wf_del: attribute
haftmann@29579
    18
  val add_recdef: bool -> xstring -> string -> ((binding * string) * Attrib.src list) list ->
wenzelm@15703
    19
    Attrib.src option -> theory -> theory
bulwahn@35756
    20
      * {lhs: term, simps: thm list, rules: thm list list, induct: thm, tcs: term list}
haftmann@29579
    21
  val add_recdef_i: bool -> xstring -> term -> ((binding * term) * attribute list) list ->
bulwahn@35756
    22
    theory -> theory * {lhs: term, simps: thm list, rules: thm list list, induct: thm, tcs: term list}
wenzelm@26336
    23
  val defer_recdef: xstring -> string list -> (Facts.ref * Attrib.src list) list
wenzelm@6557
    24
    -> theory -> theory * {induct_rules: thm}
wenzelm@27727
    25
  val defer_recdef_i: xstring -> term list -> thm list -> theory -> theory * {induct_rules: thm}
wenzelm@27727
    26
  val recdef_tc: bstring * Attrib.src list -> xstring -> int option -> bool ->
wenzelm@27727
    27
    local_theory -> Proof.state
wenzelm@27727
    28
  val recdef_tc_i: bstring * Attrib.src list -> string -> int option -> bool ->
wenzelm@27727
    29
    local_theory -> Proof.state
wenzelm@18708
    30
  val setup: theory -> theory
wenzelm@6429
    31
end;
wenzelm@6429
    32
haftmann@31723
    33
structure Recdef: RECDEF =
wenzelm@6429
    34
struct
wenzelm@6429
    35
wenzelm@6429
    36
wenzelm@9859
    37
(** recdef hints **)
wenzelm@6439
    38
wenzelm@9859
    39
(* type hints *)
wenzelm@9859
    40
wenzelm@9859
    41
type hints = {simps: thm list, congs: (string * thm) list, wfs: thm list};
wenzelm@9859
    42
wenzelm@9859
    43
fun mk_hints (simps, congs, wfs) = {simps = simps, congs = congs, wfs = wfs}: hints;
wenzelm@9859
    44
fun map_hints f ({simps, congs, wfs}: hints) = mk_hints (f (simps, congs, wfs));
wenzelm@9859
    45
wenzelm@9859
    46
fun map_simps f = map_hints (fn (simps, congs, wfs) => (f simps, congs, wfs));
wenzelm@9859
    47
fun map_congs f = map_hints (fn (simps, congs, wfs) => (simps, f congs, wfs));
wenzelm@9859
    48
fun map_wfs f = map_hints (fn (simps, congs, wfs) => (simps, congs, f wfs));
wenzelm@9859
    49
wenzelm@9859
    50
wenzelm@9859
    51
(* congruence rules *)
wenzelm@9859
    52
wenzelm@9859
    53
local
wenzelm@9859
    54
wenzelm@9859
    55
val cong_head =
wenzelm@9859
    56
  fst o Term.dest_Const o Term.head_of o fst o Logic.dest_equals o Thm.concl_of;
wenzelm@6439
    57
wenzelm@9859
    58
fun prep_cong raw_thm =
wenzelm@9859
    59
  let val thm = safe_mk_meta_eq raw_thm in (cong_head thm, thm) end;
wenzelm@9859
    60
wenzelm@9859
    61
in
wenzelm@9859
    62
wenzelm@9859
    63
fun add_cong raw_thm congs =
haftmann@21098
    64
  let
haftmann@21098
    65
    val (c, thm) = prep_cong raw_thm;
haftmann@21098
    66
    val _ = if AList.defined (op =) congs c
haftmann@21098
    67
      then warning ("Overwriting recdef congruence rule for " ^ quote c)
haftmann@21098
    68
      else ();
haftmann@21098
    69
  in AList.update (op =) (c, thm) congs end;
wenzelm@9859
    70
wenzelm@9859
    71
fun del_cong raw_thm congs =
wenzelm@9859
    72
  let
wenzelm@9859
    73
    val (c, thm) = prep_cong raw_thm;
haftmann@21098
    74
    val _ = if AList.defined (op =) congs c
haftmann@21098
    75
      then ()
haftmann@21098
    76
      else warning ("No recdef congruence rule for " ^ quote c);
haftmann@21098
    77
  in AList.delete (op =) c congs end;
wenzelm@9859
    78
wenzelm@9859
    79
end;
wenzelm@9859
    80
wenzelm@9859
    81
wenzelm@9859
    82
wenzelm@9859
    83
(** global and local recdef data **)
wenzelm@9859
    84
wenzelm@17920
    85
(* theory data *)
wenzelm@6439
    86
bulwahn@35756
    87
type recdef_info = {lhs: term, simps: thm list, rules: thm list list, induct: thm, tcs: term list};
wenzelm@6439
    88
wenzelm@33522
    89
structure GlobalRecdefData = Theory_Data
wenzelm@22846
    90
(
wenzelm@9859
    91
  type T = recdef_info Symtab.table * hints;
wenzelm@9879
    92
  val empty = (Symtab.empty, mk_hints ([], [], [])): T;
wenzelm@16458
    93
  val extend = I;
wenzelm@33522
    94
  fun merge
wenzelm@9859
    95
   ((tab1, {simps = simps1, congs = congs1, wfs = wfs1}),
wenzelm@16458
    96
    (tab2, {simps = simps2, congs = congs2, wfs = wfs2})) : T =
wenzelm@9859
    97
      (Symtab.merge (K true) (tab1, tab2),
wenzelm@24039
    98
        mk_hints (Thm.merge_thms (simps1, simps2),
wenzelm@33699
    99
          AList.merge (op =) (K true) (congs1, congs2),
wenzelm@24039
   100
          Thm.merge_thms (wfs1, wfs2)));
wenzelm@22846
   101
);
wenzelm@6439
   102
wenzelm@17412
   103
val get_recdef = Symtab.lookup o #1 o GlobalRecdefData.get;
wenzelm@6439
   104
wenzelm@6439
   105
fun put_recdef name info thy =
wenzelm@6429
   106
  let
wenzelm@9859
   107
    val (tab, hints) = GlobalRecdefData.get thy;
wenzelm@17412
   108
    val tab' = Symtab.update_new (name, info) tab
wenzelm@6439
   109
      handle Symtab.DUP _ => error ("Duplicate recursive function definition " ^ quote name);
wenzelm@9859
   110
  in GlobalRecdefData.put (tab', hints) thy end;
wenzelm@9859
   111
wenzelm@9859
   112
val get_global_hints = #2 o GlobalRecdefData.get;
wenzelm@9859
   113
wenzelm@9859
   114
wenzelm@17920
   115
(* proof data *)
wenzelm@9859
   116
wenzelm@33519
   117
structure LocalRecdefData = Proof_Data
wenzelm@22846
   118
(
wenzelm@9859
   119
  type T = hints;
wenzelm@9859
   120
  val init = get_global_hints;
wenzelm@22846
   121
);
wenzelm@9859
   122
wenzelm@21505
   123
val get_hints = LocalRecdefData.get;
wenzelm@21505
   124
fun map_hints f = Context.mapping (GlobalRecdefData.map (apsnd f)) (LocalRecdefData.map f);
wenzelm@9859
   125
wenzelm@20291
   126
wenzelm@20291
   127
(* attributes *)
wenzelm@20291
   128
wenzelm@18728
   129
fun attrib f = Thm.declaration_attribute (map_hints o f);
wenzelm@9859
   130
wenzelm@24039
   131
val simp_add = attrib (map_simps o Thm.add_thm);
wenzelm@24039
   132
val simp_del = attrib (map_simps o Thm.del_thm);
wenzelm@18688
   133
val cong_add = attrib (map_congs o add_cong);
wenzelm@18688
   134
val cong_del = attrib (map_congs o del_cong);
wenzelm@24039
   135
val wf_add = attrib (map_wfs o Thm.add_thm);
wenzelm@24039
   136
val wf_del = attrib (map_wfs o Thm.del_thm);
wenzelm@9859
   137
wenzelm@9859
   138
wenzelm@9949
   139
(* modifiers *)
wenzelm@9859
   140
wenzelm@9949
   141
val recdef_simpN = "recdef_simp";
wenzelm@9949
   142
val recdef_congN = "recdef_cong";
wenzelm@9949
   143
val recdef_wfN = "recdef_wf";
wenzelm@9859
   144
wenzelm@9859
   145
val recdef_modifiers =
wenzelm@18728
   146
 [Args.$$$ recdef_simpN -- Args.colon >> K ((I, simp_add): Method.modifier),
wenzelm@18728
   147
  Args.$$$ recdef_simpN -- Args.add -- Args.colon >> K (I, simp_add),
wenzelm@18728
   148
  Args.$$$ recdef_simpN -- Args.del -- Args.colon >> K (I, simp_del),
wenzelm@18728
   149
  Args.$$$ recdef_congN -- Args.colon >> K (I, cong_add),
wenzelm@18728
   150
  Args.$$$ recdef_congN -- Args.add -- Args.colon >> K (I, cong_add),
wenzelm@18728
   151
  Args.$$$ recdef_congN -- Args.del -- Args.colon >> K (I, cong_del),
wenzelm@18728
   152
  Args.$$$ recdef_wfN -- Args.colon >> K (I, wf_add),
wenzelm@18728
   153
  Args.$$$ recdef_wfN -- Args.add -- Args.colon >> K (I, wf_add),
wenzelm@18728
   154
  Args.$$$ recdef_wfN -- Args.del -- Args.colon >> K (I, wf_del)] @
wenzelm@9949
   155
  Clasimp.clasimp_modifiers;
wenzelm@9859
   156
wenzelm@9949
   157
wenzelm@9859
   158
wenzelm@9949
   159
(** prepare_hints(_i) **)
wenzelm@9859
   160
wenzelm@9859
   161
fun prepare_hints thy opt_src =
wenzelm@9859
   162
  let
wenzelm@9859
   163
    val ctxt0 = ProofContext.init thy;
wenzelm@9859
   164
    val ctxt =
wenzelm@9859
   165
      (case opt_src of
skalberg@15531
   166
        NONE => ctxt0
wenzelm@31243
   167
      | SOME src => #2 (Method.syntax (Method.sections recdef_modifiers) src ctxt0));
wenzelm@21505
   168
    val {simps, congs, wfs} = get_hints ctxt;
wenzelm@32149
   169
    val cs = claset_of ctxt;
wenzelm@32149
   170
    val ss = simpset_of ctxt addsimps simps;
haftmann@21098
   171
  in (cs, ss, rev (map snd congs), wfs) end;
wenzelm@9859
   172
wenzelm@9859
   173
fun prepare_hints_i thy () =
wenzelm@15032
   174
  let
wenzelm@15032
   175
    val ctxt0 = ProofContext.init thy;
wenzelm@15032
   176
    val {simps, congs, wfs} = get_global_hints thy;
wenzelm@32149
   177
  in (claset_of ctxt0, simpset_of ctxt0 addsimps simps, rev (map snd congs), wfs) end;
wenzelm@9859
   178
wenzelm@6439
   179
wenzelm@6439
   180
wenzelm@6439
   181
(** add_recdef(_i) **)
wenzelm@6439
   182
wenzelm@6557
   183
fun requires_recdef thy = Theory.requires thy "Recdef" "recursive functions";
wenzelm@6557
   184
wenzelm@17920
   185
fun gen_add_recdef tfl_fn prep_att prep_hints not_permissive raw_name R eq_srcs hints thy =
wenzelm@6439
   186
  let
haftmann@35690
   187
    val _ = legacy_feature ("\"recdef\"; prefer \"function\" instead");
wenzelm@9859
   188
    val _ = requires_recdef thy;
wenzelm@9859
   189
wenzelm@16458
   190
    val name = Sign.intern_const thy raw_name;
wenzelm@30364
   191
    val bname = Long_Name.base_name name;
wenzelm@26478
   192
    val _ = writeln ("Defining recursive function " ^ quote name ^ " ...");
wenzelm@6429
   193
wenzelm@8657
   194
    val ((eq_names, eqs), raw_eq_atts) = apfst split_list (split_list eq_srcs);
wenzelm@8657
   195
    val eq_atts = map (map (prep_att thy)) raw_eq_atts;
wenzelm@8657
   196
wenzelm@9859
   197
    val (cs, ss, congs, wfs) = prep_hints thy hints;
paulson@14241
   198
    (*We must remove imp_cong to prevent looping when the induction rule
paulson@14241
   199
      is simplified. Many induction rules have nested implications that would
paulson@14241
   200
      give rise to looping conditional rewriting.*)
bulwahn@35756
   201
    val (thy, {lhs, rules = rules_idx, induct, tcs}) =
wenzelm@17920
   202
        tfl_fn not_permissive thy cs (ss delcongs [imp_cong])
paulson@14241
   203
               congs wfs name R eqs;
haftmann@21098
   204
    val rules = (map o map) fst (partition_eq (eq_snd (op = : int * int -> bool)) rules_idx);
wenzelm@33552
   205
    val simp_att =
wenzelm@33552
   206
      if null tcs then [Simplifier.simp_add, Nitpick_Simps.add, Code.add_default_eqn_attribute]
wenzelm@33552
   207
      else [];
haftmann@18377
   208
    val ((simps' :: rules', [induct']), thy) =
wenzelm@7798
   209
      thy
wenzelm@24712
   210
      |> Sign.add_path bname
wenzelm@18688
   211
      |> PureThy.add_thmss
wenzelm@32952
   212
        (((Binding.name "simps", flat rules), simp_att) :: ((eq_names ~~ rules) ~~ eq_atts))
bulwahn@34952
   213
      ||>> PureThy.add_thms [((Binding.name "induct", induct), [])]
bulwahn@35756
   214
      ||> Spec_Rules.add_global Spec_Rules.Equational ([lhs], flat rules);
bulwahn@35756
   215
    val result = {lhs = lhs, simps = simps', rules = rules', induct = induct', tcs = tcs};
wenzelm@7798
   216
    val thy =
wenzelm@7798
   217
      thy
wenzelm@6439
   218
      |> put_recdef name result
wenzelm@24712
   219
      |> Sign.parent_path;
wenzelm@7798
   220
  in (thy, result) end;
wenzelm@6429
   221
wenzelm@18728
   222
val add_recdef = gen_add_recdef Tfl.define Attrib.attribute prepare_hints;
wenzelm@11629
   223
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
   224
wenzelm@9859
   225
wenzelm@6557
   226
wenzelm@6557
   227
(** defer_recdef(_i) **)
wenzelm@6557
   228
wenzelm@27727
   229
fun gen_defer_recdef tfl_fn eval_thms raw_name eqs raw_congs thy =
wenzelm@6557
   230
  let
wenzelm@16458
   231
    val name = Sign.intern_const thy raw_name;
wenzelm@30364
   232
    val bname = Long_Name.base_name name;
wenzelm@6557
   233
wenzelm@6557
   234
    val _ = requires_recdef thy;
wenzelm@26478
   235
    val _ = writeln ("Deferred recursive function " ^ quote name ^ " ...");
wenzelm@6557
   236
wenzelm@27727
   237
    val congs = eval_thms (ProofContext.init thy) raw_congs;
wenzelm@27727
   238
    val (thy2, induct_rules) = tfl_fn thy congs name eqs;
haftmann@18377
   239
    val ([induct_rules'], thy3) =
wenzelm@6557
   240
      thy2
wenzelm@24712
   241
      |> Sign.add_path bname
haftmann@29579
   242
      |> PureThy.add_thms [((Binding.name "induct_rules", induct_rules), [])]
wenzelm@24712
   243
      ||> Sign.parent_path;
wenzelm@8430
   244
  in (thy3, {induct_rules = induct_rules'}) end;
wenzelm@6557
   245
wenzelm@27727
   246
val defer_recdef = gen_defer_recdef Tfl.defer Attrib.eval_thms;
wenzelm@27727
   247
val defer_recdef_i = gen_defer_recdef Tfl.defer_i (K I);
wenzelm@6557
   248
wenzelm@6557
   249
wenzelm@6557
   250
wenzelm@10775
   251
(** recdef_tc(_i) **)
wenzelm@10775
   252
berghofe@24457
   253
fun gen_recdef_tc prep_att prep_name (bname, raw_atts) raw_name opt_i int lthy =
wenzelm@10775
   254
  let
wenzelm@21351
   255
    val thy = ProofContext.theory_of lthy;
wenzelm@16458
   256
    val name = prep_name thy raw_name;
wenzelm@10775
   257
    val atts = map (prep_att thy) raw_atts;
wenzelm@10775
   258
    val tcs =
wenzelm@10775
   259
      (case get_recdef thy name of
skalberg@15531
   260
        NONE => error ("No recdef definition of constant: " ^ quote name)
skalberg@15531
   261
      | SOME {tcs, ...} => tcs);
wenzelm@21351
   262
    val i = the_default 1 opt_i;
wenzelm@21351
   263
    val tc = nth tcs (i - 1) handle Subscript =>
wenzelm@10775
   264
      error ("No termination condition #" ^ string_of_int i ^
wenzelm@10775
   265
        " in recdef definition of " ^ quote name);
wenzelm@21351
   266
  in
wenzelm@33643
   267
    Specification.theorem "" NONE (K I)
wenzelm@33278
   268
      (Binding.conceal (Binding.name bname), atts) []
wenzelm@33278
   269
      (Element.Shows [(Attrib.empty_binding, [(HOLogic.mk_Trueprop tc, [])])]) int lthy
wenzelm@21351
   270
  end;
wenzelm@10775
   271
wenzelm@21351
   272
val recdef_tc = gen_recdef_tc Attrib.intern_src Sign.intern_const;
wenzelm@10775
   273
val recdef_tc_i = gen_recdef_tc (K I) (K I);
wenzelm@10775
   274
wenzelm@10775
   275
wenzelm@10775
   276
wenzelm@6439
   277
(** package setup **)
wenzelm@6439
   278
wenzelm@6439
   279
(* setup theory *)
wenzelm@6439
   280
wenzelm@9859
   281
val setup =
wenzelm@30528
   282
  Attrib.setup @{binding recdef_simp} (Attrib.add_del simp_add simp_del)
wenzelm@30528
   283
    "declaration of recdef simp rule" #>
wenzelm@30528
   284
  Attrib.setup @{binding recdef_cong} (Attrib.add_del cong_add cong_del)
wenzelm@30528
   285
    "declaration of recdef cong rule" #>
wenzelm@30528
   286
  Attrib.setup @{binding recdef_wf} (Attrib.add_del wf_add wf_del)
wenzelm@30528
   287
    "declaration of recdef wf rule";
wenzelm@6439
   288
wenzelm@6439
   289
wenzelm@6429
   290
(* outer syntax *)
wenzelm@6429
   291
wenzelm@17057
   292
local structure P = OuterParse and K = OuterKeyword in
wenzelm@6429
   293
wenzelm@27353
   294
val _ = List.app OuterKeyword.keyword ["permissive", "congs", "hints"];
wenzelm@24867
   295
wenzelm@9859
   296
val hints =
wenzelm@27809
   297
  P.$$$ "(" |-- P.!!! (P.position (P.$$$ "hints" -- Args.parse) --| P.$$$ ")") >> Args.src;
wenzelm@9859
   298
wenzelm@6429
   299
val recdef_decl =
wenzelm@11629
   300
  Scan.optional (P.$$$ "(" -- P.!!! (P.$$$ "permissive" -- P.$$$ ")") >> K false) true --
haftmann@29579
   301
  P.name -- P.term -- Scan.repeat1 (SpecParse.opt_thm_name ":" -- P.prop)
wenzelm@28083
   302
    -- Scan.option hints
wenzelm@11629
   303
  >> (fn ((((p, f), R), eqs), src) => #1 o add_recdef p f R (map P.triple_swap eqs) src);
wenzelm@6429
   304
wenzelm@24867
   305
val _ =
wenzelm@6723
   306
  OuterSyntax.command "recdef" "define general recursive functions (TFL)" K.thy_decl
wenzelm@6429
   307
    (recdef_decl >> Toplevel.theory);
wenzelm@6429
   308
wenzelm@6557
   309
wenzelm@6557
   310
val defer_recdef_decl =
wenzelm@8657
   311
  P.name -- Scan.repeat1 P.prop --
wenzelm@22101
   312
  Scan.optional (P.$$$ "(" |-- P.$$$ "congs" |-- P.!!! (SpecParse.xthms1 --| P.$$$ ")")) []
wenzelm@6557
   313
  >> (fn ((f, eqs), congs) => #1 o defer_recdef f eqs congs);
wenzelm@6557
   314
wenzelm@24867
   315
val _ =
wenzelm@6723
   316
  OuterSyntax.command "defer_recdef" "defer general recursive functions (TFL)" K.thy_decl
wenzelm@6557
   317
    (defer_recdef_decl >> Toplevel.theory);
wenzelm@6557
   318
wenzelm@24867
   319
val _ =
wenzelm@26988
   320
  OuterSyntax.local_theory_to_proof' "recdef_tc" "recommence proof of termination condition (TFL)"
wenzelm@26988
   321
    K.thy_goal
wenzelm@30223
   322
    ((SpecParse.opt_thm_name ":" >> apfst Binding.name_of) -- P.xname --
wenzelm@28083
   323
        Scan.option (P.$$$ "(" |-- P.nat --| P.$$$ ")")
wenzelm@26988
   324
      >> (fn ((thm_name, name), i) => recdef_tc thm_name name i));
wenzelm@10775
   325
wenzelm@6429
   326
end;
wenzelm@6429
   327
wenzelm@6429
   328
end;