author | haftmann |
Thu, 14 May 2009 15:09:48 +0200 | |
changeset 31156 | 90fed3d4430f |
parent 31152 | e79d1ff2abc5 |
child 31599 | 97b4d289c646 |
permissions | -rw-r--r-- |
24219 | 1 |
(* Title: Pure/Isar/code.ML |
2 |
Author: Florian Haftmann, TU Muenchen |
|
3 |
||
4 |
Abstract executable content of theory. Management of data dependent on |
|
25485 | 5 |
executable content. Cache assumes non-concurrent processing of a single theory. |
24219 | 6 |
*) |
7 |
||
8 |
signature CODE = |
|
9 |
sig |
|
31156 | 10 |
(*constructor sets*) |
11 |
val constrset_of_consts: theory -> (string * typ) list |
|
12 |
-> string * ((string * sort) list * (string * typ list) list) |
|
13 |
||
14 |
(*typ instantiations*) |
|
15 |
val typscheme: theory -> string * typ -> (string * sort) list * typ |
|
16 |
val inst_thm: theory -> sort Vartab.table -> thm -> thm |
|
17 |
||
18 |
(*constants*) |
|
19 |
val string_of_typ: theory -> typ -> string |
|
20 |
val string_of_const: theory -> string -> string |
|
21 |
val no_args: theory -> string -> int |
|
22 |
val check_const: theory -> term -> string |
|
23 |
val read_bare_const: theory -> string -> string * typ |
|
24 |
val read_const: theory -> string -> string |
|
25 |
||
26 |
(*constant aliasses*) |
|
27 |
val add_const_alias: thm -> theory -> theory |
|
28 |
val triv_classes: theory -> class list |
|
29 |
val resubst_alias: theory -> string -> string |
|
30 |
||
31 |
(*code equations*) |
|
32 |
val mk_eqn: theory -> (string -> bool) -> thm * bool -> thm * bool |
|
33 |
val mk_eqn_liberal: theory -> (string -> bool) -> thm -> (thm * bool) option |
|
34 |
val assert_eqn: theory -> thm * bool -> thm * bool |
|
35 |
val assert_eqns_const: theory -> string |
|
36 |
-> (thm * bool) list -> (thm * bool) list |
|
37 |
val const_typ_eqn: thm -> string * typ |
|
38 |
val const_eqn: theory -> thm -> string |
|
39 |
val typscheme_eqn: theory -> thm -> (string * sort) list * typ |
|
40 |
val expand_eta: theory -> int -> thm -> thm |
|
41 |
val rewrite_eqn: simpset -> thm -> thm |
|
42 |
val rewrite_head: thm list -> thm -> thm |
|
43 |
val norm_args: theory -> thm list -> thm list |
|
44 |
val norm_varnames: theory -> thm list -> thm list |
|
45 |
||
46 |
(*case certificates*) |
|
47 |
val case_cert: thm -> string * (int * string list) |
|
48 |
||
49 |
(*infrastructure*) |
|
50 |
val add_attribute: string * attribute parser -> theory -> theory |
|
51 |
val purge_data: theory -> theory |
|
52 |
||
53 |
(*executable content*) |
|
54 |
val add_datatype: (string * typ) list -> theory -> theory |
|
55 |
val add_datatype_cmd: string list -> theory -> theory |
|
56 |
val type_interpretation: |
|
57 |
(string * ((string * sort) list * (string * typ list) list) |
|
58 |
-> theory -> theory) -> theory -> theory |
|
28368 | 59 |
val add_eqn: thm -> theory -> theory |
31088 | 60 |
val add_nbe_eqn: thm -> theory -> theory |
28368 | 61 |
val add_default_eqn: thm -> theory -> theory |
28703 | 62 |
val add_default_eqn_attribute: attribute |
63 |
val add_default_eqn_attrib: Attrib.src |
|
28368 | 64 |
val del_eqn: thm -> theory -> theory |
65 |
val del_eqns: string -> theory -> theory |
|
28971 | 66 |
val add_eqnl: string * (thm * bool) list lazy -> theory -> theory |
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
67 |
val add_case: thm -> theory -> theory |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
68 |
val add_undefined: string -> theory -> theory |
24219 | 69 |
|
31156 | 70 |
(*data retrieval*) |
24219 | 71 |
val get_datatype: theory -> string -> ((string * sort) list * (string * typ list) list) |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
72 |
val get_datatype_of_constr: theory -> string -> string option |
31156 | 73 |
val default_typscheme: theory -> string -> (string * sort) list * typ |
74 |
val these_eqns: theory -> string -> (thm * bool) list |
|
30023
55954f726043
permissive check for pattern discipline in case schemes
haftmann
parents:
29970
diff
changeset
|
75 |
val get_case_scheme: theory -> string -> (int * (int * string list)) option |
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
76 |
val is_undefined: theory -> string -> bool |
24219 | 77 |
val print_codesetup: theory -> unit |
78 |
end; |
|
79 |
||
80 |
signature CODE_DATA_ARGS = |
|
81 |
sig |
|
82 |
type T |
|
83 |
val empty: T |
|
27609 | 84 |
val purge: theory -> string list -> T -> T |
24219 | 85 |
end; |
86 |
||
87 |
signature CODE_DATA = |
|
88 |
sig |
|
89 |
type T |
|
90 |
val get: theory -> T |
|
91 |
val change: theory -> (T -> T) -> T |
|
92 |
val change_yield: theory -> (T -> 'a * T) -> 'a * T |
|
93 |
end; |
|
94 |
||
95 |
signature PRIVATE_CODE = |
|
96 |
sig |
|
97 |
include CODE |
|
27609 | 98 |
val declare_data: Object.T -> (theory -> string list -> Object.T -> Object.T) |
99 |
-> serial |
|
24219 | 100 |
val get_data: serial * ('a -> Object.T) * (Object.T -> 'a) |
101 |
-> theory -> 'a |
|
102 |
val change_data: serial * ('a -> Object.T) * (Object.T -> 'a) |
|
103 |
-> theory -> ('a -> 'a) -> 'a |
|
104 |
val change_yield_data: serial * ('a -> Object.T) * (Object.T -> 'a) |
|
105 |
-> theory -> ('a -> 'b * 'a) -> 'b * 'a |
|
106 |
end; |
|
107 |
||
108 |
structure Code : PRIVATE_CODE = |
|
109 |
struct |
|
110 |
||
31156 | 111 |
(* auxiliary *) |
112 |
||
113 |
fun string_of_typ thy = setmp show_sorts true (Syntax.string_of_typ_global thy); |
|
114 |
fun string_of_const thy c = case AxClass.inst_of_param thy c |
|
115 |
of SOME (c, tyco) => Sign.extern_const thy c ^ " " ^ enclose "[" "]" (Sign.extern_type thy tyco) |
|
116 |
| NONE => Sign.extern_const thy c; |
|
117 |
||
118 |
fun no_args thy = length o fst o strip_type o Sign.the_const_type thy; |
|
119 |
||
120 |
||
121 |
(* utilities *) |
|
122 |
||
123 |
fun typscheme thy (c, ty) = |
|
124 |
let |
|
125 |
val ty' = Logic.unvarifyT ty; |
|
126 |
fun dest (TFree (v, sort)) = (v, sort) |
|
127 |
| dest ty = error ("Illegal type parameter in type scheme: " ^ Syntax.string_of_typ_global thy ty); |
|
128 |
val vs = map dest (Sign.const_typargs thy (c, ty')); |
|
129 |
in (vs, Type.strip_sorts ty') end; |
|
130 |
||
131 |
fun inst_thm thy tvars' thm = |
|
132 |
let |
|
133 |
val tvars = (Term.add_tvars o Thm.prop_of) thm []; |
|
134 |
val inter_sort = Sorts.inter_sort (Sign.classes_of thy); |
|
135 |
fun mk_inst (tvar as (v, sort)) = case Vartab.lookup tvars' v |
|
136 |
of SOME sort' => SOME (pairself (Thm.ctyp_of thy o TVar) |
|
137 |
(tvar, (v, inter_sort (sort, sort')))) |
|
138 |
| NONE => NONE; |
|
139 |
val insts = map_filter mk_inst tvars; |
|
140 |
in Thm.instantiate (insts, []) thm end; |
|
141 |
||
142 |
fun expand_eta thy k thm = |
|
143 |
let |
|
144 |
val (lhs, rhs) = (Logic.dest_equals o Thm.plain_prop_of) thm; |
|
145 |
val (head, args) = strip_comb lhs; |
|
146 |
val l = if k = ~1 |
|
147 |
then (length o fst o strip_abs) rhs |
|
148 |
else Int.max (0, k - length args); |
|
149 |
val used = Name.make_context (map (fst o fst) (Term.add_vars lhs [])); |
|
150 |
fun get_name _ 0 = pair [] |
|
151 |
| get_name (Abs (v, ty, t)) k = |
|
152 |
Name.variants [v] |
|
153 |
##>> get_name t (k - 1) |
|
154 |
#>> (fn ([v'], vs') => (v', ty) :: vs') |
|
155 |
| get_name t k = |
|
156 |
let |
|
157 |
val (tys, _) = (strip_type o fastype_of) t |
|
158 |
in case tys |
|
159 |
of [] => raise TERM ("expand_eta", [t]) |
|
160 |
| ty :: _ => |
|
161 |
Name.variants [""] |
|
162 |
#-> (fn [v] => get_name (t $ Var ((v, 0), ty)) (k - 1) |
|
163 |
#>> (fn vs' => (v, ty) :: vs')) |
|
164 |
end; |
|
165 |
val (vs, _) = get_name rhs l used; |
|
166 |
fun expand (v, ty) thm = Drule.fun_cong_rule thm |
|
167 |
(Thm.cterm_of thy (Var ((v, 0), ty))); |
|
168 |
in |
|
169 |
thm |
|
170 |
|> fold expand vs |
|
171 |
|> Conv.fconv_rule Drule.beta_eta_conversion |
|
172 |
end; |
|
173 |
||
174 |
fun eqn_conv conv = |
|
175 |
let |
|
176 |
fun lhs_conv ct = if can Thm.dest_comb ct |
|
177 |
then (Conv.combination_conv lhs_conv conv) ct |
|
178 |
else Conv.all_conv ct; |
|
179 |
in Conv.combination_conv (Conv.arg_conv lhs_conv) conv end; |
|
180 |
||
181 |
fun head_conv conv = |
|
182 |
let |
|
183 |
fun lhs_conv ct = if can Thm.dest_comb ct |
|
184 |
then (Conv.fun_conv lhs_conv) ct |
|
185 |
else conv ct; |
|
186 |
in Conv.fun_conv (Conv.arg_conv lhs_conv) end; |
|
187 |
||
188 |
val rewrite_eqn = Conv.fconv_rule o eqn_conv o Simplifier.rewrite; |
|
189 |
val rewrite_head = Conv.fconv_rule o head_conv o MetaSimplifier.rewrite false; |
|
190 |
||
191 |
fun norm_args thy thms = |
|
192 |
let |
|
193 |
val num_args_of = length o snd o strip_comb o fst o Logic.dest_equals; |
|
194 |
val k = fold (curry Int.max o num_args_of o Thm.prop_of) thms 0; |
|
195 |
in |
|
196 |
thms |
|
197 |
|> map (expand_eta thy k) |
|
198 |
|> map (Conv.fconv_rule Drule.beta_eta_conversion) |
|
199 |
end; |
|
200 |
||
201 |
fun canonical_tvars thy thm = |
|
202 |
let |
|
203 |
val ctyp = Thm.ctyp_of thy; |
|
204 |
val purify_tvar = unprefix "'" #> Name.desymbolize false #> prefix "'"; |
|
205 |
fun tvars_subst_for thm = (fold_types o fold_atyps) |
|
206 |
(fn TVar (v_i as (v, _), sort) => let |
|
207 |
val v' = purify_tvar v |
|
208 |
in if v = v' then I |
|
209 |
else insert (op =) (v_i, (v', sort)) end |
|
210 |
| _ => I) (prop_of thm) []; |
|
211 |
fun mk_inst (v_i, (v', sort)) (maxidx, acc) = |
|
212 |
let |
|
213 |
val ty = TVar (v_i, sort) |
|
214 |
in |
|
215 |
(maxidx + 1, (ctyp ty, ctyp (TVar ((v', maxidx), sort))) :: acc) |
|
216 |
end; |
|
217 |
val maxidx = Thm.maxidx_of thm + 1; |
|
218 |
val (_, inst) = fold mk_inst (tvars_subst_for thm) (maxidx + 1, []); |
|
219 |
in Thm.instantiate (inst, []) thm end; |
|
220 |
||
221 |
fun canonical_vars thy thm = |
|
222 |
let |
|
223 |
val cterm = Thm.cterm_of thy; |
|
224 |
val purify_var = Name.desymbolize false; |
|
225 |
fun vars_subst_for thm = fold_aterms |
|
226 |
(fn Var (v_i as (v, _), ty) => let |
|
227 |
val v' = purify_var v |
|
228 |
in if v = v' then I |
|
229 |
else insert (op =) (v_i, (v', ty)) end |
|
230 |
| _ => I) (prop_of thm) []; |
|
231 |
fun mk_inst (v_i as (v, i), (v', ty)) (maxidx, acc) = |
|
232 |
let |
|
233 |
val t = Var (v_i, ty) |
|
234 |
in |
|
235 |
(maxidx + 1, (cterm t, cterm (Var ((v', maxidx), ty))) :: acc) |
|
236 |
end; |
|
237 |
val maxidx = Thm.maxidx_of thm + 1; |
|
238 |
val (_, inst) = fold mk_inst (vars_subst_for thm) (maxidx + 1, []); |
|
239 |
in Thm.instantiate ([], inst) thm end; |
|
240 |
||
241 |
fun canonical_absvars thm = |
|
242 |
let |
|
243 |
val t = Thm.plain_prop_of thm; |
|
244 |
val purify_var = Name.desymbolize false; |
|
245 |
val t' = Term.map_abs_vars purify_var t; |
|
246 |
in Thm.rename_boundvars t t' thm end; |
|
247 |
||
248 |
fun norm_varnames thy thms = |
|
249 |
let |
|
250 |
fun burrow_thms f [] = [] |
|
251 |
| burrow_thms f thms = |
|
252 |
thms |
|
253 |
|> Conjunction.intr_balanced |
|
254 |
|> f |
|
255 |
|> Conjunction.elim_balanced (length thms) |
|
256 |
in |
|
257 |
thms |
|
258 |
|> map (canonical_vars thy) |
|
259 |
|> map canonical_absvars |
|
260 |
|> map Drule.zero_var_indexes |
|
261 |
|> burrow_thms (canonical_tvars thy) |
|
262 |
|> Drule.zero_var_indexes_list |
|
263 |
end; |
|
264 |
||
265 |
||
266 |
(* const aliasses *) |
|
267 |
||
268 |
structure ConstAlias = TheoryDataFun |
|
269 |
( |
|
270 |
type T = ((string * string) * thm) list * class list; |
|
271 |
val empty = ([], []); |
|
272 |
val copy = I; |
|
273 |
val extend = I; |
|
274 |
fun merge _ ((alias1, classes1), (alias2, classes2)) : T = |
|
275 |
(Library.merge (eq_snd Thm.eq_thm_prop) (alias1, alias2), |
|
276 |
Library.merge (op =) (classes1, classes2)); |
|
277 |
); |
|
278 |
||
279 |
fun add_const_alias thm thy = |
|
280 |
let |
|
281 |
val lhs_rhs = case try Logic.dest_equals (Thm.prop_of thm) |
|
282 |
of SOME lhs_rhs => lhs_rhs |
|
283 |
| _ => error ("Not an equation: " ^ Display.string_of_thm thm); |
|
284 |
val c_c' = case try (pairself (AxClass.unoverload_const thy o dest_Const)) lhs_rhs |
|
285 |
of SOME c_c' => c_c' |
|
286 |
| _ => error ("Not an equation with two constants: " ^ Display.string_of_thm thm); |
|
287 |
val some_class = the_list (AxClass.class_of_param thy (snd c_c')); |
|
288 |
in thy |> |
|
289 |
ConstAlias.map (fn (alias, classes) => |
|
290 |
((c_c', thm) :: alias, fold (insert (op =)) some_class classes)) |
|
291 |
end; |
|
292 |
||
293 |
fun resubst_alias thy = |
|
294 |
let |
|
295 |
val alias = fst (ConstAlias.get thy); |
|
296 |
val subst_inst_param = Option.map fst o AxClass.inst_of_param thy; |
|
297 |
fun subst_alias c = |
|
298 |
get_first (fn ((c', c''), _) => if c = c'' then SOME c' else NONE) alias; |
|
299 |
in |
|
300 |
perhaps subst_inst_param |
|
301 |
#> perhaps subst_alias |
|
302 |
end; |
|
303 |
||
304 |
val triv_classes = snd o ConstAlias.get; |
|
305 |
||
306 |
||
307 |
(* reading constants as terms *) |
|
308 |
||
309 |
fun check_bare_const thy t = case try dest_Const t |
|
310 |
of SOME c_ty => c_ty |
|
311 |
| NONE => error ("Not a constant: " ^ Syntax.string_of_term_global thy t); |
|
312 |
||
313 |
fun check_const thy = AxClass.unoverload_const thy o check_bare_const thy; |
|
314 |
||
315 |
fun read_bare_const thy = check_bare_const thy o Syntax.read_term_global thy; |
|
316 |
||
317 |
fun read_const thy = AxClass.unoverload_const thy o read_bare_const thy; |
|
318 |
||
319 |
||
320 |
(* constructor sets *) |
|
321 |
||
322 |
fun constrset_of_consts thy cs = |
|
323 |
let |
|
324 |
val _ = map (fn (c, _) => if (is_some o AxClass.class_of_param thy) c |
|
325 |
then error ("Is a class parameter: " ^ string_of_const thy c) else ()) cs; |
|
326 |
fun no_constr (c, ty) = error ("Not a datatype constructor: " ^ string_of_const thy c |
|
327 |
^ " :: " ^ string_of_typ thy ty); |
|
328 |
fun last_typ c_ty ty = |
|
329 |
let |
|
330 |
val frees = OldTerm.typ_tfrees ty; |
|
331 |
val (tyco, vs) = ((apsnd o map) (dest_TFree) o dest_Type o snd o strip_type) ty |
|
332 |
handle TYPE _ => no_constr c_ty |
|
333 |
val _ = if has_duplicates (eq_fst (op =)) vs then no_constr c_ty else (); |
|
334 |
val _ = if length frees <> length vs then no_constr c_ty else (); |
|
335 |
in (tyco, vs) end; |
|
336 |
fun ty_sorts (c, ty) = |
|
337 |
let |
|
338 |
val ty_decl = (Logic.unvarifyT o Sign.the_const_type thy) c; |
|
339 |
val (tyco, _) = last_typ (c, ty) ty_decl; |
|
340 |
val (_, vs) = last_typ (c, ty) ty; |
|
341 |
in ((tyco, map snd vs), (c, (map fst vs, ty))) end; |
|
342 |
fun add ((tyco', sorts'), c) ((tyco, sorts), cs) = |
|
343 |
let |
|
344 |
val _ = if tyco' <> tyco |
|
345 |
then error "Different type constructors in constructor set" |
|
346 |
else (); |
|
347 |
val sorts'' = map2 (curry (Sorts.inter_sort (Sign.classes_of thy))) sorts' sorts |
|
348 |
in ((tyco, sorts), c :: cs) end; |
|
349 |
fun inst vs' (c, (vs, ty)) = |
|
350 |
let |
|
351 |
val the_v = the o AList.lookup (op =) (vs ~~ vs'); |
|
352 |
val ty' = map_atyps (fn TFree (v, _) => TFree (the_v v)) ty; |
|
353 |
in (c, (fst o strip_type) ty') end; |
|
354 |
val c' :: cs' = map ty_sorts cs; |
|
355 |
val ((tyco, sorts), cs'') = fold add cs' (apsnd single c'); |
|
356 |
val vs = Name.names Name.context Name.aT sorts; |
|
357 |
val cs''' = map (inst vs) cs''; |
|
358 |
in (tyco, (vs, rev cs''')) end; |
|
359 |
||
360 |
||
361 |
(* code equations *) |
|
362 |
||
363 |
exception BAD_THM of string; |
|
364 |
fun bad_thm msg = raise BAD_THM msg; |
|
365 |
fun error_thm f thm = f thm handle BAD_THM msg => error msg; |
|
366 |
fun try_thm f thm = SOME (f thm) handle BAD_THM _ => NONE; |
|
367 |
||
368 |
fun is_linear thm = |
|
369 |
let val (_, args) = (strip_comb o fst o Logic.dest_equals o Thm.plain_prop_of) thm |
|
370 |
in not (has_duplicates (op =) ((fold o fold_aterms) |
|
371 |
(fn Var (v, _) => cons v | _ => I) args [])) end; |
|
372 |
||
373 |
fun gen_assert_eqn thy is_constr_head is_constr_pat (thm, proper) = |
|
374 |
let |
|
375 |
val (lhs, rhs) = (Logic.dest_equals o Thm.plain_prop_of) thm |
|
376 |
handle TERM _ => bad_thm ("Not an equation: " ^ Display.string_of_thm thm) |
|
377 |
| THM _ => bad_thm ("Not an equation: " ^ Display.string_of_thm thm); |
|
378 |
fun vars_of t = fold_aterms (fn Var (v, _) => insert (op =) v |
|
379 |
| Free _ => bad_thm ("Illegal free variable in equation\n" |
|
380 |
^ Display.string_of_thm thm) |
|
381 |
| _ => I) t []; |
|
382 |
fun tvars_of t = fold_term_types (fn _ => |
|
383 |
fold_atyps (fn TVar (v, _) => insert (op =) v |
|
384 |
| TFree _ => bad_thm |
|
385 |
("Illegal free type variable in equation\n" ^ Display.string_of_thm thm))) t []; |
|
386 |
val lhs_vs = vars_of lhs; |
|
387 |
val rhs_vs = vars_of rhs; |
|
388 |
val lhs_tvs = tvars_of lhs; |
|
389 |
val rhs_tvs = tvars_of rhs; |
|
390 |
val _ = if null (subtract (op =) lhs_vs rhs_vs) |
|
391 |
then () |
|
392 |
else bad_thm ("Free variables on right hand side of equation\n" |
|
393 |
^ Display.string_of_thm thm); |
|
394 |
val _ = if null (subtract (op =) lhs_tvs rhs_tvs) |
|
395 |
then () |
|
396 |
else bad_thm ("Free type variables on right hand side of equation\n" |
|
397 |
^ Display.string_of_thm thm) val (head, args) = (strip_comb o fst o Logic.dest_equals o Thm.plain_prop_of) thm; |
|
398 |
val (c, ty) = case head |
|
399 |
of Const (c_ty as (_, ty)) => (AxClass.unoverload_const thy c_ty, ty) |
|
400 |
| _ => bad_thm ("Equation not headed by constant\n" ^ Display.string_of_thm thm); |
|
401 |
fun check _ (Abs _) = bad_thm |
|
402 |
("Abstraction on left hand side of equation\n" |
|
403 |
^ Display.string_of_thm thm) |
|
404 |
| check 0 (Var _) = () |
|
405 |
| check _ (Var _) = bad_thm |
|
406 |
("Variable with application on left hand side of equation\n" |
|
407 |
^ Display.string_of_thm thm) |
|
408 |
| check n (t1 $ t2) = (check (n+1) t1; check 0 t2) |
|
409 |
| check n (Const (c_ty as (c, ty))) = if n = (length o fst o strip_type) ty |
|
410 |
then if not proper orelse is_constr_pat (AxClass.unoverload_const thy c_ty) |
|
411 |
then () |
|
412 |
else bad_thm (quote c ^ " is not a constructor, on left hand side of equation\n" |
|
413 |
^ Display.string_of_thm thm) |
|
414 |
else bad_thm |
|
415 |
("Partially applied constant " ^ quote c ^ " on left hand side of equation\n" |
|
416 |
^ Display.string_of_thm thm); |
|
417 |
val _ = map (check 0) args; |
|
418 |
val _ = if not proper orelse is_linear thm then () |
|
419 |
else bad_thm ("Duplicate variables on left hand side of equation\n" |
|
420 |
^ Display.string_of_thm thm); |
|
421 |
val _ = if (is_none o AxClass.class_of_param thy) c |
|
422 |
then () |
|
423 |
else bad_thm ("Polymorphic constant as head in equation\n" |
|
424 |
^ Display.string_of_thm thm) |
|
425 |
val _ = if not (is_constr_head c) |
|
426 |
then () |
|
427 |
else bad_thm ("Constructor as head in equation\n" |
|
428 |
^ Display.string_of_thm thm) |
|
429 |
val ty_decl = Sign.the_const_type thy c; |
|
430 |
val _ = if Sign.typ_equiv thy (Type.strip_sorts ty_decl, Type.strip_sorts ty) |
|
431 |
then () else bad_thm ("Type\n" ^ string_of_typ thy ty |
|
432 |
^ "\nof equation\n" |
|
433 |
^ Display.string_of_thm thm |
|
434 |
^ "\nis incompatible with declared function type\n" |
|
435 |
^ string_of_typ thy ty_decl) |
|
436 |
in (thm, proper) end; |
|
437 |
||
438 |
fun assert_eqn thy is_constr = error_thm (gen_assert_eqn thy is_constr is_constr); |
|
439 |
||
440 |
val const_typ_eqn = dest_Const o fst o strip_comb o fst o Logic.dest_equals o Thm.plain_prop_of; |
|
441 |
||
442 |
||
443 |
(*those following are permissive wrt. to overloaded constants!*) |
|
444 |
||
445 |
fun mk_eqn thy is_constr_head = error_thm (gen_assert_eqn thy is_constr_head (K true)) o |
|
446 |
apfst (LocalDefs.meta_rewrite_rule (ProofContext.init thy)); |
|
447 |
||
448 |
fun mk_eqn_liberal thy is_constr_head = Option.map (fn (thm, _) => (thm, is_linear thm)) |
|
449 |
o try_thm (gen_assert_eqn thy is_constr_head (K true)) |
|
450 |
o rpair false o LocalDefs.meta_rewrite_rule (ProofContext.init thy); |
|
451 |
||
452 |
fun const_typ_eqn_unoverload thy thm = |
|
453 |
let |
|
454 |
val (c, ty) = const_typ_eqn thm; |
|
455 |
val c' = AxClass.unoverload_const thy (c, ty); |
|
456 |
in (c', ty) end; |
|
457 |
||
458 |
fun typscheme_eqn thy = typscheme thy o const_typ_eqn_unoverload thy; |
|
459 |
fun const_eqn thy = fst o const_typ_eqn_unoverload thy; |
|
460 |
||
461 |
||
462 |
(* case cerificates *) |
|
463 |
||
464 |
fun case_certificate thm = |
|
465 |
let |
|
466 |
val ((head, raw_case_expr), cases) = (apfst Logic.dest_equals |
|
467 |
o apsnd Logic.dest_conjunctions o Logic.dest_implies o Thm.prop_of) thm; |
|
468 |
val _ = case head of Free _ => true |
|
469 |
| Var _ => true |
|
470 |
| _ => raise TERM ("case_cert", []); |
|
471 |
val ([(case_var, _)], case_expr) = Term.strip_abs_eta 1 raw_case_expr; |
|
472 |
val (Const (case_const, _), raw_params) = strip_comb case_expr; |
|
473 |
val n = find_index (fn Free (v, _) => v = case_var | _ => false) raw_params; |
|
474 |
val _ = if n = ~1 then raise TERM ("case_cert", []) else (); |
|
475 |
val params = map (fst o dest_Var) (nth_drop n raw_params); |
|
476 |
fun dest_case t = |
|
477 |
let |
|
478 |
val (head' $ t_co, rhs) = Logic.dest_equals t; |
|
479 |
val _ = if head' = head then () else raise TERM ("case_cert", []); |
|
480 |
val (Const (co, _), args) = strip_comb t_co; |
|
481 |
val (Var (param, _), args') = strip_comb rhs; |
|
482 |
val _ = if args' = args then () else raise TERM ("case_cert", []); |
|
483 |
in (param, co) end; |
|
484 |
fun analyze_cases cases = |
|
485 |
let |
|
486 |
val co_list = fold (AList.update (op =) o dest_case) cases []; |
|
487 |
in map (the o AList.lookup (op =) co_list) params end; |
|
488 |
fun analyze_let t = |
|
489 |
let |
|
490 |
val (head' $ arg, Var (param', _) $ arg') = Logic.dest_equals t; |
|
491 |
val _ = if head' = head then () else raise TERM ("case_cert", []); |
|
492 |
val _ = if arg' = arg then () else raise TERM ("case_cert", []); |
|
493 |
val _ = if [param'] = params then () else raise TERM ("case_cert", []); |
|
494 |
in [] end; |
|
495 |
fun analyze (cases as [let_case]) = |
|
496 |
(analyze_cases cases handle Bind => analyze_let let_case) |
|
497 |
| analyze cases = analyze_cases cases; |
|
498 |
in (case_const, (n, analyze cases)) end; |
|
499 |
||
500 |
fun case_cert thm = case_certificate thm |
|
501 |
handle Bind => error "bad case certificate" |
|
502 |
| TERM _ => error "bad case certificate"; |
|
503 |
||
504 |
||
25312 | 505 |
(** code attributes **) |
506 |
||
507 |
structure CodeAttr = TheoryDataFun ( |
|
30513 | 508 |
type T = (string * attribute parser) list; |
25312 | 509 |
val empty = []; |
510 |
val copy = I; |
|
511 |
val extend = I; |
|
26970 | 512 |
fun merge _ = AList.merge (op = : string * string -> bool) (K true); |
25312 | 513 |
); |
24219 | 514 |
|
25312 | 515 |
fun add_attribute (attr as (name, _)) = |
516 |
let |
|
28562 | 517 |
fun add_parser ("", parser) attrs = attrs |> rev |> AList.update (op =) ("", parser) |> rev |
25312 | 518 |
| add_parser (name, parser) attrs = (name, Args.$$$ name |-- parser) :: attrs; |
28562 | 519 |
in CodeAttr.map (fn attrs => if not (name = "") andalso AList.defined (op =) attrs name |
520 |
then error ("Code attribute " ^ name ^ " already declared") else add_parser attr attrs) |
|
25312 | 521 |
end; |
522 |
||
30722
623d4831c8cf
simplified attribute and method setup: eliminating bottom-up styles makes it easier to keep things in one place, and also SML/NJ happy;
wenzelm
parents:
30528
diff
changeset
|
523 |
val _ = Context.>> (Context.map_theory |
623d4831c8cf
simplified attribute and method setup: eliminating bottom-up styles makes it easier to keep things in one place, and also SML/NJ happy;
wenzelm
parents:
30528
diff
changeset
|
524 |
(Attrib.setup (Binding.name "code") |
623d4831c8cf
simplified attribute and method setup: eliminating bottom-up styles makes it easier to keep things in one place, and also SML/NJ happy;
wenzelm
parents:
30528
diff
changeset
|
525 |
(Scan.peek (fn context => |
623d4831c8cf
simplified attribute and method setup: eliminating bottom-up styles makes it easier to keep things in one place, and also SML/NJ happy;
wenzelm
parents:
30528
diff
changeset
|
526 |
List.foldr op || Scan.fail (map snd (CodeAttr.get (Context.theory_of context))))) |
623d4831c8cf
simplified attribute and method setup: eliminating bottom-up styles makes it easier to keep things in one place, and also SML/NJ happy;
wenzelm
parents:
30528
diff
changeset
|
527 |
"declare theorems for code generation")); |
623d4831c8cf
simplified attribute and method setup: eliminating bottom-up styles makes it easier to keep things in one place, and also SML/NJ happy;
wenzelm
parents:
30528
diff
changeset
|
528 |
|
25312 | 529 |
|
530 |
||
28143 | 531 |
(** logical and syntactical specification of executable code **) |
24219 | 532 |
|
30023
55954f726043
permissive check for pattern discipline in case schemes
haftmann
parents:
29970
diff
changeset
|
533 |
(* code equations *) |
28695 | 534 |
|
28971 | 535 |
type eqns = bool * (thm * bool) list lazy; |
31088 | 536 |
(*default flag, theorems with proper flag (perhaps lazy)*) |
24219 | 537 |
|
28673
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
wenzelm
parents:
28672
diff
changeset
|
538 |
fun pretty_lthms ctxt r = case Lazy.peek r |
28672 | 539 |
of SOME thms => map (ProofContext.pretty_thm ctxt o fst) (Exn.release thms) |
24219 | 540 |
| NONE => [Pretty.str "[...]"]; |
541 |
||
542 |
fun certificate thy f r = |
|
28673
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
wenzelm
parents:
28672
diff
changeset
|
543 |
case Lazy.peek r |
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
wenzelm
parents:
28672
diff
changeset
|
544 |
of SOME thms => (Lazy.value o f thy) (Exn.release thms) |
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
545 |
| NONE => let |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
546 |
val thy_ref = Theory.check_thy thy; |
28673
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
wenzelm
parents:
28672
diff
changeset
|
547 |
in Lazy.lazy (fn () => (f (Theory.deref thy_ref) o Lazy.force) r) end; |
24219 | 548 |
|
31088 | 549 |
fun add_drop_redundant thy (thm, proper) thms = |
24219 | 550 |
let |
31152 | 551 |
val args_of = snd o strip_comb o map_types Type.strip_sorts |
552 |
o fst o Logic.dest_equals o Thm.plain_prop_of; |
|
24219 | 553 |
val args = args_of thm; |
28403 | 554 |
val incr_idx = Logic.incr_indexes ([], Thm.maxidx_of thm + 1); |
28350 | 555 |
fun matches_args args' = length args <= length args' andalso |
28403 | 556 |
Pattern.matchess thy (args, (map incr_idx o curry Library.take (length args)) args'); |
31088 | 557 |
fun drop (thm', proper') = if (proper orelse not proper') |
28359 | 558 |
andalso matches_args (args_of thm') then |
30023
55954f726043
permissive check for pattern discipline in case schemes
haftmann
parents:
29970
diff
changeset
|
559 |
(warning ("Code generator: dropping redundant code equation\n" ^ Display.string_of_thm thm'); true) |
28350 | 560 |
else false; |
31088 | 561 |
in (thm, proper) :: filter_out drop thms end; |
24219 | 562 |
|
28673
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
wenzelm
parents:
28672
diff
changeset
|
563 |
fun add_thm thy _ thm (false, thms) = (false, Lazy.map_force (add_drop_redundant thy thm) thms) |
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
wenzelm
parents:
28672
diff
changeset
|
564 |
| add_thm thy true thm (true, thms) = (true, Lazy.map_force (fn thms => thms @ [thm]) thms) |
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
wenzelm
parents:
28672
diff
changeset
|
565 |
| add_thm thy false thm (true, thms) = (false, Lazy.value [thm]); |
24219 | 566 |
|
28143 | 567 |
fun add_lthms lthms _ = (false, lthms); |
25312 | 568 |
|
28673
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
wenzelm
parents:
28672
diff
changeset
|
569 |
fun del_thm thm = (apsnd o Lazy.map_force) (remove (eq_fst Thm.eq_thm_prop) (thm, true)); |
25312 | 570 |
|
28143 | 571 |
|
572 |
(* specification data *) |
|
573 |
||
24219 | 574 |
datatype spec = Spec of { |
28695 | 575 |
concluded_history: bool, |
576 |
eqns: ((bool * eqns) * (serial * eqns) list) Symtab.table |
|
577 |
(*with explicit history*), |
|
578 |
dtyps: ((serial * ((string * sort) list * (string * typ list) list)) list) Symtab.table |
|
579 |
(*with explicit history*), |
|
30076 | 580 |
cases: (int * (int * string list)) Symtab.table * unit Symtab.table |
24219 | 581 |
}; |
582 |
||
28695 | 583 |
fun mk_spec ((concluded_history, eqns), (dtyps, cases)) = |
584 |
Spec { concluded_history = concluded_history, eqns = eqns, dtyps = dtyps, cases = cases }; |
|
31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31091
diff
changeset
|
585 |
val empty_spec = |
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31091
diff
changeset
|
586 |
mk_spec ((false, Symtab.empty), (Symtab.empty, (Symtab.empty, Symtab.empty))); |
28695 | 587 |
fun map_spec f (Spec { concluded_history = concluded_history, eqns = eqns, |
588 |
dtyps = dtyps, cases = cases }) = |
|
589 |
mk_spec (f ((concluded_history, eqns), (dtyps, cases))); |
|
590 |
fun merge_spec (Spec { concluded_history = _, eqns = eqns1, dtyps = dtyps1, cases = (cases1, undefs1) }, |
|
591 |
Spec { concluded_history = _, eqns = eqns2, dtyps = dtyps2, cases = (cases2, undefs2) }) = |
|
24219 | 592 |
let |
28695 | 593 |
fun merge_eqns ((_, history1), (_, history2)) = |
594 |
let |
|
31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31091
diff
changeset
|
595 |
val raw_history = AList.merge (op = : serial * serial -> bool) |
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31091
diff
changeset
|
596 |
(K true) (history1, history2) |
28695 | 597 |
val filtered_history = filter_out (fst o snd) raw_history |
598 |
val history = if null filtered_history |
|
599 |
then raw_history else filtered_history; |
|
600 |
in ((false, (snd o hd) history), history) end; |
|
601 |
val eqns = Symtab.join (K merge_eqns) (eqns1, eqns2); |
|
602 |
val dtyps = Symtab.join (K (AList.merge (op =) (K true))) (dtyps1, dtyps2); |
|
27609 | 603 |
val cases = (Symtab.merge (K true) (cases1, cases2), |
604 |
Symtab.merge (K true) (undefs1, undefs2)); |
|
28695 | 605 |
in mk_spec ((false, eqns), (dtyps, cases)) end; |
24219 | 606 |
|
25312 | 607 |
|
28143 | 608 |
(* code setup data *) |
609 |
||
31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31091
diff
changeset
|
610 |
fun the_spec (Spec x) = x; |
28368 | 611 |
val the_eqns = #eqns o the_spec; |
24219 | 612 |
val the_dtyps = #dtyps o the_spec; |
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
613 |
val the_cases = #cases o the_spec; |
31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31091
diff
changeset
|
614 |
val map_concluded_history = map_spec o apfst o apfst; |
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31091
diff
changeset
|
615 |
val map_eqns = map_spec o apfst o apsnd; |
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31091
diff
changeset
|
616 |
val map_dtyps = map_spec o apsnd o apfst; |
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31091
diff
changeset
|
617 |
val map_cases = map_spec o apsnd o apsnd; |
24219 | 618 |
|
619 |
||
620 |
(* data slots dependent on executable content *) |
|
621 |
||
622 |
(*private copy avoids potential conflict of table exceptions*) |
|
623 |
structure Datatab = TableFun(type key = int val ord = int_ord); |
|
624 |
||
625 |
local |
|
626 |
||
627 |
type kind = { |
|
628 |
empty: Object.T, |
|
27609 | 629 |
purge: theory -> string list -> Object.T -> Object.T |
24219 | 630 |
}; |
631 |
||
632 |
val kinds = ref (Datatab.empty: kind Datatab.table); |
|
633 |
val kind_keys = ref ([]: serial list); |
|
634 |
||
635 |
fun invoke f k = case Datatab.lookup (! kinds) k |
|
636 |
of SOME kind => f kind |
|
637 |
| NONE => sys_error "Invalid code data identifier"; |
|
638 |
||
639 |
in |
|
640 |
||
27609 | 641 |
fun declare_data empty purge = |
24219 | 642 |
let |
643 |
val k = serial (); |
|
27609 | 644 |
val kind = {empty = empty, purge = purge}; |
24219 | 645 |
val _ = change kinds (Datatab.update (k, kind)); |
646 |
val _ = change kind_keys (cons k); |
|
647 |
in k end; |
|
648 |
||
27609 | 649 |
fun invoke_init k = invoke (fn kind => #empty kind) k; |
24219 | 650 |
|
27609 | 651 |
fun invoke_purge_all thy cs = |
24219 | 652 |
fold (fn k => Datatab.map_entry k |
27609 | 653 |
(invoke (fn kind => #purge kind thy cs) k)) (! kind_keys); |
24219 | 654 |
|
655 |
end; (*local*) |
|
656 |
||
657 |
||
25312 | 658 |
(** theory store **) |
24219 | 659 |
|
660 |
local |
|
661 |
||
662 |
type data = Object.T Datatab.table; |
|
27609 | 663 |
val empty_data = Datatab.empty : data; |
24219 | 664 |
|
31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31091
diff
changeset
|
665 |
structure Code_Data = TheoryDataFun |
24219 | 666 |
( |
31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31091
diff
changeset
|
667 |
type T = spec * data ref; |
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31091
diff
changeset
|
668 |
val empty = (empty_spec, ref empty_data); |
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31091
diff
changeset
|
669 |
fun copy (spec, data) = (spec, ref (! data)); |
24219 | 670 |
val extend = copy; |
31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31091
diff
changeset
|
671 |
fun merge pp ((spec1, data1), (spec2, data2)) = |
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31091
diff
changeset
|
672 |
(merge_spec (spec1, spec2), ref empty_data); |
24219 | 673 |
); |
674 |
||
31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31091
diff
changeset
|
675 |
fun thy_data f thy = f ((snd o Code_Data.get) thy); |
24219 | 676 |
|
677 |
fun get_ensure_init kind data_ref = |
|
678 |
case Datatab.lookup (! data_ref) kind |
|
679 |
of SOME x => x |
|
27609 | 680 |
| NONE => let val y = invoke_init kind |
24219 | 681 |
in (change data_ref (Datatab.update (kind, y)); y) end; |
682 |
||
683 |
in |
|
684 |
||
685 |
(* access to executable content *) |
|
686 |
||
31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31091
diff
changeset
|
687 |
val the_exec = fst o Code_Data.get; |
24219 | 688 |
|
27983 | 689 |
fun complete_class_params thy cs = |
690 |
fold (fn c => case AxClass.inst_of_param thy c |
|
691 |
of NONE => insert (op =) c |
|
692 |
| SOME (c', _) => insert (op =) c' #> insert (op =) c) cs []; |
|
693 |
||
24219 | 694 |
fun map_exec_purge touched f thy = |
31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31091
diff
changeset
|
695 |
Code_Data.map (fn (exec, data) => (f exec, ref (case touched |
27983 | 696 |
of SOME cs => invoke_purge_all thy (complete_class_params thy cs) (! data) |
27609 | 697 |
| NONE => empty_data))) thy; |
24219 | 698 |
|
31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31091
diff
changeset
|
699 |
val purge_data = (Code_Data.map o apsnd) (K (ref empty_data)); |
27675 | 700 |
|
24219 | 701 |
|
28695 | 702 |
(* tackling equation history *) |
703 |
||
704 |
fun get_eqns thy c = |
|
705 |
Symtab.lookup ((the_eqns o the_exec) thy) c |
|
706 |
|> Option.map (Lazy.force o snd o snd o fst) |
|
707 |
|> these; |
|
708 |
||
709 |
fun continue_history thy = if (#concluded_history o the_spec o the_exec) thy |
|
710 |
then thy |
|
31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31091
diff
changeset
|
711 |
|> (Code_Data.map o apfst o map_concluded_history) (K false) |
28695 | 712 |
|> SOME |
713 |
else NONE; |
|
714 |
||
715 |
fun conclude_history thy = if (#concluded_history o the_spec o the_exec) thy |
|
716 |
then NONE |
|
717 |
else thy |
|
31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31091
diff
changeset
|
718 |
|> (Code_Data.map o apfst) |
28695 | 719 |
((map_eqns o Symtab.map) (fn ((changed, current), history) => |
720 |
((false, current), |
|
721 |
if changed then (serial (), current) :: history else history)) |
|
722 |
#> map_concluded_history (K true)) |
|
723 |
|> SOME; |
|
724 |
||
31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31091
diff
changeset
|
725 |
val _ = Context.>> (Context.map_theory (Code_Data.init |
28695 | 726 |
#> Theory.at_begin continue_history |
727 |
#> Theory.at_end conclude_history)); |
|
728 |
||
729 |
||
24219 | 730 |
(* access to data dependent on abstract executable content *) |
731 |
||
732 |
fun get_data (kind, _, dest) = thy_data (get_ensure_init kind #> dest); |
|
733 |
||
734 |
fun change_data (kind, mk, dest) = |
|
735 |
let |
|
736 |
fun chnge data_ref f = |
|
737 |
let |
|
738 |
val data = get_ensure_init kind data_ref; |
|
739 |
val data' = f (dest data); |
|
740 |
in (change data_ref (Datatab.update (kind, mk data')); data') end; |
|
741 |
in thy_data chnge end; |
|
742 |
||
743 |
fun change_yield_data (kind, mk, dest) = |
|
744 |
let |
|
745 |
fun chnge data_ref f = |
|
746 |
let |
|
747 |
val data = get_ensure_init kind data_ref; |
|
748 |
val (x, data') = f (dest data); |
|
749 |
in (x, (change data_ref (Datatab.update (kind, mk data')); data')) end; |
|
750 |
in thy_data chnge end; |
|
751 |
||
752 |
end; (*local*) |
|
753 |
||
754 |
fun print_codesetup thy = |
|
755 |
let |
|
756 |
val ctxt = ProofContext.init thy; |
|
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
757 |
val exec = the_exec thy; |
28368 | 758 |
fun pretty_eqn (s, (_, lthms)) = |
24219 | 759 |
(Pretty.block o Pretty.fbreaks) ( |
28143 | 760 |
Pretty.str s :: pretty_lthms ctxt lthms |
24219 | 761 |
); |
762 |
fun pretty_dtyp (s, []) = |
|
763 |
Pretty.str s |
|
764 |
| pretty_dtyp (s, cos) = |
|
765 |
(Pretty.block o Pretty.breaks) ( |
|
766 |
Pretty.str s |
|
767 |
:: Pretty.str "=" |
|
31156 | 768 |
:: separate (Pretty.str "|") (map (fn (c, []) => Pretty.str (string_of_const thy c) |
24219 | 769 |
| (c, tys) => |
770 |
(Pretty.block o Pretty.breaks) |
|
31156 | 771 |
(Pretty.str (string_of_const thy c) |
26947
133905a0c493
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26928
diff
changeset
|
772 |
:: Pretty.str "of" |
133905a0c493
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26928
diff
changeset
|
773 |
:: map (Pretty.quote o Syntax.pretty_typ_global thy) tys)) cos) |
24219 | 774 |
); |
28368 | 775 |
val eqns = the_eqns exec |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
776 |
|> Symtab.dest |
31156 | 777 |
|> (map o apfst) (string_of_const thy) |
28695 | 778 |
|> (map o apsnd) (snd o fst) |
24219 | 779 |
|> sort (string_ord o pairself fst); |
780 |
val dtyps = the_dtyps exec |
|
781 |
|> Symtab.dest |
|
28695 | 782 |
|> map (fn (dtco, (_, (vs, cos)) :: _) => |
26947
133905a0c493
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26928
diff
changeset
|
783 |
(Syntax.string_of_typ_global thy (Type (dtco, map TFree vs)), cos)) |
24219 | 784 |
|> sort (string_ord o pairself fst) |
785 |
in |
|
786 |
(Pretty.writeln o Pretty.chunks) [ |
|
787 |
Pretty.block ( |
|
30023
55954f726043
permissive check for pattern discipline in case schemes
haftmann
parents:
29970
diff
changeset
|
788 |
Pretty.str "code equations:" |
24219 | 789 |
:: Pretty.fbrk |
28368 | 790 |
:: (Pretty.fbreaks o map pretty_eqn) eqns |
24219 | 791 |
), |
25968 | 792 |
Pretty.block ( |
24219 | 793 |
Pretty.str "datatypes:" |
794 |
:: Pretty.fbrk |
|
795 |
:: (Pretty.fbreaks o map pretty_dtyp) dtyps |
|
796 |
) |
|
797 |
] |
|
798 |
end; |
|
799 |
||
800 |
||
801 |
(** theorem transformation and certification **) |
|
802 |
||
28423 | 803 |
fun common_typ_eqns thy [] = [] |
804 |
| common_typ_eqns thy [thm] = [thm] |
|
805 |
| common_typ_eqns thy (thms as thm :: _) = (*FIXME is too general*) |
|
24219 | 806 |
let |
807 |
fun incr_thm thm max = |
|
808 |
let |
|
809 |
val thm' = incr_indexes max thm; |
|
810 |
val max' = Thm.maxidx_of thm' + 1; |
|
811 |
in (thm', max') end; |
|
812 |
val (thms', maxidx) = fold_map incr_thm thms 0; |
|
31156 | 813 |
val ty1 :: tys = map (snd o const_typ_eqn) thms'; |
24219 | 814 |
fun unify ty env = Sign.typ_unify thy (ty1, ty) env |
815 |
handle Type.TUNIFY => |
|
30023
55954f726043
permissive check for pattern discipline in case schemes
haftmann
parents:
29970
diff
changeset
|
816 |
error ("Type unificaton failed, while unifying code equations\n" |
24219 | 817 |
^ (cat_lines o map Display.string_of_thm) thms |
818 |
^ "\nwith types\n" |
|
31156 | 819 |
^ (cat_lines o map (string_of_typ thy)) (ty1 :: tys)); |
24219 | 820 |
val (env, _) = fold unify tys (Vartab.empty, maxidx) |
821 |
val instT = Vartab.fold (fn (x_i, (sort, ty)) => |
|
822 |
cons (Thm.ctyp_of thy (TVar (x_i, sort)), Thm.ctyp_of thy ty)) env []; |
|
823 |
in map (Thm.instantiate (instT, [])) thms' end; |
|
824 |
||
825 |
||
826 |
(** interfaces and attributes **) |
|
827 |
||
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
828 |
fun get_datatype thy tyco = |
28695 | 829 |
case these (Symtab.lookup ((the_dtyps o the_exec) thy) tyco) |
830 |
of (_, spec) :: _ => spec |
|
831 |
| [] => Sign.arity_number thy tyco |
|
24848 | 832 |
|> Name.invents Name.context Name.aT |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
833 |
|> map (rpair []) |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
834 |
|> rpair []; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
835 |
|
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
836 |
fun get_datatype_of_constr thy c = |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
837 |
case (snd o strip_type o Sign.the_const_type thy) c |
28695 | 838 |
of Type (tyco, _) => if member (op =) ((map fst o snd o get_datatype thy) tyco) c |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
839 |
then SOME tyco else NONE |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
840 |
| _ => NONE; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
841 |
|
31088 | 842 |
fun is_constr thy = is_some o get_datatype_of_constr thy; |
843 |
||
31156 | 844 |
val assert_eqn = fn thy => assert_eqn thy (is_constr thy); |
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28703
diff
changeset
|
845 |
|
31090
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31088
diff
changeset
|
846 |
fun assert_eqns_const thy c eqns = |
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28703
diff
changeset
|
847 |
let |
31156 | 848 |
fun cert (eqn as (thm, _)) = if c = const_eqn thy thm |
30023
55954f726043
permissive check for pattern discipline in case schemes
haftmann
parents:
29970
diff
changeset
|
849 |
then eqn else error ("Wrong head of code equation,\nexpected constant " |
31156 | 850 |
^ string_of_const thy c ^ "\n" ^ Display.string_of_thm thm) |
31090
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31088
diff
changeset
|
851 |
in map (cert o assert_eqn thy) eqns end; |
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28703
diff
changeset
|
852 |
|
28695 | 853 |
fun change_eqns delete c f = (map_exec_purge (SOME [c]) o map_eqns |
854 |
o (if delete then Symtab.map_entry c else Symtab.map_default (c, ((false, (true, Lazy.value [])), []))) |
|
855 |
o apfst) (fn (_, eqns) => (true, f eqns)); |
|
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
856 |
|
31090
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31088
diff
changeset
|
857 |
fun gen_add_eqn default (eqn as (thm, _)) thy = |
31156 | 858 |
let val c = const_eqn thy thm |
31090
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31088
diff
changeset
|
859 |
in change_eqns false c (add_thm thy default eqn) thy end; |
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31088
diff
changeset
|
860 |
|
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31088
diff
changeset
|
861 |
fun add_eqn thm thy = |
31156 | 862 |
gen_add_eqn false (mk_eqn thy (is_constr thy) (thm, true)) thy; |
31090
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31088
diff
changeset
|
863 |
|
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31088
diff
changeset
|
864 |
fun add_default_eqn thm thy = |
31156 | 865 |
case mk_eqn_liberal thy (is_constr thy) thm |
31090
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31088
diff
changeset
|
866 |
of SOME eqn => gen_add_eqn true eqn thy |
24624
b8383b1bbae3
distinction between regular and default code theorems
haftmann
parents:
24585
diff
changeset
|
867 |
| NONE => thy; |
b8383b1bbae3
distinction between regular and default code theorems
haftmann
parents:
24585
diff
changeset
|
868 |
|
31090
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31088
diff
changeset
|
869 |
fun add_nbe_eqn thm thy = |
31156 | 870 |
gen_add_eqn false (mk_eqn thy (is_constr thy) (thm, false)) thy; |
26021 | 871 |
|
28368 | 872 |
fun add_eqnl (c, lthms) thy = |
24219 | 873 |
let |
31090
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31088
diff
changeset
|
874 |
val lthms' = certificate thy (fn thy => assert_eqns_const thy c) lthms; |
28695 | 875 |
in change_eqns false c (add_lthms lthms') thy end; |
24219 | 876 |
|
28703 | 877 |
val add_default_eqn_attribute = Thm.declaration_attribute |
878 |
(fn thm => Context.mapping (add_default_eqn thm) I); |
|
879 |
val add_default_eqn_attrib = Attrib.internal (K add_default_eqn_attribute); |
|
24219 | 880 |
|
31156 | 881 |
fun del_eqn thm thy = case mk_eqn_liberal thy (is_constr thy) thm |
882 |
of SOME (thm, _) => change_eqns true (const_eqn thy thm) (del_thm thm) thy |
|
28695 | 883 |
| NONE => thy; |
884 |
||
885 |
fun del_eqns c = change_eqns true c (K (false, Lazy.value [])); |
|
886 |
||
30076 | 887 |
fun get_case_scheme thy = Symtab.lookup ((fst o the_cases o the_exec) thy); |
28695 | 888 |
|
889 |
val is_undefined = Symtab.defined o snd o the_cases o the_exec; |
|
890 |
||
25485 | 891 |
structure TypeInterpretation = InterpretationFun(type T = string * serial val eq = eq_snd (op =) : T * T -> bool); |
25462 | 892 |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
893 |
fun add_datatype raw_cs thy = |
24219 | 894 |
let |
25597
34860182b250
moved instance parameter management from class.ML to axclass.ML
haftmann
parents:
25501
diff
changeset
|
895 |
val cs = map (fn c_ty as (_, ty) => (AxClass.unoverload_const thy c_ty, ty)) raw_cs; |
31156 | 896 |
val (tyco, vs_cos) = constrset_of_consts thy cs; |
30076 | 897 |
val old_cs = (map fst o snd o get_datatype thy) tyco; |
898 |
fun drop_outdated_cases cases = fold Symtab.delete_safe |
|
899 |
(Symtab.fold (fn (c, (_, (_, cos))) => |
|
900 |
if exists (member (op =) old_cs) cos |
|
901 |
then insert (op =) c else I) cases []) cases; |
|
24219 | 902 |
in |
903 |
thy |
|
31091
8316d22f10f5
corrected deletetion of code equations for constructors
haftmann
parents:
31090
diff
changeset
|
904 |
|> fold (del_eqns o fst) cs |
28703 | 905 |
|> map_exec_purge NONE |
28695 | 906 |
((map_dtyps o Symtab.map_default (tyco, [])) (cons (serial (), vs_cos)) |
30076 | 907 |
#> (map_cases o apfst) drop_outdated_cases) |
25485 | 908 |
|> TypeInterpretation.data (tyco, serial ()) |
24219 | 909 |
end; |
910 |
||
25485 | 911 |
fun type_interpretation f = TypeInterpretation.interpretation |
912 |
(fn (tyco, _) => fn thy => f (tyco, get_datatype thy tyco) thy); |
|
913 |
||
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
914 |
fun add_datatype_cmd raw_cs thy = |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
915 |
let |
31156 | 916 |
val cs = map (read_bare_const thy) raw_cs; |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
917 |
in add_datatype cs thy end; |
24219 | 918 |
|
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
919 |
fun add_case thm thy = |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
920 |
let |
31156 | 921 |
val (c, (k, case_pats)) = case_cert thm; |
31088 | 922 |
val _ = case filter_out (is_constr thy) case_pats |
30076 | 923 |
of [] => () |
924 |
| cs => error ("Non-constructor(s) in case certificate: " ^ commas (map quote cs)); |
|
925 |
val entry = (1 + Int.max (1, length case_pats), (k, case_pats)) |
|
926 |
in (map_exec_purge (SOME [c]) o map_cases o apfst) (Symtab.update (c, entry)) thy end; |
|
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
927 |
|
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
928 |
fun add_undefined c thy = |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
929 |
(map_exec_purge (SOME [c]) o map_cases o apsnd) (Symtab.update (c, ())) thy; |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
930 |
|
26463 | 931 |
val _ = Context.>> (Context.map_theory |
24219 | 932 |
(let |
933 |
fun mk_attribute f = Thm.declaration_attribute (fn thm => Context.mapping (f thm) I); |
|
934 |
fun add_simple_attribute (name, f) = |
|
935 |
add_attribute (name, Scan.succeed (mk_attribute f)); |
|
936 |
fun add_del_attribute (name, (add, del)) = |
|
937 |
add_attribute (name, Args.del |-- Scan.succeed (mk_attribute del) |
|
938 |
|| Scan.succeed (mk_attribute add)) |
|
939 |
in |
|
25462 | 940 |
TypeInterpretation.init |
28562 | 941 |
#> add_del_attribute ("", (add_eqn, del_eqn)) |
31088 | 942 |
#> add_simple_attribute ("nbe", add_nbe_eqn) |
26463 | 943 |
end)); |
24219 | 944 |
|
28423 | 945 |
fun these_eqns thy c = |
31088 | 946 |
get_eqns thy c |
947 |
|> (map o apfst) (Thm.transfer thy) |
|
31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31091
diff
changeset
|
948 |
|> burrow_fst (common_typ_eqns thy); |
24219 | 949 |
|
28525 | 950 |
fun default_typscheme thy c = |
951 |
let |
|
31156 | 952 |
fun the_const_typscheme c = (curry (typscheme thy) c o snd o dest_Const |
30023
55954f726043
permissive check for pattern discipline in case schemes
haftmann
parents:
29970
diff
changeset
|
953 |
o TermSubst.zero_var_indexes o curry Const "" o Sign.the_const_type thy) c; |
55954f726043
permissive check for pattern discipline in case schemes
haftmann
parents:
29970
diff
changeset
|
954 |
fun strip_sorts (vs, ty) = (map (fn (v, _) => (v, [])) vs, ty); |
28423 | 955 |
in case AxClass.class_of_param thy c |
30023
55954f726043
permissive check for pattern discipline in case schemes
haftmann
parents:
29970
diff
changeset
|
956 |
of SOME class => ([(Name.aT, [class])], snd (the_const_typscheme c)) |
31088 | 957 |
| NONE => if is_constr thy c |
30023
55954f726043
permissive check for pattern discipline in case schemes
haftmann
parents:
29970
diff
changeset
|
958 |
then strip_sorts (the_const_typscheme c) |
55954f726043
permissive check for pattern discipline in case schemes
haftmann
parents:
29970
diff
changeset
|
959 |
else case get_eqns thy c |
31156 | 960 |
of (thm, _) :: _ => (typscheme_eqn thy o Drule.zero_var_indexes) thm |
30023
55954f726043
permissive check for pattern discipline in case schemes
haftmann
parents:
29970
diff
changeset
|
961 |
| [] => strip_sorts (the_const_typscheme c) end; |
24219 | 962 |
|
963 |
end; (*struct*) |
|
964 |
||
965 |
||
966 |
(** type-safe interfaces for data depedent on executable content **) |
|
967 |
||
968 |
functor CodeDataFun(Data: CODE_DATA_ARGS): CODE_DATA = |
|
969 |
struct |
|
970 |
||
971 |
type T = Data.T; |
|
972 |
exception Data of T; |
|
973 |
fun dest (Data x) = x |
|
974 |
||
975 |
val kind = Code.declare_data (Data Data.empty) |
|
27609 | 976 |
(fn thy => fn cs => fn Data x => Data (Data.purge thy cs x)); |
24219 | 977 |
|
978 |
val data_op = (kind, Data, dest); |
|
979 |
||
980 |
val get = Code.get_data data_op; |
|
981 |
val change = Code.change_data data_op; |
|
982 |
fun change_yield thy = Code.change_yield_data data_op thy; |
|
983 |
||
984 |
end; |
|
985 |
||
28143 | 986 |
structure Code : CODE = struct open Code; end; |