src/HOL/Tools/recdef.ML
author Philipp Meyer
Wed Sep 30 11:33:59 2009 +0200 (2009-09-30)
changeset 32866 1238cbb7c08f
parent 32863 5e8cef567042
child 32952 aeb1e44fbc19
permissions -rw-r--r--
atp_minimal using chain_ths again
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
wenzelm@8657
    10
    -> {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
wenzelm@9859
    20
      * {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 ->
wenzelm@9859
    22
    theory -> theory * {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
wenzelm@8657
    87
type recdef_info = {simps: thm list, rules: thm list list, induct: thm, tcs: term list};
wenzelm@6439
    88
wenzelm@16458
    89
structure GlobalRecdefData = TheoryDataFun
wenzelm@22846
    90
(
wenzelm@9859
    91
  type T = recdef_info Symtab.table * hints;
wenzelm@9879
    92
  val empty = (Symtab.empty, mk_hints ([], [], [])): T;
wenzelm@6557
    93
  val copy = I;
wenzelm@16458
    94
  val extend = I;
wenzelm@16458
    95
  fun merge _
wenzelm@9859
    96
   ((tab1, {simps = simps1, congs = congs1, wfs = wfs1}),
wenzelm@16458
    97
    (tab2, {simps = simps2, congs = congs2, wfs = wfs2})) : T =
wenzelm@9859
    98
      (Symtab.merge (K true) (tab1, tab2),
wenzelm@24039
    99
        mk_hints (Thm.merge_thms (simps1, simps2),
wenzelm@22360
   100
          AList.merge (op =) Thm.eq_thm (congs1, congs2),
wenzelm@24039
   101
          Thm.merge_thms (wfs1, wfs2)));
wenzelm@22846
   102
);
wenzelm@6439
   103
wenzelm@17412
   104
val get_recdef = Symtab.lookup o #1 o GlobalRecdefData.get;
wenzelm@6439
   105
wenzelm@6439
   106
fun put_recdef name info thy =
wenzelm@6429
   107
  let
wenzelm@9859
   108
    val (tab, hints) = GlobalRecdefData.get thy;
wenzelm@17412
   109
    val tab' = Symtab.update_new (name, info) tab
wenzelm@6439
   110
      handle Symtab.DUP _ => error ("Duplicate recursive function definition " ^ quote name);
wenzelm@9859
   111
  in GlobalRecdefData.put (tab', hints) thy end;
wenzelm@9859
   112
wenzelm@9859
   113
val get_global_hints = #2 o GlobalRecdefData.get;
wenzelm@9859
   114
wenzelm@9859
   115
wenzelm@17920
   116
(* proof data *)
wenzelm@9859
   117
wenzelm@16458
   118
structure LocalRecdefData = ProofDataFun
wenzelm@22846
   119
(
wenzelm@9859
   120
  type T = hints;
wenzelm@9859
   121
  val init = get_global_hints;
wenzelm@22846
   122
);
wenzelm@9859
   123
wenzelm@21505
   124
val get_hints = LocalRecdefData.get;
wenzelm@21505
   125
fun map_hints f = Context.mapping (GlobalRecdefData.map (apsnd f)) (LocalRecdefData.map f);
wenzelm@9859
   126
wenzelm@20291
   127
wenzelm@20291
   128
(* attributes *)
wenzelm@20291
   129
wenzelm@18728
   130
fun attrib f = Thm.declaration_attribute (map_hints o f);
wenzelm@9859
   131
wenzelm@24039
   132
val simp_add = attrib (map_simps o Thm.add_thm);
wenzelm@24039
   133
val simp_del = attrib (map_simps o Thm.del_thm);
wenzelm@18688
   134
val cong_add = attrib (map_congs o add_cong);
wenzelm@18688
   135
val cong_del = attrib (map_congs o del_cong);
wenzelm@24039
   136
val wf_add = attrib (map_wfs o Thm.add_thm);
wenzelm@24039
   137
val wf_del = attrib (map_wfs o Thm.del_thm);
wenzelm@9859
   138
wenzelm@9859
   139
wenzelm@9949
   140
(* modifiers *)
wenzelm@9859
   141
wenzelm@9949
   142
val recdef_simpN = "recdef_simp";
wenzelm@9949
   143
val recdef_congN = "recdef_cong";
wenzelm@9949
   144
val recdef_wfN = "recdef_wf";
wenzelm@9859
   145
wenzelm@9859
   146
val recdef_modifiers =
wenzelm@18728
   147
 [Args.$$$ recdef_simpN -- Args.colon >> K ((I, simp_add): Method.modifier),
wenzelm@18728
   148
  Args.$$$ recdef_simpN -- Args.add -- Args.colon >> K (I, simp_add),
wenzelm@18728
   149
  Args.$$$ recdef_simpN -- Args.del -- Args.colon >> K (I, simp_del),
wenzelm@18728
   150
  Args.$$$ recdef_congN -- Args.colon >> K (I, cong_add),
wenzelm@18728
   151
  Args.$$$ recdef_congN -- Args.add -- Args.colon >> K (I, cong_add),
wenzelm@18728
   152
  Args.$$$ recdef_congN -- Args.del -- Args.colon >> K (I, cong_del),
wenzelm@18728
   153
  Args.$$$ recdef_wfN -- Args.colon >> K (I, wf_add),
wenzelm@18728
   154
  Args.$$$ recdef_wfN -- Args.add -- Args.colon >> K (I, wf_add),
wenzelm@18728
   155
  Args.$$$ recdef_wfN -- Args.del -- Args.colon >> K (I, wf_del)] @
wenzelm@9949
   156
  Clasimp.clasimp_modifiers;
wenzelm@9859
   157
wenzelm@9949
   158
wenzelm@9859
   159
wenzelm@9949
   160
(** prepare_hints(_i) **)
wenzelm@9859
   161
wenzelm@9859
   162
fun prepare_hints thy opt_src =
wenzelm@9859
   163
  let
wenzelm@9859
   164
    val ctxt0 = ProofContext.init thy;
wenzelm@9859
   165
    val ctxt =
wenzelm@9859
   166
      (case opt_src of
skalberg@15531
   167
        NONE => ctxt0
wenzelm@31243
   168
      | SOME src => #2 (Method.syntax (Method.sections recdef_modifiers) src ctxt0));
wenzelm@21505
   169
    val {simps, congs, wfs} = get_hints ctxt;
wenzelm@32149
   170
    val cs = claset_of ctxt;
wenzelm@32149
   171
    val ss = simpset_of ctxt addsimps simps;
haftmann@21098
   172
  in (cs, ss, rev (map snd congs), wfs) end;
wenzelm@9859
   173
wenzelm@9859
   174
fun prepare_hints_i thy () =
wenzelm@15032
   175
  let
wenzelm@15032
   176
    val ctxt0 = ProofContext.init thy;
wenzelm@15032
   177
    val {simps, congs, wfs} = get_global_hints thy;
wenzelm@32149
   178
  in (claset_of ctxt0, simpset_of ctxt0 addsimps simps, rev (map snd congs), wfs) end;
wenzelm@9859
   179
wenzelm@6439
   180
wenzelm@6439
   181
wenzelm@6439
   182
(** add_recdef(_i) **)
wenzelm@6439
   183
wenzelm@6557
   184
fun requires_recdef thy = Theory.requires thy "Recdef" "recursive functions";
wenzelm@6557
   185
wenzelm@17920
   186
fun gen_add_recdef tfl_fn prep_att prep_hints not_permissive raw_name R eq_srcs hints thy =
wenzelm@6439
   187
  let
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.*)
paulson@14241
   201
    val (thy, {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@31902
   205
    val simp_att = if null tcs then [Simplifier.simp_add, Nitpick_Const_Simps.add,
wenzelm@31902
   206
      Code.add_default_eqn_attribute, Quickcheck_RecFun_Simps.add] else [];
wenzelm@8657
   207
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
haftmann@29579
   212
        (((Binding.name "simps", List.concat rules), simp_att) :: ((eq_names ~~ rules) ~~ eq_atts))
haftmann@29579
   213
      ||>> PureThy.add_thms [((Binding.name "induct", induct), [])];
wenzelm@8657
   214
    val result = {simps = simps', rules = rules', induct = induct', tcs = tcs};
wenzelm@7798
   215
    val thy =
wenzelm@7798
   216
      thy
wenzelm@6439
   217
      |> put_recdef name result
wenzelm@24712
   218
      |> Sign.parent_path;
wenzelm@7798
   219
  in (thy, result) end;
wenzelm@6429
   220
wenzelm@18728
   221
val add_recdef = gen_add_recdef Tfl.define Attrib.attribute prepare_hints;
wenzelm@11629
   222
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
   223
wenzelm@9859
   224
wenzelm@6557
   225
wenzelm@6557
   226
(** defer_recdef(_i) **)
wenzelm@6557
   227
wenzelm@27727
   228
fun gen_defer_recdef tfl_fn eval_thms raw_name eqs raw_congs thy =
wenzelm@6557
   229
  let
wenzelm@16458
   230
    val name = Sign.intern_const thy raw_name;
wenzelm@30364
   231
    val bname = Long_Name.base_name name;
wenzelm@6557
   232
wenzelm@6557
   233
    val _ = requires_recdef thy;
wenzelm@26478
   234
    val _ = writeln ("Deferred recursive function " ^ quote name ^ " ...");
wenzelm@6557
   235
wenzelm@27727
   236
    val congs = eval_thms (ProofContext.init thy) raw_congs;
wenzelm@27727
   237
    val (thy2, induct_rules) = tfl_fn thy congs name eqs;
haftmann@18377
   238
    val ([induct_rules'], thy3) =
wenzelm@6557
   239
      thy2
wenzelm@24712
   240
      |> Sign.add_path bname
haftmann@29579
   241
      |> PureThy.add_thms [((Binding.name "induct_rules", induct_rules), [])]
wenzelm@24712
   242
      ||> Sign.parent_path;
wenzelm@8430
   243
  in (thy3, {induct_rules = induct_rules'}) end;
wenzelm@6557
   244
wenzelm@27727
   245
val defer_recdef = gen_defer_recdef Tfl.defer Attrib.eval_thms;
wenzelm@27727
   246
val defer_recdef_i = gen_defer_recdef Tfl.defer_i (K I);
wenzelm@6557
   247
wenzelm@6557
   248
wenzelm@6557
   249
wenzelm@10775
   250
(** recdef_tc(_i) **)
wenzelm@10775
   251
berghofe@24457
   252
fun gen_recdef_tc prep_att prep_name (bname, raw_atts) raw_name opt_i int lthy =
wenzelm@10775
   253
  let
wenzelm@21351
   254
    val thy = ProofContext.theory_of lthy;
wenzelm@16458
   255
    val name = prep_name thy raw_name;
wenzelm@10775
   256
    val atts = map (prep_att thy) raw_atts;
wenzelm@10775
   257
    val tcs =
wenzelm@10775
   258
      (case get_recdef thy name of
skalberg@15531
   259
        NONE => error ("No recdef definition of constant: " ^ quote name)
skalberg@15531
   260
      | SOME {tcs, ...} => tcs);
wenzelm@21351
   261
    val i = the_default 1 opt_i;
wenzelm@21351
   262
    val tc = nth tcs (i - 1) handle Subscript =>
wenzelm@10775
   263
      error ("No termination condition #" ^ string_of_int i ^
wenzelm@10775
   264
        " in recdef definition of " ^ quote name);
wenzelm@21351
   265
  in
haftmann@28965
   266
    Specification.theorem Thm.internalK NONE (K I) (Binding.name bname, atts)
haftmann@28965
   267
      [] (Element.Shows [(Attrib.empty_binding, [(HOLogic.mk_Trueprop tc, [])])]) int lthy
wenzelm@21351
   268
  end;
wenzelm@10775
   269
wenzelm@21351
   270
val recdef_tc = gen_recdef_tc Attrib.intern_src Sign.intern_const;
wenzelm@10775
   271
val recdef_tc_i = gen_recdef_tc (K I) (K I);
wenzelm@10775
   272
wenzelm@10775
   273
wenzelm@10775
   274
wenzelm@6439
   275
(** package setup **)
wenzelm@6439
   276
wenzelm@6439
   277
(* setup theory *)
wenzelm@6439
   278
wenzelm@9859
   279
val setup =
wenzelm@30528
   280
  Attrib.setup @{binding recdef_simp} (Attrib.add_del simp_add simp_del)
wenzelm@30528
   281
    "declaration of recdef simp rule" #>
wenzelm@30528
   282
  Attrib.setup @{binding recdef_cong} (Attrib.add_del cong_add cong_del)
wenzelm@30528
   283
    "declaration of recdef cong rule" #>
wenzelm@30528
   284
  Attrib.setup @{binding recdef_wf} (Attrib.add_del wf_add wf_del)
wenzelm@30528
   285
    "declaration of recdef wf rule";
wenzelm@6439
   286
wenzelm@6439
   287
wenzelm@6429
   288
(* outer syntax *)
wenzelm@6429
   289
wenzelm@17057
   290
local structure P = OuterParse and K = OuterKeyword in
wenzelm@6429
   291
wenzelm@27353
   292
val _ = List.app OuterKeyword.keyword ["permissive", "congs", "hints"];
wenzelm@24867
   293
wenzelm@9859
   294
val hints =
wenzelm@27809
   295
  P.$$$ "(" |-- P.!!! (P.position (P.$$$ "hints" -- Args.parse) --| P.$$$ ")") >> Args.src;
wenzelm@9859
   296
wenzelm@6429
   297
val recdef_decl =
wenzelm@11629
   298
  Scan.optional (P.$$$ "(" -- P.!!! (P.$$$ "permissive" -- P.$$$ ")") >> K false) true --
haftmann@29579
   299
  P.name -- P.term -- Scan.repeat1 (SpecParse.opt_thm_name ":" -- P.prop)
wenzelm@28083
   300
    -- Scan.option hints
wenzelm@11629
   301
  >> (fn ((((p, f), R), eqs), src) => #1 o add_recdef p f R (map P.triple_swap eqs) src);
wenzelm@6429
   302
wenzelm@24867
   303
val _ =
wenzelm@6723
   304
  OuterSyntax.command "recdef" "define general recursive functions (TFL)" K.thy_decl
wenzelm@6429
   305
    (recdef_decl >> Toplevel.theory);
wenzelm@6429
   306
wenzelm@6557
   307
wenzelm@6557
   308
val defer_recdef_decl =
wenzelm@8657
   309
  P.name -- Scan.repeat1 P.prop --
wenzelm@22101
   310
  Scan.optional (P.$$$ "(" |-- P.$$$ "congs" |-- P.!!! (SpecParse.xthms1 --| P.$$$ ")")) []
wenzelm@6557
   311
  >> (fn ((f, eqs), congs) => #1 o defer_recdef f eqs congs);
wenzelm@6557
   312
wenzelm@24867
   313
val _ =
wenzelm@6723
   314
  OuterSyntax.command "defer_recdef" "defer general recursive functions (TFL)" K.thy_decl
wenzelm@6557
   315
    (defer_recdef_decl >> Toplevel.theory);
wenzelm@6557
   316
wenzelm@24867
   317
val _ =
wenzelm@26988
   318
  OuterSyntax.local_theory_to_proof' "recdef_tc" "recommence proof of termination condition (TFL)"
wenzelm@26988
   319
    K.thy_goal
wenzelm@30223
   320
    ((SpecParse.opt_thm_name ":" >> apfst Binding.name_of) -- P.xname --
wenzelm@28083
   321
        Scan.option (P.$$$ "(" |-- P.nat --| P.$$$ ")")
wenzelm@26988
   322
      >> (fn ((thm_name, name), i) => recdef_tc thm_name name i));
wenzelm@10775
   323
wenzelm@6429
   324
end;
wenzelm@6429
   325
wenzelm@6429
   326
end;