src/HOL/ex/Points.thy
author wenzelm
Thu, 17 Aug 2000 10:38:43 +0200
changeset 9627 31d4a77c89d6
parent 9297 bafe45732b10
permissions -rw-r--r--
updated; tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5753
wenzelm
parents: 5741
diff changeset
     1
(*  Title:      HOL/ex/Points.thy
wenzelm
parents: 5741
diff changeset
     2
    ID:         $Id$
wenzelm
parents: 5741
diff changeset
     3
    Author:     Wolfgang Naraschewski and Markus Wenzel, TU Muenchen
9297
wenzelm
parents: 9151
diff changeset
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
5753
wenzelm
parents: 5741
diff changeset
     5
*)
wenzelm
parents: 5741
diff changeset
     6
9151
wenzelm
parents: 9059
diff changeset
     7
header {* Points and coloured points --- using extensible records in HOL *}
wenzelm
parents: 9059
diff changeset
     8
9059
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
     9
theory Points = Main:
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    10
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
    11
9151
wenzelm
parents: 9059
diff changeset
    12
subsection {* Points *}
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
    13
139a25a1e01e Example for records
narasche
parents:
diff changeset
    14
record point =
139a25a1e01e Example for records
narasche
parents:
diff changeset
    15
  x :: nat
9059
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
    16
  y :: nat
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
    17
6744
9727e83f0578 changed {| |} verbatim syntax to {* *};
wenzelm
parents: 6737
diff changeset
    18
text {*
9059
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
    19
 Apart many other things, above record declaration produces the
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
    20
 following theorems:
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
    21
*}
5753
wenzelm
parents: 5741
diff changeset
    22
9059
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
    23
thm "point.simps"
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
    24
thm "point.iffs"
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
    25
thm "point.update_defs"
5753
wenzelm
parents: 5741
diff changeset
    26
9059
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
    27
text {*
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
    28
 The set of theorems "point.simps" is added automatically to the
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
    29
 standard simpset, "point.iffs" is added to the claset and simpset.
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
    30
*}
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
    31
6744
9727e83f0578 changed {| |} verbatim syntax to {* *};
wenzelm
parents: 6737
diff changeset
    32
text {*
5753
wenzelm
parents: 5741
diff changeset
    33
  Record declarations define new type abbreviations:
wenzelm
parents: 5741
diff changeset
    34
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    35
    point = "(| x :: nat, y :: nat |)"
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    36
    'a point_scheme = "(| x :: nat, y :: nat, ... :: 'a |)"
5753
wenzelm
parents: 5741
diff changeset
    37
wenzelm
parents: 5741
diff changeset
    38
  Extensions `...' must be in type class `more'!
9059
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
    39
*}
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    40
9059
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
    41
consts foo1 :: point
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
    42
consts foo2 :: "(| x :: nat, y :: nat |)"
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
    43
consts foo3 :: "'a => ('a::more) point_scheme"
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
    44
consts foo4 :: "'a => (| x :: nat, y :: nat, ... :: 'a |)"
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    45
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    46
9151
wenzelm
parents: 9059
diff changeset
    47
subsubsection {* Introducing concrete records and record schemes *}
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    48
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    49
defs
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    50
  foo1_def: "foo1 == (| x = 1, y = 0 |)"
9059
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
    51
  foo3_def: "foo3 ext == (| x = 1, y = 0, ... = ext |)"
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
    52
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    53
9151
wenzelm
parents: 9059
diff changeset
    54
subsubsection {* Record selection and record update *}
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    55
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    56
constdefs
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    57
  getX :: "('a::more) point_scheme => nat"
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    58
  "getX r == x r"
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    59
  setX :: "('a::more) point_scheme => nat => 'a point_scheme"
9059
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
    60
  "setX r n == r (| x := n |)"
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    61
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    62
9151
wenzelm
parents: 9059
diff changeset
    63
subsubsection {* Some lemmas about records *}
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    64
9151
wenzelm
parents: 9059
diff changeset
    65
text {* Basic simplifications *}
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
    66
9627
31d4a77c89d6 updated;
wenzelm
parents: 9297
diff changeset
    67
lemma "point.make n p = (| x = n, y = p |)"
31d4a77c89d6 updated;
wenzelm
parents: 9297
diff changeset
    68
  by simp
31d4a77c89d6 updated;
wenzelm
parents: 9297
diff changeset
    69
9059
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
    70
lemma "x (| x = m, y = n, ... = p |) = m"
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
    71
  by simp
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
    72
9059
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
    73
lemma "(| x = m, y = n, ... = p |) (| x:= 0 |) = (| x = 0, y = n, ... = p |)"
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
    74
  by simp
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
    75
139a25a1e01e Example for records
narasche
parents:
diff changeset
    76
9627
31d4a77c89d6 updated;
wenzelm
parents: 9297
diff changeset
    77
text {* Equality of records *}
31d4a77c89d6 updated;
wenzelm
parents: 9297
diff changeset
    78
