src/HOL/ex/Records.thy
author wenzelm
Fri Nov 17 02:20:03 2006 +0100 (2006-11-17)
changeset 21404 eb85850d3eb7
parent 19736 d8d0f8f51d69
child 22737 d87ccbcc2702
permissions -rw-r--r--
more robust syntax for definition/abbreviation/notation;
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@21404
    54
  getX :: "'a point_scheme => nat" where
wenzelm@19736
    55
  "getX r = xpos r"
wenzelm@21404
    56
wenzelm@21404
    57
definition
wenzelm@21404
    58
  setX :: "'a point_scheme => nat => 'a point_scheme" where
wenzelm@19736
    59
  "setX r n = r (| xpos := n |)"
wenzelm@10052
    60
wenzelm@10052
    61
wenzelm@10052
    62
subsubsection {* Some lemmas about records *}
wenzelm@10052
    63
wenzelm@10357
    64
text {* Basic simplifications. *}
wenzelm@10052
    65
wenzelm@11939
    66
lemma "point.make n p = (| xpos = n, ypos = p |)"
wenzelm@11939
    67
  by (simp only: point.make_def)
wenzelm@10052
    68
wenzelm@11939
    69
lemma "xpos (| xpos = m, ypos = n, ... = p |) = m"
wenzelm@10052
    70
  by simp
wenzelm@10052
    71
wenzelm@11939
    72
lemma "(| xpos = m, ypos = n, ... = p |) (| xpos:= 0 |) = (| xpos = 0, ypos = n, ... = p |)"
wenzelm@10052
    73
  by simp
wenzelm@10052
    74
wenzelm@10052
    75
wenzelm@10357
    76
text {* \medskip Equality of records. *}
wenzelm@10052
    77
wenzelm@11939
    78
lemma "n = n' ==> p = p' ==> (| xpos = n, ypos = p |) = (| xpos = n', ypos = p' |)"
wenzelm@10052
    79
  -- "introduction of concrete record equality"
wenzelm@10052
    80
  by simp
wenzelm@10052
    81
wenzelm@11939
    82
lemma "(| xpos = n, ypos = p |) = (| xpos = n', ypos = p' |) ==> n = n'"
wenzelm@10052
    83
  -- "elimination of concrete record equality"
wenzelm@10052
    84
  by simp
wenzelm@10052
    85
wenzelm@11939
    86
lemma "r (| xpos := n |) (| ypos := m |) = r (| ypos := m |) (| xpos := n |)"
wenzelm@10052
    87
  -- "introduction of abstract record equality"
wenzelm@10052
    88
  by simp
wenzelm@10052
    89
wenzelm@11939
    90
lemma "r (| xpos := n |) = r (| xpos := n' |) ==> n = n'"
wenzelm@10052
    91
  -- "elimination of abstract record equality (manual proof)"
wenzelm@10052
    92
proof -
wenzelm@11939
    93
  assume "r (| xpos := n |) = r (| xpos := n' |)" (is "?lhs = ?rhs")
wenzelm@11939
    94
  hence "xpos ?lhs = xpos ?rhs" by simp
wenzelm@10052
    95
  thus ?thesis by simp
wenzelm@10052
    96
qed
wenzelm@10052
    97
wenzelm@10052
    98
wenzelm@10357
    99
text {* \medskip Surjective pairing *}
wenzelm@10052
   100
wenzelm@11939
   101
lemma "r = (| xpos = xpos r, ypos = ypos r |)"
wenzelm@10052
   102
  by simp
wenzelm@10052
   103
wenzelm@12591
   104
lemma "r = (| xpos = xpos r, ypos = ypos r, ... = point.more r |)"
wenzelm@10052
   105
  by simp
wenzelm@10052
   106
wenzelm@10052
   107
wenzelm@10357
   108
text {*
wenzelm@11939
   109
  \medskip Representation of records by cases or (degenerate)
wenzelm@11939
   110
  induction.
wenzelm@10357
   111
*}
wenzelm@10052
   112
schirmer@14700
   113
lemma "r(| xpos := n |) (| ypos := m |) = r (| ypos := m |) (| xpos := n |)"
wenzelm@11939
   114
proof (cases r)
wenzelm@11939
   115
  fix xpos ypos more
wenzelm@11939
   116
  assume "r = (| xpos = xpos, ypos = ypos, ... = more |)"
wenzelm@11939
   117
  thus ?thesis by simp
wenzelm@11939
   118
qed
wenzelm@11939
   119
wenzelm@11939
   120
lemma "r (| xpos := n |) (| ypos := m |) = r (| ypos := m |) (| xpos := n |)"
wenzelm@11939
   121
proof (induct r)
wenzelm@11939
   122
  fix xpos ypos more
wenzelm@11939
   123
  show "(| xpos = xpos, ypos = ypos, ... = more |) (| xpos := n, ypos := m |) =
wenzelm@11939
   124
      (| xpos = xpos, ypos = ypos, ... = more |) (| ypos := m, xpos := n |)"
wenzelm@10052
   125
    by simp
wenzelm@10052
   126
qed
wenzelm@10052
   127
wenzelm@11939
   128
lemma "r (| xpos := n |) (| xpos := m |) = r (| xpos := m |)"
wenzelm@11939
   129
proof (cases r)
wenzelm@11939
   130
  fix xpos ypos more
wenzelm@11939
   131
  assume "r = \<lparr>xpos = xpos, ypos = ypos, \<dots> = more\<rparr>"
wenzelm@11939
   132
  thus ?thesis by simp
wenzelm@10052
   133
qed
wenzelm@10052
   134
wenzelm@11939
   135
