| author | haftmann | 
| Wed, 15 Nov 2006 17:05:38 +0100 | |
| changeset 21379 | a0561695167a | 
| parent 21338 | 56d55dd30311 | 
| child 21421 | 3436c269dd23 | 
| permissions | -rw-r--r-- | 
| 20600 | 1 | (* Title: Pure/Tools/codegen_data.ML | 
| 2 | ID: $Id$ | |
| 3 | Author: Florian Haftmann, TU Muenchen | |
| 4 | ||
| 20855 | 5 | Abstract executable content of theory. Management of data dependent on | 
| 6 | executabl content. | |
| 20600 | 7 | *) | 
| 8 | ||
| 9 | signature CODEGEN_DATA = | |
| 10 | sig | |
| 21338 | 11 | val lazy: (unit -> thm list) -> thm list Susp.T | 
| 20600 | 12 | val eval_always: bool ref | 
| 13 | ||
| 14 | val add_func: thm -> theory -> theory | |
| 21128 | 15 | val add_func_legacy: thm -> theory -> theory | 
| 20600 | 16 | val del_func: thm -> theory -> theory | 
| 21338 | 17 | val add_funcl: CodegenConsts.const * thm list Susp.T -> theory -> theory | 
| 18 | val add_datatype: string * (((string * sort) list * (string * typ list) list) * thm list Susp.T) | |
| 20600 | 19 | -> theory -> theory | 
| 20 | val del_datatype: string -> theory -> theory | |
| 21 | val add_inline: thm -> theory -> theory | |
| 22 | val del_inline: thm -> theory -> theory | |
| 20844 | 23 | val add_inline_proc: (theory -> cterm list -> thm list) -> theory -> theory | 
| 20600 | 24 | val add_preproc: (theory -> thm list -> thm list) -> theory -> theory | 
| 25 | val these_funcs: theory -> CodegenConsts.const -> thm list | |
| 26 | val get_datatype: theory -> string | |
| 27 | -> ((string * sort) list * (string * typ list) list) option | |
| 28 | val get_datatype_of_constr: theory -> CodegenConsts.const -> string option | |
| 29 | ||
| 30 | val print_thms: theory -> unit | |
| 31 | ||
| 32 | val typ_func: theory -> thm -> typ | |
| 20937 | 33 | val typ_funcs: theory -> CodegenConsts.const * thm list -> typ | 
| 20600 | 34 | val rewrite_func: thm list -> thm -> thm | 
| 21119 | 35 | val preprocess_cterm: theory -> cterm -> thm | 
| 20600 | 36 | |
| 20844 | 37 | val trace: bool ref | 
| 20600 | 38 | end; | 
| 39 | ||
| 40 | signature PRIVATE_CODEGEN_DATA = | |
| 41 | sig | |
| 42 | include CODEGEN_DATA | |
| 43 | type data | |
| 44 | structure CodeData: THEORY_DATA | |
| 45 | val declare: string -> Object.T -> (Pretty.pp -> Object.T * Object.T -> Object.T) | |
| 20937 | 46 | -> (theory option -> CodegenConsts.const list option -> Object.T -> Object.T) -> serial | 
| 20600 | 47 | val init: serial -> theory -> theory | 
| 48 | val get: serial -> (Object.T -> 'a) -> data -> 'a | |
| 49 |   val put: serial -> ('a -> Object.T) -> 'a -> data -> data
 | |
| 50 | end; | |
| 51 | ||
| 52 | structure CodegenData : PRIVATE_CODEGEN_DATA = | |
| 53 | struct | |
| 54 | ||
| 55 | (** diagnostics **) | |
| 56 | ||
| 20844 | 57 | val trace = ref false; | 
| 58 | fun tracing f x = (if !trace then Output.tracing (f x) else (); x); | |
| 20600 | 59 | |
| 60 | ||
| 61 | ||
| 62 | (** lazy theorems, certificate theorems **) | |
| 63 | ||
| 64 | val eval_always = ref false; | |
| 65 | ||
| 66 | fun lazy f = if !eval_always | |
| 67 | then Susp.value (f ()) | |
| 68 | else Susp.delay f; | |
| 69 | ||
| 70 | fun string_of_lthms r = case Susp.peek r | |
| 71 | of SOME thms => (map string_of_thm o rev) thms | |
| 72 | | NONE => ["[...]"]; | |
| 73 | ||
| 74 | fun pretty_lthms ctxt r = case Susp.peek r | |
| 21066 
ce6759d1d0b4
code nofunc now permits theorems violating typing discipline
 haftmann parents: 
20937diff
changeset | 75 | of SOME thms => map (ProofContext.pretty_thm ctxt) thms | 
| 20600 | 76 | | NONE => [Pretty.str "[...]"]; | 
| 77 | ||
| 20844 | 78 | fun certificate thy f r = | 
| 20600 | 79 | case Susp.peek r | 
| 20844 | 80 | of SOME thms => (Susp.value o f thy) thms | 
| 81 | | NONE => let | |
| 82 | val thy_ref = Theory.self_ref thy; | |
| 83 | in lazy (fn () => (f (Theory.deref thy_ref) o Susp.force) r) end; | |
| 20600 | 84 | |
| 85 | fun merge' _ ([], []) = (false, []) | |
| 86 | | merge' _ ([], ys) = (true, ys) | |
| 87 | | merge' eq (xs, ys) = fold_rev | |
| 88 | (fn y => fn (t, xs) => (t orelse not (member eq xs y), insert eq y xs)) ys (false, xs); | |
| 89 | ||
| 90 | fun merge_alist eq_key eq (xys as (xs, ys)) = | |
| 91 | if eq_list (eq_pair eq_key eq) (xs, ys) | |
| 92 | then (false, xs) | |
| 93 | else (true, AList.merge eq_key eq xys); | |
| 94 | ||
| 95 | val merge_thms = merge' eq_thm; | |
| 96 | ||
| 97 | fun merge_lthms (r1, r2) = | |
| 21119 | 98 | if Susp.same (r1, r2) | 
| 99 | then (false, r1) | |
| 20600 | 100 | else case Susp.peek r1 | 
| 101 | of SOME [] => (true, r2) | |
| 102 | | _ => case Susp.peek r2 | |
| 103 | of SOME [] => (true, r1) | |
| 104 | | _ => (apsnd (lazy o K)) (merge_thms (Susp.force r1, Susp.force r2)); | |
| 105 | ||
| 106 | ||
| 107 | ||
| 108 | (** code theorems **) | |
| 109 | ||
| 20844 | 110 | (* making rewrite theorems *) | 
| 20600 | 111 | |
| 112 | fun bad_thm msg thm = | |
| 113 | error (msg ^ ": " ^ string_of_thm thm); | |
| 114 | ||
| 20844 | 115 | fun check_rew thy thm = | 
| 116 | let | |
| 117 | val (lhs, rhs) = (Logic.dest_equals o Thm.prop_of) thm; | |
| 118 | fun vars_of t = fold_aterms | |
| 119 | (fn Var (v, _) => insert (op =) v | |
| 120 | | Free _ => bad_thm "Illegal free variable in rewrite theorem" thm | |
| 121 | | _ => I) t []; | |
| 122 | fun tvars_of t = fold_term_types | |
| 123 | (fn _ => fold_atyps (fn TVar (v, _) => insert (op =) v | |
| 124 | | TFree _ => bad_thm "Illegal free type variable in rewrite theorem" thm)) t []; | |
| 125 | val lhs_vs = vars_of lhs; | |
| 126 | val rhs_vs = vars_of rhs; | |
| 127 | val lhs_tvs = tvars_of lhs; | |
| 128 | val rhs_tvs = tvars_of lhs; | |
| 129 | val _ = if null (subtract (op =) lhs_vs rhs_vs) | |
| 130 | then () | |
| 131 | else bad_thm "Free variables on right hand side of rewrite theorems" thm | |
| 132 | val _ = if null (subtract (op =) lhs_tvs rhs_tvs) | |
| 133 | then () | |
| 134 | else bad_thm "Free type variables on right hand side of rewrite theorems" thm | |
| 135 | in thm end; | |
| 136 | ||
| 137 | fun mk_rew thy thm = | |
| 138 | let | |
| 139 | val thms = (#mk o #mk_rews o snd o MetaSimplifier.rep_ss o Simplifier.simpset_of) thy thm; | |
| 140 | in | |
| 141 | map (check_rew thy) thms | |
| 142 | end; | |
| 143 | ||
| 144 | ||
| 145 | (* making function theorems *) | |
| 146 | ||
| 21119 | 147 | fun typ_func thy = snd o dest_Const o fst o strip_comb | 
| 148 | o fst o Logic.dest_equals o ObjectLogic.drop_judgment thy o Drule.plain_prop_of; | |
| 20600 | 149 | |
| 20844 | 150 | val strict_functyp = ref true; | 
| 151 | ||
| 21119 | 152 | fun dest_func thy = apfst dest_Const o strip_comb | 
| 153 | o fst o Logic.dest_equals o ObjectLogic.drop_judgment thy o Drule.plain_prop_of | |
| 154 | o Drule.fconv_rule Drule.beta_eta_conversion; | |
| 20844 | 155 | |
| 156 | fun mk_head thy thm = | |
| 157 | ((CodegenConsts.norm_of_typ thy o fst o dest_func thy) thm, thm); | |
| 20600 | 158 | |
| 21128 | 159 | fun check_func thy thm = case try (dest_func thy) thm | 
| 20844 | 160 | of SOME (c_ty as (c, ty), args) => | 
| 161 | let | |
| 162 | val _ = | |
| 163 | if has_duplicates (op =) | |
| 21158 | 164 | ((fold o fold_aterms) (fn Var (v, _) => cons v | 
| 165 | | Abs _ => bad_thm "Abstraction on left hand side of function equation" thm | |
| 166 | | _ => I | |
| 167 | ) args []) | |
| 20844 | 168 | then bad_thm "Repeated variables on left hand side of function equation" thm | 
| 169 | else () | |
| 170 | val is_classop = (is_some o AxClass.class_of_param thy) c; | |
| 171 | val const = CodegenConsts.norm_of_typ thy c_ty; | |
| 172 | val ty_decl = CodegenConsts.disc_typ_of_const thy | |
| 173 | (snd o CodegenConsts.typ_of_inst thy) const; | |
| 174 | val string_of_typ = setmp show_sorts true (Sign.string_of_typ thy); | |
| 175 | in if Sign.typ_equiv thy (ty_decl, ty) | |
| 21128 | 176 | then SOME (const, thm) | 
| 177 | else (if is_classop | |
| 178 | then error | |
| 179 | else if Sign.typ_equiv thy (Type.strip_sorts ty_decl, Type.strip_sorts ty) | |
| 180 | then warning #> (K o SOME) (const, thm) | |
| 181 | else if !strict_functyp | |
| 182 | then error | |
| 183 | else warning #> K NONE) | |
| 20844 | 184 |           ("Type\n" ^ string_of_typ ty
 | 
| 185 | ^ "\nof function theorem\n" | |
| 186 | ^ string_of_thm thm | |
| 187 | ^ "\nis strictly less general than declared function type\n" | |
| 21128 | 188 | ^ string_of_typ ty_decl) | 
| 20844 | 189 | end | 
| 190 | | NONE => bad_thm "Not a function equation" thm; | |
| 191 | ||
| 192 | fun check_typ_classop thy thm = | |
| 193 | let | |
| 194 | val (c_ty as (c, ty), _) = dest_func thy thm; | |
| 195 | in case AxClass.class_of_param thy c | |
| 196 | of SOME class => let | |
| 197 | val const = CodegenConsts.norm_of_typ thy c_ty; | |
| 198 | val ty_decl = CodegenConsts.disc_typ_of_const thy | |
| 199 | (snd o CodegenConsts.typ_of_inst thy) const; | |
| 200 | val string_of_typ = setmp show_sorts true (Sign.string_of_typ thy); | |
| 201 | in if Sign.typ_equiv thy (ty_decl, ty) | |
| 202 | then thm | |
| 203 | else error | |
| 204 |           ("Type\n" ^ string_of_typ ty
 | |
| 205 | ^ "\nof function theorem\n" | |
| 206 | ^ string_of_thm thm | |
| 207 | ^ "\nis strictly less general than declared function type\n" | |
| 208 | ^ string_of_typ ty_decl) | |
| 209 | end | |
| 210 | | NONE => thm | |
| 211 | end; | |
| 20600 | 212 | |
| 213 | fun mk_func thy raw_thm = | |
| 20844 | 214 | mk_rew thy raw_thm | 
| 21128 | 215 | |> map_filter (check_func thy); | 
| 20600 | 216 | |
| 217 | fun get_prim_def_funcs thy c = | |
| 218 | let | |
| 219 | fun constrain thm0 thm = case AxClass.class_of_param thy (fst c) | |
| 220 | of SOME _ => | |
| 221 | let | |
| 20704 | 222 | val ty_decl = CodegenConsts.disc_typ_of_classop thy c; | 
| 20600 | 223 | val max = maxidx_of_typ ty_decl + 1; | 
| 224 | val thm = Thm.incr_indexes max thm; | |
| 225 | val ty = typ_func thy thm; | |
| 226 | val (env, _) = Sign.typ_unify thy (ty_decl, ty) (Vartab.empty, max); | |
| 227 | val instT = Vartab.fold (fn (x_i, (sort, ty)) => | |
| 228 | cons (Thm.ctyp_of thy (TVar (x_i, sort)), Thm.ctyp_of thy ty)) env []; | |
| 229 | in Thm.instantiate (instT, []) thm end | |
| 230 | | NONE => thm | |
| 231 | in case CodegenConsts.find_def thy c | |
| 232 | of SOME ((_, thm), _) => | |
| 233 | thm | |
| 234 | |> Thm.transfer thy | |
| 235 | |> try (map snd o mk_func thy) | |
| 236 | |> these | |
| 237 | |> map (constrain thm) | |
| 238 | | NONE => [] | |
| 239 | end; | |
| 240 | ||
| 241 | ||
| 242 | (* pairs of (selected, deleted) function theorems *) | |
| 243 | ||
| 21338 | 244 | type sdthms = thm list Susp.T * thm list; | 
| 20600 | 245 | |
| 246 | fun add_drop_redundant thm thms = | |
| 247 | let | |
| 248 | val thy = Context.check_thy (Thm.theory_of_thm thm); | |
| 21284 | 249 | val args_of = snd o strip_comb o fst o Logic.dest_equals o Drule.plain_prop_of; | 
| 250 | val args = args_of thm; | |
| 251 | fun matches [] _ = true | |
| 252 | | matches (Var _ :: xs) [] = matches xs [] | |
| 253 | | matches (_ :: _) [] = false | |
| 254 | | matches (x :: xs) (y :: ys) = Pattern.matches thy (x, y) andalso matches xs ys; | |
| 255 | fun drop thm' = if matches args (args_of thm') | |
| 256 |       then (warning ("Dropping redundant function theorem\n" ^ string_of_thm thm'); false)
 | |
| 257 | else true | |
| 258 | in thm :: filter drop thms end; | |
| 20600 | 259 | |
| 260 | fun add_thm thm (sels, dels) = | |
| 261 | (Susp.value (add_drop_redundant thm (Susp.force sels)), remove eq_thm thm dels); | |
| 262 | ||
| 263 | fun add_lthms lthms (sels, []) = | |
| 264 | (lazy (fn () => fold add_drop_redundant | |
| 265 | (Susp.force lthms) (Susp.force sels)), []) | |
| 266 | | add_lthms lthms (sels, dels) = | |
| 267 | fold add_thm (Susp.force lthms) (sels, dels); | |
| 268 | ||
| 269 | fun del_thm thm (sels, dels) = | |
| 270 | (Susp.value (remove eq_thm thm (Susp.force sels)), thm :: dels); | |
| 271 | ||
| 272 | fun pretty_sdthms ctxt (sels, _) = pretty_lthms ctxt sels; | |
| 273 | ||
| 21119 | 274 | fun merge_sdthms ((sels1, dels1), (sels2, dels2)) = | 
| 20600 | 275 | let | 
| 276 | val (dels_t, dels) = merge_thms (dels1, dels2); | |
| 277 | in if dels_t | |
| 278 | then let | |
| 279 | val (_, sels) = merge_thms (Susp.force sels1, subtract eq_thm dels1 (Susp.force sels2)) | |
| 280 | val (_, dels) = merge_thms (dels1, subtract eq_thm (Susp.force sels1) dels2) | |
| 281 | in (true, ((lazy o K) sels, dels)) end | |
| 282 | else let | |
| 283 | val (sels_t, sels) = merge_lthms (sels1, sels2) | |
| 284 | in (sels_t, (sels, dels)) end | |
| 285 | end; | |
| 286 | ||
| 287 | ||
| 288 | (** data structures **) | |
| 289 | ||
| 290 | structure Consttab = CodegenConsts.Consttab; | |
| 291 | ||
| 292 | datatype preproc = Preproc of {
 | |
| 293 | inlines: thm list, | |
| 20844 | 294 | inline_procs: (serial * (theory -> cterm list -> thm list)) list, | 
| 20600 | 295 | preprocs: (serial * (theory -> thm list -> thm list)) list | 
| 296 | }; | |
| 297 | ||
| 21119 | 298 | fun mk_preproc ((inlines, inline_procs), preprocs) = | 
| 299 |   Preproc { inlines = inlines, inline_procs = inline_procs, preprocs = preprocs };
 | |
| 300 | fun map_preproc f (Preproc { inlines, inline_procs, preprocs }) =
 | |
| 301 | mk_preproc (f ((inlines, inline_procs), preprocs)); | |
| 302 | fun merge_preproc (Preproc { inlines = inlines1, inline_procs = inline_procs1, preprocs = preprocs1 },
 | |
| 303 |   Preproc { inlines = inlines2, inline_procs = inline_procs2, preprocs = preprocs2 }) =
 | |
| 20600 | 304 | let | 
| 305 | val (touched1, inlines) = merge_thms (inlines1, inlines2); | |
| 20844 | 306 | val (touched2, inline_procs) = merge_alist (op =) (K true) (inline_procs1, inline_procs2); | 
| 21119 | 307 | val (touched3, preprocs) = merge_alist (op =) (K true) (preprocs1, preprocs2); | 
| 308 | in (touched1 orelse touched2 orelse touched3, | |
| 309 | mk_preproc ((inlines, inline_procs), preprocs)) end; | |
| 20600 | 310 | |
| 311 | fun join_func_thms (tabs as (tab1, tab2)) = | |
| 312 | let | |
| 313 | val cs1 = Consttab.keys tab1; | |
| 314 | val cs2 = Consttab.keys tab2; | |
| 315 | val cs' = filter (member CodegenConsts.eq_const cs2) cs1; | |
| 316 | val cs'' = subtract (op =) cs' cs1 @ subtract (op =) cs' cs2; | |
| 317 | val cs''' = ref [] : CodegenConsts.const list ref; | |
| 21119 | 318 | fun merge c x = let val (touched, thms') = merge_sdthms x in | 
| 20600 | 319 | (if touched then cs''' := cons c (!cs''') else (); thms') end; | 
| 320 | in (cs'' @ !cs''', Consttab.join merge tabs) end; | |
| 321 | fun merge_funcs (thms1, thms2) = | |
| 322 | let | |
| 323 | val (consts, thms) = join_func_thms (thms1, thms2); | |
| 324 | in (SOME consts, thms) end; | |
| 325 | ||
| 326 | val eq_string = op = : string * string -> bool; | |
| 327 | fun eq_dtyp (((vs1, cs1), _), ((vs2, cs2), _)) = | |
| 328 | gen_eq_set (eq_pair eq_string (gen_eq_set eq_string)) (vs1, vs2) | |
| 329 | andalso gen_eq_set (eq_pair eq_string (eq_list (is_equal o Term.typ_ord))) (cs1, cs2); | |
| 330 | fun merge_dtyps (tabs as (tab1, tab2)) = | |
| 331 | let | |
| 332 | val tycos1 = Symtab.keys tab1; | |
| 333 | val tycos2 = Symtab.keys tab2; | |
| 334 | val tycos' = filter (member eq_string tycos2) tycos1; | |
| 20844 | 335 | val touched = not (gen_eq_set (op =) (tycos1, tycos2) andalso | 
| 336 | gen_eq_set (eq_pair (op =) (eq_dtyp)) | |
| 20600 | 337 | (AList.make (the o Symtab.lookup tab1) tycos', | 
| 20844 | 338 | AList.make (the o Symtab.lookup tab2) tycos')); | 
| 20600 | 339 | in (touched, Symtab.merge (K true) tabs) end; | 
| 340 | ||
| 341 | datatype spec = Spec of {
 | |
| 342 | funcs: sdthms Consttab.table, | |
| 343 | dconstrs: string Consttab.table, | |
| 21338 | 344 | dtyps: (((string * sort) list * (string * typ list) list) * thm list Susp.T) Symtab.table | 
| 20600 | 345 | }; | 
| 346 | ||
| 347 | fun mk_spec ((funcs, dconstrs), dtyps) = | |
| 348 |   Spec { funcs = funcs, dconstrs = dconstrs, dtyps = dtyps };
 | |
| 349 | fun map_spec f (Spec { funcs = funcs, dconstrs = dconstrs, dtyps = dtyps }) =
 | |
| 350 | mk_spec (f ((funcs, dconstrs), dtyps)); | |
| 351 | fun merge_spec (Spec { funcs = funcs1, dconstrs = dconstrs1, dtyps = dtyps1 },
 | |
| 352 |   Spec { funcs = funcs2, dconstrs = dconstrs2, dtyps = dtyps2 }) =
 | |
| 353 | let | |
| 354 | val (touched_cs, funcs) = merge_funcs (funcs1, funcs2); | |
| 355 | val dconstrs = Consttab.merge (K true) (dconstrs1, dconstrs2); | |
| 356 | val (touched', dtyps) = merge_dtyps (dtyps1, dtyps2); | |
| 357 | val touched = if touched' then NONE else touched_cs; | |
| 358 | in (touched, mk_spec ((funcs, dconstrs), dtyps)) end; | |
| 359 | ||
| 360 | datatype exec = Exec of {
 | |
| 361 | preproc: preproc, | |
| 362 | spec: spec | |
| 363 | }; | |
| 364 | ||
| 365 | fun mk_exec (preproc, spec) = | |
| 366 |   Exec { preproc = preproc, spec = spec };
 | |
| 367 | fun map_exec f (Exec { preproc = preproc, spec = spec }) =
 | |
| 368 | mk_exec (f (preproc, spec)); | |
| 369 | fun merge_exec (Exec { preproc = preproc1, spec = spec1 },
 | |
| 370 |   Exec { preproc = preproc2, spec = spec2 }) =
 | |
| 371 | let | |
| 372 | val (touched', preproc) = merge_preproc (preproc1, preproc2); | |
| 373 | val (touched_cs, spec) = merge_spec (spec1, spec2); | |
| 374 | val touched = if touched' then NONE else touched_cs; | |
| 375 | in (touched, mk_exec (preproc, spec)) end; | |
| 21119 | 376 | val empty_exec = mk_exec (mk_preproc (([], []), []), | 
| 20600 | 377 | mk_spec ((Consttab.empty, Consttab.empty), Symtab.empty)); | 
| 378 | ||
| 379 | fun the_preproc (Exec { preproc = Preproc x, ...}) = x;
 | |
| 380 | fun the_spec (Exec { spec = Spec x, ...}) = x;
 | |
| 381 | val the_funcs = #funcs o the_spec; | |
| 382 | val the_dcontrs = #dconstrs o the_spec; | |
| 383 | val the_dtyps = #dtyps o the_spec; | |
| 384 | val map_preproc = map_exec o apfst o map_preproc; | |
| 385 | val map_funcs = map_exec o apsnd o map_spec o apfst o apfst; | |
| 386 | val map_dconstrs = map_exec o apsnd o map_spec o apfst o apsnd; | |
| 387 | val map_dtyps = map_exec o apsnd o map_spec o apsnd; | |
| 388 | ||
| 389 | ||
| 390 | (** code data structures **) | |
| 391 | ||
| 392 | (*private copy avoids potential conflict of table exceptions*) | |
| 393 | structure Datatab = TableFun(type key = int val ord = int_ord); | |
| 394 | ||
| 395 | ||
| 396 | (* data slots *) | |
| 397 | ||
| 398 | local | |
| 399 | ||
| 400 | type kind = {
 | |
| 401 | name: string, | |
| 402 | empty: Object.T, | |
| 403 | merge: Pretty.pp -> Object.T * Object.T -> Object.T, | |
| 20937 | 404 | purge: theory option -> CodegenConsts.const list option -> Object.T -> Object.T | 
| 20600 | 405 | }; | 
| 406 | ||
| 407 | val kinds = ref (Datatab.empty: kind Datatab.table); | |
| 408 | ||
| 409 | fun invoke meth_name meth_fn k = | |
| 410 | (case Datatab.lookup (! kinds) k of | |
| 411 | SOME kind => meth_fn kind |> transform_failure (fn exn => | |
| 412 | EXCEPTION (exn, "Code data method " ^ #name kind ^ "." ^ meth_name ^ " failed")) | |
| 413 |   | NONE => sys_error ("Invalid code data identifier " ^ string_of_int k));
 | |
| 414 | ||
| 415 | ||
| 416 | in | |
| 417 | ||
| 418 | fun invoke_name k = invoke "name" (K o #name) k (); | |
| 419 | fun invoke_empty k = invoke "empty" (K o #empty) k (); | |
| 420 | fun invoke_merge pp = invoke "merge" (fn kind => #merge kind pp); | |
| 20937 | 421 | fun invoke_purge thy_opt cs = invoke "purge" (fn kind => #purge kind thy_opt cs); | 
| 20600 | 422 | |
| 423 | fun declare name empty merge purge = | |
| 424 | let | |
| 425 | val k = serial (); | |
| 426 |     val kind = {name = name, empty = empty, merge = merge, purge = purge};
 | |
| 427 | val _ = conditional (Datatab.exists (equal name o #name o #2) (! kinds)) (fn () => | |
| 428 |       warning ("Duplicate declaration of code data " ^ quote name));
 | |
| 429 | val _ = change kinds (Datatab.update (k, kind)); | |
| 430 | in k end; | |
| 431 | ||
| 432 | end; (*local*) | |
| 433 | ||
| 434 | ||
| 435 | (* theory store *) | |
| 436 | ||
| 437 | type data = Object.T Datatab.table; | |
| 438 | ||
| 439 | structure CodeData = TheoryDataFun | |
| 440 | (struct | |
| 441 | val name = "Pure/codegen_data"; | |
| 442 | type T = exec * data ref; | |
| 443 | val empty = (empty_exec, ref Datatab.empty : data ref); | |
| 444 | fun copy (exec, data) = (exec, ref (! data)); | |
| 445 | val extend = copy; | |
| 446 | fun merge pp ((exec1, data1), (exec2, data2)) = | |
| 447 | let | |
| 448 | val (touched, exec) = merge_exec (exec1, exec2); | |
| 20937 | 449 | val data1' = Datatab.map' (invoke_purge NONE touched) (! data1); | 
| 450 | val data2' = Datatab.map' (invoke_purge NONE touched) (! data2); | |
| 20600 | 451 | val data = Datatab.join (invoke_merge pp) (data1', data2'); | 
| 452 | in (exec, ref data) end; | |
| 453 | fun print thy (exec, _) = | |
| 454 | let | |
| 455 | val ctxt = ProofContext.init thy; | |
| 456 | fun pretty_func (s, lthms) = | |
| 457 | (Pretty.block o Pretty.fbreaks) ( | |
| 458 | Pretty.str s :: pretty_sdthms ctxt lthms | |
| 459 | ); | |
| 460 | fun pretty_dtyp (s, cos) = | |
| 461 | (Pretty.block o Pretty.breaks) ( | |
| 462 | Pretty.str s | |
| 463 | :: Pretty.str "=" | |
| 464 | :: Pretty.separate "|" (map (fn (c, []) => Pretty.str c | |
| 465 | | (c, tys) => | |
| 466 | Pretty.block | |
| 467 | (Pretty.str c :: Pretty.brk 1 :: Pretty.str "of" :: Pretty.brk 1 | |
| 468 | :: Pretty.breaks (map (Pretty.quote o Sign.pretty_typ thy) tys))) cos) | |
| 469 | ) | |
| 470 | val inlines = (#inlines o the_preproc) exec; | |
| 471 | val funs = the_funcs exec | |
| 472 | |> Consttab.dest | |
| 473 | |> (map o apfst) (CodegenConsts.string_of_const thy) | |
| 474 | |> sort (string_ord o pairself fst); | |
| 475 | val dtyps = the_dtyps exec | |
| 476 | |> Symtab.dest | |
| 477 | |> map (fn (dtco, ((vs, cos), _)) => (Sign.string_of_typ thy (Type (dtco, map TFree vs)), cos)) | |
| 478 | |> sort (string_ord o pairself fst) | |
| 479 | in | |
| 480 | (Pretty.writeln o Pretty.block o Pretty.fbreaks) ([ | |
| 481 | Pretty.str "code theorems:", | |
| 482 | Pretty.str "function theorems:" ] @ | |
| 483 | map pretty_func funs @ [ | |
| 484 | Pretty.block ( | |
| 485 | Pretty.str "inlined theorems:" | |
| 486 | :: Pretty.fbrk | |
| 487 | :: (Pretty.fbreaks o map (ProofContext.pretty_thm ctxt)) inlines | |
| 488 | ), | |
| 489 | Pretty.block ( | |
| 490 | Pretty.str "datatypes:" | |
| 491 | :: Pretty.fbrk | |
| 492 | :: (Pretty.fbreaks o map pretty_dtyp) dtyps | |
| 493 | )] | |
| 494 | ) | |
| 495 | end; | |
| 496 | end); | |
| 497 | ||
| 498 | fun print_thms thy = CodeData.print thy; | |
| 499 | ||
| 500 | fun init k = CodeData.map | |
| 501 | (fn (exec, data) => (exec, ref (Datatab.update (k, invoke_empty k) (! data)))); | |
| 502 | ||
| 503 | fun get k dest data = | |
| 504 | (case Datatab.lookup data k of | |
| 505 | SOME x => (dest x handle Match => | |
| 506 |       error ("Failed to access code data " ^ quote (invoke_name k)))
 | |
| 507 |   | NONE => error ("Uninitialized code data " ^ quote (invoke_name k)));
 | |
| 508 | ||
| 509 | fun put k mk x = Datatab.update (k, mk x); | |
| 510 | ||
| 20937 | 511 | fun map_exec_purge touched f thy = | 
| 20600 | 512 | CodeData.map (fn (exec, data) => | 
| 20937 | 513 | (f exec, ref (Datatab.map' (invoke_purge (SOME thy) touched) (! data)))) thy; | 
| 20600 | 514 | |
| 515 | val get_exec = fst o CodeData.get; | |
| 516 | ||
| 517 | val _ = Context.add_setup CodeData.init; | |
| 518 | ||
| 519 | ||
| 520 | ||
| 521 | (** theorem transformation and certification **) | |
| 522 | ||
| 523 | fun rewrite_func rewrites thm = | |
| 524 | let | |
| 20844 | 525 | val rewrite = Tactic.rewrite false rewrites; | 
| 526 | val (ct_eq, [ct_lhs, ct_rhs]) = (Drule.strip_comb o Thm.cprop_of) thm; | |
| 527 |     val Const ("==", _) = Thm.term_of ct_eq;
 | |
| 20600 | 528 | val (ct_f, ct_args) = Drule.strip_comb ct_lhs; | 
| 529 | val rhs' = rewrite ct_rhs; | |
| 530 | val args' = map rewrite ct_args; | |
| 531 | val lhs' = Thm.symmetric (fold (fn th1 => fn th2 => Thm.combination th2 th1) | |
| 532 | args' (Thm.reflexive ct_f)); | |
| 533 | in | |
| 534 | Thm.transitive (Thm.transitive lhs' thm) rhs' | |
| 535 | end handle Bind => raise ERROR "rewrite_func" | |
| 536 | ||
| 537 | fun common_typ_funcs thy [] = [] | |
| 538 | | common_typ_funcs thy [thm] = [thm] | |
| 539 | | common_typ_funcs thy thms = | |
| 540 | let | |
| 541 | fun incr_thm thm max = | |
| 542 | let | |
| 543 | val thm' = incr_indexes max thm; | |
| 21119 | 544 | val max' = Thm.maxidx_of thm' + 1; | 
| 20600 | 545 | in (thm', max') end; | 
| 546 | val (thms', maxidx) = fold_map incr_thm thms 0; | |
| 21119 | 547 | val (ty1::tys) = map (typ_func thy) thms'; | 
| 20600 | 548 | fun unify ty env = Sign.typ_unify thy (ty1, ty) env | 
| 549 | handle Type.TUNIFY => | |
| 550 |             error ("Type unificaton failed, while unifying function equations\n"
 | |
| 551 | ^ (cat_lines o map Display.string_of_thm) thms | |
| 552 | ^ "\nwith types\n" | |
| 553 | ^ (cat_lines o map (Sign.string_of_typ thy)) (ty1 :: tys)); | |
| 554 | val (env, _) = fold unify tys (Vartab.empty, maxidx) | |
| 555 | val instT = Vartab.fold (fn (x_i, (sort, ty)) => | |
| 556 | cons (Thm.ctyp_of thy (TVar (x_i, sort)), Thm.ctyp_of thy ty)) env []; | |
| 21119 | 557 | in map (Thm.instantiate (instT, [])) thms' end; | 
| 20600 | 558 | |
| 20844 | 559 | fun certify_const thy c c_thms = | 
| 20600 | 560 | let | 
| 561 | fun cert (c', thm) = if CodegenConsts.eq_const (c, c') | |
| 562 |       then thm else bad_thm ("Wrong head of function equation,\nexpected constant "
 | |
| 563 | ^ CodegenConsts.string_of_const thy c) thm | |
| 20844 | 564 | in map cert c_thms end; | 
| 20600 | 565 | |
| 566 | fun mk_cos tyco vs cos = | |
| 567 | let | |
| 568 | val dty = Type (tyco, map TFree vs); | |
| 569 | fun mk_co (co, tys) = (Const (co, (tys ---> dty)), map I tys); | |
| 570 | in map mk_co cos end; | |
| 571 | ||
| 572 | fun mk_co_args (co, tys) ctxt = | |
| 573 | let | |
| 574 | val names = Name.invents ctxt "a" (length tys); | |
| 575 | val ctxt' = fold Name.declare names ctxt; | |
| 576 | val vs = map2 (fn v => fn ty => Free (fst (v, 0), I ty)) names tys; | |
| 577 | in (vs, ctxt') end; | |
| 578 | ||
| 579 | fun check_freeness thy cos thms = | |
| 580 | let | |
| 581 | val props = AList.make Drule.plain_prop_of thms; | |
| 582 | fun sym_product [] = [] | |
| 583 | | sym_product (x::xs) = map (pair x) xs @ sym_product xs; | |
| 584 | val quodlibet = | |
| 585 | let | |
| 586 | val judg = ObjectLogic.fixed_judgment (the_context ()) "x"; | |
| 587 | val [free] = fold_aterms (fn v as Free _ => cons v | _ => I) judg []; | |
| 588 | val judg' = Term.subst_free [(free, Bound 0)] judg; | |
| 589 |         val prop = Type ("prop", []);
 | |
| 590 | val prop' = fastype_of judg'; | |
| 591 | in | |
| 592 |         Const ("all", (prop' --> prop) --> prop) $ Abs ("P", prop', judg')
 | |
| 593 | end; | |
| 594 | fun check_inj (co, []) = | |
| 595 | NONE | |
| 596 | | check_inj (co, tys) = | |
| 597 | let | |
| 598 | val ((xs, ys), _) = Name.context | |
| 599 | |> mk_co_args (co, tys) | |
| 600 | ||>> mk_co_args (co, tys); | |
| 601 | val prem = Logic.mk_equals | |
| 602 | (list_comb (co, xs), list_comb (co, ys)); | |
| 603 | val concl = Logic.mk_conjunction_list | |
| 604 | (map2 (curry Logic.mk_equals) xs ys); | |
| 605 | val t = Logic.mk_implies (prem, concl); | |
| 606 | in case find_first (curry Term.could_unify t o snd) props | |
| 607 | of SOME (thm, _) => SOME thm | |
| 608 |             | NONE => error ("Could not prove injectiveness statement\n"
 | |
| 609 | ^ Sign.string_of_term thy t | |
| 610 | ^ "\nfor constructor " | |
| 611 | ^ CodegenConsts.string_of_const_typ thy (dest_Const co) | |
| 612 | ^ "\nwith theorems\n" ^ cat_lines (map string_of_thm thms)) | |
| 613 | end; | |
| 614 | fun check_dist ((co1, tys1), (co2, tys2)) = | |
| 615 | let | |
| 616 | val ((xs1, xs2), _) = Name.context | |
| 617 | |> mk_co_args (co1, tys1) | |
| 618 | ||>> mk_co_args (co2, tys2); | |
| 619 | val prem = Logic.mk_equals | |
| 620 | (list_comb (co1, xs1), list_comb (co2, xs2)); | |
| 621 | val t = Logic.mk_implies (prem, quodlibet); | |
| 622 | in case find_first (curry Term.could_unify t o snd) props | |
| 623 | of SOME (thm, _) => thm | |
| 624 |             | NONE => error ("Could not prove distinctness statement\n"
 | |
| 625 | ^ Sign.string_of_term thy t | |
| 626 | ^ "\nfor constructors " | |
| 627 | ^ CodegenConsts.string_of_const_typ thy (dest_Const co1) | |
| 628 | ^ " and " | |
| 629 | ^ CodegenConsts.string_of_const_typ thy (dest_Const co2) | |
| 630 | ^ "\nwith theorems\n" ^ cat_lines (map string_of_thm thms)) | |
| 631 | end; | |
| 632 | in (map_filter check_inj cos, map check_dist (sym_product cos)) end; | |
| 633 | ||
| 634 | fun certify_datatype thy dtco cs thms = | |
| 635 | (op @) (check_freeness thy cs thms); | |
| 636 | ||
| 637 | ||
| 638 | ||
| 639 | (** interfaces **) | |
| 640 | ||
| 641 | fun add_func thm thy = | |
| 642 | let | |
| 643 | val thms = mk_func thy thm; | |
| 644 | val cs = map fst thms; | |
| 645 | in | |
| 646 | map_exec_purge (SOME cs) (map_funcs | |
| 647 | (fold (fn (c, thm) => Consttab.map_default | |
| 648 | (c, (Susp.value [], [])) (add_thm thm)) thms)) thy | |
| 649 | end; | |
| 650 | ||
| 21128 | 651 | fun add_func_legacy thm = setmp strict_functyp false (add_func thm); | 
| 652 | ||
| 20600 | 653 | fun del_func thm thy = | 
| 654 | let | |
| 21158 | 655 | val thms = mk_func thy thm; | 
| 20600 | 656 | val cs = map fst thms; | 
| 657 | in | |
| 658 | map_exec_purge (SOME cs) (map_funcs | |
| 659 | (fold (fn (c, thm) => Consttab.map_entry c | |
| 660 | (del_thm thm)) thms)) thy | |
| 661 | end; | |
| 662 | ||
| 663 | fun add_funcl (c, lthms) thy = | |
| 664 | let | |
| 665 | val c' = CodegenConsts.norm thy c; | |
| 20844 | 666 | val lthms' = certificate thy (fn thy => certify_const thy c' o maps (mk_func thy)) lthms; | 
| 20600 | 667 | in | 
| 668 | map_exec_purge (SOME [c]) (map_funcs (Consttab.map_default (c', (Susp.value [], [])) | |
| 669 | (add_lthms lthms'))) thy | |
| 670 | end; | |
| 671 | ||
| 672 | fun add_datatype (tyco, (vs_cos as (vs, cos), lthms)) thy = | |
| 673 | let | |
| 674 | val cs = mk_cos tyco vs cos; | |
| 675 | val consts = map (CodegenConsts.norm_of_typ thy o dest_Const o fst) cs; | |
| 676 | val add = | |
| 677 | map_dtyps (Symtab.update_new (tyco, | |
| 20844 | 678 | (vs_cos, certificate thy (fn thy => certify_datatype thy tyco cs) lthms))) | 
| 20600 | 679 | #> map_dconstrs (fold (fn c => Consttab.update (c, tyco)) consts) | 
| 680 | in map_exec_purge (SOME consts) add thy end; | |
| 681 | ||
| 682 | fun del_datatype tyco thy = | |
| 683 | let | |
| 20639 | 684 | val SOME ((vs, cos), _) = Symtab.lookup ((the_dtyps o get_exec) thy) tyco; | 
| 685 | val cs = mk_cos tyco vs cos; | |
| 686 | val consts = map (CodegenConsts.norm_of_typ thy o dest_Const o fst) cs; | |
| 20600 | 687 | val del = | 
| 688 | map_dtyps (Symtab.delete tyco) | |
| 20639 | 689 | #> map_dconstrs (fold Consttab.delete consts) | 
| 690 | in map_exec_purge (SOME consts) del thy end; | |
| 20600 | 691 | |
| 692 | fun add_inline thm thy = | |
| 20844 | 693 | (map_exec_purge NONE o map_preproc o apfst o apfst) (fold (insert eq_thm) (mk_rew thy thm)) thy; | 
| 20600 | 694 | |
| 695 | fun del_inline thm thy = | |
| 20844 | 696 | (map_exec_purge NONE o map_preproc o apfst o apfst) (fold (remove eq_thm) (mk_rew thy thm)) thy ; | 
| 697 | ||
| 698 | fun add_inline_proc f = | |
| 699 | (map_exec_purge NONE o map_preproc o apfst o apsnd) (cons (serial (), f)); | |
| 700 | ||
| 20600 | 701 | fun add_preproc f = | 
| 21119 | 702 | (map_exec_purge NONE o map_preproc o apsnd) (cons (serial (), f)); | 
| 20844 | 703 | |
| 704 | local | |
| 705 | ||
| 706 | fun gen_apply_inline_proc prep post thy f x = | |
| 707 | let | |
| 708 | val cts = prep x; | |
| 709 | val rews = map (check_rew thy) (f thy cts); | |
| 710 | in post rews x end; | |
| 711 | ||
| 712 | val apply_inline_proc = gen_apply_inline_proc (maps | |
| 713 | ((fn [args, rhs] => rhs :: (snd o Drule.strip_comb) args) o snd o Drule.strip_comb o Thm.cprop_of)) | |
| 714 | (fn rews => map (rewrite_func rews)); | |
| 715 | val apply_inline_proc_cterm = gen_apply_inline_proc single | |
| 716 | (Tactic.rewrite false); | |
| 20600 | 717 | |
| 20844 | 718 | fun apply_preproc thy f [] = [] | 
| 719 | | apply_preproc thy f (thms as (thm :: _)) = | |
| 720 | let | |
| 721 | val thms' = f thy thms; | |
| 722 | val c = (CodegenConsts.norm_of_typ thy o fst o dest_func thy) thm; | |
| 723 | in (certify_const thy c o map (mk_head thy)) thms' end; | |
| 724 | ||
| 725 | fun cmp_thms thy = | |
| 726 | make_ord (fn (thm1, thm2) => not (Sign.typ_instance thy (typ_func thy thm1, typ_func thy thm2))); | |
| 727 | ||
| 728 | fun rhs_conv conv thm = | |
| 729 | let | |
| 730 | val thm' = (conv o snd o Drule.dest_equals o Thm.cprop_of) thm; | |
| 731 | in Thm.transitive thm thm' end | |
| 732 | ||
| 733 | in | |
| 20600 | 734 | |
| 735 | fun preprocess thy thms = | |
| 20844 | 736 | thms | 
| 737 | |> fold (fn (_, f) => apply_preproc thy f) ((#preprocs o the_preproc o get_exec) thy) | |
| 738 | |> map (rewrite_func ((#inlines o the_preproc o get_exec) thy)) | |
| 739 | |> fold (fn (_, f) => apply_inline_proc thy f) ((#inline_procs o the_preproc o get_exec) thy) | |
| 21128 | 740 | (*FIXME - must check: rewrite rule, function equation, proper constant |> map (snd o check_func false thy) *) | 
| 20844 | 741 | |> sort (cmp_thms thy) | 
| 742 | |> common_typ_funcs thy; | |
| 20600 | 743 | |
| 21119 | 744 | fun preprocess_cterm thy ct = | 
| 20844 | 745 | ct | 
| 21119 | 746 | |> Thm.reflexive | 
| 747 | |> fold (rhs_conv o Tactic.rewrite false o single) ((#inlines o the_preproc o get_exec) thy) | |
| 748 | |> fold (fn (_, f) => rhs_conv (apply_inline_proc_cterm thy f)) ((#inline_procs o the_preproc o get_exec) thy) | |
| 20844 | 749 | |
| 750 | end; (*local*) | |
| 20600 | 751 | |
| 752 | fun these_funcs thy c = | |
| 753 | let | |
| 20844 | 754 | val funcs_1 = | 
| 20600 | 755 | Consttab.lookup ((the_funcs o get_exec) thy) c | 
| 756 | |> Option.map (Susp.force o fst) | |
| 757 | |> these | |
| 758 | |> map (Thm.transfer thy); | |
| 20844 | 759 | val funcs_2 = case funcs_1 | 
| 760 | of [] => get_prim_def_funcs thy c | |
| 761 | | xs => xs; | |
| 20600 | 762 | fun drop_refl thy = filter_out (is_equal o Term.fast_term_ord o Logic.dest_equals | 
| 763 | o ObjectLogic.drop_judgment thy o Drule.plain_prop_of); | |
| 764 | in | |
| 20844 | 765 | funcs_2 | 
| 20600 | 766 | |> preprocess thy | 
| 767 | |> drop_refl thy | |
| 768 | end; | |
| 769 | ||
| 770 | fun get_datatype thy tyco = | |
| 771 | Symtab.lookup ((the_dtyps o get_exec) thy) tyco | |
| 772 | |> Option.map (fn (spec, thms) => (Susp.force thms; spec)); | |
| 773 | ||
| 774 | fun get_datatype_of_constr thy c = | |
| 775 | Consttab.lookup ((the_dcontrs o get_exec) thy) c | |
| 776 | |> (Option.map o tap) (fn dtco => get_datatype thy dtco); | |
| 777 | ||
| 20937 | 778 | fun typ_funcs thy (c as (name, _), []) = (case AxClass.class_of_param thy name | 
| 779 | of SOME class => CodegenConsts.disc_typ_of_classop thy c | |
| 780 | | NONE => (case Option.map (Susp.force o fst) (Consttab.lookup ((the_funcs o get_exec) thy) c) | |
| 781 | of SOME [eq] => typ_func thy eq | |
| 782 | | _ => Sign.the_const_type thy name)) | |
| 783 | | typ_funcs thy (_, eq :: _) = typ_func thy eq; | |
| 20600 | 784 | |
| 785 | ||
| 786 | (** code attributes **) | |
| 787 | ||
| 788 | local | |
| 789 | fun add_simple_attribute (name, f) = | |
| 790 | (Codegen.add_attribute name o (Scan.succeed o Thm.declaration_attribute)) | |
| 20897 | 791 | (fn th => Context.mapping (f th) I); | 
| 20600 | 792 | in | 
| 793 | val _ = map (Context.add_setup o add_simple_attribute) [ | |
| 794 |     ("func", add_func),
 | |
| 795 |     ("nofunc", del_func),
 | |
| 796 |     ("unfold", (fn thm => Codegen.add_unfold thm #> add_inline thm)),
 | |
| 797 |     ("inline", add_inline),
 | |
| 798 |     ("noinline", del_inline)
 | |
| 799 | ] | |
| 800 | end; (*local*) | |
| 801 | ||
| 802 | end; (*struct*) | |
| 803 | ||
| 804 | ||
| 805 | ||
| 806 | (** type-safe interfaces for data depedent on executable content **) | |
| 807 | ||
| 808 | signature CODE_DATA_ARGS = | |
| 809 | sig | |
| 810 | val name: string | |
| 811 | type T | |
| 812 | val empty: T | |
| 813 | val merge: Pretty.pp -> T * T -> T | |
| 20937 | 814 | val purge: theory option -> CodegenConsts.const list option -> T -> T | 
| 20600 | 815 | end; | 
| 816 | ||
| 817 | signature CODE_DATA = | |
| 818 | sig | |
| 819 | type T | |
| 820 | val init: theory -> theory | |
| 821 | val get: theory -> T | |
| 822 | val change: theory -> (T -> T) -> T | |
| 823 | val change_yield: theory -> (T -> 'a * T) -> 'a * T | |
| 824 | end; | |
| 825 | ||
| 826 | functor CodeDataFun(Data: CODE_DATA_ARGS): CODE_DATA = | |
| 827 | struct | |
| 828 | ||
| 829 | type T = Data.T; | |
| 830 | exception Data of T; | |
| 831 | fun dest (Data x) = x | |
| 832 | ||
| 833 | val kind = CodegenData.declare Data.name (Data Data.empty) | |
| 834 | (fn pp => fn (Data x1, Data x2) => Data (Data.merge pp (x1, x2))) | |
| 20937 | 835 | (fn thy_opt => fn cs => fn Data x => Data (Data.purge thy_opt cs x)); | 
| 20600 | 836 | |
| 837 | val init = CodegenData.init kind; | |
| 838 | fun get thy = CodegenData.get kind dest ((! o snd o CodegenData.CodeData.get) thy); | |
| 839 | fun change thy f = | |
| 840 | let | |
| 841 | val data_ref = (snd o CodegenData.CodeData.get) thy; | |
| 842 | val x = (f o CodegenData.get kind dest o !) data_ref; | |
| 843 | val data = CodegenData.put kind Data x (! data_ref); | |
| 844 | in (data_ref := data; x) end; | |
| 845 | fun change_yield thy f = | |
| 846 | let | |
| 847 | val data_ref = (snd o CodegenData.CodeData.get) thy; | |
| 848 | val (y, x) = (f o CodegenData.get kind dest o !) data_ref; | |
| 849 | val data = CodegenData.put kind Data x (! data_ref); | |
| 850 | in (data_ref := data; (y, x)) end; | |
| 851 | ||
| 852 | end; | |
| 853 | ||
| 854 | structure CodegenData : CODEGEN_DATA = | |
| 855 | struct | |
| 856 | ||
| 857 | open CodegenData; | |
| 858 | ||
| 859 | end; |