src/HOL/ex/Points.thy
author wenzelm
Wed, 26 May 1999 22:45:59 +0200
changeset 6737 03f0ff7ee029
parent 5753 c90b5f7d0c61
child 6744 9727e83f0578
permissions -rw-r--r--
ex/Points Isar'ized;
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
wenzelm
parents: 5741
diff changeset
     4
*)
wenzelm
parents: 5741
diff changeset
     5
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
     6
theory Points = Main:;
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
     7
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
     8
title "Basic use of extensible records in HOL, including the famous points
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
     9
 and coloured points.";
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
    10
139a25a1e01e Example for records
narasche
parents:
diff changeset
    11
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    12
section "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
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    16
  y :: nat;
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
    17
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    18
text {|
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    19
  Apart many other things, above record declaration produces the
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    20
  following theorems:
5753
wenzelm
parents: 5741
diff changeset
    21
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    22
    thms "point.simps"
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    23
    thms "point.iffs"
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    24
    thms "point.update_defs"
5753
wenzelm
parents: 5741
diff changeset
    25
wenzelm
parents: 5741
diff changeset
    26
  The set of theorems "point.simps" is added automatically to the
wenzelm
parents: 5741
diff changeset
    27
  standard simpset, "point.iffs" is added to the claset and simpset.
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    28
|};
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
    29
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    30
text {|
5753
wenzelm
parents: 5741
diff changeset
    31
  Record declarations define new type abbreviations:
wenzelm
parents: 5741
diff changeset
    32
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    33
    point = "(| x :: nat, y :: nat |)"
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    34
    'a point_scheme = "(| x :: nat, y :: nat, ... :: 'a |)"
5753
wenzelm
parents: 5741
diff changeset
    35
wenzelm
parents: 5741
diff changeset
    36
  Extensions `...' must be in type class `more'!
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    37
|};
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    38
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    39
consts foo1 :: point;
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    40
consts foo2 :: "(| x :: nat, y :: nat |)";
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    41
consts foo3 :: "'a => ('a::more) point_scheme";
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    42
consts foo4 :: "'a => (| x :: nat, y :: nat, ... :: 'a |)";
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    43
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    44
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    45
subsection "Introducing concrete records and record schemes";
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    46
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    47
defs
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    48
  foo1_def: "foo1 == (| x = 1, y = 0 |)"
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    49
  foo3_def: "foo3 ext == (| x = 1, y = 0, ... = ext |)";
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
    50
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    51
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    52
subsection "Record selection and record update";
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    53
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    54
constdefs
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    55
  getX :: "('a::more) point_scheme => nat"
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    56
  "getX r == x r"
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    57
  setX :: "('a::more) point_scheme => nat => 'a point_scheme"
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    58
  "setX r n == r (| x := n |)";
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    59
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    60
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    61
subsection "Some lemmas about records";
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    62
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    63
text "Note: any of these goals may be solved with a single
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    64
 stroke of auto or force.";
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
    65
139a25a1e01e Example for records
narasche
parents:
diff changeset
    66
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    67
text "basic simplifications";
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
    68
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    69
lemma "x (| x = m, y = n, ... = p |) = m";
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    70
  by simp;
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    71
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    72
lemma "(| x = m, y = n, ... = p |) (| x:= 0 |) = (| x = 0, y = n, ... = p |)";
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    73
  by simp;
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
    74
139a25a1e01e Example for records
narasche
parents:
diff changeset
    75
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    76
text "splitting quantifiers: the !!r is NECESSARY";
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
    77
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    78
lemma "!!r. r (| x := n |) (| y := m |) = r (| y := m |) (| x := n |)";
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    79
proof record_split;
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    80
  fix a b rest;
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    81
  show "(| x = a, y = b, ... = rest |)(| x := n, y := m |) =
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    82
        (| x = a, y = b, ... = rest |)(| y := m, x := n |)";
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    83
    by simp;
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    84
qed;
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    85
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    86
lemma "!!r. r (| x := n |) (| x := m |) = r (| x := m |)";
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    87
proof record_split;
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    88
  fix a b rest;
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    89
  show "(| x = a, y = b, ... = rest |)(| x := n, x := m |) =
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    90
        (| x = a, y = b, ... = rest |)(| x := m |)";
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    91
    by simp;
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    92
qed;
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
    93
139a25a1e01e Example for records
narasche
parents:
diff changeset
    94
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    95
text "equality of records";
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    96
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    97
lemma "(| x = n, y = p |) = (| x = n', y = p' |) --> n = n'" (is "??EQ --> _");
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    98
proof;
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
    99
  assume ??EQ;
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   100
  thus "n = n'"; by simp;
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   101
qed;
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   102
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   103
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   104
text "concrete records are type instances of record schemes";
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   105
139a25a1e01e Example for records
narasche
parents:
diff changeset
   106
