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