haftmann@24219
|
1 |
(* Title: Pure/Isar/code.ML
|
haftmann@24219
|
2 |
ID: $Id$
|
haftmann@24219
|
3 |
Author: Florian Haftmann, TU Muenchen
|
haftmann@24219
|
4 |
|
haftmann@24219
|
5 |
Abstract executable content of theory. Management of data dependent on
|
haftmann@24219
|
6 |
executable content.
|
haftmann@24219
|
7 |
*)
|
haftmann@24219
|
8 |
|
haftmann@24219
|
9 |
signature CODE =
|
haftmann@24219
|
10 |
sig
|
haftmann@24624
|
11 |
val add_func: thm -> theory -> theory
|
haftmann@24624
|
12 |
val add_liberal_func: thm -> theory -> theory
|
haftmann@24624
|
13 |
val add_default_func: thm -> theory -> theory
|
haftmann@24624
|
14 |
val add_default_func_attr: Attrib.src
|
haftmann@24219
|
15 |
val del_func: thm -> theory -> theory
|
haftmann@24423
|
16 |
val add_funcl: string * thm list Susp.T -> theory -> theory
|
haftmann@24219
|
17 |
val add_inline: thm -> theory -> theory
|
haftmann@24219
|
18 |
val del_inline: thm -> theory -> theory
|
haftmann@24219
|
19 |
val add_inline_proc: string * (theory -> cterm list -> thm list) -> theory -> theory
|
haftmann@24219
|
20 |
val del_inline_proc: string -> theory -> theory
|
haftmann@24219
|
21 |
val add_preproc: string * (theory -> thm list -> thm list) -> theory -> theory
|
haftmann@24219
|
22 |
val del_preproc: string -> theory -> theory
|
haftmann@24219
|
23 |
val add_post: thm -> theory -> theory
|
haftmann@24219
|
24 |
val del_post: thm -> theory -> theory
|
haftmann@24423
|
25 |
val add_datatype: (string * typ) list -> theory -> theory
|
haftmann@24423
|
26 |
val add_datatype_cmd: string list -> theory -> theory
|
haftmann@24219
|
27 |
|
haftmann@24219
|
28 |
val coregular_algebra: theory -> Sorts.algebra
|
haftmann@24219
|
29 |
val operational_algebra: theory -> (sort -> sort) * Sorts.algebra
|
haftmann@24423
|
30 |
val these_funcs: theory -> string -> thm list
|
haftmann@24219
|
31 |
val get_datatype: theory -> string -> ((string * sort) list * (string * typ list) list)
|
haftmann@24423
|
32 |
val get_datatype_of_constr: theory -> string -> string option
|
haftmann@24423
|
33 |
val default_typ: theory -> string -> typ
|
haftmann@24219
|
34 |
|
haftmann@24219
|
35 |
val preprocess_conv: cterm -> thm
|
haftmann@24219
|
36 |
val postprocess_conv: cterm -> thm
|
haftmann@24219
|
37 |
|
haftmann@24219
|
38 |
val add_attribute: string * (Args.T list -> attribute * Args.T list) -> theory -> theory
|
haftmann@24219
|
39 |
|
haftmann@24219
|
40 |
val print_codesetup: theory -> unit
|
haftmann@24219
|
41 |
end;
|
haftmann@24219
|
42 |
|
haftmann@24219
|
43 |
signature CODE_DATA_ARGS =
|
haftmann@24219
|
44 |
sig
|
haftmann@24219
|
45 |
type T
|
haftmann@24219
|
46 |
val empty: T
|
haftmann@24219
|
47 |
val merge: Pretty.pp -> T * T -> T
|
haftmann@24423
|
48 |
val purge: theory option -> string list option -> T -> T
|
haftmann@24219
|
49 |
end;
|
haftmann@24219
|
50 |
|
haftmann@24219
|
51 |
signature CODE_DATA =
|
haftmann@24219
|
52 |
sig
|
haftmann@24219
|
53 |
type T
|
haftmann@24219
|
54 |
val get: theory -> T
|
haftmann@24219
|
55 |
val change: theory -> (T -> T) -> T
|
haftmann@24219
|
56 |
val change_yield: theory -> (T -> 'a * T) -> 'a * T
|
haftmann@24219
|
57 |
end;
|
haftmann@24219
|
58 |
|
haftmann@24219
|
59 |
signature PRIVATE_CODE =
|
haftmann@24219
|
60 |
sig
|
haftmann@24219
|
61 |
include CODE
|
haftmann@24219
|
62 |
val declare_data: Object.T -> (Pretty.pp -> Object.T * Object.T -> Object.T)
|
haftmann@24423
|
63 |
-> (theory option -> string list option -> Object.T -> Object.T) -> serial
|
haftmann@24219
|
64 |
val get_data: serial * ('a -> Object.T) * (Object.T -> 'a)
|
haftmann@24219
|
65 |
-> theory -> 'a
|
haftmann@24219
|
66 |
val change_data: serial * ('a -> Object.T) * (Object.T -> 'a)
|
haftmann@24219
|
67 |
-> theory -> ('a -> 'a) -> 'a
|
haftmann@24219
|
68 |
val change_yield_data: serial * ('a -> Object.T) * (Object.T -> 'a)
|
haftmann@24219
|
69 |
-> theory -> ('a -> 'b * 'a) -> 'b * 'a
|
haftmann@24219
|
70 |
end;
|
haftmann@24219
|
71 |
|
haftmann@24219
|
72 |
structure Code : PRIVATE_CODE =
|
haftmann@24219
|
73 |
struct
|
haftmann@24219
|
74 |
|
haftmann@24219
|
75 |
(** preliminaries **)
|
haftmann@24219
|
76 |
|
haftmann@24219
|
77 |
(* certificate theorems *)
|
haftmann@24219
|
78 |
|
haftmann@24219
|
79 |
fun string_of_lthms r = case Susp.peek r
|
haftmann@24219
|
80 |
of SOME thms => (map string_of_thm o rev) thms
|
haftmann@24219
|
81 |
| NONE => ["[...]"];
|
haftmann@24219
|
82 |
|
haftmann@24219
|
83 |
fun pretty_lthms ctxt r = case Susp.peek r
|
haftmann@24219
|
84 |
of SOME thms => map (ProofContext.pretty_thm ctxt) thms
|
haftmann@24219
|
85 |
| NONE => [Pretty.str "[...]"];
|
haftmann@24219
|
86 |
|
haftmann@24219
|
87 |
fun certificate thy f r =
|
haftmann@24219
|
88 |
case Susp.peek r
|
haftmann@24219
|
89 |
of SOME thms => (Susp.value o f thy) thms
|
haftmann@24219
|
90 |
| NONE => let
|
haftmann@24219
|
91 |
val thy_ref = Theory.check_thy thy;
|
haftmann@24219
|
92 |
in Susp.delay (fn () => (f (Theory.deref thy_ref) o Susp.force) r) end;
|
haftmann@24219
|
93 |
|
haftmann@24219
|
94 |
fun merge' _ ([], []) = (false, [])
|
haftmann@24219
|
95 |
| merge' _ ([], ys) = (true, ys)
|
haftmann@24219
|
96 |
| merge' eq (xs, ys) = fold_rev
|
haftmann@24219
|
97 |
(fn y => fn (t, xs) => (t orelse not (member eq xs y), insert eq y xs)) ys (false, xs);
|
haftmann@24219
|
98 |
|
haftmann@24219
|
99 |
fun merge_alist eq_key eq (xys as (xs, ys)) =
|
haftmann@24219
|
100 |
if eq_list (eq_pair eq_key eq) (xs, ys)
|
haftmann@24219
|
101 |
then (false, xs)
|
haftmann@24219
|
102 |
else (true, AList.merge eq_key eq xys);
|
haftmann@24219
|
103 |
|
haftmann@24219
|
104 |
val merge_thms = merge' Thm.eq_thm_prop;
|
haftmann@24219
|
105 |
|
haftmann@24219
|
106 |
fun merge_lthms (r1, r2) =
|
haftmann@24219
|
107 |
if Susp.same (r1, r2)
|
haftmann@24219
|
108 |
then (false, r1)
|
haftmann@24219
|
109 |
else case Susp.peek r1
|
haftmann@24219
|
110 |
of SOME [] => (true, r2)
|
haftmann@24219
|
111 |
| _ => case Susp.peek r2
|
haftmann@24219
|
112 |
of SOME [] => (true, r1)
|
haftmann@24219
|
113 |
| _ => (apsnd (Susp.delay o K)) (merge_thms (Susp.force r1, Susp.force r2));
|
haftmann@24219
|
114 |
|
haftmann@24219
|
115 |
|
haftmann@24219
|
116 |
(* pairs of (selected, deleted) defining equations *)
|
haftmann@24219
|
117 |
|
haftmann@24219
|
118 |
type sdthms = thm list Susp.T * thm list;
|
haftmann@24219
|
119 |
|
haftmann@24219
|
120 |
fun add_drop_redundant thm (sels, dels) =
|
haftmann@24219
|
121 |
let
|
haftmann@24219
|
122 |
val thy = Thm.theory_of_thm thm;
|
haftmann@24219
|
123 |
val args_of = snd o strip_comb o fst o Logic.dest_equals o Thm.plain_prop_of;
|
haftmann@24219
|
124 |
val args = args_of thm;
|
haftmann@24219
|
125 |
fun matches [] _ = true
|
haftmann@24219
|
126 |
| matches (Var _ :: xs) [] = matches xs []
|
haftmann@24219
|
127 |
| matches (_ :: _) [] = false
|
haftmann@24219
|
128 |
| matches (x :: xs) (y :: ys) = Pattern.matches thy (x, y) andalso matches xs ys;
|
haftmann@24219
|
129 |
fun drop thm' = not (matches args (args_of thm'))
|
haftmann@24219
|
130 |
orelse (warning ("code generator: dropping redundant defining equation\n" ^ string_of_thm thm'); false);
|
haftmann@24219
|
131 |
val (keeps, drops) = List.partition drop sels;
|
haftmann@24219
|
132 |
in (thm :: keeps, dels |> remove Thm.eq_thm_prop thm |> fold (insert Thm.eq_thm_prop) drops) end;
|
haftmann@24219
|
133 |
|
haftmann@24219
|
134 |
fun add_thm thm (sels, dels) =
|
haftmann@24219
|
135 |
apfst Susp.value (add_drop_redundant thm (Susp.force sels, dels));
|
haftmann@24219
|
136 |
|
haftmann@24219
|
137 |
fun add_lthms lthms (sels, []) =
|
haftmann@24219
|
138 |
(Susp.delay (fn () => fold add_drop_redundant
|
haftmann@24219
|
139 |
(Susp.force lthms) (Susp.force sels, []) |> fst), [])
|
haftmann@24219
|
140 |
(*FIXME*)
|
haftmann@24219
|
141 |
| add_lthms lthms (sels, dels) =
|
haftmann@24219
|
142 |
fold add_thm (Susp.force lthms) (sels, dels);
|
haftmann@24219
|
143 |
|
haftmann@24219
|
144 |
fun del_thm thm (sels, dels) =
|
haftmann@24219
|
145 |
(Susp.value (remove Thm.eq_thm_prop thm (Susp.force sels)), thm :: dels);
|
haftmann@24219
|
146 |
|
haftmann@24219
|
147 |
fun pretty_sdthms ctxt (sels, _) = pretty_lthms ctxt sels;
|
haftmann@24219
|
148 |
|
haftmann@24219
|
149 |
fun merge_sdthms ((sels1, dels1), (sels2, dels2)) =
|
haftmann@24219
|
150 |
let
|
haftmann@24219
|
151 |
val (dels_t, dels) = merge_thms (dels1, dels2);
|
haftmann@24219
|
152 |
in if dels_t
|
haftmann@24219
|
153 |
then let
|
haftmann@24219
|
154 |
val (_, sels) = merge_thms
|
haftmann@24219
|
155 |
(subtract Thm.eq_thm_prop dels2 (Susp.force sels1), Susp.force sels2);
|
haftmann@24219
|
156 |
val (_, dels) = merge_thms
|
haftmann@24219
|
157 |
(subtract Thm.eq_thm_prop (Susp.force sels2) dels1, dels2);
|
haftmann@24219
|
158 |
in (true, ((Susp.delay o K) sels, dels)) end
|
haftmann@24219
|
159 |
else let
|
haftmann@24219
|
160 |
val (sels_t, sels) = merge_lthms (sels1, sels2);
|
haftmann@24219
|
161 |
in (sels_t, (sels, dels)) end
|
haftmann@24219
|
162 |
end;
|
haftmann@24219
|
163 |
|
haftmann@24219
|
164 |
|
haftmann@24219
|
165 |
(* code attributes *)
|
haftmann@24219
|
166 |
|
haftmann@24219
|
167 |
structure CodeAttr = TheoryDataFun (
|
haftmann@24219
|
168 |
type T = (string * (Args.T list -> attribute * Args.T list)) list;
|
haftmann@24219
|
169 |
val empty = [];
|
haftmann@24219
|
170 |
val copy = I;
|
haftmann@24219
|
171 |
val extend = I;
|
haftmann@24219
|
172 |
fun merge _ = AList.merge (op =) (K true);
|
haftmann@24219
|
173 |
);
|
haftmann@24219
|
174 |
|
haftmann@24219
|
175 |
fun add_attribute (attr as (name, _)) =
|
haftmann@24219
|
176 |
let
|
haftmann@24219
|
177 |
fun add_parser ("", parser) attrs = attrs @ [("", parser)]
|
haftmann@24219
|
178 |
| add_parser (name, parser) attrs = (name, Args.$$$ name |-- parser) :: attrs;
|
haftmann@24219
|
179 |
fun error "" = error ("Code attribute already declared")
|
haftmann@24219
|
180 |
| error name = error ("Code attribute " ^ name ^ " already declared")
|
haftmann@24219
|
181 |
in CodeAttr.map (fn attrs => if AList.defined (op =) attrs name
|
haftmann@24219
|
182 |
then error name else add_parser attr attrs)
|
haftmann@24219
|
183 |
end;
|
haftmann@24219
|
184 |
|
haftmann@24219
|
185 |
val _ =
|
haftmann@24219
|
186 |
let
|
haftmann@24219
|
187 |
val code_attr = Attrib.syntax (Scan.peek (fn context =>
|
haftmann@24219
|
188 |
List.foldr op || Scan.fail (map snd (CodeAttr.get (Context.theory_of context)))));
|
haftmann@24219
|
189 |
in
|
haftmann@24219
|
190 |
Context.add_setup (Attrib.add_attributes
|
haftmann@24219
|
191 |
[("code", code_attr, "declare theorems for code generation")])
|
haftmann@24219
|
192 |
end;
|
haftmann@24219
|
193 |
|
haftmann@24219
|
194 |
|
haftmann@24219
|
195 |
|
haftmann@24219
|
196 |
(** exeuctable content **)
|
haftmann@24219
|
197 |
|
haftmann@24219
|
198 |
datatype thmproc = Preproc of {
|
haftmann@24219
|
199 |
inlines: thm list,
|
haftmann@24219
|
200 |
inline_procs: (string * (serial * (theory -> cterm list -> thm list))) list,
|
haftmann@24219
|
201 |
preprocs: (string * (serial * (theory -> thm list -> thm list))) list,
|
haftmann@24219
|
202 |
posts: thm list
|
haftmann@24219
|
203 |
};
|
haftmann@24219
|
204 |
|
haftmann@24219
|
205 |
fun mk_thmproc (((inlines, inline_procs), preprocs), posts) =
|
haftmann@24219
|
206 |
Preproc { inlines = inlines, inline_procs = inline_procs, preprocs = preprocs,
|
haftmann@24219
|
207 |
posts = posts };
|
haftmann@24219
|
208 |
fun map_thmproc f (Preproc { inlines, inline_procs, preprocs, posts }) =
|
haftmann@24219
|
209 |
mk_thmproc (f (((inlines, inline_procs), preprocs), posts));
|
haftmann@24219
|
210 |
fun merge_thmproc (Preproc { inlines = inlines1, inline_procs = inline_procs1,
|
haftmann@24219
|
211 |
preprocs = preprocs1, posts = posts1 },
|
haftmann@24219
|
212 |
Preproc { inlines = inlines2, inline_procs = inline_procs2,
|
haftmann@24219
|
213 |
preprocs = preprocs2, posts= posts2 }) =
|
haftmann@24219
|
214 |
let
|
haftmann@24219
|
215 |
val (touched1, inlines) = merge_thms (inlines1, inlines2);
|
haftmann@24219
|
216 |
val (touched2, inline_procs) = merge_alist (op =) (eq_fst (op =)) (inline_procs1, inline_procs2);
|
haftmann@24219
|
217 |
val (touched3, preprocs) = merge_alist (op =) (eq_fst (op =)) (preprocs1, preprocs2);
|
haftmann@24219
|
218 |
val (_, posts) = merge_thms (posts1, posts2);
|
haftmann@24219
|
219 |
in (touched1 orelse touched2 orelse touched3,
|
haftmann@24219
|
220 |
mk_thmproc (((inlines, inline_procs), preprocs), posts)) end;
|
haftmann@24219
|
221 |
|
haftmann@24219
|
222 |
fun join_func_thms (tabs as (tab1, tab2)) =
|
haftmann@24219
|
223 |
let
|
haftmann@24423
|
224 |
val cs1 = Symtab.keys tab1;
|
haftmann@24423
|
225 |
val cs2 = Symtab.keys tab2;
|
haftmann@24423
|
226 |
val cs' = filter (member (op =) cs2) cs1;
|
haftmann@24219
|
227 |
val cs'' = subtract (op =) cs' cs1 @ subtract (op =) cs' cs2;
|
haftmann@24423
|
228 |
val cs''' = ref [] : string list ref;
|
haftmann@24219
|
229 |
fun merge c x = let val (touched, thms') = merge_sdthms x in
|
haftmann@24219
|
230 |
(if touched then cs''' := cons c (!cs''') else (); thms') end;
|
haftmann@24423
|
231 |
in (cs'' @ !cs''', Symtab.join merge tabs) end;
|
haftmann@24219
|
232 |
fun merge_funcs (thms1, thms2) =
|
haftmann@24219
|
233 |
let
|
haftmann@24219
|
234 |
val (consts, thms) = join_func_thms (thms1, thms2);
|
haftmann@24219
|
235 |
in (SOME consts, thms) end;
|
haftmann@24219
|
236 |
|
haftmann@24219
|
237 |
val eq_string = op = : string * string -> bool;
|
haftmann@24219
|
238 |
fun eq_dtyp ((vs1, cs1), (vs2, cs2)) =
|
haftmann@24219
|
239 |
gen_eq_set (eq_pair eq_string (gen_eq_set eq_string)) (vs1, vs2)
|
haftmann@24423
|
240 |
andalso gen_eq_set (eq_fst eq_string) (cs1, cs2);
|
haftmann@24219
|
241 |
fun merge_dtyps (tabs as (tab1, tab2)) =
|
haftmann@24219
|
242 |
let
|
haftmann@24219
|
243 |
val tycos1 = Symtab.keys tab1;
|
haftmann@24219
|
244 |
val tycos2 = Symtab.keys tab2;
|
haftmann@24219
|
245 |
val tycos' = filter (member eq_string tycos2) tycos1;
|
haftmann@24219
|
246 |
val new_types = not (gen_eq_set (op =) (tycos1, tycos2));
|
haftmann@24219
|
247 |
val diff_types = not (gen_eq_set (eq_pair (op =) eq_dtyp)
|
haftmann@24219
|
248 |
(AList.make (the o Symtab.lookup tab1) tycos',
|
haftmann@24219
|
249 |
AList.make (the o Symtab.lookup tab2) tycos'));
|
haftmann@24219
|
250 |
fun join _ (cos as (_, cos2)) = if eq_dtyp cos
|
haftmann@24219
|
251 |
then raise Symtab.SAME else cos2;
|
haftmann@24219
|
252 |
in ((new_types, diff_types), Symtab.join join tabs) end;
|
haftmann@24219
|
253 |
|
haftmann@24219
|
254 |
datatype spec = Spec of {
|
haftmann@24423
|
255 |
funcs: sdthms Symtab.table,
|
haftmann@24219
|
256 |
dtyps: ((string * sort) list * (string * typ list) list) Symtab.table
|
haftmann@24219
|
257 |
};
|
haftmann@24219
|
258 |
|
haftmann@24219
|
259 |
fun mk_spec (funcs, dtyps) =
|
haftmann@24219
|
260 |
Spec { funcs = funcs, dtyps = dtyps };
|
haftmann@24219
|
261 |
fun map_spec f (Spec { funcs = funcs, dtyps = dtyps }) =
|
haftmann@24219
|
262 |
mk_spec (f (funcs, dtyps));
|
haftmann@24219
|
263 |
fun merge_spec (Spec { funcs = funcs1, dtyps = dtyps1 },
|
haftmann@24219
|
264 |
Spec { funcs = funcs2, dtyps = dtyps2 }) =
|
haftmann@24219
|
265 |
let
|
haftmann@24219
|
266 |
val (touched_cs, funcs) = merge_funcs (funcs1, funcs2);
|
haftmann@24219
|
267 |
val ((new_types, diff_types), dtyps) = merge_dtyps (dtyps1, dtyps2);
|
haftmann@24219
|
268 |
val touched = if new_types orelse diff_types then NONE else touched_cs;
|
haftmann@24219
|
269 |
in (touched, mk_spec (funcs, dtyps)) end;
|
haftmann@24219
|
270 |
|
haftmann@24219
|
271 |
datatype exec = Exec of {
|
haftmann@24219
|
272 |
thmproc: thmproc,
|
haftmann@24219
|
273 |
spec: spec
|
haftmann@24219
|
274 |
};
|
haftmann@24219
|
275 |
|
haftmann@24219
|
276 |
fun mk_exec (thmproc, spec) =
|
haftmann@24219
|
277 |
Exec { thmproc = thmproc, spec = spec };
|
haftmann@24219
|
278 |
fun map_exec f (Exec { thmproc = thmproc, spec = spec }) =
|
haftmann@24219
|
279 |
mk_exec (f (thmproc, spec));
|
haftmann@24219
|
280 |
fun merge_exec (Exec { thmproc = thmproc1, spec = spec1 },
|
haftmann@24219
|
281 |
Exec { thmproc = thmproc2, spec = spec2 }) =
|
haftmann@24219
|
282 |
let
|
haftmann@24219
|
283 |
val (touched', thmproc) = merge_thmproc (thmproc1, thmproc2);
|
haftmann@24219
|
284 |
val (touched_cs, spec) = merge_spec (spec1, spec2);
|
haftmann@24219
|
285 |
val touched = if touched' then NONE else touched_cs;
|
haftmann@24219
|
286 |
in (touched, mk_exec (thmproc, spec)) end;
|
haftmann@24219
|
287 |
val empty_exec = mk_exec (mk_thmproc ((([], []), []), []),
|
haftmann@24423
|
288 |
mk_spec (Symtab.empty, Symtab.empty));
|
haftmann@24219
|
289 |
|
haftmann@24219
|
290 |
fun the_thmproc (Exec { thmproc = Preproc x, ...}) = x;
|
haftmann@24219
|
291 |
fun the_spec (Exec { spec = Spec x, ...}) = x;
|
haftmann@24219
|
292 |
val the_funcs = #funcs o the_spec;
|
haftmann@24219
|
293 |
val the_dtyps = #dtyps o the_spec;
|
haftmann@24219
|
294 |
val map_thmproc = map_exec o apfst o map_thmproc;
|
haftmann@24219
|
295 |
val map_funcs = map_exec o apsnd o map_spec o apfst;
|
haftmann@24219
|
296 |
val map_dtyps = map_exec o apsnd o map_spec o apsnd;
|
haftmann@24219
|
297 |
|
haftmann@24219
|
298 |
|
haftmann@24219
|
299 |
(* data slots dependent on executable content *)
|
haftmann@24219
|
300 |
|
haftmann@24219
|
301 |
(*private copy avoids potential conflict of table exceptions*)
|
haftmann@24219
|
302 |
structure Datatab = TableFun(type key = int val ord = int_ord);
|
haftmann@24219
|
303 |
|
haftmann@24219
|
304 |
local
|
haftmann@24219
|
305 |
|
haftmann@24219
|
306 |
type kind = {
|
haftmann@24219
|
307 |
empty: Object.T,
|
haftmann@24219
|
308 |
merge: Pretty.pp -> Object.T * Object.T -> Object.T,
|
haftmann@24423
|
309 |
purge: theory option -> string list option -> Object.T -> Object.T
|
haftmann@24219
|
310 |
};
|
haftmann@24219
|
311 |
|
haftmann@24219
|
312 |
val kinds = ref (Datatab.empty: kind Datatab.table);
|
haftmann@24219
|
313 |
val kind_keys = ref ([]: serial list);
|
haftmann@24219
|
314 |
|
haftmann@24219
|
315 |
fun invoke f k = case Datatab.lookup (! kinds) k
|
haftmann@24219
|
316 |
of SOME kind => f kind
|
haftmann@24219
|
317 |
| NONE => sys_error "Invalid code data identifier";
|
haftmann@24219
|
318 |
|
haftmann@24219
|
319 |
in
|
haftmann@24219
|
320 |
|
haftmann@24219
|
321 |
fun declare_data empty merge purge =
|
haftmann@24219
|
322 |
let
|
haftmann@24219
|
323 |
val k = serial ();
|
haftmann@24219
|
324 |
val kind = {empty = empty, merge = merge, purge = purge};
|
haftmann@24219
|
325 |
val _ = change kinds (Datatab.update (k, kind));
|
haftmann@24219
|
326 |
val _ = change kind_keys (cons k);
|
haftmann@24219
|
327 |
in k end;
|
haftmann@24219
|
328 |
|
haftmann@24219
|
329 |
fun invoke_empty k = invoke (fn kind => #empty kind) k;
|
haftmann@24219
|
330 |
|
haftmann@24219
|
331 |
fun invoke_merge_all pp = Datatab.join
|
haftmann@24219
|
332 |
(invoke (fn kind => #merge kind pp));
|
haftmann@24219
|
333 |
|
haftmann@24219
|
334 |
fun invoke_purge_all thy_opt cs =
|
haftmann@24219
|
335 |
fold (fn k => Datatab.map_entry k
|
haftmann@24219
|
336 |
(invoke (fn kind => #purge kind thy_opt cs) k)) (! kind_keys);
|
haftmann@24219
|
337 |
|
haftmann@24219
|
338 |
end; (*local*)
|
haftmann@24219
|
339 |
|
haftmann@24219
|
340 |
|
haftmann@24219
|
341 |
(* theory store *)
|
haftmann@24219
|
342 |
|
haftmann@24219
|
343 |
local
|
haftmann@24219
|
344 |
|
haftmann@24219
|
345 |
type data = Object.T Datatab.table;
|
haftmann@24219
|
346 |
|
haftmann@24219
|
347 |
structure CodeData = TheoryDataFun
|
haftmann@24219
|
348 |
(
|
haftmann@24219
|
349 |
type T = exec * data ref;
|
haftmann@24219
|
350 |
val empty = (empty_exec, ref Datatab.empty : data ref);
|
haftmann@24219
|
351 |
fun copy (exec, data) = (exec, ref (! data));
|
haftmann@24219
|
352 |
val extend = copy;
|
haftmann@24219
|
353 |
fun merge pp ((exec1, data1), (exec2, data2)) =
|
haftmann@24219
|
354 |
let
|
haftmann@24219
|
355 |
val (touched, exec) = merge_exec (exec1, exec2);
|
haftmann@24219
|
356 |
val data1' = invoke_purge_all NONE touched (! data1);
|
haftmann@24219
|
357 |
val data2' = invoke_purge_all NONE touched (! data2);
|
haftmann@24219
|
358 |
val data = invoke_merge_all pp (data1', data2');
|
haftmann@24219
|
359 |
in (exec, ref data) end;
|
haftmann@24219
|
360 |
);
|
haftmann@24219
|
361 |
|
haftmann@24219
|
362 |
val _ = Context.add_setup CodeData.init;
|
haftmann@24219
|
363 |
|
haftmann@24219
|
364 |
fun ch r f = let val x = f (! r) in (r := x; x) end;
|
haftmann@24219
|
365 |
fun thy_data f thy = f ((snd o CodeData.get) thy);
|
haftmann@24219
|
366 |
|
haftmann@24219
|
367 |
fun get_ensure_init kind data_ref =
|
haftmann@24219
|
368 |
case Datatab.lookup (! data_ref) kind
|
haftmann@24219
|
369 |
of SOME x => x
|
haftmann@24219
|
370 |
| NONE => let val y = invoke_empty kind
|
haftmann@24219
|
371 |
in (change data_ref (Datatab.update (kind, y)); y) end;
|
haftmann@24219
|
372 |
|
haftmann@24219
|
373 |
in
|
haftmann@24219
|
374 |
|
haftmann@24219
|
375 |
(* access to executable content *)
|
haftmann@24219
|
376 |
|
haftmann@24219
|
377 |
val get_exec = fst o CodeData.get;
|
haftmann@24219
|
378 |
|
haftmann@24219
|
379 |
fun map_exec_purge touched f thy =
|
haftmann@24219
|
380 |
CodeData.map (fn (exec, data) =>
|
haftmann@24219
|
381 |
(f exec, ref (invoke_purge_all (SOME thy) touched (! data)))) thy;
|
haftmann@24219
|
382 |
|
haftmann@24219
|
383 |
|
haftmann@24219
|
384 |
(* access to data dependent on abstract executable content *)
|
haftmann@24219
|
385 |
|
haftmann@24219
|
386 |
fun get_data (kind, _, dest) = thy_data (get_ensure_init kind #> dest);
|
haftmann@24219
|
387 |
|
haftmann@24219
|
388 |
fun change_data (kind, mk, dest) =
|
haftmann@24219
|
389 |
let
|
haftmann@24219
|
390 |
fun chnge data_ref f =
|
haftmann@24219
|
391 |
let
|
haftmann@24219
|
392 |
val data = get_ensure_init kind data_ref;
|
haftmann@24219
|
393 |
val data' = f (dest data);
|
haftmann@24219
|
394 |
in (change data_ref (Datatab.update (kind, mk data')); data') end;
|
haftmann@24219
|
395 |
in thy_data chnge end;
|
haftmann@24219
|
396 |
|
haftmann@24219
|
397 |
fun change_yield_data (kind, mk, dest) =
|
haftmann@24219
|
398 |
let
|
haftmann@24219
|
399 |
fun chnge data_ref f =
|
haftmann@24219
|
400 |
let
|
haftmann@24219
|
401 |
val data = get_ensure_init kind data_ref;
|
haftmann@24219
|
402 |
val (x, data') = f (dest data);
|
haftmann@24219
|
403 |
in (x, (change data_ref (Datatab.update (kind, mk data')); data')) end;
|
haftmann@24219
|
404 |
in thy_data chnge end;
|
haftmann@24219
|
405 |
|
haftmann@24219
|
406 |
end; (*local*)
|
haftmann@24219
|
407 |
|
haftmann@24219
|
408 |
|
haftmann@24219
|
409 |
(* print executable content *)
|
haftmann@24219
|
410 |
|
haftmann@24219
|
411 |
fun print_codesetup thy =
|
haftmann@24219
|
412 |
let
|
haftmann@24219
|
413 |
val ctxt = ProofContext.init thy;
|
haftmann@24219
|
414 |
val exec = get_exec thy;
|
haftmann@24219
|
415 |
fun pretty_func (s, lthms) =
|
haftmann@24219
|
416 |
(Pretty.block o Pretty.fbreaks) (
|
haftmann@24219
|
417 |
Pretty.str s :: pretty_sdthms ctxt lthms
|
haftmann@24219
|
418 |
);
|
haftmann@24219
|
419 |
fun pretty_dtyp (s, []) =
|
haftmann@24219
|
420 |
Pretty.str s
|
haftmann@24219
|
421 |
| pretty_dtyp (s, cos) =
|
haftmann@24219
|
422 |
(Pretty.block o Pretty.breaks) (
|
haftmann@24219
|
423 |
Pretty.str s
|
haftmann@24219
|
424 |
:: Pretty.str "="
|
haftmann@24219
|
425 |
:: separate (Pretty.str "|") (map (fn (c, []) => Pretty.str c
|
haftmann@24219
|
426 |
| (c, tys) =>
|
haftmann@24219
|
427 |
(Pretty.block o Pretty.breaks)
|
haftmann@24423
|
428 |
(Pretty.str (CodeUnit.string_of_const thy c)
|
haftmann@24423
|
429 |
:: Pretty.str "of" :: map (Pretty.quote o Sign.pretty_typ thy) tys)) cos)
|
haftmann@24219
|
430 |
);
|
haftmann@24219
|
431 |
val inlines = (#inlines o the_thmproc) exec;
|
haftmann@24219
|
432 |
val inline_procs = (map fst o #inline_procs o the_thmproc) exec;
|
haftmann@24219
|
433 |
val preprocs = (map fst o #preprocs o the_thmproc) exec;
|
haftmann@24219
|
434 |
val funs = the_funcs exec
|
haftmann@24423
|
435 |
|> Symtab.dest
|
haftmann@24219
|
436 |
|> (map o apfst) (CodeUnit.string_of_const thy)
|
haftmann@24219
|
437 |
|> sort (string_ord o pairself fst);
|
haftmann@24219
|
438 |
val dtyps = the_dtyps exec
|
haftmann@24219
|
439 |
|> Symtab.dest
|
haftmann@24219
|
440 |
|> map (fn (dtco, (vs, cos)) => (Sign.string_of_typ thy (Type (dtco, map TFree vs)), cos))
|
haftmann@24219
|
441 |
|> sort (string_ord o pairself fst)
|
haftmann@24219
|
442 |
in
|
haftmann@24219
|
443 |
(Pretty.writeln o Pretty.chunks) [
|
haftmann@24219
|
444 |
Pretty.block (
|
haftmann@24219
|
445 |
Pretty.str "defining equations:"
|
haftmann@24219
|
446 |
:: Pretty.fbrk
|
haftmann@24219
|
447 |
:: (Pretty.fbreaks o map pretty_func) funs
|
haftmann@24219
|
448 |
),
|
haftmann@24219
|
449 |
Pretty.block (
|
haftmann@24219
|
450 |
Pretty.str "inlining theorems:"
|
haftmann@24219
|
451 |
:: Pretty.fbrk
|
haftmann@24219
|
452 |
:: (Pretty.fbreaks o map (ProofContext.pretty_thm ctxt)) inlines
|
haftmann@24219
|
453 |
),
|
haftmann@24219
|
454 |
Pretty.block (
|
haftmann@24219
|
455 |
Pretty.str "inlining procedures:"
|
haftmann@24219
|
456 |
:: Pretty.fbrk
|
haftmann@24219
|
457 |
:: (Pretty.fbreaks o map Pretty.str) inline_procs
|
haftmann@24219
|
458 |
),
|
haftmann@24219
|
459 |
Pretty.block (
|
haftmann@24219
|
460 |
Pretty.str "preprocessors:"
|
haftmann@24219
|
461 |
:: Pretty.fbrk
|
haftmann@24219
|
462 |
:: (Pretty.fbreaks o map Pretty.str) preprocs
|
haftmann@24219
|
463 |
),
|
haftmann@24219
|
464 |
Pretty.block (
|
haftmann@24219
|
465 |
Pretty.str "datatypes:"
|
haftmann@24219
|
466 |
:: Pretty.fbrk
|
haftmann@24219
|
467 |
:: (Pretty.fbreaks o map pretty_dtyp) dtyps
|
haftmann@24219
|
468 |
)
|
haftmann@24219
|
469 |
]
|
haftmann@24219
|
470 |
end;
|
haftmann@24219
|
471 |
|
haftmann@24219
|
472 |
|
haftmann@24219
|
473 |
|
haftmann@24219
|
474 |
(** theorem transformation and certification **)
|
haftmann@24219
|
475 |
|
haftmann@24219
|
476 |
fun common_typ_funcs [] = []
|
haftmann@24219
|
477 |
| common_typ_funcs [thm] = [thm]
|
haftmann@24219
|
478 |
| common_typ_funcs (thms as thm :: _) =
|
haftmann@24219
|
479 |
let
|
haftmann@24219
|
480 |
val thy = Thm.theory_of_thm thm;
|
haftmann@24219
|
481 |
fun incr_thm thm max =
|
haftmann@24219
|
482 |
let
|
haftmann@24219
|
483 |
val thm' = incr_indexes max thm;
|
haftmann@24219
|
484 |
val max' = Thm.maxidx_of thm' + 1;
|
haftmann@24219
|
485 |
in (thm', max') end;
|
haftmann@24219
|
486 |
val (thms', maxidx) = fold_map incr_thm thms 0;
|
haftmann@24219
|
487 |
val ty1 :: tys = map (snd o CodeUnit.head_func) thms';
|
haftmann@24219
|
488 |
fun unify ty env = Sign.typ_unify thy (ty1, ty) env
|
haftmann@24219
|
489 |
handle Type.TUNIFY =>
|
haftmann@24219
|
490 |
error ("Type unificaton failed, while unifying defining equations\n"
|
haftmann@24219
|
491 |
^ (cat_lines o map Display.string_of_thm) thms
|
haftmann@24219
|
492 |
^ "\nwith types\n"
|
haftmann@24219
|
493 |
^ (cat_lines o map (CodeUnit.string_of_typ thy)) (ty1 :: tys));
|
haftmann@24219
|
494 |
val (env, _) = fold unify tys (Vartab.empty, maxidx)
|
haftmann@24219
|
495 |
val instT = Vartab.fold (fn (x_i, (sort, ty)) =>
|
haftmann@24219
|
496 |
cons (Thm.ctyp_of thy (TVar (x_i, sort)), Thm.ctyp_of thy ty)) env [];
|
haftmann@24219
|
497 |
in map (Thm.instantiate (instT, [])) thms' end;
|
haftmann@24219
|
498 |
|
haftmann@24423
|
499 |
fun const_of_func thy = Class.unoverload_const thy o CodeUnit.head_func;
|
haftmann@24423
|
500 |
|
haftmann@24219
|
501 |
fun certify_const thy const thms =
|
haftmann@24219
|
502 |
let
|
haftmann@24423
|
503 |
fun cert thm = if const = const_of_func thy thm
|
haftmann@24219
|
504 |
then thm else error ("Wrong head of defining equation,\nexpected constant "
|
haftmann@24219
|
505 |
^ CodeUnit.string_of_const thy const ^ "\n" ^ string_of_thm thm)
|
haftmann@24219
|
506 |
in map cert thms end;
|
haftmann@24219
|
507 |
|
haftmann@24219
|
508 |
|
haftmann@24219
|
509 |
|
haftmann@24219
|
510 |
(** operational sort algebra and class discipline **)
|
haftmann@24219
|
511 |
|
haftmann@24219
|
512 |
local
|
haftmann@24219
|
513 |
|
haftmann@24219
|
514 |
fun aggr_neutr f y [] = y
|
haftmann@24219
|
515 |
| aggr_neutr f y (x::xs) = aggr_neutr f (f y x) xs;
|
haftmann@24219
|
516 |
|
haftmann@24219
|
517 |
fun aggregate f [] = NONE
|
haftmann@24219
|
518 |
| aggregate f (x::xs) = SOME (aggr_neutr f x xs);
|
haftmann@24219
|
519 |
|
haftmann@24219
|
520 |
fun inter_sorts thy =
|
haftmann@24219
|
521 |
let
|
haftmann@24219
|
522 |
val algebra = Sign.classes_of thy;
|
haftmann@24219
|
523 |
val inters = curry (Sorts.inter_sort algebra);
|
haftmann@24219
|
524 |
in aggregate (map2 inters) end;
|
haftmann@24219
|
525 |
|
haftmann@24219
|
526 |
fun specific_constraints thy (class, tyco) =
|
haftmann@24219
|
527 |
let
|
haftmann@24219
|
528 |
val vs = Name.invents Name.context "" (Sign.arity_number thy tyco);
|
haftmann@24423
|
529 |
val clsops = (map fst o these o Option.map snd
|
haftmann@24423
|
530 |
o try (AxClass.params_of_class thy)) class;
|
haftmann@24219
|
531 |
val funcs = clsops
|
haftmann@24423
|
532 |
|> map (fn c => Class.inst_const thy (c, tyco))
|
haftmann@24423
|
533 |
|> map (Symtab.lookup ((the_funcs o get_exec) thy))
|
haftmann@24219
|
534 |
|> (map o Option.map) (Susp.force o fst)
|
haftmann@24219
|
535 |
|> maps these
|
haftmann@24423
|
536 |
|> map (Thm.transfer thy)
|
haftmann@24423
|
537 |
fun sorts_of [Type (_, tys)] = map (snd o dest_TVar) tys
|
haftmann@24423
|
538 |
| sorts_of tys = map (snd o dest_TVar) tys;
|
haftmann@24423
|
539 |
val sorts = map (sorts_of o Sign.const_typargs thy o CodeUnit.head_func) funcs;
|
haftmann@24219
|
540 |
in sorts end;
|
haftmann@24219
|
541 |
|
haftmann@24219
|
542 |
fun weakest_constraints thy (class, tyco) =
|
haftmann@24219
|
543 |
let
|
haftmann@24219
|
544 |
val all_superclasses = class :: Graph.all_succs ((#classes o Sorts.rep_algebra o Sign.classes_of) thy) [class];
|
haftmann@24219
|
545 |
in case inter_sorts thy (maps (fn class => specific_constraints thy (class, tyco)) all_superclasses)
|
haftmann@24219
|
546 |
of SOME sorts => sorts
|
haftmann@24219
|
547 |
| NONE => Sign.arity_sorts thy tyco [class]
|
haftmann@24219
|
548 |
end;
|
haftmann@24219
|
549 |
|
haftmann@24219
|
550 |
fun strongest_constraints thy (class, tyco) =
|
haftmann@24219
|
551 |
let
|
haftmann@24219
|
552 |
val algebra = Sign.classes_of thy;
|
haftmann@24219
|
553 |
val all_subclasses = class :: Graph.all_preds ((#classes o Sorts.rep_algebra) algebra) [class];
|
haftmann@24219
|
554 |
val inst_subclasses = filter (can (Sorts.mg_domain algebra tyco) o single) all_subclasses;
|
haftmann@24219
|
555 |
in case inter_sorts thy (maps (fn class => specific_constraints thy (class, tyco)) inst_subclasses)
|
haftmann@24219
|
556 |
of SOME sorts => sorts
|
haftmann@24219
|
557 |
| NONE => replicate
|
haftmann@24219
|
558 |
(Sign.arity_number thy tyco) (Sign.certify_sort thy (Sign.all_classes thy))
|
haftmann@24219
|
559 |
end;
|
haftmann@24219
|
560 |
|
haftmann@24219
|
561 |
fun gen_classop_typ constr thy class (c, tyco) =
|
haftmann@24219
|
562 |
let
|
haftmann@24219
|
563 |
val (var, cs) = try (AxClass.params_of_class thy) class |> the_default ("'a", [])
|
haftmann@24219
|
564 |
val ty = (the o AList.lookup (op =) cs) c;
|
haftmann@24219
|
565 |
val sort_args = Name.names (Name.declare var Name.context) "'a"
|
haftmann@24219
|
566 |
(constr thy (class, tyco));
|
haftmann@24219
|
567 |
val ty_inst = Type (tyco, map TFree sort_args);
|
haftmann@24219
|
568 |
in Logic.varifyT (map_type_tfree (K ty_inst) ty) end;
|
haftmann@24219
|
569 |
|
haftmann@24219
|
570 |
fun retrieve_algebra thy operational =
|
haftmann@24219
|
571 |
Sorts.subalgebra (Sign.pp thy) operational
|
haftmann@24219
|
572 |
(weakest_constraints thy)
|
haftmann@24219
|
573 |
(Sign.classes_of thy);
|
haftmann@24219
|
574 |
|
haftmann@24219
|
575 |
in
|
haftmann@24219
|
576 |
|
haftmann@24219
|
577 |
fun coregular_algebra thy = retrieve_algebra thy (K true) |> snd;
|
haftmann@24219
|
578 |
fun operational_algebra thy =
|
haftmann@24219
|
579 |
let
|
haftmann@24219
|
580 |
fun add_iff_operational class =
|
haftmann@24219
|
581 |
can (AxClass.get_definition thy) class ? cons class;
|
haftmann@24219
|
582 |
val operational_classes = fold add_iff_operational (Sign.all_classes thy) []
|
haftmann@24219
|
583 |
in retrieve_algebra thy (member (op =) operational_classes) end;
|
haftmann@24219
|
584 |
|
haftmann@24219
|
585 |
val classop_weakest_typ = gen_classop_typ weakest_constraints;
|
haftmann@24219
|
586 |
val classop_strongest_typ = gen_classop_typ strongest_constraints;
|
haftmann@24219
|
587 |
|
haftmann@24219
|
588 |
fun assert_func_typ thm =
|
haftmann@24219
|
589 |
let
|
haftmann@24219
|
590 |
val thy = Thm.theory_of_thm thm;
|
haftmann@24423
|
591 |
fun check_typ_classop tyco (c, thm) =
|
haftmann@24219
|
592 |
let
|
haftmann@24423
|
593 |
val SOME class = AxClass.class_of_param thy c;
|
haftmann@24219
|
594 |
val (_, ty) = CodeUnit.head_func thm;
|
haftmann@24219
|
595 |
val ty_decl = classop_weakest_typ thy class (c, tyco);
|
haftmann@24219
|
596 |
val ty_strongest = classop_strongest_typ thy class (c, tyco);
|
haftmann@24219
|
597 |
fun constrain thm =
|
haftmann@24219
|
598 |
let
|
haftmann@24219
|
599 |
val max = Thm.maxidx_of thm + 1;
|
haftmann@24219
|
600 |
val ty_decl' = Logic.incr_tvar max ty_decl;
|
haftmann@24219
|
601 |
val (_, ty') = CodeUnit.head_func thm;
|
haftmann@24219
|
602 |
val (env, _) = Sign.typ_unify thy (ty_decl', ty') (Vartab.empty, max);
|
haftmann@24219
|
603 |
val instT = Vartab.fold (fn (x_i, (sort, ty)) =>
|
haftmann@24219
|
604 |
cons (Thm.ctyp_of thy (TVar (x_i, sort)), Thm.ctyp_of thy ty)) env [];
|
haftmann@24219
|
605 |
in Thm.instantiate (instT, []) thm end;
|
haftmann@24219
|
606 |
in if Sign.typ_instance thy (ty_strongest, ty)
|
haftmann@24219
|
607 |
then if Sign.typ_instance thy (ty, ty_decl)
|
haftmann@24219
|
608 |
then thm
|
haftmann@24219
|
609 |
else (warning ("Constraining type\n" ^ CodeUnit.string_of_typ thy ty
|
haftmann@24219
|
610 |
^ "\nof defining equation\n"
|
haftmann@24219
|
611 |
^ string_of_thm thm
|
haftmann@24219
|
612 |
^ "\nto permitted most general type\n"
|
haftmann@24219
|
613 |
^ CodeUnit.string_of_typ thy ty_decl);
|
haftmann@24219
|
614 |
constrain thm)
|
haftmann@24219
|
615 |
else CodeUnit.bad_thm ("Type\n" ^ CodeUnit.string_of_typ thy ty
|
haftmann@24219
|
616 |
^ "\nof defining equation\n"
|
haftmann@24219
|
617 |
^ string_of_thm thm
|
haftmann@24219
|
618 |
^ "\nis incompatible with permitted least general type\n"
|
haftmann@24219
|
619 |
^ CodeUnit.string_of_typ thy ty_strongest)
|
haftmann@24423
|
620 |
end;
|
haftmann@24423
|
621 |
fun check_typ_fun (c, thm) =
|
haftmann@24219
|
622 |
let
|
haftmann@24219
|
623 |
val (_, ty) = CodeUnit.head_func thm;
|
haftmann@24219
|
624 |
val ty_decl = Sign.the_const_type thy c;
|
haftmann@24219
|
625 |
in if Sign.typ_equiv thy (Type.strip_sorts ty_decl, Type.strip_sorts ty)
|
haftmann@24219
|
626 |
then thm
|
haftmann@24219
|
627 |
else CodeUnit.bad_thm ("Type\n" ^ CodeUnit.string_of_typ thy ty
|
haftmann@24219
|
628 |
^ "\nof defining equation\n"
|
haftmann@24219
|
629 |
^ string_of_thm thm
|
haftmann@24219
|
630 |
^ "\nis incompatible with declared function type\n"
|
haftmann@24219
|
631 |
^ CodeUnit.string_of_typ thy ty_decl)
|
haftmann@24219
|
632 |
end;
|
haftmann@24423
|
633 |
fun check_typ (c, thm) =
|
haftmann@24423
|
634 |
case Class.param_const thy c
|
haftmann@24423
|
635 |
of SOME (c, tyco) => check_typ_classop tyco (c, thm)
|
haftmann@24423
|
636 |
| NONE => check_typ_fun (c, thm);
|
haftmann@24423
|
637 |
in check_typ (const_of_func thy thm, thm) end;
|
haftmann@24219
|
638 |
|
haftmann@24283
|
639 |
val mk_func = CodeUnit.error_thm (assert_func_typ o CodeUnit.mk_func);
|
haftmann@24624
|
640 |
val mk_liberal_func = CodeUnit.warning_thm (assert_func_typ o CodeUnit.mk_func);
|
haftmann@24624
|
641 |
val mk_default_func = CodeUnit.try_thm (assert_func_typ o CodeUnit.mk_func);
|
haftmann@24219
|
642 |
|
haftmann@24219
|
643 |
end;
|
haftmann@24219
|
644 |
|
haftmann@24219
|
645 |
|
haftmann@24219
|
646 |
|
haftmann@24219
|
647 |
(** interfaces and attributes **)
|
haftmann@24219
|
648 |
|
haftmann@24624
|
649 |
fun delete_force msg key xs =
|
haftmann@24624
|
650 |
if AList.defined (op =) xs key then AList.delete (op =) key xs
|
haftmann@24624
|
651 |
else error ("No such " ^ msg ^ ": " ^ quote key);
|
haftmann@24624
|
652 |
|
haftmann@24423
|
653 |
fun get_datatype thy tyco =
|
haftmann@24423
|
654 |
case Symtab.lookup ((the_dtyps o get_exec) thy) tyco
|
haftmann@24423
|
655 |
of SOME spec => spec
|
haftmann@24423
|
656 |
| NONE => Sign.arity_number thy tyco
|
haftmann@24423
|
657 |
|> Name.invents Name.context "'a"
|
haftmann@24423
|
658 |
|> map (rpair [])
|
haftmann@24423
|
659 |
|> rpair [];
|
haftmann@24423
|
660 |
|
haftmann@24423
|
661 |
fun get_datatype_of_constr thy c =
|
haftmann@24423
|
662 |
case (snd o strip_type o Sign.the_const_type thy) c
|
haftmann@24423
|
663 |
of Type (tyco, _) => if member (op =)
|
haftmann@24423
|
664 |
((the_default [] o Option.map (map fst o snd) o Symtab.lookup ((the_dtyps o get_exec) thy)) tyco) c
|
haftmann@24423
|
665 |
then SOME tyco else NONE
|
haftmann@24423
|
666 |
| _ => NONE;
|
haftmann@24423
|
667 |
|
haftmann@24423
|
668 |
fun get_constr_typ thy c =
|
haftmann@24423
|
669 |
case get_datatype_of_constr thy c
|
haftmann@24423
|
670 |
of SOME tyco => let
|
haftmann@24423
|
671 |
val (vs, cos) = get_datatype thy tyco;
|
haftmann@24423
|
672 |
val SOME tys = AList.lookup (op =) cos c;
|
haftmann@24423
|
673 |
val ty = tys ---> Type (tyco, map TFree vs);
|
haftmann@24423
|
674 |
in SOME (Logic.varifyT ty) end
|
haftmann@24423
|
675 |
| NONE => NONE;
|
haftmann@24423
|
676 |
|
haftmann@24624
|
677 |
fun add_func thm thy =
|
haftmann@24624
|
678 |
let
|
haftmann@24624
|
679 |
val func = mk_func thm;
|
haftmann@24624
|
680 |
val c = const_of_func thy func;
|
haftmann@24624
|
681 |
val _ = if (is_some o AxClass.class_of_param thy) c
|
haftmann@24624
|
682 |
then error ("Rejected polymorphic equation for overloaded constant:\n"
|
haftmann@24624
|
683 |
^ string_of_thm thm)
|
haftmann@24624
|
684 |
else ();
|
haftmann@24624
|
685 |
val _ = if (is_some o get_datatype_of_constr thy) c
|
haftmann@24624
|
686 |
then error ("Rejected equation for datatype constructor:\n"
|
haftmann@24624
|
687 |
^ string_of_thm func)
|
haftmann@24624
|
688 |
else ();
|
haftmann@24624
|
689 |
in
|
haftmann@24624
|
690 |
(map_exec_purge (SOME [c]) o map_funcs) (Symtab.map_default
|
haftmann@24624
|
691 |
(c, (Susp.value [], [])) (add_thm func)) thy
|
haftmann@24624
|
692 |
end;
|
haftmann@24219
|
693 |
|
haftmann@24624
|
694 |
fun add_liberal_func thm thy =
|
haftmann@24624
|
695 |
case mk_liberal_func thm
|
haftmann@24624
|
696 |
of SOME func => let
|
haftmann@24624
|
697 |
val c = const_of_func thy func
|
haftmann@24624
|
698 |
in if (is_some o AxClass.class_of_param thy) c
|
haftmann@24624
|
699 |
orelse (is_some o get_datatype_of_constr thy) c
|
haftmann@24624
|
700 |
then thy
|
haftmann@24624
|
701 |
else map_exec_purge (SOME [c]) (map_funcs
|
haftmann@24624
|
702 |
(Symtab.map_default
|
haftmann@24624
|
703 |
(c, (Susp.value [], [])) (add_thm func))) thy
|
haftmann@24624
|
704 |
end
|
haftmann@24624
|
705 |
| NONE => thy;
|
haftmann@24624
|
706 |
|
haftmann@24624
|
707 |
fun add_default_func thm thy =
|
haftmann@24624
|
708 |
case mk_default_func thm
|
haftmann@24624
|
709 |
of SOME func => let
|
haftmann@24624
|
710 |
val c = const_of_func thy func
|
haftmann@24624
|
711 |
in if (is_some o AxClass.class_of_param thy) c
|
haftmann@24624
|
712 |
orelse (is_some o get_datatype_of_constr thy) c
|
haftmann@24624
|
713 |
then thy
|
haftmann@24624
|
714 |
else map_exec_purge (SOME [c]) (map_funcs
|
haftmann@24624
|
715 |
(Symtab.map_default
|
haftmann@24624
|
716 |
(c, (Susp.value [], [])) (add_thm func))) thy
|
haftmann@24624
|
717 |
end
|
haftmann@24624
|
718 |
| NONE => thy;
|
haftmann@24219
|
719 |
|
haftmann@24219
|
720 |
fun del_func thm thy =
|
haftmann@24659
|
721 |
case mk_liberal_func thm
|
haftmann@24659
|
722 |
of SOME func => let
|
haftmann@24659
|
723 |
val c = const_of_func thy func;
|
haftmann@24659
|
724 |
in map_exec_purge (SOME [c]) (map_funcs
|
haftmann@24659
|
725 |
(Symtab.map_entry c (del_thm func))) thy
|
haftmann@24659
|
726 |
end
|
haftmann@24659
|
727 |
| NONE => thy;
|
haftmann@24219
|
728 |
|
haftmann@24219
|
729 |
fun add_funcl (const, lthms) thy =
|
haftmann@24219
|
730 |
let
|
haftmann@24219
|
731 |
val lthms' = certificate thy (fn thy => certify_const thy const) lthms;
|
haftmann@24219
|
732 |
(*FIXME must check compatibility with sort algebra;
|
haftmann@24219
|
733 |
alas, naive checking results in non-termination!*)
|
haftmann@24219
|
734 |
in
|
haftmann@24423
|
735 |
map_exec_purge (SOME [const]) (map_funcs (Symtab.map_default (const, (Susp.value [], []))
|
haftmann@24219
|
736 |
(add_lthms lthms'))) thy
|
haftmann@24219
|
737 |
end;
|
haftmann@24219
|
738 |
|
haftmann@24624
|
739 |
val add_default_func_attr = Attrib.internal (fn _ => Thm.declaration_attribute
|
haftmann@24624
|
740 |
(fn thm => Context.mapping (add_default_func thm) I));
|
haftmann@24219
|
741 |
|
haftmann@24423
|
742 |
fun add_datatype raw_cs thy =
|
haftmann@24219
|
743 |
let
|
haftmann@24423
|
744 |
val cs = map (fn c_ty as (_, ty) => (Class.unoverload_const thy c_ty, ty)) raw_cs;
|
haftmann@24423
|
745 |
val (tyco, vs_cos) = CodeUnit.constrset_of_consts thy cs;
|
haftmann@24423
|
746 |
val purge_cs = map fst (snd vs_cos);
|
haftmann@24423
|
747 |
val purge_cs' = case Symtab.lookup ((the_dtyps o get_exec) thy) tyco
|
haftmann@24423
|
748 |
of SOME (vs, cos) => if null cos then NONE else SOME (purge_cs @ map fst cos)
|
haftmann@24423
|
749 |
| NONE => NONE;
|
haftmann@24219
|
750 |
in
|
haftmann@24219
|
751 |
thy
|
haftmann@24423
|
752 |
|> map_exec_purge purge_cs' (map_dtyps (Symtab.update (tyco, vs_cos))
|
haftmann@24423
|
753 |
#> map_funcs (fold (Symtab.delete_safe o fst) cs))
|
haftmann@24219
|
754 |
end;
|
haftmann@24219
|
755 |
|
haftmann@24423
|
756 |
fun add_datatype_cmd raw_cs thy =
|
haftmann@24423
|
757 |
let
|
haftmann@24423
|
758 |
val cs = map (CodeUnit.read_bare_const thy) raw_cs;
|
haftmann@24423
|
759 |
in add_datatype cs thy end;
|
haftmann@24219
|
760 |
|
haftmann@24219
|
761 |
fun add_inline thm thy =
|
haftmann@24219
|
762 |
(map_exec_purge NONE o map_thmproc o apfst o apfst o apfst)
|
haftmann@24219
|
763 |
(insert Thm.eq_thm_prop (CodeUnit.error_thm CodeUnit.mk_rew thm)) thy;
|
haftmann@24219
|
764 |
(*fully applied in order to get right context for mk_rew!*)
|
haftmann@24219
|
765 |
|
haftmann@24219
|
766 |
fun del_inline thm thy =
|
haftmann@24219
|
767 |
(map_exec_purge NONE o map_thmproc o apfst o apfst o apfst)
|
haftmann@24219
|
768 |
(remove Thm.eq_thm_prop (CodeUnit.error_thm CodeUnit.mk_rew thm)) thy;
|
haftmann@24219
|
769 |
(*fully applied in order to get right context for mk_rew!*)
|
haftmann@24219
|
770 |
|
haftmann@24219
|
771 |
fun add_inline_proc (name, f) =
|
haftmann@24219
|
772 |
(map_exec_purge NONE o map_thmproc o apfst o apfst o apsnd)
|
haftmann@24219
|
773 |
(AList.update (op =) (name, (serial (), f)));
|
haftmann@24219
|
774 |
|
haftmann@24219
|
775 |
fun del_inline_proc name =
|
haftmann@24219
|
776 |
(map_exec_purge NONE o map_thmproc o apfst o apfst o apsnd)
|
haftmann@24219
|
777 |
(delete_force "inline procedure" name);
|
haftmann@24219
|
778 |
|
haftmann@24219
|
779 |
fun add_preproc (name, f) =
|
haftmann@24219
|
780 |
(map_exec_purge NONE o map_thmproc o apfst o apsnd)
|
haftmann@24219
|
781 |
(AList.update (op =) (name, (serial (), f)));
|
haftmann@24219
|
782 |
|
haftmann@24219
|
783 |
fun del_preproc name =
|
haftmann@24219
|
784 |
(map_exec_purge NONE o map_thmproc o apfst o apsnd)
|
haftmann@24219
|
785 |
(delete_force "preprocessor" name);
|
haftmann@24219
|
786 |
|
haftmann@24219
|
787 |
fun add_post thm thy =
|
haftmann@24219
|
788 |
(map_exec_purge NONE o map_thmproc o apsnd)
|
haftmann@24219
|
789 |
(insert Thm.eq_thm_prop (CodeUnit.error_thm CodeUnit.mk_rew thm)) thy;
|
haftmann@24219
|
790 |
(*fully applied in order to get right context for mk_rew!*)
|
haftmann@24219
|
791 |
|
haftmann@24219
|
792 |
fun del_post thm thy =
|
haftmann@24219
|
793 |
(map_exec_purge NONE o map_thmproc o apsnd)
|
haftmann@24219
|
794 |
(remove Thm.eq_thm_prop (CodeUnit.error_thm CodeUnit.mk_rew thm)) thy;
|
haftmann@24219
|
795 |
(*fully applied in order to get right context for mk_rew!*)
|
haftmann@24219
|
796 |
|
haftmann@24219
|
797 |
val _ = Context.add_setup
|
haftmann@24219
|
798 |
(let
|
haftmann@24219
|
799 |
fun mk_attribute f = Thm.declaration_attribute (fn thm => Context.mapping (f thm) I);
|
haftmann@24219
|
800 |
fun add_simple_attribute (name, f) =
|
haftmann@24219
|
801 |
add_attribute (name, Scan.succeed (mk_attribute f));
|
haftmann@24219
|
802 |
fun add_del_attribute (name, (add, del)) =
|
haftmann@24219
|
803 |
add_attribute (name, Args.del |-- Scan.succeed (mk_attribute del)
|
haftmann@24219
|
804 |
|| Scan.succeed (mk_attribute add))
|
haftmann@24219
|
805 |
in
|
haftmann@24624
|
806 |
add_del_attribute ("func", (add_func, del_func))
|
haftmann@24219
|
807 |
#> add_del_attribute ("inline", (add_inline, del_inline))
|
haftmann@24219
|
808 |
#> add_del_attribute ("post", (add_post, del_post))
|
haftmann@24219
|
809 |
end);
|
haftmann@24219
|
810 |
|
haftmann@24219
|
811 |
|
haftmann@24219
|
812 |
(** post- and preprocessing **)
|
haftmann@24219
|
813 |
|
haftmann@24219
|
814 |
local
|
haftmann@24219
|
815 |
|
haftmann@24219
|
816 |
fun gen_apply_inline_proc prep post thy f x =
|
haftmann@24219
|
817 |
let
|
haftmann@24219
|
818 |
val cts = prep x;
|
haftmann@24219
|
819 |
val rews = map CodeUnit.assert_rew (f thy cts);
|
haftmann@24219
|
820 |
in post rews x end;
|
haftmann@24219
|
821 |
|
haftmann@24219
|
822 |
val apply_inline_proc = gen_apply_inline_proc (maps
|
haftmann@24219
|
823 |
((fn [args, rhs] => rhs :: (snd o Drule.strip_comb) args) o snd o Drule.strip_comb o Thm.cprop_of))
|
haftmann@24219
|
824 |
(fn rews => map (CodeUnit.rewrite_func rews));
|
haftmann@24219
|
825 |
val apply_inline_proc_cterm = gen_apply_inline_proc single
|
haftmann@24219
|
826 |
(MetaSimplifier.rewrite false);
|
haftmann@24219
|
827 |
|
haftmann@24219
|
828 |
fun apply_preproc thy f [] = []
|
haftmann@24219
|
829 |
| apply_preproc thy f (thms as (thm :: _)) =
|
haftmann@24219
|
830 |
let
|
haftmann@24423
|
831 |
val const = const_of_func thy thm;
|
haftmann@24219
|
832 |
val thms' = f thy thms;
|
haftmann@24219
|
833 |
in certify_const thy const thms' end;
|
haftmann@24219
|
834 |
|
haftmann@24219
|
835 |
fun rhs_conv conv thm =
|
haftmann@24219
|
836 |
let
|
haftmann@24219
|
837 |
val thm' = (conv o Thm.rhs_of) thm;
|
haftmann@24219
|
838 |
in Thm.transitive thm thm' end
|
haftmann@24219
|
839 |
|
haftmann@24219
|
840 |
in
|
haftmann@24219
|
841 |
|
haftmann@24219
|
842 |
fun preprocess thy thms =
|
haftmann@24219
|
843 |
thms
|
haftmann@24219
|
844 |
|> fold (fn (_, (_, f)) => apply_preproc thy f) ((#preprocs o the_thmproc o get_exec) thy)
|
haftmann@24219
|
845 |
|> map (CodeUnit.rewrite_func ((#inlines o the_thmproc o get_exec) thy))
|
haftmann@24219
|
846 |
|> fold (fn (_, (_, f)) => apply_inline_proc thy f) ((#inline_procs o the_thmproc o get_exec) thy)
|
haftmann@24219
|
847 |
(*FIXME - must check: rewrite rule, defining equation, proper constant |> map (snd o check_func false thy) *)
|
haftmann@24423
|
848 |
|> common_typ_funcs
|
haftmann@24423
|
849 |
|> map (Conv.fconv_rule (Class.unoverload thy));
|
haftmann@24219
|
850 |
|
haftmann@24219
|
851 |
fun preprocess_conv ct =
|
haftmann@24219
|
852 |
let
|
haftmann@24219
|
853 |
val thy = Thm.theory_of_cterm ct;
|
haftmann@24219
|
854 |
in
|
haftmann@24219
|
855 |
ct
|
haftmann@24219
|
856 |
|> MetaSimplifier.rewrite false ((#inlines o the_thmproc o get_exec) thy)
|
haftmann@24219
|
857 |
|> fold (fn (_, (_, f)) => rhs_conv (apply_inline_proc_cterm thy f))
|
haftmann@24219
|
858 |
((#inline_procs o the_thmproc o get_exec) thy)
|
haftmann@24423
|
859 |
|> rhs_conv (Class.unoverload thy)
|
haftmann@24219
|
860 |
end;
|
haftmann@24219
|
861 |
|
haftmann@24219
|
862 |
fun postprocess_conv ct =
|
haftmann@24219
|
863 |
let
|
haftmann@24219
|
864 |
val thy = Thm.theory_of_cterm ct;
|
haftmann@24219
|
865 |
in
|
haftmann@24219
|
866 |
ct
|
haftmann@24423
|
867 |
|> Class.overload thy
|
haftmann@24423
|
868 |
|> rhs_conv (MetaSimplifier.rewrite false ((#posts o the_thmproc o get_exec) thy))
|
haftmann@24219
|
869 |
end;
|
haftmann@24219
|
870 |
|
haftmann@24219
|
871 |
end; (*local*)
|
haftmann@24219
|
872 |
|
haftmann@24423
|
873 |
fun default_typ_proto thy c = case Class.param_const thy c
|
haftmann@24423
|
874 |
of SOME (c, tyco) => classop_weakest_typ thy ((the o AxClass.class_of_param thy) c)
|
haftmann@24423
|
875 |
(c, tyco) |> SOME
|
haftmann@24423
|
876 |
| NONE => (case AxClass.class_of_param thy c
|
haftmann@24423
|
877 |
of SOME class => SOME (Term.map_type_tvar
|
haftmann@24423
|
878 |
(K (TVar (("'a", 0), [class]))) (Sign.the_const_type thy c))
|
haftmann@24423
|
879 |
| NONE => get_constr_typ thy c);
|
haftmann@24219
|
880 |
|
haftmann@24219
|
881 |
local
|
haftmann@24219
|
882 |
|
haftmann@24219
|
883 |
fun get_funcs thy const =
|
haftmann@24423
|
884 |
Symtab.lookup ((the_funcs o get_exec) thy) const
|
haftmann@24219
|
885 |
|> Option.map (Susp.force o fst)
|
haftmann@24219
|
886 |
|> these
|
haftmann@24219
|
887 |
|> map (Thm.transfer thy);
|
haftmann@24219
|
888 |
|
haftmann@24219
|
889 |
in
|
haftmann@24219
|
890 |
|
haftmann@24219
|
891 |
fun these_funcs thy const =
|
haftmann@24219
|
892 |
let
|
haftmann@24219
|
893 |
fun drop_refl thy = filter_out (is_equal o Term.fast_term_ord o Logic.dest_equals
|
haftmann@24219
|
894 |
o ObjectLogic.drop_judgment thy o Thm.plain_prop_of);
|
haftmann@24219
|
895 |
in
|
haftmann@24219
|
896 |
get_funcs thy const
|
haftmann@24219
|
897 |
|> preprocess thy
|
haftmann@24219
|
898 |
|> drop_refl thy
|
haftmann@24219
|
899 |
end;
|
haftmann@24219
|
900 |
|
haftmann@24423
|
901 |
fun default_typ thy c = case default_typ_proto thy c
|
haftmann@24219
|
902 |
of SOME ty => ty
|
haftmann@24423
|
903 |
| NONE => (case get_funcs thy c
|
haftmann@24423
|
904 |
of thm :: _ => snd (CodeUnit.head_func (Conv.fconv_rule (Class.unoverload thy) thm))
|
haftmann@24219
|
905 |
| [] => Sign.the_const_type thy c);
|
haftmann@24219
|
906 |
|
haftmann@24219
|
907 |
end; (*local*)
|
haftmann@24219
|
908 |
|
haftmann@24219
|
909 |
end; (*struct*)
|
haftmann@24219
|
910 |
|
haftmann@24219
|
911 |
|
haftmann@24219
|
912 |
(** type-safe interfaces for data depedent on executable content **)
|
haftmann@24219
|
913 |
|
haftmann@24219
|
914 |
functor CodeDataFun(Data: CODE_DATA_ARGS): CODE_DATA =
|
haftmann@24219
|
915 |
struct
|
haftmann@24219
|
916 |
|
haftmann@24219
|
917 |
type T = Data.T;
|
haftmann@24219
|
918 |
exception Data of T;
|
haftmann@24219
|
919 |
fun dest (Data x) = x
|
haftmann@24219
|
920 |
|
haftmann@24219
|
921 |
val kind = Code.declare_data (Data Data.empty)
|
haftmann@24219
|
922 |
(fn pp => fn (Data x1, Data x2) => Data (Data.merge pp (x1, x2)))
|
haftmann@24219
|
923 |
(fn thy_opt => fn cs => fn Data x => Data (Data.purge thy_opt cs x));
|
haftmann@24219
|
924 |
|
haftmann@24219
|
925 |
val data_op = (kind, Data, dest);
|
haftmann@24219
|
926 |
|
haftmann@24219
|
927 |
val get = Code.get_data data_op;
|
haftmann@24219
|
928 |
val change = Code.change_data data_op;
|
haftmann@24219
|
929 |
fun change_yield thy = Code.change_yield_data data_op thy;
|
haftmann@24219
|
930 |
|
haftmann@24219
|
931 |
end;
|
haftmann@24219
|
932 |
|
haftmann@24219
|
933 |
structure Code : CODE =
|
haftmann@24219
|
934 |
struct
|
haftmann@24219
|
935 |
|
haftmann@24219
|
936 |
open Code;
|
haftmann@24219
|
937 |
|
haftmann@24219
|
938 |
end;
|