src/HOL/Tools/primrec_package.ML
changeset 12448 473cb9f9e237
parent 12311 ce5f9e61c037
child 12474 0404454d97df
equal deleted inserted replaced
12447:e752c9aecdec 12448:473cb9f9e237
     7 *)
     7 *)
     8 
     8 
     9 signature PRIMREC_PACKAGE =
     9 signature PRIMREC_PACKAGE =
    10 sig
    10 sig
    11   val quiet_mode: bool ref
    11   val quiet_mode: bool ref
    12   val print_primrecs: theory -> unit
       
    13   val get_primrec: theory -> string -> (string * thm list) list option
       
    14   val add_primrec: string -> ((bstring * string) * Args.src list) list
    12   val add_primrec: string -> ((bstring * string) * Args.src list) list
    15     -> theory -> theory * thm list
    13     -> theory -> theory * thm list
    16   val add_primrec_i: string -> ((bstring * term) * theory attribute list) list
    14   val add_primrec_i: string -> ((bstring * term) * theory attribute list) list
    17     -> theory -> theory * thm list
    15     -> theory -> theory * thm list
    18   val setup: (theory -> theory) list
       
    19 end;
    16 end;
    20 
    17 
    21 structure PrimrecPackage : PRIMREC_PACKAGE =
    18 structure PrimrecPackage : PRIMREC_PACKAGE =
    22 struct
    19 struct
    23 
    20 
    32 
    29 
    33 (* messages *)
    30 (* messages *)
    34 
    31 
    35 val quiet_mode = ref false;
    32 val quiet_mode = ref false;
    36 fun message s = if ! quiet_mode then () else writeln s;
    33 fun message s = if ! quiet_mode then () else writeln s;
    37 
       
    38 
       
    39 (** theory data **)
       
    40 
       
    41 (* data kind 'HOL/primrec' *)
       
    42 
       
    43 structure PrimrecArgs =
       
    44 struct
       
    45   val name = "HOL/primrec";
       
    46   type T = (string * thm list) list Symtab.table;
       
    47 
       
    48   val empty = Symtab.empty;
       
    49   val copy = I;
       
    50   val prep_ext = I;
       
    51   val merge: T * T -> T = Symtab.merge (K true);
       
    52 
       
    53   fun print sg tab =
       
    54     Pretty.writeln (Pretty.strs ("primrecs:" ::
       
    55       map #1 (Sign.cond_extern_table sg Sign.constK tab)));
       
    56 end;
       
    57 
       
    58 structure PrimrecData = TheoryDataFun(PrimrecArgs);
       
    59 val print_primrecs = PrimrecData.print;
       
    60 
       
    61 
       
    62 (* get and put data *)
       
    63 
       
    64 fun get_primrec thy name = Symtab.lookup (PrimrecData.get thy, name);
       
    65 
       
    66 fun put_primrec name info thy =
       
    67   let val tab = PrimrecData.get thy
       
    68   in 
       
    69     PrimrecData.put (case Symtab.lookup (tab, name) of
       
    70        None => Symtab.update_new ((name, [info]), tab)
       
    71      | Some infos => Symtab.update ((name, info::infos), tab)) thy end;
       
    72 
    34 
    73 
    35 
    74 (* preprocessing of equations *)
    36 (* preprocessing of equations *)
    75 
    37 
    76 fun process_eqn sign (eq, rec_fns) = 
    38 fun process_eqn sign (eq, rec_fns) = 
   303     val _ = message ("Proving equations for primrec function(s) " ^ commas_quote names1 ^ " ...");
   265     val _ = message ("Proving equations for primrec function(s) " ^ commas_quote names1 ^ " ...");
   304     val simps = map (fn (_, t) => prove_goalw_cterm rewrites (cterm_of (Theory.sign_of thy') t)
   266     val simps = map (fn (_, t) => prove_goalw_cterm rewrites (cterm_of (Theory.sign_of thy') t)
   305         (fn _ => [rtac refl 1])) eqns;
   267         (fn _ => [rtac refl 1])) eqns;
   306     val (thy'', simps') = PureThy.add_thms ((map fst eqns ~~ simps) ~~ atts) thy';
   268     val (thy'', simps') = PureThy.add_thms ((map fst eqns ~~ simps) ~~ atts) thy';
   307     val thy''' = thy''
   269     val thy''' = thy''
   308       |> (#1 o PureThy.add_thmss [(("simps", simps'), [Simplifier.simp_add_global])])
   270       |> (#1 o PureThy.add_thmss [(("simps", simps'), [Simplifier.simp_add_global, RecfunCodegen.add])])
   309       |> (#1 o PureThy.add_thms [(("induct", prepare_induct (#2 (hd dts)) rec_eqns), [])])
   271       |> (#1 o PureThy.add_thms [(("induct", prepare_induct (#2 (hd dts)) rec_eqns), [])])
   310       |> Theory.parent_path
   272       |> Theory.parent_path
   311   in
   273   in
   312     (foldl (fn (thy, (fname, _, _, tname)) =>
   274     (thy''', simps')
   313        put_primrec fname (tname, simps) thy) (thy''', defs), simps')
       
   314   end;
   275   end;
   315 
   276 
   316 
   277 
   317 fun add_primrec alt_name eqns thy =
   278 fun add_primrec alt_name eqns thy =
   318   let
   279   let
   326   in
   287   in
   327     add_primrec_i alt_name (names ~~ eqn_ts' ~~ atts) thy
   288     add_primrec_i alt_name (names ~~ eqn_ts' ~~ atts) thy
   328   end;
   289   end;
   329 
   290 
   330 
   291 
   331 (** package setup **)
       
   332 
       
   333 val setup = [PrimrecData.init];
       
   334 
       
   335 (* outer syntax *)
   292 (* outer syntax *)
   336 
   293 
   337 local structure P = OuterParse and K = OuterSyntax.Keyword in
   294 local structure P = OuterParse and K = OuterSyntax.Keyword in
   338 
   295 
   339 val primrec_decl =
   296 val primrec_decl =