author | wenzelm |
Tue, 07 Aug 2001 22:37:30 +0200 | |
changeset 11473 | 4546d8d39221 |
parent 10008 | 61eb9f3aa92a |
child 11490 | f9ae28f55178 |
permissions | -rw-r--r-- |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
1 |
(* Title: HOL/Tools/record_package.ML |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
2 |
ID: $Id$ |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
3 |
Author: Wolfgang Naraschewski and Markus Wenzel, TU Muenchen |
9230 | 4 |
License: GPL (GNU GENERAL PUBLIC LICENSE) |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
5 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
6 |
Extensible records with structural subtyping in HOL. |
5698 | 7 |
*) |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
8 |
|
5698 | 9 |
signature BASIC_RECORD_PACKAGE = |
10 |
sig |
|
7178 | 11 |
val record_simproc: simproc |
5698 | 12 |
val record_split_tac: int -> tactic |
5713 | 13 |
val record_split_name: string |
5698 | 14 |
val record_split_wrapper: string * wrapper |
15 |
end; |
|
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
16 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
17 |
signature RECORD_PACKAGE = |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
18 |
sig |
5698 | 19 |
include BASIC_RECORD_PACKAGE |
20 |
val quiet_mode: bool ref |
|
8574 | 21 |
val updateN: string |
4890 | 22 |
val moreS: sort |
23 |
val mk_fieldT: (string * typ) * typ -> typ |
|
24 |
val dest_fieldT: typ -> (string * typ) * typ |
|
25 |
val mk_field: (string * term) * term -> term |
|
26 |
val mk_fst: term -> term |
|
27 |
val mk_snd: term -> term |
|
28 |
val mk_recordT: (string * typ) list * typ -> typ |
|
29 |
val dest_recordT: typ -> (string * typ) list * typ |
|
30 |
val mk_record: (string * term) list * term -> term |
|
31 |
val mk_sel: term -> string -> term |
|
32 |
val mk_update: term -> string * term -> term |
|
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
33 |
val print_records: theory -> unit |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
34 |
val add_record: (string list * bstring) -> string option |
6519 | 35 |
-> (bstring * string) list -> theory -> theory * {simps: thm list, iffs: thm list} |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
36 |
val add_record_i: (string list * bstring) -> (typ list * string) option |
6519 | 37 |
-> (bstring * typ) list -> theory -> theory * {simps: thm list, iffs: thm list} |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
38 |
val setup: (theory -> theory) list |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
39 |
end; |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
40 |
|
7178 | 41 |
|
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
42 |
structure RecordPackage: RECORD_PACKAGE = |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
43 |
struct |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
44 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
45 |
|
4894 | 46 |
(*** utilities ***) |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
47 |
|
5698 | 48 |
(* messages *) |
49 |
||
50 |
val quiet_mode = ref false; |
|
51 |
fun message s = if ! quiet_mode then () else writeln s; |
|
52 |
||
53 |
||
4894 | 54 |
(* definitions and equations *) |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
55 |
|
5698 | 56 |
infix 0 :== ===; |
4894 | 57 |
|
58 |
val (op :==) = Logic.mk_defpair; |
|
59 |
val (op ===) = HOLogic.mk_Trueprop o HOLogic.mk_eq; |
|
60 |
||
61 |
||
62 |
(* proof by simplification *) |
|
4890 | 63 |
|
7178 | 64 |
fun prove_simp sign ss tacs simps = |
4894 | 65 |
let |
7178 | 66 |
val ss' = Simplifier.addsimps (ss, simps); |
4895 | 67 |
|
68 |
fun prove goal = |
|
6092 | 69 |
Goals.prove_goalw_cterm [] (Thm.cterm_of sign goal) |
9626 | 70 |
(K (tacs @ [ALLGOALS (Simplifier.asm_full_simp_tac ss')])) |
6092 | 71 |
handle ERROR => error ("The error(s) above occurred while trying to prove " |
72 |
^ quote (Sign.string_of_term sign goal)); |
|
4894 | 73 |
in prove end; |
74 |
||
75 |
||
76 |
||
77 |
(*** syntax operations ***) |
|
78 |
||
79 |
(** name components **) |
|
80 |
||
8294 | 81 |
val recordN = "record"; |
4894 | 82 |
val moreN = "more"; |
83 |
val schemeN = "_scheme"; |
|
84 |
val fieldN = "_field"; |
|
5698 | 85 |
val raw_fieldN = "_raw_field"; |
4894 | 86 |
val field_typeN = "_field_type"; |
5698 | 87 |
val fstN = "_val"; |
88 |
val sndN = "_more"; |
|
4894 | 89 |
val updateN = "_update"; |
90 |
val makeN = "make"; |
|
91 |
val make_schemeN = "make_scheme"; |
|
4890 | 92 |
|
5698 | 93 |
(*see datatype package*) |
94 |
val caseN = "_case"; |
|
95 |
||
96 |
||
97 |
||
98 |
(** generic operations **) |
|
99 |
||
5713 | 100 |
(* adhoc priming of vars *) |
101 |
||
102 |
fun prime (Free (x, T)) = Free (x ^ "'", T) |
|
103 |
| prime t = raise TERM ("prime: no free variable", [t]); |
|
104 |
||
105 |
||
106 |
(* product case *) |
|
107 |
||
5698 | 108 |
fun fst_fn T U = Abs ("x", T, Abs ("y", U, Bound 1)); |
109 |
fun snd_fn T U = Abs ("x", T, Abs ("y", U, Bound 0)); |
|
110 |
||
111 |
fun mk_prod_case name f p = |
|
112 |
let |
|
113 |
val fT as Type ("fun", [A, Type ("fun", [B, C])]) = fastype_of f; |
|
114 |
val pT = fastype_of p; |
|
115 |
in Const (suffix caseN name, fT --> pT --> C) $ f $ p end; |
|
116 |
||
4890 | 117 |
|
118 |
||
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
119 |
(** tuple operations **) |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
120 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
121 |
(* more type class *) |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
122 |
|
5210 | 123 |
val moreS = ["Record.more"]; |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
124 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
125 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
126 |
(* types *) |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
127 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
128 |
fun mk_fieldT ((c, T), U) = Type (suffix field_typeN c, [T, U]); |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
129 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
130 |
fun dest_fieldT (typ as Type (c_field_type, [T, U])) = |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
131 |
(case try (unsuffix field_typeN) c_field_type of |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
132 |
None => raise TYPE ("dest_fieldT", [typ], []) |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
133 |
| Some c => ((c, T), U)) |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
134 |
| dest_fieldT typ = raise TYPE ("dest_fieldT", [typ], []); |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
135 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
136 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
137 |
(* constructors *) |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
138 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
139 |
fun mk_fieldC U (c, T) = (suffix fieldN c, T --> U --> mk_fieldT ((c, T), U)); |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
140 |
|
5698 | 141 |
fun gen_mk_field sfx ((c, t), u) = |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
142 |
let val T = fastype_of t and U = fastype_of u |
5698 | 143 |
in Const (suffix sfx c, [T, U] ---> mk_fieldT ((c, T), U)) $ t $ u end; |
144 |
||
145 |
val mk_field = gen_mk_field fieldN; |
|
146 |
val mk_raw_field = gen_mk_field raw_fieldN; |
|
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
147 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
148 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
149 |
(* destructors *) |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
150 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
151 |
fun mk_fstC U (c, T) = (suffix fstN c, mk_fieldT ((c, T), U) --> T); |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
152 |
fun mk_sndC U (c, T) = (suffix sndN c, mk_fieldT ((c, T), U) --> U); |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
153 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
154 |
fun dest_field fst_or_snd p = |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
155 |
let |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
156 |
val pT = fastype_of p; |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
157 |
val ((c, T), U) = dest_fieldT pT; |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
158 |
val (destN, destT) = if fst_or_snd then (fstN, T) else (sndN, U); |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
159 |
in Const (suffix destN c, pT --> destT) $ p end; |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
160 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
161 |
val mk_fst = dest_field true; |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
162 |
val mk_snd = dest_field false; |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
163 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
164 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
165 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
166 |
(** record operations **) |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
167 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
168 |
(* types *) |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
169 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
170 |
val mk_recordT = foldr mk_fieldT; |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
171 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
172 |
fun dest_recordT T = |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
173 |
(case try dest_fieldT T of |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
174 |
None => ([], T) |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
175 |
| Some (c_T, U) => apfst (cons c_T) (dest_recordT U)); |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
176 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
177 |
fun find_fieldT c rT = |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
178 |
(case assoc (fst (dest_recordT rT), c) of |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
179 |
None => raise TYPE ("find_field: " ^ c, [rT], []) |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
180 |
| Some T => T); |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
181 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
182 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
183 |
(* constructors *) |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
184 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
185 |
val mk_record = foldr mk_field; |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
186 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
187 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
188 |
(* selectors *) |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
189 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
190 |
fun mk_selC rT (c, T) = (c, rT --> T); |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
191 |
|
4890 | 192 |
fun mk_sel r c = |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
193 |
let val rT = fastype_of r |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
194 |
in Const (mk_selC rT (c, find_fieldT c rT)) $ r end; |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
195 |
|
4894 | 196 |
val mk_moreC = mk_selC; |
197 |
||
198 |
fun mk_more r c = |
|
199 |
let val rT = fastype_of r |
|
200 |
in Const (mk_moreC rT (c, snd (dest_recordT rT))) $ r end; |
|
201 |
||
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
202 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
203 |
(* updates *) |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
204 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
205 |
fun mk_updateC rT (c, T) = (suffix updateN c, T --> rT --> rT); |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
206 |
|
4890 | 207 |
fun mk_update r (c, x) = |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
208 |
let val rT = fastype_of r |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
209 |
in Const (mk_updateC rT (c, find_fieldT c rT)) $ x $ r end; |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
210 |
|
5197 | 211 |
val mk_more_updateC = mk_updateC; |
212 |
||
213 |
fun mk_more_update r (c, x) = |
|
214 |
let val rT = fastype_of r |
|
215 |
in Const (mk_more_updateC rT (c, snd (dest_recordT rT))) $ x $ r end; |
|
216 |
||
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
217 |
|
4890 | 218 |
(* make *) |
219 |
||
220 |
fun mk_makeC rT (c, Ts) = (c, Ts ---> rT); |
|
221 |
||
222 |
||
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
223 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
224 |
(** concrete syntax for records **) |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
225 |
|
11473
4546d8d39221
fix problem with user translations by making field names appear as consts;
wenzelm
parents:
10008
diff
changeset
|
226 |
(* parse ast translations *) |
4546d8d39221
fix problem with user translations by making field names appear as consts;
wenzelm
parents:
10008
diff
changeset
|
227 |
|
4546d8d39221
fix problem with user translations by making field names appear as consts;
wenzelm
parents:
10008
diff
changeset
|
228 |
fun field_name_ast_tr [Syntax.Variable c] = Syntax.Constant c |
4546d8d39221
fix problem with user translations by making field names appear as consts;
wenzelm
parents:
10008
diff
changeset
|
229 |
| field_name_ast_tr asts = raise Syntax.AST ("field_name_ast_tr", asts); |
4546d8d39221
fix problem with user translations by making field names appear as consts;
wenzelm
parents:
10008
diff
changeset
|
230 |
|
4546d8d39221
fix problem with user translations by making field names appear as consts;
wenzelm
parents:
10008
diff
changeset
|
231 |
|
4546d8d39221
fix problem with user translations by making field names appear as consts;
wenzelm
parents:
10008
diff
changeset
|
232 |
val parse_ast_translation = |
4546d8d39221
fix problem with user translations by making field names appear as consts;
wenzelm
parents:
10008
diff
changeset
|
233 |
[("_field_name", field_name_ast_tr)]; |
4546d8d39221
fix problem with user translations by making field names appear as consts;
wenzelm
parents:
10008
diff
changeset
|
234 |
|
4546d8d39221
fix problem with user translations by making field names appear as consts;
wenzelm
parents:
10008
diff
changeset
|
235 |
|
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
236 |
(* parse translations *) |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
237 |
|
11473
4546d8d39221
fix problem with user translations by making field names appear as consts;
wenzelm
parents:
10008
diff
changeset
|
238 |
fun gen_field_tr mark sfx (t as Const (c, _) $ Const (name, _) $ arg) = |
5197 | 239 |
if c = mark then Syntax.const (suffix sfx name) $ arg |
240 |
else raise TERM ("gen_field_tr: " ^ mark, [t]) |
|
241 |
| gen_field_tr mark _ t = raise TERM ("gen_field_tr: " ^ mark, [t]); |
|
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
242 |
|
5197 | 243 |
fun gen_fields_tr sep mark sfx (tm as Const (c, _) $ t $ u) = |
244 |
if c = sep then gen_field_tr mark sfx t :: gen_fields_tr sep mark sfx u |
|
5201 | 245 |
else [gen_field_tr mark sfx tm] |
246 |
| gen_fields_tr _ mark sfx tm = [gen_field_tr mark sfx tm]; |
|
5197 | 247 |
|
248 |
fun gen_record_tr sep mark sfx unit [t] = foldr (op $) (gen_fields_tr sep mark sfx t, unit) |
|
5201 | 249 |
| gen_record_tr _ _ _ _ ts = raise TERM ("gen_record_tr", ts); |
5197 | 250 |
|
251 |
fun gen_record_scheme_tr sep mark sfx [t, more] = foldr (op $) (gen_fields_tr sep mark sfx t, more) |
|
5201 | 252 |
| gen_record_scheme_tr _ _ _ ts = raise TERM ("gen_record_scheme_tr", ts); |
5197 | 253 |
|
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
254 |
|
5197 | 255 |
val record_type_tr = gen_record_tr "_field_types" "_field_type" field_typeN (Syntax.const "unit"); |
256 |
val record_type_scheme_tr = gen_record_scheme_tr "_field_types" "_field_type" field_typeN; |
|
257 |
||
258 |
val record_tr = gen_record_tr "_fields" "_field" fieldN HOLogic.unit; |
|
259 |
val record_scheme_tr = gen_record_scheme_tr "_fields" "_field" fieldN; |
|
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
260 |
|
5197 | 261 |
fun record_update_tr [t, u] = |
262 |
foldr (op $) (rev (gen_fields_tr "_updates" "_update" updateN u), t) |
|
263 |
| record_update_tr ts = raise TERM ("record_update_tr", ts); |
|
264 |
||
265 |
||
266 |
val parse_translation = |
|
267 |
[("_record_type", record_type_tr), |
|
268 |
("_record_type_scheme", record_type_scheme_tr), |
|
269 |
("_record", record_tr), |
|
270 |
("_record_scheme", record_scheme_tr), |
|
271 |
("_record_update", record_update_tr)]; |
|
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
272 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
273 |
|
4890 | 274 |
(* print translations *) |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
275 |
|
5197 | 276 |
fun gen_fields_tr' mark sfx (tm as Const (name_field, _) $ t $ u) = |
277 |
(case try (unsuffix sfx) name_field of |
|
278 |
Some name => |
|
279 |
apfst (cons (Syntax.const mark $ Syntax.free name $ t)) (gen_fields_tr' mark sfx u) |
|
280 |
| None => ([], tm)) |
|
281 |
| gen_fields_tr' _ _ tm = ([], tm); |
|
282 |
||
283 |
fun gen_record_tr' sep mark sfx is_unit record record_scheme tm = |
|
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
284 |
let |
5197 | 285 |
val (ts, u) = gen_fields_tr' mark sfx tm; |
286 |
val t' = foldr1 (fn (v, w) => Syntax.const sep $ v $ w) ts; |
|
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
287 |
in |
5197 | 288 |
if is_unit u then Syntax.const record $ t' |
289 |
else Syntax.const record_scheme $ t' $ u |
|
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
290 |
end; |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
291 |
|
5197 | 292 |
|
293 |
val record_type_tr' = |
|
294 |
gen_record_tr' "_field_types" "_field_type" field_typeN |
|
295 |
(fn Const ("unit", _) => true | _ => false) "_record_type" "_record_type_scheme"; |
|
296 |
||
297 |
val record_tr' = |
|
298 |
gen_record_tr' "_fields" "_field" fieldN HOLogic.is_unit "_record" "_record_scheme"; |
|
299 |
||
300 |
fun record_update_tr' tm = |
|
301 |
let val (ts, u) = gen_fields_tr' "_update" updateN tm in |
|
302 |
Syntax.const "_record_update" $ u $ |
|
303 |
foldr1 (fn (v, w) => Syntax.const "_updates" $ v $ w) (rev ts) |
|
304 |
end; |
|
305 |
||
306 |
||
5201 | 307 |
fun gen_field_tr' sfx tr' name = |
308 |
let val name_sfx = suffix sfx name |
|
309 |
in (name_sfx, fn [t, u] => tr' (Syntax.const name_sfx $ t $ u) | _ => raise Match) end; |
|
310 |
||
5197 | 311 |
fun print_translation names = |
312 |
map (gen_field_tr' field_typeN record_type_tr') names @ |
|
313 |
map (gen_field_tr' fieldN record_tr') names @ |
|
314 |
map (gen_field_tr' updateN record_update_tr') names; |
|
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
315 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
316 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
317 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
318 |
(*** extend theory by record definition ***) |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
319 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
320 |
(** record info **) |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
321 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
322 |
(* type record_info and parent_info *) |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
323 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
324 |
type record_info = |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
325 |
{args: (string * sort) list, |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
326 |
parent: (typ list * string) option, |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
327 |
fields: (string * typ) list, |
6092 | 328 |
simps: thm list}; |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
329 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
330 |
type parent_info = |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
331 |
{name: string, |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
332 |
fields: (string * typ) list, |
6092 | 333 |
simps: thm list}; |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
334 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
335 |
|
5052 | 336 |
(* data kind 'HOL/records' *) |
5001 | 337 |
|
7178 | 338 |
type record_data = |
339 |
{records: record_info Symtab.table, |
|
340 |
sel_upd: |
|
341 |
{selectors: unit Symtab.table, |
|
342 |
updates: string Symtab.table, |
|
343 |
simpset: Simplifier.simpset}, |
|
344 |
field_splits: |
|
345 |
{fields: unit Symtab.table, |
|
346 |
simpset: Simplifier.simpset}}; |
|
347 |
||
348 |
fun make_record_data records sel_upd field_splits = |
|
349 |
{records = records, sel_upd = sel_upd, field_splits = field_splits}: record_data; |
|
350 |
||
5006 | 351 |
structure RecordsArgs = |
352 |
struct |
|
353 |
val name = "HOL/records"; |
|
7178 | 354 |
type T = record_data; |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
355 |
|
7178 | 356 |
val empty = |
357 |
make_record_data Symtab.empty |
|
358 |
{selectors = Symtab.empty, updates = Symtab.empty, simpset = HOL_basic_ss} |
|
359 |
{fields = Symtab.empty, simpset = HOL_basic_ss}; |
|
360 |
||
6556 | 361 |
val copy = I; |
5006 | 362 |
val prep_ext = I; |
7178 | 363 |
fun merge |
364 |
({records = recs1, |
|
365 |
sel_upd = {selectors = sels1, updates = upds1, simpset = ss1}, |
|
366 |
field_splits = {fields = flds1, simpset = fld_ss1}}, |
|
367 |
{records = recs2, |
|
368 |
sel_upd = {selectors = sels2, updates = upds2, simpset = ss2}, |
|
369 |
field_splits = {fields = flds2, simpset = fld_ss2}}) = |
|
370 |
make_record_data |
|
371 |
(Symtab.merge (K true) (recs1, recs2)) |
|
372 |
{selectors = Symtab.merge (K true) (sels1, sels2), |
|
373 |
updates = Symtab.merge (K true) (upds1, upds2), |
|
374 |
simpset = Simplifier.merge_ss (ss1, ss2)} |
|
375 |
{fields = Symtab.merge (K true) (flds1, flds2), |
|
376 |
simpset = Simplifier.merge_ss (fld_ss1, fld_ss2)}; |
|
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
377 |
|
7178 | 378 |
fun print sg ({records = recs, ...}: record_data) = |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
379 |
let |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
380 |
val prt_typ = Sign.pretty_typ sg; |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
381 |
val ext_const = Sign.cond_extern sg Sign.constK; |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
382 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
383 |
fun pretty_parent None = [] |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
384 |
| pretty_parent (Some (Ts, name)) = |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
385 |
[Pretty.block [prt_typ (Type (name, Ts)), Pretty.str " +"]]; |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
386 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
387 |
fun pretty_field (c, T) = Pretty.block |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
388 |
[Pretty.str (ext_const c), Pretty.str " ::", Pretty.brk 1, Pretty.quote (prt_typ T)]; |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
389 |
|
4895 | 390 |
fun pretty_record (name, {args, parent, fields, simps = _}) = Pretty.block (Pretty.fbreaks |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
391 |
(Pretty.block [prt_typ (Type (name, map TFree args)), Pretty.str " = "] :: |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
392 |
pretty_parent parent @ map pretty_field fields)); |
8720 | 393 |
in |
394 |
map pretty_record (Sign.cond_extern_table sg Sign.typeK recs) |
|
395 |
|> Pretty.chunks |> Pretty.writeln |
|
396 |
end; |
|
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
397 |
end; |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
398 |
|
5006 | 399 |
structure RecordsData = TheoryDataFun(RecordsArgs); |
400 |
val print_records = RecordsData.print; |
|
401 |
||
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
402 |
|
7178 | 403 |
(* access 'records' *) |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
404 |
|
7178 | 405 |
fun get_record thy name = Symtab.lookup (#records (RecordsData.get thy), name); |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
406 |
|
4890 | 407 |
fun put_record name info thy = |
7178 | 408 |
let |
409 |
val {records, sel_upd, field_splits} = RecordsData.get thy; |
|
410 |
val data = make_record_data (Symtab.update ((name, info), records)) sel_upd field_splits; |
|
411 |
in RecordsData.put data thy end; |
|
412 |
||
413 |
||
414 |
(* access 'sel_upd' *) |
|
415 |
||
416 |
fun get_sel_upd sg = #sel_upd (RecordsData.get_sg sg); |
|
417 |
||
418 |
fun get_selectors sg name = Symtab.lookup (#selectors (get_sel_upd sg), name); |
|
419 |
fun get_updates sg name = Symtab.lookup (#updates (get_sel_upd sg), name); |
|
420 |
fun get_simpset sg = #simpset (get_sel_upd sg); |
|
421 |
||
422 |
||
423 |
fun put_sel_upd names simps thy = |
|
424 |
let |
|
425 |
val sels = map (rpair ()) names; |
|
426 |
val upds = map (suffix updateN) names ~~ names; |
|
427 |
||
428 |
val {records, sel_upd = {selectors, updates, simpset}, field_splits} = RecordsData.get thy; |
|
429 |
val data = make_record_data records |
|
430 |
{selectors = Symtab.extend (selectors, sels), |
|
431 |
updates = Symtab.extend (updates, upds), |
|
432 |
simpset = Simplifier.addsimps (simpset, simps)} |
|
433 |
field_splits; |
|
434 |
in RecordsData.put data thy end; |
|
435 |
||
436 |
||
437 |
(* access 'field_splits' *) |
|
5698 | 438 |
|
439 |
fun add_record_splits splits thy = |
|
440 |
let |
|
7178 | 441 |
val {records, sel_upd, field_splits = {fields, simpset}} = RecordsData.get thy; |
442 |
val flds = map (rpair () o fst) splits; |
|
443 |
val simps = map snd splits; |
|
444 |
val data = make_record_data records sel_upd |
|
445 |
{fields = Symtab.extend (fields, flds), simpset = Simplifier.addsimps (simpset, simps)}; |
|
446 |
in RecordsData.put data thy end; |
|
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
447 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
448 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
449 |
(* parent records *) |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
450 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
451 |
fun inst_record thy (types, name) = |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
452 |
let |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
453 |
val sign = Theory.sign_of thy; |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
454 |
fun err msg = error (msg ^ " parent record " ^ quote name); |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
455 |
|
4895 | 456 |
val {args, parent, fields, simps} = |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
457 |
(case get_record thy name of Some info => info | None => err "Unknown"); |
4895 | 458 |
val _ = if length types <> length args then err "Bad number of arguments for" else (); |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
459 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
460 |
fun bad_inst ((x, S), T) = |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
461 |
if Sign.of_sort sign (T, S) then None else Some x |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
462 |
val bads = mapfilter bad_inst (args ~~ types); |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
463 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
464 |
val inst = map fst args ~~ types; |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
465 |
val subst = Term.map_type_tfree (fn (x, _) => the (assoc (inst, x))); |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
466 |
in |
4895 | 467 |
if not (null bads) then |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
468 |
err ("Ill-sorted instantiation of " ^ commas bads ^ " in") |
4895 | 469 |
else (apsome (apfst (map subst)) parent, map (apsnd subst) fields, simps) |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
470 |
end; |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
471 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
472 |
fun add_parents thy (None, parents) = parents |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
473 |
| add_parents thy (Some (types, name), parents) = |
4895 | 474 |
let val (pparent, pfields, psimps) = inst_record thy (types, name) |
475 |
in add_parents thy (pparent, {name = name, fields = pfields, simps = psimps} :: parents) end; |
|
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
476 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
477 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
478 |
|
7178 | 479 |
(** record simproc **) |
480 |
||
481 |
local |
|
482 |
||
8100 | 483 |
val sel_upd_pat = [Thm.read_cterm (Theory.sign_of HOL.thy) ("s (u k r)", HOLogic.termT)]; |
7178 | 484 |
|
485 |
fun proc sg _ t = |
|
486 |
(case t of (sel as Const (s, _)) $ ((upd as Const (u, _)) $ k $ r) => |
|
487 |
(case get_selectors sg s of Some () => |
|
488 |
(case get_updates sg u of Some u_name => |
|
489 |
let |
|
490 |
fun atomize x t = Free (x, fastype_of t); |
|
491 |
val k' = atomize "k" k; |
|
492 |
val r' = atomize "r" r; |
|
493 |
val t' = sel $ (upd $ k' $ r'); |
|
494 |
val prove = mk_meta_eq o prove_simp sg (get_simpset sg) [] []; |
|
495 |
in |
|
496 |
if u_name = s then Some (prove (t' === k')) |
|
497 |
else Some (prove (t' === sel $ r')) |
|
498 |
end |
|
499 |
| None => None) |
|
500 |
| None => None) |
|
501 |
| _ => None); |
|
502 |
||
503 |
in |
|
504 |
||
505 |
val record_simproc = Simplifier.mk_simproc "record_simp" sel_upd_pat proc; |
|
506 |
||
507 |
end; |
|
508 |
||
509 |
||
510 |
||
5698 | 511 |
(** record field splitting **) |
512 |
||
6358 | 513 |
(* tactic *) |
514 |
||
5698 | 515 |
fun record_split_tac i st = |
516 |
let |
|
7178 | 517 |
val {field_splits = {fields, simpset}, ...} = RecordsData.get_sg (Thm.sign_of_thm st); |
5698 | 518 |
|
7178 | 519 |
fun is_fieldT (_, Type (a, [_, _])) = is_some (Symtab.lookup (fields, a)) |
5698 | 520 |
| is_fieldT _ = false; |
521 |
val params = Logic.strip_params (Library.nth_elem (i - 1, Thm.prems_of st)); |
|
522 |
in |
|
7178 | 523 |
if exists is_fieldT params then Simplifier.full_simp_tac simpset i st |
5698 | 524 |
else Seq.empty |
525 |
end handle Library.LIST _ => Seq.empty; |
|
526 |
||
6358 | 527 |
|
528 |
(* wrapper *) |
|
529 |
||
5707 | 530 |
val record_split_name = "record_split_tac"; |
531 |
val record_split_wrapper = (record_split_name, fn tac => record_split_tac ORELSE' tac); |
|
5698 | 532 |
|
533 |
||
6358 | 534 |
(* method *) |
535 |
||
536 |
val record_split_method = |
|
9705 | 537 |
("record_split", Method.no_args (Method.SIMPLE_METHOD' HEADGOAL record_split_tac), |
6358 | 538 |
"split record fields"); |
539 |
||
540 |
||
5698 | 541 |
|
4890 | 542 |
(** internal theory extenders **) |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
543 |
|
5698 | 544 |
(* field_type_defs *) |
545 |
||
5713 | 546 |
fun field_type_def ((thy, simps), (name, tname, vs, T, U)) = |
5698 | 547 |
let |
6394 | 548 |
val full = Sign.full_name (Theory.sign_of thy); |
5713 | 549 |
val (thy', {simps = simps', ...}) = |
5698 | 550 |
thy |
551 |
|> setmp DatatypePackage.quiet_mode true |
|
552 |
(DatatypePackage.add_datatype_i true [tname] |
|
553 |
[(vs, tname, Syntax.NoSyn, [(name, [T, U], Syntax.NoSyn)])]); |
|
554 |
val thy'' = |
|
555 |
thy' |
|
556 |
|> setmp AxClass.quiet_mode true |
|
557 |
(AxClass.add_inst_arity_i (full tname, [HOLogic.termS, moreS], moreS) [] [] None); |
|
5713 | 558 |
in (thy'', simps' @ simps) end; |
5698 | 559 |
|
5713 | 560 |
fun field_type_defs args thy = foldl field_type_def ((thy, []), args); |
5698 | 561 |
|
562 |
||
4894 | 563 |
(* field_definitions *) |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
564 |
|
9040 | 565 |
fun field_definitions fields names xs zeta moreT more vars named_vars thy = |
4890 | 566 |
let |
5698 | 567 |
val sign = Theory.sign_of thy; |
4890 | 568 |
val base = Sign.base_name; |
5698 | 569 |
val full_path = Sign.full_name_path sign; |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
570 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
571 |
|
4890 | 572 |
(* prepare declarations and definitions *) |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
573 |
|
4890 | 574 |
(*field types*) |
575 |
fun mk_fieldT_spec c = |
|
5698 | 576 |
(suffix raw_fieldN c, suffix field_typeN c, |
577 |
["'a", zeta], TFree ("'a", HOLogic.termS), moreT); |
|
4890 | 578 |
val fieldT_specs = map (mk_fieldT_spec o base) names; |
579 |
||
5698 | 580 |
(*field constructors*) |
4890 | 581 |
val field_decls = map (mk_fieldC moreT) fields; |
582 |
||
583 |
fun mk_field_spec (c, v) = |
|
5698 | 584 |
mk_field ((c, v), more) :== mk_raw_field ((c, v), more); |
4894 | 585 |
val field_specs = map mk_field_spec named_vars; |
4890 | 586 |
|
587 |
(*field destructors*) |
|
5698 | 588 |
val dest_decls = map (mk_fstC moreT) fields @ map (mk_sndC moreT) fields; |
589 |
||
590 |
fun mk_dest_spec dest f (c, T) = |
|
591 |
let val p = Free ("p", mk_fieldT ((c, T), moreT)); |
|
592 |
in dest p :== mk_prod_case (suffix field_typeN c) (f T moreT) p end; |
|
4890 | 593 |
val dest_specs = |
5698 | 594 |
map (mk_dest_spec mk_fst fst_fn) fields @ |
595 |
map (mk_dest_spec mk_snd snd_fn) fields; |
|
4890 | 596 |
|
597 |
||
598 |
(* prepare theorems *) |
|
4894 | 599 |
|
5713 | 600 |
(*constructor injects*) |
601 |
fun mk_inject_prop (c, v) = |
|
602 |
HOLogic.mk_eq (mk_field ((c, v), more), mk_field ((c, prime v), prime more)) === |
|
603 |
(HOLogic.conj $ HOLogic.mk_eq (v, prime v) $ HOLogic.mk_eq (more, prime more)); |
|
604 |
val inject_props = map mk_inject_prop named_vars; |
|
605 |
||
5698 | 606 |
(*destructor conversions*) |
4890 | 607 |
fun mk_dest_prop dest dest' (c, v) = |
4894 | 608 |
dest (mk_field ((c, v), more)) === dest' (v, more); |
4890 | 609 |
val dest_props = |
4895 | 610 |
map (mk_dest_prop mk_fst fst) named_vars @ |
611 |
map (mk_dest_prop mk_snd snd) named_vars; |
|
4890 | 612 |
|
5698 | 613 |
(*surjective pairing*) |
614 |
fun mk_surj_prop (c, T) = |
|
615 |
let val p = Free ("p", mk_fieldT ((c, T), moreT)); |
|
616 |
in p === mk_field ((c, mk_fst p), mk_snd p) end; |
|
617 |
val surj_props = map mk_surj_prop fields; |
|
4890 | 618 |
|
5698 | 619 |
|
620 |
(* 1st stage: types_thy *) |
|
621 |
||
6092 | 622 |
val (types_thy, datatype_simps) = |
5698 | 623 |
thy |
624 |
|> field_type_defs fieldT_specs; |
|
625 |
||
626 |
||
627 |
(* 2nd stage: defs_thy *) |
|
4890 | 628 |
|
8428 | 629 |
val (defs_thy, (field_defs, dest_defs)) = |
5698 | 630 |
types_thy |
8428 | 631 |
|> (Theory.add_consts_i o map (Syntax.no_syn o apfst base)) (field_decls @ dest_decls) |
9315 | 632 |
|> (PureThy.add_defs_i false o map (fn x => (x, [Drule.tag_internal]))) field_specs |
633 |
|>>> (PureThy.add_defs_i false o map (fn x => (x, [Drule.tag_internal]))) dest_specs; |
|
4890 | 634 |
|
635 |
||
5698 | 636 |
(* 3rd stage: thms_thy *) |
637 |
||
7178 | 638 |
val prove = prove_simp (Theory.sign_of defs_thy) HOL_basic_ss; |
5713 | 639 |
val prove_std = prove [] (field_defs @ dest_defs @ datatype_simps); |
4890 | 640 |
|
5713 | 641 |
val field_injects = map prove_std inject_props; |
642 |
val dest_convs = map prove_std dest_props; |
|
5698 | 643 |
val surj_pairs = map (prove [DatatypePackage.induct_tac "p" 1] |
6092 | 644 |
(map Thm.symmetric field_defs @ dest_convs)) surj_props; |
5698 | 645 |
|
9040 | 646 |
fun mk_split (x, th) = SplitPairedAll.rule_params x moreN (th RS eq_reflection); |
647 |
val field_splits = map2 mk_split (xs, surj_pairs); |
|
4894 | 648 |
|
4890 | 649 |
val thms_thy = |
650 |
defs_thy |
|
8428 | 651 |
|> (#1 oo (PureThy.add_thmss o map Thm.no_attributes)) |
4890 | 652 |
[("field_defs", field_defs), |
653 |
("dest_defs", dest_defs), |
|
5698 | 654 |
("dest_convs", dest_convs), |
655 |
("surj_pairs", surj_pairs), |
|
5713 | 656 |
("field_splits", field_splits)]; |
4890 | 657 |
|
5713 | 658 |
in (thms_thy, dest_convs, field_injects, field_splits) end; |
4890 | 659 |
|
660 |
||
661 |
(* record_definition *) |
|
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
662 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
663 |
fun record_definition (args, bname) parent (parents: parent_info list) bfields thy = |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
664 |
let |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
665 |
val sign = Theory.sign_of thy; |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
666 |
val full = Sign.full_name_path sign bname; |
4890 | 667 |
val base = Sign.base_name; |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
668 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
669 |
|
4890 | 670 |
(* basic components *) |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
671 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
672 |
val alphas = map fst args; |
5197 | 673 |
val name = Sign.full_name sign bname; (*not made part of record name space!*) |
4890 | 674 |
|
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
675 |
val parent_fields = flat (map #fields parents); |
4890 | 676 |
val parent_names = map fst parent_fields; |
677 |
val parent_types = map snd parent_fields; |
|
678 |
val parent_len = length parent_fields; |
|
8294 | 679 |
val parent_xs = variantlist (map (base o fst) parent_fields, [moreN, recordN]); |
4890 | 680 |
val parent_vars = ListPair.map Free (parent_xs, parent_types); |
4894 | 681 |
val parent_named_vars = parent_names ~~ parent_vars; |
4890 | 682 |
|
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
683 |
val fields = map (apfst full) bfields; |
4890 | 684 |
val names = map fst fields; |
685 |
val types = map snd fields; |
|
686 |
val len = length fields; |
|
8294 | 687 |
val xs = variantlist (map fst bfields, moreN :: recordN :: parent_xs); |
4890 | 688 |
val vars = ListPair.map Free (xs, types); |
4894 | 689 |
val named_vars = names ~~ vars; |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
690 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
691 |
val all_fields = parent_fields @ fields; |
4890 | 692 |
val all_names = parent_names @ names; |
693 |
val all_types = parent_types @ types; |
|
694 |
val all_len = parent_len + len; |
|
695 |
val all_xs = parent_xs @ xs; |
|
696 |
val all_vars = parent_vars @ vars; |
|
4894 | 697 |
val all_named_vars = parent_named_vars @ named_vars; |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
698 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
699 |
val zeta = variant alphas "'z"; |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
700 |
val moreT = TFree (zeta, moreS); |
4895 | 701 |
val more = Free (moreN, moreT); |
5197 | 702 |
val full_moreN = full moreN; |
703 |
fun more_part t = mk_more t full_moreN; |
|
704 |
fun more_part_update t x = mk_more_update t (full_moreN, x); |
|
4894 | 705 |
|
706 |
val parent_more = funpow parent_len mk_snd; |
|
707 |
val idxs = 0 upto (len - 1); |
|
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
708 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
709 |
val rec_schemeT = mk_recordT (all_fields, moreT); |
4894 | 710 |
val rec_scheme = mk_record (all_named_vars, more); |
8294 | 711 |
val r = Free (recordN, rec_schemeT); |
4894 | 712 |
val recT = mk_recordT (all_fields, HOLogic.unitT); |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
713 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
714 |
|
4890 | 715 |
(* prepare print translation functions *) |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
716 |
|
5698 | 717 |
val field_tr's = |
718 |
print_translation (distinct (flat (map NameSpace.accesses (full_moreN :: names)))); |
|
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
719 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
720 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
721 |
(* prepare declarations *) |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
722 |
|
5197 | 723 |
val sel_decls = map (mk_selC rec_schemeT) bfields @ |
724 |
[mk_moreC rec_schemeT (moreN, moreT)]; |
|
725 |
val update_decls = map (mk_updateC rec_schemeT) bfields @ |
|
726 |
[mk_more_updateC rec_schemeT (moreN, moreT)]; |
|
4890 | 727 |
val make_decls = |
728 |
[(mk_makeC rec_schemeT (make_schemeN, all_types @ [moreT])), |
|
729 |
(mk_makeC recT (makeN, all_types))]; |
|
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
730 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
731 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
732 |
(* prepare definitions *) |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
733 |
|
4895 | 734 |
(*record (scheme) type abbreviation*) |
4890 | 735 |
val recordT_specs = |
736 |
[(suffix schemeN bname, alphas @ [zeta], rec_schemeT, Syntax.NoSyn), |
|
737 |
(bname, alphas, recT, Syntax.NoSyn)]; |
|
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
738 |
|
4894 | 739 |
(*selectors*) |
4890 | 740 |
fun mk_sel_spec (i, c) = |
4894 | 741 |
mk_sel r c :== mk_fst (funpow i mk_snd (parent_more r)); |
742 |
val sel_specs = |
|
743 |
ListPair.map mk_sel_spec (idxs, names) @ |
|
744 |
[more_part r :== funpow len mk_snd (parent_more r)]; |
|
4890 | 745 |
|
746 |
(*updates*) |
|
4894 | 747 |
val all_sels = all_names ~~ map (mk_sel r) all_names; |
4890 | 748 |
fun mk_upd_spec (i, (c, x)) = |
4894 | 749 |
mk_update r (c, x) :== |
750 |
mk_record (nth_update (c, x) (parent_len + i, all_sels), more_part r) |
|
5197 | 751 |
val update_specs = |
752 |
ListPair.map mk_upd_spec (idxs, named_vars) @ |
|
753 |
[more_part_update r more :== mk_record (all_sels, more)]; |
|
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
754 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
755 |
(*makes*) |
4890 | 756 |
val make_scheme = Const (mk_makeC rec_schemeT (full make_schemeN, all_types @ [moreT])); |
757 |
val make = Const (mk_makeC recT (full makeN, all_types)); |
|
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
758 |
val make_specs = |
4894 | 759 |
[list_comb (make_scheme, all_vars) $ more :== rec_scheme, |
760 |
list_comb (make, all_vars) :== mk_record (all_named_vars, HOLogic.unit)]; |
|
761 |
||
762 |
||
763 |
(* prepare propositions *) |
|
764 |
||
765 |
(*selectors*) |
|
766 |
val sel_props = |
|
767 |
map (fn (c, x) => mk_sel rec_scheme c === x) named_vars @ |
|
768 |
[more_part rec_scheme === more]; |
|
769 |
||
770 |
(*updates*) |
|
771 |
fun mk_upd_prop (i, (c, T)) = |
|
772 |
let val x' = Free (variant all_xs (base c ^ "'"), T) in |
|
773 |
mk_update rec_scheme (c, x') === |
|
774 |
mk_record (nth_update (c, x') (parent_len + i, all_named_vars), more) |
|
775 |
end; |
|
5197 | 776 |
val update_props = |
777 |
ListPair.map mk_upd_prop (idxs, fields) @ |
|
778 |
let val more' = Free (variant all_xs (moreN ^ "'"), moreT) |
|
779 |
in [more_part_update rec_scheme more' === mk_record (all_named_vars, more')] end; |
|
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
780 |
|
9626 | 781 |
(*equality*) |
782 |
fun mk_sel_eq (t, T) = |
|
783 |
let val t' = Term.abstract_over (r, t) |
|
784 |
in HOLogic.mk_Trueprop (HOLogic.eq_const T $ Term.incr_boundvars 1 t' $ t') end; |
|
785 |
val sel_eqs = map2 mk_sel_eq (map (mk_sel r) all_names @ [more_part r], all_types @ [moreT]); |
|
786 |
val equality_prop = |
|
787 |
Term.all rec_schemeT $ (Abs ("r", rec_schemeT, |
|
788 |
Term.all rec_schemeT $ (Abs ("r'", rec_schemeT, |
|
789 |
Logic.list_implies (sel_eqs, |
|
790 |
HOLogic.mk_Trueprop (HOLogic.eq_const rec_schemeT $ Bound 1 $ Bound 0)))))); |
|
791 |
||
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
792 |
|
4890 | 793 |
(* 1st stage: fields_thy *) |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
794 |
|
5713 | 795 |
val (fields_thy, field_simps, field_injects, field_splits) = |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
796 |
thy |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
797 |
|> Theory.add_path bname |
9040 | 798 |
|> field_definitions fields names xs zeta moreT more vars named_vars; |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
799 |
|
6092 | 800 |
val named_splits = map2 (fn (c, th) => (suffix field_typeN c, th)) (names, field_splits); |
5698 | 801 |
|
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
802 |
|
4890 | 803 |
(* 2nd stage: defs_thy *) |
804 |
||
8428 | 805 |
val (defs_thy, ((sel_defs, update_defs), make_defs)) = |
4890 | 806 |
fields_thy |
9626 | 807 |
|> add_record_splits named_splits |
4890 | 808 |
|> Theory.parent_path |
5197 | 809 |
|> Theory.add_tyabbrs_i recordT_specs (*not made part of record name space!*) |
4890 | 810 |
|> Theory.add_path bname |
5197 | 811 |
|> Theory.add_trfuns ([], [], field_tr's, []) |
4894 | 812 |
|> (Theory.add_consts_i o map Syntax.no_syn) |
813 |
(sel_decls @ update_decls @ make_decls) |
|
9315 | 814 |
|> (PureThy.add_defs_i false o map (fn x => (x, [Drule.tag_internal]))) sel_specs |
815 |
|>>> (PureThy.add_defs_i false o map (fn x => (x, [Drule.tag_internal]))) update_specs |
|
816 |
|>>> (PureThy.add_defs_i false o map Thm.no_attributes) make_specs; |
|
4890 | 817 |
|
818 |
||
819 |
(* 3rd stage: thms_thy *) |
|
820 |
||
4895 | 821 |
val parent_simps = flat (map #simps parents); |
7178 | 822 |
val prove = prove_simp (Theory.sign_of defs_thy) HOL_basic_ss []; |
9626 | 823 |
val prove' = prove_simp (Theory.sign_of defs_thy) HOL_ss; |
4890 | 824 |
|
4895 | 825 |
val sel_convs = map (prove (parent_simps @ sel_defs @ field_simps)) sel_props; |
826 |
val update_convs = map (prove (parent_simps @ update_defs @ sel_convs)) update_props; |
|
9626 | 827 |
val equality = |
828 |
prove' [ALLGOALS record_split_tac] (parent_simps @ sel_convs @ field_injects) equality_prop; |
|
4894 | 829 |
|
9626 | 830 |
val simps = field_simps @ sel_convs @ update_convs @ make_defs @ [equality]; |
6519 | 831 |
val iffs = field_injects; |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
832 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
833 |
val thms_thy = |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
834 |
defs_thy |
8428 | 835 |
|> (#1 oo (PureThy.add_thmss o map Thm.no_attributes)) |
4895 | 836 |
[("select_defs", sel_defs), |
4890 | 837 |
("update_defs", update_defs), |
4894 | 838 |
("make_defs", make_defs), |
4895 | 839 |
("select_convs", sel_convs), |
4894 | 840 |
("update_convs", update_convs)] |
10008 | 841 |
|> (#1 oo PureThy.add_thms) |
842 |
[(("equality", equality), [Classical.xtra_intro_global])] |
|
8428 | 843 |
|> (#1 oo PureThy.add_thmss) |
5707 | 844 |
[(("simps", simps), [Simplifier.simp_add_global]), |
6519 | 845 |
(("iffs", iffs), [iff_add_global])]; |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
846 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
847 |
|
4890 | 848 |
(* 4th stage: final_thy *) |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
849 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
850 |
val final_thy = |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
851 |
thms_thy |
4895 | 852 |
|> put_record name {args = args, parent = parent, fields = fields, simps = simps} |
9626 | 853 |
|> put_sel_upd (names @ [full_moreN]) (field_simps @ sel_defs @ update_defs) |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
854 |
|> Theory.parent_path; |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
855 |
|
6519 | 856 |
in (final_thy, {simps = simps, iffs = iffs}) end; |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
857 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
858 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
859 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
860 |
(** theory extender interface **) |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
861 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
862 |
(* prepare arguments *) |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
863 |
|
4894 | 864 |
(*note: read_raw_typ avoids expanding type abbreviations*) |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
865 |
fun read_raw_parent sign s = |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
866 |
(case Sign.read_raw_typ (sign, K None) s handle TYPE (msg, _, _) => error msg of |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
867 |
Type (name, Ts) => (Ts, name) |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
868 |
| _ => error ("Bad parent record specification: " ^ quote s)); |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
869 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
870 |
fun read_typ sign (env, s) = |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
871 |
let |
5060 | 872 |
fun def_sort (x, ~1) = assoc (env, x) |
873 |
| def_sort _ = None; |
|
874 |
val T = Type.no_tvars (Sign.read_typ (sign, def_sort) s) handle TYPE (msg, _, _) => error msg; |
|
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
875 |
in (Term.add_typ_tfrees (T, env), T) end; |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
876 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
877 |
fun cert_typ sign (env, raw_T) = |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
878 |
let val T = Type.no_tvars (Sign.certify_typ sign raw_T) handle TYPE (msg, _, _) => error msg |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
879 |
in (Term.add_typ_tfrees (T, env), T) end; |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
880 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
881 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
882 |
(* add_record *) |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
883 |
|
4895 | 884 |
(*we do all preparations and error checks here, deferring the real |
885 |
work to record_definition*) |
|
4890 | 886 |
|
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
887 |
fun gen_add_record prep_typ prep_raw_parent (params, bname) raw_parent raw_fields thy = |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
888 |
let |
4970 | 889 |
val _ = Theory.requires thy "Record" "record definitions"; |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
890 |
val sign = Theory.sign_of thy; |
5698 | 891 |
val _ = message ("Defining record " ^ quote bname ^ " ..."); |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
892 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
893 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
894 |
(* parents *) |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
895 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
896 |
fun prep_inst T = snd (cert_typ sign ([], T)); |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
897 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
898 |
val parent = apsome (apfst (map prep_inst) o prep_raw_parent sign) raw_parent |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
899 |
handle ERROR => error ("The error(s) above in parent record specification"); |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
900 |
val parents = add_parents thy (parent, []); |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
901 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
902 |
val init_env = |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
903 |
(case parent of |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
904 |
None => [] |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
905 |
| Some (types, _) => foldr Term.add_typ_tfrees (types, [])); |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
906 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
907 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
908 |
(* fields *) |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
909 |
|
4967 | 910 |
fun prep_field (env, (c, raw_T)) = |
911 |
let val (env', T) = prep_typ sign (env, raw_T) handle ERROR => |
|
912 |
error ("The error(s) above occured in field " ^ quote c) |
|
913 |
in (env', (c, T)) end; |
|
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
914 |
|
4967 | 915 |
val (envir, bfields) = foldl_map prep_field (init_env, raw_fields); |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
916 |
val envir_names = map fst envir; |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
917 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
918 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
919 |
(* args *) |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
920 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
921 |
val defaultS = Sign.defaultS sign; |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
922 |
val args = map (fn x => (x, if_none (assoc (envir, x)) defaultS)) params; |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
923 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
924 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
925 |
(* errors *) |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
926 |
|
4890 | 927 |
val name = Sign.full_name sign bname; |
928 |
val err_dup_record = |
|
929 |
if is_none (get_record thy name) then [] |
|
930 |
else ["Duplicate definition of record " ^ quote name]; |
|
931 |
||
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
932 |
val err_dup_parms = |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
933 |
(case duplicates params of |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
934 |
[] => [] |
4890 | 935 |
| dups => ["Duplicate parameter(s) " ^ commas dups]); |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
936 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
937 |
val err_extra_frees = |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
938 |
(case gen_rems (op =) (envir_names, params) of |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
939 |
[] => [] |
4890 | 940 |
| extras => ["Extra free type variable(s) " ^ commas extras]); |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
941 |
|
4890 | 942 |
val err_no_fields = if null bfields then ["No fields present"] else []; |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
943 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
944 |
val err_dup_fields = |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
945 |
(case duplicates (map fst bfields) of |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
946 |
[] => [] |
4890 | 947 |
| dups => ["Duplicate field(s) " ^ commas_quote dups]); |
948 |
||
949 |
val err_bad_fields = |
|
950 |
if forall (not_equal moreN o fst) bfields then [] |
|
951 |
else ["Illegal field name " ^ quote moreN]; |
|
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
952 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
953 |
val err_dup_sorts = |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
954 |
(case duplicates envir_names of |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
955 |
[] => [] |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
956 |
| dups => ["Inconsistent sort constraints for " ^ commas dups]); |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
957 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
958 |
val errs = |
4890 | 959 |
err_dup_record @ err_dup_parms @ err_extra_frees @ err_no_fields @ |
960 |
err_dup_fields @ err_bad_fields @ err_dup_sorts; |
|
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
961 |
in |
4890 | 962 |
if null errs then () else error (cat_lines errs); |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
963 |
thy |> record_definition (args, bname) parent parents bfields |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
964 |
end |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
965 |
handle ERROR => error ("Failed to define record " ^ quote bname); |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
966 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
967 |
val add_record = gen_add_record read_typ read_raw_parent; |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
968 |
val add_record_i = gen_add_record cert_typ (K I); |
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
969 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
970 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
971 |
|
6358 | 972 |
(** package setup **) |
973 |
||
974 |
(* setup theory *) |
|
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
975 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
976 |
val setup = |
5006 | 977 |
[RecordsData.init, |
11473
4546d8d39221
fix problem with user translations by making field names appear as consts;
wenzelm
parents:
10008
diff
changeset
|
978 |
Theory.add_trfuns (parse_ast_translation, parse_translation, [], []), |
6358 | 979 |
Method.add_methods [record_split_method], |
7178 | 980 |
Simplifier.change_simpset_of Simplifier.addsimprocs [record_simproc]]; |
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
981 |
|
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
982 |
|
6358 | 983 |
(* outer syntax *) |
984 |
||
6723 | 985 |
local structure P = OuterParse and K = OuterSyntax.Keyword in |
6358 | 986 |
|
987 |
val record_decl = |
|
6723 | 988 |
P.type_args -- P.name -- (P.$$$ "=" |-- Scan.option (P.typ --| P.$$$ "+") |
6729 | 989 |
-- Scan.repeat1 (P.name -- (P.$$$ "::" |-- P.typ) --| P.marg_comment)); |
6358 | 990 |
|
991 |
val recordP = |
|
6723 | 992 |
OuterSyntax.command "record" "define extensible record" K.thy_decl |
6519 | 993 |
(record_decl >> (fn (x, (y, z)) => Toplevel.theory (#1 o add_record x y z))); |
6358 | 994 |
|
995 |
val _ = OuterSyntax.add_parsers [recordP]; |
|
996 |
||
4867
9be2bf0ce909
package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff
changeset
|
997 |
end; |
5698 | 998 |
|
999 |
||
6384 | 1000 |
end; |
1001 |
||
5698 | 1002 |
structure BasicRecordPackage: BASIC_RECORD_PACKAGE = RecordPackage; |
1003 |
open BasicRecordPackage; |