haftmann@24219
|
1 |
(* Title: Pure/Isar/code.ML
|
haftmann@24219
|
2 |
Author: Florian Haftmann, TU Muenchen
|
haftmann@24219
|
3 |
|
haftmann@34160
|
4 |
Abstract executable ingredients of theory. Management of data
|
haftmann@34160
|
5 |
dependent on executable ingredients as synchronized cache; purged
|
haftmann@34160
|
6 |
on any change of underlying executable ingredients.
|
haftmann@24219
|
7 |
*)
|
haftmann@24219
|
8 |
|
haftmann@24219
|
9 |
signature CODE =
|
haftmann@24219
|
10 |
sig
|
haftmann@31957
|
11 |
(*constants*)
|
haftmann@31957
|
12 |
val check_const: theory -> term -> string
|
haftmann@31957
|
13 |
val read_bare_const: theory -> string -> string * typ
|
haftmann@31957
|
14 |
val read_const: theory -> string -> string
|
haftmann@31962
|
15 |
val string_of_const: theory -> string -> string
|
haftmann@33906
|
16 |
val cert_signature: theory -> typ -> typ
|
haftmann@33906
|
17 |
val read_signature: theory -> string -> typ
|
haftmann@33906
|
18 |
val const_typ: theory -> string -> typ
|
haftmann@33906
|
19 |
val subst_signatures: theory -> term -> term
|
haftmann@31962
|
20 |
val args_number: theory -> string -> int
|
haftmann@31957
|
21 |
|
haftmann@31156
|
22 |
(*constructor sets*)
|
haftmann@31156
|
23 |
val constrset_of_consts: theory -> (string * typ) list
|
haftmann@31156
|
24 |
-> string * ((string * sort) list * (string * typ list) list)
|
haftmann@31156
|
25 |
|
haftmann@31156
|
26 |
(*code equations*)
|
haftmann@31962
|
27 |
val mk_eqn: theory -> thm * bool -> thm * bool
|
haftmann@31962
|
28 |
val mk_eqn_warning: theory -> thm -> (thm * bool) option
|
haftmann@31962
|
29 |
val mk_eqn_liberal: theory -> thm -> (thm * bool) option
|
haftmann@31156
|
30 |
val assert_eqn: theory -> thm * bool -> thm * bool
|
haftmann@31156
|
31 |
val assert_eqns_const: theory -> string
|
haftmann@31156
|
32 |
-> (thm * bool) list -> (thm * bool) list
|
haftmann@31957
|
33 |
val const_typ_eqn: theory -> thm -> string * typ
|
haftmann@31156
|
34 |
val typscheme_eqn: theory -> thm -> (string * sort) list * typ
|
haftmann@32873
|
35 |
val typscheme_eqns: theory -> string -> thm list -> (string * sort) list * typ
|
haftmann@32929
|
36 |
val standard_typscheme: theory -> thm list -> thm list
|
haftmann@31156
|
37 |
|
haftmann@31962
|
38 |
(*executable code*)
|
haftmann@33906
|
39 |
val add_type: string -> theory -> theory
|
haftmann@33906
|
40 |
val add_type_cmd: string -> theory -> theory
|
haftmann@33906
|
41 |
val add_signature: string * typ -> theory -> theory
|
haftmann@33906
|
42 |
val add_signature_cmd: string * string -> theory -> theory
|
haftmann@31156
|
43 |
val add_datatype: (string * typ) list -> theory -> theory
|
haftmann@31156
|
44 |
val add_datatype_cmd: string list -> theory -> theory
|
haftmann@31156
|
45 |
val type_interpretation:
|
haftmann@31156
|
46 |
(string * ((string * sort) list * (string * typ list) list)
|
haftmann@31156
|
47 |
-> theory -> theory) -> theory -> theory
|
haftmann@28368
|
48 |
val add_eqn: thm -> theory -> theory
|
haftmann@31088
|
49 |
val add_nbe_eqn: thm -> theory -> theory
|
haftmann@28368
|
50 |
val add_default_eqn: thm -> theory -> theory
|
haftmann@28703
|
51 |
val add_default_eqn_attribute: attribute
|
haftmann@28703
|
52 |
val add_default_eqn_attrib: Attrib.src
|
haftmann@28368
|
53 |
val del_eqn: thm -> theory -> theory
|
haftmann@28368
|
54 |
val del_eqns: string -> theory -> theory
|
haftmann@24844
|
55 |
val add_case: thm -> theory -> theory
|
haftmann@24844
|
56 |
val add_undefined: string -> theory -> theory
|
haftmann@24219
|
57 |
val get_datatype: theory -> string -> ((string * sort) list * (string * typ list) list)
|
haftmann@24423
|
58 |
val get_datatype_of_constr: theory -> string -> string option
|
haftmann@31156
|
59 |
val these_eqns: theory -> string -> (thm * bool) list
|
haftmann@31642
|
60 |
val all_eqns: theory -> (thm * bool) list
|
haftmann@29960
|
61 |
val get_case_scheme: theory -> string -> (int * (int * string list)) option
|
haftmann@31890
|
62 |
val undefineds: theory -> string list
|
haftmann@24219
|
63 |
val print_codesetup: theory -> unit
|
haftmann@31957
|
64 |
|
haftmann@31957
|
65 |
(*infrastructure*)
|
haftmann@31998
|
66 |
val set_code_target_attr: (string -> thm -> theory -> theory) -> theory -> theory
|
haftmann@31957
|
67 |
val purge_data: theory -> theory
|
haftmann@24219
|
68 |
end;
|
haftmann@24219
|
69 |
|
haftmann@24219
|
70 |
signature CODE_DATA_ARGS =
|
haftmann@24219
|
71 |
sig
|
haftmann@24219
|
72 |
type T
|
haftmann@24219
|
73 |
val empty: T
|
haftmann@24219
|
74 |
end;
|
haftmann@24219
|
75 |
|
haftmann@24219
|
76 |
signature CODE_DATA =
|
haftmann@24219
|
77 |
sig
|
haftmann@24219
|
78 |
type T
|
haftmann@24219
|
79 |
val change: theory -> (T -> T) -> T
|
haftmann@24219
|
80 |
val change_yield: theory -> (T -> 'a * T) -> 'a * T
|
haftmann@24219
|
81 |
end;
|
haftmann@24219
|
82 |
|
haftmann@24219
|
83 |
signature PRIVATE_CODE =
|
haftmann@24219
|
84 |
sig
|
haftmann@24219
|
85 |
include CODE
|
haftmann@34160
|
86 |
val declare_data: Object.T -> serial
|
haftmann@24219
|
87 |
val change_data: serial * ('a -> Object.T) * (Object.T -> 'a)
|
haftmann@24219
|
88 |
-> theory -> ('a -> 'a) -> 'a
|
haftmann@24219
|
89 |
val change_yield_data: serial * ('a -> Object.T) * (Object.T -> 'a)
|
haftmann@24219
|
90 |
-> theory -> ('a -> 'b * 'a) -> 'b * 'a
|
haftmann@24219
|
91 |
end;
|
haftmann@24219
|
92 |
|
haftmann@24219
|
93 |
structure Code : PRIVATE_CODE =
|
haftmann@24219
|
94 |
struct
|
haftmann@24219
|
95 |
|
haftmann@31962
|
96 |
(** auxiliary **)
|
haftmann@31962
|
97 |
|
haftmann@31962
|
98 |
(* printing *)
|
haftmann@31156
|
99 |
|
wenzelm@32966
|
100 |
fun string_of_typ thy = setmp_CRITICAL show_sorts true (Syntax.string_of_typ_global thy);
|
haftmann@31962
|
101 |
|
haftmann@31156
|
102 |
fun string_of_const thy c = case AxClass.inst_of_param thy c
|
haftmann@31156
|
103 |
of SOME (c, tyco) => Sign.extern_const thy c ^ " " ^ enclose "[" "]" (Sign.extern_type thy tyco)
|
haftmann@31156
|
104 |
| NONE => Sign.extern_const thy c;
|
haftmann@31156
|
105 |
|
haftmann@31156
|
106 |
|
haftmann@31962
|
107 |
(* constants *)
|
haftmann@31156
|
108 |
|
haftmann@33906
|
109 |
fun typ_equiv tys = Type.raw_instance tys andalso Type.raw_instance (swap tys);
|
haftmann@33906
|
110 |
|
haftmann@31962
|
111 |
fun check_bare_const thy t = case try dest_Const t
|
haftmann@31962
|
112 |
of SOME c_ty => c_ty
|
haftmann@31962
|
113 |
| NONE => error ("Not a constant: " ^ Syntax.string_of_term_global thy t);
|
haftmann@31962
|
114 |
|
haftmann@31962
|
115 |
fun check_const thy = AxClass.unoverload_const thy o check_bare_const thy;
|
haftmann@31962
|
116 |
|
haftmann@31962
|
117 |
fun read_bare_const thy = check_bare_const thy o Syntax.read_term_global thy;
|
haftmann@31962
|
118 |
|
haftmann@31962
|
119 |
fun read_const thy = AxClass.unoverload_const thy o read_bare_const thy;
|
haftmann@31156
|
120 |
|
haftmann@32872
|
121 |
|
haftmann@31962
|
122 |
|
haftmann@31962
|
123 |
(** data store **)
|
haftmann@31962
|
124 |
|
haftmann@31962
|
125 |
(* code equations *)
|
haftmann@31962
|
126 |
|
haftmann@33003
|
127 |
type eqns = bool * (thm * bool) list;
|
haftmann@33003
|
128 |
(*default flag, theorems with proper flag *)
|
haftmann@31962
|
129 |
|
haftmann@31962
|
130 |
fun add_drop_redundant thy (thm, proper) thms =
|
haftmann@31962
|
131 |
let
|
haftmann@31962
|
132 |
val args_of = snd o strip_comb o map_types Type.strip_sorts
|
haftmann@31962
|
133 |
o fst o Logic.dest_equals o Thm.plain_prop_of;
|
haftmann@31962
|
134 |
val args = args_of thm;
|
haftmann@31962
|
135 |
val incr_idx = Logic.incr_indexes ([], Thm.maxidx_of thm + 1);
|
haftmann@31962
|
136 |
fun matches_args args' = length args <= length args' andalso
|
haftmann@33956
|
137 |
Pattern.matchess thy (args, (map incr_idx o take (length args)) args');
|
haftmann@31962
|
138 |
fun drop (thm', proper') = if (proper orelse not proper')
|
haftmann@31962
|
139 |
andalso matches_args (args_of thm') then
|
wenzelm@32111
|
140 |
(warning ("Code generator: dropping redundant code equation\n" ^
|
wenzelm@32111
|
141 |
Display.string_of_thm_global thy thm'); true)
|
haftmann@31962
|
142 |
else false;
|
haftmann@31962
|
143 |
in (thm, proper) :: filter_out drop thms end;
|
haftmann@31962
|
144 |
|
haftmann@33003
|
145 |
fun add_thm thy _ thm (false, thms) = (false, add_drop_redundant thy thm thms)
|
haftmann@33003
|
146 |
| add_thm thy true thm (true, thms) = (true, thms @ [thm])
|
haftmann@33003
|
147 |
| add_thm thy false thm (true, thms) = (false, [thm]);
|
haftmann@31962
|
148 |
|
haftmann@33003
|
149 |
fun del_thm thm = apsnd (remove (eq_fst Thm.eq_thm_prop) (thm, true));
|
haftmann@31962
|
150 |
|
haftmann@31962
|
151 |
|
haftmann@31962
|
152 |
(* executable code data *)
|
haftmann@31962
|
153 |
|
haftmann@31962
|
154 |
datatype spec = Spec of {
|
haftmann@31962
|
155 |
history_concluded: bool,
|
haftmann@33906
|
156 |
signatures: int Symtab.table * typ Symtab.table,
|
haftmann@31962
|
157 |
eqns: ((bool * eqns) * (serial * eqns) list) Symtab.table
|
haftmann@31962
|
158 |
(*with explicit history*),
|
haftmann@31962
|
159 |
dtyps: ((serial * ((string * sort) list * (string * typ list) list)) list) Symtab.table
|
haftmann@31962
|
160 |
(*with explicit history*),
|
haftmann@31962
|
161 |
cases: (int * (int * string list)) Symtab.table * unit Symtab.table
|
haftmann@31962
|
162 |
};
|
haftmann@31962
|
163 |
|
haftmann@33906
|
164 |
fun make_spec (history_concluded, ((signatures, eqns), (dtyps, cases))) =
|
haftmann@33906
|
165 |
Spec { history_concluded = history_concluded,
|
haftmann@33906
|
166 |
signatures = signatures, eqns = eqns, dtyps = dtyps, cases = cases };
|
haftmann@33906
|
167 |
fun map_spec f (Spec { history_concluded = history_concluded, signatures = signatures,
|
haftmann@33906
|
168 |
eqns = eqns, dtyps = dtyps, cases = cases }) =
|
haftmann@33906
|
169 |
make_spec (f (history_concluded, ((signatures, eqns), (dtyps, cases))));
|
haftmann@33906
|
170 |
fun merge_spec (Spec { history_concluded = _, signatures = (tycos1, sigs1), eqns = eqns1,
|
haftmann@31962
|
171 |
dtyps = dtyps1, cases = (cases1, undefs1) },
|
haftmann@33906
|
172 |
Spec { history_concluded = _, signatures = (tycos2, sigs2), eqns = eqns2,
|
haftmann@31962
|
173 |
dtyps = dtyps2, cases = (cases2, undefs2) }) =
|
haftmann@31962
|
174 |
let
|
haftmann@33906
|
175 |
val signatures = (Symtab.merge (op =) (tycos1, tycos2),
|
haftmann@33906
|
176 |
Symtab.merge typ_equiv (sigs1, sigs2));
|
haftmann@31962
|
177 |
fun merge_eqns ((_, history1), (_, history2)) =
|
haftmann@31962
|
178 |
let
|
haftmann@31962
|
179 |
val raw_history = AList.merge (op = : serial * serial -> bool)
|
haftmann@31962
|
180 |
(K true) (history1, history2)
|
haftmann@31962
|
181 |
val filtered_history = filter_out (fst o snd) raw_history
|
haftmann@31962
|
182 |
val history = if null filtered_history
|
haftmann@31962
|
183 |
then raw_history else filtered_history;
|
haftmann@31962
|
184 |
in ((false, (snd o hd) history), history) end;
|
haftmann@31962
|
185 |
val eqns = Symtab.join (K merge_eqns) (eqns1, eqns2);
|
haftmann@31962
|
186 |
val dtyps = Symtab.join (K (AList.merge (op =) (K true))) (dtyps1, dtyps2);
|
haftmann@31962
|
187 |
val cases = (Symtab.merge (K true) (cases1, cases2),
|
haftmann@31962
|
188 |
Symtab.merge (K true) (undefs1, undefs2));
|
haftmann@33906
|
189 |
in make_spec (false, ((signatures, eqns), (dtyps, cases))) end;
|
haftmann@31962
|
190 |
|
haftmann@31962
|
191 |
fun history_concluded (Spec { history_concluded, ... }) = history_concluded;
|
haftmann@33906
|
192 |
fun the_signatures (Spec { signatures, ... }) = signatures;
|
haftmann@31962
|
193 |
fun the_eqns (Spec { eqns, ... }) = eqns;
|
haftmann@31962
|
194 |
fun the_dtyps (Spec { dtyps, ... }) = dtyps;
|
haftmann@31962
|
195 |
fun the_cases (Spec { cases, ... }) = cases;
|
haftmann@32544
|
196 |
val map_history_concluded = map_spec o apfst;
|
haftmann@33906
|
197 |
val map_signatures = map_spec o apsnd o apfst o apfst;
|
haftmann@33906
|
198 |
val map_eqns = map_spec o apsnd o apfst o apsnd;
|
haftmann@31962
|
199 |
val map_dtyps = map_spec o apsnd o apsnd o apfst;
|
haftmann@31962
|
200 |
val map_cases = map_spec o apsnd o apsnd o apsnd;
|
haftmann@31962
|
201 |
|
haftmann@31962
|
202 |
|
haftmann@31962
|
203 |
(* data slots dependent on executable code *)
|
haftmann@31962
|
204 |
|
haftmann@31962
|
205 |
(*private copy avoids potential conflict of table exceptions*)
|
wenzelm@31972
|
206 |
structure Datatab = Table(type key = int val ord = int_ord);
|
haftmann@31962
|
207 |
|
haftmann@31962
|
208 |
local
|
haftmann@31962
|
209 |
|
haftmann@34160
|
210 |
type kind = { empty: Object.T };
|
haftmann@31962
|
211 |
|
wenzelm@32738
|
212 |
val kinds = Unsynchronized.ref (Datatab.empty: kind Datatab.table);
|
haftmann@31962
|
213 |
|
haftmann@31962
|
214 |
fun invoke f k = case Datatab.lookup (! kinds) k
|
haftmann@31962
|
215 |
of SOME kind => f kind
|
haftmann@31962
|
216 |
| NONE => sys_error "Invalid code data identifier";
|
haftmann@31962
|
217 |
|
haftmann@31962
|
218 |
in
|
haftmann@31962
|
219 |
|
haftmann@34160
|
220 |
fun declare_data empty =
|
haftmann@31962
|
221 |
let
|
haftmann@31962
|
222 |
val k = serial ();
|
haftmann@34160
|
223 |
val kind = { empty = empty };
|
haftmann@34160
|
224 |
val _ = CRITICAL (fn () => Unsynchronized.change kinds (Datatab.update (k, kind)));
|
haftmann@31962
|
225 |
in k end;
|
haftmann@31962
|
226 |
|
haftmann@31962
|
227 |
fun invoke_init k = invoke (fn kind => #empty kind) k;
|
haftmann@31962
|
228 |
|
haftmann@31962
|
229 |
end; (*local*)
|
haftmann@31962
|
230 |
|
haftmann@31962
|
231 |
|
haftmann@31962
|
232 |
(* theory store *)
|
haftmann@31962
|
233 |
|
haftmann@31962
|
234 |
local
|
haftmann@31962
|
235 |
|
haftmann@31962
|
236 |
type data = Object.T Datatab.table;
|
haftmann@34160
|
237 |
fun create_data data = Synchronized.var "code data" data;
|
haftmann@34160
|
238 |
fun empty_data () = create_data Datatab.empty;
|
haftmann@31962
|
239 |
|
haftmann@31962
|
240 |
structure Code_Data = TheoryDataFun
|
haftmann@31962
|
241 |
(
|
haftmann@34160
|
242 |
type T = spec * data Synchronized.var;
|
haftmann@33906
|
243 |
val empty = (make_spec (false, (((Symtab.empty, Symtab.empty), Symtab.empty),
|
haftmann@34160
|
244 |
(Symtab.empty, (Symtab.empty, Symtab.empty)))), empty_data ());
|
haftmann@34160
|
245 |
fun copy (spec, data) = (spec, create_data (Synchronized.value data));
|
haftmann@31962
|
246 |
val extend = copy;
|
haftmann@33968
|
247 |
fun merge _ ((spec1, _), (spec2, _)) =
|
haftmann@34160
|
248 |
(merge_spec (spec1, spec2), empty_data ());
|
haftmann@31962
|
249 |
);
|
haftmann@31962
|
250 |
|
haftmann@31962
|
251 |
fun thy_data f thy = f ((snd o Code_Data.get) thy);
|
haftmann@31962
|
252 |
|
haftmann@34160
|
253 |
fun get_ensure_init kind data =
|
haftmann@34160
|
254 |
case Datatab.lookup (Synchronized.value data) kind
|
haftmann@31962
|
255 |
of SOME x => x
|
haftmann@31962
|
256 |
| NONE => let val y = invoke_init kind
|
haftmann@34160
|
257 |
in (Synchronized.change data (Datatab.update (kind, y)); y) end;
|
haftmann@31962
|
258 |
|
haftmann@31962
|
259 |
in
|
haftmann@31962
|
260 |
|
haftmann@31962
|
261 |
(* access to executable code *)
|
haftmann@31962
|
262 |
|
haftmann@31962
|
263 |
val the_exec = fst o Code_Data.get;
|
haftmann@31962
|
264 |
|
haftmann@34160
|
265 |
fun map_exec_purge f =
|
haftmann@34160
|
266 |
Code_Data.map (fn (exec, data) => (f exec, empty_data ()));
|
haftmann@31962
|
267 |
|
haftmann@34160
|
268 |
val purge_data = (Code_Data.map o apsnd) (fn _ => empty_data ());
|
haftmann@31962
|
269 |
|
haftmann@34160
|
270 |
fun change_eqns delete c f = (map_exec_purge o map_eqns
|
haftmann@33003
|
271 |
o (if delete then Symtab.map_entry c else Symtab.map_default (c, ((false, (true, [])), [])))
|
haftmann@31962
|
272 |
o apfst) (fn (_, eqns) => (true, f eqns));
|
haftmann@31962
|
273 |
|
haftmann@33003
|
274 |
fun del_eqns c = change_eqns true c (K (false, []));
|
haftmann@31962
|
275 |
|
haftmann@31962
|
276 |
|
haftmann@31962
|
277 |
(* tackling equation history *)
|
haftmann@31962
|
278 |
|
haftmann@31962
|
279 |
fun get_eqns thy c =
|
haftmann@31962
|
280 |
Symtab.lookup ((the_eqns o the_exec) thy) c
|
haftmann@33003
|
281 |
|> Option.map (snd o snd o fst)
|
haftmann@31962
|
282 |
|> these;
|
haftmann@31962
|
283 |
|
haftmann@31962
|
284 |
fun continue_history thy = if (history_concluded o the_exec) thy
|
haftmann@31962
|
285 |
then thy
|
haftmann@31962
|
286 |
|> (Code_Data.map o apfst o map_history_concluded) (K false)
|
haftmann@31962
|
287 |
|> SOME
|
haftmann@31962
|
288 |
else NONE;
|
haftmann@31962
|
289 |
|
haftmann@31962
|
290 |
fun conclude_history thy = if (history_concluded o the_exec) thy
|
haftmann@31962
|
291 |
then NONE
|
haftmann@31962
|
292 |
else thy
|
haftmann@31962
|
293 |
|> (Code_Data.map o apfst)
|
haftmann@31962
|
294 |
((map_eqns o Symtab.map) (fn ((changed, current), history) =>
|
haftmann@31962
|
295 |
((false, current),
|
haftmann@31962
|
296 |
if changed then (serial (), current) :: history else history))
|
haftmann@31962
|
297 |
#> map_history_concluded (K true))
|
haftmann@31962
|
298 |
|> SOME;
|
haftmann@31962
|
299 |
|
haftmann@31962
|
300 |
val _ = Context.>> (Context.map_theory (Code_Data.init
|
haftmann@31962
|
301 |
#> Theory.at_begin continue_history
|
haftmann@31962
|
302 |
#> Theory.at_end conclude_history));
|
haftmann@31962
|
303 |
|
haftmann@31962
|
304 |
|
haftmann@31962
|
305 |
(* access to data dependent on abstract executable code *)
|
haftmann@31962
|
306 |
|
haftmann@31962
|
307 |
fun change_data (kind, mk, dest) =
|
haftmann@31962
|
308 |
let
|
haftmann@31962
|
309 |
fun chnge data_ref f =
|
haftmann@31962
|
310 |
let
|
haftmann@31962
|
311 |
val data = get_ensure_init kind data_ref;
|
haftmann@31962
|
312 |
val data' = f (dest data);
|
haftmann@34160
|
313 |
in (Synchronized.change data_ref (Datatab.update (kind, mk data')); data') end;
|
haftmann@31962
|
314 |
in thy_data chnge end;
|
haftmann@31962
|
315 |
|
haftmann@31962
|
316 |
fun change_yield_data (kind, mk, dest) =
|
haftmann@31962
|
317 |
let
|
haftmann@31962
|
318 |
fun chnge data_ref f =
|
haftmann@31962
|
319 |
let
|
haftmann@31962
|
320 |
val data = get_ensure_init kind data_ref;
|
haftmann@31962
|
321 |
val (x, data') = f (dest data);
|
haftmann@34160
|
322 |
in (x, (Synchronized.change data_ref (Datatab.update (kind, mk data')); data')) end;
|
haftmann@31962
|
323 |
in thy_data chnge end;
|
haftmann@31962
|
324 |
|
haftmann@31962
|
325 |
end; (*local*)
|
haftmann@31962
|
326 |
|
haftmann@31962
|
327 |
|
haftmann@31962
|
328 |
(** foundation **)
|
haftmann@31962
|
329 |
|
haftmann@31962
|
330 |
(* constants *)
|
haftmann@31962
|
331 |
|
haftmann@33906
|
332 |
fun arity_number thy tyco = case Symtab.lookup ((fst o the_signatures o the_exec) thy) tyco
|
haftmann@33906
|
333 |
of SOME n => n
|
haftmann@33906
|
334 |
| NONE => Sign.arity_number thy tyco;
|
haftmann@33906
|
335 |
|
haftmann@33906
|
336 |
fun build_tsig thy =
|
haftmann@33906
|
337 |
let
|
haftmann@33906
|
338 |
val (tycos, _) = (the_signatures o the_exec) thy;
|
haftmann@33906
|
339 |
val decls = (#types o Type.rep_tsig o Sign.tsig_of) thy
|
haftmann@33906
|
340 |
|> apsnd (Symtab.fold (fn (tyco, n) =>
|
haftmann@33906
|
341 |
Symtab.update (tyco, Type.LogicalType n)) tycos);
|
haftmann@33906
|
342 |
in Type.build_tsig ((Name_Space.empty "", Sorts.empty_algebra), [], decls) end;
|
haftmann@33906
|
343 |
|
haftmann@33906
|
344 |
fun cert_signature thy = Logic.varifyT o Type.cert_typ (build_tsig thy) o Type.no_tvars;
|
haftmann@33906
|
345 |
|
haftmann@33906
|
346 |
fun read_signature thy = cert_signature thy o Type.strip_sorts
|
haftmann@33906
|
347 |
o Syntax.parse_typ (ProofContext.init thy);
|
haftmann@33906
|
348 |
|
haftmann@33906
|
349 |
fun expand_signature thy = Type.cert_typ_mode Type.mode_syntax (Sign.tsig_of thy);
|
haftmann@33906
|
350 |
|
haftmann@33906
|
351 |
fun lookup_typ thy = Symtab.lookup ((snd o the_signatures o the_exec) thy);
|
haftmann@33906
|
352 |
|
haftmann@33906
|
353 |
fun const_typ thy c = case lookup_typ thy c
|
haftmann@33906
|
354 |
of SOME ty => ty
|
haftmann@33906
|
355 |
| NONE => (Type.strip_sorts o Sign.the_const_type thy) c;
|
haftmann@33906
|
356 |
|
haftmann@33906
|
357 |
fun subst_signature thy c ty =
|
haftmann@33906
|
358 |
let
|
haftmann@33906
|
359 |
fun mk_subst (Type (tyco, tys1)) (ty2 as Type (tyco2, tys2)) =
|
haftmann@33906
|
360 |
fold2 mk_subst tys1 tys2
|
haftmann@33906
|
361 |
| mk_subst ty (TVar (v, sort)) = Vartab.update (v, ([], ty))
|
haftmann@33906
|
362 |
in case lookup_typ thy c
|
haftmann@33906
|
363 |
of SOME ty' => Envir.subst_type (mk_subst ty (expand_signature thy ty') Vartab.empty) ty'
|
haftmann@33906
|
364 |
| NONE => ty
|
haftmann@33906
|
365 |
end;
|
haftmann@33906
|
366 |
|
haftmann@33906
|
367 |
fun subst_signatures thy = map_aterms (fn Const (c, ty) => Const (c, subst_signature thy c ty) | t => t);
|
haftmann@33906
|
368 |
|
haftmann@33906
|
369 |
fun args_number thy = length o fst o strip_type o const_typ thy;
|
haftmann@31962
|
370 |
|
haftmann@31962
|
371 |
|
haftmann@31962
|
372 |
(* datatypes *)
|
haftmann@31156
|
373 |
|
haftmann@31156
|
374 |
fun constrset_of_consts thy cs =
|
haftmann@31156
|
375 |
let
|
haftmann@31156
|
376 |
val _ = map (fn (c, _) => if (is_some o AxClass.class_of_param thy) c
|
haftmann@31156
|
377 |
then error ("Is a class parameter: " ^ string_of_const thy c) else ()) cs;
|
haftmann@33531
|
378 |
fun no_constr s (c, ty) = error ("Not a datatype constructor:\n" ^ string_of_const thy c
|
haftmann@33531
|
379 |
^ " :: " ^ string_of_typ thy ty ^ "\n" ^ enclose "(" ")" s);
|
haftmann@31156
|
380 |
fun last_typ c_ty ty =
|
haftmann@31156
|
381 |
let
|
haftmann@33531
|
382 |
val tfrees = Term.add_tfreesT ty [];
|
haftmann@31156
|
383 |
val (tyco, vs) = ((apsnd o map) (dest_TFree) o dest_Type o snd o strip_type) ty
|
haftmann@33531
|
384 |
handle TYPE _ => no_constr "bad type" c_ty
|
haftmann@33531
|
385 |
val _ = if has_duplicates (eq_fst (op =)) vs
|
haftmann@33531
|
386 |
then no_constr "duplicate type variables in datatype" c_ty else ();
|
haftmann@33531
|
387 |
val _ = if length tfrees <> length vs
|
haftmann@33531
|
388 |
then no_constr "type variables missing in datatype" c_ty else ();
|
haftmann@31156
|
389 |
in (tyco, vs) end;
|
haftmann@33906
|
390 |
fun ty_sorts (c, raw_ty) =
|
haftmann@31156
|
391 |
let
|
haftmann@33906
|
392 |
val ty = subst_signature thy c raw_ty;
|
haftmann@33906
|
393 |
val ty_decl = (Logic.unvarifyT o const_typ thy) c;
|
haftmann@31156
|
394 |
val (tyco, _) = last_typ (c, ty) ty_decl;
|
haftmann@31156
|
395 |
val (_, vs) = last_typ (c, ty) ty;
|
haftmann@31156
|
396 |
in ((tyco, map snd vs), (c, (map fst vs, ty))) end;
|
haftmann@31156
|
397 |
fun add ((tyco', sorts'), c) ((tyco, sorts), cs) =
|
haftmann@31156
|
398 |
let
|
haftmann@31998
|
399 |
val _ = if (tyco' : string) <> tyco
|
haftmann@31156
|
400 |
then error "Different type constructors in constructor set"
|
haftmann@31156
|
401 |
else ();
|
haftmann@31156
|
402 |
val sorts'' = map2 (curry (Sorts.inter_sort (Sign.classes_of thy))) sorts' sorts
|
haftmann@31156
|
403 |
in ((tyco, sorts), c :: cs) end;
|
haftmann@31156
|
404 |
fun inst vs' (c, (vs, ty)) =
|
haftmann@31156
|
405 |
let
|
haftmann@31156
|
406 |
val the_v = the o AList.lookup (op =) (vs ~~ vs');
|
haftmann@31156
|
407 |
val ty' = map_atyps (fn TFree (v, _) => TFree (the_v v)) ty;
|
haftmann@31156
|
408 |
in (c, (fst o strip_type) ty') end;
|
haftmann@31156
|
409 |
val c' :: cs' = map ty_sorts cs;
|
haftmann@31156
|
410 |
val ((tyco, sorts), cs'') = fold add cs' (apsnd single c');
|
haftmann@31156
|
411 |
val vs = Name.names Name.context Name.aT sorts;
|
haftmann@31156
|
412 |
val cs''' = map (inst vs) cs'';
|
haftmann@31156
|
413 |
in (tyco, (vs, rev cs''')) end;
|
haftmann@31156
|
414 |
|
haftmann@31962
|
415 |
fun get_datatype thy tyco =
|
haftmann@31962
|
416 |
case these (Symtab.lookup ((the_dtyps o the_exec) thy) tyco)
|
haftmann@31962
|
417 |
of (_, spec) :: _ => spec
|
haftmann@33906
|
418 |
| [] => arity_number thy tyco
|
haftmann@31962
|
419 |
|> Name.invents Name.context Name.aT
|
haftmann@31962
|
420 |
|> map (rpair [])
|
haftmann@31962
|
421 |
|> rpair [];
|
haftmann@31962
|
422 |
|
haftmann@31962
|
423 |
fun get_datatype_of_constr thy c =
|
haftmann@33906
|
424 |
case (snd o strip_type o const_typ thy) c
|
haftmann@31962
|
425 |
of Type (tyco, _) => if member (op =) ((map fst o snd o get_datatype thy) tyco) c
|
haftmann@31962
|
426 |
then SOME tyco else NONE
|
haftmann@31962
|
427 |
| _ => NONE;
|
haftmann@31962
|
428 |
|
haftmann@31962
|
429 |
fun is_constr thy = is_some o get_datatype_of_constr thy;
|
haftmann@31962
|
430 |
|
haftmann@31156
|
431 |
|
haftmann@31156
|
432 |
(* code equations *)
|
haftmann@31156
|
433 |
|
haftmann@31156
|
434 |
exception BAD_THM of string;
|
haftmann@31156
|
435 |
fun bad_thm msg = raise BAD_THM msg;
|
haftmann@31156
|
436 |
fun error_thm f thm = f thm handle BAD_THM msg => error msg;
|
haftmann@31642
|
437 |
fun warning_thm f thm = SOME (f thm) handle BAD_THM msg => (warning msg; NONE)
|
haftmann@31156
|
438 |
fun try_thm f thm = SOME (f thm) handle BAD_THM _ => NONE;
|
haftmann@31156
|
439 |
|
haftmann@31156
|
440 |
fun is_linear thm =
|
haftmann@31156
|
441 |
let val (_, args) = (strip_comb o fst o Logic.dest_equals o Thm.plain_prop_of) thm
|
haftmann@31156
|
442 |
in not (has_duplicates (op =) ((fold o fold_aterms)
|
haftmann@31156
|
443 |
(fn Var (v, _) => cons v | _ => I) args [])) end;
|
haftmann@31156
|
444 |
|
haftmann@31962
|
445 |
fun gen_assert_eqn thy is_constr_pat (thm, proper) =
|
haftmann@31156
|
446 |
let
|
haftmann@31156
|
447 |
val (lhs, rhs) = (Logic.dest_equals o Thm.plain_prop_of) thm
|
wenzelm@32111
|
448 |
handle TERM _ => bad_thm ("Not an equation: " ^ Display.string_of_thm_global thy thm)
|
wenzelm@32111
|
449 |
| THM _ => bad_thm ("Not an equation: " ^ Display.string_of_thm_global thy thm);
|
haftmann@31156
|
450 |
fun vars_of t = fold_aterms (fn Var (v, _) => insert (op =) v
|
haftmann@31156
|
451 |
| Free _ => bad_thm ("Illegal free variable in equation\n"
|
wenzelm@32111
|
452 |
^ Display.string_of_thm_global thy thm)
|
haftmann@31156
|
453 |
| _ => I) t [];
|
haftmann@31156
|
454 |
fun tvars_of t = fold_term_types (fn _ =>
|
haftmann@31156
|
455 |
fold_atyps (fn TVar (v, _) => insert (op =) v
|
haftmann@31156
|
456 |
| TFree _ => bad_thm
|
wenzelm@32111
|
457 |
("Illegal free type variable in equation\n" ^ Display.string_of_thm_global thy thm))) t [];
|
haftmann@31156
|
458 |
val lhs_vs = vars_of lhs;
|
haftmann@31156
|
459 |
val rhs_vs = vars_of rhs;
|
haftmann@31156
|
460 |
val lhs_tvs = tvars_of lhs;
|
haftmann@31156
|
461 |
val rhs_tvs = tvars_of rhs;
|
haftmann@31156
|
462 |
val _ = if null (subtract (op =) lhs_vs rhs_vs)
|
haftmann@31156
|
463 |
then ()
|
haftmann@31156
|
464 |
else bad_thm ("Free variables on right hand side of equation\n"
|
wenzelm@32111
|
465 |
^ Display.string_of_thm_global thy thm);
|
haftmann@31156
|
466 |
val _ = if null (subtract (op =) lhs_tvs rhs_tvs)
|
haftmann@31156
|
467 |
then ()
|
haftmann@31156
|
468 |
else bad_thm ("Free type variables on right hand side of equation\n"
|
wenzelm@32111
|
469 |
^ Display.string_of_thm_global thy thm)
|
wenzelm@32111
|
470 |
val (head, args) = (strip_comb o fst o Logic.dest_equals o Thm.plain_prop_of) thm;
|
haftmann@31156
|
471 |
val (c, ty) = case head
|
haftmann@31156
|
472 |
of Const (c_ty as (_, ty)) => (AxClass.unoverload_const thy c_ty, ty)
|
wenzelm@32111
|
473 |
| _ => bad_thm ("Equation not headed by constant\n" ^ Display.string_of_thm_global thy thm);
|
haftmann@31156
|
474 |
fun check _ (Abs _) = bad_thm
|
haftmann@31156
|
475 |
("Abstraction on left hand side of equation\n"
|
wenzelm@32111
|
476 |
^ Display.string_of_thm_global thy thm)
|
haftmann@31156
|
477 |
| check 0 (Var _) = ()
|
haftmann@31156
|
478 |
| check _ (Var _) = bad_thm
|
haftmann@31156
|
479 |
("Variable with application on left hand side of equation\n"
|
wenzelm@32111
|
480 |
^ Display.string_of_thm_global thy thm)
|
haftmann@31156
|
481 |
| check n (t1 $ t2) = (check (n+1) t1; check 0 t2)
|
haftmann@33906
|
482 |
| check n (Const (c_ty as (_, ty))) =
|
haftmann@33906
|
483 |
let
|
haftmann@33906
|
484 |
val c' = AxClass.unoverload_const thy c_ty
|
haftmann@33906
|
485 |
in if n = (length o fst o strip_type o subst_signature thy c') ty
|
haftmann@33906
|
486 |
then if not proper orelse is_constr_pat c'
|
haftmann@33906
|
487 |
then ()
|
haftmann@33906
|
488 |
else bad_thm (quote c ^ " is not a constructor, on left hand side of equation\n"
|
haftmann@33906
|
489 |
^ Display.string_of_thm_global thy thm)
|
haftmann@33906
|
490 |
else bad_thm
|
haftmann@33906
|
491 |
("Partially applied constant " ^ quote c ^ " on left hand side of equation\n"
|
haftmann@33906
|
492 |
^ Display.string_of_thm_global thy thm)
|
haftmann@33906
|
493 |
end;
|
haftmann@31156
|
494 |
val _ = map (check 0) args;
|
haftmann@31156
|
495 |
val _ = if not proper orelse is_linear thm then ()
|
haftmann@31156
|
496 |
else bad_thm ("Duplicate variables on left hand side of equation\n"
|
wenzelm@32111
|
497 |
^ Display.string_of_thm_global thy thm);
|
haftmann@31156
|
498 |
val _ = if (is_none o AxClass.class_of_param thy) c
|
haftmann@31156
|
499 |
then ()
|
haftmann@33906
|
500 |
else bad_thm ("Overloaded constant as head in equation\n"
|
wenzelm@32111
|
501 |
^ Display.string_of_thm_global thy thm)
|
haftmann@31962
|
502 |
val _ = if not (is_constr thy c)
|
haftmann@31156
|
503 |
then ()
|
haftmann@31156
|
504 |
else bad_thm ("Constructor as head in equation\n"
|
wenzelm@32111
|
505 |
^ Display.string_of_thm_global thy thm)
|
haftmann@31156
|
506 |
val ty_decl = Sign.the_const_type thy c;
|
haftmann@31156
|
507 |
val _ = if Sign.typ_equiv thy (Type.strip_sorts ty_decl, Type.strip_sorts ty)
|
haftmann@31156
|
508 |
then () else bad_thm ("Type\n" ^ string_of_typ thy ty
|
haftmann@31156
|
509 |
^ "\nof equation\n"
|
wenzelm@32111
|
510 |
^ Display.string_of_thm_global thy thm
|
haftmann@31156
|
511 |
^ "\nis incompatible with declared function type\n"
|
haftmann@31156
|
512 |
^ string_of_typ thy ty_decl)
|
haftmann@31156
|
513 |
in (thm, proper) end;
|
haftmann@31156
|
514 |
|
haftmann@31962
|
515 |
fun assert_eqn thy = error_thm (gen_assert_eqn thy (is_constr thy));
|
haftmann@31156
|
516 |
|
haftmann@31962
|
517 |
fun meta_rewrite thy = LocalDefs.meta_rewrite_rule (ProofContext.init thy);
|
haftmann@31962
|
518 |
|
haftmann@31962
|
519 |
fun mk_eqn thy = error_thm (gen_assert_eqn thy (K true)) o
|
haftmann@31962
|
520 |
apfst (meta_rewrite thy);
|
haftmann@31962
|
521 |
|
haftmann@31962
|
522 |
fun mk_eqn_warning thy = Option.map (fn (thm, _) => (thm, is_linear thm))
|
haftmann@31962
|
523 |
o warning_thm (gen_assert_eqn thy (K true)) o rpair false o meta_rewrite thy;
|
haftmann@31962
|
524 |
|
haftmann@31962
|
525 |
fun mk_eqn_liberal thy = Option.map (fn (thm, _) => (thm, is_linear thm))
|
haftmann@31962
|
526 |
o try_thm (gen_assert_eqn thy (K true)) o rpair false o meta_rewrite thy;
|
haftmann@31156
|
527 |
|
haftmann@33906
|
528 |
val head_eqn = dest_Const o fst o strip_comb o fst o Logic.dest_equals o Thm.plain_prop_of;
|
haftmann@31156
|
529 |
|
haftmann@31957
|
530 |
fun const_typ_eqn thy thm =
|
haftmann@31156
|
531 |
let
|
haftmann@32640
|
532 |
val (c, ty) = head_eqn thm;
|
haftmann@31156
|
533 |
val c' = AxClass.unoverload_const thy (c, ty);
|
haftmann@33906
|
534 |
(*permissive wrt. to overloaded constants!*)
|
haftmann@31156
|
535 |
in (c', ty) end;
|
haftmann@33906
|
536 |
|
haftmann@31957
|
537 |
fun const_eqn thy = fst o const_typ_eqn thy;
|
haftmann@31156
|
538 |
|
haftmann@33906
|
539 |
fun raw_typscheme thy (c, ty) =
|
haftmann@32873
|
540 |
(map dest_TFree (Sign.const_typargs thy (c, ty)), Type.strip_sorts ty);
|
haftmann@33906
|
541 |
|
haftmann@33906
|
542 |
fun typscheme thy (c, ty) = raw_typscheme thy (c, subst_signature thy c ty);
|
haftmann@33906
|
543 |
|
haftmann@32873
|
544 |
fun typscheme_eqn thy = typscheme thy o apsnd Logic.unvarifyT o const_typ_eqn thy;
|
haftmann@33906
|
545 |
|
haftmann@32873
|
546 |
fun typscheme_eqns thy c [] =
|
haftmann@32873
|
547 |
let
|
haftmann@33906
|
548 |
val raw_ty = const_typ thy c;
|
haftmann@32873
|
549 |
val tvars = Term.add_tvar_namesT raw_ty [];
|
haftmann@32873
|
550 |
val tvars' = case AxClass.class_of_param thy c
|
haftmann@32873
|
551 |
of SOME class => [TFree (Name.aT, [class])]
|
haftmann@32873
|
552 |
| NONE => Name.invent_list [] Name.aT (length tvars)
|
haftmann@32873
|
553 |
|> map (fn v => TFree (v, []));
|
haftmann@32873
|
554 |
val ty = typ_subst_TVars (tvars ~~ tvars') raw_ty;
|
haftmann@33906
|
555 |
in raw_typscheme thy (c, ty) end
|
haftmann@33968
|
556 |
| typscheme_eqns thy c (thm :: _) = typscheme_eqn thy thm;
|
haftmann@32872
|
557 |
|
haftmann@31962
|
558 |
fun assert_eqns_const thy c eqns =
|
haftmann@31962
|
559 |
let
|
haftmann@31962
|
560 |
fun cert (eqn as (thm, _)) = if c = const_eqn thy thm
|
haftmann@31962
|
561 |
then eqn else error ("Wrong head of code equation,\nexpected constant "
|
wenzelm@32111
|
562 |
^ string_of_const thy c ^ "\n" ^ Display.string_of_thm_global thy thm)
|
haftmann@31962
|
563 |
in map (cert o assert_eqn thy) eqns end;
|
haftmann@31156
|
564 |
|
haftmann@32929
|
565 |
fun standard_typscheme thy thms =
|
haftmann@32354
|
566 |
let
|
haftmann@32640
|
567 |
fun tvars_of T = rev (Term.add_tvarsT T []);
|
haftmann@32640
|
568 |
val vss = map (tvars_of o snd o head_eqn) thms;
|
haftmann@32354
|
569 |
fun inter_sorts vs =
|
haftmann@32354
|
570 |
fold (curry (Sorts.inter_sort (Sign.classes_of thy)) o snd) vs [];
|
haftmann@32354
|
571 |
val sorts = map_transpose inter_sorts vss;
|
haftmann@32873
|
572 |
val vts = Name.names Name.context Name.aT sorts
|
haftmann@32354
|
573 |
|> map (fn (v, sort) => TVar ((v, 0), sort));
|
haftmann@32354
|
574 |
in map2 (fn vs => Thm.certify_instantiate (vs ~~ vts, [])) vss thms end;
|
haftmann@31962
|
575 |
|
haftmann@31962
|
576 |
fun these_eqns thy c =
|
haftmann@31962
|
577 |
get_eqns thy c
|
haftmann@31962
|
578 |
|> (map o apfst) (Thm.transfer thy)
|
haftmann@32929
|
579 |
|> burrow_fst (standard_typscheme thy);
|
haftmann@31962
|
580 |
|
haftmann@31962
|
581 |
fun all_eqns thy =
|
haftmann@31962
|
582 |
Symtab.dest ((the_eqns o the_exec) thy)
|
haftmann@33003
|
583 |
|> maps (snd o snd o fst o snd);
|
haftmann@31962
|
584 |
|
haftmann@31962
|
585 |
|
haftmann@31962
|
586 |
(* cases *)
|
haftmann@31156
|
587 |
|
haftmann@31156
|
588 |
fun case_certificate thm =
|
haftmann@31156
|
589 |
let
|
haftmann@31156
|
590 |
val ((head, raw_case_expr), cases) = (apfst Logic.dest_equals
|
haftmann@32640
|
591 |
o apsnd Logic.dest_conjunctions o Logic.dest_implies o Thm.plain_prop_of) thm;
|
haftmann@31156
|
592 |
val _ = case head of Free _ => true
|
haftmann@31156
|
593 |
| Var _ => true
|
haftmann@31156
|
594 |
| _ => raise TERM ("case_cert", []);
|
haftmann@31156
|
595 |
val ([(case_var, _)], case_expr) = Term.strip_abs_eta 1 raw_case_expr;
|
haftmann@31156
|
596 |
val (Const (case_const, _), raw_params) = strip_comb case_expr;
|
haftmann@31156
|
597 |
val n = find_index (fn Free (v, _) => v = case_var | _ => false) raw_params;
|
haftmann@31156
|
598 |
val _ = if n = ~1 then raise TERM ("case_cert", []) else ();
|
haftmann@31156
|
599 |
val params = map (fst o dest_Var) (nth_drop n raw_params);
|
haftmann@31156
|
600 |
fun dest_case t =
|
haftmann@31156
|
601 |
let
|
haftmann@31156
|
602 |
val (head' $ t_co, rhs) = Logic.dest_equals t;
|
haftmann@31156
|
603 |
val _ = if head' = head then () else raise TERM ("case_cert", []);
|
haftmann@31156
|
604 |
val (Const (co, _), args) = strip_comb t_co;
|
haftmann@31156
|
605 |
val (Var (param, _), args') = strip_comb rhs;
|
haftmann@31156
|
606 |
val _ = if args' = args then () else raise TERM ("case_cert", []);
|
haftmann@31156
|
607 |
in (param, co) end;
|
haftmann@31156
|
608 |
fun analyze_cases cases =
|
haftmann@31156
|
609 |
let
|
haftmann@31156
|
610 |
val co_list = fold (AList.update (op =) o dest_case) cases [];
|
haftmann@31156
|
611 |
in map (the o AList.lookup (op =) co_list) params end;
|
haftmann@31156
|
612 |
fun analyze_let t =
|
haftmann@31156
|
613 |
let
|
haftmann@31156
|
614 |
val (head' $ arg, Var (param', _) $ arg') = Logic.dest_equals t;
|
haftmann@31156
|
615 |
val _ = if head' = head then () else raise TERM ("case_cert", []);
|
haftmann@31156
|
616 |
val _ = if arg' = arg then () else raise TERM ("case_cert", []);
|
haftmann@31156
|
617 |
val _ = if [param'] = params then () else raise TERM ("case_cert", []);
|
haftmann@31156
|
618 |
in [] end;
|
haftmann@31156
|
619 |
fun analyze (cases as [let_case]) =
|
haftmann@31156
|
620 |
(analyze_cases cases handle Bind => analyze_let let_case)
|
haftmann@31156
|
621 |
| analyze cases = analyze_cases cases;
|
haftmann@31156
|
622 |
in (case_const, (n, analyze cases)) end;
|
haftmann@31156
|
623 |
|
haftmann@31156
|
624 |
fun case_cert thm = case_certificate thm
|
haftmann@31156
|
625 |
handle Bind => error "bad case certificate"
|
haftmann@31156
|
626 |
| TERM _ => error "bad case certificate";
|
haftmann@31156
|
627 |
|
haftmann@31962
|
628 |
fun get_case_scheme thy = Symtab.lookup ((fst o the_cases o the_exec) thy);
|
haftmann@31156
|
629 |
|
haftmann@31962
|
630 |
val undefineds = Symtab.keys o snd o the_cases o the_exec;
|
haftmann@24219
|
631 |
|
haftmann@25312
|
632 |
|
haftmann@31962
|
633 |
(* diagnostic *)
|
haftmann@24219
|
634 |
|
haftmann@24219
|
635 |
fun print_codesetup thy =
|
haftmann@24219
|
636 |
let
|
haftmann@24219
|
637 |
val ctxt = ProofContext.init thy;
|
haftmann@24844
|
638 |
val exec = the_exec thy;
|
haftmann@33003
|
639 |
fun pretty_eqns (s, (_, eqns)) =
|
haftmann@24219
|
640 |
(Pretty.block o Pretty.fbreaks) (
|
haftmann@33003
|
641 |
Pretty.str s :: map (Display.pretty_thm ctxt o fst) eqns
|
haftmann@24219
|
642 |
);
|
haftmann@24219
|
643 |
fun pretty_dtyp (s, []) =
|
haftmann@24219
|
644 |
Pretty.str s
|
haftmann@24219
|
645 |
| pretty_dtyp (s, cos) =
|
haftmann@24219
|
646 |
(Pretty.block o Pretty.breaks) (
|
haftmann@24219
|
647 |
Pretty.str s
|
haftmann@24219
|
648 |
:: Pretty.str "="
|
haftmann@31156
|
649 |
:: separate (Pretty.str "|") (map (fn (c, []) => Pretty.str (string_of_const thy c)
|
haftmann@24219
|
650 |
| (c, tys) =>
|
haftmann@24219
|
651 |
(Pretty.block o Pretty.breaks)
|
haftmann@31156
|
652 |
(Pretty.str (string_of_const thy c)
|
wenzelm@26947
|
653 |
:: Pretty.str "of"
|
wenzelm@26947
|
654 |
:: map (Pretty.quote o Syntax.pretty_typ_global thy) tys)) cos)
|
haftmann@24219
|
655 |
);
|
haftmann@28368
|
656 |
val eqns = the_eqns exec
|
haftmann@24423
|
657 |
|> Symtab.dest
|
haftmann@31156
|
658 |
|> (map o apfst) (string_of_const thy)
|
haftmann@28695
|
659 |
|> (map o apsnd) (snd o fst)
|
haftmann@24219
|
660 |
|> sort (string_ord o pairself fst);
|
haftmann@24219
|
661 |
val dtyps = the_dtyps exec
|
haftmann@24219
|
662 |
|> Symtab.dest
|
haftmann@28695
|
663 |
|> map (fn (dtco, (_, (vs, cos)) :: _) =>
|
haftmann@31957
|
664 |
(string_of_typ thy (Type (dtco, map TFree vs)), cos))
|
haftmann@24219
|
665 |
|> sort (string_ord o pairself fst)
|
haftmann@24219
|
666 |
in
|
haftmann@24219
|
667 |
(Pretty.writeln o Pretty.chunks) [
|
haftmann@24219
|
668 |
Pretty.block (
|
haftmann@29960
|
669 |
Pretty.str "code equations:"
|
haftmann@24219
|
670 |
:: Pretty.fbrk
|
haftmann@33003
|
671 |
:: (Pretty.fbreaks o map pretty_eqns) eqns
|
haftmann@24219
|
672 |
),
|
haftmann@25968
|
673 |
Pretty.block (
|
haftmann@24219
|
674 |
Pretty.str "datatypes:"
|
haftmann@24219
|
675 |
:: Pretty.fbrk
|
haftmann@24219
|
676 |
:: (Pretty.fbreaks o map pretty_dtyp) dtyps
|
haftmann@24219
|
677 |
)
|
haftmann@24219
|
678 |
]
|
haftmann@24219
|
679 |
end;
|
haftmann@24219
|
680 |
|
haftmann@24219
|
681 |
|
haftmann@31962
|
682 |
(** declaring executable ingredients **)
|
haftmann@24219
|
683 |
|
haftmann@33906
|
684 |
(* constant signatures *)
|
haftmann@33906
|
685 |
|
haftmann@33906
|
686 |
fun add_type tyco thy =
|
haftmann@33906
|
687 |
case Symtab.lookup ((snd o #types o Type.rep_tsig o Sign.tsig_of) thy) tyco
|
haftmann@33906
|
688 |
of SOME (Type.Abbreviation (vs, _, _)) =>
|
haftmann@34160
|
689 |
(map_exec_purge o map_signatures o apfst)
|
haftmann@33906
|
690 |
(Symtab.update (tyco, length vs)) thy
|
haftmann@33906
|
691 |
| _ => error ("No such type abbreviation: " ^ quote tyco);
|
haftmann@33906
|
692 |
|
haftmann@33906
|
693 |
fun add_type_cmd s thy = add_type (Sign.intern_type thy s) thy;
|
haftmann@33906
|
694 |
|
haftmann@33906
|
695 |
fun gen_add_signature prep_const prep_signature (raw_c, raw_ty) thy =
|
haftmann@33906
|
696 |
let
|
haftmann@33906
|
697 |
val c = prep_const thy raw_c;
|
haftmann@33906
|
698 |
val ty = prep_signature thy raw_ty;
|
haftmann@33906
|
699 |
val ty' = expand_signature thy ty;
|
haftmann@33906
|
700 |
val ty'' = Sign.the_const_type thy c;
|
haftmann@33906
|
701 |
val _ = if typ_equiv (ty', ty'') then () else
|
haftmann@33906
|
702 |
error ("Illegal constant signature: " ^ Syntax.string_of_typ_global thy ty);
|
haftmann@33906
|
703 |
in
|
haftmann@33906
|
704 |
thy
|
haftmann@34160
|
705 |
|> (map_exec_purge o map_signatures o apsnd) (Symtab.update (c, ty))
|
haftmann@33906
|
706 |
end;
|
haftmann@33906
|
707 |
|
haftmann@33906
|
708 |
val add_signature = gen_add_signature (K I) cert_signature;
|
haftmann@33906
|
709 |
val add_signature_cmd = gen_add_signature read_const read_signature;
|
haftmann@33906
|
710 |
|
haftmann@33906
|
711 |
|
haftmann@31962
|
712 |
(* datatypes *)
|
haftmann@24423
|
713 |
|
wenzelm@33322
|
714 |
structure Type_Interpretation =
|
wenzelm@33322
|
715 |
Interpretation(type T = string * serial val eq = eq_snd (op =) : T * T -> bool);
|
haftmann@25462
|
716 |
|
haftmann@24423
|
717 |
fun add_datatype raw_cs thy =
|
haftmann@24219
|
718 |
let
|
haftmann@25597
|
719 |
val cs = map (fn c_ty as (_, ty) => (AxClass.unoverload_const thy c_ty, ty)) raw_cs;
|
haftmann@31156
|
720 |
val (tyco, vs_cos) = constrset_of_consts thy cs;
|
haftmann@30013
|
721 |
val old_cs = (map fst o snd o get_datatype thy) tyco;
|
haftmann@30013
|
722 |
fun drop_outdated_cases cases = fold Symtab.delete_safe
|
haftmann@30013
|
723 |
(Symtab.fold (fn (c, (_, (_, cos))) =>
|
haftmann@30013
|
724 |
if exists (member (op =) old_cs) cos
|
haftmann@30013
|
725 |
then insert (op =) c else I) cases []) cases;
|
haftmann@24219
|
726 |
in
|
haftmann@24219
|
727 |
thy
|
haftmann@31091
|
728 |
|> fold (del_eqns o fst) cs
|
haftmann@34160
|
729 |
|> map_exec_purge
|
haftmann@28695
|
730 |
((map_dtyps o Symtab.map_default (tyco, [])) (cons (serial (), vs_cos))
|
haftmann@30013
|
731 |
#> (map_cases o apfst) drop_outdated_cases)
|
haftmann@31962
|
732 |
|> Type_Interpretation.data (tyco, serial ())
|
haftmann@24219
|
733 |
end;
|
haftmann@24219
|
734 |
|
haftmann@31962
|
735 |
fun type_interpretation f = Type_Interpretation.interpretation
|
haftmann@25485
|
736 |
(fn (tyco, _) => fn thy => f (tyco, get_datatype thy tyco) thy);
|
haftmann@25485
|
737 |
|
haftmann@24423
|
738 |
fun add_datatype_cmd raw_cs thy =
|
haftmann@24423
|
739 |
let
|
haftmann@31156
|
740 |
val cs = map (read_bare_const thy) raw_cs;
|
haftmann@24423
|
741 |
in add_datatype cs thy end;
|
haftmann@24219
|
742 |
|
haftmann@31962
|
743 |
|
haftmann@31962
|
744 |
(* code equations *)
|
haftmann@31962
|
745 |
|
haftmann@33075
|
746 |
fun gen_add_eqn default (thm, proper) thy =
|
haftmann@33075
|
747 |
let
|
haftmann@33075
|
748 |
val thm' = Thm.close_derivation thm;
|
haftmann@33075
|
749 |
val c = const_eqn thy thm';
|
haftmann@33075
|
750 |
in change_eqns false c (add_thm thy default (thm', proper)) thy end;
|
haftmann@31962
|
751 |
|
haftmann@31962
|
752 |
fun add_eqn thm thy =
|
haftmann@31962
|
753 |
gen_add_eqn false (mk_eqn thy (thm, true)) thy;
|
haftmann@31962
|
754 |
|
haftmann@31962
|
755 |
fun add_warning_eqn thm thy =
|
haftmann@31962
|
756 |
case mk_eqn_warning thy thm
|
haftmann@31962
|
757 |
of SOME eqn => gen_add_eqn false eqn thy
|
haftmann@31962
|
758 |
| NONE => thy;
|
haftmann@31962
|
759 |
|
haftmann@31962
|
760 |
fun add_default_eqn thm thy =
|
haftmann@31962
|
761 |
case mk_eqn_liberal thy thm
|
haftmann@31962
|
762 |
of SOME eqn => gen_add_eqn true eqn thy
|
haftmann@31962
|
763 |
| NONE => thy;
|
haftmann@31962
|
764 |
|
haftmann@31962
|
765 |
fun add_nbe_eqn thm thy =
|
haftmann@31962
|
766 |
gen_add_eqn false (mk_eqn thy (thm, false)) thy;
|
haftmann@31962
|
767 |
|
haftmann@31962
|
768 |
val add_default_eqn_attribute = Thm.declaration_attribute
|
haftmann@31962
|
769 |
(fn thm => Context.mapping (add_default_eqn thm) I);
|
haftmann@31962
|
770 |
val add_default_eqn_attrib = Attrib.internal (K add_default_eqn_attribute);
|
haftmann@31962
|
771 |
|
haftmann@31962
|
772 |
fun del_eqn thm thy = case mk_eqn_liberal thy thm
|
haftmann@31962
|
773 |
of SOME (thm, _) => change_eqns true (const_eqn thy thm) (del_thm thm) thy
|
haftmann@31962
|
774 |
| NONE => thy;
|
haftmann@31962
|
775 |
|
haftmann@32062
|
776 |
(* c.f. src/HOL/Tools/recfun_codegen.ML *)
|
haftmann@32062
|
777 |
|
wenzelm@33522
|
778 |
structure Code_Target_Attr = Theory_Data
|
wenzelm@33522
|
779 |
(
|
haftmann@31998
|
780 |
type T = (string -> thm -> theory -> theory) option;
|
haftmann@31998
|
781 |
val empty = NONE;
|
haftmann@31998
|
782 |
val extend = I;
|
wenzelm@33522
|
783 |
fun merge (f1, f2) = if is_some f1 then f1 else f2;
|
haftmann@31998
|
784 |
);
|
haftmann@31998
|
785 |
|
haftmann@32062
|
786 |
fun set_code_target_attr f = Code_Target_Attr.map (K (SOME f));
|
haftmann@32062
|
787 |
|
haftmann@32062
|
788 |
fun code_target_attr prefix thm thy =
|
haftmann@32062
|
789 |
let
|
haftmann@32062
|
790 |
val attr = the_default ((K o K) I) (Code_Target_Attr.get thy);
|
haftmann@32062
|
791 |
in thy |> add_warning_eqn thm |> attr prefix thm end;
|
haftmann@32062
|
792 |
|
haftmann@32062
|
793 |
(* setup *)
|
haftmann@31998
|
794 |
|
haftmann@31962
|
795 |
val _ = Context.>> (Context.map_theory
|
haftmann@31962
|
796 |
(let
|
haftmann@31962
|
797 |
fun mk_attribute f = Thm.declaration_attribute (fn thm => Context.mapping (f thm) I);
|
haftmann@31998
|
798 |
val code_attribute_parser =
|
haftmann@31998
|
799 |
Args.del |-- Scan.succeed (mk_attribute del_eqn)
|
haftmann@31998
|
800 |
|| Args.$$$ "nbe" |-- Scan.succeed (mk_attribute add_nbe_eqn)
|
haftmann@31998
|
801 |
|| (Args.$$$ "target" |-- Args.colon |-- Args.name >>
|
haftmann@32062
|
802 |
(mk_attribute o code_target_attr))
|
haftmann@31998
|
803 |
|| Scan.succeed (mk_attribute add_warning_eqn);
|
haftmann@31962
|
804 |
in
|
haftmann@31962
|
805 |
Type_Interpretation.init
|
haftmann@31998
|
806 |
#> Attrib.setup (Binding.name "code") (Scan.lift code_attribute_parser)
|
haftmann@31998
|
807 |
"declare theorems for code generation"
|
haftmann@31962
|
808 |
end));
|
haftmann@31962
|
809 |
|
haftmann@31962
|
810 |
|
haftmann@31962
|
811 |
(* cases *)
|
haftmann@31962
|
812 |
|
haftmann@24844
|
813 |
fun add_case thm thy =
|
haftmann@24844
|
814 |
let
|
haftmann@31156
|
815 |
val (c, (k, case_pats)) = case_cert thm;
|
haftmann@31088
|
816 |
val _ = case filter_out (is_constr thy) case_pats
|
haftmann@30013
|
817 |
of [] => ()
|
haftmann@30013
|
818 |
| cs => error ("Non-constructor(s) in case certificate: " ^ commas (map quote cs));
|
haftmann@30013
|
819 |
val entry = (1 + Int.max (1, length case_pats), (k, case_pats))
|
haftmann@34160
|
820 |
in (map_exec_purge o map_cases o apfst) (Symtab.update (c, entry)) thy end;
|
haftmann@24844
|
821 |
|
haftmann@24844
|
822 |
fun add_undefined c thy =
|
haftmann@34160
|
823 |
(map_exec_purge o map_cases o apsnd) (Symtab.update (c, ())) thy;
|
haftmann@24844
|
824 |
|
haftmann@24219
|
825 |
end; (*struct*)
|
haftmann@24219
|
826 |
|
haftmann@24219
|
827 |
|
bulwahn@32661
|
828 |
(** type-safe interfaces for data dependent on executable code **)
|
haftmann@24219
|
829 |
|
haftmann@34160
|
830 |
functor Code_Data(Data: CODE_DATA_ARGS): CODE_DATA =
|
haftmann@24219
|
831 |
struct
|
haftmann@24219
|
832 |
|
haftmann@24219
|
833 |
type T = Data.T;
|
haftmann@24219
|
834 |
exception Data of T;
|
haftmann@24219
|
835 |
fun dest (Data x) = x
|
haftmann@24219
|
836 |
|
haftmann@34160
|
837 |
val kind = Code.declare_data (Data Data.empty);
|
haftmann@24219
|
838 |
|
haftmann@24219
|
839 |
val data_op = (kind, Data, dest);
|
haftmann@24219
|
840 |
|
haftmann@24219
|
841 |
val change = Code.change_data data_op;
|
haftmann@24219
|
842 |
fun change_yield thy = Code.change_yield_data data_op thy;
|
haftmann@24219
|
843 |
|
haftmann@24219
|
844 |
end;
|
haftmann@24219
|
845 |
|
haftmann@28143
|
846 |
structure Code : CODE = struct open Code; end;
|