src/HOL/Tools/recdef_package.ML
author wenzelm
Thu Apr 22 12:50:39 1999 +0200 (1999-04-22)
changeset 6478 48f90bc10cf5
parent 6458 13c779aec65a
child 6520 08637598f7ec
permissions -rw-r--r--
add_recdef: actual simpset;
add_recdef_x: named simpset;
     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 setup: (theory -> theory) list
    20 end;
    21 
    22 structure RecdefPackage: RECDEF_PACKAGE =
    23 struct
    24 
    25 (* FIXME tmp
    26 val quiet_mode = Tfl.quiet_mode;
    27 val message = Tfl.message;
    28 *)
    29 val quiet_mode = ref false;
    30 val message = writeln;
    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 prep_ext = I;
    47   val merge: T * T -> T = Symtab.merge (K true);
    48 
    49   fun print sg tab =
    50     Pretty.writeln (Pretty.strs ("recdefs:" ::
    51       map (Sign.cond_extern sg Sign.constK o fst) (Symtab.dest tab)));
    52 end;
    53 
    54 structure RecdefData = TheoryDataFun(RecdefArgs);
    55 val print_recdefs = RecdefData.print;
    56 
    57 
    58 (* get and put data *)
    59 
    60 fun get_recdef thy name =
    61   (case Symtab.lookup (RecdefData.get thy, name) of
    62     Some info => info
    63   | None => error ("Unknown recursive function " ^ quote name));
    64 
    65 fun put_recdef name info thy =
    66   let
    67     val tab = Symtab.update_new ((name, info), RecdefData.get thy)
    68       handle Symtab.DUP _ => error ("Duplicate recursive function definition " ^ quote name);
    69   in RecdefData.put tab thy end;
    70 
    71 
    72 
    73 (** add_recdef(_i) **)
    74 
    75 fun gen_add_recdef tfl_def prep_att prep_ss app_thms raw_name R eq_srcs raw_ss raw_congs thy =
    76   let
    77     val name = Sign.intern_const (Theory.sign_of thy) raw_name;
    78     val bname = Sign.base_name name;
    79 
    80     val _ = Theory.requires thy "Recdef" "recursive function definitions";
    81     val _ = message ("Defining recursive function " ^ quote name ^ " ...");
    82 
    83     val ((eq_names, eqs), raw_eq_atts) = apfst split_list (split_list eq_srcs);
    84     val eq_atts = map (map (prep_att thy)) raw_eq_atts;
    85     val ss = (case raw_ss of None => Simplifier.simpset_of thy | Some x => prep_ss x);
    86     val (thy1, congs) = thy |> app_thms raw_congs;
    87     val (thy2, pats) = tfl_def thy1 name R eqs;
    88     val (result as {rules, induct, tcs}) = Tfl.simplify_defn (ss, congs) (thy2, (name, pats));
    89     val thy3 =
    90       thy2
    91       |> Theory.add_path bname
    92       |> PureThy.add_thmss [(("rules", rules), [])]
    93       |> PureThy.add_thms ((("induct", induct), []) :: ((eq_names ~~ rules) ~~ eq_atts))
    94       |> put_recdef name result
    95       |> Theory.parent_path;
    96   in (thy3, result) end;
    97 
    98 val add_recdef = gen_add_recdef Tfl.define Attrib.global_attribute I IsarThy.apply_theorems;
    99 val add_recdef_x = gen_add_recdef Tfl.define Attrib.global_attribute
   100   (Simplifier.simpset_of o ThyInfo.get_theory) IsarThy.apply_theorems;
   101 val add_recdef_i = gen_add_recdef Tfl.define_i (K I) I IsarThy.apply_theorems_i;
   102 
   103 
   104 (** package setup **)
   105 
   106 (* setup theory *)
   107 
   108 val setup = [RecdefData.init];
   109 
   110 
   111 (* outer syntax *)
   112 
   113 local open OuterParse in
   114 
   115 val recdef_decl =
   116   name -- term -- Scan.repeat1 (opt_thm_name ":" -- term) --
   117   Scan.optional ($$$ "(" |-- $$$ "congs" |-- !!! (xthms1 --| $$$ ")")) [] --
   118   Scan.option ($$$ "(" |-- $$$ "simpset" |-- !!! (name --| $$$ ")"))
   119   >> (fn ((((f, R), eqs), congs), ss) => #1 o add_recdef_x f R (map triple_swap eqs) ss congs);
   120 
   121 val recdefP =
   122   OuterSyntax.command "recdef" "define general recursive functions (TFL)"
   123     (recdef_decl >> Toplevel.theory);
   124 
   125 val _ = OuterSyntax.add_keywords ["congs", "simpset"];
   126 val _ = OuterSyntax.add_parsers [recdefP];
   127 
   128 end;
   129 
   130 
   131 end;