author | haftmann |
Fri, 07 Dec 2007 15:07:54 +0100 | |
changeset 25569 | c597835d5de4 |
parent 25539 | 8b7ed8aef001 |
child 25679 | b77f797b528a |
permissions | -rw-r--r-- |
24710 | 1 |
(* Title: HOL/Tools/function_package/size.ML |
2 |
ID: $Id$ |
|
3 |
Author: Stefan Berghofer, Florian Haftmann, TU Muenchen |
|
4 |
||
5 |
Size functions for datatypes. |
|
6 |
*) |
|
7 |
||
8 |
signature SIZE = |
|
9 |
sig |
|
10 |
val size_thms: theory -> string -> thm list |
|
11 |
val setup: theory -> theory |
|
12 |
end; |
|
13 |
||
14 |
structure Size: SIZE = |
|
15 |
struct |
|
16 |
||
17 |
open DatatypeAux; |
|
18 |
||
24714
1618c2ac1b74
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24711
diff
changeset
|
19 |
structure SizeData = TheoryDataFun |
1618c2ac1b74
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24711
diff
changeset
|
20 |
( |
24710 | 21 |
type T = thm list Symtab.table; |
22 |
val empty = Symtab.empty; |
|
23 |
val copy = I |
|
24 |
val extend = I |
|
25 |
fun merge _ = Symtab.merge (K true); |
|
24714
1618c2ac1b74
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24711
diff
changeset
|
26 |
); |
24710 | 27 |
|
28 |
fun add_axioms label ts atts thy = |
|
29 |
thy |
|
30 |
|> PureThy.add_axiomss_i [((label, ts), atts)]; |
|
31 |
||
32 |
val Const (size_name, _) = HOLogic.size_const dummyT; |
|
33 |
val size_name_base = NameSpace.base size_name; |
|
34 |
val size_suffix = "_" ^ size_name_base; |
|
35 |
||
25569 | 36 |
fun instance_size_class tyco thy = |
37 |
thy |
|
38 |
|> TheoryTarget.instantiation |
|
39 |
([tyco], replicate (Sign.arity_number thy tyco) HOLogic.typeS, [HOLogic.class_size]) |
|
40 |
|> Class.prove_instantiation_instance (K (Class.intro_classes_tac [])) |
|
41 |
|> LocalTheory.exit |
|
42 |
|> ProofContext.theory_of; |
|
24710 | 43 |
|
44 |
fun plus (t1, t2) = Const ("HOL.plus_class.plus", |
|
45 |
HOLogic.natT --> HOLogic.natT --> HOLogic.natT) $ t1 $ t2; |
|
46 |
||
47 |
fun make_size head_len descr' sorts recTs thy = |
|
48 |
let |
|
49 |
val size_names = replicate head_len size_name @ |
|
25155 | 50 |
map (Sign.intern_const thy) (DatatypeProp.indexify_names |
24710 | 51 |
(map (fn T => name_of_typ T ^ size_suffix) (Library.drop (head_len, recTs)))); |
52 |
val size_consts = map2 (fn s => fn T => Const (s, T --> HOLogic.natT)) |
|
53 |
size_names recTs; |
|
54 |
||
55 |
fun make_tnames Ts = |
|
56 |
let |
|
57 |
fun type_name (TFree (name, _)) = implode (tl (explode name)) |
|
58 |
| type_name (Type (name, _)) = |
|
59 |
let val name' = Sign.base_name name |
|
60 |
in if Syntax.is_identifier name' then name' else "x" end; |
|
25155 | 61 |
in DatatypeProp.indexify_names (map type_name Ts) end; |
24710 | 62 |
|
63 |
fun make_size_eqn size_const T (cname, cargs) = |
|
64 |
let |
|
65 |
val recs = filter is_rec_type cargs; |
|
66 |
val Ts = map (typ_of_dtyp descr' sorts) cargs; |
|
67 |
val recTs = map (typ_of_dtyp descr' sorts) recs; |
|
68 |
val tnames = make_tnames Ts; |
|
69 |
val rec_tnames = map fst (filter (is_rec_type o snd) (tnames ~~ cargs)); |
|
70 |
val ts = map2 (fn (r, s) => fn T => nth size_consts (dest_DtRec r) $ |
|
71 |
Free (s, T)) (recs ~~ rec_tnames) recTs; |
|
72 |
val t = if null ts then HOLogic.zero else |
|
73 |
Library.foldl plus (hd ts, tl ts @ [HOLogic.Suc_zero]); |
|
74 |
in |
|
75 |
HOLogic.mk_Trueprop (HOLogic.mk_eq (size_const $ |
|
76 |
list_comb (Const (cname, Ts ---> T), map2 (curry Free) tnames Ts), t)) |
|
77 |
end |
|
78 |
||
79 |
in |
|
80 |
maps (fn (((_, (_, _, constrs)), size_const), T) => |
|
81 |
map (make_size_eqn size_const T) constrs) (descr' ~~ size_consts ~~ recTs) |
|
82 |
end; |
|
83 |
||
84 |
fun prove_size_thms (info : datatype_info) head_len thy = |
|
85 |
let |
|
86 |
val descr' = #descr info; |
|
87 |
val sorts = #sorts info; |
|
88 |
val reccomb_names = #rec_names info; |
|
89 |
val primrec_thms = #rec_rewrites info; |
|
90 |
val recTs = get_rec_types descr' sorts; |
|
91 |
||
92 |
val size_names = replicate head_len size_name @ |
|
93 |
map (Sign.full_name thy) (DatatypeProp.indexify_names |
|
94 |
(map (fn T => name_of_typ T ^ size_suffix) (Library.drop (head_len, recTs)))); |
|
95 |
val def_names = map (fn s => s ^ "_def") (DatatypeProp.indexify_names |
|
96 |
(map (fn T => name_of_typ T ^ size_suffix) recTs)); |
|
97 |
||
98 |
fun make_sizefun (_, cargs) = |
|
99 |
let |
|
100 |
val Ts = map (typ_of_dtyp descr' sorts) cargs; |
|
101 |
val k = length (filter is_rec_type cargs); |
|
102 |
val ts = map Bound (k - 1 downto 0); |
|
103 |
val t = if null ts then HOLogic.zero else |
|
104 |
Library.foldl plus (hd ts, tl ts @ [HOLogic.Suc_zero]); |
|
105 |
||
106 |
in |
|
107 |
foldr (fn (T, t') => Abs ("x", T, t')) t (Ts @ replicate k HOLogic.natT) |
|
108 |
end; |
|
109 |
||
110 |
val fs = maps (fn (_, (_, _, constrs)) => map make_sizefun constrs) descr'; |
|
111 |
val fTs = map fastype_of fs; |
|
112 |
||
113 |
val (size_def_thms, thy') = |
|
114 |
thy |
|
24714
1618c2ac1b74
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24711
diff
changeset
|
115 |
|> Sign.add_consts_i (map (fn (s, T) => |
24710 | 116 |
(Sign.base_name s, T --> HOLogic.natT, NoSyn)) |
117 |
(Library.drop (head_len, size_names ~~ recTs))) |
|
118 |
|> fold (fn (_, (name, _, _)) => instance_size_class name) descr' |
|
119 |
|> PureThy.add_defs_i true (map (Thm.no_attributes o (fn (((s, T), def_name), rec_name) => |
|
120 |
(def_name, Logic.mk_equals (Const (s, T --> HOLogic.natT), |
|
121 |
list_comb (Const (rec_name, fTs @ [T] ---> HOLogic.natT), fs))))) |
|
122 |
(size_names ~~ recTs ~~ def_names ~~ reccomb_names)); |
|
123 |
||
124 |
val rewrites = size_def_thms @ map mk_meta_eq primrec_thms; |
|
125 |
||
126 |
val size_thms = map (fn t => Goal.prove_global thy' [] [] t |
|
127 |
(fn _ => EVERY [rewrite_goals_tac rewrites, rtac refl 1])) |
|
128 |
(make_size head_len descr' sorts recTs thy') |
|
129 |
||
130 |
in |
|
131 |
thy' |
|
132 |
|> PureThy.add_thmss [((size_name_base, size_thms), [])] |
|
133 |
|>> flat |
|
134 |
end; |
|
135 |
||
136 |
fun axiomatize_size_thms (info : datatype_info) head_len thy = |
|
137 |
let |
|
138 |
val descr' = #descr info; |
|
139 |
val sorts = #sorts info; |
|
140 |
val recTs = get_rec_types descr' sorts; |
|
141 |
||
142 |
val used = map fst (fold Term.add_tfreesT recTs []); |
|
143 |
||
144 |
val size_names = DatatypeProp.indexify_names |
|
145 |
(map (fn T => name_of_typ T ^ size_suffix) (Library.drop (head_len, recTs))); |
|
146 |
||
24962 | 147 |
val thy' = thy |> fold (fn (s, T) => |
25017 | 148 |
snd o Theory.specify_const [] (Sign.base_name s, T --> HOLogic.natT, NoSyn) []) |
24962 | 149 |
(size_names ~~ Library.drop (head_len, recTs)) |
24710 | 150 |
val size_axs = make_size head_len descr' sorts recTs thy'; |
151 |
in |
|
152 |
thy' |
|
153 |
|> add_axioms size_name_base size_axs [] |
|
154 |
||> fold (fn (_, (name, _, _)) => instance_size_class name) descr' |
|
155 |
|>> flat |
|
156 |
end; |
|
157 |
||
158 |
fun add_size_thms (name :: _) thy = |
|
159 |
let |
|
160 |
val info = DatatypePackage.the_datatype thy name; |
|
161 |
val descr' = #descr info; |
|
162 |
val head_len = #head_len info; |
|
163 |
val typnames = map (#1 o snd) (curry Library.take head_len descr'); |
|
164 |
val prefix = space_implode "_" (map NameSpace.base typnames); |
|
165 |
val no_size = exists (fn (_, (_, _, constrs)) => exists (fn (_, cargs) => exists (fn dt => |
|
166 |
is_rec_type dt andalso not (null (fst (strip_dtyp dt)))) cargs) constrs) |
|
167 |
(#descr info) |
|
168 |
in if no_size then thy |
|
169 |
else |
|
170 |
thy |
|
24714
1618c2ac1b74
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24711
diff
changeset
|
171 |
|> Sign.add_path prefix |
24710 | 172 |
|> (if ! quick_and_dirty |
173 |
then axiomatize_size_thms info head_len |
|
174 |
else prove_size_thms info head_len) |
|
24714
1618c2ac1b74
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24711
diff
changeset
|
175 |
||> Sign.parent_path |
24710 | 176 |
|-> (fn thms => PureThy.add_thmss [(("", thms), |
177 |
[Simplifier.simp_add, Thm.declaration_attribute |
|
178 |
(fn thm => Context.mapping (Code.add_default_func thm) I)])]) |
|
179 |
|-> (fn thms => SizeData.map (fold (fn typname => Symtab.update_new |
|
180 |
(typname, flat thms)) typnames)) |
|
181 |
end; |
|
182 |
||
183 |
fun size_thms thy = the o Symtab.lookup (SizeData.get thy); |
|
184 |
||
24711 | 185 |
val setup = DatatypePackage.interpretation add_size_thms; |
24710 | 186 |
|
187 |
end; |