doc-src/TutorialI/Types/Records.thy
author haftmann
Fri, 17 Jun 2005 16:12:49 +0200
changeset 16417 9bc16273c2d4
parent 15905 0a4cc9b113c7
child 27015 f8537d69f514
permissions -rw-r--r--
migrated theory headers to new format
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
     1
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
     2
header {* Records \label{sec:records} *}
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
     3
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
     4
(*<*)
16417
9bc16273c2d4 migrated theory headers to new format
haftmann
parents: 15905
diff changeset
     5
theory Records imports Main begin
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
     6
(*>*)
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
     7
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
     8
text {*
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
     9
  \index{records|(}%
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    10
  Records are familiar from programming languages.  A record of $n$
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    11
  fields is essentially an $n$-tuple, but the record's components have
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    12
  names, which can make expressions easier to read and reduces the
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    13
  risk of confusing one field for another.
11387
8db249f786ee for the records section
paulson
parents:
diff changeset
    14
12586
wenzelm
parents: 12567
diff changeset
    15
  A record of Isabelle/HOL covers a collection of fields, with select
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    16
  and update operations.  Each field has a specified type, which may
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    17
  be polymorphic.  The field names are part of the record type, and
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    18
  the order of the fields is significant --- as it is in Pascal but
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    19
  not in Standard ML.  If two different record types have field names
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    20
  in common, then the ambiguity is resolved in the usual way, by
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    21
  qualified names.
11387
8db249f786ee for the records section
paulson
parents:
diff changeset
    22
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    23
  Record types can also be defined by extending other record types.
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    24
  Extensible records make use of the reserved pseudo-field \cdx{more},
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    25
  which is present in every record type.  Generic record operations
12586
wenzelm
parents: 12567
diff changeset
    26
  work on all possible extensions of a given type scheme; polymorphism
wenzelm
parents: 12567
diff changeset
    27
  takes care of structural sub-typing behind the scenes.  There are
wenzelm
parents: 12567
diff changeset
    28
  also explicit coercion functions between fixed record types.
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    29
*}
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    30
11387
8db249f786ee for the records section
paulson
parents:
diff changeset
    31
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    32
subsection {* Record Basics *}
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    33
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    34
text {*
12586
wenzelm
parents: 12567
diff changeset
    35
  Record types are not primitive in Isabelle and have a delicate
12655
wenzelm
parents: 12634
diff changeset
    36
  internal representation \cite{NaraschewskiW-TPHOLs98}, based on
wenzelm
parents: 12634
diff changeset
    37
  nested copies of the primitive product type.  A \commdx{record}
wenzelm
parents: 12634
diff changeset
    38
  declaration introduces a new record type scheme by specifying its
wenzelm
parents: 12634
diff changeset
    39
  fields, which are packaged internally to hold up the perception of
12700
f0d09c9693d6 stylistic changes
paulson
parents: 12657
diff changeset
    40
  the record as a distinguished entity.  Here is a simple example:
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    41
*}
11387
8db249f786ee for the records section
paulson
parents:
diff changeset
    42
8db249f786ee for the records section
paulson
parents:
diff changeset
    43
record point =
8db249f786ee for the records section
paulson
parents:
diff changeset
    44
  Xcoord :: int
8db249f786ee for the records section
paulson
parents:
diff changeset
    45
  Ycoord :: int
8db249f786ee for the records section
paulson
parents:
diff changeset
    46
8db249f786ee for the records section
paulson
parents:
diff changeset
    47
text {*
15905
0a4cc9b113c7 introduced @{const ...} antiquotation
haftmann
parents: 12700
diff changeset
    48
  Records of type @{typ point} have two fields named @{const Xcoord}
0a4cc9b113c7 introduced @{const ...} antiquotation
haftmann
parents: 12700
diff changeset
    49
  and @{const Ycoord}, both of type~@{typ int}.  We now define a
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    50
  constant of type @{typ point}:
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    51
*}
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    52
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    53
constdefs
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    54
  pt1 :: point
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    55
  "pt1 \<equiv> (| Xcoord = 999, Ycoord = 23 |)"
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    56
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    57
text {*
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    58
  We see above the ASCII notation for record brackets.  You can also
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    59
  use the symbolic brackets @{text \<lparr>} and @{text \<rparr>}.  Record type
12655
wenzelm
parents: 12634
diff changeset
    60
  expressions can be also written directly with individual fields.
12700
f0d09c9693d6 stylistic changes
paulson
parents: 12657
diff changeset
    61
  The type name above is merely an abbreviation.
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    62
*}
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    63
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    64
constdefs
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    65
  pt2 :: "\<lparr>Xcoord :: int, Ycoord :: int\<rparr>"
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    66
  "pt2 \<equiv> \<lparr>Xcoord = -45, Ycoord = 97\<rparr>"
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    67
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    68
text {*
12586
wenzelm
parents: 12567
diff changeset
    69
  For each field, there is a \emph{selector}\index{selector!record}
wenzelm
parents: 12567
diff changeset
    70
  function of the same name.  For example, if @{text p} has type @{typ
wenzelm
parents: 12567
diff changeset
    71
  point} then @{text "Xcoord p"} denotes the value of the @{text
wenzelm
parents: 12567
diff changeset
    72
  Xcoord} field of~@{text p}.  Expressions involving field selection
wenzelm
parents: 12567
diff changeset
    73
  of explicit records are simplified automatically:
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    74
*}
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    75
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    76
lemma "Xcoord \<lparr>Xcoord = a, Ycoord = b\<rparr> = a"
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    77
  by simp
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    78
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    79
text {*
12586
wenzelm
parents: 12567
diff changeset
    80
  The \emph{update}\index{update!record} operation is functional.  For
15905
0a4cc9b113c7 introduced @{const ...} antiquotation
haftmann
parents: 12700
diff changeset
    81
  example, @{term "p\<lparr>Xcoord := 0\<rparr>"} is a record whose @{const Xcoord}
0a4cc9b113c7 introduced @{const ...} antiquotation
haftmann
parents: 12700
diff changeset
    82
  value is zero and whose @{const Ycoord} value is copied from~@{text
12655
wenzelm
parents: 12634
diff changeset
    83
  p}.  Updates of explicit records are also simplified automatically:
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    84
*}
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    85
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    86
lemma "\<lparr>Xcoord = a, Ycoord = b\<rparr>\<lparr>Xcoord := 0\<rparr> =
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    87
    \<lparr>Xcoord = 0, Ycoord = b\<rparr>"
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    88
  by simp
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    89
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    90
text {*
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    91
  \begin{warn}
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    92
  Field names are declared as constants and can no longer be used as
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    93
  variables.  It would be unwise, for example, to call the fields of
12586
wenzelm
parents: 12567
diff changeset
    94
  type @{typ point} simply @{text x} and~@{text y}.
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    95
  \end{warn}
11387
8db249f786ee for the records section
paulson
parents:
diff changeset
    96
*}
8db249f786ee for the records section
paulson
parents:
diff changeset
    97
11428
332347b9b942 tidying the index
paulson
parents: 11389
diff changeset
    98
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
    99
subsection {* Extensible Records and Generic Operations *}
11387
8db249f786ee for the records section
paulson
parents:
diff changeset
   100
8db249f786ee for the records section
paulson
parents:
diff changeset
   101
text {*
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   102
  \index{records!extensible|(}%
11387
8db249f786ee for the records section
paulson
parents:
diff changeset
   103
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   104
  Now, let us define coloured points (type @{text cpoint}) to be
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   105
  points extended with a field @{text col} of type @{text colour}:
11387
8db249f786ee for the records section
paulson
parents:
diff changeset
   106
*}
8db249f786ee for the records section
paulson
parents:
diff changeset
   107
8db249f786ee for the records section
paulson
parents:
diff changeset
   108
datatype colour = Red | Green | Blue
8db249f786ee for the records section
paulson
parents:
diff changeset
   109
8db249f786ee for the records section
paulson
parents:
diff changeset
   110
record cpoint = point +
8db249f786ee for the records section
paulson
parents:
diff changeset
   111
  col :: colour
8db249f786ee for the records section
paulson
parents:
diff changeset
   112
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   113
text {*
15905
0a4cc9b113c7 introduced @{const ...} antiquotation
haftmann
parents: 12700
diff changeset
   114
  The fields of this new type are @{const Xcoord}, @{text Ycoord} and
12655
wenzelm
parents: 12634
diff changeset
   115
  @{text col}, in that order.
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   116
*}
11387
8db249f786ee for the records section
paulson
parents:
diff changeset
   117
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   118
constdefs
11387
8db249f786ee for the records section
paulson
parents:
diff changeset
   119
  cpt1 :: cpoint
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   120
  "cpt1 \<equiv> \<lparr>Xcoord = 999, Ycoord = 23, col = Green\<rparr>"
11387
8db249f786ee for the records section
paulson
parents:
diff changeset
   121
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   122
text {*
12657
wenzelm
parents: 12655
diff changeset
   123
  \medskip We can define generic operations that work on arbitrary
wenzelm
parents: 12655
diff changeset
   124
  instances of a record scheme, e.g.\ covering @{typ point}, @{typ
wenzelm
parents: 12655
diff changeset
   125
  cpoint}, and any further extensions.  Every record structure has an
wenzelm
parents: 12655
diff changeset
   126
  implicit pseudo-field, \cdx{more}, that keeps the extension as an
wenzelm
parents: 12655
diff changeset
   127
  explicit value.  Its type is declared as completely
wenzelm
parents: 12655
diff changeset
   128
  polymorphic:~@{typ 'a}.  When a fixed record value is expressed
wenzelm
parents: 12655
diff changeset
   129
  using just its standard fields, the value of @{text more} is
wenzelm
parents: 12655
diff changeset
   130
  implicitly set to @{text "()"}, the empty tuple, which has type
wenzelm
parents: 12655
diff changeset
   131
  @{typ unit}.  Within the record brackets, you can refer to the
wenzelm
parents: 12655
diff changeset
   132
  @{text more} field by writing ``@{text "\<dots>"}'' (three dots):
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   133
*}
11387
8db249f786ee for the records section
paulson
parents:
diff changeset
   134
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   135
lemma "Xcoord \<lparr>Xcoord = a, Ycoord = b, \<dots> = p\<rparr> = a"
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   136
  by simp
11387
8db249f786ee for the records section
paulson
parents:
diff changeset
   137
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   138
text {*
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   139
  This lemma applies to any record whose first two fields are @{text
15905
0a4cc9b113c7 introduced @{const ...} antiquotation
haftmann
parents: 12700
diff changeset
   140
  Xcoord} and~@{const Ycoord}.  Note that @{text "\<lparr>Xcoord = a, Ycoord
12655
wenzelm
parents: 12634
diff changeset
   141
  = b, \<dots> = ()\<rparr>"} is exactly the same as @{text "\<lparr>Xcoord = a, Ycoord
wenzelm
parents: 12634
diff changeset
   142
  = b\<rparr>"}.  Selectors and updates are always polymorphic wrt.\ the
wenzelm
parents: 12634
diff changeset
   143
  @{text more} part of a record scheme, its value is just ignored (for
12586
wenzelm
parents: 12567
diff changeset
   144
  select) or copied (for update).
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   145
12586
wenzelm
parents: 12567
diff changeset
   146
  The @{text more} pseudo-field may be manipulated directly as well,
wenzelm
parents: 12567
diff changeset
   147
  but the identifier needs to be qualified:
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   148
*}
11387
8db249f786ee for the records section
paulson
parents:
diff changeset
   149
8db249f786ee for the records section
paulson
parents:
diff changeset
   150
lemma "point.more cpt1 = \<lparr>col = Green\<rparr>"
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   151
  by (simp add: cpt1_def)
11387
8db249f786ee for the records section
paulson
parents:
diff changeset
   152
8db249f786ee for the records section
paulson
parents:
diff changeset
   153
text {*
12655
wenzelm
parents: 12634
diff changeset
   154
  We see that the colour part attached to this @{typ point} is a
12700
f0d09c9693d6 stylistic changes
paulson
parents: 12657
diff changeset
   155
  rudimentary record in its own right, namely @{text "\<lparr>col =
12586
wenzelm
parents: 12567
diff changeset
   156
  Green\<rparr>"}.  In order to select or update @{text col}, this fragment
wenzelm
parents: 12567
diff changeset
   157
  needs to be put back into the context of the parent type scheme, say
wenzelm
parents: 12567
diff changeset
   158
  as @{text more} part of another @{typ point}.
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   159
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   160
  To define generic operations, we need to know a bit more about
12655
wenzelm
parents: 12634
diff changeset
   161
  records.  Our definition of @{typ point} above has generated two
wenzelm
parents: 12634
diff changeset
   162
  type abbreviations:
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   163
12586
wenzelm
parents: 12567
diff changeset
   164
  \medskip
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   165
  \begin{tabular}{l}
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   166
  @{typ point}~@{text "="}~@{typ "\<lparr>Xcoord :: int, Ycoord :: int\<rparr>"} \\
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   167
  @{typ "'a point_scheme"}~@{text "="}~@{typ "\<lparr>Xcoord :: int, Ycoord :: int, \<dots> :: 'a\<rparr>"} \\
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   168
  \end{tabular}
12586
wenzelm
parents: 12567
diff changeset
   169
  \medskip
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   170
12586
wenzelm
parents: 12567
diff changeset
   171
  Type @{typ point} is for fixed records having exactly the two fields
15905
0a4cc9b113c7 introduced @{const ...} antiquotation
haftmann
parents: 12700
diff changeset
   172
  @{const Xcoord} and~@{text Ycoord}, while the polymorphic type @{typ
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   173
  "'a point_scheme"} comprises all possible extensions to those two
12586
wenzelm
parents: 12567
diff changeset
   174
  fields.  Note that @{typ "unit point_scheme"} coincides with @{typ
wenzelm
parents: 12567
diff changeset
   175
  point}, and @{typ "\<lparr>col :: colour\<rparr> point_scheme"} with @{typ
wenzelm
parents: 12567
diff changeset
   176
  cpoint}.
wenzelm
parents: 12567
diff changeset
   177
wenzelm
parents: 12567
diff changeset
   178
  In the following example we define two operations --- methods, if we
wenzelm
parents: 12567
diff changeset
   179
  regard records as objects --- to get and set any point's @{text
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   180
  Xcoord} field.
11387
8db249f786ee for the records section
paulson
parents:
diff changeset
   181
*}
8db249f786ee for the records section
paulson
parents:
diff changeset
   182
8db249f786ee for the records section
paulson
parents:
diff changeset
   183
constdefs
11942
06fac365248d accomodate some recent changes of record package;
wenzelm
parents: 11711
diff changeset
   184
  getX :: "'a point_scheme \<Rightarrow> int"
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   185
  "getX r \<equiv> Xcoord r"
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   186
  setX :: "'a point_scheme \<Rightarrow> int \<Rightarrow> 'a point_scheme"
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   187
  "setX r a \<equiv> r\<lparr>Xcoord := a\<rparr>"
11387
8db249f786ee for the records section
paulson
parents:
diff changeset
   188
8db249f786ee for the records section
paulson
parents:
diff changeset
   189
text {*
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   190
  Here is a generic method that modifies a point, incrementing its
15905
0a4cc9b113c7 introduced @{const ...} antiquotation
haftmann
parents: 12700
diff changeset
   191
  @{const Xcoord} field.  The @{text Ycoord} and @{text more} fields
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   192
  are copied across.  It works for any record type scheme derived from
12586
wenzelm
parents: 12567
diff changeset
   193
  @{typ point} (including @{typ cpoint} etc.):
11387
8db249f786ee for the records section
paulson
parents:
diff changeset
   194
*}
8db249f786ee for the records section
paulson
parents:
diff changeset
   195
8db249f786ee for the records section
paulson
parents:
diff changeset
   196
constdefs
11942
06fac365248d accomodate some recent changes of record package;
wenzelm
parents: 11711
diff changeset
   197
  incX :: "'a point_scheme \<Rightarrow> 'a point_scheme"
12655
wenzelm
parents: 12634
diff changeset
   198
  "incX r \<equiv>
wenzelm
parents: 12634
diff changeset
   199
    \<lparr>Xcoord = Xcoord r + 1, Ycoord = Ycoord r, \<dots> = point.more r\<rparr>"
11387
8db249f786ee for the records section
paulson
parents:
diff changeset
   200
8db249f786ee for the records section
paulson
parents:
diff changeset
   201
text {*
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   202
  Generic theorems can be proved about generic methods.  This trivial
15905
0a4cc9b113c7 introduced @{const ...} antiquotation
haftmann
parents: 12700
diff changeset
   203
  lemma relates @{const incX} to @{text getX} and @{text setX}:
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   204
*}
11387
8db249f786ee for the records section
paulson
parents:
diff changeset
   205
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   206
lemma "incX r = setX r (getX r + 1)"
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   207
  by (simp add: getX_def setX_def incX_def)
11387
8db249f786ee for the records section
paulson
parents:
diff changeset
   208
8db249f786ee for the records section
paulson
parents:
diff changeset
   209
text {*
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   210
  \begin{warn}
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   211
  If you use the symbolic record brackets @{text \<lparr>} and @{text \<rparr>},
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   212
  then you must also use the symbolic ellipsis, ``@{text \<dots>}'', rather
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   213
  than three consecutive periods, ``@{text "..."}''.  Mixing the ASCII
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   214
  and symbolic versions causes a syntax error.  (The two versions are
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   215
  more distinct on screen than they are on paper.)
12634
3baa6143a9c4 fixed \index;
wenzelm
parents: 12586
diff changeset
   216
  \end{warn}%
3baa6143a9c4 fixed \index;
wenzelm
parents: 12586
diff changeset
   217
  \index{records!extensible|)}
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   218
*}
11387
8db249f786ee for the records section
paulson
parents:
diff changeset
   219
8db249f786ee for the records section
paulson
parents:
diff changeset
   220
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   221
subsection {* Record Equality *}
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   222
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   223
text {*
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   224
  Two records are equal\index{equality!of records} if all pairs of
12655
wenzelm
parents: 12634
diff changeset
   225
  corresponding fields are equal.  Concrete record equalities are
wenzelm
parents: 12634
diff changeset
   226
  simplified automatically:
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   227
*}
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   228
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   229
lemma "(\<lparr>Xcoord = a, Ycoord = b\<rparr> = \<lparr>Xcoord = a', Ycoord = b'\<rparr>) =
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   230
    (a = a' \<and> b = b')"
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   231
  by simp
11387
8db249f786ee for the records section
paulson
parents:
diff changeset
   232
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   233
text {*
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   234
  The following equality is similar, but generic, in that @{text r}
12586
wenzelm
parents: 12567
diff changeset
   235
  can be any instance of @{typ "'a point_scheme"}:
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   236
*}
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   237
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   238
lemma "r\<lparr>Xcoord := a, Ycoord := b\<rparr> = r\<lparr>Ycoord := b, Xcoord := a\<rparr>"
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   239
  by simp
11387
8db249f786ee for the records section
paulson
parents:
diff changeset
   240
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   241
text {*
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   242
  We see above the syntax for iterated updates.  We could equivalently
12586
wenzelm
parents: 12567
diff changeset
   243
  have written the left-hand side as @{text "r\<lparr>Xcoord := a\<rparr>\<lparr>Ycoord :=
wenzelm
parents: 12567
diff changeset
   244
  b\<rparr>"}.
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   245
12586
wenzelm
parents: 12567
diff changeset
   246
  \medskip Record equality is \emph{extensional}:
wenzelm
parents: 12567
diff changeset
   247
  \index{extensionality!for records} a record is determined entirely
wenzelm
parents: 12567
diff changeset
   248
  by the values of its fields.
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   249
*}
11387
8db249f786ee for the records section
paulson
parents:
diff changeset
   250
8db249f786ee for the records section
paulson
parents:
diff changeset
   251
lemma "r = \<lparr>Xcoord = Xcoord r, Ycoord = Ycoord r\<rparr>"
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   252
  by simp
11387
8db249f786ee for the records section
paulson
parents:
diff changeset
   253
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   254
text {*
12586
wenzelm
parents: 12567
diff changeset
   255
  The generic version of this equality includes the pseudo-field
wenzelm
parents: 12567
diff changeset
   256
  @{text more}:
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   257
*}
11387
8db249f786ee for the records section
paulson
parents:
diff changeset
   258
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   259
lemma "r = \<lparr>Xcoord = Xcoord r, Ycoord = Ycoord r, \<dots> = point.more r\<rparr>"
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   260
  by simp
11387
8db249f786ee for the records section
paulson
parents:
diff changeset
   261
8db249f786ee for the records section
paulson
parents:
diff changeset
   262
text {*
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   263
  \medskip The simplifier can prove many record equalities
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   264
  automatically, but general equality reasoning can be tricky.
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   265
  Consider proving this obvious fact:
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   266
*}
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   267
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   268
lemma "r\<lparr>Xcoord := a\<rparr> = r\<lparr>Xcoord := a'\<rparr> \<Longrightarrow> a = a'"
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   269
  apply simp?
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   270
  oops
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   271
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   272
text {*
12586
wenzelm
parents: 12567
diff changeset
   273
  Here the simplifier can do nothing, since general record equality is
wenzelm
parents: 12567
diff changeset
   274
  not eliminated automatically.  One way to proceed is by an explicit
15905
0a4cc9b113c7 introduced @{const ...} antiquotation
haftmann
parents: 12700
diff changeset
   275
  forward step that applies the selector @{const Xcoord} to both sides
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   276
  of the assumed record equality:
11387
8db249f786ee for the records section
paulson
parents:
diff changeset
   277
*}
8db249f786ee for the records section
paulson
parents:
diff changeset
   278
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   279
lemma "r\<lparr>Xcoord := a\<rparr> = r\<lparr>Xcoord := a'\<rparr> \<Longrightarrow> a = a'"
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   280
  apply (drule_tac f = Xcoord in arg_cong)
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   281
  txt {* @{subgoals [display, indent = 0, margin = 65]}
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   282
    Now, @{text simp} will reduce the assumption to the desired
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   283
    conclusion. *}
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   284
  apply simp
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   285
  done
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   286
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   287
text {*
12586
wenzelm
parents: 12567
diff changeset
   288
  The @{text cases} method is preferable to such a forward proof.  We
wenzelm
parents: 12567
diff changeset
   289
  state the desired lemma again:
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   290
*}
11387
8db249f786ee for the records section
paulson
parents:
diff changeset
   291
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   292
lemma "r\<lparr>Xcoord := a\<rparr> = r\<lparr>Xcoord := a'\<rparr> \<Longrightarrow> a = a'"
12586
wenzelm
parents: 12567
diff changeset
   293
wenzelm
parents: 12567
diff changeset
   294
  txt {* The \methdx{cases} method adds an equality to replace the
wenzelm
parents: 12567
diff changeset
   295
  named record term by an explicit record expression, listing all
wenzelm
parents: 12567
diff changeset
   296
  fields.  It even includes the pseudo-field @{text more}, since the
wenzelm
parents: 12567
diff changeset
   297
  record equality stated here is generic for all extensions. *}
wenzelm
parents: 12567
diff changeset
   298
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   299
  apply (cases r)
11387
8db249f786ee for the records section
paulson
parents:
diff changeset
   300
12586
wenzelm
parents: 12567
diff changeset
   301
  txt {* @{subgoals [display, indent = 0, margin = 65]} Again, @{text
wenzelm
parents: 12567
diff changeset
   302
  simp} finishes the proof.  Because @{text r} is now represented as
wenzelm
parents: 12567
diff changeset
   303
  an explicit record construction, the updates can be applied and the
wenzelm
parents: 12567
diff changeset
   304
  record equality can be replaced by equality of the corresponding
wenzelm
parents: 12567
diff changeset
   305
  fields (due to injectivity). *}
wenzelm
parents: 12567
diff changeset
   306
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   307
  apply simp
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   308
  done
11387
8db249f786ee for the records section
paulson
parents:
diff changeset
   309
12586
wenzelm
parents: 12567
diff changeset
   310
text {*
wenzelm
parents: 12567
diff changeset
   311
  The generic cases method does not admit references to locally bound
wenzelm
parents: 12567
diff changeset
   312
  parameters of a goal.  In longer proof scripts one might have to
wenzelm
parents: 12567
diff changeset
   313
  fall back on the primitive @{text rule_tac} used together with the
12657
wenzelm
parents: 12655
diff changeset
   314
  internal field representation rules of records.  The above use of
wenzelm
parents: 12655
diff changeset
   315
  @{text "(cases r)"} would become @{text "(rule_tac r = r in
12586
wenzelm
parents: 12567
diff changeset
   316
  point.cases_scheme)"}.
wenzelm
parents: 12567
diff changeset
   317
*}
wenzelm
parents: 12567
diff changeset
   318
11387
8db249f786ee for the records section
paulson
parents:
diff changeset
   319
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   320
subsection {* Extending and Truncating Records *}
11387
8db249f786ee for the records section
paulson
parents:
diff changeset
   321
8db249f786ee for the records section
paulson
parents:
diff changeset
   322
text {*
12586
wenzelm
parents: 12567
diff changeset
   323
  Each record declaration introduces a number of derived operations to
wenzelm
parents: 12567
diff changeset
   324
  refer collectively to a record's fields and to convert between fixed
wenzelm
parents: 12567
diff changeset
   325
  record types.  They can, for instance, convert between types @{typ
wenzelm
parents: 12567
diff changeset
   326
  point} and @{typ cpoint}.  We can add a colour to a point or convert
wenzelm
parents: 12567
diff changeset
   327
  a @{typ cpoint} to a @{typ point} by forgetting its colour.
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   328
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   329
  \begin{itemize}
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   330
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   331
  \item Function \cdx{make} takes as arguments all of the record's
12586
wenzelm
parents: 12567
diff changeset
   332
  fields (including those inherited from ancestors).  It returns the
wenzelm
parents: 12567
diff changeset
   333
  corresponding record.
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   334
12586
wenzelm
parents: 12567
diff changeset
   335
  \item Function \cdx{fields} takes the record's very own fields and
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   336
  returns a record fragment consisting of just those fields.  This may
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   337
  be filled into the @{text more} part of the parent record scheme.
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   338
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   339
  \item Function \cdx{extend} takes two arguments: a record to be
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   340
  extended and a record containing the new fields.
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   341
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   342
  \item Function \cdx{truncate} takes a record (possibly an extension
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   343
  of the original record type) and returns a fixed record, removing
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   344
  any additional fields.
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   345
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   346
  \end{itemize}
11387
8db249f786ee for the records section
paulson
parents:
diff changeset
   347
12700
f0d09c9693d6 stylistic changes
paulson
parents: 12657
diff changeset
   348
  These functions provide useful abbreviations for standard
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   349
  record expressions involving constructors and selectors.  The
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   350
  definitions, which are \emph{not} unfolded by default, are made
12586
wenzelm
parents: 12567
diff changeset
   351
  available by the collective name of @{text defs} (@{text
wenzelm
parents: 12567
diff changeset
   352
  point.defs}, @{text cpoint.defs}, etc.).
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   353
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   354
  For example, here are the versions of those functions generated for
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   355
  record @{typ point}.  We omit @{text point.fields}, which happens to
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   356
  be the same as @{text point.make}.
11387
8db249f786ee for the records section
paulson
parents:
diff changeset
   357
12586
wenzelm
parents: 12567
diff changeset
   358
  @{thm [display, indent = 0, margin = 65] point.make_def [no_vars]
wenzelm
parents: 12567
diff changeset
   359
  point.extend_def [no_vars] point.truncate_def [no_vars]}
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   360
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   361
  Contrast those with the corresponding functions for record @{typ
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   362
  cpoint}.  Observe @{text cpoint.fields} in particular.
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   363
12586
wenzelm
parents: 12567
diff changeset
   364
  @{thm [display, indent = 0, margin = 65] cpoint.make_def [no_vars]
wenzelm
parents: 12567
diff changeset
   365
  cpoint.fields_def [no_vars] cpoint.extend_def [no_vars]
wenzelm
parents: 12567
diff changeset
   366
  cpoint.truncate_def [no_vars]}
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   367
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   368
  To demonstrate these functions, we declare a new coloured point by
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   369
  extending an ordinary point.  Function @{text point.extend} augments
12586
wenzelm
parents: 12567
diff changeset
   370
  @{text pt1} with a colour value, which is converted into an
wenzelm
parents: 12567
diff changeset
   371
  appropriate record fragment by @{text cpoint.fields}.
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   372
*}
11387
8db249f786ee for the records section
paulson
parents:
diff changeset
   373
12407
70ebb59264f1 record extend and truncate
paulson
parents: 12156
diff changeset
   374
constdefs
70ebb59264f1 record extend and truncate
paulson
parents: 12156
diff changeset
   375
  cpt2 :: cpoint
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   376
  "cpt2 \<equiv> point.extend pt1 (cpoint.fields Green)"
12407
70ebb59264f1 record extend and truncate
paulson
parents: 12156
diff changeset
   377
70ebb59264f1 record extend and truncate
paulson
parents: 12156
diff changeset
   378
text {*
15905
0a4cc9b113c7 introduced @{const ...} antiquotation
haftmann
parents: 12700
diff changeset
   379
  The coloured points @{const cpt1} and @{text cpt2} are equal.  The
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   380
  proof is trivial, by unfolding all the definitions.  We deliberately
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   381
  omit the definition of~@{text pt1} in order to reveal the underlying
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   382
  comparison on type @{typ point}.
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   383
*}
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   384
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   385
lemma "cpt1 = cpt2"
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   386
  apply (simp add: cpt1_def cpt2_def point.defs cpoint.defs)
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   387
  txt {* @{subgoals [display, indent = 0, margin = 65]} *}
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   388
  apply (simp add: pt1_def)
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   389
  done
12407
70ebb59264f1 record extend and truncate
paulson
parents: 12156
diff changeset
   390
70ebb59264f1 record extend and truncate
paulson
parents: 12156
diff changeset
   391
text {*
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   392
  In the example below, a coloured point is truncated to leave a
12586
wenzelm
parents: 12567
diff changeset
   393
  point.  We use the @{text truncate} function of the target record.
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   394
*}
12407
70ebb59264f1 record extend and truncate
paulson
parents: 12156
diff changeset
   395
70ebb59264f1 record extend and truncate
paulson
parents: 12156
diff changeset
   396
lemma "point.truncate cpt2 = pt1"
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   397
  by (simp add: pt1_def cpt2_def point.defs)
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   398
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   399
text {*
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   400
  \begin{exercise}
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   401
  Extend record @{typ cpoint} to have a further field, @{text
12586
wenzelm
parents: 12567
diff changeset
   402
  intensity}, of type~@{typ nat}.  Experiment with generic operations
wenzelm
parents: 12567
diff changeset
   403
  (using polymorphic selectors and updates) and explicit coercions
wenzelm
parents: 12567
diff changeset
   404
  (using @{text extend}, @{text truncate} etc.) among the three record
wenzelm
parents: 12567
diff changeset
   405
  types.
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   406
  \end{exercise}
12407
70ebb59264f1 record extend and truncate
paulson
parents: 12156
diff changeset
   407
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   408
  \begin{exercise}
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   409
  (For Java programmers.)
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   410
  Model a small class hierarchy using records.
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   411
  \end{exercise}
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   412
  \index{records|)}
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   413
*}
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   414
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   415
(*<*)
11387
8db249f786ee for the records section
paulson
parents:
diff changeset
   416
end
12567
614ef5ca41ed turned into proper Isabelle document;
wenzelm
parents: 12409
diff changeset
   417
(*>*)