src/HOL/Examples/Records.thy
author wenzelm
Mon, 12 Apr 2021 14:14:47 +0200
changeset 73563 55b66a45bc94
parent 72030 eece87547736
child 76039 ca7737249aa4
permissions -rw-r--r--
tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
     1
(*  Title:      HOL/Examples/Records.thy
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
     2
    Author:     Wolfgang Naraschewski, TU Muenchen
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
     3
    Author:     Norbert Schirmer, TU Muenchen
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
     4
    Author:     Markus Wenzel, TU Muenchen
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
     5
*)
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
     6
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
     7
section \<open>Using extensible records in HOL -- points and coloured points\<close>
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
     8
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
     9
theory Records
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
    10
  imports Main
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    11
begin
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    12
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    13
subsection \<open>Points\<close>
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    14
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    15
record point =
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    16
  xpos :: nat
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    17
  ypos :: nat
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    18
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    19
text \<open>
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    20
  Apart many other things, above record declaration produces the
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    21
  following theorems:
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    22
\<close>
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    23
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    24
thm point.simps
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    25
thm point.iffs
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    26
thm point.defs
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    27
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    28
text \<open>
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    29
  The set of theorems @{thm [source] point.simps} is added
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    30
  automatically to the standard simpset, @{thm [source] point.iffs} is
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    31
  added to the Classical Reasoner and Simplifier context.
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    32
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
    33
  \<^medskip> Record declarations define new types and type abbreviations:
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    34
  @{text [display]
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    35
\<open>point = \<lparr>xpos :: nat, ypos :: nat\<rparr> = () point_ext_type
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    36
'a point_scheme = \<lparr>xpos :: nat, ypos :: nat, ... :: 'a\<rparr>  = 'a point_ext_type\<close>}
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    37
\<close>
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    38
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
    39
consts foo2 :: "\<lparr>xpos :: nat, ypos :: nat\<rparr>"
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
    40
consts foo4 :: "'a \<Rightarrow> \<lparr>xpos :: nat, ypos :: nat, \<dots> :: 'a\<rparr>"
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    41
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    42
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    43
subsubsection \<open>Introducing concrete records and record schemes\<close>
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    44
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    45
definition foo1 :: point
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
    46
  where "foo1 = \<lparr>xpos = 1, ypos = 0\<rparr>"
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    47
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
    48
definition foo3 :: "'a \<Rightarrow> 'a point_scheme"
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
    49
  where "foo3 ext = \<lparr>xpos = 1, ypos = 0, \<dots> = ext\<rparr>"
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    50
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    51
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    52
subsubsection \<open>Record selection and record update\<close>
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    53
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
    54
definition getX :: "'a point_scheme \<Rightarrow> nat"
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    55
  where "getX r = xpos r"
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    56
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
    57
definition setX :: "'a point_scheme \<Rightarrow> nat \<Rightarrow> 'a point_scheme"
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
    58
  where "setX r n = r \<lparr>xpos := n\<rparr>"
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    59
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    60
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    61
subsubsection \<open>Some lemmas about records\<close>
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    62
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    63
text \<open>Basic simplifications.\<close>
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    64
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
    65
lemma "point.make n p = \<lparr>xpos = n, ypos = p\<rparr>"
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    66
  by (simp only: point.make_def)
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    67
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
    68
lemma "xpos \<lparr>xpos = m, ypos = n, \<dots> = p\<rparr> = m"
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    69
  by simp
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    70
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
    71
lemma "\<lparr>xpos = m, ypos = n, \<dots> = p\<rparr>\<lparr>xpos:= 0\<rparr> = \<lparr>xpos = 0, ypos = n, \<dots> = p\<rparr>"
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    72
  by simp
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    73
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    74
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
    75
text \<open>\<^medskip> Equality of records.\<close>
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    76
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
    77
lemma "n = n' \<Longrightarrow> p = p' \<Longrightarrow> \<lparr>xpos = n, ypos = p\<rparr> = \<lparr>xpos = n', ypos = p'\<rparr>"
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    78
  \<comment> \<open>introduction of concrete record equality\<close>
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    79
  by simp
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    80
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
    81
lemma "\<lparr>xpos = n, ypos = p\<rparr> = \<lparr>xpos = n', ypos = p'\<rparr> \<Longrightarrow> n = n'"
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    82
  \<comment> \<open>elimination of concrete record equality\<close>
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    83
  by simp
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    84
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
    85
lemma "r\<lparr>xpos := n\<rparr>\<lparr>ypos := m\<rparr> = r\<lparr>ypos := m\<rparr>\<lparr>xpos := n\<rparr>"
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    86
  \<comment> \<open>introduction of abstract record equality\<close>
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    87
  by simp
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    88
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
    89
lemma "r\<lparr>xpos := n\<rparr> = r\<lparr>xpos := n'\<rparr>" if "n = n'"
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    90
  \<comment> \<open>elimination of abstract record equality (manual proof)\<close>
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    91
proof -
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
    92
  let "?lhs = ?rhs" = ?thesis
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
    93
  from that have "xpos ?lhs = xpos ?rhs" by simp
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    94
  then show ?thesis by simp
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    95
qed
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    96
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    97
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
    98
text \<open>\<^medskip> Surjective pairing\<close>
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    99
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   100
lemma "r = \<lparr>xpos = xpos r, ypos = ypos r\<rparr>"
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   101
  by simp
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   102
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   103
lemma "r = \<lparr>xpos = xpos r, ypos = ypos r, \<dots> = point.more r\<rparr>"
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   104
  by simp
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   105
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   106
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   107
text \<open>\<^medskip> Representation of records by cases or (degenerate) induction.\<close>
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   108
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   109
lemma "r\<lparr>xpos := n\<rparr>\<lparr>ypos := m\<rparr> = r\<lparr>ypos := m\<rparr>\<lparr>xpos := n\<rparr>"
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   110
proof (cases r)
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   111
  fix xpos ypos more
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   112
  assume "r = \<lparr>xpos = xpos, ypos = ypos, \<dots> = more\<rparr>"
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   113
  then show ?thesis by simp
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   114
qed
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   115
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   116
lemma "r\<lparr>xpos := n\<rparr>\<lparr>ypos := m\<rparr> = r\<lparr>ypos := m\<rparr>\<lparr>xpos := n\<rparr>"
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   117
proof (induct r)
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   118
  fix xpos ypos more
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   119
  show "\<lparr>xpos = xpos, ypos = ypos, \<dots> = more\<rparr>\<lparr>xpos := n, ypos := m\<rparr> =
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   120
      \<lparr>xpos = xpos, ypos = ypos, \<dots> = more\<rparr>\<lparr>ypos := m, xpos := n\<rparr>"
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   121
    by simp
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   122
qed
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   123
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   124
lemma "r\<lparr>xpos := n\<rparr>\<lparr>xpos := m\<rparr> = r\<lparr>xpos := m\<rparr>"
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   125
proof (cases r)
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   126
  fix xpos ypos more
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   127
  assume "r = \<lparr>xpos = xpos, ypos = ypos, \<dots> = more\<rparr>"
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   128
  then show ?thesis by simp
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   129
qed
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   130
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   131
lemma "r\<lparr>xpos := n\<rparr>\<lparr>xpos := m\<rparr> = r\<lparr>xpos := m\<rparr>"
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   132
proof (cases r)
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   133
  case fields
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   134
  then show ?thesis by simp
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   135
qed
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   136
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   137
lemma "r\<lparr>xpos := n\<rparr>\<lparr>xpos := m\<rparr> = r\<lparr>xpos := m\<rparr>"
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   138
  by (cases r) simp
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   139
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   140
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   141
text \<open>\<^medskip> Concrete records are type instances of record schemes.\<close>
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   142
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   143
definition foo5 :: nat
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   144
  where "foo5 = getX \<lparr>xpos = 1, ypos = 0\<rparr>"
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   145
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   146
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   147
text \<open>\<^medskip> Manipulating the ``\<open>...\<close>'' (more) part.\<close>
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   148
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   149
definition incX :: "'a point_scheme \<Rightarrow> 'a point_scheme"
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   150
  where "incX r = \<lparr>xpos = xpos r + 1, ypos = ypos r, \<dots> = point.more r\<rparr>"
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   151
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   152
lemma "incX r = setX r (Suc (getX r))"
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   153
  by (simp add: getX_def setX_def incX_def)
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   154
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   155
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   156
text \<open>\<^medskip> An alternative definition.\<close>
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   157
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   158
definition incX' :: "'a point_scheme \<Rightarrow> 'a point_scheme"
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   159
  where "incX' r = r\<lparr>xpos := xpos r + 1\<rparr>"
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   160
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   161
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   162
subsection \<open>Coloured points: record extension\<close>
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   163
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   164
datatype colour = Red | Green | Blue
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   165
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   166
record cpoint = point +
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   167
  colour :: colour
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   168
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   169
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   170
text \<open>
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   171
  The record declaration defines a new type constructor and abbreviations:
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   172
  @{text [display]
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   173
\<open>cpoint = \<lparr>xpos :: nat, ypos :: nat, colour :: colour\<rparr> =
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   174
  () cpoint_ext_type point_ext_type
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   175
'a cpoint_scheme = \<lparr>xpos :: nat, ypos :: nat, colour :: colour, \<dots> :: 'a\<rparr> =
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   176
  'a cpoint_ext_type point_ext_type\<close>}
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   177
\<close>
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   178
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   179
consts foo6 :: cpoint
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   180
consts foo7 :: "\<lparr>xpos :: nat, ypos :: nat, colour :: colour\<rparr>"
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   181
consts foo8 :: "'a cpoint_scheme"
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   182
consts foo9 :: "\<lparr>xpos :: nat, ypos :: nat, colour :: colour, \<dots> :: 'a\<rparr>"
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   183
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   184
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   185
text \<open>Functions on \<open>point\<close> schemes work for \<open>cpoints\<close> as well.\<close>
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   186
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   187
definition foo10 :: nat
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   188
  where "foo10 = getX \<lparr>xpos = 2, ypos = 0, colour = Blue\<rparr>"
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   189
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   190
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   191
subsubsection \<open>Non-coercive structural subtyping\<close>
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   192
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   193
text \<open>Term \<^term>\<open>foo11\<close> has type \<^typ>\<open>cpoint\<close>, not type \<^typ>\<open>point\<close> --- Great!\<close>
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   194
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   195
definition foo11 :: cpoint
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   196
  where "foo11 = setX \<lparr>xpos = 2, ypos = 0, colour = Blue\<rparr> 0"
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   197
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   198
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   199
subsection \<open>Other features\<close>
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   200
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   201
text \<open>Field names contribute to record identity.\<close>
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   202
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   203
record point' =
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   204
  xpos' :: nat
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   205
  ypos' :: nat
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   206
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   207
text \<open>
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   208
  \<^noindent> May not apply \<^term>\<open>getX\<close> to @{term [source] "\<lparr>xpos' = 2, ypos' = 0\<rparr>"}
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   209
  --- type error.
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   210
\<close>
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   211
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   212
text \<open>\<^medskip> Polymorphic records.\<close>
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   213
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   214
record 'a point'' = point +
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   215
  content :: 'a
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   216
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   217
type_synonym cpoint'' = "colour point''"
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   218
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   219
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   220
text \<open>Updating a record field with an identical value is simplified.\<close>
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   221
lemma "r\<lparr>xpos := xpos r\<rparr> = r"
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   222
  by simp
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   223
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   224
text \<open>Only the most recent update to a component survives simplification.\<close>
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   225
lemma "r\<lparr>xpos := x, ypos := y, xpos := x'\<rparr> = r\<lparr>ypos := y, xpos := x'\<rparr>"
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   226
  by simp
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   227
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   228
text \<open>
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   229
  In some cases its convenient to automatically split (quantified) records.
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   230
  For this purpose there is the simproc @{ML [source] "Record.split_simproc"}
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   231
  and the tactic @{ML [source] "Record.split_simp_tac"}. The simplification
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   232
  procedure only splits the records, whereas the tactic also simplifies the
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   233
  resulting goal with the standard record simplification rules. A
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   234
  (generalized) predicate on the record is passed as parameter that decides
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   235
  whether or how `deep' to split the record. It can peek on the subterm
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   236
  starting at the quantified occurrence of the record (including the
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   237
  quantifier). The value \<^ML>\<open>0\<close> indicates no split, a value greater
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   238
  \<^ML>\<open>0\<close> splits up to the given bound of record extension and finally the
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   239
  value \<^ML>\<open>~1\<close> completely splits the record. @{ML [source]
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   240
  "Record.split_simp_tac"} additionally takes a list of equations for
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   241
  simplification and can also split fixed record variables.
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   242
\<close>
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   243
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   244
lemma "(\<forall>r. P (xpos r)) \<longrightarrow> (\<forall>x. P x)"
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   245
  apply (tactic \<open>simp_tac (put_simpset HOL_basic_ss \<^context>
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   246
    addsimprocs [Record.split_simproc (K ~1)]) 1\<close>)
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   247
  apply simp
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   248
  done
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   249
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   250
lemma "(\<forall>r. P (xpos r)) \<longrightarrow> (\<forall>x. P x)"
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   251
  apply (tactic \<open>Record.split_simp_tac \<^context> [] (K ~1) 1\<close>)
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   252
  apply simp
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   253
  done
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   254
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   255
lemma "(\<exists>r. P (xpos r)) \<longrightarrow> (\<exists>x. P x)"
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   256
  apply (tactic \<open>simp_tac (put_simpset HOL_basic_ss \<^context>
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   257
    addsimprocs [Record.split_simproc (K ~1)]) 1\<close>)
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   258
  apply simp
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   259
  done
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   260
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   261
lemma "(\<exists>r. P (xpos r)) \<longrightarrow> (\<exists>x. P x)"
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   262
  apply (tactic \<open>Record.split_simp_tac \<^context> [] (K ~1) 1\<close>)
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   263
  apply simp
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   264
  done
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   265
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   266
lemma "\<And>r. P (xpos r) \<Longrightarrow> (\<exists>x. P x)"
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   267
  apply (tactic \<open>simp_tac (put_simpset HOL_basic_ss \<^context>
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   268
    addsimprocs [Record.split_simproc (K ~1)]) 1\<close>)
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   269
  apply auto
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   270
  done
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   271
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   272
lemma "\<And>r. P (xpos r) \<Longrightarrow> (\<exists>x. P x)"
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   273
  apply (tactic \<open>Record.split_simp_tac \<^context> [] (K ~1) 1\<close>)
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   274
  apply auto
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   275
  done
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   276
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   277
lemma "P (xpos r) \<Longrightarrow> (\<exists>x. P x)"
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   278
  apply (tactic \<open>Record.split_simp_tac \<^context> [] (K ~1) 1\<close>)
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   279
  apply auto
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   280
  done
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   281
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   282
notepad
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   283
begin
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   284
  have "\<exists>x. P x"
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   285
    if "P (xpos r)" for P r
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   286
    apply (insert that)
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   287
    apply (tactic \<open>Record.split_simp_tac \<^context> [] (K ~1) 1\<close>)
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   288
    apply auto
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   289
    done
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   290
end
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   291
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   292
text \<open>
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   293
  The effect of simproc @{ML [source] Record.ex_sel_eq_simproc} is illustrated
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   294
  by the following lemma.\<close>
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   295
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   296
lemma "\<exists>r. xpos r = x"
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   297
  by (tactic \<open>simp_tac (put_simpset HOL_basic_ss \<^context>
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   298
    addsimprocs [Record.ex_sel_eq_simproc]) 1\<close>)
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   299
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   300
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   301
subsection \<open>A more complex record expression\<close>
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   302
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   303
record ('a, 'b, 'c) bar = bar1 :: 'a
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   304
  bar2 :: 'b
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   305
  bar3 :: 'c
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   306
  bar21 :: "'b \<times> 'a"
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   307
  bar32 :: "'c \<times> 'b"
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   308
  bar31 :: "'c \<times> 'a"
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   309
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   310
print_record "('a,'b,'c) bar"
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   311
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   312
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   313
subsection \<open>Some code generation\<close>
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   314
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   315
export_code foo1 foo3 foo5 foo10 checking SML
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   316
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   317
text \<open>
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   318
  Code generation can also be switched off, for instance for very large
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   319
  records:\<close>
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   320
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   321
declare [[record_codegen = false]]
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   322
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   323
record not_so_large_record =
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   324
  bar520 :: nat
72030
eece87547736 misc tuning and modernization;
wenzelm
parents: 72029
diff changeset
   325
  bar521 :: "nat \<times> nat"
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   326
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   327
declare [[record_codegen = true]]
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   328
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
   329
end