author | schirmer |
Fri, 17 Mar 2006 17:38:38 +0100 | |
changeset 19284 | 4c86109423d5 |
parent 19202 | 0b9eb4b0ad98 |
child 19347 | e2e709f3f955 |
permissions | -rw-r--r-- |
5181
4ba3787d9709
New theory Datatype. Needed as an ancestor when defining datatypes.
berghofe
parents:
diff
changeset
|
1 |
(* Title: HOL/Datatype.thy |
4ba3787d9709
New theory Datatype. Needed as an ancestor when defining datatypes.
berghofe
parents:
diff
changeset
|
2 |
ID: $Id$ |
11954 | 3 |
Author: Stefan Berghofer and Markus Wenzel, TU Muenchen |
5181
4ba3787d9709
New theory Datatype. Needed as an ancestor when defining datatypes.
berghofe
parents:
diff
changeset
|
4 |
*) |
4ba3787d9709
New theory Datatype. Needed as an ancestor when defining datatypes.
berghofe
parents:
diff
changeset
|
5 |
|
12918 | 6 |
header {* Datatypes *} |
11954 | 7 |
|
15131 | 8 |
theory Datatype |
15140 | 9 |
imports Datatype_Universe |
15131 | 10 |
begin |
11954 | 11 |
|
12 |
subsection {* Representing primitive types *} |
|
5181
4ba3787d9709
New theory Datatype. Needed as an ancestor when defining datatypes.
berghofe
parents:
diff
changeset
|
13 |
|
5759 | 14 |
rep_datatype bool |
11954 | 15 |
distinct True_not_False False_not_True |
16 |
induction bool_induct |
|
17 |
||
18 |
declare case_split [cases type: bool] |
|
19 |
-- "prefer plain propositional version" |
|
20 |
||
21 |
rep_datatype unit |
|
22 |
induction unit_induct |
|
5181
4ba3787d9709
New theory Datatype. Needed as an ancestor when defining datatypes.
berghofe
parents:
diff
changeset
|
23 |
|
4ba3787d9709
New theory Datatype. Needed as an ancestor when defining datatypes.
berghofe
parents:
diff
changeset
|
24 |
rep_datatype prod |
11954 | 25 |
inject Pair_eq |
26 |
induction prod_induct |
|
27 |
||
12918 | 28 |
rep_datatype sum |
29 |
distinct Inl_not_Inr Inr_not_Inl |
|
30 |
inject Inl_eq Inr_eq |
|
31 |
induction sum_induct |
|
32 |
||
33 |
ML {* |
|
34 |
val [sum_case_Inl, sum_case_Inr] = thms "sum.cases"; |
|
35 |
bind_thm ("sum_case_Inl", sum_case_Inl); |
|
36 |
bind_thm ("sum_case_Inr", sum_case_Inr); |
|
37 |
*} -- {* compatibility *} |
|
38 |
||
39 |
lemma surjective_sum: "sum_case (%x::'a. f (Inl x)) (%y::'b. f (Inr y)) s = f(s)" |
|
40 |
apply (rule_tac s = s in sumE) |
|
41 |
apply (erule ssubst) |
|
42 |
apply (rule sum_case_Inl) |
|
43 |
apply (erule ssubst) |
|
44 |
apply (rule sum_case_Inr) |
|
45 |
done |
|
46 |
||
47 |
lemma sum_case_weak_cong: "s = t ==> sum_case f g s = sum_case f g t" |
|
48 |
-- {* Prevents simplification of @{text f} and @{text g}: much faster. *} |
|
49 |
by (erule arg_cong) |
|
50 |
||
51 |
lemma sum_case_inject: |
|
52 |
"sum_case f1 f2 = sum_case g1 g2 ==> (f1 = g1 ==> f2 = g2 ==> P) ==> P" |
|
53 |
proof - |
|
54 |
assume a: "sum_case f1 f2 = sum_case g1 g2" |
|
55 |
assume r: "f1 = g1 ==> f2 = g2 ==> P" |
|
56 |
show P |
|
57 |
apply (rule r) |
|
58 |
apply (rule ext) |
|
14208 | 59 |
apply (cut_tac x = "Inl x" in a [THEN fun_cong], simp) |
12918 | 60 |
apply (rule ext) |
14208 | 61 |
apply (cut_tac x = "Inr x" in a [THEN fun_cong], simp) |
12918 | 62 |
done |
63 |
qed |
|
64 |
||
13635
c41e88151b54
Added functions Suml and Sumr which are useful for constructing
berghofe
parents:
12918
diff
changeset
|
65 |
constdefs |
c41e88151b54
Added functions Suml and Sumr which are useful for constructing
berghofe
parents:
12918
diff
changeset
|
66 |
Suml :: "('a => 'c) => 'a + 'b => 'c" |
c41e88151b54
Added functions Suml and Sumr which are useful for constructing
berghofe
parents:
12918
diff
changeset
|
67 |
"Suml == (%f. sum_case f arbitrary)" |
c41e88151b54
Added functions Suml and Sumr which are useful for constructing
berghofe
parents:
12918
diff
changeset
|
68 |
|
c41e88151b54
Added functions Suml and Sumr which are useful for constructing
berghofe
parents:
12918
diff
changeset
|
69 |
Sumr :: "('b => 'c) => 'a + 'b => 'c" |
c41e88151b54
Added functions Suml and Sumr which are useful for constructing
berghofe
parents:
12918
diff
changeset
|
70 |
"Sumr == sum_case arbitrary" |
c41e88151b54
Added functions Suml and Sumr which are useful for constructing
berghofe
parents:
12918
diff
changeset
|
71 |
|
c41e88151b54
Added functions Suml and Sumr which are useful for constructing
berghofe
parents:
12918
diff
changeset
|
72 |
lemma Suml_inject: "Suml f = Suml g ==> f = g" |
c41e88151b54
Added functions Suml and Sumr which are useful for constructing
berghofe
parents:
12918
diff
changeset
|
73 |
by (unfold Suml_def) (erule sum_case_inject) |
c41e88151b54
Added functions Suml and Sumr which are useful for constructing
berghofe
parents:
12918
diff
changeset
|
74 |
|
c41e88151b54
Added functions Suml and Sumr which are useful for constructing
berghofe
parents:
12918
diff
changeset
|
75 |
lemma Sumr_inject: "Sumr f = Sumr g ==> f = g" |
c41e88151b54
Added functions Suml and Sumr which are useful for constructing
berghofe
parents:
12918
diff
changeset
|
76 |
by (unfold Sumr_def) (erule sum_case_inject) |
c41e88151b54
Added functions Suml and Sumr which are useful for constructing
berghofe
parents:
12918
diff
changeset
|
77 |
|
c41e88151b54
Added functions Suml and Sumr which are useful for constructing
berghofe
parents:
12918
diff
changeset
|
78 |
|
c41e88151b54
Added functions Suml and Sumr which are useful for constructing
berghofe
parents:
12918
diff
changeset
|
79 |
subsection {* Finishing the datatype package setup *} |
c41e88151b54
Added functions Suml and Sumr which are useful for constructing
berghofe
parents:
12918
diff
changeset
|
80 |
|
c41e88151b54
Added functions Suml and Sumr which are useful for constructing
berghofe
parents:
12918
diff
changeset
|
81 |
text {* Belongs to theory @{text Datatype_Universe}; hides popular names. *} |
18230 | 82 |
hide (open) const Push Node Atom Leaf Numb Lim Split Case Suml Sumr |
83 |
hide (open) type node item |
|
13635
c41e88151b54
Added functions Suml and Sumr which are useful for constructing
berghofe
parents:
12918
diff
changeset
|
84 |
|
12918 | 85 |
|
86 |
subsection {* Further cases/induct rules for tuples *} |
|
11954 | 87 |
|
88 |
lemma prod_cases3 [case_names fields, cases type]: |
|
89 |
"(!!a b c. y = (a, b, c) ==> P) ==> P" |
|
90 |
apply (cases y) |
|
14208 | 91 |
apply (case_tac b, blast) |
11954 | 92 |
done |
93 |
||
94 |
lemma prod_induct3 [case_names fields, induct type]: |
|
95 |
"(!!a b c. P (a, b, c)) ==> P x" |
|
96 |
by (cases x) blast |
|
97 |
||
98 |
lemma prod_cases4 [case_names fields, cases type]: |
|
99 |
"(!!a b c d. y = (a, b, c, d) ==> P) ==> P" |
|
100 |
apply (cases y) |
|
14208 | 101 |
apply (case_tac c, blast) |
11954 | 102 |
done |
103 |
||
104 |
lemma prod_induct4 [case_names fields, induct type]: |
|
105 |
"(!!a b c d. P (a, b, c, d)) ==> P x" |
|
106 |
by (cases x) blast |
|
5181
4ba3787d9709
New theory Datatype. Needed as an ancestor when defining datatypes.
berghofe
parents:
diff
changeset
|
107 |
|
11954 | 108 |
lemma prod_cases5 [case_names fields, cases type]: |
109 |
"(!!a b c d e. y = (a, b, c, d, e) ==> P) ==> P" |
|
110 |
apply (cases y) |
|
14208 | 111 |
apply (case_tac d, blast) |
11954 | 112 |
done |
113 |
||
114 |
lemma prod_induct5 [case_names fields, induct type]: |
|
115 |
"(!!a b c d e. P (a, b, c, d, e)) ==> P x" |
|
116 |
by (cases x) blast |
|
117 |
||
118 |
lemma prod_cases6 [case_names fields, cases type]: |
|
119 |
"(!!a b c d e f. y = (a, b, c, d, e, f) ==> P) ==> P" |
|
120 |
apply (cases y) |
|
14208 | 121 |
apply (case_tac e, blast) |
11954 | 122 |
done |
123 |
||
124 |
lemma prod_induct6 [case_names fields, induct type]: |
|
125 |
"(!!a b c d e f. P (a, b, c, d, e, f)) ==> P x" |
|
126 |
by (cases x) blast |
|
127 |
||
128 |
lemma prod_cases7 [case_names fields, cases type]: |
|
129 |
"(!!a b c d e f g. y = (a, b, c, d, e, f, g) ==> P) ==> P" |
|
130 |
apply (cases y) |
|
14208 | 131 |
apply (case_tac f, blast) |
11954 | 132 |
done |
133 |
||
134 |
lemma prod_induct7 [case_names fields, induct type]: |
|
135 |
"(!!a b c d e f g. P (a, b, c, d, e, f, g)) ==> P x" |
|
136 |
by (cases x) blast |
|
5759 | 137 |
|
12918 | 138 |
|
139 |
subsection {* The option type *} |
|
140 |
||
141 |
datatype 'a option = None | Some 'a |
|
142 |
||
18576 | 143 |
lemma not_None_eq[iff]: "(x ~= None) = (EX y. x = Some y)" |
144 |
by (induct x) auto |
|
145 |
||
146 |
lemma not_Some_eq[iff]: "(ALL y. x ~= Some y) = (x = None)" |
|
18447 | 147 |
by (induct x) auto |
148 |
||
18576 | 149 |
text{*Although it may appear that both of these equalities are helpful |
150 |
only when applied to assumptions, in practice it seems better to give |
|
151 |
them the uniform iff attribute. *} |
|
12918 | 152 |
|
18576 | 153 |
(* |
18447 | 154 |
lemmas not_None_eq_D = not_None_eq [THEN iffD1] |
155 |
declare not_None_eq_D [dest!] |
|
156 |
||
157 |
lemmas not_Some_eq_D = not_Some_eq [THEN iffD1] |
|
158 |
declare not_Some_eq_D [dest!] |
|
18576 | 159 |
*) |
12918 | 160 |
|
161 |
lemma option_caseE: |
|
162 |
"(case x of None => P | Some y => Q y) ==> |
|
163 |
(x = None ==> P ==> R) ==> |
|
164 |
(!!y. x = Some y ==> Q y ==> R) ==> R" |
|
165 |
by (cases x) simp_all |
|
166 |
||
167 |
||
168 |
subsubsection {* Operations *} |
|
169 |
||
170 |
consts |
|
171 |
the :: "'a option => 'a" |
|
172 |
primrec |
|
173 |
"the (Some x) = x" |
|
174 |
||
175 |
consts |
|
176 |
o2s :: "'a option => 'a set" |
|
177 |
primrec |
|
178 |
"o2s None = {}" |
|
179 |
"o2s (Some x) = {x}" |
|
180 |
||
181 |
lemma ospec [dest]: "(ALL x:o2s A. P x) ==> A = Some x ==> P x" |
|
182 |
by simp |
|
183 |
||
17876 | 184 |
ML_setup {* change_claset (fn cs => cs addSD2 ("ospec", thm "ospec")) *} |
12918 | 185 |
|
186 |
lemma elem_o2s [iff]: "(x : o2s xo) = (xo = Some x)" |
|
187 |
by (cases xo) auto |
|
188 |
||
189 |
lemma o2s_empty_eq [simp]: "(o2s xo = {}) = (xo = None)" |
|
190 |
by (cases xo) auto |
|
191 |
||
192 |
||
193 |
constdefs |
|
194 |
option_map :: "('a => 'b) => ('a option => 'b option)" |
|
195 |
"option_map == %f y. case y of None => None | Some x => Some (f x)" |
|
196 |
||
197 |
lemma option_map_None [simp]: "option_map f None = None" |
|
198 |
by (simp add: option_map_def) |
|
199 |
||
200 |
lemma option_map_Some [simp]: "option_map f (Some x) = Some (f x)" |
|
201 |
by (simp add: option_map_def) |
|
202 |
||
14187 | 203 |
lemma option_map_is_None[iff]: |
204 |
"(option_map f opt = None) = (opt = None)" |
|
205 |
by (simp add: option_map_def split add: option.split) |
|
206 |
||
12918 | 207 |
lemma option_map_eq_Some [iff]: |
208 |
"(option_map f xo = Some y) = (EX z. xo = Some z & f z = y)" |
|
14187 | 209 |
by (simp add: option_map_def split add: option.split) |
210 |
||
211 |
lemma option_map_comp: |
|
212 |
"option_map f (option_map g opt) = option_map (f o g) opt" |
|
213 |
by (simp add: option_map_def split add: option.split) |
|
12918 | 214 |
|
215 |
lemma option_map_o_sum_case [simp]: |
|
216 |
"option_map f o sum_case g h = sum_case (option_map f o g) (option_map f o h)" |
|
217 |
apply (rule ext) |
|
218 |
apply (simp split add: sum.split) |
|
219 |
done |
|
220 |
||
17458
e42bfad176eb
lemmas [code] = imp_conv_disj (from Main.thy) -- Why does it need Datatype?
wenzelm
parents:
15140
diff
changeset
|
221 |
lemmas [code] = imp_conv_disj |
e42bfad176eb
lemmas [code] = imp_conv_disj (from Main.thy) -- Why does it need Datatype?
wenzelm
parents:
15140
diff
changeset
|
222 |
|
18702 | 223 |
subsubsection {* Codegenerator setup *} |
224 |
||
19138 | 225 |
lemma [code]: |
226 |
"(\<not> True) = False" by (rule HOL.simp_thms) |
|
227 |
||
228 |
lemma [code]: |
|
229 |
"(\<not> False) = True" by (rule HOL.simp_thms) |
|
230 |
||
231 |
lemma [code]: |
|
19179
61ef97e3f531
changed and retracted change of location of code lemmas.
nipkow
parents:
19150
diff
changeset
|
232 |
shows "(False \<and> x) = False" |
61ef97e3f531
changed and retracted change of location of code lemmas.
nipkow
parents:
19150
diff
changeset
|
233 |
and "(True \<and> x) = x" |
61ef97e3f531
changed and retracted change of location of code lemmas.
nipkow
parents:
19150
diff
changeset
|
234 |
and "(x \<and> False) = False" |
61ef97e3f531
changed and retracted change of location of code lemmas.
nipkow
parents:
19150
diff
changeset
|
235 |
and "(x \<and> True) = x" by simp_all |
19138 | 236 |
|
237 |
lemma [code]: |
|
19179
61ef97e3f531
changed and retracted change of location of code lemmas.
nipkow
parents:
19150
diff
changeset
|
238 |
shows "(False \<or> x) = x" |
61ef97e3f531
changed and retracted change of location of code lemmas.
nipkow
parents:
19150
diff
changeset
|
239 |
and "(True \<or> x) = True" |
61ef97e3f531
changed and retracted change of location of code lemmas.
nipkow
parents:
19150
diff
changeset
|
240 |
and "(x \<or> False) = x" |
61ef97e3f531
changed and retracted change of location of code lemmas.
nipkow
parents:
19150
diff
changeset
|
241 |
and "(x \<or> True) = True" by simp_all |
19138 | 242 |
|
243 |
declare |
|
244 |
if_True [code] |
|
245 |
if_False [code] |
|
19179
61ef97e3f531
changed and retracted change of location of code lemmas.
nipkow
parents:
19150
diff
changeset
|
246 |
fst_conv [code] |
61ef97e3f531
changed and retracted change of location of code lemmas.
nipkow
parents:
19150
diff
changeset
|
247 |
snd_conv [code] |
19138 | 248 |
|
19202 | 249 |
lemma [code unfold]: |
250 |
"1 == Suc 0" by simp |
|
251 |
||
19138 | 252 |
code_generate True False Not "op &" "op |" If |
253 |
||
254 |
code_syntax_tyco bool |
|
255 |
ml (target_atom "bool") |
|
256 |
haskell (target_atom "Bool") |
|
257 |
||
18702 | 258 |
code_syntax_const |
259 |
True |
|
18757 | 260 |
ml (target_atom "true") |
261 |
haskell (target_atom "True") |
|
18702 | 262 |
False |
18757 | 263 |
ml (target_atom "false") |
264 |
haskell (target_atom "False") |
|
19138 | 265 |
Not |
266 |
ml (target_atom "not") |
|
267 |
haskell (target_atom "not") |
|
268 |
"op &" |
|
269 |
ml (infixl 1 "andalso") |
|
270 |
haskell (infixl 3 "&&") |
|
271 |
"op |" |
|
272 |
ml (infixl 0 "orelse") |
|
273 |
haskell (infixl 2 "||") |
|
274 |
If |
|
275 |
ml (target_atom "(if __/ then __/ else __)") |
|
276 |
haskell (target_atom "(if __/ then __/ else __)") |
|
277 |
||
278 |
code_generate Pair |
|
279 |
||
280 |
code_syntax_tyco |
|
281 |
* |
|
282 |
ml (infix 2 "*") |
|
283 |
haskell (target_atom "(__,/ __)") |
|
18702 | 284 |
|
285 |
code_syntax_const |
|
286 |
Pair |
|
18757 | 287 |
ml (target_atom "(__,/ __)") |
288 |
haskell (target_atom "(__,/ __)") |
|
18702 | 289 |
|
19150 | 290 |
code_generate Unity |
291 |
||
292 |
code_syntax_tyco |
|
293 |
unit |
|
294 |
ml (target_atom "unit") |
|
295 |
haskell (target_atom "()") |
|
296 |
||
297 |
code_syntax_const |
|
298 |
Unity |
|
299 |
ml (target_atom "()") |
|
300 |
haskell (target_atom "()") |
|
301 |
||
302 |
code_generate None Some |
|
303 |
||
304 |
code_syntax_tyco |
|
305 |
option |
|
306 |
ml ("_ option") |
|
307 |
haskell ("Maybe _") |
|
308 |
||
309 |
code_syntax_const |
|
310 |
None |
|
311 |
ml (target_atom "NONE") |
|
312 |
haskell (target_atom "Nothing") |
|
313 |
Some |
|
314 |
ml (target_atom "SOME") |
|
315 |
haskell (target_atom "Just") |
|
316 |
||
5181
4ba3787d9709
New theory Datatype. Needed as an ancestor when defining datatypes.
berghofe
parents:
diff
changeset
|
317 |
end |