doc-src/TutorialI/Types/Records.thy
changeset 11711 ecdfd237ffee
parent 11428 332347b9b942
child 11942 06fac365248d
equal deleted inserted replaced
11710:f5401162c9f0 11711:ecdfd237ffee
    50 unfortunately write the fields in a canonical order.*}
    50 unfortunately write the fields in a canonical order.*}
    51 
    51 
    52 
    52 
    53 constdefs 
    53 constdefs 
    54   pt1 :: point
    54   pt1 :: point
    55    "pt1 == (| Xcoord = #999, Ycoord = #23 |)"
    55    "pt1 == (| Xcoord = 999, Ycoord = 23 |)"
    56 
    56 
    57   pt2 :: "(| Xcoord :: int, Ycoord :: int |)" 
    57   pt2 :: "(| Xcoord :: int, Ycoord :: int |)" 
    58    "pt2 == (| Xcoord = #-45, Ycoord = #97 |)" 
    58    "pt2 == (| Xcoord = -45, Ycoord = 97 |)" 
    59 
    59 
    60 
    60 
    61 subsubsection {* Some lemmas about records *}
    61 subsubsection {* Some lemmas about records *}
    62 
    62 
    63 text {* Basic simplifications. *}
    63 text {* Basic simplifications. *}
    87   col :: colour
    87   col :: colour
    88 
    88 
    89 
    89 
    90 constdefs 
    90 constdefs 
    91   cpt1 :: cpoint
    91   cpt1 :: cpoint
    92    "cpt1 == (| Xcoord = #999, Ycoord = #23, col = Green |)"
    92    "cpt1 == (| Xcoord = 999, Ycoord = 23, col = Green |)"
    93 
    93 
    94 
    94 
    95 subsection {* Generic operations *}
    95 subsection {* Generic operations *}
    96 
    96 
    97 
    97 
   119   getX :: "('a::more) point_scheme \<Rightarrow> int"
   119   getX :: "('a::more) point_scheme \<Rightarrow> int"
   120    "getX r == Xcoord r"
   120    "getX r == Xcoord r"
   121   setX :: "[('a::more) point_scheme, int] \<Rightarrow> 'a point_scheme"
   121   setX :: "[('a::more) point_scheme, int] \<Rightarrow> 'a point_scheme"
   122    "setX r a == r (| Xcoord := a |)"
   122    "setX r a == r (| Xcoord := a |)"
   123   extendpt :: "'a \<Rightarrow> ('a::more) point_scheme"
   123   extendpt :: "'a \<Rightarrow> ('a::more) point_scheme"
   124    "extendpt ext == (| Xcoord = #15, Ycoord = 0, ... = ext |)"
   124    "extendpt ext == (| Xcoord = 15, Ycoord = 0, ... = ext |)"
   125      text{*not sure what this is for!*}
   125      text{*not sure what this is for!*}
   126 
   126 
   127 
   127 
   128 text {*
   128 text {*
   129  \medskip Concrete records are type instances of record schemes.
   129  \medskip Concrete records are type instances of record schemes.
   130 *}
   130 *}
   131 
   131 
   132 lemma "getX (| Xcoord = #64, Ycoord = #36 |) = #64"
   132 lemma "getX (| Xcoord = 64, Ycoord = 36 |) = 64"
   133 by (simp add: getX_def) 
   133 by (simp add: getX_def) 
   134 
   134 
   135 
   135 
   136 text {* \medskip Manipulating the `...' (more) part.  beware: EACH record
   136 text {* \medskip Manipulating the `...' (more) part.  beware: EACH record
   137    has its OWN more field, so a compound name is required! *}
   137    has its OWN more field, so a compound name is required! *}
   138 
   138 
   139 constdefs
   139 constdefs
   140   incX :: "('a::more) point_scheme \<Rightarrow> 'a point_scheme"
   140   incX :: "('a::more) point_scheme \<Rightarrow> 'a point_scheme"
   141   "incX r == \<lparr>Xcoord = (Xcoord r) + #1, Ycoord = Ycoord r, \<dots> = point.more r\<rparr>"
   141   "incX r == \<lparr>Xcoord = (Xcoord r) + 1, Ycoord = Ycoord r, \<dots> = point.more r\<rparr>"
   142 
   142 
   143 constdefs
   143 constdefs
   144   setGreen :: "[colour, ('a::more) point_scheme] \<Rightarrow> cpoint"
   144   setGreen :: "[colour, ('a::more) point_scheme] \<Rightarrow> cpoint"
   145   "setGreen cl r == (| Xcoord = Xcoord r, Ycoord = Ycoord r, col = cl |)"
   145   "setGreen cl r == (| Xcoord = Xcoord r, Ycoord = Ycoord r, col = cl |)"
   146 
   146 
   147 
   147 
   148 text {* works (I think) for ALL extensions of type point? *}
   148 text {* works (I think) for ALL extensions of type point? *}
   149 
   149 
   150 lemma "incX r = setX r ((getX r) + #1)"
   150 lemma "incX r = setX r ((getX r) + 1)"
   151 by (simp add: getX_def setX_def incX_def)
   151 by (simp add: getX_def setX_def incX_def)
   152 
   152 
   153 text {* An equivalent definition. *}
   153 text {* An equivalent definition. *}
   154 lemma "incX r = r \<lparr>Xcoord := (Xcoord r) + #1\<rparr>"
   154 lemma "incX r = r \<lparr>Xcoord := (Xcoord r) + 1\<rparr>"
   155 by (simp add: incX_def)
   155 by (simp add: incX_def)
   156 
   156 
   157 
   157 
   158 
   158 
   159 text {*
   159 text {*
   160  Functions on @{text point} schemes work for type @{text cpoint} as
   160  Functions on @{text point} schemes work for type @{text cpoint} as
   161  well.  *}
   161  well.  *}
   162 
   162 
   163 lemma "getX \<lparr>Xcoord = #23, Ycoord = #10, col = Blue\<rparr> = #23"
   163 lemma "getX \<lparr>Xcoord = 23, Ycoord = 10, col = Blue\<rparr> = 23"
   164 by (simp add: getX_def)
   164 by (simp add: getX_def)
   165 
   165 
   166 
   166 
   167 subsubsection {* Non-coercive structural subtyping *}
   167 subsubsection {* Non-coercive structural subtyping *}
   168 
   168 
   169 text {*
   169 text {*
   170  Function @{term setX} can be applied to type @{typ cpoint}, not just type
   170  Function @{term setX} can be applied to type @{typ cpoint}, not just type
   171  @{typ point}, and returns a result of the same type!  *}
   171  @{typ point}, and returns a result of the same type!  *}
   172 
   172 
   173 lemma "setX \<lparr>Xcoord = #12, Ycoord = 0, col = Blue\<rparr> #23 =  
   173 lemma "setX \<lparr>Xcoord = 12, Ycoord = 0, col = Blue\<rparr> 23 =  
   174             \<lparr>Xcoord = #23, Ycoord = 0, col = Blue\<rparr>"
   174             \<lparr>Xcoord = 23, Ycoord = 0, col = Blue\<rparr>"
   175 by (simp add: setX_def)
   175 by (simp add: setX_def)
   176 
   176 
   177 
   177 
   178 subsection {* Other features *}
   178 subsection {* Other features *}
   179 
   179