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