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