lemma "r (| xpos := n |) (| xpos := m |) = r (| xpos := m |)"
wenzelm@11939
   136
proof (cases r)
wenzelm@11939
   137
  case fields
wenzelm@11939
   138
  thus ?thesis by simp
wenzelm@11939
   139
qed
wenzelm@11939
   140
wenzelm@11939
   141
lemma "r (| xpos := n |) (| xpos := m |) = r (| xpos := m |)"
wenzelm@11939
   142
  by (cases r) simp
wenzelm@11939
   143
wenzelm@10052
   144
wenzelm@10357
   145
text {*
wenzelm@10357
   146
 \medskip Concrete records are type instances of record schemes.
wenzelm@10357
   147
*}
wenzelm@10052
   148
wenzelm@19736
   149
definition
wenzelm@21404
   150
  foo5 :: nat where
wenzelm@19736
   151
  "foo5 = getX (| xpos = 1, ypos = 0 |)"
wenzelm@10052
   152
wenzelm@10052
   153
wenzelm@11939
   154
text {* \medskip Manipulating the ``@{text "..."}'' (more) part. *}
wenzelm@10052
   155
wenzelm@19736
   156
definition
wenzelm@21404
   157
  incX :: "'a point_scheme => 'a point_scheme" where
wenzelm@19736
   158
  "incX r = (| xpos = xpos r + 1, ypos = ypos r, ... = point.more r |)"
wenzelm@10052
   159
wenzelm@11939
   160
lemma "incX r = setX r (Suc (getX r))"
wenzelm@11939
   161
  by (simp add: getX_def setX_def incX_def)
wenzelm@11939
   162
wenzelm@10052
   163
wenzelm@10357
   164
text {* An alternative definition. *}
wenzelm@10052
   165
wenzelm@19736
   166
definition
wenzelm@21404
   167
  incX' :: "'a point_scheme => 'a point_scheme" where
wenzelm@19736
   168
  "incX' r = r (| xpos := xpos r + 1 |)"
wenzelm@10052
   169
wenzelm@10052
   170
wenzelm@10052
   171
subsection {* Coloured points: record extension *}
wenzelm@10052
   172
wenzelm@10052
   173
datatype colour = Red | Green | Blue
wenzelm@10052
   174
wenzelm@10052
   175
record cpoint = point +
wenzelm@10052
   176
  colour :: colour
wenzelm@10052
   177
wenzelm@10052
   178
wenzelm@10052
   179
text {*
schirmer@14700
   180
  The record declaration defines a new type constructure and abbreviations:
wenzelm@10357
   181
  @{text [display]
schirmer@14700
   182
"  cpoint = (| xpos :: nat, ypos :: nat, colour :: colour |) = 
schirmer@14700
   183
     () cpoint_ext_type point_ext_type
schirmer@14700
   184
   'a cpoint_scheme = (| xpos :: nat, ypos :: nat, colour :: colour, ... :: 'a |) = 
schirmer@14700
   185
     'a cpoint_ext_type point_ext_type"}
wenzelm@10052
   186
*}
wenzelm@10052
   187
wenzelm@10052
   188
consts foo6 :: cpoint
wenzelm@11939
   189
consts foo7 :: "(| xpos :: nat, ypos :: nat, colour :: colour |)"
wenzelm@11939
   190
consts foo8 :: "'a cpoint_scheme"
wenzelm@11939
   191
consts foo9 :: "(| xpos :: nat, ypos :: nat, colour :: colour, ... :: 'a |)"
wenzelm@10052
   192
wenzelm@10052
   193
wenzelm@10357
   194
text {*
wenzelm@10357
   195
 Functions on @{text point} schemes work for @{text cpoints} as well.
wenzelm@10357
   196
*}
wenzelm@10052
   197
wenzelm@19736
   198
definition
wenzelm@21404
   199
  foo10 :: nat where
wenzelm@19736
   200
  "foo10 = getX (| xpos = 2, ypos = 0, colour = Blue |)"
wenzelm@10052
   201
wenzelm@10052
   202
wenzelm@10052
   203
subsubsection {* Non-coercive structural subtyping *}
wenzelm@10052
   204
wenzelm@10357
   205
text {*
wenzelm@10357
   206
 Term @{term foo11} has type @{typ cpoint}, not type @{typ point} ---
wenzelm@10357
   207
 Great!
wenzelm@10357
   208
*}
wenzelm@10052
   209
wenzelm@19736
   210
definition
wenzelm@21404
   211
  foo11 :: cpoint where
wenzelm@19736
   212
  "foo11 = setX (| xpos = 2, ypos = 0, colour = Blue |) 0"
wenzelm@10052
   213
wenzelm@10052
   214
wenzelm@10052
   215
subsection {* Other features *}
wenzelm@10052
   216
wenzelm@10357
   217
text {* Field names contribute to record identity. *}
wenzelm@10052
   218
wenzelm@10052
   219
record point' =
wenzelm@11939
   220
  xpos' :: nat
wenzelm@11939
   221
  ypos' :: nat
wenzelm@10052
   222
wenzelm@10357
   223
text {*
wenzelm@11939
   224
  \noindent May not apply @{term getX} to @{term [source] "(| xpos' =
wenzelm@11939
   225
  2, ypos' = 0 |)"} -- type error.
wenzelm@10357
   226
*}
wenzelm@10052
   227
wenzelm@10357
   228
text {* \medskip Polymorphic records. *}
wenzelm@10052
   229
wenzelm@10052
   230
record 'a point'' = point +
wenzelm@10052
   231
  content :: 'a
wenzelm@10052
   232
wenzelm@10052
   233
types cpoint'' = "colour point''"
wenzelm@10052
   234
wenzelm@10052
   235
end