src/Pure/Isar/code.ML
author haftmann
Tue Oct 07 16:07:59 2008 +0200 (2008-10-07)
changeset 28525 42297ae4df47
parent 28423 9fc3befd8191
child 28562 4e74209f113e
permissions -rw-r--r--
clarified preprocessor policies
haftmann@24219
     1
(*  Title:      Pure/Isar/code.ML
haftmann@24219
     2
    ID:         $Id$
haftmann@24219
     3
    Author:     Florian Haftmann, TU Muenchen
haftmann@24219
     4
haftmann@24219
     5
Abstract executable content of theory.  Management of data dependent on
haftmann@25485
     6
executable content.  Cache assumes non-concurrent processing of a single theory.
haftmann@24219
     7
*)
haftmann@24219
     8
haftmann@24219
     9
signature CODE =
haftmann@24219
    10
sig
haftmann@28368
    11
  val add_eqn: thm -> theory -> theory
haftmann@28368
    12
  val add_nonlinear_eqn: thm -> theory -> theory
haftmann@28368
    13
  val add_liberal_eqn: thm -> theory -> theory
haftmann@28368
    14
  val add_default_eqn: thm -> theory -> theory
haftmann@28368
    15
  val add_default_eqn_attr: Attrib.src
haftmann@28368
    16
  val del_eqn: thm -> theory -> theory
haftmann@28368
    17
  val del_eqns: string -> theory -> theory
haftmann@28368
    18
  val add_eqnl: string * (thm * bool) list Susp.T -> theory -> theory
haftmann@27557
    19
  val map_pre: (MetaSimplifier.simpset -> MetaSimplifier.simpset) -> theory -> theory
haftmann@27557
    20
  val map_post: (MetaSimplifier.simpset -> MetaSimplifier.simpset) -> theory -> theory
haftmann@24219
    21
  val add_inline: thm -> theory -> theory
haftmann@24219
    22
  val del_inline: thm -> theory -> theory
haftmann@24219
    23
  val add_post: thm -> theory -> theory
haftmann@24219
    24
  val del_post: thm -> theory -> theory
haftmann@28423
    25
  val add_functrans: string * (theory -> (thm * bool) list -> (thm * bool) list option) -> theory -> theory
haftmann@27557
    26
  val del_functrans: string -> theory -> theory
haftmann@24423
    27
  val add_datatype: (string * typ) list -> theory -> theory
haftmann@24423
    28
  val add_datatype_cmd: string list -> theory -> theory
haftmann@25485
    29
  val type_interpretation:
haftmann@25485
    30
    (string * ((string * sort) list * (string * typ list) list)
haftmann@25485
    31
      -> theory -> theory) -> theory -> theory
haftmann@24844
    32
  val add_case: thm -> theory -> theory
haftmann@24844
    33
  val add_undefined: string -> theory -> theory
haftmann@27675
    34
  val purge_data: theory -> theory
haftmann@24219
    35
haftmann@24219
    36
  val coregular_algebra: theory -> Sorts.algebra
haftmann@24219
    37
  val operational_algebra: theory -> (sort -> sort) * Sorts.algebra
haftmann@28368
    38
  val these_eqns: theory -> string -> (thm * bool) list
haftmann@28525
    39
  val these_raw_eqns: theory -> string -> (thm * bool) list
haftmann@24219
    40
  val get_datatype: theory -> string -> ((string * sort) list * (string * typ list) list)
haftmann@24423
    41
  val get_datatype_of_constr: theory -> string -> string option
haftmann@24844
    42
  val get_case_data: theory -> string -> (int * string list) option
haftmann@24844
    43
  val is_undefined: theory -> string -> bool
haftmann@28423
    44
  val default_typscheme: theory -> string -> (string * sort) list * typ
haftmann@24219
    45
haftmann@28423
    46
  val preprocess_conv: theory -> cterm -> thm
haftmann@24837
    47
  val preprocess_term: theory -> term -> term
haftmann@28423
    48
  val postprocess_conv: theory -> cterm -> thm
haftmann@24837
    49
  val postprocess_term: theory -> term -> term
haftmann@24219
    50
haftmann@24219
    51
  val add_attribute: string * (Args.T list -> attribute * Args.T list) -> theory -> theory
haftmann@24219
    52
haftmann@24219
    53
  val print_codesetup: theory -> unit
haftmann@24219
    54
end;
haftmann@24219
    55
haftmann@24219
    56
signature CODE_DATA_ARGS =
haftmann@24219
    57
sig
haftmann@24219
    58
  type T
haftmann@24219
    59
  val empty: T
haftmann@27609
    60
  val purge: theory -> string list -> T -> T
haftmann@24219
    61
end;
haftmann@24219
    62
haftmann@24219
    63
signature CODE_DATA =
haftmann@24219
    64
sig
haftmann@24219
    65
  type T
haftmann@24219
    66
  val get: theory -> T
haftmann@24219
    67
  val change: theory -> (T -> T) -> T
