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