31d4a77c89d6 updated;
wenzelm
parents: 9297
diff changeset
    79
lemma "n = n' ==> p = p' ==> (| x = n, y = p |) = (| x = n', y = p' |)"
31d4a77c89d6 updated;
wenzelm
parents: 9297
diff changeset
    80
  -- "introduction of concrete record equality"
31d4a77c89d6 updated;
wenzelm
parents: 9297
diff changeset
    81
  by simp
31d4a77c89d6 updated;
wenzelm
parents: 9297
diff changeset
    82
31d4a77c89d6 updated;
wenzelm
parents: 9297
diff changeset
    83
lemma "(| x = n, y = p |) = (| x = n', y = p' |) ==> n = n'"
31d4a77c89d6 updated;
wenzelm
parents: 9297
diff changeset
    84
  -- "elimination of concrete record equality"
31d4a77c89d6 updated;
wenzelm
parents: 9297
diff changeset
    85
  by simp
31d4a77c89d6 updated;
wenzelm
parents: 9297
diff changeset
    86
31d4a77c89d6 updated;
wenzelm
parents: 9297
diff changeset
    87
lemma "r (| x := n |) (| y := m |) = r (| y := m |) (| x := n |)"
31d4a77c89d6 updated;
wenzelm
parents: 9297
diff changeset
    88
  -- "introduction of abstract record equality"
31d4a77c89d6 updated;
wenzelm
parents: 9297
diff changeset
    89
  by simp
31d4a77c89d6 updated;
wenzelm
parents: 9297
diff changeset
    90
31d4a77c89d6 updated;
wenzelm
parents: 9297
diff changeset
    91
lemma "r (| x := n |) = r (| x := n' |) ==> n = n'"
31d4a77c89d6 updated;
wenzelm
parents: 9297
diff changeset
    92
  -- "elimination of abstract record equality (manual proof)"
31d4a77c89d6 updated;
wenzelm
parents: 9297
diff changeset
    93
proof -
31d4a77c89d6 updated;
wenzelm
parents: 9297
diff changeset
    94
  assume "r (| x := n |) = r (| x := n' |)" (is "?lhs = ?rhs")
31d4a77c89d6 updated;
wenzelm
parents: 9297
diff changeset
    95
  hence "x ?lhs = x ?rhs" by simp
31d4a77c89d6 updated;
wenzelm
parents: 9297
diff changeset
    96
  thus ?thesis by simp
31d4a77c89d6 updated;
wenzelm
parents: 9297
diff changeset
    97
qed
31d4a77c89d6 updated;
wenzelm
parents: 9297
diff changeset
    98
31d4a77c89d6 updated;
wenzelm
parents: 9297
diff changeset
    99
31d4a77c89d6 updated;
wenzelm
parents: 9297
diff changeset
   100
text {* Surjective pairing *}
31d4a77c89d6 updated;
wenzelm
parents: 9297
diff changeset
   101
31d4a77c89d6 updated;
wenzelm
parents: 9297
diff changeset
   102
lemma "r = (| x = x r, y = y r |)"
31d4a77c89d6 updated;
wenzelm
parents: 9297
diff changeset
   103
  by simp
31d4a77c89d6 updated;
wenzelm
parents: 9297
diff changeset
   104
31d4a77c89d6 updated;
wenzelm
parents: 9297
diff changeset
   105
lemma "r = (| x = x r, y = y r, ... = more r |)"
31d4a77c89d6 updated;
wenzelm
parents: 9297
diff changeset
   106
  by simp
31d4a77c89d6 updated;
wenzelm
parents: 9297
diff changeset
   107
31d4a77c89d6 updated;
wenzelm
parents: 9297
diff changeset
   108
9151
wenzelm
parents: 9059
diff changeset
   109
text {* Splitting quantifiers: the !!r is NECESSARY here *}
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   110
9059
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
   111
lemma "!!r. r (| x := n |) (| y := m |) = r (| y := m |) (| x := n |)"
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
   112
proof record_split
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
   113
  fix x y more
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
   114
  show "(| x = x, y = y, ... = more |)(| x := n, y := m |) =
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
   115
        (| x = x, y = y, ... = more |)(| y := m, x := n |)"
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
   116
    by simp
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
   117
qed
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   118
9059
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
   119
lemma "!!r. r (| x := n |) (| x := m |) = r (| x := m |)"
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
   120
proof record_split
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
   121
  fix x y more
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
   122
  show "(| x = x, y = y, ... = more |)(| x := n, x := m |) =
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
   123
        (| x = x, y = y, ... = more |)(| x := m |)"
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
   124
    by simp
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
   125
qed
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   126
139a25a1e01e Example for records
narasche
parents:
diff changeset
   127
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   128
9151
wenzelm
parents: 9059
diff changeset
   129
text {* Concrete records are type instances of record schemes *}
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   130
139a25a1e01e Example for records
narasche
parents:
diff changeset
   131