haftmann@24219
    68
  val change_yield: theory -> (T -> 'a * T) -> 'a * T
haftmann@24219
    69
end;
haftmann@24219
    70
haftmann@24219
    71
signature PRIVATE_CODE =
haftmann@24219
    72
sig
haftmann@24219
    73
  include CODE
haftmann@27609
    74
  val declare_data: Object.T -> (theory -> string list -> Object.T -> Object.T)
haftmann@27609
    75
    -> serial
haftmann@24219
    76
  val get_data: serial * ('a -> Object.T) * (Object.T -> 'a)
haftmann@24219
    77
    -> theory -> 'a
haftmann@24219
    78
  val change_data: serial * ('a -> Object.T) * (Object.T -> 'a)
haftmann@24219
    79
    -> theory -> ('a -> 'a) -> 'a
haftmann@24219
    80
  val change_yield_data: serial * ('a -> Object.T) * (Object.T -> 'a)
haftmann@24219
    81
    -> theory -> ('a -> 'b * 'a) -> 'b * 'a
haftmann@24219
    82
end;
haftmann@24219
    83
haftmann@24219
    84
structure Code : PRIVATE_CODE =
haftmann@24219
    85
struct
haftmann@24219
    86
haftmann@25312
    87
(** code attributes **)
haftmann@25312
    88
haftmann@25312
    89
structure CodeAttr = TheoryDataFun (
haftmann@25312
    90
  type T = (string * (Args.T list -> attribute * Args.T list)) list;
haftmann@25312
    91
  val empty = [];
haftmann@25312
    92
  val copy = I;
haftmann@25312
    93
  val extend = I;
haftmann@26970
    94
  fun merge _ = AList.merge (op = : string * string -> bool) (K true);
haftmann@25312
    95
);
haftmann@24219
    96
haftmann@25312
    97
fun add_attribute (attr as (name, _)) =
haftmann@25312
    98
  let
haftmann@25312
    99
    fun add_parser ("", parser) attrs = attrs @ [("", parser)]
haftmann@25312
   100
      | add_parser (name, parser) attrs = (name, Args.$$$ name |-- parser) :: attrs;
haftmann@25312
   101
    fun error "" = error ("Code attribute already declared")
haftmann@25312
   102
      | error name = error ("Code attribute " ^ name ^ " already declared")
haftmann@25312
   103
  in CodeAttr.map (fn attrs => if AList.defined (op =) attrs name
haftmann@25312
   104
    then error name else add_parser attr attrs)
haftmann@25312
   105
  end;
haftmann@25312
   106
haftmann@25312
   107
val _ =
haftmann@25312
   108
  let
haftmann@25312
   109
    val code_attr = Attrib.syntax (Scan.peek (fn context =>
haftmann@25312
   110
      List.foldr op || Scan.fail (map snd (CodeAttr.get (Context.theory_of context)))));
haftmann@25312
   111
  in
wenzelm@26463
   112
    Context.>> (Context.map_theory
wenzelm@26463
   113
      (Attrib.add_attributes
wenzelm@26463
   114
        [("code", code_attr, "declare theorems for code generation")]))
haftmann@25312
   115
  end;
haftmann@25312
   116
haftmann@25312
   117
haftmann@28143
   118
(** logical and syntactical specification of executable code **)
haftmann@24219
   119
haftmann@28350
   120
(* defining equations with linear flag, default flag and lazy theorems *)
haftmann@24219
   121
haftmann@24219
   122
fun pretty_lthms ctxt r = case Susp.peek r
haftmann@28350
   123
 of SOME thms => map (ProofContext.pretty_thm ctxt o fst) thms
haftmann@24219
   124
  | NONE => [Pretty.str "[...]"];
haftmann@24219
   125
haftmann@24219
   126
fun certificate thy f r =
haftmann@24219
   127
  case Susp.peek r
haftmann@28423
   128
   of SOME thms => (Susp.value o f thy) thms
haftmann@24844
   129
    | NONE => let
haftmann@24844
   130
        val thy_ref = Theory.check_thy thy;
haftmann@28423
   131
      in Susp.delay (fn () => (f (Theory.deref thy_ref) o Susp.force) r) end;
haftmann@24219
   132
haftmann@28423
   133
fun add_drop_redundant thy (thm, linear) thms =
haftmann@24219
   134
  let
haftmann@24219
   135
    val args_of = snd o strip_comb o fst o Logic.dest_equals o Thm.plain_prop_of;
haftmann@24219
   136
    val args = args_of thm;
haftmann@28403
   137
    val incr_idx = Logic.incr_indexes ([], Thm.maxidx_of thm + 1);
haftmann@28350
   138
    fun matches_args args' = length args <= length args' andalso
haftmann@28403
   139
      Pattern.matchess thy (args, (map incr_idx o curry Library.take (length args)) args');
haftmann@28359
   140
    fun drop (thm', linear') = if (linear orelse not linear')
haftmann@28359
   141
      andalso matches_args (args_of thm') then 
haftmann@28359
   142
        (warning ("Code generator: dropping redundant defining equation\n" ^ Display.string_of_thm thm'); true)
haftmann@28350
   143
      else false;
haftmann@28350
   144
  in (thm, linear) :: filter_out drop thms end;
haftmann@24219
   145
haftmann@28423
   146
fun add_thm thy _ thm (false, thms) = (false, Susp.map_force (add_drop_redundant thy thm) thms)
haftmann@28423
   147
  | add_thm thy true thm (true, thms) = (true, Susp.map_force (fn thms => thms @ [thm]) thms)
haftmann@28423
   148
  | add_thm thy false thm (true, thms) = (false, Susp.value [thm]);
haftmann@24219
   149
haftmann@28143
   150
fun add_lthms lthms _ = (false, lthms);
haftmann@25312
   151
haftmann@28350
   152
fun del_thm thm = (apsnd o Susp.map_force) (remove (eq_fst Thm.eq_thm_prop) (thm, true));
haftmann@25312
   153
haftmann@28143
   154
fun merge_defthms ((true, _), defthms2) = defthms2
haftmann@28143
   155
  | merge_defthms (defthms1 as (false, _), (true, _)) = defthms1
haftmann@28143
   156
  | merge_defthms ((false, _), defthms2 as (false, _)) = defthms2;
haftmann@24219
   157
haftmann@24219
   158
haftmann@28143
   159
(* syntactic datatypes *)
haftmann@24219
   160
haftmann@24219
   161
val eq_string = op = : string * string -> bool;
haftmann@28143
   162
haftmann@24219
   163
fun eq_dtyp ((vs1, cs1), (vs2, cs2)) = 
haftmann@24219
   164
  gen_eq_set (eq_pair eq_string (gen_eq_set eq_string)) (vs1, vs2)
haftmann@24423
   165
    andalso gen_eq_set (eq_fst eq_string) (cs1, cs2);
haftmann@28143
   166
haftmann@27582
   167
fun merge_dtyps (tabs as (tab1, tab2)) =
haftmann@24219
   168
  let
haftmann@27582
   169
    fun join _ (cos as (_, cos2)) = if eq_dtyp cos then raise Symtab.SAME else cos2;
haftmann@27582
   170
  in Symtab.join join tabs end;
haftmann@24219
   171
haftmann@28143
   172
haftmann@28143
   173
(* specification data *)
haftmann@28143
   174
haftmann@24219
   175
datatype spec = Spec of {
haftmann@28368
   176
  eqns: (bool * (thm * bool) list Susp.T) Symtab.table,
haftmann@24844
   177
  dtyps: ((string * sort) list * (string * typ list) list) Symtab.table,
haftmann@24844
   178
  cases: (int * string list) Symtab.table * unit Symtab.table
haftmann@24219
   179
};
haftmann@24219
   180
haftmann@28368
   181
fun mk_spec (eqns, (dtyps, cases)) =
haftmann@28368
   182
  Spec { eqns = eqns, dtyps = dtyps, cases = cases };
haftmann@28368
   183
fun map_spec f (Spec { eqns = eqns, dtyps = dtyps, cases = cases }) =
haftmann@28368
   184
  mk_spec (f (eqns, (dtyps, cases)));
haftmann@28368
   185
fun merge_spec (Spec { eqns = eqns1, dtyps = dtyps1, cases = (cases1, undefs1) },
haftmann@28368
   186
  Spec { eqns = eqns2, dtyps = dtyps2, cases = (cases2, undefs2) }) =
haftmann@24219
   187
  let
haftmann@28368
   188
    val eqns = Symtab.join (K merge_defthms) (eqns1, eqns2);
haftmann@27582
   189
    val dtyps = merge_dtyps (dtyps1, dtyps2);
haftmann@27609
   190
    val cases = (Symtab.merge (K true) (cases1, cases2),
haftmann@27609
   191
      Symtab.merge (K true) (undefs1, undefs2));
haftmann@28368
   192
  in mk_spec (eqns, (dtyps, cases)) end;
haftmann@24219
   193
haftmann@25312
   194
haftmann@25312
   195
(* pre- and postprocessor *)
haftmann@25312
   196
haftmann@25312
   197
datatype thmproc = Thmproc of {
haftmann@27557
   198
  pre: MetaSimplifier.simpset,
haftmann@27557
   199
  post: MetaSimplifier.simpset,
haftmann@28423
   200
  functrans: (string * (serial * (theory -> (thm * bool) list -> (thm * bool) list option))) list
haftmann@25312
   201
};
haftmann@25312
   202
haftmann@27557
   203
fun mk_thmproc ((pre, post), functrans) =
haftmann@27557
   204
  Thmproc { pre = pre, post = post, functrans = functrans };
haftmann@27557
   205
fun map_thmproc f (Thmproc { pre, post, functrans }) =
haftmann@27557
   206
  mk_thmproc (f ((pre, post), functrans));
haftmann@27557
   207
fun merge_thmproc (Thmproc { pre = pre1, post = post1, functrans = functrans1 },
haftmann@27557
   208
  Thmproc { pre = pre2, post = post2, functrans = functrans2 }) =
haftmann@25312
   209
    let
haftmann@27557
   210
      val pre = MetaSimplifier.merge_ss (pre1, pre2);
haftmann@27557
   211
      val post = MetaSimplifier.merge_ss (post1, post2);
haftmann@27557
   212
      val functrans = AList.merge (op =) (eq_fst (op =)) (functrans1, functrans2);
haftmann@27557
   213
    in mk_thmproc ((pre, post), functrans) end;
haftmann@25312
   214
haftmann@24219
   215
datatype exec = Exec of {
haftmann@24219
   216
  thmproc: thmproc,
haftmann@24219
   217
  spec: spec
haftmann@24219
   218
};
haftmann@24219
   219
haftmann@28143
   220
haftmann@28143
   221
(* code setup data *)
haftmann@28143
   222
haftmann@24219
   223
fun mk_exec (thmproc, spec) =
haftmann@24219
   224
  Exec { thmproc = thmproc, spec = spec };
haftmann@24219
   225
fun map_exec f (Exec { thmproc = thmproc, spec = spec }) =
haftmann@24219
   226
  mk_exec (f (thmproc, spec));
haftmann@27557
   227
fun merge_exec (Exec { thmproc = thmproc1, spec = spec1 },
haftmann@24219
   228
  Exec { thmproc = thmproc2, spec = spec2 }) =
haftmann@24219
   229
  let
haftmann@27557
   230
    val thmproc = merge_thmproc (thmproc1, thmproc2);
haftmann@27557
   231
    val spec = merge_spec (spec1, spec2);
haftmann@27557
   232
  in mk_exec (thmproc, spec) end;
haftmann@27557
   233
val empty_exec = mk_exec (mk_thmproc ((MetaSimplifier.empty_ss, MetaSimplifier.empty_ss), []),
haftmann@24844
   234
  mk_spec (Symtab.empty, (Symtab.empty, (Symtab.empty, Symtab.empty))));
haftmann@24219
   235
haftmann@25312
   236
fun the_thmproc (Exec { thmproc = Thmproc x, ...}) = x;
haftmann@24219
   237
fun the_spec (Exec { spec = Spec x, ...}) = x;
haftmann@28368
   238
val the_eqns = #eqns o the_spec;
haftmann@24219
   239
val the_dtyps = #dtyps o the_spec;
haftmann@24844
   240
val the_cases = #cases o the_spec;
haftmann@24219
   241
val map_thmproc = map_exec o apfst o map_thmproc;
haftmann@28368
   242
val map_eqns = map_exec o apsnd o map_spec o apfst;
haftmann@24844
   243
val map_dtyps = map_exec o apsnd o map_spec o apsnd o apfst;
haftmann@24844
   244
val map_cases = map_exec o apsnd o map_spec o apsnd o apsnd;
haftmann@24219
   245
haftmann@24219
   246
haftmann@24219
   247
(* data slots dependent on executable content *)
haftmann@24219
   248
haftmann@24219
   249
(*private copy avoids potential conflict of table exceptions*)
haftmann@24219
   250
structure Datatab = TableFun(type key = int val ord = int_ord);
haftmann@24219
   251
haftmann@24219
   252
local
haftmann@24219
   253
haftmann@24219
   254
type kind = {
haftmann@24219
   255
  empty: Object.T,
haftmann@27609
   256
  purge: theory -> string list -> Object.T -> Object.T
haftmann@24219
   257
};
haftmann@24219
   258
haftmann@24219
   259
val kinds = ref (Datatab.empty: kind Datatab.table);
haftmann@24219
   260
val kind_keys = ref ([]: serial list);
haftmann@24219
   261
haftmann@24219
   262
fun invoke f k = case Datatab.lookup (! kinds) k
haftmann@24219
   263
 of SOME kind => f kind
haftmann@24219
   264
  | NONE => sys_error "Invalid code data identifier";
haftmann@24219
   265
haftmann@24219
   266
in
haftmann@24219
   267
haftmann@27609
   268
fun declare_data empty purge =
haftmann@24219
   269
  let
haftmann@24219
   270
    val k = serial ();
haftmann@27609
   271
    val kind = {empty = empty, purge = purge};
haftmann@24219
   272
    val _ = change kinds (Datatab.update (k, kind));
haftmann@24219
   273
    val _ = change kind_keys (cons k);
haftmann@24219
   274
  in k end;
haftmann@24219
   275
haftmann@27609
   276
fun invoke_init k = invoke (fn kind => #empty kind) k;
haftmann@24219
   277
haftmann@27609
   278
fun invoke_purge_all thy cs =
haftmann@24219
   279
  fold (fn k => Datatab.map_entry k
haftmann@27609
   280
    (invoke (fn kind => #purge kind thy cs) k)) (! kind_keys);
haftmann@24219
   281
haftmann@24219
   282
end; (*local*)
haftmann@24219
   283
haftmann@24219
   284
haftmann@25312
   285
(** theory store **)
haftmann@24219
   286
haftmann@24219
   287
local
haftmann@24219
   288
haftmann@24219
   289
type data = Object.T Datatab.table;
haftmann@27609
   290
val empty_data = Datatab.empty : data;
haftmann@24219
   291
haftmann@24219
   292
structure CodeData = TheoryDataFun
haftmann@24219
   293
(
haftmann@24219
   294
  type T = exec * data ref;
haftmann@27609
   295
  val empty = (empty_exec, ref empty_data);
haftmann@24219
   296
  fun copy (exec, data) = (exec, ref (! data));
haftmann@24219
   297
  val extend = copy;
haftmann@24219
   298
  fun merge pp ((exec1, data1), (exec2, data2)) =
haftmann@27609
   299
    (merge_exec (exec1, exec2), ref empty_data);
haftmann@24219
   300
);
haftmann@24219
   301
wenzelm@26463
   302
val _ = Context.>> (Context.map_theory CodeData.init);
haftmann@24219
   303
haftmann@24219
   304
fun thy_data f thy = f ((snd o CodeData.get) thy);
haftmann@24219
   305
haftmann@24219
   306
fun get_ensure_init kind data_ref =
haftmann@24219
   307
  case Datatab.lookup (! data_ref) kind
haftmann@24219
   308
   of SOME x => x
haftmann@27609
   309
    | NONE => let val y = invoke_init kind
haftmann@24219
   310
        in (change data_ref (Datatab.update (kind, y)); y) end;
haftmann@24219
   311
haftmann@24219
   312
in
haftmann@24219
   313
haftmann@24219
   314
(* access to executable content *)
haftmann@24219
   315
haftmann@24844
   316
val the_exec = fst o CodeData.get;
haftmann@24219
   317
haftmann@27983
   318
fun complete_class_params thy cs =
haftmann@27983
   319
  fold (fn c => case AxClass.inst_of_param thy c
haftmann@27983
   320
   of NONE => insert (op =) c
haftmann@27983
   321
    | SOME (c', _) => insert (op =) c' #> insert (op =) c) cs [];
haftmann@27983
   322
haftmann@24219
   323
fun map_exec_purge touched f thy =
haftmann@27609
   324
  CodeData.map (fn (exec, data) => (f exec, ref (case touched
haftmann@27983
   325
   of SOME cs => invoke_purge_all thy (complete_class_params thy cs) (! data)
haftmann@27609
   326
    | NONE => empty_data))) thy;
haftmann@24219
   327
haftmann@27675
   328
val purge_data = (CodeData.map o apsnd) (K (ref empty_data));
haftmann@27675
   329
haftmann@24219
   330
haftmann@24219
   331
(* access to data dependent on abstract executable content *)
haftmann@24219
   332
haftmann@24219
   333
fun get_data (kind, _, dest) = thy_data (get_ensure_init kind #> dest);
haftmann@24219
   334
haftmann@24219
   335
fun change_data (kind, mk, dest) =
haftmann@24219
   336
  let
haftmann@24219
   337
    fun chnge data_ref f =
haftmann@24219
   338
      let
haftmann@24219
   339
        val data = get_ensure_init kind data_ref;
haftmann@24219
   340
        val data' = f (dest data);
haftmann@24219
   341
      in (change data_ref (Datatab.update (kind, mk data')); data') end;
haftmann@24219
   342
  in thy_data chnge end;
haftmann@24219
   343
haftmann@24219
   344
fun change_yield_data (kind, mk, dest) =
haftmann@24219
   345
  let
haftmann@24219
   346
    fun chnge data_ref f =
haftmann@24219
   347
      let
haftmann@24219
   348
        val data = get_ensure_init kind data_ref;
haftmann@24219
   349
        val (x, data') = f (dest data);
haftmann@24219
   350
      in (x, (change data_ref (Datatab.update (kind, mk data')); data')) end;
haftmann@24219
   351
  in thy_data chnge end;
haftmann@24219
   352
haftmann@24219
   353
end; (*local*)
haftmann@24219
   354
haftmann@24219
   355
haftmann@24219
   356
(* print executable content *)
haftmann@24219
   357
haftmann@24219
   358
fun print_codesetup thy =
haftmann@24219
   359
  let
haftmann@24219
   360
    val ctxt = ProofContext.init thy;
haftmann@24844
   361
    val exec = the_exec thy;
haftmann@28368
   362
    fun pretty_eqn (s, (_, lthms)) =
haftmann@24219
   363
      (Pretty.block o Pretty.fbreaks) (
haftmann@28143
   364
        Pretty.str s :: pretty_lthms ctxt lthms
haftmann@24219
   365
      );
haftmann@24219
   366
    fun pretty_dtyp (s, []) =
haftmann@24219
   367
          Pretty.str s
haftmann@24219
   368
      | pretty_dtyp (s, cos) =
haftmann@24219
   369
          (Pretty.block o Pretty.breaks) (
haftmann@24219
   370
            Pretty.str s
haftmann@24219
   371
            :: Pretty.str "="
haftmann@28403
   372
            :: separate (Pretty.str "|") (map (fn (c, []) => Pretty.str (Code_Unit.string_of_const thy c)
haftmann@24219
   373
                 | (c, tys) =>
haftmann@24219
   374
                     (Pretty.block o Pretty.breaks)
haftmann@28054
   375
                        (Pretty.str (Code_Unit.string_of_const thy c)
wenzelm@26947
   376
                          :: Pretty.str "of"
wenzelm@26947
   377
                          :: map (Pretty.quote o Syntax.pretty_typ_global thy) tys)) cos)
haftmann@24219
   378
          );
haftmann@27557
   379
    val pre = (#pre o the_thmproc) exec;
haftmann@27557
   380
    val post = (#post o the_thmproc) exec;
haftmann@27557
   381
    val functrans = (map fst o #functrans o the_thmproc) exec;
haftmann@28368
   382
    val eqns = the_eqns exec
haftmann@24423
   383
      |> Symtab.dest
haftmann@28054
   384
      |> (map o apfst) (Code_Unit.string_of_const thy)
haftmann@24219
   385
      |> sort (string_ord o pairself fst);
haftmann@24219
   386
    val dtyps = the_dtyps exec
haftmann@24219
   387
      |> Symtab.dest
wenzelm@26947
   388
      |> map (fn (dtco, (vs, cos)) =>
wenzelm@26947
   389
          (Syntax.string_of_typ_global thy (Type (dtco, map TFree vs)), cos))
haftmann@24219
   390
      |> sort (string_ord o pairself fst)
haftmann@24219
   391
  in
haftmann@24219
   392
    (Pretty.writeln o Pretty.chunks) [
haftmann@24219
   393
      Pretty.block (
haftmann@24219
   394
        Pretty.str "defining equations:"
haftmann@24219
   395
        :: Pretty.fbrk
haftmann@28368
   396
        :: (Pretty.fbreaks o map pretty_eqn) eqns
haftmann@24219
   397
      ),
haftmann@27557
   398
      Pretty.block [
haftmann@27557
   399
        Pretty.str "preprocessing simpset:",
haftmann@27557
   400
        Pretty.fbrk,
haftmann@27557
   401
        MetaSimplifier.pretty_ss pre
haftmann@27557
   402
      ],
haftmann@27557
   403
      Pretty.block [
haftmann@27557
   404
        Pretty.str "postprocessing simpset:",
haftmann@27557
   405
        Pretty.fbrk,
haftmann@27557
   406
        MetaSimplifier.pretty_ss post
haftmann@27557
   407
      ],
haftmann@24219
   408
      Pretty.block (
haftmann@27609
   409
        Pretty.str "function transformers:"
haftmann@24219
   410
        :: Pretty.fbrk
haftmann@27557
   411
        :: (Pretty.fbreaks o map Pretty.str) functrans
haftmann@25968
   412
      ),
haftmann@25968
   413
      Pretty.block (
haftmann@24219
   414
        Pretty.str "datatypes:"
haftmann@24219
   415
        :: Pretty.fbrk
haftmann@24219
   416
        :: (Pretty.fbreaks o map pretty_dtyp) dtyps
haftmann@24219
   417
      )
haftmann@24219
   418
    ]
haftmann@24219
   419
  end;
haftmann@24219
   420
haftmann@24219
   421
haftmann@24219
   422
(** theorem transformation and certification **)
haftmann@24219
   423
haftmann@28423
   424
fun common_typ_eqns thy [] = []
haftmann@28423
   425
  | common_typ_eqns thy [thm] = [thm]
haftmann@28423
   426
  | common_typ_eqns thy (thms as thm :: _) = (*FIXME is too general*)
haftmann@24219
   427
      let
haftmann@24219
   428
        fun incr_thm thm max =
haftmann@24219
   429
          let
haftmann@24219
   430
            val thm' = incr_indexes max thm;
haftmann@24219
   431
            val max' = Thm.maxidx_of thm' + 1;
haftmann@24219
   432
          in (thm', max') end;
haftmann@24219
   433
        val (thms', maxidx) = fold_map incr_thm thms 0;
haftmann@28423
   434
        val ty1 :: tys = map (snd o Code_Unit.const_typ_eqn) thms';
haftmann@24219
   435
        fun unify ty env = Sign.typ_unify thy (ty1, ty) env
haftmann@24219
   436
          handle Type.TUNIFY =>
haftmann@24219
   437
            error ("Type unificaton failed, while unifying defining equations\n"
haftmann@24219
   438
            ^ (cat_lines o map Display.string_of_thm) thms
haftmann@24219
   439
            ^ "\nwith types\n"
haftmann@28054
   440
            ^ (cat_lines o map (Code_Unit.string_of_typ thy)) (ty1 :: tys));
haftmann@24219
   441
        val (env, _) = fold unify tys (Vartab.empty, maxidx)
haftmann@24219
   442
        val instT = Vartab.fold (fn (x_i, (sort, ty)) =>
haftmann@24219
   443
          cons (Thm.ctyp_of thy (TVar (x_i, sort)), Thm.ctyp_of thy ty)) env [];
haftmann@24219
   444
      in map (Thm.instantiate (instT, [])) thms' end;
haftmann@24219
   445
haftmann@28423
   446
fun certify_const thy c eqns =
haftmann@24219
   447
  let
haftmann@28423
   448
    fun cert (eqn as (thm, _)) = if c = Code_Unit.const_eqn thm
haftmann@28423
   449
      then eqn else error ("Wrong head of defining equation,\nexpected constant "
haftmann@28423
   450
        ^ Code_Unit.string_of_const thy c ^ "\n" ^ Display.string_of_thm thm)
haftmann@28423
   451
  in map cert eqns end;
haftmann@24219
   452
haftmann@28423
   453
fun check_linear (eqn as (thm, linear)) =
haftmann@28423
   454
  if linear then eqn else Code_Unit.bad_thm
haftmann@28423
   455
    ("Duplicate variables on left hand side of defining equation:\n"
haftmann@28423
   456
      ^ Display.string_of_thm thm);
haftmann@28423
   457
haftmann@28423
   458
fun mk_eqn thy linear =
haftmann@28423
   459
  Code_Unit.error_thm ((if linear then check_linear else I) o Code_Unit.mk_eqn thy);
haftmann@28423
   460
fun mk_liberal_eqn thy = Code_Unit.warning_thm (check_linear o Code_Unit.mk_eqn thy);
haftmann@28423
   461
fun mk_syntactic_eqn thy = Code_Unit.warning_thm (Code_Unit.mk_eqn thy);
haftmann@28423
   462
fun mk_default_eqn thy = Code_Unit.try_thm (check_linear o Code_Unit.mk_eqn thy);
haftmann@24219
   463
haftmann@24219
   464
haftmann@24219
   465
(** operational sort algebra and class discipline **)
haftmann@24219
   466
haftmann@24219
   467
local
haftmann@24219
   468
haftmann@28423
   469
fun arity_constraints thy algebra (class, tyco) =
haftmann@24219
   470
  let
haftmann@28423
   471
    val base_constraints = Sorts.mg_domain algebra tyco [class];
haftmann@28423
   472
    val classparam_constraints = Sorts.complete_sort algebra [class]
haftmann@28423
   473
      |> maps (map fst o these o try (#params o AxClass.get_info thy))
haftmann@25597
   474
      |> map_filter (fn c => try (AxClass.param_of_inst thy) (c, tyco))
haftmann@28368
   475
      |> map (Symtab.lookup ((the_eqns o the_exec) thy))
haftmann@28350
   476
      |> (map o Option.map) (map fst o Susp.force o snd)
haftmann@24219
   477
      |> maps these
haftmann@28423
   478
      |> map (map (snd o dest_TVar) o Sign.const_typargs thy o Code_Unit.const_typ_eqn);
haftmann@28423
   479
    val inter_sorts = map2 (curry (Sorts.inter_sort algebra));
haftmann@28423
   480
  in fold inter_sorts classparam_constraints base_constraints end;
haftmann@24219
   481
haftmann@24219
   482
fun retrieve_algebra thy operational =
wenzelm@26947
   483
  Sorts.subalgebra (Syntax.pp_global thy) operational
haftmann@28423
   484
    (arity_constraints thy (Sign.classes_of thy))
haftmann@24219
   485
    (Sign.classes_of thy);
haftmann@24219
   486
haftmann@24219
   487
in
haftmann@24219
   488
haftmann@24219
   489
fun coregular_algebra thy = retrieve_algebra thy (K true) |> snd;
haftmann@24219
   490
fun operational_algebra thy =
haftmann@24219
   491
  let
haftmann@24219
   492
    fun add_iff_operational class =
wenzelm@24928
   493
      can (AxClass.get_info thy) class ? cons class;
haftmann@24219
   494
    val operational_classes = fold add_iff_operational (Sign.all_classes thy) []
haftmann@24219
   495
  in retrieve_algebra thy (member (op =) operational_classes) end;
haftmann@24219
   496
haftmann@28143
   497
end; (*local*)
haftmann@24219
   498
haftmann@24219
   499
haftmann@24219
   500
(** interfaces and attributes **)
haftmann@24219
   501
haftmann@24624
   502
fun delete_force msg key xs =
haftmann@24624
   503
  if AList.defined (op =) xs key then AList.delete (op =) key xs
haftmann@24624
   504
  else error ("No such " ^ msg ^ ": " ^ quote key);
haftmann@24624
   505
haftmann@24423
   506
fun get_datatype thy tyco =
haftmann@24844
   507
  case Symtab.lookup ((the_dtyps o the_exec) thy) tyco
haftmann@24423
   508
   of SOME spec => spec
haftmann@24423
   509
    | NONE => Sign.arity_number thy tyco
wenzelm@24848
   510
        |> Name.invents Name.context Name.aT
haftmann@24423
   511
        |> map (rpair [])
haftmann@24423
   512
        |> rpair [];
haftmann@24423
   513
haftmann@24423
   514
fun get_datatype_of_constr thy c =
haftmann@24423
   515
  case (snd o strip_type o Sign.the_const_type thy) c
haftmann@24423
   516
   of Type (tyco, _) => if member (op =)
haftmann@24844
   517
       ((the_default [] o Option.map (map fst o snd) o Symtab.lookup ((the_dtyps o the_exec) thy)) tyco) c
haftmann@24423
   518
       then SOME tyco else NONE
haftmann@24423
   519
    | _ => NONE;
haftmann@24423
   520
haftmann@24423
   521
fun get_constr_typ thy c =
haftmann@24423
   522
  case get_datatype_of_constr thy c
haftmann@24423
   523
   of SOME tyco => let
haftmann@24423
   524
          val (vs, cos) = get_datatype thy tyco;
haftmann@24423
   525
          val SOME tys = AList.lookup (op =) cos c;
haftmann@24423
   526
          val ty = tys ---> Type (tyco, map TFree vs);
haftmann@24423
   527
        in SOME (Logic.varifyT ty) end
haftmann@24423
   528
    | NONE => NONE;
haftmann@24423
   529
haftmann@24844
   530
val get_case_data = Symtab.lookup o fst o the_cases o the_exec;
haftmann@24844
   531
haftmann@24844
   532
val is_undefined = Symtab.defined o snd o the_cases o the_exec;
haftmann@24844
   533
haftmann@28368
   534
fun gen_add_eqn linear strict default thm thy =
haftmann@28423
   535
  case (if strict then SOME o mk_eqn thy linear else mk_liberal_eqn thy) thm
haftmann@28368
   536
   of SOME (thm, _) =>
haftmann@28143
   537
        let
haftmann@28423
   538
          val c = Code_Unit.const_eqn thm;
haftmann@28143
   539
          val _ = if strict andalso (is_some o AxClass.class_of_param thy) c
haftmann@28143
   540
            then error ("Rejected polymorphic equation for overloaded constant:\n"
haftmann@28143
   541
              ^ Display.string_of_thm thm)
haftmann@28143
   542
            else ();
haftmann@28143
   543
          val _ = if strict andalso (is_some o get_datatype_of_constr thy) c
haftmann@28143
   544
            then error ("Rejected equation for datatype constructor:\n"
haftmann@28368
   545
              ^ Display.string_of_thm thm)
haftmann@28143
   546
            else ();
haftmann@28143
   547
        in
haftmann@28368
   548
          (map_exec_purge (SOME [c]) o map_eqns) (Symtab.map_default
haftmann@28423
   549
            (c, (true, Susp.value [])) (add_thm thy default (thm, linear))) thy
haftmann@24624
   550
        end
haftmann@24624
   551
    | NONE => thy;
haftmann@24624
   552
haftmann@28368
   553
val add_eqn = gen_add_eqn true true false;
haftmann@28368
   554
val add_liberal_eqn = gen_add_eqn true false false;
haftmann@28368
   555
val add_default_eqn = gen_add_eqn true false true;
haftmann@28368
   556
val add_nonlinear_eqn = gen_add_eqn false true false;
haftmann@24219
   557
haftmann@28423
   558
fun del_eqn thm thy = case mk_syntactic_eqn thy thm
haftmann@28423
   559
 of SOME (thm, _) => let val c = Code_Unit.const_eqn thm
haftmann@28423
   560
      in map_exec_purge (SOME [c]) (map_eqns (Symtab.map_entry c (del_thm thm))) thy end
haftmann@28143
   561
  | NONE => thy;
haftmann@24219
   562
haftmann@28368
   563
fun del_eqns c = map_exec_purge (SOME [c])
haftmann@28368
   564
  (map_eqns (Symtab.map_entry c (K (false, Susp.value []))));
haftmann@26021
   565
haftmann@28368
   566
fun add_eqnl (c, lthms) thy =
haftmann@24219
   567
  let
haftmann@28423
   568
    val lthms' = certificate thy
haftmann@28423
   569
      (fn thy => map (Code_Unit.assert_linear) o certify_const thy c) lthms;
haftmann@24219
   570
  in
haftmann@28143
   571
    map_exec_purge (SOME [c])
haftmann@28368
   572
      (map_eqns (Symtab.map_default (c, (true, Susp.value []))
haftmann@28143
   573
        (add_lthms lthms'))) thy
haftmann@24219
   574
  end;
haftmann@24219
   575
haftmann@28368
   576
val add_default_eqn_attr = Attrib.internal (fn _ => Thm.declaration_attribute
haftmann@28368
   577
  (fn thm => Context.mapping (add_default_eqn thm) I));
haftmann@24219
   578
haftmann@25485
   579
structure TypeInterpretation = InterpretationFun(type T = string * serial val eq = eq_snd (op =) : T * T -> bool);
haftmann@25462
   580
haftmann@24423
   581
fun add_datatype raw_cs thy =
haftmann@24219
   582
  let
haftmann@25597
   583
    val cs = map (fn c_ty as (_, ty) => (AxClass.unoverload_const thy c_ty, ty)) raw_cs;
haftmann@28054
   584
    val (tyco, vs_cos) = Code_Unit.constrset_of_consts thy cs;
haftmann@25462
   585
    val cs' = map fst (snd vs_cos);
haftmann@25462
   586
    val purge_cs = case Symtab.lookup ((the_dtyps o the_exec) thy) tyco
haftmann@25462
   587
     of SOME (vs, cos) => if null cos then NONE else SOME (cs' @ map fst cos)
haftmann@24423
   588
      | NONE => NONE;
haftmann@24219
   589
  in
haftmann@24219
   590
    thy
haftmann@25462
   591
    |> map_exec_purge purge_cs (map_dtyps (Symtab.update (tyco, vs_cos))
haftmann@28368
   592
        #> map_eqns (fold (Symtab.delete_safe o fst) cs))
haftmann@25485
   593
    |> TypeInterpretation.data (tyco, serial ())
haftmann@24219
   594
  end;
haftmann@24219
   595
haftmann@25485
   596
fun type_interpretation f =  TypeInterpretation.interpretation
haftmann@25485
   597
  (fn (tyco, _) => fn thy => f (tyco, get_datatype thy tyco) thy);
haftmann@25485
   598
haftmann@24423
   599
fun add_datatype_cmd raw_cs thy =
haftmann@24423
   600
  let
haftmann@28054
   601
    val cs = map (Code_Unit.read_bare_const thy) raw_cs;
haftmann@24423
   602
  in add_datatype cs thy end;
haftmann@24219
   603
haftmann@24844
   604
fun add_case thm thy =
haftmann@24844
   605
  let
haftmann@28054
   606
    val entry as (c, _) = Code_Unit.case_cert thm;
haftmann@24844
   607
  in
haftmann@24844
   608
    (map_exec_purge (SOME [c]) o map_cases o apfst) (Symtab.update entry) thy
haftmann@24844
   609
  end;
haftmann@24844
   610
haftmann@24844
   611
fun add_undefined c thy =
haftmann@24844
   612
  (map_exec_purge (SOME [c]) o map_cases o apsnd) (Symtab.update (c, ())) thy;
haftmann@24844
   613
haftmann@27557
   614
val map_pre = map_exec_purge NONE o map_thmproc o apfst o apfst;
haftmann@27557
   615
val map_post = map_exec_purge NONE o map_thmproc o apfst o apsnd;
haftmann@27557
   616
haftmann@28525
   617
val add_inline = map_pre o MetaSimplifier.add_simp;
haftmann@28525
   618
val del_inline = map_pre o MetaSimplifier.del_simp;
haftmann@28525
   619
val add_post = map_post o MetaSimplifier.add_simp;
haftmann@28525
   620
val del_post = map_post o MetaSimplifier.del_simp;
haftmann@27557
   621
  
haftmann@27557
   622
fun add_functrans (name, f) =
haftmann@27557
   623
  (map_exec_purge NONE o map_thmproc o apsnd)
haftmann@24219
   624
    (AList.update (op =) (name, (serial (), f)));
haftmann@24219
   625
haftmann@27557
   626
fun del_functrans name =
haftmann@24219
   627
  (map_exec_purge NONE o map_thmproc o apsnd)
haftmann@27609
   628
    (delete_force "function transformer" name);
haftmann@24219
   629
wenzelm@26463
   630
val _ = Context.>> (Context.map_theory
haftmann@24219
   631
  (let
haftmann@24219
   632
    fun mk_attribute f = Thm.declaration_attribute (fn thm => Context.mapping (f thm) I);
haftmann@24219
   633
    fun add_simple_attribute (name, f) =
haftmann@24219
   634
      add_attribute (name, Scan.succeed (mk_attribute f));
haftmann@24219
   635
    fun add_del_attribute (name, (add, del)) =
haftmann@24219
   636
      add_attribute (name, Args.del |-- Scan.succeed (mk_attribute del)
haftmann@24219
   637
        || Scan.succeed (mk_attribute add))
haftmann@24219
   638
  in
haftmann@25462
   639
    TypeInterpretation.init
haftmann@28368
   640
    #> add_del_attribute ("func", (add_eqn, del_eqn))
haftmann@28368
   641
    #> add_simple_attribute ("nbe", add_nonlinear_eqn)
haftmann@24219
   642
    #> add_del_attribute ("inline", (add_inline, del_inline))
haftmann@24219
   643
    #> add_del_attribute ("post", (add_post, del_post))
wenzelm@26463
   644
  end));
haftmann@24219
   645
haftmann@24219
   646
haftmann@24219
   647
(** post- and preprocessing **)
haftmann@24219
   648
haftmann@24219
   649
local
haftmann@24219
   650
haftmann@28525
   651
fun get_eqns thy c =
haftmann@28525
   652
  Symtab.lookup ((the_eqns o the_exec) thy) c
haftmann@28525
   653
  |> Option.map (Susp.force o snd)
haftmann@28525
   654
  |> these
haftmann@28525
   655
  |> (map o apfst) (Thm.transfer thy);
haftmann@28525
   656
haftmann@28423
   657
fun apply_functrans thy c _ [] = []
haftmann@28423
   658
  | apply_functrans thy c [] eqns = eqns
haftmann@28423
   659
  | apply_functrans thy c functrans eqns = eqns
haftmann@28423
   660
      |> perhaps (perhaps_loop (perhaps_apply functrans))
haftmann@28525
   661
      |> (map o apfst) (AxClass.unoverload thy)
haftmann@28525
   662
      |> (map o Code_Unit.error_thm) (Code_Unit.assert_linear o apfst (Code_Unit.assert_eqn thy))
haftmann@28525
   663
      |> certify_const thy c
haftmann@28525
   664
      |> (map o apfst) (AxClass.overload thy);
haftmann@24219
   665
haftmann@28423
   666
fun rhs_conv conv thm = Thm.transitive thm ((conv o Thm.rhs_of) thm);
haftmann@24219
   667
haftmann@24837
   668
fun term_of_conv thy f =
haftmann@24837
   669
  Thm.cterm_of thy
haftmann@24837
   670
  #> f
haftmann@24837
   671
  #> Thm.prop_of
haftmann@24837
   672
  #> Logic.dest_equals
haftmann@24837
   673
  #> snd;
haftmann@24837
   674
haftmann@28423
   675
fun preprocess thy functrans c eqns =
haftmann@27582
   676
  let
haftmann@27582
   677
    val pre = (Simplifier.theory_context thy o #pre o the_thmproc o the_exec) thy;
haftmann@27582
   678
  in
haftmann@28423
   679
    eqns
haftmann@28525
   680
    |> (map o apfst) (AxClass.overload thy)
haftmann@28423
   681
    |> apply_functrans thy c functrans
haftmann@28368
   682
    |> (map o apfst) (Code_Unit.rewrite_eqn pre)
haftmann@28525
   683
    |> (map o apfst) (AxClass.unoverload thy)
haftmann@28423
   684
    |> map (Code_Unit.assert_linear o apfst (Code_Unit.assert_eqn thy))
haftmann@28423
   685
    |> burrow_fst (common_typ_eqns thy)
haftmann@27582
   686
  end;
haftmann@26970
   687
haftmann@28423
   688
in
haftmann@28423
   689
haftmann@28423
   690
fun preprocess_conv thy ct =
haftmann@24219
   691
  let
haftmann@27582
   692
    val pre = (Simplifier.theory_context thy o #pre o the_thmproc o the_exec) thy;
haftmann@24219
   693
  in
haftmann@24219
   694
    ct
haftmann@27582
   695
    |> Simplifier.rewrite pre
haftmann@25597
   696
    |> rhs_conv (AxClass.unoverload_conv thy)
haftmann@24219
   697
  end;
haftmann@24219
   698
haftmann@28423
   699
fun preprocess_term thy = term_of_conv thy (preprocess_conv thy);
haftmann@24837
   700
haftmann@28423
   701
fun postprocess_conv thy ct =
haftmann@24219
   702
  let
haftmann@27582
   703
    val post = (Simplifier.theory_context thy o #post o the_thmproc o the_exec) thy;
haftmann@24219
   704
  in
haftmann@24219
   705
    ct
haftmann@25597
   706
    |> AxClass.overload_conv thy
haftmann@27582
   707
    |> rhs_conv (Simplifier.rewrite post)
haftmann@24219
   708
  end;
haftmann@24219
   709
haftmann@28423
   710
fun postprocess_term thy = term_of_conv thy (postprocess_conv thy);
haftmann@24219
   711
haftmann@28525
   712
fun these_raw_eqns thy c =
haftmann@28525
   713
  get_eqns thy c
haftmann@28525
   714
  |> burrow_fst (common_typ_eqns thy);
haftmann@28525
   715
haftmann@28423
   716
fun these_eqns thy c =
haftmann@24219
   717
  let
haftmann@28423
   718
    val functrans = (map (fn (_, (_, f)) => f thy) o #functrans
haftmann@28423
   719
      o the_thmproc o the_exec) thy;
haftmann@24219
   720
  in
haftmann@28423
   721
    get_eqns thy c
haftmann@28423
   722
    |> preprocess thy functrans c
haftmann@24219
   723
  end;
haftmann@24219
   724
haftmann@28525
   725
fun default_typscheme thy c =
haftmann@28525
   726
  let
haftmann@28423
   727
    val typscheme = curry (Code_Unit.typscheme thy) c
haftmann@28423
   728
    val the_const_type = snd o dest_Const o TermSubst.zero_var_indexes
haftmann@28423
   729
      o curry Const "" o Sign.the_const_type thy;
haftmann@28423
   730
  in case AxClass.class_of_param thy c
haftmann@28423
   731
   of SOME class => the_const_type c
haftmann@28423
   732
        |> Term.map_type_tvar (K (TVar ((Name.aT, 0), [class])))
haftmann@28423
   733
        |> typscheme
haftmann@28423
   734
    | NONE => (case get_constr_typ thy c
haftmann@28423
   735
       of SOME ty => typscheme ty
haftmann@28423
   736
        | NONE => (case get_eqns thy c
haftmann@28423
   737
           of (thm, _) :: _ => snd (Code_Unit.head_eqn thy (Drule.zero_var_indexes thm))
haftmann@28423
   738
            | [] => typscheme (the_const_type c))) end;
haftmann@24219
   739
haftmann@24219
   740
end; (*local*)
haftmann@24219
   741
haftmann@24219
   742
end; (*struct*)
haftmann@24219
   743
haftmann@24219
   744
haftmann@24219
   745
(** type-safe interfaces for data depedent on executable content **)
haftmann@24219
   746
haftmann@24219
   747
functor CodeDataFun(Data: CODE_DATA_ARGS): CODE_DATA =
haftmann@24219
   748
struct
haftmann@24219
   749
haftmann@24219
   750
type T = Data.T;
haftmann@24219
   751
exception Data of T;
haftmann@24219
   752
fun dest (Data x) = x
haftmann@24219
   753
haftmann@24219
   754
val kind = Code.declare_data (Data Data.empty)
haftmann@27609
   755
  (fn thy => fn cs => fn Data x => Data (Data.purge thy cs x));
haftmann@24219
   756
haftmann@24219
   757
val data_op = (kind, Data, dest);
haftmann@24219
   758
haftmann@24219
   759
val get = Code.get_data data_op;
haftmann@24219
   760
val change = Code.change_data data_op;
haftmann@24219
   761
fun change_yield thy = Code.change_yield_data data_op thy;
haftmann@24219
   762
haftmann@24219
   763
end;
haftmann@24219
   764
haftmann@28143
   765
structure Code : CODE = struct open Code; end;