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