src/HOL/Tools/recdef_package.ML
author wenzelm
Fri Apr 30 18:10:35 1999 +0200 (1999-04-30)
changeset 6557 d7e7532c128a
parent 6520 08637598f7ec
child 6576 7e0b35bed503
permissions -rw-r--r--
peoper defer_recdef interface;
     1 (*  Title:      HOL/Tools/recdef_package.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4 
     5 Wrapper module for Konrad Slind's TFL package.
     6 *)
     7 
     8 signature RECDEF_PACKAGE =
     9 sig
    10   val quiet_mode: bool ref
    11   val print_recdefs: theory -> unit
    12   val get_recdef: theory -> string -> {rules: thm list, induct: thm, tcs: term list}
    13   val add_recdef: xstring -> string -> ((bstring * string) * Args.src list) list
    14     -> simpset option -> (xstring * Args.src list) list
    15     -> theory -> theory * {rules: thm list, induct: thm, tcs: term list}
    16   val add_recdef_i: xstring -> term -> ((bstring * term) * theory attribute list) list
    17     -> simpset option -> (thm * theory attribute list) list
    18     -> theory -> theory * {rules: thm list, induct: thm, tcs: term list}
    19   val defer_recdef: xstring -> string list -> (xstring * Args.src list) list
    20     -> theory -> theory * {induct_rules: thm}
    21   val defer_recdef_i: xstring -> term list -> (thm * theory attribute list) list
    22     -> theory -> theory * {induct_rules: thm}
    23   val setup: (theory -> theory) list
    24 end;
    25 
    26 structure RecdefPackage: RECDEF_PACKAGE =
    27 struct
    28 
    29 val quiet_mode = Tfl.quiet_mode;
    30 val message = Tfl.message;
    31 
    32 
    33 
    34 (** theory data **)
    35 
    36 (* data kind 'HOL/recdef' *)
    37 
    38 type recdef_info = {rules: thm list, induct: thm, tcs: term list};
    39 
    40 structure RecdefArgs =
    41 struct
    42   val name = "HOL/recdef";
    43   type T = recdef_info Symtab.table;
    44 
    45   val empty = Symtab.empty;
    46   val copy = I;
    47   val prep_ext = I;
    48   val merge: T * T -> T = Symtab.merge (K true);
    49 
    50   fun print sg tab =
    51     Pretty.writeln (Pretty.strs ("recdefs:" ::
    52       map (Sign.cond_extern sg Sign.constK o fst) (Symtab.dest tab)));
    53 end;
    54 
    55 structure RecdefData = TheoryDataFun(RecdefArgs);
    56 val print_recdefs = RecdefData.print;
    57 
    58 
    59 (* get and put data *)
    60 
    61 fun get_recdef thy name =
    62   (case Symtab.lookup (RecdefData.get thy, name) of
    63     Some info => info
    64   | None => error ("Unknown recursive function " ^ quote name));
    65 
    66 fun put_recdef name info thy =
    67   let
    68     val tab = Symtab.update_new ((name, info), RecdefData.get thy)
    69       handle Symtab.DUP _ => error ("Duplicate recursive function definition " ^ quote name);
    70   in RecdefData.put tab thy end;
    71 
    72 
    73 
    74 (** add_recdef(_i) **)
    75 
    76 fun requires_recdef thy = Theory.requires thy "Recdef" "recursive functions";
    77 
    78 fun gen_add_recdef tfl_fn prep_att prep_ss app_thms raw_name R eq_srcs raw_ss raw_congs thy =
    79   let
    80     val name = Sign.intern_const (Theory.sign_of thy) raw_name;
    81     val bname = Sign.base_name name;
    82 
    83     val _ = requires_recdef thy;
    84     val _ = message ("Defining recursive function " ^ quote name ^ " ...");
    85 
    86     val ((eq_names, eqs), raw_eq_atts) = apfst split_list (split_list eq_srcs);
    87     val eq_atts = map (map (prep_att thy)) raw_eq_atts;
    88     val ss = (case raw_ss of None => Simplifier.simpset_of thy | Some x => prep_ss x);
    89     val (thy1, congs) = thy |> app_thms raw_congs;
    90     val (thy2, pats) = tfl_fn thy1 name R eqs;
    91     val (result as {rules, induct, tcs}) = Tfl.simplify_defn (ss, congs) (thy2, (name, pats));
    92     val thy3 =
    93       thy2
    94       |> Theory.add_path bname
    95       |> PureThy.add_thmss [(("rules", rules), [])]
    96       |> PureThy.add_thms ((("induct", induct), []) :: ((eq_names ~~ rules) ~~ eq_atts))
    97       |> put_recdef name result
    98       |> Theory.parent_path;
    99   in (thy3, result) end;
   100 
   101 val add_recdef = gen_add_recdef Tfl.define Attrib.global_attribute I IsarThy.apply_theorems;
   102 val add_recdef_x = gen_add_recdef Tfl.define Attrib.global_attribute
   103   (Simplifier.simpset_of o ThyInfo.get_theory) IsarThy.apply_theorems;
   104 val add_recdef_i = gen_add_recdef Tfl.define_i (K I) I IsarThy.apply_theorems_i;
   105 
   106 
   107 
   108 (** defer_recdef(_i) **)
   109 
   110 fun gen_defer_recdef tfl_fn app_thms raw_name eqs raw_congs thy =
   111   let
   112     val name = Sign.intern_const (Theory.sign_of thy) raw_name;
   113     val bname = Sign.base_name name;
   114 
   115     val _ = requires_recdef thy;
   116     val _ = message ("Deferred recursive function " ^ quote name ^ " ...");
   117 
   118     val (thy1, congs) = thy |> app_thms raw_congs;
   119     val (thy2, induct_rules) = tfl_fn thy1 name congs eqs;
   120     val thy3 =
   121       thy2
   122       |> Theory.add_path bname
   123       |> PureThy.add_thms [(("induct_rules", induct_rules), [])]
   124       |> Theory.parent_path;
   125   in (thy3, {induct_rules = induct_rules}) end;
   126 
   127 val defer_recdef = gen_defer_recdef Tfl.defer IsarThy.apply_theorems;
   128 val defer_recdef_i = gen_defer_recdef Tfl.defer_i IsarThy.apply_theorems_i;
   129 
   130 
   131 
   132 (** package setup **)
   133 
   134 (* setup theory *)
   135 
   136 val setup = [RecdefData.init];
   137 
   138 
   139 (* outer syntax *)
   140 
   141 local open OuterParse in
   142 
   143 val recdef_decl =
   144   name -- term -- Scan.repeat1 (opt_thm_name ":" -- term) --
   145   Scan.optional ($$$ "(" |-- $$$ "congs" |-- !!! (xthms1 --| $$$ ")")) [] --
   146   Scan.option ($$$ "(" |-- $$$ "simpset" |-- !!! (name --| $$$ ")"))
   147   >> (fn ((((f, R), eqs), congs), ss) => #1 o add_recdef_x f R (map triple_swap eqs) ss congs);
   148 
   149 val recdefP =
   150   OuterSyntax.command "recdef" "define general recursive functions (TFL)"
   151     (recdef_decl >> Toplevel.theory);
   152 
   153 
   154 val defer_recdef_decl =
   155   name -- Scan.repeat1 term --
   156   Scan.optional ($$$ "(" |-- $$$ "congs" |-- !!! (xthms1 --| $$$ ")")) []
   157   >> (fn ((f, eqs), congs) => #1 o defer_recdef f eqs congs);
   158 
   159 val defer_recdefP =
   160   OuterSyntax.command "defer_recdef" "defer general recursive functions (TFL)"
   161     (defer_recdef_decl >> Toplevel.theory);
   162 
   163 val _ = OuterSyntax.add_keywords ["congs", "simpset"];
   164 val _ = OuterSyntax.add_parsers [recdefP, defer_recdefP];
   165 
   166 end;
   167 
   168 
   169 end;