72029
|
1 |
(* Title: HOL/Examples/Records.thy
|
|
2 |
Author: Wolfgang Naraschewski, TU Muenchen
|
|
3 |
Author: Norbert Schirmer, TU Muenchen
|
|
4 |
Author: Markus Wenzel, TU Muenchen
|
|
5 |
*)
|
|
6 |
|
|
7 |
section \<open>Using extensible records in HOL -- points and coloured points\<close>
|
|
8 |
|
|
9 |
theory Records
|
|
10 |
imports Main
|
|
11 |
begin
|
|
12 |
|
|
13 |
subsection \<open>Points\<close>
|
|
14 |
|
|
15 |
record point =
|
|
16 |
xpos :: nat
|
|
17 |
ypos :: nat
|
|
18 |
|
|
19 |
text \<open>
|
|
20 |
Apart many other things, above record declaration produces the
|
|
21 |
following theorems:
|
|
22 |
\<close>
|
|
23 |
|
|
24 |
|
|
25 |
thm point.simps
|
|
26 |
thm point.iffs
|
|
27 |
thm point.defs
|
|
28 |
|
|
29 |
text \<open>
|
|
30 |
The set of theorems @{thm [source] point.simps} is added
|
|
31 |
automatically to the standard simpset, @{thm [source] point.iffs} is
|
|
32 |
added to the Classical Reasoner and Simplifier context.
|
|
33 |
|
|
34 |
\medskip Record declarations define new types and type abbreviations:
|
|
35 |
@{text [display]
|
|
36 |
\<open>point = \<lparr>xpos :: nat, ypos :: nat\<rparr> = () point_ext_type
|
|
37 |
'a point_scheme = \<lparr>xpos :: nat, ypos :: nat, ... :: 'a\<rparr> = 'a point_ext_type\<close>}
|
|
38 |
\<close>
|
|
39 |
|
|
40 |
consts foo2 :: "(| xpos :: nat, ypos :: nat |)"
|
|
41 |
consts foo4 :: "'a => (| xpos :: nat, ypos :: nat, ... :: 'a |)"
|
|
42 |
|
|
43 |
|
|
44 |
subsubsection \<open>Introducing concrete records and record schemes\<close>
|
|
45 |
|
|
46 |
definition foo1 :: point
|
|
47 |
where "foo1 = (| xpos = 1, ypos = 0 |)"
|
|
48 |
|
|
49 |
definition foo3 :: "'a => 'a point_scheme"
|
|
50 |
where "foo3 ext = (| xpos = 1, ypos = 0, ... = ext |)"
|
|
51 |
|
|
52 |
|
|
53 |
subsubsection \<open>Record selection and record update\<close>
|
|
54 |
|
|
55 |
definition getX :: "'a point_scheme => nat"
|
|
56 |
where "getX r = xpos r"
|
|
57 |
|
|
58 |
definition setX :: "'a point_scheme => nat => 'a point_scheme"
|
|
59 |
where "setX r n = r (| xpos := n |)"
|
|
60 |
|
|
61 |
|
|
62 |
subsubsection \<open>Some lemmas about records\<close>
|
|
63 |
|
|
64 |
text \<open>Basic simplifications.\<close>
|
|
65 |
|
|
66 |
lemma "point.make n p = (| xpos = n, ypos = p |)"
|
|
67 |
by (simp only: point.make_def)
|
|
68 |
|
|
69 |
lemma "xpos (| xpos = m, ypos = n, ... = p |) = m"
|
|
70 |
by simp
|
|
71 |
|
|
72 |
lemma "(| xpos = m, ypos = n, ... = p |) (| xpos:= 0 |) = (| xpos = 0, ypos = n, ... = p |)"
|
|
73 |
by simp
|
|
74 |
|
|
75 |
|
|
76 |
text \<open>\medskip Equality of records.\<close>
|
|
77 |
|
|
78 |
lemma "n = n' ==> p = p' ==> (| xpos = n, ypos = p |) = (| xpos = n', ypos = p' |)"
|
|
79 |
\<comment> \<open>introduction of concrete record equality\<close>
|
|
80 |
by simp
|
|
81 |
|
|
82 |
lemma "(| xpos = n, ypos = p |) = (| xpos = n', ypos = p' |) ==> n = n'"
|
|
83 |
\<comment> \<open>elimination of concrete record equality\<close>
|
|
84 |
by simp
|
|
85 |
|
|
86 |
lemma "r (| xpos := n |) (| ypos := m |) = r (| ypos := m |) (| xpos := n |)"
|
|
87 |
\<comment> \<open>introduction of abstract record equality\<close>
|
|
88 |
by simp
|
|
89 |
|
|
90 |
lemma "r (| xpos := n |) = r (| xpos := n' |) ==> n = n'"
|
|
91 |
\<comment> \<open>elimination of abstract record equality (manual proof)\<close>
|
|
92 |
proof -
|
|
93 |
assume "r (| xpos := n |) = r (| xpos := n' |)" (is "?lhs = ?rhs")
|
|
94 |
then have "xpos ?lhs = xpos ?rhs" by simp
|
|
95 |
then show ?thesis by simp
|
|
96 |
qed
|
|
97 |
|
|
98 |
|
|
99 |
text \<open>\medskip Surjective pairing\<close>
|
|
100 |
|
|
101 |
lemma "r = (| xpos = xpos r, ypos = ypos r |)"
|
|
102 |
by simp
|
|
103 |
|
|
104 |
lemma "r = (| xpos = xpos r, ypos = ypos r, ... = point.more r |)"
|
|
105 |
by simp
|
|
106 |
|
|
107 |
|
|
108 |
text \<open>
|
|
109 |
\medskip Representation of records by cases or (degenerate)
|
|
110 |
induction.
|
|
111 |
\<close>
|
|
112 |
|
|
113 |
lemma "r(| xpos := n |) (| ypos := m |) = r (| ypos := m |) (| xpos := n |)"
|
|
114 |
proof (cases r)
|
|
115 |
fix xpos ypos more
|
|
116 |
assume "r = (| xpos = xpos, ypos = ypos, ... = more |)"
|
|
117 |
then show ?thesis by simp
|
|
118 |
qed
|
|
119 |
|
|
120 |
lemma "r (| xpos := n |) (| ypos := m |) = r (| ypos := m |) (| xpos := n |)"
|
|
121 |
proof (induct r)
|
|
122 |
fix xpos ypos more
|
|
123 |
show "(| xpos = xpos, ypos = ypos, ... = more |) (| xpos := n, ypos := m |) =
|
|
124 |
(| xpos = xpos, ypos = ypos, ... = more |) (| ypos := m, xpos := n |)"
|
|
125 |
by simp
|
|
126 |
qed
|
|
127 |
|
|
128 |
lemma "r (| xpos := n |) (| xpos := m |) = r (| xpos := m |)"
|
|
129 |
proof (cases r)
|
|
130 |
fix xpos ypos more
|
|
131 |
assume "r = \<lparr>xpos = xpos, ypos = ypos, \<dots> = more\<rparr>"
|
|
132 |
then show ?thesis by simp
|
|
133 |
qed
|
|
134 |
|
|
135 |
lemma "r (| xpos := n |) (| xpos := m |) = r (| xpos := m |)"
|
|
136 |
proof (cases r)
|
|
137 |
case fields
|
|
138 |
then show ?thesis by simp
|
|
139 |
qed
|
|
140 |
|
|
141 |
lemma "r (| xpos := n |) (| xpos := m |) = r (| xpos := m |)"
|
|
142 |
by (cases r) simp
|
|
143 |
|
|
144 |
|
|
145 |
text \<open>
|
|
146 |
\medskip Concrete records are type instances of record schemes.
|
|
147 |
\<close>
|
|
148 |
|
|
149 |
definition foo5 :: nat
|
|
150 |
where "foo5 = getX (| xpos = 1, ypos = 0 |)"
|
|
151 |
|
|
152 |
|
|
153 |
text \<open>\medskip Manipulating the ``\<open>...\<close>'' (more) part.\<close>
|
|
154 |
|
|
155 |
definition incX :: "'a point_scheme => 'a point_scheme"
|
|
156 |
where "incX r = (| xpos = xpos r + 1, ypos = ypos r, ... = point.more r |)"
|
|
157 |
|
|
158 |
lemma "incX r = setX r (Suc (getX r))"
|
|
159 |
by (simp add: getX_def setX_def incX_def)
|
|
160 |
|
|
161 |
|
|
162 |
text \<open>An alternative definition.\<close>
|
|
163 |
|
|
164 |
definition incX' :: "'a point_scheme => 'a point_scheme"
|
|
165 |
where "incX' r = r (| xpos := xpos r + 1 |)"
|
|
166 |
|
|
167 |
|
|
168 |
subsection \<open>Coloured points: record extension\<close>
|
|
169 |
|
|
170 |
datatype colour = Red | Green | Blue
|
|
171 |
|
|
172 |
record cpoint = point +
|
|
173 |
colour :: colour
|
|
174 |
|
|
175 |
|
|
176 |
text \<open>
|
|
177 |
The record declaration defines a new type constructor and abbreviations:
|
|
178 |
@{text [display]
|
|
179 |
\<open>cpoint = (| xpos :: nat, ypos :: nat, colour :: colour |) =
|
|
180 |
() cpoint_ext_type point_ext_type
|
|
181 |
'a cpoint_scheme = (| xpos :: nat, ypos :: nat, colour :: colour, ... :: 'a |) =
|
|
182 |
'a cpoint_ext_type point_ext_type\<close>}
|
|
183 |
\<close>
|
|
184 |
|
|
185 |
consts foo6 :: cpoint
|
|
186 |
consts foo7 :: "(| xpos :: nat, ypos :: nat, colour :: colour |)"
|
|
187 |
consts foo8 :: "'a cpoint_scheme"
|
|
188 |
consts foo9 :: "(| xpos :: nat, ypos :: nat, colour :: colour, ... :: 'a |)"
|
|
189 |
|
|
190 |
|
|
191 |
text \<open>
|
|
192 |
Functions on \<open>point\<close> schemes work for \<open>cpoints\<close> as well.
|
|
193 |
\<close>
|
|
194 |
|
|
195 |
definition foo10 :: nat
|
|
196 |
where "foo10 = getX (| xpos = 2, ypos = 0, colour = Blue |)"
|
|
197 |
|
|
198 |
|
|
199 |
subsubsection \<open>Non-coercive structural subtyping\<close>
|
|
200 |
|
|
201 |
text \<open>
|
|
202 |
Term \<^term>\<open>foo11\<close> has type \<^typ>\<open>cpoint\<close>, not type \<^typ>\<open>point\<close> ---
|
|
203 |
Great!
|
|
204 |
\<close>
|
|
205 |
|
|
206 |
definition foo11 :: cpoint
|
|
207 |
where "foo11 = setX (| xpos = 2, ypos = 0, colour = Blue |) 0"
|
|
208 |
|
|
209 |
|
|
210 |
subsection \<open>Other features\<close>
|
|
211 |
|
|
212 |
text \<open>Field names contribute to record identity.\<close>
|
|
213 |
|
|
214 |
record point' =
|
|
215 |
xpos' :: nat
|
|
216 |
ypos' :: nat
|
|
217 |
|
|
218 |
text \<open>
|
|
219 |
\noindent May not apply \<^term>\<open>getX\<close> to @{term [source] "(| xpos' =
|
|
220 |
2, ypos' = 0 |)"} -- type error.
|
|
221 |
\<close>
|
|
222 |
|
|
223 |
text \<open>\medskip Polymorphic records.\<close>
|
|
224 |
|
|
225 |
record 'a point'' = point +
|
|
226 |
content :: 'a
|
|
227 |
|
|
228 |
type_synonym cpoint'' = "colour point''"
|
|
229 |
|
|
230 |
|
|
231 |
|
|
232 |
text \<open>Updating a record field with an identical value is simplified.\<close>
|
|
233 |
lemma "r (| xpos := xpos r |) = r"
|
|
234 |
by simp
|
|
235 |
|
|
236 |
text \<open>Only the most recent update to a component survives simplification.\<close>
|
|
237 |
lemma "r (| xpos := x, ypos := y, xpos := x' |) = r (| ypos := y, xpos := x' |)"
|
|
238 |
by simp
|
|
239 |
|
|
240 |
text \<open>In some cases its convenient to automatically split
|
|
241 |
(quantified) records. For this purpose there is the simproc @{ML [source]
|
|
242 |
"Record.split_simproc"} and the tactic @{ML [source]
|
|
243 |
"Record.split_simp_tac"}. The simplification procedure
|
|
244 |
only splits the records, whereas the tactic also simplifies the
|
|
245 |
resulting goal with the standard record simplification rules. A
|
|
246 |
(generalized) predicate on the record is passed as parameter that
|
|
247 |
decides whether or how `deep' to split the record. It can peek on the
|
|
248 |
subterm starting at the quantified occurrence of the record (including
|
|
249 |
the quantifier). The value \<^ML>\<open>0\<close> indicates no split, a value
|
|
250 |
greater \<^ML>\<open>0\<close> splits up to the given bound of record extension and
|
|
251 |
finally the value \<^ML>\<open>~1\<close> completely splits the record.
|
|
252 |
@{ML [source] "Record.split_simp_tac"} additionally takes a list of
|
|
253 |
equations for simplification and can also split fixed record variables.
|
|
254 |
|
|
255 |
\<close>
|
|
256 |
|
|
257 |
lemma "(\<forall>r. P (xpos r)) \<longrightarrow> (\<forall>x. P x)"
|
|
258 |
apply (tactic \<open>simp_tac (put_simpset HOL_basic_ss \<^context>
|
|
259 |
addsimprocs [Record.split_simproc (K ~1)]) 1\<close>)
|
|
260 |
apply simp
|
|
261 |
done
|
|
262 |
|
|
263 |
lemma "(\<forall>r. P (xpos r)) \<longrightarrow> (\<forall>x. P x)"
|
|
264 |
apply (tactic \<open>Record.split_simp_tac \<^context> [] (K ~1) 1\<close>)
|
|
265 |
apply simp
|
|
266 |
done
|
|
267 |
|
|
268 |
lemma "(\<exists>r. P (xpos r)) \<longrightarrow> (\<exists>x. P x)"
|
|
269 |
apply (tactic \<open>simp_tac (put_simpset HOL_basic_ss \<^context>
|
|
270 |
addsimprocs [Record.split_simproc (K ~1)]) 1\<close>)
|
|
271 |
apply simp
|
|
272 |
done
|
|
273 |
|
|
274 |
lemma "(\<exists>r. P (xpos r)) \<longrightarrow> (\<exists>x. P x)"
|
|
275 |
apply (tactic \<open>Record.split_simp_tac \<^context> [] (K ~1) 1\<close>)
|
|
276 |
apply simp
|
|
277 |
done
|
|
278 |
|
|
279 |
lemma "\<And>r. P (xpos r) \<Longrightarrow> (\<exists>x. P x)"
|
|
280 |
apply (tactic \<open>simp_tac (put_simpset HOL_basic_ss \<^context>
|
|
281 |
addsimprocs [Record.split_simproc (K ~1)]) 1\<close>)
|
|
282 |
apply auto
|
|
283 |
done
|
|
284 |
|
|
285 |
lemma "\<And>r. P (xpos r) \<Longrightarrow> (\<exists>x. P x)"
|
|
286 |
apply (tactic \<open>Record.split_simp_tac \<^context> [] (K ~1) 1\<close>)
|
|
287 |
apply auto
|
|
288 |
done
|
|
289 |
|
|
290 |
lemma "P (xpos r) \<Longrightarrow> (\<exists>x. P x)"
|
|
291 |
apply (tactic \<open>Record.split_simp_tac \<^context> [] (K ~1) 1\<close>)
|
|
292 |
apply auto
|
|
293 |
done
|
|
294 |
|
|
295 |
lemma True
|
|
296 |
proof -
|
|
297 |
{
|
|
298 |
fix P r
|
|
299 |
assume pre: "P (xpos r)"
|
|
300 |
then have "\<exists>x. P x"
|
|
301 |
apply -
|
|
302 |
apply (tactic \<open>Record.split_simp_tac \<^context> [] (K ~1) 1\<close>)
|
|
303 |
apply auto
|
|
304 |
done
|
|
305 |
}
|
|
306 |
show ?thesis ..
|
|
307 |
qed
|
|
308 |
|
|
309 |
text \<open>The effect of simproc @{ML [source] Record.ex_sel_eq_simproc} is
|
|
310 |
illustrated by the following lemma.\<close>
|
|
311 |
|
|
312 |
lemma "\<exists>r. xpos r = x"
|
|
313 |
apply (tactic \<open>simp_tac (put_simpset HOL_basic_ss \<^context>
|
|
314 |
addsimprocs [Record.ex_sel_eq_simproc]) 1\<close>)
|
|
315 |
done
|
|
316 |
|
|
317 |
|
|
318 |
subsection \<open>A more complex record expression\<close>
|
|
319 |
|
|
320 |
record ('a, 'b, 'c) bar = bar1 :: 'a
|
|
321 |
bar2 :: 'b
|
|
322 |
bar3 :: 'c
|
|
323 |
bar21 :: "'b \<times> 'a"
|
|
324 |
bar32 :: "'c \<times> 'b"
|
|
325 |
bar31 :: "'c \<times> 'a"
|
|
326 |
|
|
327 |
print_record "('a,'b,'c) bar"
|
|
328 |
|
|
329 |
subsection \<open>Some code generation\<close>
|
|
330 |
|
|
331 |
export_code foo1 foo3 foo5 foo10 checking SML
|
|
332 |
|
|
333 |
text \<open>
|
|
334 |
Code generation can also be switched off, for instance for very large records
|
|
335 |
\<close>
|
|
336 |
|
|
337 |
declare [[record_codegen = false]]
|
|
338 |
|
|
339 |
record not_so_large_record =
|
|
340 |
bar520 :: nat
|
|
341 |
bar521 :: "nat * nat"
|
|
342 |
|
|
343 |
declare [[record_codegen = true]]
|
|
344 |
|
|
345 |
end
|