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 |