haftmann@24219
|
1 |
(* Title: Pure/Isar/code.ML
|
haftmann@24219
|
2 |
Author: Florian Haftmann, TU Muenchen
|
haftmann@24219
|
3 |
|
haftmann@34160
|
4 |
Abstract executable ingredients of theory. Management of data
|
haftmann@34160
|
5 |
dependent on executable ingredients as synchronized cache; purged
|
haftmann@34160
|
6 |
on any change of underlying executable ingredients.
|
haftmann@24219
|
7 |
*)
|
haftmann@24219
|
8 |
|
haftmann@24219
|
9 |
signature CODE =
|
haftmann@24219
|
10 |
sig
|
haftmann@31957
|
11 |
(*constants*)
|
haftmann@31957
|
12 |
val check_const: theory -> term -> string
|
haftmann@31957
|
13 |
val read_bare_const: theory -> string -> string * typ
|
haftmann@31957
|
14 |
val read_const: theory -> string -> string
|
haftmann@31962
|
15 |
val string_of_const: theory -> string -> string
|
haftmann@33906
|
16 |
val const_typ: theory -> string -> typ
|
haftmann@31962
|
17 |
val args_number: theory -> string -> int
|
haftmann@31957
|
18 |
|
haftmann@31156
|
19 |
(*constructor sets*)
|
haftmann@31156
|
20 |
val constrset_of_consts: theory -> (string * typ) list
|
haftmann@40969
|
21 |
-> string * ((string * sort) list * (string * ((string * sort) list * typ list)) list)
|
haftmann@31156
|
22 |
|
haftmann@34313
|
23 |
(*code equations and certificates*)
|
haftmann@31962
|
24 |
val mk_eqn: theory -> thm * bool -> thm * bool
|
haftmann@31962
|
25 |
val mk_eqn_liberal: theory -> thm -> (thm * bool) option
|
haftmann@31156
|
26 |
val assert_eqn: theory -> thm * bool -> thm * bool
|
haftmann@31957
|
27 |
val const_typ_eqn: theory -> thm -> string * typ
|
haftmann@34895
|
28 |
val expand_eta: theory -> int -> thm -> thm
|
haftmann@34895
|
29 |
type cert
|
haftmann@34891
|
30 |
val empty_cert: theory -> string -> cert
|
haftmann@34891
|
31 |
val cert_of_eqns: theory -> string -> (thm * bool) list -> cert
|
haftmann@34313
|
32 |
val constrain_cert: theory -> sort list -> cert -> cert
|
haftmann@50986
|
33 |
val conclude_cert: cert -> cert
|
haftmann@35226
|
34 |
val typargs_deps_of_cert: theory -> cert -> (string * sort) list * (string * typ list) list
|
haftmann@36209
|
35 |
val equations_of_cert: theory -> cert -> ((string * sort) list * typ)
|
haftmann@36209
|
36 |
* (((term * string option) list * (term * string option)) * (thm option * bool)) list
|
haftmann@35226
|
37 |
val bare_thms_of_cert: theory -> cert -> thm list
|
haftmann@34895
|
38 |
val pretty_cert: theory -> cert -> Pretty.T list
|
haftmann@31156
|
39 |
|
haftmann@31962
|
40 |
(*executable code*)
|
haftmann@31156
|
41 |
val add_datatype: (string * typ) list -> theory -> theory
|
haftmann@31156
|
42 |
val add_datatype_cmd: string list -> theory -> theory
|
haftmann@35299
|
43 |
val datatype_interpretation:
|
haftmann@40969
|
44 |
(string * ((string * sort) list * (string * ((string * sort) list * typ list)) list)
|
haftmann@35299
|
45 |
-> theory -> theory) -> theory -> theory
|
haftmann@36112
|
46 |
val add_abstype: thm -> theory -> theory
|
haftmann@35299
|
47 |
val abstype_interpretation:
|
haftmann@40969
|
48 |
(string * ((string * sort) list * ((string * ((string * sort) list * typ)) * (string * thm)))
|
haftmann@31156
|
49 |
-> theory -> theory) -> theory -> theory
|
haftmann@28368
|
50 |
val add_eqn: thm -> theory -> theory
|
haftmann@31088
|
51 |
val add_nbe_eqn: thm -> theory -> theory
|
haftmann@47384
|
52 |
val add_abs_eqn: thm -> theory -> theory
|
haftmann@28368
|
53 |
val add_default_eqn: thm -> theory -> theory
|
haftmann@28703
|
54 |
val add_default_eqn_attribute: attribute
|
haftmann@28703
|
55 |
val add_default_eqn_attrib: Attrib.src
|
haftmann@37400
|
56 |
val add_nbe_default_eqn: thm -> theory -> theory
|
haftmann@37400
|
57 |
val add_nbe_default_eqn_attribute: attribute
|
haftmann@37400
|
58 |
val add_nbe_default_eqn_attrib: Attrib.src
|
haftmann@28368
|
59 |
val del_eqn: thm -> theory -> theory
|
haftmann@28368
|
60 |
val del_eqns: string -> theory -> theory
|
haftmann@24844
|
61 |
val add_case: thm -> theory -> theory
|
haftmann@24844
|
62 |
val add_undefined: string -> theory -> theory
|
haftmann@40969
|
63 |
val get_type: theory -> string
|
haftmann@40969
|
64 |
-> ((string * sort) list * (string * ((string * sort) list * typ list)) list) * bool
|
haftmann@35299
|
65 |
val get_type_of_constr_or_abstr: theory -> string -> (string * bool) option
|
haftmann@35226
|
66 |
val is_constr: theory -> string -> bool
|
haftmann@35226
|
67 |
val is_abstr: theory -> string -> bool
|
haftmann@49090
|
68 |
val get_cert: theory -> { functrans: ((thm * bool) list -> (thm * bool) list option) list,
|
haftmann@49090
|
69 |
ss: simpset } -> string -> cert
|
Andreas@48305
|
70 |
val get_case_scheme: theory -> string -> (int * (int * string option list)) option
|
haftmann@37413
|
71 |
val get_case_cong: theory -> string -> thm option
|
haftmann@31890
|
72 |
val undefineds: theory -> string list
|
haftmann@24219
|
73 |
val print_codesetup: theory -> unit
|
haftmann@24219
|
74 |
end;
|
haftmann@24219
|
75 |
|
haftmann@24219
|
76 |
signature CODE_DATA_ARGS =
|
haftmann@24219
|
77 |
sig
|
haftmann@24219
|
78 |
type T
|
haftmann@24219
|
79 |
val empty: T
|
haftmann@24219
|
80 |
end;
|
haftmann@24219
|
81 |
|
haftmann@24219
|
82 |
signature CODE_DATA =
|
haftmann@24219
|
83 |
sig
|
haftmann@24219
|
84 |
type T
|
haftmann@39641
|
85 |
val change: theory option -> (T -> T) -> T
|
haftmann@39641
|
86 |
val change_yield: theory option -> (T -> 'a * T) -> 'a * T
|
haftmann@24219
|
87 |
end;
|
haftmann@24219
|
88 |
|
haftmann@24219
|
89 |
signature PRIVATE_CODE =
|
haftmann@24219
|
90 |
sig
|
haftmann@24219
|
91 |
include CODE
|
wenzelm@52505
|
92 |
val declare_data: Any.T -> serial
|
wenzelm@52505
|
93 |
val change_yield_data: serial * ('a -> Any.T) * (Any.T -> 'a)
|
haftmann@34246
|
94 |
-> theory -> ('a -> 'b * 'a) -> 'b * 'a
|
haftmann@24219
|
95 |
end;
|
haftmann@24219
|
96 |
|
haftmann@24219
|
97 |
structure Code : PRIVATE_CODE =
|
haftmann@24219
|
98 |
struct
|
haftmann@24219
|
99 |
|
haftmann@31962
|
100 |
(** auxiliary **)
|
haftmann@31962
|
101 |
|
haftmann@31962
|
102 |
(* printing *)
|
haftmann@31156
|
103 |
|
wenzelm@39393
|
104 |
fun string_of_typ thy =
|
wenzelm@39393
|
105 |
Syntax.string_of_typ (Config.put show_sorts true (Syntax.init_pretty_global thy));
|
haftmann@31962
|
106 |
|
wenzelm@43230
|
107 |
fun string_of_const thy c =
|
wenzelm@43231
|
108 |
let val ctxt = Proof_Context.init_global thy in
|
wenzelm@52822
|
109 |
case Axclass.inst_of_param thy c of
|
wenzelm@43230
|
110 |
SOME (c, tyco) =>
|
wenzelm@43231
|
111 |
Proof_Context.extern_const ctxt c ^ " " ^ enclose "[" "]"
|
wenzelm@43231
|
112 |
(Proof_Context.extern_type ctxt tyco)
|
wenzelm@43231
|
113 |
| NONE => Proof_Context.extern_const ctxt c
|
wenzelm@43230
|
114 |
end;
|
haftmann@31156
|
115 |
|
haftmann@31156
|
116 |
|
haftmann@31962
|
117 |
(* constants *)
|
haftmann@31156
|
118 |
|
haftmann@50549
|
119 |
fun const_typ thy = Type.strip_sorts o Sign.the_const_type thy;
|
haftmann@50549
|
120 |
|
haftmann@50549
|
121 |
fun args_number thy = length o binder_types o const_typ thy;
|
haftmann@50549
|
122 |
|
haftmann@50549
|
123 |
fun devarify ty =
|
haftmann@50549
|
124 |
let
|
haftmann@50549
|
125 |
val tys = fold_atyps (fn TVar vi_sort => AList.update (op =) vi_sort) ty [];
|
haftmann@50549
|
126 |
val vs = Name.invent Name.context Name.aT (length tys);
|
haftmann@50549
|
127 |
val mapping = map2 (fn v => fn (vi, sort) => (vi, TFree (v, sort))) vs tys;
|
haftmann@50549
|
128 |
in Term.typ_subst_TVars mapping ty end;
|
haftmann@50549
|
129 |
|
haftmann@50549
|
130 |
fun typscheme thy (c, ty) =
|
haftmann@50549
|
131 |
(map dest_TFree (Sign.const_typargs thy (c, ty)), Type.strip_sorts ty);
|
haftmann@50549
|
132 |
|
haftmann@50549
|
133 |
fun typscheme_equiv (ty1, ty2) =
|
haftmann@50549
|
134 |
Type.raw_instance (devarify ty1, ty2) andalso Type.raw_instance (devarify ty2, ty1);
|
haftmann@50549
|
135 |
|
haftmann@31962
|
136 |
fun check_bare_const thy t = case try dest_Const t
|
haftmann@31962
|
137 |
of SOME c_ty => c_ty
|
haftmann@31962
|
138 |
| NONE => error ("Not a constant: " ^ Syntax.string_of_term_global thy t);
|
haftmann@31962
|
139 |
|
haftmann@40608
|
140 |
fun check_unoverload thy (c, ty) =
|
haftmann@40608
|
141 |
let
|
wenzelm@52822
|
142 |
val c' = Axclass.unoverload_const thy (c, ty);
|
haftmann@40608
|
143 |
val ty_decl = Sign.the_const_type thy c';
|
wenzelm@46216
|
144 |
in
|
haftmann@50549
|
145 |
if typscheme_equiv (ty_decl, Logic.varifyT_global ty)
|
wenzelm@46216
|
146 |
then c'
|
wenzelm@46216
|
147 |
else
|
wenzelm@46216
|
148 |
error ("Type\n" ^ string_of_typ thy ty ^
|
wenzelm@46216
|
149 |
"\nof constant " ^ quote c ^
|
wenzelm@46216
|
150 |
"\nis too specific compared to declared type\n" ^
|
wenzelm@46216
|
151 |
string_of_typ thy ty_decl)
|
haftmann@40608
|
152 |
end;
|
haftmann@40608
|
153 |
|
haftmann@40608
|
154 |
fun check_const thy = check_unoverload thy o check_bare_const thy;
|
haftmann@31962
|
155 |
|
haftmann@31962
|
156 |
fun read_bare_const thy = check_bare_const thy o Syntax.read_term_global thy;
|
haftmann@31962
|
157 |
|
haftmann@40608
|
158 |
fun read_const thy = check_unoverload thy o read_bare_const thy;
|
haftmann@31156
|
159 |
|
haftmann@32872
|
160 |
|
haftmann@31962
|
161 |
(** data store **)
|
haftmann@31962
|
162 |
|
haftmann@35226
|
163 |
(* datatypes *)
|
haftmann@31962
|
164 |
|
haftmann@44505
|
165 |
datatype typ_spec = Constructors of (string * ((string * sort) list * typ list)) list *
|
haftmann@44505
|
166 |
string list (*references to associated case constructors*)
|
haftmann@40969
|
167 |
| Abstractor of (string * ((string * sort) list * typ)) * (string * thm);
|
haftmann@31962
|
168 |
|
haftmann@44505
|
169 |
fun constructors_of (Constructors (cos, _)) = (cos, false)
|
haftmann@40969
|
170 |
| constructors_of (Abstractor ((co, (vs, ty)), _)) = ([(co, (vs, [ty]))], true);
|
haftmann@31962
|
171 |
|
haftmann@44509
|
172 |
fun case_consts_of (Constructors (_, case_consts)) = case_consts
|
haftmann@44509
|
173 |
| case_consts_of (Abstractor _) = [];
|
haftmann@31962
|
174 |
|
haftmann@35226
|
175 |
(* functions *)
|
haftmann@35226
|
176 |
|
haftmann@37435
|
177 |
datatype fun_spec = Default of (thm * bool) list * (thm * bool) list lazy
|
haftmann@44505
|
178 |
(* (cache for default equations, lazy computation of default equations)
|
haftmann@44505
|
179 |
-- helps to restore natural order of default equations *)
|
haftmann@35226
|
180 |
| Eqns of (thm * bool) list
|
haftmann@35226
|
181 |
| Proj of term * string
|
haftmann@35226
|
182 |
| Abstr of thm * string;
|
haftmann@35226
|
183 |
|
haftmann@37435
|
184 |
val empty_fun_spec = Default ([], Lazy.value []);
|
haftmann@35226
|
185 |
|
haftmann@35226
|
186 |
fun is_default (Default _) = true
|
haftmann@35226
|
187 |
| is_default _ = false;
|
haftmann@35226
|
188 |
|
haftmann@35226
|
189 |
fun associated_abstype (Abstr (_, tyco)) = SOME tyco
|
haftmann@35226
|
190 |
| associated_abstype _ = NONE;
|
haftmann@31962
|
191 |
|
haftmann@31962
|
192 |
|
haftmann@31962
|
193 |
(* executable code data *)
|
haftmann@31962
|
194 |
|
haftmann@31962
|
195 |
datatype spec = Spec of {
|
haftmann@31962
|
196 |
history_concluded: bool,
|
haftmann@35226
|
197 |
functions: ((bool * fun_spec) * (serial * fun_spec) list) Symtab.table
|
haftmann@31962
|
198 |
(*with explicit history*),
|
haftmann@35299
|
199 |
types: ((serial * ((string * sort) list * typ_spec)) list) Symtab.table
|
haftmann@31962
|
200 |
(*with explicit history*),
|
Andreas@48305
|
201 |
cases: ((int * (int * string option list)) * thm) Symtab.table * unit Symtab.table
|
haftmann@31962
|
202 |
};
|
haftmann@31962
|
203 |
|
haftmann@46858
|
204 |
fun make_spec (history_concluded, (functions, (types, cases))) =
|
haftmann@46858
|
205 |
Spec { history_concluded = history_concluded, functions = functions, types = types, cases = cases };
|
haftmann@46858
|
206 |
fun map_spec f (Spec { history_concluded = history_concluded,
|
haftmann@35299
|
207 |
functions = functions, types = types, cases = cases }) =
|
haftmann@46858
|
208 |
make_spec (f (history_concluded, (functions, (types, cases))));
|
haftmann@46858
|
209 |
fun merge_spec (Spec { history_concluded = _, functions = functions1,
|
haftmann@35299
|
210 |
types = types1, cases = (cases1, undefs1) },
|
haftmann@46858
|
211 |
Spec { history_concluded = _, functions = functions2,
|
haftmann@35299
|
212 |
types = types2, cases = (cases2, undefs2) }) =
|
haftmann@31962
|
213 |
let
|
bulwahn@43578
|
214 |
val types = Symtab.join (K (AList.merge (op =) (K true))) (types1, types2);
|
haftmann@44510
|
215 |
val case_consts_of' = (maps case_consts_of o map (snd o snd o hd o snd) o Symtab.dest);
|
haftmann@35226
|
216 |
fun merge_functions ((_, history1), (_, history2)) =
|
haftmann@31962
|
217 |
let
|
haftmann@31962
|
218 |
val raw_history = AList.merge (op = : serial * serial -> bool)
|
haftmann@35226
|
219 |
(K true) (history1, history2);
|
haftmann@35226
|
220 |
val filtered_history = filter_out (is_default o snd) raw_history;
|
haftmann@31962
|
221 |
val history = if null filtered_history
|
haftmann@31962
|
222 |
then raw_history else filtered_history;
|
haftmann@31962
|
223 |
in ((false, (snd o hd) history), history) end;
|
haftmann@44509
|
224 |
val all_datatype_specs = map (snd o snd o hd o snd) (Symtab.dest types);
|
haftmann@44509
|
225 |
val all_constructors = maps (map fst o fst o constructors_of) all_datatype_specs;
|
haftmann@44510
|
226 |
val invalidated_case_consts = union (op =) (case_consts_of' types1) (case_consts_of' types2)
|
haftmann@44510
|
227 |
|> subtract (op =) (maps case_consts_of all_datatype_specs)
|
bulwahn@43578
|
228 |
val functions = Symtab.join (K merge_functions) (functions1, functions2)
|
bulwahn@43578
|
229 |
|> fold (fn c => Symtab.map_entry c (apfst (K (true, empty_fun_spec)))) all_constructors;
|
haftmann@44509
|
230 |
val cases = (Symtab.merge (K true) (cases1, cases2)
|
haftmann@44510
|
231 |
|> fold Symtab.delete invalidated_case_consts, Symtab.merge (K true) (undefs1, undefs2));
|
haftmann@46858
|
232 |
in make_spec (false, (functions, (types, cases))) end;
|
haftmann@31962
|
233 |
|
haftmann@31962
|
234 |
fun history_concluded (Spec { history_concluded, ... }) = history_concluded;
|
haftmann@35226
|
235 |
fun the_functions (Spec { functions, ... }) = functions;
|
haftmann@35299
|
236 |
fun the_types (Spec { types, ... }) = types;
|
haftmann@31962
|
237 |
fun the_cases (Spec { cases, ... }) = cases;
|
haftmann@32544
|
238 |
val map_history_concluded = map_spec o apfst;
|
haftmann@46858
|
239 |
val map_functions = map_spec o apsnd o apfst;
|
haftmann@35226
|
240 |
val map_typs = map_spec o apsnd o apsnd o apfst;
|
haftmann@31962
|
241 |
val map_cases = map_spec o apsnd o apsnd o apsnd;
|
haftmann@31962
|
242 |
|
haftmann@31962
|
243 |
|
haftmann@31962
|
244 |
(* data slots dependent on executable code *)
|
haftmann@31962
|
245 |
|
haftmann@31962
|
246 |
(*private copy avoids potential conflict of table exceptions*)
|
wenzelm@31972
|
247 |
structure Datatab = Table(type key = int val ord = int_ord);
|
haftmann@31962
|
248 |
|
haftmann@31962
|
249 |
local
|
haftmann@31962
|
250 |
|
wenzelm@52505
|
251 |
type kind = { empty: Any.T };
|
haftmann@31962
|
252 |
|
wenzelm@44563
|
253 |
val kinds = Synchronized.var "Code_Data" (Datatab.empty: kind Datatab.table);
|
haftmann@31962
|
254 |
|
wenzelm@44563
|
255 |
fun invoke f k =
|
wenzelm@44563
|
256 |
(case Datatab.lookup (Synchronized.value kinds) k of
|
wenzelm@44563
|
257 |
SOME kind => f kind
|
wenzelm@44563
|
258 |
| NONE => raise Fail "Invalid code data identifier");
|
haftmann@31962
|
259 |
|
haftmann@31962
|
260 |
in
|
haftmann@31962
|
261 |
|
haftmann@34160
|
262 |
fun declare_data empty =
|
haftmann@31962
|
263 |
let
|
haftmann@31962
|
264 |
val k = serial ();
|
haftmann@34160
|
265 |
val kind = { empty = empty };
|
wenzelm@44563
|
266 |
val _ = Synchronized.change kinds (Datatab.update (k, kind));
|
haftmann@31962
|
267 |
in k end;
|
haftmann@31962
|
268 |
|
haftmann@31962
|
269 |
fun invoke_init k = invoke (fn kind => #empty kind) k;
|
haftmann@31962
|
270 |
|
haftmann@31962
|
271 |
end; (*local*)
|
haftmann@31962
|
272 |
|
haftmann@31962
|
273 |
|
haftmann@31962
|
274 |
(* theory store *)
|
haftmann@31962
|
275 |
|
haftmann@31962
|
276 |
local
|
haftmann@31962
|
277 |
|
wenzelm@52505
|
278 |
type data = Any.T Datatab.table;
|
wenzelm@53925
|
279 |
fun empty_dataref () = Synchronized.var "code data" (NONE : (data * theory) option);
|
haftmann@31962
|
280 |
|
haftmann@34239
|
281 |
structure Code_Data = Theory_Data
|
haftmann@31962
|
282 |
(
|
wenzelm@53925
|
283 |
type T = spec * (data * theory) option Synchronized.var;
|
haftmann@46858
|
284 |
val empty = (make_spec (false, (Symtab.empty,
|
haftmann@34239
|
285 |
(Symtab.empty, (Symtab.empty, Symtab.empty)))), empty_dataref ());
|
haftmann@50571
|
286 |
val extend : T -> T = apsnd (K (empty_dataref ()));
|
haftmann@34239
|
287 |
fun merge ((spec1, _), (spec2, _)) =
|
haftmann@34239
|
288 |
(merge_spec (spec1, spec2), empty_dataref ());
|
haftmann@31962
|
289 |
);
|
haftmann@31962
|
290 |
|
haftmann@31962
|
291 |
in
|
haftmann@31962
|
292 |
|
haftmann@35226
|
293 |
|
haftmann@31962
|
294 |
(* access to executable code *)
|
haftmann@31962
|
295 |
|
haftmann@50550
|
296 |
val the_exec : theory -> spec = fst o Code_Data.get;
|
haftmann@31962
|
297 |
|
haftmann@34239
|
298 |
fun map_exec_purge f = Code_Data.map (fn (exec, _) => (f exec, empty_dataref ()));
|
haftmann@31962
|
299 |
|
haftmann@35226
|
300 |
fun change_fun_spec delete c f = (map_exec_purge o map_functions
|
haftmann@35226
|
301 |
o (if delete then Symtab.map_entry c else Symtab.map_default (c, ((false, empty_fun_spec), [])))
|
haftmann@35226
|
302 |
o apfst) (fn (_, spec) => (true, f spec));
|
haftmann@31962
|
303 |
|
haftmann@31962
|
304 |
|
haftmann@31962
|
305 |
(* tackling equation history *)
|
haftmann@31962
|
306 |
|
haftmann@31962
|
307 |
fun continue_history thy = if (history_concluded o the_exec) thy
|
haftmann@31962
|
308 |
then thy
|
haftmann@31962
|
309 |
|> (Code_Data.map o apfst o map_history_concluded) (K false)
|
haftmann@31962
|
310 |
|> SOME
|
haftmann@31962
|
311 |
else NONE;
|
haftmann@31962
|
312 |
|
haftmann@31962
|
313 |
fun conclude_history thy = if (history_concluded o the_exec) thy
|
haftmann@31962
|
314 |
then NONE
|
haftmann@31962
|
315 |
else thy
|
haftmann@31962
|
316 |
|> (Code_Data.map o apfst)
|
haftmann@39264
|
317 |
((map_functions o Symtab.map) (fn _ => fn ((changed, current), history) =>
|
haftmann@31962
|
318 |
((false, current),
|
haftmann@31962
|
319 |
if changed then (serial (), current) :: history else history))
|
haftmann@31962
|
320 |
#> map_history_concluded (K true))
|
haftmann@31962
|
321 |
|> SOME;
|
haftmann@31962
|
322 |
|
wenzelm@54308
|
323 |
val _ = Theory.setup
|
wenzelm@54308
|
324 |
(Theory.at_begin continue_history #> Theory.at_end conclude_history);
|
haftmann@31962
|
325 |
|
haftmann@31962
|
326 |
|
haftmann@31962
|
327 |
(* access to data dependent on abstract executable code *)
|
haftmann@31962
|
328 |
|
haftmann@34239
|
329 |
fun change_yield_data (kind, mk, dest) theory f =
|
haftmann@31962
|
330 |
let
|
haftmann@34239
|
331 |
val dataref = (snd o Code_Data.get) theory;
|
wenzelm@53925
|
332 |
val (datatab, thy) = case Synchronized.value dataref
|
wenzelm@53925
|
333 |
of SOME (datatab, thy) =>
|
wenzelm@53925
|
334 |
if Theory.eq_thy (theory, thy)
|
wenzelm@53925
|
335 |
then (datatab, thy)
|
wenzelm@53925
|
336 |
else (Datatab.empty, theory)
|
wenzelm@53925
|
337 |
| NONE => (Datatab.empty, theory)
|
haftmann@34239
|
338 |
val data = case Datatab.lookup datatab kind
|
haftmann@34239
|
339 |
of SOME data => data
|
haftmann@34239
|
340 |
| NONE => invoke_init kind;
|
haftmann@41006
|
341 |
val result as (_, data') = f (dest data);
|
haftmann@34239
|
342 |
val _ = Synchronized.change dataref
|
wenzelm@53925
|
343 |
((K o SOME) (Datatab.update (kind, mk data') datatab, thy));
|
haftmann@34239
|
344 |
in result end;
|
haftmann@31962
|
345 |
|
haftmann@31962
|
346 |
end; (*local*)
|
haftmann@31962
|
347 |
|
haftmann@31962
|
348 |
|
haftmann@31962
|
349 |
(** foundation **)
|
haftmann@31962
|
350 |
|
haftmann@31962
|
351 |
(* datatypes *)
|
haftmann@31156
|
352 |
|
haftmann@35226
|
353 |
fun no_constr thy s (c, ty) = error ("Not a datatype constructor:\n" ^ string_of_const thy c
|
haftmann@35226
|
354 |
^ " :: " ^ string_of_typ thy ty ^ "\n" ^ enclose "(" ")" s);
|
haftmann@35226
|
355 |
|
haftmann@46858
|
356 |
fun analyze_constructor thy (c, ty) =
|
haftmann@35226
|
357 |
let
|
haftmann@46858
|
358 |
val _ = Thm.cterm_of thy (Const (c, ty));
|
haftmann@55976
|
359 |
val ty_decl = devarify (const_typ thy c);
|
haftmann@35226
|
360 |
fun last_typ c_ty ty =
|
haftmann@35226
|
361 |
let
|
haftmann@35226
|
362 |
val tfrees = Term.add_tfreesT ty [];
|
wenzelm@41093
|
363 |
val (tyco, vs) = (apsnd o map) dest_TFree (dest_Type (body_type ty))
|
haftmann@35226
|
364 |
handle TYPE _ => no_constr thy "bad type" c_ty
|
haftmann@36112
|
365 |
val _ = if tyco = "fun" then no_constr thy "bad type" c_ty else ();
|
wenzelm@46216
|
366 |
val _ =
|
wenzelm@46216
|
367 |
if has_duplicates (eq_fst (op =)) vs
|
haftmann@35226
|
368 |
then no_constr thy "duplicate type variables in datatype" c_ty else ();
|
wenzelm@46216
|
369 |
val _ =
|
wenzelm@46216
|
370 |
if length tfrees <> length vs
|
haftmann@35226
|
371 |
then no_constr thy "type variables missing in datatype" c_ty else ();
|
haftmann@35226
|
372 |
in (tyco, vs) end;
|
haftmann@35226
|
373 |
val (tyco, _) = last_typ (c, ty) ty_decl;
|
haftmann@35226
|
374 |
val (_, vs) = last_typ (c, ty) ty;
|
haftmann@35226
|
375 |
in ((tyco, map snd vs), (c, (map fst vs, ty))) end;
|
haftmann@35226
|
376 |
|
haftmann@50919
|
377 |
fun constrset_of_consts thy consts =
|
haftmann@31156
|
378 |
let
|
wenzelm@52822
|
379 |
val _ = map (fn (c, _) => if (is_some o Axclass.class_of_param thy) c
|
haftmann@50919
|
380 |
then error ("Is a class parameter: " ^ string_of_const thy c) else ()) consts;
|
haftmann@50919
|
381 |
val raw_constructors = map (analyze_constructor thy) consts;
|
haftmann@50919
|
382 |
val tyco = case distinct (op =) (map (fst o fst) raw_constructors)
|
haftmann@50919
|
383 |
of [tyco] => tyco
|
haftmann@50919
|
384 |
| [] => error "Empty constructor set"
|
haftmann@50919
|
385 |
| tycos => error ("Different type constructors in constructor set: " ^ commas_quote tycos)
|
haftmann@50919
|
386 |
val vs = Name.invent Name.context Name.aT (Sign.arity_number thy tyco);
|
haftmann@31156
|
387 |
fun inst vs' (c, (vs, ty)) =
|
haftmann@31156
|
388 |
let
|
haftmann@31156
|
389 |
val the_v = the o AList.lookup (op =) (vs ~~ vs');
|
haftmann@50919
|
390 |
val ty' = map_type_tfree (fn (v, _) => TFree (the_v v, [])) ty;
|
haftmann@50919
|
391 |
val (vs'', ty'') = typscheme thy (c, ty');
|
haftmann@50919
|
392 |
in (c, (vs'', binder_types ty'')) end;
|
haftmann@50919
|
393 |
val constructors = map (inst vs o snd) raw_constructors;
|
haftmann@50919
|
394 |
in (tyco, (map (rpair []) vs, constructors)) end;
|
haftmann@31156
|
395 |
|
haftmann@35299
|
396 |
fun get_type_entry thy tyco = case these (Symtab.lookup ((the_types o the_exec) thy) tyco)
|
haftmann@35226
|
397 |
of (_, entry) :: _ => SOME entry
|
haftmann@35226
|
398 |
| _ => NONE;
|
haftmann@35226
|
399 |
|
haftmann@40969
|
400 |
fun get_type thy tyco = case get_type_entry thy tyco
|
haftmann@35226
|
401 |
of SOME (vs, spec) => apfst (pair vs) (constructors_of spec)
|
haftmann@46858
|
402 |
| NONE => Sign.arity_number thy tyco
|
wenzelm@44211
|
403 |
|> Name.invent Name.context Name.aT
|
haftmann@35226
|
404 |
|> map (rpair [])
|
haftmann@35226
|
405 |
|> rpair []
|
haftmann@35226
|
406 |
|> rpair false;
|
haftmann@35226
|
407 |
|
haftmann@35299
|
408 |
fun get_abstype_spec thy tyco = case get_type_entry thy tyco
|
haftmann@35226
|
409 |
of SOME (vs, Abstractor spec) => (vs, spec)
|
haftmann@36122
|
410 |
| _ => error ("Not an abstract type: " ^ tyco);
|
haftmann@35226
|
411 |
|
haftmann@35299
|
412 |
fun get_type_of_constr_or_abstr thy c =
|
wenzelm@41093
|
413 |
case (body_type o const_typ thy) c
|
haftmann@41006
|
414 |
of Type (tyco, _) => let val ((_, cos), abstract) = get_type thy tyco
|
haftmann@35226
|
415 |
in if member (op =) (map fst cos) c then SOME (tyco, abstract) else NONE end
|
haftmann@31962
|
416 |
| _ => NONE;
|
haftmann@31962
|
417 |
|
haftmann@35299
|
418 |
fun is_constr thy c = case get_type_of_constr_or_abstr thy c
|
haftmann@35226
|
419 |
of SOME (_, false) => true
|
haftmann@35226
|
420 |
| _ => false;
|
haftmann@35226
|
421 |
|
haftmann@35299
|
422 |
fun is_abstr thy c = case get_type_of_constr_or_abstr thy c
|
haftmann@35226
|
423 |
of SOME (_, true) => true
|
haftmann@35226
|
424 |
| _ => false;
|
haftmann@31962
|
425 |
|
haftmann@31156
|
426 |
|
haftmann@34313
|
427 |
(* bare code equations *)
|
haftmann@31156
|
428 |
|
haftmann@35226
|
429 |
(* convention for variables:
|
haftmann@35226
|
430 |
?x ?'a for free-floating theorems (e.g. in the data store)
|
haftmann@35226
|
431 |
?x 'a for certificates
|
haftmann@35226
|
432 |
x 'a for final representation of equations
|
haftmann@35226
|
433 |
*)
|
haftmann@35226
|
434 |
|
haftmann@31156
|
435 |
exception BAD_THM of string;
|
haftmann@31156
|
436 |
fun bad_thm msg = raise BAD_THM msg;
|
haftmann@50775
|
437 |
fun error_thm f thy (thm, proper) = f (thm, proper)
|
haftmann@50775
|
438 |
handle BAD_THM msg => error (msg ^ ", in theorem:\n" ^ Display.string_of_thm_global thy thm);
|
haftmann@50775
|
439 |
fun error_abs_thm f thy thm = f thm
|
haftmann@50775
|
440 |
handle BAD_THM msg => error (msg ^ ", in theorem:\n" ^ Display.string_of_thm_global thy thm);
|
haftmann@50775
|
441 |
fun warning_thm f thy (thm, proper) = SOME (f (thm, proper))
|
haftmann@50775
|
442 |
handle BAD_THM msg => (warning (msg ^ ", in theorem:\n" ^ Display.string_of_thm_global thy thm); NONE)
|
haftmann@50775
|
443 |
fun try_thm f thm_proper = SOME (f thm_proper)
|
haftmann@50775
|
444 |
handle BAD_THM _ => NONE;
|
haftmann@31156
|
445 |
|
haftmann@31156
|
446 |
fun is_linear thm =
|
haftmann@31156
|
447 |
let val (_, args) = (strip_comb o fst o Logic.dest_equals o Thm.plain_prop_of) thm
|
haftmann@31156
|
448 |
in not (has_duplicates (op =) ((fold o fold_aterms)
|
haftmann@31156
|
449 |
(fn Var (v, _) => cons v | _ => I) args [])) end;
|
haftmann@31156
|
450 |
|
haftmann@36209
|
451 |
fun check_decl_ty thy (c, ty) =
|
haftmann@36209
|
452 |
let
|
haftmann@36209
|
453 |
val ty_decl = Sign.the_const_type thy c;
|
haftmann@50549
|
454 |
in if typscheme_equiv (ty_decl, ty) then ()
|
haftmann@36209
|
455 |
else bad_thm ("Type\n" ^ string_of_typ thy ty
|
haftmann@36209
|
456 |
^ "\nof constant " ^ quote c
|
haftmann@40608
|
457 |
^ "\nis too specific compared to declared type\n"
|
haftmann@36209
|
458 |
^ string_of_typ thy ty_decl)
|
haftmann@36209
|
459 |
end;
|
haftmann@36209
|
460 |
|
haftmann@35226
|
461 |
fun check_eqn thy { allow_nonlinear, allow_consts, allow_pats } thm (lhs, rhs) =
|
haftmann@31156
|
462 |
let
|
haftmann@31156
|
463 |
fun vars_of t = fold_aterms (fn Var (v, _) => insert (op =) v
|
haftmann@50775
|
464 |
| Free _ => bad_thm "Illegal free variable"
|
haftmann@31156
|
465 |
| _ => I) t [];
|
haftmann@31156
|
466 |
fun tvars_of t = fold_term_types (fn _ =>
|
haftmann@31156
|
467 |
fold_atyps (fn TVar (v, _) => insert (op =) v
|
haftmann@50775
|
468 |
| TFree _ => bad_thm "Illegal free type variable")) t [];
|
haftmann@31156
|
469 |
val lhs_vs = vars_of lhs;
|
haftmann@31156
|
470 |
val rhs_vs = vars_of rhs;
|
haftmann@31156
|
471 |
val lhs_tvs = tvars_of lhs;
|
haftmann@31156
|
472 |
val rhs_tvs = tvars_of rhs;
|
haftmann@31156
|
473 |
val _ = if null (subtract (op =) lhs_vs rhs_vs)
|
haftmann@31156
|
474 |
then ()
|
haftmann@50775
|
475 |
else bad_thm "Free variables on right hand side of equation";
|
haftmann@31156
|
476 |
val _ = if null (subtract (op =) lhs_tvs rhs_tvs)
|
haftmann@31156
|
477 |
then ()
|
haftmann@50775
|
478 |
else bad_thm "Free type variables on right hand side of equation";
|
haftmann@34894
|
479 |
val (head, args) = strip_comb lhs;
|
haftmann@31156
|
480 |
val (c, ty) = case head
|
wenzelm@52822
|
481 |
of Const (c_ty as (_, ty)) => (Axclass.unoverload_const thy c_ty, ty)
|
haftmann@50775
|
482 |
| _ => bad_thm "Equation not headed by constant";
|
haftmann@50775
|
483 |
fun check _ (Abs _) = bad_thm "Abstraction on left hand side of equation"
|
haftmann@31156
|
484 |
| check 0 (Var _) = ()
|
haftmann@50775
|
485 |
| check _ (Var _) = bad_thm "Variable with application on left hand side of equation"
|
haftmann@31156
|
486 |
| check n (t1 $ t2) = (check (n+1) t1; check 0 t2)
|
haftmann@34894
|
487 |
| check n (Const (c_ty as (c, ty))) =
|
haftmann@35226
|
488 |
if allow_pats then let
|
wenzelm@52822
|
489 |
val c' = Axclass.unoverload_const thy c_ty
|
haftmann@46858
|
490 |
in if n = (length o binder_types) ty
|
haftmann@35226
|
491 |
then if allow_consts orelse is_constr thy c'
|
haftmann@33906
|
492 |
then ()
|
haftmann@50775
|
493 |
else bad_thm (quote c ^ " is not a constructor, on left hand side of equation")
|
haftmann@50775
|
494 |
else bad_thm ("Partially applied constant " ^ quote c ^ " on left hand side of equation")
|
haftmann@50775
|
495 |
end else bad_thm ("Pattern not allowed here, but constant " ^ quote c ^ " encountered on left hand side of equation")
|
haftmann@31156
|
496 |
val _ = map (check 0) args;
|
haftmann@35226
|
497 |
val _ = if allow_nonlinear orelse is_linear thm then ()
|
haftmann@50775
|
498 |
else bad_thm "Duplicate variables on left hand side of equation";
|
wenzelm@52822
|
499 |
val _ = if (is_none o Axclass.class_of_param thy) c then ()
|
haftmann@50775
|
500 |
else bad_thm "Overloaded constant as head in equation";
|
haftmann@34894
|
501 |
val _ = if not (is_constr thy c) then ()
|
haftmann@50775
|
502 |
else bad_thm "Constructor as head in equation";
|
haftmann@35226
|
503 |
val _ = if not (is_abstr thy c) then ()
|
haftmann@50775
|
504 |
else bad_thm "Abstractor as head in equation";
|
haftmann@36209
|
505 |
val _ = check_decl_ty thy (c, ty);
|
haftmann@53612
|
506 |
val _ = case strip_type ty
|
haftmann@53612
|
507 |
of (Type (tyco, _) :: _, _) => (case get_type_entry thy tyco
|
haftmann@53612
|
508 |
of SOME (_, Abstractor (_, (proj, _))) => if c = proj
|
haftmann@53612
|
509 |
then bad_thm "Projection as head in equation"
|
haftmann@53612
|
510 |
else ()
|
haftmann@53612
|
511 |
| _ => ())
|
haftmann@53612
|
512 |
| _ => ();
|
haftmann@35226
|
513 |
in () end;
|
haftmann@35226
|
514 |
|
haftmann@35226
|
515 |
fun gen_assert_eqn thy check_patterns (thm, proper) =
|
haftmann@35226
|
516 |
let
|
haftmann@35226
|
517 |
val (lhs, rhs) = (Logic.dest_equals o Thm.plain_prop_of) thm
|
haftmann@50775
|
518 |
handle TERM _ => bad_thm "Not an equation"
|
haftmann@50775
|
519 |
| THM _ => bad_thm "Not a proper equation";
|
haftmann@35226
|
520 |
val _ = check_eqn thy { allow_nonlinear = not proper,
|
haftmann@35226
|
521 |
allow_consts = not (proper andalso check_patterns), allow_pats = true } thm (lhs, rhs);
|
haftmann@31156
|
522 |
in (thm, proper) end;
|
haftmann@31156
|
523 |
|
haftmann@35226
|
524 |
fun assert_abs_eqn thy some_tyco thm =
|
haftmann@35226
|
525 |
let
|
haftmann@35226
|
526 |
val (full_lhs, rhs) = (Logic.dest_equals o Thm.plain_prop_of) thm
|
haftmann@50775
|
527 |
handle TERM _ => bad_thm "Not an equation"
|
haftmann@50775
|
528 |
| THM _ => bad_thm "Not a proper equation";
|
haftmann@35226
|
529 |
val (rep, lhs) = dest_comb full_lhs
|
haftmann@50775
|
530 |
handle TERM _ => bad_thm "Not an abstract equation";
|
haftmann@47384
|
531 |
val (rep_const, ty) = dest_Const rep
|
haftmann@50775
|
532 |
handle TERM _ => bad_thm "Not an abstract equation";
|
haftmann@40428
|
533 |
val (tyco, Ts) = (dest_Type o domain_type) ty
|
haftmann@50775
|
534 |
handle TERM _ => bad_thm "Not an abstract equation"
|
haftmann@50775
|
535 |
| TYPE _ => bad_thm "Not an abstract equation";
|
haftmann@35226
|
536 |
val _ = case some_tyco of SOME tyco' => if tyco = tyco' then ()
|
haftmann@50775
|
537 |
else bad_thm ("Abstract type mismatch:" ^ quote tyco ^ " vs. " ^ quote tyco')
|
haftmann@35226
|
538 |
| NONE => ();
|
haftmann@53774
|
539 |
val (vs', (_, (rep', _))) = case try (get_abstype_spec thy) tyco
|
haftmann@53774
|
540 |
of SOME data => data
|
haftmann@53774
|
541 |
| NONE => bad_thm ("Not an abstract type: " ^ tyco);
|
haftmann@35226
|
542 |
val _ = if rep_const = rep' then ()
|
haftmann@50775
|
543 |
else bad_thm ("Projection mismatch: " ^ quote rep_const ^ " vs. " ^ quote rep');
|
haftmann@35226
|
544 |
val _ = check_eqn thy { allow_nonlinear = false,
|
haftmann@35226
|
545 |
allow_consts = false, allow_pats = false } thm (lhs, rhs);
|
haftmann@40810
|
546 |
val _ = if forall2 (fn T => fn (_, sort) => Sign.of_sort thy (T, sort)) Ts vs' then ()
|
haftmann@40428
|
547 |
else error ("Type arguments do not satisfy sort constraints of abstype certificate.");
|
haftmann@35226
|
548 |
in (thm, tyco) end;
|
haftmann@35226
|
549 |
|
haftmann@50775
|
550 |
fun assert_eqn thy = error_thm (gen_assert_eqn thy true) thy;
|
haftmann@31156
|
551 |
|
wenzelm@43231
|
552 |
fun meta_rewrite thy = Local_Defs.meta_rewrite_rule (Proof_Context.init_global thy);
|
haftmann@31962
|
553 |
|
haftmann@50775
|
554 |
fun mk_eqn thy = error_thm (gen_assert_eqn thy false) thy o
|
haftmann@31962
|
555 |
apfst (meta_rewrite thy);
|
haftmann@31962
|
556 |
|
haftmann@31962
|
557 |
fun mk_eqn_liberal thy = Option.map (fn (thm, _) => (thm, is_linear thm))
|
haftmann@34894
|
558 |
o try_thm (gen_assert_eqn thy false) o rpair false o meta_rewrite thy;
|
haftmann@31156
|
559 |
|
haftmann@53774
|
560 |
fun mk_eqn_maybe_abs thy raw_thm =
|
haftmann@53774
|
561 |
let
|
haftmann@53774
|
562 |
val thm = meta_rewrite thy raw_thm;
|
haftmann@53774
|
563 |
val some_abs_thm = try_thm (assert_abs_eqn thy NONE) thm;
|
haftmann@53774
|
564 |
in case some_abs_thm
|
haftmann@53774
|
565 |
of SOME (thm, tyco) => SOME ((thm, true), SOME tyco)
|
haftmann@53774
|
566 |
| NONE => (Option.map (fn (thm, _) => ((thm, is_linear thm), NONE))
|
haftmann@53774
|
567 |
o warning_thm (gen_assert_eqn thy false) thy) (thm, false)
|
haftmann@53774
|
568 |
end;
|
haftmann@53774
|
569 |
|
haftmann@50775
|
570 |
fun mk_abs_eqn thy = error_abs_thm (assert_abs_eqn thy NONE) thy o meta_rewrite thy;
|
haftmann@35226
|
571 |
|
haftmann@33906
|
572 |
val head_eqn = dest_Const o fst o strip_comb o fst o Logic.dest_equals o Thm.plain_prop_of;
|
haftmann@31156
|
573 |
|
haftmann@31957
|
574 |
fun const_typ_eqn thy thm =
|
haftmann@31156
|
575 |
let
|
haftmann@32640
|
576 |
val (c, ty) = head_eqn thm;
|
wenzelm@52822
|
577 |
val c' = Axclass.unoverload_const thy (c, ty);
|
haftmann@33906
|
578 |
(*permissive wrt. to overloaded constants!*)
|
haftmann@31156
|
579 |
in (c', ty) end;
|
haftmann@33906
|
580 |
|
haftmann@31957
|
581 |
fun const_eqn thy = fst o const_typ_eqn thy;
|
haftmann@31156
|
582 |
|
wenzelm@52822
|
583 |
fun const_abs_eqn thy = Axclass.unoverload_const thy o dest_Const o fst o strip_comb o snd
|
haftmann@35226
|
584 |
o dest_comb o fst o Logic.dest_equals o Thm.plain_prop_of;
|
haftmann@35226
|
585 |
|
haftmann@35226
|
586 |
fun mk_proj tyco vs ty abs rep =
|
haftmann@35226
|
587 |
let
|
haftmann@35226
|
588 |
val ty_abs = Type (tyco, map TFree vs);
|
haftmann@35226
|
589 |
val xarg = Var (("x", 0), ty);
|
haftmann@35226
|
590 |
in Logic.mk_equals (Const (rep, ty_abs --> ty) $ (Const (abs, ty --> ty_abs) $ xarg), xarg) end;
|
haftmann@35226
|
591 |
|
haftmann@34895
|
592 |
|
haftmann@34895
|
593 |
(* technical transformations of code equations *)
|
haftmann@34895
|
594 |
|
haftmann@34895
|
595 |
fun expand_eta thy k thm =
|
haftmann@31962
|
596 |
let
|
haftmann@34895
|
597 |
val (lhs, rhs) = (Logic.dest_equals o Thm.plain_prop_of) thm;
|
haftmann@34895
|
598 |
val (_, args) = strip_comb lhs;
|
haftmann@34895
|
599 |
val l = if k = ~1
|
haftmann@34895
|
600 |
then (length o fst o strip_abs) rhs
|
haftmann@34895
|
601 |
else Int.max (0, k - length args);
|
haftmann@34895
|
602 |
val (raw_vars, _) = Term.strip_abs_eta l rhs;
|
haftmann@34895
|
603 |
val vars = burrow_fst (Name.variant_list (map (fst o fst) (Term.add_vars lhs [])))
|
haftmann@34895
|
604 |
raw_vars;
|
haftmann@34895
|
605 |
fun expand (v, ty) thm = Drule.fun_cong_rule thm
|
haftmann@34895
|
606 |
(Thm.cterm_of thy (Var ((v, 0), ty)));
|
haftmann@34895
|
607 |
in
|
haftmann@34895
|
608 |
thm
|
haftmann@34895
|
609 |
|> fold expand vars
|
haftmann@34895
|
610 |
|> Conv.fconv_rule Drule.beta_eta_conversion
|
haftmann@34895
|
611 |
end;
|
haftmann@34895
|
612 |
|
haftmann@34895
|
613 |
fun same_arity thy thms =
|
haftmann@34895
|
614 |
let
|
haftmann@34895
|
615 |
val num_args_of = length o snd o strip_comb o fst o Logic.dest_equals;
|
haftmann@34895
|
616 |
val k = fold (Integer.max o num_args_of o Thm.prop_of) thms 0;
|
haftmann@34895
|
617 |
in map (expand_eta thy k) thms end;
|
haftmann@34895
|
618 |
|
haftmann@34895
|
619 |
fun mk_desymbolization pre post mk vs =
|
haftmann@34895
|
620 |
let
|
haftmann@34895
|
621 |
val names = map (pre o fst o fst) vs
|
haftmann@34895
|
622 |
|> map (Name.desymbolize false)
|
haftmann@34895
|
623 |
|> Name.variant_list []
|
haftmann@34895
|
624 |
|> map post;
|
haftmann@34895
|
625 |
in map_filter (fn (((v, i), x), v') =>
|
haftmann@34895
|
626 |
if v = v' andalso i = 0 then NONE
|
haftmann@34895
|
627 |
else SOME (((v, i), x), mk ((v', 0), x))) (vs ~~ names)
|
haftmann@34895
|
628 |
end;
|
haftmann@34895
|
629 |
|
haftmann@41006
|
630 |
fun desymbolize_tvars thms =
|
haftmann@34895
|
631 |
let
|
haftmann@34895
|
632 |
val tvs = fold (Term.add_tvars o Thm.prop_of) thms [];
|
haftmann@34895
|
633 |
val tvar_subst = mk_desymbolization (unprefix "'") (prefix "'") TVar tvs;
|
haftmann@34895
|
634 |
in map (Thm.certify_instantiate (tvar_subst, [])) thms end;
|
haftmann@34895
|
635 |
|
haftmann@41006
|
636 |
fun desymbolize_vars thm =
|
haftmann@34895
|
637 |
let
|
haftmann@34895
|
638 |
val vs = Term.add_vars (Thm.prop_of thm) [];
|
haftmann@34895
|
639 |
val var_subst = mk_desymbolization I I Var vs;
|
haftmann@34895
|
640 |
in Thm.certify_instantiate ([], var_subst) thm end;
|
haftmann@34895
|
641 |
|
haftmann@41006
|
642 |
fun canonize_thms thy = desymbolize_tvars #> same_arity thy #> map desymbolize_vars;
|
haftmann@31156
|
643 |
|
haftmann@34313
|
644 |
|
haftmann@36112
|
645 |
(* abstype certificates *)
|
haftmann@36112
|
646 |
|
haftmann@36112
|
647 |
fun check_abstype_cert thy proto_thm =
|
haftmann@36112
|
648 |
let
|
wenzelm@52822
|
649 |
val thm = (Axclass.unoverload thy o meta_rewrite thy) proto_thm;
|
haftmann@36112
|
650 |
val (lhs, rhs) = Logic.dest_equals (Thm.plain_prop_of thm)
|
haftmann@50775
|
651 |
handle TERM _ => bad_thm "Not an equation"
|
haftmann@50775
|
652 |
| THM _ => bad_thm "Not a proper equation";
|
haftmann@36209
|
653 |
val ((abs, raw_ty), ((rep, rep_ty), param)) = (apsnd (apfst dest_Const o dest_comb)
|
haftmann@36112
|
654 |
o apfst dest_Const o dest_comb) lhs
|
haftmann@50775
|
655 |
handle TERM _ => bad_thm "Not an abstype certificate";
|
wenzelm@52822
|
656 |
val _ = pairself (fn c => if (is_some o Axclass.class_of_param thy) c
|
haftmann@36209
|
657 |
then error ("Is a class parameter: " ^ string_of_const thy c) else ()) (abs, rep);
|
haftmann@36209
|
658 |
val _ = check_decl_ty thy (abs, raw_ty);
|
haftmann@36209
|
659 |
val _ = check_decl_ty thy (rep, rep_ty);
|
haftmann@49083
|
660 |
val _ = if length (binder_types raw_ty) = 1
|
haftmann@49083
|
661 |
then ()
|
haftmann@50775
|
662 |
else bad_thm "Bad type for abstract constructor";
|
haftmann@41006
|
663 |
val _ = (fst o dest_Var) param
|
haftmann@50775
|
664 |
handle TERM _ => bad_thm "Not an abstype certificate";
|
haftmann@50775
|
665 |
val _ = if param = rhs then () else bad_thm "Not an abstype certificate";
|
wenzelm@46216
|
666 |
val ((tyco, sorts), (abs, (vs, ty'))) =
|
haftmann@55976
|
667 |
analyze_constructor thy (abs, devarify raw_ty);
|
haftmann@36112
|
668 |
val ty = domain_type ty';
|
haftmann@50549
|
669 |
val (vs', _) = typscheme thy (abs, ty');
|
haftmann@40969
|
670 |
in (tyco, (vs ~~ sorts, ((abs, (vs', ty)), (rep, thm)))) end;
|
haftmann@36112
|
671 |
|
haftmann@36112
|
672 |
|
haftmann@34313
|
673 |
(* code equation certificates *)
|
haftmann@34313
|
674 |
|
haftmann@34895
|
675 |
fun build_head thy (c, ty) =
|
haftmann@34895
|
676 |
Thm.cterm_of thy (Logic.mk_equals (Free ("HEAD", ty), Const (c, ty)));
|
haftmann@34313
|
677 |
|
haftmann@34895
|
678 |
fun get_head thy cert_thm =
|
haftmann@34895
|
679 |
let
|
haftmann@34895
|
680 |
val [head] = (#hyps o Thm.crep_thm) cert_thm;
|
haftmann@34895
|
681 |
val (_, Const (c, ty)) = (Logic.dest_equals o Thm.term_of) head;
|
haftmann@34895
|
682 |
in (typscheme thy (c, ty), head) end;
|
haftmann@34895
|
683 |
|
haftmann@35226
|
684 |
fun typscheme_projection thy =
|
haftmann@35226
|
685 |
typscheme thy o dest_Const o fst o dest_comb o fst o Logic.dest_equals;
|
haftmann@35226
|
686 |
|
haftmann@35226
|
687 |
fun typscheme_abs thy =
|
haftmann@35226
|
688 |
typscheme thy o dest_Const o fst o strip_comb o snd o dest_comb o fst o Logic.dest_equals o Thm.prop_of;
|
haftmann@35226
|
689 |
|
haftmann@35226
|
690 |
fun constrain_thm thy vs sorts thm =
|
haftmann@35226
|
691 |
let
|
haftmann@35226
|
692 |
val mapping = map2 (fn (v, sort) => fn sort' =>
|
haftmann@35226
|
693 |
(v, Sorts.inter_sort (Sign.classes_of thy) (sort, sort'))) vs sorts;
|
haftmann@35226
|
694 |
val inst = map2 (fn (v, sort) => fn (_, sort') =>
|
haftmann@35226
|
695 |
(((v, 0), sort), TFree (v, sort'))) vs mapping;
|
haftmann@41049
|
696 |
val subst = (map_types o map_type_tfree)
|
haftmann@41049
|
697 |
(fn (v, _) => TFree (v, the (AList.lookup (op =) mapping v)));
|
haftmann@35226
|
698 |
in
|
haftmann@35226
|
699 |
thm
|
wenzelm@35845
|
700 |
|> Thm.varifyT_global
|
haftmann@35226
|
701 |
|> Thm.certify_instantiate (inst, [])
|
haftmann@35226
|
702 |
|> pair subst
|
haftmann@35226
|
703 |
end;
|
haftmann@35226
|
704 |
|
haftmann@35226
|
705 |
fun concretify_abs thy tyco abs_thm =
|
haftmann@35226
|
706 |
let
|
haftmann@41006
|
707 |
val (_, ((c, _), (_, cert))) = get_abstype_spec thy tyco;
|
haftmann@35226
|
708 |
val lhs = (fst o Logic.dest_equals o Thm.prop_of) abs_thm
|
haftmann@35226
|
709 |
val ty = fastype_of lhs;
|
haftmann@35226
|
710 |
val ty_abs = (fastype_of o snd o dest_comb) lhs;
|
haftmann@35226
|
711 |
val abs = Thm.cterm_of thy (Const (c, ty --> ty_abs));
|
haftmann@35226
|
712 |
val raw_concrete_thm = Drule.transitive_thm OF [Thm.symmetric cert, Thm.combination (Thm.reflexive abs) abs_thm];
|
wenzelm@35845
|
713 |
in (c, (Thm.varifyT_global o zero_var_indexes) raw_concrete_thm) end;
|
haftmann@35226
|
714 |
|
haftmann@35226
|
715 |
fun add_rhss_of_eqn thy t =
|
haftmann@35226
|
716 |
let
|
haftmann@46858
|
717 |
val (args, rhs) = (apfst (snd o strip_comb) o Logic.dest_equals) t;
|
haftmann@35226
|
718 |
fun add_const (Const (c, ty)) = insert (op =) (c, Sign.const_typargs thy (c, ty))
|
haftmann@35226
|
719 |
| add_const _ = I
|
haftmann@39810
|
720 |
val add_consts = fold_aterms add_const
|
haftmann@39810
|
721 |
in add_consts rhs o fold add_consts args end;
|
haftmann@35226
|
722 |
|
haftmann@47384
|
723 |
val dest_eqn = apfst (snd o strip_comb) o Logic.dest_equals o Logic.unvarify_global;
|
haftmann@35226
|
724 |
|
haftmann@35226
|
725 |
abstype cert = Equations of thm * bool list
|
haftmann@35226
|
726 |
| Projection of term * string
|
haftmann@35226
|
727 |
| Abstract of thm * string
|
haftmann@35226
|
728 |
with
|
haftmann@34891
|
729 |
|
haftmann@34891
|
730 |
fun empty_cert thy c =
|
haftmann@34891
|
731 |
let
|
haftmann@55976
|
732 |
val raw_ty = devarify (const_typ thy c);
|
haftmann@50549
|
733 |
val (vs, _) = typscheme thy (c, raw_ty);
|
wenzelm@52822
|
734 |
val sortargs = case Axclass.class_of_param thy c
|
haftmann@41009
|
735 |
of SOME class => [[class]]
|
haftmann@41009
|
736 |
| NONE => (case get_type_of_constr_or_abstr thy c
|
haftmann@41009
|
737 |
of SOME (tyco, _) => (map snd o fst o the)
|
haftmann@41009
|
738 |
(AList.lookup (op =) ((snd o fst o get_type thy) tyco) c)
|
haftmann@41009
|
739 |
| NONE => replicate (length vs) []);
|
haftmann@41009
|
740 |
val the_sort = the o AList.lookup (op =) (map fst vs ~~ sortargs);
|
haftmann@41009
|
741 |
val ty = map_type_tfree (fn (v, _) => TFree (v, the_sort v)) raw_ty
|
haftmann@34895
|
742 |
val chead = build_head thy (c, ty);
|
haftmann@35226
|
743 |
in Equations (Thm.weaken chead Drule.dummy_thm, []) end;
|
haftmann@34891
|
744 |
|
haftmann@34891
|
745 |
fun cert_of_eqns thy c [] = empty_cert thy c
|
haftmann@34895
|
746 |
| cert_of_eqns thy c raw_eqns =
|
haftmann@34313
|
747 |
let
|
haftmann@34895
|
748 |
val eqns = burrow_fst (canonize_thms thy) raw_eqns;
|
haftmann@34895
|
749 |
val _ = map (assert_eqn thy) eqns;
|
haftmann@34891
|
750 |
val (thms, propers) = split_list eqns;
|
haftmann@34895
|
751 |
val _ = map (fn thm => if c = const_eqn thy thm then ()
|
haftmann@34895
|
752 |
else error ("Wrong head of code equation,\nexpected constant "
|
haftmann@34895
|
753 |
^ string_of_const thy c ^ "\n" ^ Display.string_of_thm_global thy thm)) thms;
|
haftmann@34891
|
754 |
fun tvars_of T = rev (Term.add_tvarsT T []);
|
haftmann@34891
|
755 |
val vss = map (tvars_of o snd o head_eqn) thms;
|
haftmann@34891
|
756 |
fun inter_sorts vs =
|
haftmann@34891
|
757 |
fold (curry (Sorts.inter_sort (Sign.classes_of thy)) o snd) vs [];
|
haftmann@34891
|
758 |
val sorts = map_transpose inter_sorts vss;
|
wenzelm@44211
|
759 |
val vts = Name.invent_names Name.context Name.aT sorts;
|
haftmann@41006
|
760 |
val thms' =
|
haftmann@34891
|
761 |
map2 (fn vs => Thm.certify_instantiate (vs ~~ map TFree vts, [])) vss thms;
|
haftmann@41006
|
762 |
val head_thm = Thm.symmetric (Thm.assume (build_head thy (head_eqn (hd thms'))));
|
haftmann@34313
|
763 |
fun head_conv ct = if can Thm.dest_comb ct
|
haftmann@34313
|
764 |
then Conv.fun_conv head_conv ct
|
haftmann@34313
|
765 |
else Conv.rewr_conv head_thm ct;
|
haftmann@34313
|
766 |
val rewrite_head = Conv.fconv_rule (Conv.arg1_conv head_conv);
|
haftmann@41006
|
767 |
val cert_thm = Conjunction.intr_balanced (map rewrite_head thms');
|
haftmann@35226
|
768 |
in Equations (cert_thm, propers) end;
|
haftmann@34313
|
769 |
|
haftmann@35226
|
770 |
fun cert_of_proj thy c tyco =
|
haftmann@34313
|
771 |
let
|
haftmann@41006
|
772 |
val (vs, ((abs, (_, ty)), (rep, _))) = get_abstype_spec thy tyco;
|
haftmann@35226
|
773 |
val _ = if c = rep then () else
|
haftmann@35226
|
774 |
error ("Wrong head of projection,\nexpected constant " ^ string_of_const thy rep);
|
haftmann@35226
|
775 |
in Projection (mk_proj tyco vs ty abs rep, tyco) end;
|
haftmann@34313
|
776 |
|
haftmann@35226
|
777 |
fun cert_of_abs thy tyco c raw_abs_thm =
|
haftmann@35226
|
778 |
let
|
haftmann@35226
|
779 |
val abs_thm = singleton (canonize_thms thy) raw_abs_thm;
|
haftmann@35226
|
780 |
val _ = assert_abs_eqn thy (SOME tyco) abs_thm;
|
haftmann@35226
|
781 |
val _ = if c = const_abs_eqn thy abs_thm then ()
|
haftmann@35226
|
782 |
else error ("Wrong head of abstract code equation,\nexpected constant "
|
haftmann@35226
|
783 |
^ string_of_const thy c ^ "\n" ^ Display.string_of_thm_global thy abs_thm);
|
wenzelm@36638
|
784 |
in Abstract (Thm.legacy_freezeT abs_thm, tyco) end;
|
haftmann@34895
|
785 |
|
haftmann@35226
|
786 |
fun constrain_cert thy sorts (Equations (cert_thm, propers)) =
|
haftmann@35226
|
787 |
let
|
haftmann@35226
|
788 |
val ((vs, _), head) = get_head thy cert_thm;
|
haftmann@35226
|
789 |
val (subst, cert_thm') = cert_thm
|
haftmann@35226
|
790 |
|> Thm.implies_intr head
|
haftmann@35226
|
791 |
|> constrain_thm thy vs sorts;
|
haftmann@35226
|
792 |
val head' = Thm.term_of head
|
haftmann@35226
|
793 |
|> subst
|
haftmann@35226
|
794 |
|> Thm.cterm_of thy;
|
haftmann@35226
|
795 |
val cert_thm'' = cert_thm'
|
haftmann@35226
|
796 |
|> Thm.elim_implies (Thm.assume head');
|
haftmann@35226
|
797 |
in Equations (cert_thm'', propers) end
|
haftmann@35226
|
798 |
| constrain_cert thy _ (cert as Projection _) =
|
haftmann@35226
|
799 |
cert
|
haftmann@35226
|
800 |
| constrain_cert thy sorts (Abstract (abs_thm, tyco)) =
|
haftmann@35226
|
801 |
Abstract (snd (constrain_thm thy (fst (typscheme_abs thy abs_thm)) sorts abs_thm), tyco);
|
haftmann@34895
|
802 |
|
haftmann@50986
|
803 |
fun conclude_cert (Equations (cert_thm, propers)) =
|
haftmann@50986
|
804 |
(Equations (Thm.close_derivation cert_thm, propers))
|
haftmann@50986
|
805 |
| conclude_cert (cert as Projection _) =
|
haftmann@50986
|
806 |
cert
|
haftmann@50986
|
807 |
| conclude_cert (Abstract (abs_thm, tyco)) =
|
haftmann@50986
|
808 |
(Abstract (Thm.close_derivation abs_thm, tyco));
|
haftmann@50986
|
809 |
|
haftmann@35226
|
810 |
fun typscheme_of_cert thy (Equations (cert_thm, _)) =
|
haftmann@35226
|
811 |
fst (get_head thy cert_thm)
|
haftmann@35226
|
812 |
| typscheme_of_cert thy (Projection (proj, _)) =
|
haftmann@35226
|
813 |
typscheme_projection thy proj
|
haftmann@35226
|
814 |
| typscheme_of_cert thy (Abstract (abs_thm, _)) =
|
haftmann@35226
|
815 |
typscheme_abs thy abs_thm;
|
haftmann@34895
|
816 |
|
haftmann@35226
|
817 |
fun typargs_deps_of_cert thy (Equations (cert_thm, propers)) =
|
haftmann@35226
|
818 |
let
|
haftmann@35226
|
819 |
val vs = (fst o fst) (get_head thy cert_thm);
|
haftmann@35226
|
820 |
val equations = if null propers then [] else
|
haftmann@35226
|
821 |
Thm.prop_of cert_thm
|
haftmann@35226
|
822 |
|> Logic.dest_conjunction_balanced (length propers);
|
haftmann@35226
|
823 |
in (vs, fold (add_rhss_of_eqn thy) equations []) end
|
haftmann@41006
|
824 |
| typargs_deps_of_cert thy (Projection (t, _)) =
|
haftmann@35226
|
825 |
(fst (typscheme_projection thy t), add_rhss_of_eqn thy t [])
|
haftmann@35226
|
826 |
| typargs_deps_of_cert thy (Abstract (abs_thm, tyco)) =
|
haftmann@35226
|
827 |
let
|
haftmann@35226
|
828 |
val vs = fst (typscheme_abs thy abs_thm);
|
haftmann@35226
|
829 |
val (_, concrete_thm) = concretify_abs thy tyco abs_thm;
|
wenzelm@46216
|
830 |
in (vs, add_rhss_of_eqn thy (Logic.unvarify_types_global (Thm.prop_of concrete_thm)) []) end;
|
haftmann@35226
|
831 |
|
haftmann@35226
|
832 |
fun equations_of_cert thy (cert as Equations (cert_thm, propers)) =
|
haftmann@35226
|
833 |
let
|
haftmann@35226
|
834 |
val tyscm = typscheme_of_cert thy cert;
|
haftmann@35226
|
835 |
val thms = if null propers then [] else
|
haftmann@35226
|
836 |
cert_thm
|
wenzelm@35624
|
837 |
|> Local_Defs.expand [snd (get_head thy cert_thm)]
|
wenzelm@35845
|
838 |
|> Thm.varifyT_global
|
haftmann@35226
|
839 |
|> Conjunction.elim_balanced (length propers);
|
haftmann@36209
|
840 |
fun abstractions (args, rhs) = (map (rpair NONE) args, (rhs, NONE));
|
haftmann@47384
|
841 |
in (tyscm, map (abstractions o dest_eqn o Thm.prop_of) thms ~~ (map SOME thms ~~ propers)) end
|
haftmann@35226
|
842 |
| equations_of_cert thy (Projection (t, tyco)) =
|
haftmann@35226
|
843 |
let
|
haftmann@35226
|
844 |
val (_, ((abs, _), _)) = get_abstype_spec thy tyco;
|
haftmann@35226
|
845 |
val tyscm = typscheme_projection thy t;
|
wenzelm@46216
|
846 |
val t' = Logic.varify_types_global t;
|
haftmann@36209
|
847 |
fun abstractions (args, rhs) = (map (rpair (SOME abs)) args, (rhs, NONE));
|
haftmann@47384
|
848 |
in (tyscm, [((abstractions o dest_eqn) t', (NONE, true))]) end
|
haftmann@35226
|
849 |
| equations_of_cert thy (Abstract (abs_thm, tyco)) =
|
haftmann@35226
|
850 |
let
|
haftmann@35226
|
851 |
val tyscm = typscheme_abs thy abs_thm;
|
haftmann@35226
|
852 |
val (abs, concrete_thm) = concretify_abs thy tyco abs_thm;
|
haftmann@36209
|
853 |
fun abstractions (args, rhs) = (map (rpair NONE) args, (rhs, (SOME abs)));
|
wenzelm@35845
|
854 |
in
|
haftmann@47384
|
855 |
(tyscm, [((abstractions o dest_eqn o Thm.prop_of) concrete_thm,
|
haftmann@36209
|
856 |
(SOME (Thm.varifyT_global abs_thm), true))])
|
wenzelm@35845
|
857 |
end;
|
haftmann@35226
|
858 |
|
haftmann@35226
|
859 |
fun pretty_cert thy (cert as Equations _) =
|
wenzelm@52822
|
860 |
(map_filter (Option.map (Display.pretty_thm_global thy o Axclass.overload thy) o fst o snd)
|
haftmann@35226
|
861 |
o snd o equations_of_cert thy) cert
|
haftmann@35226
|
862 |
| pretty_cert thy (Projection (t, _)) =
|
wenzelm@46216
|
863 |
[Syntax.pretty_term_global thy (Logic.varify_types_global t)]
|
haftmann@41006
|
864 |
| pretty_cert thy (Abstract (abs_thm, _)) =
|
wenzelm@52822
|
865 |
[(Display.pretty_thm_global thy o Axclass.overload thy o Thm.varifyT_global) abs_thm];
|
haftmann@35226
|
866 |
|
haftmann@35226
|
867 |
fun bare_thms_of_cert thy (cert as Equations _) =
|
haftmann@35226
|
868 |
(map_filter (fn (_, (some_thm, proper)) => if proper then some_thm else NONE)
|
haftmann@35226
|
869 |
o snd o equations_of_cert thy) cert
|
haftmann@35376
|
870 |
| bare_thms_of_cert thy (Projection _) = []
|
haftmann@35376
|
871 |
| bare_thms_of_cert thy (Abstract (abs_thm, tyco)) =
|
wenzelm@35845
|
872 |
[Thm.varifyT_global (snd (concretify_abs thy tyco abs_thm))];
|
haftmann@34895
|
873 |
|
haftmann@34895
|
874 |
end;
|
haftmann@34891
|
875 |
|
haftmann@34313
|
876 |
|
haftmann@35226
|
877 |
(* code certificate access *)
|
haftmann@34313
|
878 |
|
haftmann@35226
|
879 |
fun retrieve_raw thy c =
|
haftmann@35226
|
880 |
Symtab.lookup ((the_functions o the_exec) thy) c
|
haftmann@35226
|
881 |
|> Option.map (snd o fst)
|
haftmann@37435
|
882 |
|> the_default empty_fun_spec
|
haftmann@35226
|
883 |
|
haftmann@49090
|
884 |
fun eqn_conv conv ct =
|
haftmann@49090
|
885 |
let
|
haftmann@49090
|
886 |
fun lhs_conv ct = if can Thm.dest_comb ct
|
haftmann@49090
|
887 |
then Conv.combination_conv lhs_conv conv ct
|
haftmann@49090
|
888 |
else Conv.all_conv ct;
|
haftmann@49090
|
889 |
in Conv.combination_conv (Conv.arg_conv lhs_conv) conv ct end;
|
haftmann@49090
|
890 |
|
haftmann@49090
|
891 |
fun rewrite_eqn thy conv ss =
|
haftmann@49090
|
892 |
let
|
haftmann@49090
|
893 |
val ctxt = Proof_Context.init_global thy;
|
wenzelm@52854
|
894 |
val rewrite = Conv.fconv_rule (conv (Simplifier.rewrite (put_simpset ss ctxt)));
|
haftmann@49090
|
895 |
in singleton (Variable.trade (K (map rewrite)) ctxt) end;
|
haftmann@49090
|
896 |
|
haftmann@49090
|
897 |
fun cert_of_eqns_preprocess thy functrans ss c =
|
haftmann@49090
|
898 |
(map o apfst) (Thm.transfer thy)
|
haftmann@49090
|
899 |
#> perhaps (perhaps_loop (perhaps_apply functrans))
|
haftmann@49090
|
900 |
#> (map o apfst) (rewrite_eqn thy eqn_conv ss)
|
wenzelm@52822
|
901 |
#> (map o apfst) (Axclass.unoverload thy)
|
haftmann@49090
|
902 |
#> cert_of_eqns thy c;
|
haftmann@49090
|
903 |
|
haftmann@49090
|
904 |
fun get_cert thy { functrans, ss } c =
|
haftmann@49090
|
905 |
case retrieve_raw thy c
|
haftmann@49090
|
906 |
of Default (_, eqns_lazy) => Lazy.force eqns_lazy
|
haftmann@49090
|
907 |
|> cert_of_eqns_preprocess thy functrans ss c
|
haftmann@49090
|
908 |
| Eqns eqns => eqns
|
haftmann@49090
|
909 |
|> cert_of_eqns_preprocess thy functrans ss c
|
haftmann@49090
|
910 |
| Proj (_, tyco) =>
|
haftmann@49090
|
911 |
cert_of_proj thy c tyco
|
haftmann@49090
|
912 |
| Abstr (abs_thm, tyco) => abs_thm
|
haftmann@49090
|
913 |
|> Thm.transfer thy
|
haftmann@49090
|
914 |
|> rewrite_eqn thy Conv.arg_conv ss
|
wenzelm@52822
|
915 |
|> Axclass.unoverload thy
|
haftmann@49090
|
916 |
|> cert_of_abs thy tyco c;
|
haftmann@31962
|
917 |
|
haftmann@31962
|
918 |
|
haftmann@31962
|
919 |
(* cases *)
|
haftmann@31156
|
920 |
|
haftmann@31156
|
921 |
fun case_certificate thm =
|
haftmann@31156
|
922 |
let
|
haftmann@31156
|
923 |
val ((head, raw_case_expr), cases) = (apfst Logic.dest_equals
|
haftmann@32640
|
924 |
o apsnd Logic.dest_conjunctions o Logic.dest_implies o Thm.plain_prop_of) thm;
|
haftmann@31156
|
925 |
val _ = case head of Free _ => true
|
haftmann@31156
|
926 |
| Var _ => true
|
haftmann@31156
|
927 |
| _ => raise TERM ("case_cert", []);
|
haftmann@31156
|
928 |
val ([(case_var, _)], case_expr) = Term.strip_abs_eta 1 raw_case_expr;
|
haftmann@31156
|
929 |
val (Const (case_const, _), raw_params) = strip_comb case_expr;
|
haftmann@31156
|
930 |
val n = find_index (fn Free (v, _) => v = case_var | _ => false) raw_params;
|
haftmann@31156
|
931 |
val _ = if n = ~1 then raise TERM ("case_cert", []) else ();
|
haftmann@31156
|
932 |
val params = map (fst o dest_Var) (nth_drop n raw_params);
|
haftmann@31156
|
933 |
fun dest_case t =
|
haftmann@31156
|
934 |
let
|
haftmann@31156
|
935 |
val (head' $ t_co, rhs) = Logic.dest_equals t;
|
haftmann@31156
|
936 |
val _ = if head' = head then () else raise TERM ("case_cert", []);
|
haftmann@31156
|
937 |
val (Const (co, _), args) = strip_comb t_co;
|
haftmann@31156
|
938 |
val (Var (param, _), args') = strip_comb rhs;
|
haftmann@31156
|
939 |
val _ = if args' = args then () else raise TERM ("case_cert", []);
|
haftmann@31156
|
940 |
in (param, co) end;
|
haftmann@31156
|
941 |
fun analyze_cases cases =
|
haftmann@31156
|
942 |
let
|
haftmann@31156
|
943 |
val co_list = fold (AList.update (op =) o dest_case) cases [];
|
Andreas@48305
|
944 |
in map (AList.lookup (op =) co_list) params end;
|
haftmann@31156
|
945 |
fun analyze_let t =
|
haftmann@31156
|
946 |
let
|
haftmann@31156
|
947 |
val (head' $ arg, Var (param', _) $ arg') = Logic.dest_equals t;
|
haftmann@31156
|
948 |
val _ = if head' = head then () else raise TERM ("case_cert", []);
|
haftmann@31156
|
949 |
val _ = if arg' = arg then () else raise TERM ("case_cert", []);
|
haftmann@31156
|
950 |
val _ = if [param'] = params then () else raise TERM ("case_cert", []);
|
haftmann@31156
|
951 |
in [] end;
|
haftmann@31156
|
952 |
fun analyze (cases as [let_case]) =
|
haftmann@31156
|
953 |
(analyze_cases cases handle Bind => analyze_let let_case)
|
haftmann@31156
|
954 |
| analyze cases = analyze_cases cases;
|
haftmann@31156
|
955 |
in (case_const, (n, analyze cases)) end;
|
haftmann@31156
|
956 |
|
haftmann@31156
|
957 |
fun case_cert thm = case_certificate thm
|
haftmann@31156
|
958 |
handle Bind => error "bad case certificate"
|
haftmann@31156
|
959 |
| TERM _ => error "bad case certificate";
|
haftmann@31156
|
960 |
|
haftmann@37413
|
961 |
fun get_case_scheme thy = Option.map fst o Symtab.lookup ((fst o the_cases o the_exec) thy);
|
haftmann@37413
|
962 |
fun get_case_cong thy = Option.map snd o Symtab.lookup ((fst o the_cases o the_exec) thy);
|
haftmann@31156
|
963 |
|
haftmann@31962
|
964 |
val undefineds = Symtab.keys o snd o the_cases o the_exec;
|
haftmann@24219
|
965 |
|
haftmann@25312
|
966 |
|
haftmann@31962
|
967 |
(* diagnostic *)
|
haftmann@24219
|
968 |
|
haftmann@24219
|
969 |
fun print_codesetup thy =
|
haftmann@24219
|
970 |
let
|
wenzelm@43231
|
971 |
val ctxt = Proof_Context.init_global thy;
|
haftmann@24844
|
972 |
val exec = the_exec thy;
|
haftmann@35226
|
973 |
fun pretty_equations const thms =
|
wenzelm@52721
|
974 |
(Pretty.block o Pretty.fbreaks)
|
wenzelm@52721
|
975 |
(Pretty.str (string_of_const thy const) :: map (Display.pretty_thm_item ctxt) thms);
|
wenzelm@52717
|
976 |
fun pretty_function (const, Default (_, eqns_lazy)) =
|
wenzelm@52717
|
977 |
pretty_equations const (map fst (Lazy.force eqns_lazy))
|
haftmann@35226
|
978 |
| pretty_function (const, Eqns eqns) = pretty_equations const (map fst eqns)
|
haftmann@35226
|
979 |
| pretty_function (const, Proj (proj, _)) = Pretty.block
|
haftmann@35226
|
980 |
[Pretty.str (string_of_const thy const), Pretty.fbrk, Syntax.pretty_term ctxt proj]
|
haftmann@35226
|
981 |
| pretty_function (const, Abstr (thm, _)) = pretty_equations const [thm];
|
haftmann@35226
|
982 |
fun pretty_typ (tyco, vs) = Pretty.str
|
haftmann@35226
|
983 |
(string_of_typ thy (Type (tyco, map TFree vs)));
|
haftmann@35226
|
984 |
fun pretty_typspec (typ, (cos, abstract)) = if null cos
|
haftmann@35226
|
985 |
then pretty_typ typ
|
haftmann@35226
|
986 |
else (Pretty.block o Pretty.breaks) (
|
haftmann@35226
|
987 |
pretty_typ typ
|
haftmann@35226
|
988 |
:: Pretty.str "="
|
haftmann@35226
|
989 |
:: (if abstract then [Pretty.str "(abstract)"] else [])
|
haftmann@40969
|
990 |
@ separate (Pretty.str "|") (map (fn (c, (_, [])) => Pretty.str (string_of_const thy c)
|
haftmann@40969
|
991 |
| (c, (_, tys)) =>
|
haftmann@35226
|
992 |
(Pretty.block o Pretty.breaks)
|
haftmann@35226
|
993 |
(Pretty.str (string_of_const thy c)
|
haftmann@35226
|
994 |
:: Pretty.str "of"
|
haftmann@35226
|
995 |
:: map (Pretty.quote o Syntax.pretty_typ_global thy) tys)) cos)
|
haftmann@35226
|
996 |
);
|
haftmann@48420
|
997 |
fun pretty_caseparam NONE = "<ignored>"
|
haftmann@48420
|
998 |
| pretty_caseparam (SOME c) = string_of_const thy c
|
haftmann@37413
|
999 |
fun pretty_case (const, ((_, (_, [])), _)) = Pretty.str (string_of_const thy const)
|
haftmann@37413
|
1000 |
| pretty_case (const, ((_, (_, cos)), _)) = (Pretty.block o Pretty.breaks) [
|
haftmann@34901
|
1001 |
Pretty.str (string_of_const thy const), Pretty.str "with",
|
haftmann@48420
|
1002 |
(Pretty.block o Pretty.commas o map (Pretty.str o pretty_caseparam)) cos];
|
haftmann@35226
|
1003 |
val functions = the_functions exec
|
haftmann@24423
|
1004 |
|> Symtab.dest
|
haftmann@28695
|
1005 |
|> (map o apsnd) (snd o fst)
|
haftmann@24219
|
1006 |
|> sort (string_ord o pairself fst);
|
haftmann@35299
|
1007 |
val datatypes = the_types exec
|
haftmann@24219
|
1008 |
|> Symtab.dest
|
haftmann@35226
|
1009 |
|> map (fn (tyco, (_, (vs, spec)) :: _) =>
|
haftmann@35226
|
1010 |
((tyco, vs), constructors_of spec))
|
haftmann@35226
|
1011 |
|> sort (string_ord o pairself (fst o fst));
|
haftmann@34901
|
1012 |
val cases = Symtab.dest ((fst o the_cases o the_exec) thy);
|
haftmann@34901
|
1013 |
val undefineds = Symtab.keys ((snd o the_cases o the_exec) thy);
|
haftmann@24219
|
1014 |
in
|
haftmann@24219
|
1015 |
(Pretty.writeln o Pretty.chunks) [
|
haftmann@24219
|
1016 |
Pretty.block (
|
haftmann@34901
|
1017 |
Pretty.str "code equations:" :: Pretty.fbrk
|
haftmann@35226
|
1018 |
:: (Pretty.fbreaks o map pretty_function) functions
|
haftmann@24219
|
1019 |
),
|
haftmann@25968
|
1020 |
Pretty.block (
|
haftmann@34901
|
1021 |
Pretty.str "datatypes:" :: Pretty.fbrk
|
haftmann@35226
|
1022 |
:: (Pretty.fbreaks o map pretty_typspec) datatypes
|
haftmann@34901
|
1023 |
),
|
haftmann@34901
|
1024 |
Pretty.block (
|
haftmann@34901
|
1025 |
Pretty.str "cases:" :: Pretty.fbrk
|
haftmann@34901
|
1026 |
:: (Pretty.fbreaks o map pretty_case) cases
|
haftmann@34901
|
1027 |
),
|
haftmann@34901
|
1028 |
Pretty.block (
|
haftmann@34901
|
1029 |
Pretty.str "undefined:" :: Pretty.fbrk
|
haftmann@34901
|
1030 |
:: (Pretty.commas o map (Pretty.str o string_of_const thy)) undefineds
|
haftmann@24219
|
1031 |
)
|
haftmann@24219
|
1032 |
]
|
haftmann@24219
|
1033 |
end;
|
haftmann@24219
|
1034 |
|
haftmann@24219
|
1035 |
|
haftmann@31962
|
1036 |
(** declaring executable ingredients **)
|
haftmann@24219
|
1037 |
|
haftmann@31962
|
1038 |
(* code equations *)
|
haftmann@31962
|
1039 |
|
haftmann@35226
|
1040 |
fun gen_add_eqn default (raw_thm, proper) thy =
|
haftmann@33075
|
1041 |
let
|
haftmann@35226
|
1042 |
val thm = Thm.close_derivation raw_thm;
|
haftmann@35226
|
1043 |
val c = const_eqn thy thm;
|
wenzelm@53925
|
1044 |
fun update_subsume verbose (thm, proper) eqns =
|
haftmann@37435
|
1045 |
let
|
wenzelm@49917
|
1046 |
val args_of = snd o take_prefix is_Var o rev o snd o strip_comb
|
haftmann@40037
|
1047 |
o map_types Type.strip_sorts o fst o Logic.dest_equals o Thm.plain_prop_of;
|
haftmann@37435
|
1048 |
val args = args_of thm;
|
haftmann@37435
|
1049 |
val incr_idx = Logic.incr_indexes ([], Thm.maxidx_of thm + 1);
|
haftmann@40040
|
1050 |
fun matches_args args' =
|
haftmann@40040
|
1051 |
let
|
haftmann@40040
|
1052 |
val k = length args' - length args
|
haftmann@40040
|
1053 |
in if k >= 0
|
haftmann@40040
|
1054 |
then Pattern.matchess thy (args, (map incr_idx o drop k) args')
|
haftmann@40040
|
1055 |
else false
|
haftmann@40040
|
1056 |
end;
|
haftmann@37435
|
1057 |
fun drop (thm', proper') = if (proper orelse not proper')
|
haftmann@37435
|
1058 |
andalso matches_args (args_of thm') then
|
haftmann@53918
|
1059 |
(if verbose then warning ("Code generator: dropping subsumed code equation\n" ^
|
haftmann@53918
|
1060 |
Display.string_of_thm_global thy thm') else (); true)
|
haftmann@37435
|
1061 |
else false;
|
haftmann@37435
|
1062 |
in (thm, proper) :: filter_out drop eqns end;
|
wenzelm@53925
|
1063 |
fun natural_order eqns =
|
wenzelm@53925
|
1064 |
(eqns, Lazy.lazy (fn () => fold (update_subsume false) eqns []))
|
wenzelm@53925
|
1065 |
fun add_eqn' true (Default (eqns, _)) = Default (natural_order ((thm, proper) :: eqns))
|
haftmann@37435
|
1066 |
(*this restores the natural order and drops syntactic redundancies*)
|
haftmann@40040
|
1067 |
| add_eqn' true fun_spec = fun_spec
|
wenzelm@53925
|
1068 |
| add_eqn' false (Eqns eqns) = Eqns (update_subsume true (thm, proper) eqns)
|
haftmann@35226
|
1069 |
| add_eqn' false _ = Eqns [(thm, proper)];
|
haftmann@35226
|
1070 |
in change_fun_spec false c (add_eqn' default) thy end;
|
haftmann@31962
|
1071 |
|
haftmann@53774
|
1072 |
fun gen_add_abs_eqn raw_thm thy =
|
haftmann@53774
|
1073 |
let
|
haftmann@53774
|
1074 |
val (abs_thm, tyco) = apfst Thm.close_derivation raw_thm;
|
haftmann@53774
|
1075 |
val c = const_abs_eqn thy abs_thm;
|
haftmann@53774
|
1076 |
in change_fun_spec false c (K (Abstr (abs_thm, tyco))) thy end;
|
haftmann@53774
|
1077 |
|
haftmann@31962
|
1078 |
fun add_eqn thm thy =
|
haftmann@31962
|
1079 |
gen_add_eqn false (mk_eqn thy (thm, true)) thy;
|
haftmann@31962
|
1080 |
|
haftmann@37400
|
1081 |
fun add_nbe_eqn thm thy =
|
haftmann@37400
|
1082 |
gen_add_eqn false (mk_eqn thy (thm, false)) thy;
|
haftmann@37400
|
1083 |
|
haftmann@31962
|
1084 |
fun add_default_eqn thm thy =
|
haftmann@31962
|
1085 |
case mk_eqn_liberal thy thm
|
haftmann@31962
|
1086 |
of SOME eqn => gen_add_eqn true eqn thy
|
haftmann@31962
|
1087 |
| NONE => thy;
|
haftmann@31962
|
1088 |
|
haftmann@31962
|
1089 |
val add_default_eqn_attribute = Thm.declaration_attribute
|
haftmann@31962
|
1090 |
(fn thm => Context.mapping (add_default_eqn thm) I);
|
haftmann@31962
|
1091 |
val add_default_eqn_attrib = Attrib.internal (K add_default_eqn_attribute);
|
haftmann@31962
|
1092 |
|
haftmann@37400
|
1093 |
fun add_nbe_default_eqn thm thy =
|
haftmann@37400
|
1094 |
gen_add_eqn true (mk_eqn thy (thm, false)) thy;
|
haftmann@37400
|
1095 |
|
haftmann@37400
|
1096 |
val add_nbe_default_eqn_attribute = Thm.declaration_attribute
|
haftmann@37400
|
1097 |
(fn thm => Context.mapping (add_nbe_default_eqn thm) I);
|
haftmann@37400
|
1098 |
val add_nbe_default_eqn_attrib = Attrib.internal (K add_nbe_default_eqn_attribute);
|
haftmann@37400
|
1099 |
|
haftmann@53774
|
1100 |
fun add_abs_eqn raw_thm thy = gen_add_abs_eqn (mk_abs_eqn thy raw_thm) thy;
|
haftmann@53774
|
1101 |
|
haftmann@53774
|
1102 |
fun add_eqn_maybe_abs thm thy =
|
haftmann@53774
|
1103 |
case mk_eqn_maybe_abs thy thm
|
haftmann@53774
|
1104 |
of SOME (eqn, NONE) => gen_add_eqn false eqn thy
|
haftmann@53774
|
1105 |
| SOME ((thm, _), SOME tyco) => gen_add_abs_eqn (thm, tyco) thy
|
haftmann@53774
|
1106 |
| NONE => thy;
|
haftmann@35226
|
1107 |
|
haftmann@31962
|
1108 |
fun del_eqn thm thy = case mk_eqn_liberal thy thm
|
haftmann@35226
|
1109 |
of SOME (thm, _) => let
|
haftmann@41006
|
1110 |
fun del_eqn' (Default _) = empty_fun_spec
|
haftmann@35226
|
1111 |
| del_eqn' (Eqns eqns) =
|
haftmann@35226
|
1112 |
Eqns (filter_out (fn (thm', _) => Thm.eq_thm_prop (thm, thm')) eqns)
|
haftmann@35226
|
1113 |
| del_eqn' spec = spec
|
haftmann@35226
|
1114 |
in change_fun_spec true (const_eqn thy thm) del_eqn' thy end
|
haftmann@31962
|
1115 |
| NONE => thy;
|
haftmann@31962
|
1116 |
|
haftmann@35226
|
1117 |
fun del_eqns c = change_fun_spec true c (K empty_fun_spec);
|
haftmann@34239
|
1118 |
|
haftmann@34239
|
1119 |
|
haftmann@34239
|
1120 |
(* cases *)
|
haftmann@34239
|
1121 |
|
haftmann@41006
|
1122 |
fun case_cong thy case_const (num_args, (pos, _)) =
|
haftmann@37413
|
1123 |
let
|
wenzelm@44208
|
1124 |
val ([x, y], ctxt) = fold_map Name.variant ["A", "A'"] Name.context;
|
wenzelm@44208
|
1125 |
val (zs, _) = fold_map Name.variant (replicate (num_args - 1) "") ctxt;
|
haftmann@37413
|
1126 |
val (ws, vs) = chop pos zs;
|
haftmann@55976
|
1127 |
val T = devarify (Sign.the_const_type thy case_const);
|
wenzelm@41093
|
1128 |
val Ts = binder_types T;
|
haftmann@37413
|
1129 |
val T_cong = nth Ts pos;
|
haftmann@37413
|
1130 |
fun mk_prem z = Free (z, T_cong);
|
haftmann@37413
|
1131 |
fun mk_concl z = list_comb (Const (case_const, T), map2 (curry Free) (ws @ z :: vs) Ts);
|
haftmann@37413
|
1132 |
val (prem, concl) = pairself Logic.mk_equals (pairself mk_prem (x, y), pairself mk_concl (x, y));
|
haftmann@37521
|
1133 |
fun tac { context, prems } = Simplifier.rewrite_goals_tac prems
|
wenzelm@43231
|
1134 |
THEN ALLGOALS (Proof_Context.fact_tac [Drule.reflexive_thm]);
|
wenzelm@52688
|
1135 |
in Goal.prove_sorry_global thy (x :: y :: zs) [prem] concl tac end;
|
haftmann@37413
|
1136 |
|
haftmann@34239
|
1137 |
fun add_case thm thy =
|
haftmann@34239
|
1138 |
let
|
haftmann@44505
|
1139 |
val (case_const, (k, cos)) = case_cert thm;
|
haftmann@48420
|
1140 |
val _ = case (filter_out (is_constr thy) o map_filter I) cos
|
haftmann@34239
|
1141 |
of [] => ()
|
wenzelm@46301
|
1142 |
| cs => error ("Non-constructor(s) in case certificate: " ^ commas_quote cs);
|
haftmann@44505
|
1143 |
val entry = (1 + Int.max (1, length cos), (k, cos));
|
haftmann@44505
|
1144 |
fun register_case cong = (map_cases o apfst)
|
haftmann@44505
|
1145 |
(Symtab.update (case_const, (entry, cong)));
|
haftmann@44505
|
1146 |
fun register_for_constructors (Constructors (cos', cases)) =
|
haftmann@44505
|
1147 |
Constructors (cos',
|
Andreas@48305
|
1148 |
if exists (fn (co, _) => member (op =) cos (SOME co)) cos'
|
haftmann@44505
|
1149 |
then insert (op =) case_const cases
|
haftmann@44505
|
1150 |
else cases)
|
haftmann@44505
|
1151 |
| register_for_constructors (x as Abstractor _) = x;
|
haftmann@44505
|
1152 |
val register_type = (map_typs o Symtab.map)
|
haftmann@44505
|
1153 |
(K ((map o apsnd o apsnd) register_for_constructors));
|
haftmann@37413
|
1154 |
in
|
haftmann@37413
|
1155 |
thy
|
haftmann@37413
|
1156 |
|> `(fn thy => case_cong thy case_const entry)
|
haftmann@44505
|
1157 |
|-> (fn cong => map_exec_purge (register_case cong #> register_type))
|
haftmann@37413
|
1158 |
end;
|
haftmann@34239
|
1159 |
|
haftmann@34239
|
1160 |
fun add_undefined c thy =
|
haftmann@34239
|
1161 |
(map_exec_purge o map_cases o apsnd) (Symtab.update (c, ())) thy;
|
haftmann@34239
|
1162 |
|
haftmann@34239
|
1163 |
|
haftmann@35299
|
1164 |
(* types *)
|
haftmann@34239
|
1165 |
|
haftmann@35299
|
1166 |
fun register_type (tyco, vs_spec) thy =
|
haftmann@34239
|
1167 |
let
|
haftmann@35226
|
1168 |
val (old_constrs, some_old_proj) =
|
haftmann@35299
|
1169 |
case these (Symtab.lookup ((the_types o the_exec) thy) tyco)
|
haftmann@44505
|
1170 |
of (_, (_, Constructors (cos, _))) :: _ => (map fst cos, NONE)
|
haftmann@36209
|
1171 |
| (_, (_, Abstractor ((co, _), (proj, _)))) :: _ => ([co], SOME proj)
|
haftmann@44507
|
1172 |
| [] => ([], NONE);
|
haftmann@44507
|
1173 |
val outdated_funs1 = (map fst o fst o constructors_of o snd) vs_spec;
|
haftmann@44507
|
1174 |
val outdated_funs2 = case some_old_proj
|
haftmann@44507
|
1175 |
of NONE => []
|
haftmann@35226
|
1176 |
| SOME old_proj => Symtab.fold
|
haftmann@36209
|
1177 |
(fn (c, ((_, spec), _)) =>
|
haftmann@36209
|
1178 |
if member (op =) (the_list (associated_abstype spec)) tyco
|
haftmann@35226
|
1179 |
then insert (op =) c else I)
|
haftmann@44508
|
1180 |
((the_functions o the_exec) thy) [old_proj];
|
haftmann@34239
|
1181 |
fun drop_outdated_cases cases = fold Symtab.delete_safe
|
haftmann@37413
|
1182 |
(Symtab.fold (fn (c, ((_, (_, cos)), _)) =>
|
Andreas@48305
|
1183 |
if exists (member (op =) old_constrs) (map_filter I cos)
|
haftmann@34239
|
1184 |
then insert (op =) c else I) cases []) cases;
|
haftmann@34239
|
1185 |
in
|
haftmann@34239
|
1186 |
thy
|
haftmann@44507
|
1187 |
|> fold del_eqns (outdated_funs1 @ outdated_funs2)
|
haftmann@34239
|
1188 |
|> map_exec_purge
|
haftmann@35226
|
1189 |
((map_typs o Symtab.map_default (tyco, [])) (cons (serial (), vs_spec))
|
haftmann@34239
|
1190 |
#> (map_cases o apfst) drop_outdated_cases)
|
haftmann@34239
|
1191 |
end;
|
haftmann@34239
|
1192 |
|
wenzelm@52822
|
1193 |
fun unoverload_const_typ thy (c, ty) = (Axclass.unoverload_const thy (c, ty), ty);
|
haftmann@34239
|
1194 |
|
haftmann@35299
|
1195 |
structure Datatype_Interpretation =
|
haftmann@35299
|
1196 |
Interpretation(type T = string * serial val eq = eq_snd (op =) : T * T -> bool);
|
haftmann@35299
|
1197 |
|
haftmann@35299
|
1198 |
fun datatype_interpretation f = Datatype_Interpretation.interpretation
|
haftmann@40969
|
1199 |
(fn (tyco, _) => fn thy => f (tyco, fst (get_type thy tyco)) thy);
|
haftmann@35226
|
1200 |
|
haftmann@35226
|
1201 |
fun add_datatype proto_constrs thy =
|
haftmann@34239
|
1202 |
let
|
haftmann@35226
|
1203 |
val constrs = map (unoverload_const_typ thy) proto_constrs;
|
haftmann@35226
|
1204 |
val (tyco, (vs, cos)) = constrset_of_consts thy constrs;
|
haftmann@35226
|
1205 |
in
|
haftmann@35226
|
1206 |
thy
|
haftmann@44505
|
1207 |
|> register_type (tyco, (vs, Constructors (cos, [])))
|
haftmann@35299
|
1208 |
|> Datatype_Interpretation.data (tyco, serial ())
|
haftmann@35226
|
1209 |
end;
|
haftmann@34239
|
1210 |
|
haftmann@35226
|
1211 |
fun add_datatype_cmd raw_constrs thy =
|
haftmann@35226
|
1212 |
add_datatype (map (read_bare_const thy) raw_constrs) thy;
|
haftmann@35226
|
1213 |
|
haftmann@35299
|
1214 |
structure Abstype_Interpretation =
|
haftmann@35299
|
1215 |
Interpretation(type T = string * serial val eq = eq_snd (op =) : T * T -> bool);
|
haftmann@35299
|
1216 |
|
haftmann@35299
|
1217 |
fun abstype_interpretation f = Abstype_Interpretation.interpretation
|
haftmann@35299
|
1218 |
(fn (tyco, _) => fn thy => f (tyco, get_abstype_spec thy tyco) thy);
|
haftmann@35299
|
1219 |
|
haftmann@36112
|
1220 |
fun add_abstype proto_thm thy =
|
haftmann@35226
|
1221 |
let
|
haftmann@40969
|
1222 |
val (tyco, (vs, (abs_ty as (abs, (_, ty)), (rep, cert)))) =
|
haftmann@50775
|
1223 |
error_abs_thm (check_abstype_cert thy) thy proto_thm;
|
haftmann@35226
|
1224 |
in
|
haftmann@35226
|
1225 |
thy
|
haftmann@36112
|
1226 |
|> register_type (tyco, (vs, Abstractor (abs_ty, (rep, cert))))
|
wenzelm@46216
|
1227 |
|> change_fun_spec false rep
|
wenzelm@46216
|
1228 |
(K (Proj (Logic.varify_types_global (mk_proj tyco vs ty abs rep), tyco)))
|
haftmann@36112
|
1229 |
|> Abstype_Interpretation.data (tyco, serial ())
|
haftmann@35226
|
1230 |
end;
|
haftmann@35226
|
1231 |
|
haftmann@35226
|
1232 |
|
haftmann@32062
|
1233 |
(* setup *)
|
haftmann@31998
|
1234 |
|
wenzelm@54308
|
1235 |
val _ = Theory.setup
|
haftmann@31962
|
1236 |
(let
|
haftmann@31962
|
1237 |
fun mk_attribute f = Thm.declaration_attribute (fn thm => Context.mapping (f thm) I);
|
haftmann@31998
|
1238 |
val code_attribute_parser =
|
haftmann@31998
|
1239 |
Args.del |-- Scan.succeed (mk_attribute del_eqn)
|
haftmann@53774
|
1240 |
|| Args.$$$ "equation" |-- Scan.succeed (mk_attribute add_eqn)
|
haftmann@31998
|
1241 |
|| Args.$$$ "nbe" |-- Scan.succeed (mk_attribute add_nbe_eqn)
|
haftmann@36112
|
1242 |
|| Args.$$$ "abstype" |-- Scan.succeed (mk_attribute add_abstype)
|
haftmann@35226
|
1243 |
|| Args.$$$ "abstract" |-- Scan.succeed (mk_attribute add_abs_eqn)
|
haftmann@53774
|
1244 |
|| Scan.succeed (mk_attribute add_eqn_maybe_abs);
|
haftmann@31962
|
1245 |
in
|
haftmann@35299
|
1246 |
Datatype_Interpretation.init
|
haftmann@31998
|
1247 |
#> Attrib.setup (Binding.name "code") (Scan.lift code_attribute_parser)
|
haftmann@31998
|
1248 |
"declare theorems for code generation"
|
wenzelm@54308
|
1249 |
end);
|
haftmann@31962
|
1250 |
|
haftmann@24219
|
1251 |
end; (*struct*)
|
haftmann@24219
|
1252 |
|
haftmann@24219
|
1253 |
|
haftmann@35226
|
1254 |
(* type-safe interfaces for data dependent on executable code *)
|
haftmann@24219
|
1255 |
|
haftmann@34160
|
1256 |
functor Code_Data(Data: CODE_DATA_ARGS): CODE_DATA =
|
haftmann@24219
|
1257 |
struct
|
haftmann@24219
|
1258 |
|
haftmann@24219
|
1259 |
type T = Data.T;
|
haftmann@24219
|
1260 |
exception Data of T;
|
haftmann@24219
|
1261 |
fun dest (Data x) = x
|
haftmann@24219
|
1262 |
|
haftmann@34160
|
1263 |
val kind = Code.declare_data (Data Data.empty);
|
haftmann@24219
|
1264 |
|
haftmann@24219
|
1265 |
val data_op = (kind, Data, dest);
|
haftmann@24219
|
1266 |
|
haftmann@39641
|
1267 |
fun change_yield (SOME thy) f = Code.change_yield_data data_op thy f
|
haftmann@39641
|
1268 |
| change_yield NONE f = f Data.empty
|
haftmann@39641
|
1269 |
|
haftmann@39641
|
1270 |
fun change some_thy f = snd (change_yield some_thy (pair () o f));
|
haftmann@24219
|
1271 |
|
haftmann@24219
|
1272 |
end;
|
haftmann@24219
|
1273 |
|
haftmann@28143
|
1274 |
structure Code : CODE = struct open Code; end;
|