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