src/HOL/ex/Records.thy
author wenzelm
Tue, 03 Mar 2009 14:07:43 +0100
changeset 30211 556d1810cdad
parent 26932 c398a3866082
child 31248 d1c65a593daf
permissions -rw-r--r--
Thm.binding;
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
25707
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   239
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   240
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   241
text {* Updating a record field with an identical value is simplified.*}
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   242
lemma "r (| xpos := xpos r |) = r"
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   243
  by simp
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   244
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   245
text {* Only the most recent update to a component survives simplification. *}
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   246
lemma "r (| xpos := x, ypos := y, xpos := x' |) = r (| ypos := y, xpos := x' |)"
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   247
  by simp
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   248
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   249
text {* In some cases its convenient to automatically split
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   250
(quantified) records. For this purpose there is the simproc @{ML [source]
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   251
"RecordPackage.record_split_simproc"} and the tactic @{ML [source]
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   252
"RecordPackage.record_split_simp_tac"}.  The simplification procedure
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   253
only splits the records, whereas the tactic also simplifies the
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   254
resulting goal with the standard record simplification rules. A
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   255
(generalized) predicate on the record is passed as parameter that
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   256
decides whether or how `deep' to split the record. It can peek on the
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   257
subterm starting at the quantified occurrence of the record (including
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   258
the quantifier). The value @{ML "0"} indicates no split, a value
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   259
greater @{ML "0"} splits up to the given bound of record extension and
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   260
finally the value @{ML "~1"} completely splits the record.
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   261
@{ML [source] "RecordPackage.record_split_simp_tac"} additionally takes a list of
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   262
equations for simplification and can also split fixed record variables.
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   263
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   264
*}
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   265
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   266
lemma "(\<forall>r. P (xpos r)) \<longrightarrow> (\<forall>x. P x)"
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   267
  apply (tactic {* simp_tac
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   268
          (HOL_basic_ss addsimprocs [RecordPackage.record_split_simproc (K ~1)]) 1*})
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   269
  apply simp
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   270
  done
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   271
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   272
lemma "(\<forall>r. P (xpos r)) \<longrightarrow> (\<forall>x. P x)"
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   273
  apply (tactic {* RecordPackage.record_split_simp_tac [] (K ~1) 1*})
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   274
  apply simp
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   275
  done
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   276
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   277
lemma "(\<exists>r. P (xpos r)) \<longrightarrow> (\<exists>x. P x)"
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   278
  apply (tactic {* simp_tac
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   279
          (HOL_basic_ss addsimprocs [RecordPackage.record_split_simproc (K ~1)]) 1*})
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   280
  apply simp
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   281
  done
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   282
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   283
lemma "(\<exists>r. P (xpos r)) \<longrightarrow> (\<exists>x. P x)"
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   284
  apply (tactic {* RecordPackage.record_split_simp_tac [] (K ~1) 1*})
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   285
  apply simp
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   286
  done
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   287
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   288
lemma "\<And>r. P (xpos r) \<Longrightarrow> (\<exists>x. P x)"
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   289
  apply (tactic {* simp_tac
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   290
          (HOL_basic_ss addsimprocs [RecordPackage.record_split_simproc (K ~1)]) 1*})
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   291
  apply auto
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   292
  done
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   293
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   294
lemma "\<And>r. P (xpos r) \<Longrightarrow> (\<exists>x. P x)"
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   295
  apply (tactic {* RecordPackage.record_split_simp_tac [] (K ~1) 1*})
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   296
  apply auto
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   297
  done
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   298
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   299
lemma "P (xpos r) \<Longrightarrow> (\<exists>x. P x)"
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   300
  apply (tactic {* RecordPackage.record_split_simp_tac [] (K ~1) 1*})
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   301
  apply auto
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   302
  done
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   303
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   304
lemma fixes r shows "P (xpos r) \<Longrightarrow> (\<exists>x. P x)"
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   305
  apply (tactic {* RecordPackage.record_split_simp_tac [] (K ~1) 1*})
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   306
  apply auto
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   307
  done
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   308
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   309
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   310
lemma True
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   311
proof -
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   312
  {
26932
c398a3866082 avoid undeclared variables within proofs;
wenzelm
parents: 25707
diff changeset
   313
    fix P r
25707
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   314
    assume pre: "P (xpos r)"
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   315
    have "\<exists>x. P x"
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   316
      using pre
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   317
      apply -
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   318
      apply (tactic {* RecordPackage.record_split_simp_tac [] (K ~1) 1*})
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   319
      apply auto 
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   320
      done
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   321
  }
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   322
  show ?thesis ..
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   323
qed
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   324
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   325
text {* The effect of simproc @{ML [source]
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   326
"RecordPackage.record_ex_sel_eq_simproc"} is illustrated by the
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   327
following lemma.  
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   328
*}
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   329
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   330
lemma "\<exists>r. xpos r = x"
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   331
  apply (tactic {*simp_tac 
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   332
           (HOL_basic_ss addsimprocs [RecordPackage.record_ex_sel_eq_simproc]) 1*})
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   333
  done
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   334
0a599404f5a1 more examples
schirmer
parents: 22737
diff changeset
   335
10052
5fa8d8d5c852 renamed HOL/ex/Points to HOL/ex/Records;
wenzelm
parents:
diff changeset
   336
end