constdefs
139a25a1e01e Example for records
narasche
parents:
diff changeset
   132
  foo5 :: nat
9059
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
   133
  "foo5 == getX (| x = 1, y = 0 |)"
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   134
139a25a1e01e Example for records
narasche
parents:
diff changeset
   135
9151
wenzelm
parents: 9059
diff changeset
   136
text {* Manipulating the `...' (more) part *}
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   137
139a25a1e01e Example for records
narasche
parents:
diff changeset
   138
constdefs
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   139
  incX :: "('a::more) point_scheme => 'a point_scheme"
9059
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
   140
  "incX r == (| x = Suc (x r), y = y r, ... = point.more r |)"
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   141
9059
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
   142
lemma "!!r n. incX r = setX r (Suc (getX r))"
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
   143
proof (unfold getX_def setX_def incX_def)
9151
wenzelm
parents: 9059
diff changeset
   144
  show "!!r n. (| x = Suc (x r), y = y r, ... = more r |) = r(| x := Suc (x r) |)"
9059
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
   145
    by record_split simp
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
   146
qed
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   147
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   148
9059
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
   149
text {* alternative definition *}
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   150
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   151
constdefs
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   152
  incX' :: "('a::more) point_scheme => 'a point_scheme"
9059
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
   153
  "incX' r == r (| x := Suc (x r) |)"
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   154
139a25a1e01e Example for records
narasche
parents:
diff changeset
   155
9151
wenzelm
parents: 9059
diff changeset
   156
subsection {* Coloured points: record extension *}
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   157
9059
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
   158
datatype colour = Red | Green | Blue
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   159
139a25a1e01e Example for records
narasche
parents:
diff changeset
   160
record cpoint = point +
9059
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
   161
  colour :: colour
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   162
139a25a1e01e Example for records
narasche
parents:
diff changeset
   163
6744
9727e83f0578 changed {| |} verbatim syntax to {* *};
wenzelm
parents: 6737
diff changeset
   164
text {*
5753
wenzelm
parents: 5741
diff changeset
   165
  The record declaration defines new type constructors:
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   166
5753
wenzelm
parents: 5741
diff changeset
   167
    cpoint = (| x :: nat, y :: nat, colour :: colour |)
wenzelm
parents: 5741
diff changeset
   168
    'a cpoint_scheme = (| x :: nat, y :: nat, colour :: colour, ... :: 'a |)
9059
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
   169
*}
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   170
9059
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
   171
consts foo6 :: cpoint
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
   172
consts foo7 :: "(| x :: nat, y :: nat, colour :: colour |)"
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
   173
consts foo8 :: "('a::more) cpoint_scheme"
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
   174
consts foo9 :: "(| x :: nat, y :: nat, colour :: colour, ... :: 'a |)"
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   175
139a25a1e01e Example for records
narasche
parents:
diff changeset
   176
9151
wenzelm
parents: 9059
diff changeset
   177
text {* Functions on point schemes work for cpoints as well *}
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   178
5753
wenzelm
parents: 5741
diff changeset
   179
constdefs
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   180
  foo10 :: nat
9059
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
   181
  "foo10 == getX (| x = 2, y = 0, colour = Blue |)"
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   182
139a25a1e01e Example for records
narasche
parents:
diff changeset
   183
9151
wenzelm
parents: 9059
diff changeset
   184
subsubsection {* Non-coercive structural subtyping *}
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   185
9059
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
   186
text {* foo11 has type cpoint, not type point --- Great! *}
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   187
5753
wenzelm
parents: 5741
diff changeset
   188
constdefs
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   189
  foo11 :: cpoint
9059
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
   190
  "foo11 == setX (| x = 2, y = 0, colour = Blue |) 0"
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   191
139a25a1e01e Example for records
narasche
parents:
diff changeset
   192
9151
wenzelm
parents: 9059
diff changeset
   193
subsection {* Other features *}
5753
wenzelm
parents: 5741
diff changeset
   194
9059
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
   195
text {* field names contribute to record identity *}
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   196
139a25a1e01e Example for records
narasche
parents:
diff changeset
   197
record point' =
139a25a1e01e Example for records
narasche
parents:
diff changeset
   198
  x' :: nat
9059
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
   199
  y' :: nat
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   200
9151
wenzelm
parents: 9059
diff changeset
   201
text {* May not apply @{term getX} to @{term "(| x' = 2, y' = 0 |)"} *}
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   202
139a25a1e01e Example for records
narasche
parents:
diff changeset
   203
9151
wenzelm
parents: 9059
diff changeset
   204
text {* Polymorphic records *}
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   205
139a25a1e01e Example for records
narasche
parents:
diff changeset
   206
record 'a point'' = point +
9059
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
   207
  content :: 'a
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   208
9059
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
   209
types cpoint'' = "colour point''"
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   210
9059
2b537d9e6f49 updated;
wenzelm
parents: 7742
diff changeset
   211
end