author | paulson <lp15@cam.ac.uk> |
Mon, 03 Jul 2023 11:45:59 +0100 | |
changeset 78248 | 740b23f1138a |
parent 76042 | e076b1b42c44 |
child 78624 | 8d7394e533f8 |
permissions | -rw-r--r-- |
72029 | 1 |
(* Title: HOL/Examples/Records.thy |
2 |
Author: Wolfgang Naraschewski, TU Muenchen |
|
3 |
Author: Norbert Schirmer, TU Muenchen |
|
76039
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
4 |
Author: Norbert Schirmer, Apple, 2022 |
72029 | 5 |
Author: Markus Wenzel, TU Muenchen |
6 |
*) |
|
7 |
||
8 |
section \<open>Using extensible records in HOL -- points and coloured points\<close> |
|
9 |
||
10 |
theory Records |
|
72030 | 11 |
imports Main |
72029 | 12 |
begin |
13 |
||
14 |
subsection \<open>Points\<close> |
|
15 |
||
16 |
record point = |
|
17 |
xpos :: nat |
|
18 |
ypos :: nat |
|
19 |
||
20 |
text \<open> |
|
21 |
Apart many other things, above record declaration produces the |
|
22 |
following theorems: |
|
23 |
\<close> |
|
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 |
||
72030 | 34 |
\<^medskip> Record declarations define new types and type abbreviations: |
72029 | 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 |
||
72030 | 40 |
consts foo2 :: "\<lparr>xpos :: nat, ypos :: nat\<rparr>" |
41 |
consts foo4 :: "'a \<Rightarrow> \<lparr>xpos :: nat, ypos :: nat, \<dots> :: 'a\<rparr>" |
|
72029 | 42 |
|
43 |
||
44 |
subsubsection \<open>Introducing concrete records and record schemes\<close> |
|
45 |
||
46 |
definition foo1 :: point |
|
72030 | 47 |
where "foo1 = \<lparr>xpos = 1, ypos = 0\<rparr>" |
72029 | 48 |
|
72030 | 49 |
definition foo3 :: "'a \<Rightarrow> 'a point_scheme" |
50 |
where "foo3 ext = \<lparr>xpos = 1, ypos = 0, \<dots> = ext\<rparr>" |
|
72029 | 51 |
|
52 |
||
53 |
subsubsection \<open>Record selection and record update\<close> |
|
54 |
||
72030 | 55 |
definition getX :: "'a point_scheme \<Rightarrow> nat" |
72029 | 56 |
where "getX r = xpos r" |
57 |
||
72030 | 58 |
definition setX :: "'a point_scheme \<Rightarrow> nat \<Rightarrow> 'a point_scheme" |
59 |
where "setX r n = r \<lparr>xpos := n\<rparr>" |
|
72029 | 60 |
|
61 |
||
62 |
subsubsection \<open>Some lemmas about records\<close> |
|
63 |
||
64 |
text \<open>Basic simplifications.\<close> |
|
65 |
||
72030 | 66 |
lemma "point.make n p = \<lparr>xpos = n, ypos = p\<rparr>" |
72029 | 67 |
by (simp only: point.make_def) |
68 |
||
72030 | 69 |
lemma "xpos \<lparr>xpos = m, ypos = n, \<dots> = p\<rparr> = m" |
72029 | 70 |
by simp |
71 |
||
72030 | 72 |
lemma "\<lparr>xpos = m, ypos = n, \<dots> = p\<rparr>\<lparr>xpos:= 0\<rparr> = \<lparr>xpos = 0, ypos = n, \<dots> = p\<rparr>" |
72029 | 73 |
by simp |
74 |
||
75 |
||
72030 | 76 |
text \<open>\<^medskip> Equality of records.\<close> |
72029 | 77 |
|
72030 | 78 |
lemma "n = n' \<Longrightarrow> p = p' \<Longrightarrow> \<lparr>xpos = n, ypos = p\<rparr> = \<lparr>xpos = n', ypos = p'\<rparr>" |
72029 | 79 |
\<comment> \<open>introduction of concrete record equality\<close> |
80 |
by simp |
|
81 |
||
72030 | 82 |
lemma "\<lparr>xpos = n, ypos = p\<rparr> = \<lparr>xpos = n', ypos = p'\<rparr> \<Longrightarrow> n = n'" |
72029 | 83 |
\<comment> \<open>elimination of concrete record equality\<close> |
84 |
by simp |
|
85 |
||
72030 | 86 |
lemma "r\<lparr>xpos := n\<rparr>\<lparr>ypos := m\<rparr> = r\<lparr>ypos := m\<rparr>\<lparr>xpos := n\<rparr>" |
72029 | 87 |
\<comment> \<open>introduction of abstract record equality\<close> |
88 |
by simp |
|
89 |
||
72030 | 90 |
lemma "r\<lparr>xpos := n\<rparr> = r\<lparr>xpos := n'\<rparr>" if "n = n'" |
72029 | 91 |
\<comment> \<open>elimination of abstract record equality (manual proof)\<close> |
92 |
proof - |
|
72030 | 93 |
let "?lhs = ?rhs" = ?thesis |
94 |
from that have "xpos ?lhs = xpos ?rhs" by simp |
|
72029 | 95 |
then show ?thesis by simp |
96 |
qed |
|
97 |
||
98 |
||
72030 | 99 |
text \<open>\<^medskip> Surjective pairing\<close> |
72029 | 100 |
|
72030 | 101 |
lemma "r = \<lparr>xpos = xpos r, ypos = ypos r\<rparr>" |
72029 | 102 |
by simp |
103 |
||
72030 | 104 |
lemma "r = \<lparr>xpos = xpos r, ypos = ypos r, \<dots> = point.more r\<rparr>" |
72029 | 105 |
by simp |
106 |
||
107 |
||
72030 | 108 |
text \<open>\<^medskip> Representation of records by cases or (degenerate) induction.\<close> |
72029 | 109 |
|
72030 | 110 |
lemma "r\<lparr>xpos := n\<rparr>\<lparr>ypos := m\<rparr> = r\<lparr>ypos := m\<rparr>\<lparr>xpos := n\<rparr>" |
72029 | 111 |
proof (cases r) |
112 |
fix xpos ypos more |
|
113 |
assume "r = \<lparr>xpos = xpos, ypos = ypos, \<dots> = more\<rparr>" |
|
114 |
then show ?thesis by simp |
|
115 |
qed |
|
116 |
||
72030 | 117 |
lemma "r\<lparr>xpos := n\<rparr>\<lparr>ypos := m\<rparr> = r\<lparr>ypos := m\<rparr>\<lparr>xpos := n\<rparr>" |
118 |
proof (induct r) |
|
119 |
fix xpos ypos more |
|
120 |
show "\<lparr>xpos = xpos, ypos = ypos, \<dots> = more\<rparr>\<lparr>xpos := n, ypos := m\<rparr> = |
|
121 |
\<lparr>xpos = xpos, ypos = ypos, \<dots> = more\<rparr>\<lparr>ypos := m, xpos := n\<rparr>" |
|
122 |
by simp |
|
123 |
qed |
|
124 |
||
125 |
lemma "r\<lparr>xpos := n\<rparr>\<lparr>xpos := m\<rparr> = r\<lparr>xpos := m\<rparr>" |
|
126 |
proof (cases r) |
|
127 |
fix xpos ypos more |
|
128 |
assume "r = \<lparr>xpos = xpos, ypos = ypos, \<dots> = more\<rparr>" |
|
129 |
then show ?thesis by simp |
|
130 |
qed |
|
131 |
||
132 |
lemma "r\<lparr>xpos := n\<rparr>\<lparr>xpos := m\<rparr> = r\<lparr>xpos := m\<rparr>" |
|
72029 | 133 |
proof (cases r) |
134 |
case fields |
|
135 |
then show ?thesis by simp |
|
136 |
qed |
|
137 |
||
72030 | 138 |
lemma "r\<lparr>xpos := n\<rparr>\<lparr>xpos := m\<rparr> = r\<lparr>xpos := m\<rparr>" |
72029 | 139 |
by (cases r) simp |
140 |
||
141 |
||
72030 | 142 |
text \<open>\<^medskip> Concrete records are type instances of record schemes.\<close> |
72029 | 143 |
|
144 |
definition foo5 :: nat |
|
72030 | 145 |
where "foo5 = getX \<lparr>xpos = 1, ypos = 0\<rparr>" |
72029 | 146 |
|
147 |
||
72030 | 148 |
text \<open>\<^medskip> Manipulating the ``\<open>...\<close>'' (more) part.\<close> |
72029 | 149 |
|
72030 | 150 |
definition incX :: "'a point_scheme \<Rightarrow> 'a point_scheme" |
151 |
where "incX r = \<lparr>xpos = xpos r + 1, ypos = ypos r, \<dots> = point.more r\<rparr>" |
|
72029 | 152 |
|
153 |
lemma "incX r = setX r (Suc (getX r))" |
|
154 |
by (simp add: getX_def setX_def incX_def) |
|
155 |
||
156 |
||
72030 | 157 |
text \<open>\<^medskip> An alternative definition.\<close> |
72029 | 158 |
|
72030 | 159 |
definition incX' :: "'a point_scheme \<Rightarrow> 'a point_scheme" |
160 |
where "incX' r = r\<lparr>xpos := xpos r + 1\<rparr>" |
|
72029 | 161 |
|
162 |
||
163 |
subsection \<open>Coloured points: record extension\<close> |
|
164 |
||
165 |
datatype colour = Red | Green | Blue |
|
166 |
||
167 |
record cpoint = point + |
|
168 |
colour :: colour |
|
169 |
||
170 |
||
171 |
text \<open> |
|
172 |
The record declaration defines a new type constructor and abbreviations: |
|
173 |
@{text [display] |
|
72030 | 174 |
\<open>cpoint = \<lparr>xpos :: nat, ypos :: nat, colour :: colour\<rparr> = |
72029 | 175 |
() cpoint_ext_type point_ext_type |
72030 | 176 |
'a cpoint_scheme = \<lparr>xpos :: nat, ypos :: nat, colour :: colour, \<dots> :: 'a\<rparr> = |
72029 | 177 |
'a cpoint_ext_type point_ext_type\<close>} |
178 |
\<close> |
|
179 |
||
180 |
consts foo6 :: cpoint |
|
72030 | 181 |
consts foo7 :: "\<lparr>xpos :: nat, ypos :: nat, colour :: colour\<rparr>" |
72029 | 182 |
consts foo8 :: "'a cpoint_scheme" |
72030 | 183 |
consts foo9 :: "\<lparr>xpos :: nat, ypos :: nat, colour :: colour, \<dots> :: 'a\<rparr>" |
72029 | 184 |
|
185 |
||
72030 | 186 |
text \<open>Functions on \<open>point\<close> schemes work for \<open>cpoints\<close> as well.\<close> |
72029 | 187 |
|
188 |
definition foo10 :: nat |
|
72030 | 189 |
where "foo10 = getX \<lparr>xpos = 2, ypos = 0, colour = Blue\<rparr>" |
72029 | 190 |
|
191 |
||
192 |
subsubsection \<open>Non-coercive structural subtyping\<close> |
|
193 |
||
72030 | 194 |
text \<open>Term \<^term>\<open>foo11\<close> has type \<^typ>\<open>cpoint\<close>, not type \<^typ>\<open>point\<close> --- Great!\<close> |
72029 | 195 |
|
196 |
definition foo11 :: cpoint |
|
72030 | 197 |
where "foo11 = setX \<lparr>xpos = 2, ypos = 0, colour = Blue\<rparr> 0" |
72029 | 198 |
|
199 |
||
200 |
subsection \<open>Other features\<close> |
|
201 |
||
202 |
text \<open>Field names contribute to record identity.\<close> |
|
203 |
||
204 |
record point' = |
|
205 |
xpos' :: nat |
|
206 |
ypos' :: nat |
|
207 |
||
208 |
text \<open> |
|
72030 | 209 |
\<^noindent> May not apply \<^term>\<open>getX\<close> to @{term [source] "\<lparr>xpos' = 2, ypos' = 0\<rparr>"} |
210 |
--- type error. |
|
72029 | 211 |
\<close> |
212 |
||
72030 | 213 |
text \<open>\<^medskip> Polymorphic records.\<close> |
72029 | 214 |
|
215 |
record 'a point'' = point + |
|
216 |
content :: 'a |
|
217 |
||
218 |
type_synonym cpoint'' = "colour point''" |
|
219 |
||
220 |
||
221 |
text \<open>Updating a record field with an identical value is simplified.\<close> |
|
72030 | 222 |
lemma "r\<lparr>xpos := xpos r\<rparr> = r" |
72029 | 223 |
by simp |
224 |
||
225 |
text \<open>Only the most recent update to a component survives simplification.\<close> |
|
72030 | 226 |
lemma "r\<lparr>xpos := x, ypos := y, xpos := x'\<rparr> = r\<lparr>ypos := y, xpos := x'\<rparr>" |
72029 | 227 |
by simp |
228 |
||
72030 | 229 |
text \<open> |
230 |
In some cases its convenient to automatically split (quantified) records. |
|
231 |
For this purpose there is the simproc @{ML [source] "Record.split_simproc"} |
|
232 |
and the tactic @{ML [source] "Record.split_simp_tac"}. The simplification |
|
233 |
procedure only splits the records, whereas the tactic also simplifies the |
|
234 |
resulting goal with the standard record simplification rules. A |
|
235 |
(generalized) predicate on the record is passed as parameter that decides |
|
236 |
whether or how `deep' to split the record. It can peek on the subterm |
|
237 |
starting at the quantified occurrence of the record (including the |
|
238 |
quantifier). The value \<^ML>\<open>0\<close> indicates no split, a value greater |
|
239 |
\<^ML>\<open>0\<close> splits up to the given bound of record extension and finally the |
|
240 |
value \<^ML>\<open>~1\<close> completely splits the record. @{ML [source] |
|
241 |
"Record.split_simp_tac"} additionally takes a list of equations for |
|
242 |
simplification and can also split fixed record variables. |
|
72029 | 243 |
\<close> |
244 |
||
245 |
lemma "(\<forall>r. P (xpos r)) \<longrightarrow> (\<forall>x. P x)" |
|
246 |
apply (tactic \<open>simp_tac (put_simpset HOL_basic_ss \<^context> |
|
247 |
addsimprocs [Record.split_simproc (K ~1)]) 1\<close>) |
|
248 |
apply simp |
|
249 |
done |
|
250 |
||
251 |
lemma "(\<forall>r. P (xpos r)) \<longrightarrow> (\<forall>x. P x)" |
|
252 |
apply (tactic \<open>Record.split_simp_tac \<^context> [] (K ~1) 1\<close>) |
|
253 |
apply simp |
|
254 |
done |
|
255 |
||
256 |
lemma "(\<exists>r. P (xpos r)) \<longrightarrow> (\<exists>x. P x)" |
|
257 |
apply (tactic \<open>simp_tac (put_simpset HOL_basic_ss \<^context> |
|
258 |
addsimprocs [Record.split_simproc (K ~1)]) 1\<close>) |
|
259 |
apply simp |
|
260 |
done |
|
261 |
||
262 |
lemma "(\<exists>r. P (xpos r)) \<longrightarrow> (\<exists>x. P x)" |
|
263 |
apply (tactic \<open>Record.split_simp_tac \<^context> [] (K ~1) 1\<close>) |
|
264 |
apply simp |
|
265 |
done |
|
266 |
||
267 |
lemma "\<And>r. P (xpos r) \<Longrightarrow> (\<exists>x. P x)" |
|
268 |
apply (tactic \<open>simp_tac (put_simpset HOL_basic_ss \<^context> |
|
269 |
addsimprocs [Record.split_simproc (K ~1)]) 1\<close>) |
|
270 |
apply auto |
|
271 |
done |
|
272 |
||
273 |
lemma "\<And>r. P (xpos r) \<Longrightarrow> (\<exists>x. P x)" |
|
274 |
apply (tactic \<open>Record.split_simp_tac \<^context> [] (K ~1) 1\<close>) |
|
275 |
apply auto |
|
276 |
done |
|
277 |
||
278 |
lemma "P (xpos r) \<Longrightarrow> (\<exists>x. P x)" |
|
279 |
apply (tactic \<open>Record.split_simp_tac \<^context> [] (K ~1) 1\<close>) |
|
280 |
apply auto |
|
281 |
done |
|
282 |
||
72030 | 283 |
notepad |
284 |
begin |
|
285 |
have "\<exists>x. P x" |
|
286 |
if "P (xpos r)" for P r |
|
287 |
apply (insert that) |
|
288 |
apply (tactic \<open>Record.split_simp_tac \<^context> [] (K ~1) 1\<close>) |
|
289 |
apply auto |
|
290 |
done |
|
291 |
end |
|
72029 | 292 |
|
72030 | 293 |
text \<open> |
294 |
The effect of simproc @{ML [source] Record.ex_sel_eq_simproc} is illustrated |
|
295 |
by the following lemma.\<close> |
|
72029 | 296 |
|
297 |
lemma "\<exists>r. xpos r = x" |
|
76039
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
298 |
supply [[simproc add: Record.ex_sel_eq]] |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
299 |
apply (simp) |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
300 |
done |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
301 |
|
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
302 |
subsection \<open>Simprocs for update and equality\<close> |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
303 |
|
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
304 |
record alph1 = |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
305 |
a::nat |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
306 |
b::nat |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
307 |
|
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
308 |
record alph2 = alph1 + |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
309 |
c::nat |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
310 |
d::nat |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
311 |
|
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
312 |
record alph3 = alph2 + |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
313 |
e::nat |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
314 |
f::nat |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
315 |
|
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
316 |
text \<open>The simprocs that are activated by default are: |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
317 |
\<^item> @{ML [source] Record.simproc}: field selection of (nested) record updates. |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
318 |
\<^item> @{ML [source] Record.upd_simproc}: nested record updates. |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
319 |
\<^item> @{ML [source] Record.eq_simproc}: (componentwise) equality of records. |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
320 |
\<close> |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
321 |
|
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
322 |
|
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
323 |
text \<open>By default record updates are not ordered by simplification.\<close> |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
324 |
schematic_goal "r\<lparr>b := x, a:= y\<rparr> = ?X" |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
325 |
by simp |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
326 |
|
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
327 |
text \<open>Normalisation towards an update ordering (string ordering of update function names) can |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
328 |
be configured as follows.\<close> |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
329 |
schematic_goal "r\<lparr>b := y, a := x\<rparr> = ?X" |
76042 | 330 |
supply [[record_sort_updates]] |
76039
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
331 |
by simp |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
332 |
|
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
333 |
text \<open>Note the interplay between update ordering and record equality. Without update ordering |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
334 |
the following equality is handled by @{ML [source] Record.eq_simproc}. Record equality is thus |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
335 |
solved by componentwise comparison of all the fields of the records which can be expensive |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
336 |
in the presence of many fields.\<close> |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
337 |
|
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
338 |
lemma "r\<lparr>f := x1, a:= x2\<rparr> = r\<lparr>a := x2, f:= x1\<rparr>" |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
339 |
by simp |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
340 |
|
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
341 |
lemma "r\<lparr>f := x1, a:= x2\<rparr> = r\<lparr>a := x2, f:= x1\<rparr>" |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
342 |
supply [[simproc del: Record.eq]] |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
343 |
apply (simp?) |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
344 |
oops |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
345 |
|
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
346 |
text \<open>With update ordering the equality is already established after update normalisation. There |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
347 |
is no need for componentwise comparison.\<close> |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
348 |
|
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
349 |
lemma "r\<lparr>f := x1, a:= x2\<rparr> = r\<lparr>a := x2, f:= x1\<rparr>" |
76042 | 350 |
supply [[record_sort_updates, simproc del: Record.eq]] |
76039
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
351 |
apply simp |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
352 |
done |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
353 |
|
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
354 |
schematic_goal "r\<lparr>f := x1, e := x2, d:= x3, c:= x4, b:=x5, a:= x6\<rparr> = ?X" |
76042 | 355 |
supply [[record_sort_updates]] |
76039
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
356 |
by simp |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
357 |
|
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
358 |
schematic_goal "r\<lparr>f := x1, e := x2, d:= x3, c:= x4, e:=x5, a:= x6\<rparr> = ?X" |
76042 | 359 |
supply [[record_sort_updates]] |
76039
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
360 |
by simp |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
361 |
|
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
362 |
schematic_goal "r\<lparr>f := x1, e := x2, d:= x3, c:= x4, e:=x5, a:= x6\<rparr> = ?X" |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
363 |
by simp |
72029 | 364 |
|
365 |
||
366 |
subsection \<open>A more complex record expression\<close> |
|
367 |
||
368 |
record ('a, 'b, 'c) bar = bar1 :: 'a |
|
369 |
bar2 :: 'b |
|
370 |
bar3 :: 'c |
|
371 |
bar21 :: "'b \<times> 'a" |
|
372 |
bar32 :: "'c \<times> 'b" |
|
373 |
bar31 :: "'c \<times> 'a" |
|
374 |
||
375 |
print_record "('a,'b,'c) bar" |
|
376 |
||
72030 | 377 |
|
72029 | 378 |
subsection \<open>Some code generation\<close> |
379 |
||
380 |
export_code foo1 foo3 foo5 foo10 checking SML |
|
381 |
||
382 |
text \<open> |
|
72030 | 383 |
Code generation can also be switched off, for instance for very large |
384 |
records:\<close> |
|
72029 | 385 |
|
386 |
declare [[record_codegen = false]] |
|
387 |
||
388 |
record not_so_large_record = |
|
389 |
bar520 :: nat |
|
72030 | 390 |
bar521 :: "nat \<times> nat" |
72029 | 391 |
|
76039
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
392 |
|
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
393 |
setup \<open> |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
394 |
let |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
395 |
val N = 300 |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
396 |
in |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
397 |
Record.add_record {overloaded=false} ([], \<^binding>\<open>large_record\<close>) NONE |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
398 |
(map (fn i => (Binding.make ("fld_" ^ string_of_int i, \<^here>), @{typ nat}, Mixfix.NoSyn)) |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
399 |
(1 upto N)) |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
400 |
end |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
401 |
\<close> |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
402 |
|
76042 | 403 |
declare [[record_codegen]] |
72029 | 404 |
|
76039
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
405 |
schematic_goal \<open>fld_1 (r\<lparr>fld_300 := x300, fld_20 := x20, fld_200 := x200\<rparr>) = ?X\<close> |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
406 |
by simp |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
407 |
|
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
408 |
schematic_goal \<open>r\<lparr>fld_300 := x300, fld_20 := x20, fld_200 := x200\<rparr> = ?X\<close> |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
409 |
supply [[record_sort_updates]] |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
410 |
by simp |
ca7737249aa4
option "sort_updates" for record update simproc. Make proper record simproc definitions.
Norbert Schirmer <nschirmer@apple.com>
parents:
72030
diff
changeset
|
411 |
|
72029 | 412 |
end |