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