author | schirmer |
Mon, 03 May 2004 23:22:17 +0200 | |
changeset 14700 | 2f885b7e5ba7 |
parent 12591 | 5a46569d2b05 |
child 15010 | 72fbe711e414 |
permissions | -rw-r--r-- |
10052 | 1 |
(* Title: HOL/ex/Records.thy |
2 |
ID: $Id$ |
|
14700
2f885b7e5ba7
reimplementation of HOL records; only one type is created for
schirmer
parents:
12591
diff
changeset
|
3 |
Author: Wolfgang Naraschewski, Norbert Schirmer and Markus Wenzel, |
2f885b7e5ba7
reimplementation of HOL records; only one type is created for
schirmer
parents:
12591
diff
changeset
|
4 |
TU Muenchen |
10052 | 5 |
License: GPL (GNU GENERAL PUBLIC LICENSE) |
6 |
*) |
|
7 |
||
8 |
header {* Using extensible records in HOL -- points and coloured points *} |
|
9 |
||
10 |
theory Records = Main: |
|
11 |
||
12 |
subsection {* Points *} |
|
13 |
||
14 |
record point = |
|
11939 | 15 |
xpos :: nat |
16 |
ypos :: nat |
|
10052 | 17 |
|
18 |
text {* |
|
11939 | 19 |
Apart many other things, above record declaration produces the |
20 |
following theorems: |
|
10052 | 21 |
*} |
22 |
||
14700
2f885b7e5ba7
reimplementation of HOL records; only one type is created for
schirmer
parents:
12591
diff
changeset
|
23 |
|
10052 | 24 |
thm "point.simps" |
25 |
thm "point.iffs" |
|
12266 | 26 |
thm "point.defs" |
10052 | 27 |
|
28 |
text {* |
|
11939 | 29 |
The set of theorems @{thm [source] point.simps} is added |
30 |
automatically to the standard simpset, @{thm [source] point.iffs} is |
|
31 |
added to the Classical Reasoner and Simplifier context. |
|
10052 | 32 |
|
14700
2f885b7e5ba7
reimplementation of HOL records; only one type is created for
schirmer
parents:
12591
diff
changeset
|
33 |
\medskip Record declarations define new types and type abbreviations: |
10357 | 34 |
@{text [display] |
14700
2f885b7e5ba7
reimplementation of HOL records; only one type is created for
schirmer
parents:
12591
diff
changeset
|
35 |
" point = \<lparr>xpos :: nat, ypos :: nat\<rparr> = () point_ext_type |
2f885b7e5ba7
reimplementation of HOL records; only one type is created for
schirmer
parents:
12591
diff
changeset
|
36 |
'a point_scheme = \<lparr>xpos :: nat, ypos :: nat, ... :: 'a\<rparr> = 'a point_ext_type"} |
10052 | 37 |
*} |
38 |
||
39 |
consts foo1 :: point |
|
11939 | 40 |
consts foo2 :: "(| xpos :: nat, ypos :: nat |)" |
41 |
consts foo3 :: "'a => 'a point_scheme" |
|
42 |
consts foo4 :: "'a => (| xpos :: nat, ypos :: nat, ... :: 'a |)" |
|
10052 | 43 |
|
44 |
||
45 |
subsubsection {* Introducing concrete records and record schemes *} |
|
46 |
||
47 |
defs |
|
11939 | 48 |
foo1_def: "foo1 == (| xpos = 1, ypos = 0 |)" |
49 |
foo3_def: "foo3 ext == (| xpos = 1, ypos = 0, ... = ext |)" |
|
10052 | 50 |
|
51 |
||
52 |
subsubsection {* Record selection and record update *} |
|
53 |
||
54 |
constdefs |
|
11939 | 55 |
getX :: "'a point_scheme => nat" |
56 |
"getX r == xpos r" |
|
57 |
setX :: "'a point_scheme => nat => 'a point_scheme" |
|
58 |
"setX r n == r (| xpos := n |)" |
|
10052 | 59 |
|
60 |
||
61 |
subsubsection {* Some lemmas about records *} |
|
62 |
||
10357 | 63 |
text {* Basic simplifications. *} |
10052 | 64 |
|
11939 | 65 |
lemma "point.make n p = (| xpos = n, ypos = p |)" |
66 |
by (simp only: point.make_def) |
|
10052 | 67 |
|
11939 | 68 |
lemma "xpos (| xpos = m, ypos = n, ... = p |) = m" |
10052 | 69 |
by simp |
70 |
||
11939 | 71 |
lemma "(| xpos = m, ypos = n, ... = p |) (| xpos:= 0 |) = (| xpos = 0, ypos = n, ... = p |)" |
10052 | 72 |
by simp |
73 |
||
74 |
||
10357 | 75 |
text {* \medskip Equality of records. *} |
10052 | 76 |
|
11939 | 77 |
lemma "n = n' ==> p = p' ==> (| xpos = n, ypos = p |) = (| xpos = n', ypos = p' |)" |
10052 | 78 |
-- "introduction of concrete record equality" |
79 |
by simp |
|
80 |
||
11939 | 81 |
lemma "(| xpos = n, ypos = p |) = (| xpos = n', ypos = p' |) ==> n = n'" |
10052 | 82 |
-- "elimination of concrete record equality" |
83 |
by simp |
|
84 |
||
11939 | 85 |
lemma "r (| xpos := n |) (| ypos := m |) = r (| ypos := m |) (| xpos := n |)" |
10052 | 86 |
-- "introduction of abstract record equality" |
87 |
by simp |
|
88 |
||
11939 | 89 |
lemma "r (| xpos := n |) = r (| xpos := n' |) ==> n = n'" |
10052 | 90 |
-- "elimination of abstract record equality (manual proof)" |
91 |
proof - |
|
11939 | 92 |
assume "r (| xpos := n |) = r (| xpos := n' |)" (is "?lhs = ?rhs") |
93 |
hence "xpos ?lhs = xpos ?rhs" by simp |
|
10052 | 94 |
thus ?thesis by simp |
95 |
qed |
|
96 |
||
97 |
||
10357 | 98 |
text {* \medskip Surjective pairing *} |
10052 | 99 |
|
11939 | 100 |
lemma "r = (| xpos = xpos r, ypos = ypos r |)" |
10052 | 101 |
by simp |
102 |
||
12591 | 103 |
lemma "r = (| xpos = xpos r, ypos = ypos r, ... = point.more r |)" |
10052 | 104 |
by simp |
105 |
||
106 |
||
10357 | 107 |
text {* |
11939 | 108 |
\medskip Representation of records by cases or (degenerate) |
109 |
induction. |
|
10357 | 110 |
*} |
10052 | 111 |
|
14700
2f885b7e5ba7
reimplementation of HOL records; only one type is created for
schirmer
parents:
12591
diff
changeset
|
112 |
lemma "r(| xpos := n |) (| ypos := m |) = r (| ypos := m |) (| xpos := n |)" |
11939 | 113 |
proof (cases r) |
114 |
fix xpos ypos more |
|
115 |
assume "r = (| xpos = xpos, ypos = ypos, ... = more |)" |
|
116 |
thus ?thesis by simp |
|
117 |
qed |
|
118 |
||
119 |
lemma "r (| xpos := n |) (| ypos := m |) = r (| ypos := m |) (| xpos := n |)" |
|
120 |
proof (induct r) |
|
121 |
fix xpos ypos more |
|
122 |
show "(| xpos = xpos, ypos = ypos, ... = more |) (| xpos := n, ypos := m |) = |
|
123 |
(| xpos = xpos, ypos = ypos, ... = more |) (| ypos := m, xpos := n |)" |
|
10052 | 124 |
by simp |
125 |
qed |
|
126 |
||
11939 | 127 |
lemma "r (| xpos := n |) (| xpos := m |) = r (| xpos := m |)" |
128 |
proof (cases r) |
|
129 |
fix xpos ypos more |
|
130 |
assume "r = \<lparr>xpos = xpos, ypos = ypos, \<dots> = more\<rparr>" |
|
131 |
thus ?thesis by simp |
|
10052 | 132 |
qed |
133 |
||
11939 | 134 |
lemma "r (| xpos := n |) (| xpos := m |) = r (| xpos := m |)" |
135 |
proof (cases r) |
|
136 |
case fields |
|
137 |
thus ?thesis by simp |
|
138 |
qed |
|
139 |
||
140 |
lemma "r (| xpos := n |) (| xpos := m |) = r (| xpos := m |)" |
|
141 |
by (cases r) simp |
|
142 |
||
10052 | 143 |
|
10357 | 144 |
text {* |
145 |
\medskip Concrete records are type instances of record schemes. |
|
146 |
*} |
|
10052 | 147 |
|
148 |
constdefs |
|
149 |
foo5 :: nat |
|
11939 | 150 |
"foo5 == getX (| xpos = 1, ypos = 0 |)" |
10052 | 151 |
|
152 |
||
11939 | 153 |
text {* \medskip Manipulating the ``@{text "..."}'' (more) part. *} |
10052 | 154 |
|
155 |
constdefs |
|
11939 | 156 |
incX :: "'a point_scheme => 'a point_scheme" |
157 |
"incX r == (| xpos = xpos r + 1, ypos = ypos r, ... = point.more r |)" |
|
10052 | 158 |
|
11939 | 159 |
lemma "incX r = setX r (Suc (getX r))" |
160 |
by (simp add: getX_def setX_def incX_def) |
|
161 |
||
10052 | 162 |
|
10357 | 163 |
text {* An alternative definition. *} |
10052 | 164 |
|
165 |
constdefs |
|
11939 | 166 |
incX' :: "'a point_scheme => 'a point_scheme" |
167 |
"incX' r == r (| xpos := xpos r + 1 |)" |
|
10052 | 168 |
|
169 |
||
170 |
subsection {* Coloured points: record extension *} |
|
171 |
||
172 |
datatype colour = Red | Green | Blue |
|
173 |
||
174 |
record cpoint = point + |
|
175 |
colour :: colour |
|
176 |
||
177 |
||
178 |
text {* |
|
14700
2f885b7e5ba7
reimplementation of HOL records; only one type is created for
schirmer
parents:
12591
diff
changeset
|
179 |
The record declaration defines a new type constructure and abbreviations: |
10357 | 180 |
@{text [display] |
14700
2f885b7e5ba7
reimplementation of HOL records; only one type is created for
schirmer
parents:
12591
diff
changeset
|
181 |
" cpoint = (| xpos :: nat, ypos :: nat, colour :: colour |) = |
2f885b7e5ba7
reimplementation of HOL records; only one type is created for
schirmer
parents:
12591
diff
changeset
|
182 |
() cpoint_ext_type point_ext_type |
2f885b7e5ba7
reimplementation of HOL records; only one type is created for
schirmer
parents:
12591
diff
changeset
|
183 |
'a cpoint_scheme = (| xpos :: nat, ypos :: nat, colour :: colour, ... :: 'a |) = |
2f885b7e5ba7
reimplementation of HOL records; only one type is created for
schirmer
parents:
12591
diff
changeset
|
184 |
'a cpoint_ext_type point_ext_type"} |
10052 | 185 |
*} |
186 |
||
187 |
consts foo6 :: cpoint |
|
11939 | 188 |
consts foo7 :: "(| xpos :: nat, ypos :: nat, colour :: colour |)" |
189 |
consts foo8 :: "'a cpoint_scheme" |
|
190 |
consts foo9 :: "(| xpos :: nat, ypos :: nat, colour :: colour, ... :: 'a |)" |
|
10052 | 191 |
|
192 |
||
10357 | 193 |
text {* |
194 |
Functions on @{text point} schemes work for @{text cpoints} as well. |
|
195 |
*} |
|
10052 | 196 |
|
197 |
constdefs |
|
198 |
foo10 :: nat |
|
11939 | 199 |
"foo10 == getX (| xpos = 2, ypos = 0, colour = Blue |)" |
10052 | 200 |
|
201 |
||
202 |
subsubsection {* Non-coercive structural subtyping *} |
|
203 |
||
10357 | 204 |
text {* |
205 |
Term @{term foo11} has type @{typ cpoint}, not type @{typ point} --- |
|
206 |
Great! |
|
207 |
*} |
|
10052 | 208 |
|
209 |
constdefs |
|
210 |
foo11 :: cpoint |
|
11939 | 211 |
"foo11 == setX (| xpos = 2, ypos = 0, colour = Blue |) 0" |
10052 | 212 |
|
213 |
||
214 |
subsection {* Other features *} |
|
215 |
||
10357 | 216 |
text {* Field names contribute to record identity. *} |
10052 | 217 |
|
218 |
record point' = |
|
11939 | 219 |
xpos' :: nat |
220 |
ypos' :: nat |
|
10052 | 221 |
|
10357 | 222 |
text {* |
11939 | 223 |
\noindent May not apply @{term getX} to @{term [source] "(| xpos' = |
224 |
2, ypos' = 0 |)"} -- type error. |
|
10357 | 225 |
*} |
10052 | 226 |
|
10357 | 227 |
text {* \medskip Polymorphic records. *} |
10052 | 228 |
|
229 |
record 'a point'' = point + |
|
230 |
content :: 'a |
|
231 |
||
232 |
types cpoint'' = "colour point''" |
|
233 |
||
234 |
end |