src/HOL/Tools/recdef_package.ML
author nipkow
Thu Mar 30 19:47:17 2000 +0200 (2000-03-30)
changeset 8625 93a11ebacf32
parent 8481 89d498a8d3f6
child 8657 b9475dad85ed
permissions -rw-r--r--
If all termination conditions are proved automatically,
the recusrion equations are added to the simpset automatically.

recdef.rules -> recdef.simps
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
nipkow@8625
    12
  val get_recdef: theory -> string -> {simps: thm list, induct: thm, tcs: term list} option
wenzelm@6576
    13
  val add_recdef: xstring -> string -> string list -> simpset option
wenzelm@6576
    14
    -> (xstring * Args.src list) list -> theory
nipkow@8625
    15
    -> theory * {simps: thm list, induct: thm, tcs: term list}
wenzelm@6576
    16
  val add_recdef_i: xstring -> term -> term list -> simpset option
wenzelm@6576
    17
    -> (thm * theory attribute list) list
nipkow@8625
    18
    -> theory -> theory * {simps: thm list, induct: thm, tcs: term list}
wenzelm@6557
    19
  val defer_recdef: xstring -> string list -> (xstring * Args.src list) list
wenzelm@6557
    20
    -> theory -> theory * {induct_rules: thm}
wenzelm@6557
    21
  val defer_recdef_i: xstring -> term list -> (thm * theory attribute list) list
wenzelm@6557
    22
    -> theory -> theory * {induct_rules: thm}
wenzelm@6439
    23
  val setup: (theory -> theory) list
wenzelm@6429
    24
end;
wenzelm@6429
    25
wenzelm@6429
    26
structure RecdefPackage: RECDEF_PACKAGE =
wenzelm@6429
    27
struct
wenzelm@6429
    28
wenzelm@6429
    29
val quiet_mode = Tfl.quiet_mode;
wenzelm@6429
    30
val message = Tfl.message;
wenzelm@6429
    31
wenzelm@6429
    32
wenzelm@6439
    33
wenzelm@6439
    34
(** theory data **)
wenzelm@6439
    35
wenzelm@6439
    36
(* data kind 'HOL/recdef' *)
wenzelm@6439
    37
nipkow@8625
    38
type recdef_info = {simps: thm list, induct: thm, tcs: term list};
wenzelm@6439
    39
wenzelm@6439
    40
structure RecdefArgs =
wenzelm@6439
    41
struct
wenzelm@6439
    42
  val name = "HOL/recdef";
wenzelm@6439
    43
  type T = recdef_info Symtab.table;
wenzelm@6439
    44
wenzelm@6439
    45
  val empty = Symtab.empty;
wenzelm@6557
    46
  val copy = I;
wenzelm@6439
    47
  val prep_ext = I;
wenzelm@6439
    48
  val merge: T * T -> T = Symtab.merge (K true);
wenzelm@6439
    49
wenzelm@6439
    50
  fun print sg tab =
