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