src/Pure/Isar/code.ML
author haftmann
Mon Oct 27 16:15:50 2008 +0100 (2008-10-27)
changeset 28695 f7a06d7284c8
parent 28673 d746a8c12c43
child 28703 aef727ef30e5
permissions -rw-r--r--
explicit history for equations; tuned
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@28368
    14
  val add_default_eqn_attr: Attrib.src
haftmann@28368
    15
  val del_eqn: thm -> theory -> theory
haftmann@28368
    16
  val del_eqns: string -> theory -> theory
wenzelm@28673
    17
  val add_eqnl: string * (thm * bool) list Lazy.T -> theory -> theory
haftmann@27557
    18
  val map_pre: (MetaSimplifier.simpset -> MetaSimplifier.simpset) -> theory -> theory
haftmann@27557
    19
  val map_post: (MetaSimplifier.simpset -> MetaSimplifier.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@24844
    38
  val get_case_data: theory -> string -> (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@28695
   114
(* defining equations *)
haftmann@28695
   115
haftmann@28695
   116
type eqns = bool * (thm * bool) list Lazy.T;
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@28359
   139
        (warning ("Code generator: dropping redundant defining 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@24844
   160
  cases: (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 {
haftmann@27557
   188
  pre: MetaSimplifier.simpset,
haftmann@27557
   189
  post: MetaSimplifier.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
haftmann@27557
   200
      val pre = MetaSimplifier.merge_ss (pre1, pre2);
haftmann@27557
   201
      val post = MetaSimplifier.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;
haftmann@27557
   223
val empty_exec = mk_exec (mk_thmproc ((MetaSimplifier.empty_ss, MetaSimplifier.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@24219
   412
        Pretty.str "defining 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,
haftmann@27557
   419
        MetaSimplifier.pretty_ss pre
haftmann@27557
   420
      ],
haftmann@27557
   421
      Pretty.block [
haftmann@27557
   422
        Pretty.str "postprocessing simpset:",
haftmann@27557
   423
        Pretty.fbrk,
haftmann@27557
   424
        MetaSimplifier.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@24219
   455
            error ("Type unificaton failed, while unifying defining 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 certify_const thy c eqns =
haftmann@24219
   465
  let
haftmann@28423
   466
    fun cert (eqn as (thm, _)) = if c = Code_Unit.const_eqn thm
haftmann@28423
   467
      then eqn else error ("Wrong head of defining equation,\nexpected constant "
haftmann@28423
   468
        ^ Code_Unit.string_of_const thy c ^ "\n" ^ Display.string_of_thm thm)
haftmann@28423
   469
  in map cert eqns end;
haftmann@24219
   470
haftmann@28423
   471
fun check_linear (eqn as (thm, linear)) =
haftmann@28423
   472
  if linear then eqn else Code_Unit.bad_thm
haftmann@28423
   473
    ("Duplicate variables on left hand side of defining equation:\n"
haftmann@28423
   474
      ^ Display.string_of_thm thm);
haftmann@28423
   475
haftmann@28423
   476
fun mk_eqn thy linear =
haftmann@28423
   477
  Code_Unit.error_thm ((if linear then check_linear else I) o Code_Unit.mk_eqn thy);
haftmann@28423
   478
fun mk_syntactic_eqn thy = Code_Unit.warning_thm (Code_Unit.mk_eqn thy);
haftmann@28423
   479
fun mk_default_eqn thy = Code_Unit.try_thm (check_linear o Code_Unit.mk_eqn thy);
haftmann@24219
   480
haftmann@24219
   481
haftmann@24219
   482
(** operational sort algebra and class discipline **)
haftmann@24219
   483
haftmann@24219
   484
local
haftmann@24219
   485
haftmann@28423
   486
fun arity_constraints thy algebra (class, tyco) =
haftmann@24219
   487
  let
haftmann@28423
   488
    val base_constraints = Sorts.mg_domain algebra tyco [class];
haftmann@28423
   489
    val classparam_constraints = Sorts.complete_sort algebra [class]
haftmann@28423
   490
      |> maps (map fst o these o try (#params o AxClass.get_info thy))
haftmann@25597
   491
      |> map_filter (fn c => try (AxClass.param_of_inst thy) (c, tyco))
haftmann@28695
   492
      |> maps (map fst o get_eqns thy)
haftmann@28423
   493
      |> map (map (snd o dest_TVar) o Sign.const_typargs thy o Code_Unit.const_typ_eqn);
haftmann@28423
   494
    val inter_sorts = map2 (curry (Sorts.inter_sort algebra));
haftmann@28423
   495
  in fold inter_sorts classparam_constraints base_constraints end;
haftmann@24219
   496
haftmann@24219
   497
fun retrieve_algebra thy operational =
wenzelm@26947
   498
  Sorts.subalgebra (Syntax.pp_global thy) operational
haftmann@28423
   499
    (arity_constraints thy (Sign.classes_of thy))
haftmann@24219
   500
    (Sign.classes_of thy);
haftmann@24219
   501
haftmann@24219
   502
in
haftmann@24219
   503
haftmann@24219
   504
fun coregular_algebra thy = retrieve_algebra thy (K true) |> snd;
haftmann@24219
   505
fun operational_algebra thy =
haftmann@24219
   506
  let
haftmann@24219
   507
    fun add_iff_operational class =
wenzelm@24928
   508
      can (AxClass.get_info thy) class ? cons class;
haftmann@24219
   509
    val operational_classes = fold add_iff_operational (Sign.all_classes thy) []
haftmann@24219
   510
  in retrieve_algebra thy (member (op =) operational_classes) end;
haftmann@24219
   511
haftmann@28143
   512
end; (*local*)
haftmann@24219
   513
haftmann@24219
   514
haftmann@24219
   515
(** interfaces and attributes **)
haftmann@24219
   516
haftmann@24624
   517
fun delete_force msg key xs =
haftmann@24624
   518
  if AList.defined (op =) xs key then AList.delete (op =) key xs
haftmann@24624
   519
  else error ("No such " ^ msg ^ ": " ^ quote key);
haftmann@24624
   520
haftmann@24423
   521
fun get_datatype thy tyco =
haftmann@28695
   522
  case these (Symtab.lookup ((the_dtyps o the_exec) thy) tyco)
haftmann@28695
   523
   of (_, spec) :: _ => spec
haftmann@28695
   524
    | [] => Sign.arity_number thy tyco
wenzelm@24848
   525
        |> Name.invents Name.context Name.aT
haftmann@24423
   526
        |> map (rpair [])
haftmann@24423
   527
        |> rpair [];
haftmann@24423
   528
haftmann@24423
   529
fun get_datatype_of_constr thy c =
haftmann@24423
   530
  case (snd o strip_type o Sign.the_const_type thy) c
haftmann@28695
   531
   of Type (tyco, _) => if member (op =) ((map fst o snd o get_datatype thy) tyco) c
haftmann@24423
   532
       then SOME tyco else NONE
haftmann@24423
   533
    | _ => NONE;
haftmann@24423
   534
haftmann@24423
   535
fun get_constr_typ thy c =
haftmann@24423
   536
  case get_datatype_of_constr thy c
haftmann@24423
   537
   of SOME tyco => let
haftmann@24423
   538
          val (vs, cos) = get_datatype thy tyco;
haftmann@24423
   539
          val SOME tys = AList.lookup (op =) cos c;
haftmann@24423
   540
          val ty = tys ---> Type (tyco, map TFree vs);
haftmann@24423
   541
        in SOME (Logic.varifyT ty) end
haftmann@24423
   542
    | NONE => NONE;
haftmann@24423
   543
haftmann@28695
   544
fun change_eqns delete c f = (map_exec_purge (SOME [c]) o map_eqns
haftmann@28695
   545
  o (if delete then Symtab.map_entry c else Symtab.map_default (c, ((false, (true, Lazy.value [])), [])))
haftmann@28695
   546
    o apfst) (fn (_, eqns) => (true, f eqns));
haftmann@24844
   547
haftmann@28695
   548
fun gen_add_eqn linear default thm thy =
haftmann@28695
   549
  case (if default then mk_default_eqn thy else SOME o mk_eqn thy linear) thm
haftmann@28368
   550
   of SOME (thm, _) =>
haftmann@28143
   551
        let
haftmann@28423
   552
          val c = Code_Unit.const_eqn thm;
haftmann@28695
   553
          val _ = if not default andalso (is_some o AxClass.class_of_param thy) c
haftmann@28143
   554
            then error ("Rejected polymorphic equation for overloaded constant:\n"
haftmann@28143
   555
              ^ Display.string_of_thm thm)
haftmann@28143
   556
            else ();
haftmann@28695
   557
          val _ = if not default andalso (is_some o get_datatype_of_constr thy) c
haftmann@28143
   558
            then error ("Rejected equation for datatype constructor:\n"
haftmann@28368
   559
              ^ Display.string_of_thm thm)
haftmann@28143
   560
            else ();
haftmann@28695
   561
        in change_eqns false c (add_thm thy default (thm, linear)) thy end
haftmann@24624
   562
    | NONE => thy;
haftmann@24624
   563
haftmann@28695
   564
val add_eqn = gen_add_eqn true false;
haftmann@28695
   565
val add_default_eqn = gen_add_eqn true true;
haftmann@28695
   566
val add_nonlinear_eqn = gen_add_eqn false false;
haftmann@26021
   567
haftmann@28368
   568
fun add_eqnl (c, lthms) thy =
haftmann@24219
   569
  let
haftmann@28423
   570
    val lthms' = certificate thy
haftmann@28423
   571
      (fn thy => map (Code_Unit.assert_linear) o certify_const thy c) lthms;
haftmann@28695
   572
  in change_eqns false c (add_lthms lthms') thy end;
haftmann@24219
   573
haftmann@28368
   574
val add_default_eqn_attr = Attrib.internal (fn _ => Thm.declaration_attribute
haftmann@28368
   575
  (fn thm => Context.mapping (add_default_eqn thm) I));
haftmann@24219
   576
haftmann@28695
   577
fun del_eqn thm thy = case mk_syntactic_eqn thy thm
haftmann@28695
   578
 of SOME (thm, _) => change_eqns true (Code_Unit.const_eqn thm) (del_thm thm) thy
haftmann@28695
   579
  | NONE => thy;
haftmann@28695
   580
haftmann@28695
   581
fun del_eqns c = change_eqns true c (K (false, Lazy.value []));
haftmann@28695
   582
haftmann@28695
   583
val get_case_data = Symtab.lookup o fst o the_cases o the_exec;
haftmann@28695
   584
haftmann@28695
   585
val is_undefined = Symtab.defined o snd o the_cases o the_exec;
haftmann@28695
   586
haftmann@25485
   587
structure TypeInterpretation = InterpretationFun(type T = string * serial val eq = eq_snd (op =) : T * T -> bool);
haftmann@25462
   588
haftmann@24423
   589
fun add_datatype raw_cs thy =
haftmann@24219
   590
  let
haftmann@25597
   591
    val cs = map (fn c_ty as (_, ty) => (AxClass.unoverload_const thy c_ty, ty)) raw_cs;
haftmann@28054
   592
    val (tyco, vs_cos) = Code_Unit.constrset_of_consts thy cs;
haftmann@25462
   593
    val cs' = map fst (snd vs_cos);
haftmann@28695
   594
    val purge_cs = case get_datatype thy tyco
haftmann@28695
   595
     of (_, []) => NONE
haftmann@28695
   596
      | (_, cos) => SOME (cs' @ map fst cos);
haftmann@24219
   597
  in
haftmann@24219
   598
    thy
haftmann@28695
   599
    |> map_exec_purge purge_cs
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@28525
   665
      |> (map o Code_Unit.error_thm) (Code_Unit.assert_linear o apfst (Code_Unit.assert_eqn thy))
haftmann@28525
   666
      |> certify_const thy c
haftmann@28525
   667
      |> (map o apfst) (AxClass.overload thy);
haftmann@24219
   668
haftmann@28423
   669
fun rhs_conv conv thm = Thm.transitive thm ((conv o Thm.rhs_of) thm);
haftmann@24219
   670
haftmann@24837
   671
fun term_of_conv thy f =
haftmann@24837
   672
  Thm.cterm_of thy
haftmann@24837
   673
  #> f
haftmann@24837
   674
  #> Thm.prop_of
haftmann@24837
   675
  #> Logic.dest_equals
haftmann@24837
   676
  #> snd;
haftmann@24837
   677
haftmann@28423
   678
fun preprocess thy functrans c eqns =
haftmann@27582
   679
  let
haftmann@27582
   680
    val pre = (Simplifier.theory_context thy o #pre o the_thmproc o the_exec) thy;
haftmann@27582
   681
  in
haftmann@28423
   682
    eqns
haftmann@28525
   683
    |> (map o apfst) (AxClass.overload thy)
haftmann@28423
   684
    |> apply_functrans thy c functrans
haftmann@28368
   685
    |> (map o apfst) (Code_Unit.rewrite_eqn pre)
haftmann@28525
   686
    |> (map o apfst) (AxClass.unoverload thy)
haftmann@28423
   687
    |> map (Code_Unit.assert_linear o apfst (Code_Unit.assert_eqn thy))
haftmann@28423
   688
    |> burrow_fst (common_typ_eqns thy)
haftmann@27582
   689
  end;
haftmann@26970
   690
haftmann@28423
   691
in
haftmann@28423
   692
haftmann@28423
   693
fun preprocess_conv thy ct =
haftmann@24219
   694
  let
haftmann@27582
   695
    val pre = (Simplifier.theory_context thy o #pre o the_thmproc o the_exec) thy;
haftmann@24219
   696
  in
haftmann@24219
   697
    ct
haftmann@27582
   698
    |> Simplifier.rewrite pre
haftmann@25597
   699
    |> rhs_conv (AxClass.unoverload_conv thy)
haftmann@24219
   700
  end;
haftmann@24219
   701
haftmann@28423
   702
fun preprocess_term thy = term_of_conv thy (preprocess_conv thy);
haftmann@24837
   703
haftmann@28423
   704
fun postprocess_conv thy ct =
haftmann@24219
   705
  let
haftmann@27582
   706
    val post = (Simplifier.theory_context thy o #post o the_thmproc o the_exec) thy;
haftmann@24219
   707
  in
haftmann@24219
   708
    ct
haftmann@25597
   709
    |> AxClass.overload_conv thy
haftmann@27582
   710
    |> rhs_conv (Simplifier.rewrite post)
haftmann@24219
   711
  end;
haftmann@24219
   712
haftmann@28423
   713
fun postprocess_term thy = term_of_conv thy (postprocess_conv thy);
haftmann@24219
   714
haftmann@28525
   715
fun these_raw_eqns thy c =
haftmann@28525
   716
  get_eqns thy c
haftmann@28695
   717
  |> (map o apfst) (Thm.transfer thy)
haftmann@28525
   718
  |> burrow_fst (common_typ_eqns thy);
haftmann@28525
   719
haftmann@28423
   720
fun these_eqns thy c =
haftmann@24219
   721
  let
haftmann@28423
   722
    val functrans = (map (fn (_, (_, f)) => f thy) o #functrans
haftmann@28423
   723
      o the_thmproc o the_exec) thy;
haftmann@24219
   724
  in
haftmann@28423
   725
    get_eqns thy c
haftmann@28695
   726
    |> (map o apfst) (Thm.transfer thy)
haftmann@28423
   727
    |> preprocess thy functrans c
haftmann@24219
   728
  end;
haftmann@24219
   729
haftmann@28525
   730
fun default_typscheme thy c =
haftmann@28525
   731
  let
haftmann@28423
   732
    val typscheme = curry (Code_Unit.typscheme thy) c
haftmann@28423
   733
    val the_const_type = snd o dest_Const o TermSubst.zero_var_indexes
haftmann@28423
   734
      o curry Const "" o Sign.the_const_type thy;
haftmann@28423
   735
  in case AxClass.class_of_param thy c
haftmann@28423
   736
   of SOME class => the_const_type c
haftmann@28423
   737
        |> Term.map_type_tvar (K (TVar ((Name.aT, 0), [class])))
haftmann@28423
   738
        |> typscheme
haftmann@28423
   739
    | NONE => (case get_constr_typ thy c
haftmann@28423
   740
       of SOME ty => typscheme ty
haftmann@28423
   741
        | NONE => (case get_eqns thy c
haftmann@28423
   742
           of (thm, _) :: _ => snd (Code_Unit.head_eqn thy (Drule.zero_var_indexes thm))
haftmann@28423
   743
            | [] => typscheme (the_const_type c))) end;
haftmann@24219
   744
haftmann@24219
   745
end; (*local*)
haftmann@24219
   746
haftmann@24219
   747
end; (*struct*)
haftmann@24219
   748
haftmann@24219
   749
haftmann@24219
   750
(** type-safe interfaces for data depedent on executable content **)
haftmann@24219
   751
haftmann@24219
   752
functor CodeDataFun(Data: CODE_DATA_ARGS): CODE_DATA =
haftmann@24219
   753
struct
haftmann@24219
   754
haftmann@24219
   755
type T = Data.T;
haftmann@24219
   756
exception Data of T;
haftmann@24219
   757
fun dest (Data x) = x
haftmann@24219
   758
haftmann@24219
   759
val kind = Code.declare_data (Data Data.empty)
haftmann@27609
   760
  (fn thy => fn cs => fn Data x => Data (Data.purge thy cs x));
haftmann@24219
   761
haftmann@24219
   762
val data_op = (kind, Data, dest);
haftmann@24219
   763
haftmann@24219
   764
val get = Code.get_data data_op;
haftmann@24219
   765
val change = Code.change_data data_op;
haftmann@24219
   766
fun change_yield thy = Code.change_yield_data data_op thy;
haftmann@24219
   767
haftmann@24219
   768
end;
haftmann@24219
   769
haftmann@28143
   770
structure Code : CODE = struct open Code; end;