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