constdefs
139a25a1e01e Example for records
narasche
parents:
diff changeset
   107
  foo5 :: nat
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   108
  "foo5 == getX (| x = 1, y = 0 |)";
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   109
139a25a1e01e Example for records
narasche
parents:
diff changeset
   110
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   111
text "manipulating the `...' (more) part";
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   112
139a25a1e01e Example for records
narasche
parents:
diff changeset
   113
constdefs
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   114
  incX :: "('a::more) point_scheme => 'a point_scheme"
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   115
  "incX r == (| x = Suc (x r), y = y r, ... = point.more r |)";
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   116
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   117
lemma "!!r n. incX r = setX r (Suc (getX r))";
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   118
proof (unfold getX_def setX_def incX_def);
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   119
  show "!! r n. (| x = Suc (x r), y = y r, ... = more r |) = r(| x := Suc (x r) |)";
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   120
    by (record_split, simp);
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   121
qed;
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   122
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   123
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   124
text "alternative definition";
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   125
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   126
constdefs
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   127
  incX' :: "('a::more) point_scheme => 'a point_scheme"
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   128
  "incX' r == r (| x := Suc (x r) |)";
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   129
139a25a1e01e Example for records
narasche
parents:
diff changeset
   130
139a25a1e01e Example for records
narasche
parents:
diff changeset
   131
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   132
section "coloured points: record extension";
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   133
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   134
datatype colour = Red | Green | Blue;
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   135
139a25a1e01e Example for records
narasche
parents:
diff changeset
   136
record cpoint = point +
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   137
  colour :: colour;
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   138
139a25a1e01e Example for records
narasche
parents:
diff changeset
   139
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   140
text {|
5753
wenzelm
parents: 5741
diff changeset
   141
  The record declaration defines new type constructors:
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   142
5753
wenzelm
parents: 5741
diff changeset
   143
    cpoint = (| x :: nat, y :: nat, colour :: colour |)
wenzelm
parents: 5741
diff changeset
   144
    'a cpoint_scheme = (| x :: nat, y :: nat, colour :: colour, ... :: 'a |)
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   145
|};
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   146
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   147
consts foo6 :: cpoint;
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   148
consts foo7 :: "(| x :: nat, y :: nat, colour :: colour |)";
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   149
consts foo8 :: "('a::more) cpoint_scheme";
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   150
consts foo9 :: "(| x :: nat, y :: nat, colour :: colour, ... :: 'a |)";
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   151
139a25a1e01e Example for records
narasche
parents:
diff changeset
   152
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   153
text "functions on point schemes work for cpoints as well";
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   154
5753
wenzelm
parents: 5741
diff changeset
   155
constdefs
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   156
  foo10 :: nat
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   157
  "foo10 == getX (| x = 2, y = 0, colour = Blue |)";
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   158
139a25a1e01e Example for records
narasche
parents:
diff changeset
   159
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   160
subsection "Non-coercive structural subtyping";
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   161
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   162
text "foo11 has type cpoint, not type point --- Great!";
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   163
5753
wenzelm
parents: 5741
diff changeset
   164
constdefs
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   165
  foo11 :: cpoint
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   166
  "foo11 == setX (| x = 2, y = 0, colour = Blue |) 0";
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   167
139a25a1e01e Example for records
narasche
parents:
diff changeset
   168
139a25a1e01e Example for records
narasche
parents:
diff changeset
   169
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   170
section "Other features";
5753
wenzelm
parents: 5741
diff changeset
   171
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   172
text "field names contribute to record identity";
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   173
139a25a1e01e Example for records
narasche
parents:
diff changeset
   174
record point' =
139a25a1e01e Example for records
narasche
parents:
diff changeset
   175
  x' :: nat
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   176
  y' :: nat;
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   177
139a25a1e01e Example for records
narasche
parents:
diff changeset
   178
consts
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   179
  foo12 :: nat;
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   180
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   181
text {| Definition @{term "foo12 == getX (| x' = 2, y' = 0 |)"} is invalid |};
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   182
139a25a1e01e Example for records
narasche
parents:
diff changeset
   183
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   184
text "polymorphic records";
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   185
139a25a1e01e Example for records
narasche
parents:
diff changeset
   186
record 'a point'' = point +
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   187
  content :: 'a;
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   188
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   189
types cpoint'' = "colour point''";
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   190
139a25a1e01e Example for records
narasche
parents:
diff changeset
   191
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   192
text "Have a lot of fun!";
5741
139a25a1e01e Example for records
narasche
parents:
diff changeset
   193
6737
03f0ff7ee029 ex/Points Isar'ized;
wenzelm
parents: 5753
diff changeset
   194
end;