wenzelm@6439
    51
    Pretty.writeln (Pretty.strs ("recdefs:" ::
wenzelm@6851
    52
      map #1 (Sign.cond_extern_table sg Sign.constK tab)));
wenzelm@6439
    53
end;
wenzelm@6439
    54
wenzelm@6439
    55
structure RecdefData = TheoryDataFun(RecdefArgs);
wenzelm@6439
    56
val print_recdefs = RecdefData.print;
wenzelm@6429
    57
wenzelm@6439
    58
wenzelm@6439
    59
(* get and put data *)
wenzelm@6439
    60
berghofe@8481
    61
fun get_recdef thy name = Symtab.lookup (RecdefData.get thy, name);
wenzelm@6439
    62
wenzelm@6439
    63
fun put_recdef name info thy =
wenzelm@6429
    64
  let
wenzelm@6439
    65
    val tab = Symtab.update_new ((name, info), RecdefData.get thy)
wenzelm@6439
    66
      handle Symtab.DUP _ => error ("Duplicate recursive function definition " ^ quote name);
wenzelm@6439
    67
  in RecdefData.put tab thy end;
wenzelm@6439
    68
wenzelm@6439
    69
wenzelm@6439
    70
wenzelm@6439
    71
(** add_recdef(_i) **)
wenzelm@6439
    72
wenzelm@6557
    73
fun requires_recdef thy = Theory.requires thy "Recdef" "recursive functions";
wenzelm@6557
    74
nipkow@8625
    75
nipkow@8625
    76
fun prepare_simps no_tcs ixsimps =
nipkow@8625
    77
  let val partnd = partition_eq (fn ((_,i),(_,j)) => i=j) ixsimps;
nipkow@8625
    78
      val attr = if no_tcs then [Simplifier.simp_add_global] else []
nipkow@8625
    79
  in map (fn (rl,i)::rls => ((string_of_int i, rl::map fst rls), attr)) partnd
nipkow@8625
    80
  end;
nipkow@8625
    81
wenzelm@6576
    82
fun gen_add_recdef tfl_fn prep_ss app_thms raw_name R eqs raw_ss raw_congs thy =
wenzelm@6439
    83
  let
wenzelm@6439
    84
    val name = Sign.intern_const (Theory.sign_of thy) raw_name;
wenzelm@6439
    85
    val bname = Sign.base_name name;
wenzelm@6439
    86
wenzelm@6557
    87
    val _ = requires_recdef thy;
wenzelm@6429
    88
    val _ = message ("Defining recursive function " ^ quote name ^ " ...");
wenzelm@6429
    89
wenzelm@6429
    90
    val ss = (case raw_ss of None => Simplifier.simpset_of thy | Some x => prep_ss x);
wenzelm@7798
    91
    val (thy, congs) = thy |> app_thms raw_congs;
wenzelm@7798
    92
    val (thy, {rules, induct, tcs}) = tfl_fn thy name R (ss, congs) eqs;
nipkow@8625
    93
    val named_simps = prepare_simps (null tcs) rules
wenzelm@8430
    94
    val case_numbers = map Library.string_of_int (1 upto Thm.nprems_of induct);
nipkow@8625
    95
    val (thy, (simpss, [induct])) =
wenzelm@7798
    96
      thy
wenzelm@6439
    97
      |> Theory.add_path bname
nipkow@8625
    98
      |> PureThy.add_thmss named_simps
wenzelm@8430
    99
      |>>> PureThy.add_thms [(("induct", induct), [RuleCases.case_names case_numbers])];
nipkow@8625
   100
    val result = {simps = flat simpss, induct = induct, tcs = tcs};
wenzelm@7798
   101
    val thy =
wenzelm@7798
   102
      thy
wenzelm@6439
   103
      |> put_recdef name result
wenzelm@6429
   104
      |> Theory.parent_path;
wenzelm@7798
   105
  in (thy, result) end;
wenzelm@6429
   106
wenzelm@6576
   107
val add_recdef = gen_add_recdef Tfl.define I IsarThy.apply_theorems;
wenzelm@6576
   108
val add_recdef_x = gen_add_recdef Tfl.define (Simplifier.simpset_of o ThyInfo.get_theory)
wenzelm@6576
   109
  IsarThy.apply_theorems;
wenzelm@6576
   110
val add_recdef_i = gen_add_recdef Tfl.define_i I IsarThy.apply_theorems_i;
wenzelm@6429
   111
wenzelm@6429
   112
wenzelm@6557
   113
wenzelm@6557
   114
(** defer_recdef(_i) **)
wenzelm@6557
   115
wenzelm@6557
   116
fun gen_defer_recdef tfl_fn app_thms raw_name eqs raw_congs thy =
wenzelm@6557
   117
  let
wenzelm@6557
   118
    val name = Sign.intern_const (Theory.sign_of thy) raw_name;
wenzelm@6557
   119
    val bname = Sign.base_name name;
wenzelm@6557
   120
wenzelm@6557
   121
    val _ = requires_recdef thy;
wenzelm@6557
   122
    val _ = message ("Deferred recursive function " ^ quote name ^ " ...");
wenzelm@6557
   123
wenzelm@6557
   124
    val (thy1, congs) = thy |> app_thms raw_congs;
wenzelm@6557
   125
    val (thy2, induct_rules) = tfl_fn thy1 name congs eqs;
wenzelm@8430
   126
    val (thy3, [induct_rules']) =
wenzelm@6557
   127
      thy2
wenzelm@6557
   128
      |> Theory.add_path bname
wenzelm@6557
   129
      |> PureThy.add_thms [(("induct_rules", induct_rules), [])]
wenzelm@8430
   130
      |>> Theory.parent_path;
wenzelm@8430
   131
  in (thy3, {induct_rules = induct_rules'}) end;
wenzelm@6557
   132
wenzelm@6557
   133
val defer_recdef = gen_defer_recdef Tfl.defer IsarThy.apply_theorems;
wenzelm@6557
   134
val defer_recdef_i = gen_defer_recdef Tfl.defer_i IsarThy.apply_theorems_i;
wenzelm@6557
   135
wenzelm@6557
   136
wenzelm@6557
   137
wenzelm@6439
   138
(** package setup **)
wenzelm@6439
   139
wenzelm@6439
   140
(* setup theory *)
wenzelm@6439
   141
wenzelm@6439
   142
val setup = [RecdefData.init];
wenzelm@6439
   143
wenzelm@6439
   144
wenzelm@6429
   145
(* outer syntax *)
wenzelm@6429
   146
wenzelm@6723
   147
local structure P = OuterParse and K = OuterSyntax.Keyword in
wenzelm@6429
   148
wenzelm@6429
   149
val recdef_decl =
wenzelm@6729
   150
  P.name -- P.term -- Scan.repeat1 (P.term --| P.marg_comment) --
wenzelm@6723
   151
  Scan.optional (P.$$$ "(" |-- P.$$$ "congs" |-- P.!!! (P.xthms1 --| P.$$$ ")")) [] --
wenzelm@6723
   152
  Scan.option (P.$$$ "(" |-- P.$$$ "simpset" |-- P.!!! (P.name --| P.$$$ ")"))
wenzelm@6576
   153
  >> (fn ((((f, R), eqs), congs), ss) => #1 o add_recdef_x f R eqs ss congs);
wenzelm@6429
   154
wenzelm@6429
   155
val recdefP =
wenzelm@6723
   156
  OuterSyntax.command "recdef" "define general recursive functions (TFL)" K.thy_decl
wenzelm@6429
   157
    (recdef_decl >> Toplevel.theory);
wenzelm@6429
   158
wenzelm@6557
   159
wenzelm@6557
   160
val defer_recdef_decl =
wenzelm@6723
   161
  P.name -- Scan.repeat1 P.term --
wenzelm@6723
   162
  Scan.optional (P.$$$ "(" |-- P.$$$ "congs" |-- P.!!! (P.xthms1 --| P.$$$ ")")) []
wenzelm@6557
   163
  >> (fn ((f, eqs), congs) => #1 o defer_recdef f eqs congs);
wenzelm@6557
   164
wenzelm@6557
   165
val defer_recdefP =
wenzelm@6723
   166
  OuterSyntax.command "defer_recdef" "defer general recursive functions (TFL)" K.thy_decl
wenzelm@6557
   167
    (defer_recdef_decl >> Toplevel.theory);
wenzelm@6557
   168
wenzelm@6429
   169
val _ = OuterSyntax.add_keywords ["congs", "simpset"];
wenzelm@6557
   170
val _ = OuterSyntax.add_parsers [recdefP, defer_recdefP];
wenzelm@6429
   171
wenzelm@6429
   172
end;
wenzelm@6429
   173
wenzelm@6429
   174
wenzelm@6429
   175
end;