4 Author: Norbert Schirmer, TU Muenchen |
4 Author: Norbert Schirmer, TU Muenchen |
5 Author: Thomas Sewell, NICTA |
5 Author: Thomas Sewell, NICTA |
6 Author: Florian Haftmann, TU Muenchen |
6 Author: Florian Haftmann, TU Muenchen |
7 *) |
7 *) |
8 |
8 |
9 section {* Extensible records with structural subtyping *} |
9 section \<open>Extensible records with structural subtyping\<close> |
10 |
10 |
11 theory Record |
11 theory Record |
12 imports Quickcheck_Exhaustive |
12 imports Quickcheck_Exhaustive |
13 keywords "record" :: thy_decl |
13 keywords "record" :: thy_decl |
14 begin |
14 begin |
15 |
15 |
16 subsection {* Introduction *} |
16 subsection \<open>Introduction\<close> |
17 |
17 |
18 text {* |
18 text \<open> |
19 Records are isomorphic to compound tuple types. To implement |
19 Records are isomorphic to compound tuple types. To implement |
20 efficient records, we make this isomorphism explicit. Consider the |
20 efficient records, we make this isomorphism explicit. Consider the |
21 record access/update simplification @{text "alpha (beta_update f |
21 record access/update simplification @{text "alpha (beta_update f |
22 rec) = alpha rec"} for distinct fields alpha and beta of some record |
22 rec) = alpha rec"} for distinct fields alpha and beta of some record |
23 rec with n fields. There are @{text "n ^ 2"} such theorems, which |
23 rec with n fields. There are @{text "n ^ 2"} such theorems, which |
73 possible pattern matches. The rule set is used as it is viewed |
73 possible pattern matches. The rule set is used as it is viewed |
74 outside the locale, with the locale assumption (that the isomorphism |
74 outside the locale, with the locale assumption (that the isomorphism |
75 is valid) left as a rule assumption. All rules are structured to aid |
75 is valid) left as a rule assumption. All rules are structured to aid |
76 net matching, using either a point-free form or an encapsulating |
76 net matching, using either a point-free form or an encapsulating |
77 predicate. |
77 predicate. |
78 *} |
78 \<close> |
79 |
79 |
80 subsection {* Operators and lemmas for types isomorphic to tuples *} |
80 subsection \<open>Operators and lemmas for types isomorphic to tuples\<close> |
81 |
81 |
82 datatype (dead 'a, dead 'b, dead 'c) tuple_isomorphism = |
82 datatype (dead 'a, dead 'b, dead 'c) tuple_isomorphism = |
83 Tuple_Isomorphism "'a \<Rightarrow> 'b \<times> 'c" "'b \<times> 'c \<Rightarrow> 'a" |
83 Tuple_Isomorphism "'a \<Rightarrow> 'b \<times> 'c" "'b \<times> 'c \<Rightarrow> 'a" |
84 |
84 |
85 primrec |
85 primrec |
112 iso_tuple_cons :: |
112 iso_tuple_cons :: |
113 "('a, 'b, 'c) tuple_isomorphism \<Rightarrow> 'b \<Rightarrow> 'c \<Rightarrow> 'a" where |
113 "('a, 'b, 'c) tuple_isomorphism \<Rightarrow> 'b \<Rightarrow> 'c \<Rightarrow> 'a" where |
114 "iso_tuple_cons isom = curry (abst isom)" |
114 "iso_tuple_cons isom = curry (abst isom)" |
115 |
115 |
116 |
116 |
117 subsection {* Logical infrastructure for records *} |
117 subsection \<open>Logical infrastructure for records\<close> |
118 |
118 |
119 definition |
119 definition |
120 iso_tuple_surjective_proof_assist :: "'a \<Rightarrow> 'b \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> bool" where |
120 iso_tuple_surjective_proof_assist :: "'a \<Rightarrow> 'b \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> bool" where |
121 "iso_tuple_surjective_proof_assist x y f \<longleftrightarrow> f x = y" |
121 "iso_tuple_surjective_proof_assist x y f \<longleftrightarrow> f x = y" |
122 |
122 |
450 "_record" :: "fields => 'a" ("(3\<lparr>_\<rparr>)") |
450 "_record" :: "fields => 'a" ("(3\<lparr>_\<rparr>)") |
451 "_record_scheme" :: "fields => 'a => 'a" ("(3\<lparr>_,/ (2\<dots> =/ _)\<rparr>)") |
451 "_record_scheme" :: "fields => 'a => 'a" ("(3\<lparr>_,/ (2\<dots> =/ _)\<rparr>)") |
452 "_record_update" :: "'a => field_updates => 'b" ("_/(3\<lparr>_\<rparr>)" [900, 0] 900) |
452 "_record_update" :: "'a => field_updates => 'b" ("_/(3\<lparr>_\<rparr>)" [900, 0] 900) |
453 |
453 |
454 |
454 |
455 subsection {* Record package *} |
455 subsection \<open>Record package\<close> |
456 |
456 |
457 ML_file "Tools/record.ML" |
457 ML_file "Tools/record.ML" |
458 |
458 |
459 hide_const (open) Tuple_Isomorphism repr abst iso_tuple_fst iso_tuple_snd |
459 hide_const (open) Tuple_Isomorphism repr abst iso_tuple_fst iso_tuple_snd |
460 iso_tuple_fst_update iso_tuple_snd_update iso_tuple_cons |
460 iso_tuple_fst_update iso_tuple_snd_update iso_tuple_cons |