src/Pure/Isar/code.ML
author haftmann
Tue Jul 15 16:02:07 2008 +0200 (2008-07-15 ago)
changeset 27609 b23c9ad0fe7d
parent 27582 367aff8d7ffd
child 27675 cb0021d56e11
permissions -rw-r--r--
tuned code theorem bookkeeping
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@24624
    11
  val add_func: thm -> theory -> theory
haftmann@24624
    12
  val add_liberal_func: thm -> theory -> theory
haftmann@24624
    13
  val add_default_func: thm -> theory -> theory
haftmann@24624
    14
  val add_default_func_attr: Attrib.src
haftmann@24219
    15
  val del_func: thm -> theory -> theory
haftmann@26021
    16
  val del_funcs: string -> theory -> theory
haftmann@24423
    17
  val add_funcl: string * thm list Susp.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@24219
    21
  val del_inline: thm -> theory -> theory
haftmann@24219
    22
  val add_post: thm -> theory -> theory
haftmann@24219
    23
  val del_post: thm -> theory -> theory
haftmann@27609
    24
  val add_functrans: string * (theory -> thm list -> thm list option) -> theory -> theory
haftmann@27557
    25
  val del_functrans: string -> theory -> theory
haftmann@24423
    26
  val add_datatype: (string * typ) list -> theory -> theory
haftmann@24423
    27
  val add_datatype_cmd: string list -> theory -> theory
haftmann@25485
    28
  val type_interpretation:
haftmann@25485
    29
    (string * ((string * sort) list * (string * typ list) list)
haftmann@25485
    30
      -> theory -> theory) -> theory -> theory
haftmann@24844
    31
  val add_case: thm -> theory -> theory
haftmann@24844
    32
  val add_undefined: string -> theory -> theory
haftmann@24219
    33
haftmann@24219
    34
  val coregular_algebra: theory -> Sorts.algebra
haftmann@24219
    35
  val operational_algebra: theory -> (sort -> sort) * Sorts.algebra
haftmann@24423
    36
  val these_funcs: theory -> string -> thm 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@26970
    41
  val default_typ: theory -> string -> (string * sort) list * typ
haftmann@24219
    42
haftmann@24219
    43
  val preprocess_conv: cterm -> thm
haftmann@24837
    44
  val preprocess_term: theory -> term -> term
haftmann@24219
    45
  val postprocess_conv: 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@25312
    96
    fun add_parser ("", parser) attrs = attrs @ [("", parser)]
haftmann@25312
    97
      | add_parser (name, parser) attrs = (name, Args.$$$ name |-- parser) :: attrs;
haftmann@25312
    98
    fun error "" = error ("Code attribute already declared")
haftmann@25312
    99
      | error name = error ("Code attribute " ^ name ^ " already declared")
haftmann@25312
   100
  in CodeAttr.map (fn attrs => if AList.defined (op =) attrs name
haftmann@25312
   101
    then error name else add_parser attr attrs)
haftmann@25312
   102
  end;
haftmann@25312
   103
haftmann@25312
   104
val _ =
haftmann@25312
   105
  let
haftmann@25312
   106
    val code_attr = Attrib.syntax (Scan.peek (fn context =>
haftmann@25312
   107
      List.foldr op || Scan.fail (map snd (CodeAttr.get (Context.theory_of context)))));
haftmann@25312
   108
  in
wenzelm@26463
   109
    Context.>> (Context.map_theory
wenzelm@26463
   110
      (Attrib.add_attributes
wenzelm@26463
   111
        [("code", code_attr, "declare theorems for code generation")]))
haftmann@25312
   112
  end;
haftmann@25312
   113
haftmann@25312
   114
haftmann@25312
   115
(** certificate theorems **)
haftmann@24219
   116
haftmann@24219
   117
fun string_of_lthms r = case Susp.peek r
wenzelm@26928
   118
 of SOME thms => (map Display.string_of_thm o rev) thms
haftmann@24219
   119
  | NONE => ["[...]"];
haftmann@24219
   120
haftmann@24219
   121
fun pretty_lthms ctxt r = case Susp.peek r
haftmann@24219
   122
 of SOME thms => map (ProofContext.pretty_thm ctxt) thms
haftmann@24219
   123
  | NONE => [Pretty.str "[...]"];
haftmann@24219
   124
haftmann@24219
   125
fun certificate thy f r =
haftmann@24219
   126
  case Susp.peek r
haftmann@24219
   127
   of SOME thms => (Susp.value o f thy) thms
haftmann@24844
   128
    | NONE => let
haftmann@24844
   129
        val thy_ref = Theory.check_thy thy;
haftmann@24844
   130
      in Susp.delay (fn () => (f (Theory.deref thy_ref) o Susp.force) r) end;
haftmann@24219
   131
haftmann@24219
   132
haftmann@25312
   133
(** logical and syntactical specification of executable code **)
haftmann@24219
   134
haftmann@24219
   135
(* pairs of (selected, deleted) defining equations *)
haftmann@24219
   136
haftmann@24219
   137
type sdthms = thm list Susp.T * thm list;
haftmann@24219
   138
haftmann@24219
   139
fun add_drop_redundant thm (sels, dels) =
haftmann@24219
   140
  let
haftmann@24219
   141
    val thy = Thm.theory_of_thm thm;
haftmann@24219
   142
    val args_of = snd o strip_comb o fst o Logic.dest_equals o Thm.plain_prop_of;
haftmann@24219
   143
    val args = args_of thm;
haftmann@24219
   144
    fun matches [] _ = true
haftmann@24219
   145
      | matches (Var _ :: xs) [] = matches xs []
haftmann@24219
   146
      | matches (_ :: _) [] = false
haftmann@24219
   147
      | matches (x :: xs) (y :: ys) = Pattern.matches thy (x, y) andalso matches xs ys;
haftmann@24219
   148
    fun drop thm' = not (matches args (args_of thm'))
wenzelm@26947
   149
      orelse (warning ("Code generator: dropping redundant defining equation\n" ^ Display.string_of_thm thm'); false);
haftmann@24219
   150
    val (keeps, drops) = List.partition drop sels;
haftmann@24219
   151
  in (thm :: keeps, dels |> remove Thm.eq_thm_prop thm |> fold (insert Thm.eq_thm_prop) drops) end;
haftmann@24219
   152
haftmann@24219
   153
fun add_thm thm (sels, dels) =
haftmann@24219
   154
  apfst Susp.value (add_drop_redundant thm (Susp.force sels, dels));
haftmann@24219
   155
haftmann@24219
   156
fun add_lthms lthms (sels, []) =
haftmann@24219
   157
      (Susp.delay (fn () => fold add_drop_redundant
haftmann@24219
   158
        (Susp.force lthms) (Susp.force sels, []) |> fst), [])
haftmann@24219
   159
        (*FIXME*)
haftmann@24219
   160
  | add_lthms lthms (sels, dels) =
haftmann@24219
   161
      fold add_thm (Susp.force lthms) (sels, dels);
haftmann@24219
   162
haftmann@24219
   163
fun del_thm thm (sels, dels) =
haftmann@24219
   164
  (Susp.value (remove Thm.eq_thm_prop thm (Susp.force sels)), thm :: dels);
haftmann@24219
   165
haftmann@26021
   166
fun del_thms (sels, dels) =
haftmann@26021
   167
  let
haftmann@26021
   168
    val all_sels = Susp.force sels;
haftmann@26021
   169
  in (Susp.value [], rev all_sels @ dels) end;
haftmann@26021
   170
haftmann@24219
   171
fun pretty_sdthms ctxt (sels, _) = pretty_lthms ctxt sels;
haftmann@24219
   172
haftmann@25312
   173
fun melt _ ([], []) = (false, [])
haftmann@25312
   174
  | melt _ ([], ys) = (true, ys)
haftmann@25312
   175
  | melt eq (xs, ys) = fold_rev
haftmann@25312
   176
      (fn y => fn (t, xs) => (t orelse not (member eq xs y), insert eq y xs)) ys (false, xs);
haftmann@25312
   177
haftmann@25312
   178
val melt_thms = melt Thm.eq_thm_prop;
haftmann@25312
   179
haftmann@25312
   180
fun melt_lthms (r1, r2) =
haftmann@25312
   181
  if Susp.same (r1, r2)
haftmann@25312
   182
    then (false, r1)
haftmann@25312
   183
  else case Susp.peek r1
haftmann@25312
   184
   of SOME [] => (true, r2)
haftmann@25312
   185
    | _ => case Susp.peek r2
haftmann@25312
   186
       of SOME [] => (true, r1)
haftmann@25312
   187
        | _ => (apsnd (Susp.delay o K)) (melt_thms (Susp.force r1, Susp.force r2));
haftmann@25312
   188
haftmann@25312
   189
fun melt_sdthms ((sels1, dels1), (sels2, dels2)) =
haftmann@24219
   190
  let
haftmann@25312
   191
    val (dels_t, dels) = melt_thms (dels1, dels2);
haftmann@24219
   192
  in if dels_t
haftmann@24219
   193
    then let
haftmann@25312
   194
      val (_, sels) = melt_thms
haftmann@24219
   195
        (subtract Thm.eq_thm_prop dels2 (Susp.force sels1), Susp.force sels2);
haftmann@25312
   196
      val (_, dels) = melt_thms
haftmann@24219
   197
        (subtract Thm.eq_thm_prop (Susp.force sels2) dels1, dels2);
haftmann@27609
   198
    in ((Susp.delay o K) sels, dels) end
haftmann@24219
   199
    else let
haftmann@27609
   200
      val (_, sels) = melt_lthms (sels1, sels2);
haftmann@27609
   201
    in (sels, dels) end
haftmann@24219
   202
  end;
haftmann@24219
   203
haftmann@24219
   204
haftmann@25312
   205
(* specification data *)
haftmann@24219
   206
haftmann@24219
   207
val eq_string = op = : string * string -> bool;
haftmann@24219
   208
fun eq_dtyp ((vs1, cs1), (vs2, cs2)) = 
haftmann@24219
   209
  gen_eq_set (eq_pair eq_string (gen_eq_set eq_string)) (vs1, vs2)
haftmann@24423
   210
    andalso gen_eq_set (eq_fst eq_string) (cs1, cs2);
haftmann@27582
   211
fun merge_dtyps (tabs as (tab1, tab2)) =
haftmann@24219
   212
  let
haftmann@27582
   213
    fun join _ (cos as (_, cos2)) = if eq_dtyp cos then raise Symtab.SAME else cos2;
haftmann@27582
   214
  in Symtab.join join tabs end;
haftmann@24219
   215
haftmann@24219
   216
datatype spec = Spec of {
haftmann@27609
   217
  funcs: sdthms Symtab.table,
haftmann@24844
   218
  dtyps: ((string * sort) list * (string * typ list) list) Symtab.table,
haftmann@24844
   219
  cases: (int * string list) Symtab.table * unit Symtab.table
haftmann@24219
   220
};
haftmann@24219
   221
haftmann@24844
   222
fun mk_spec (funcs, (dtyps, cases)) =
haftmann@24844
   223
  Spec { funcs = funcs, dtyps = dtyps, cases = cases };
haftmann@24844
   224
fun map_spec f (Spec { funcs = funcs, dtyps = dtyps, cases = cases }) =
haftmann@24844
   225
  mk_spec (f (funcs, (dtyps, cases)));
haftmann@27609
   226
fun merge_spec (Spec { funcs = funcs1, dtyps = dtyps1, cases = (cases1, undefs1) },
haftmann@27609
   227
  Spec { funcs = funcs2, dtyps = dtyps2, cases = (cases2, undefs2) }) =
haftmann@24219
   228
  let
haftmann@27609
   229
    val funcs = Symtab.join (K melt_sdthms) (funcs1, funcs2);
haftmann@27582
   230
    val dtyps = merge_dtyps (dtyps1, dtyps2);
haftmann@27609
   231
    val cases = (Symtab.merge (K true) (cases1, cases2),
haftmann@27609
   232
      Symtab.merge (K true) (undefs1, undefs2));
haftmann@27557
   233
  in mk_spec (funcs, (dtyps, cases)) end;
haftmann@24219
   234
haftmann@25312
   235
haftmann@25312
   236
(* pre- and postprocessor *)
haftmann@25312
   237
haftmann@25312
   238
datatype thmproc = Thmproc of {
haftmann@27557
   239
  pre: MetaSimplifier.simpset,
haftmann@27557
   240
  post: MetaSimplifier.simpset,
haftmann@27609
   241
  functrans: (string * (serial * (theory -> thm list -> thm list option))) list
haftmann@25312
   242
};
haftmann@25312
   243
haftmann@27557
   244
fun mk_thmproc ((pre, post), functrans) =
haftmann@27557
   245
  Thmproc { pre = pre, post = post, functrans = functrans };
haftmann@27557
   246
fun map_thmproc f (Thmproc { pre, post, functrans }) =
haftmann@27557
   247
  mk_thmproc (f ((pre, post), functrans));
haftmann@27557
   248
fun merge_thmproc (Thmproc { pre = pre1, post = post1, functrans = functrans1 },
haftmann@27557
   249
  Thmproc { pre = pre2, post = post2, functrans = functrans2 }) =
haftmann@25312
   250
    let
haftmann@27557
   251
      val pre = MetaSimplifier.merge_ss (pre1, pre2);
haftmann@27557
   252
      val post = MetaSimplifier.merge_ss (post1, post2);
haftmann@27557
   253
      val functrans = AList.merge (op =) (eq_fst (op =)) (functrans1, functrans2);
haftmann@27557
   254
    in mk_thmproc ((pre, post), functrans) end;
haftmann@25312
   255
haftmann@24219
   256
datatype exec = Exec of {
haftmann@24219
   257
  thmproc: thmproc,
haftmann@24219
   258
  spec: spec
haftmann@24219
   259
};
haftmann@24219
   260
haftmann@24219
   261
fun mk_exec (thmproc, spec) =
haftmann@24219
   262
  Exec { thmproc = thmproc, spec = spec };
haftmann@24219
   263
fun map_exec f (Exec { thmproc = thmproc, spec = spec }) =
haftmann@24219
   264
  mk_exec (f (thmproc, spec));
haftmann@27557
   265
fun merge_exec (Exec { thmproc = thmproc1, spec = spec1 },
haftmann@24219
   266
  Exec { thmproc = thmproc2, spec = spec2 }) =
haftmann@24219
   267
  let
haftmann@27557
   268
    val thmproc = merge_thmproc (thmproc1, thmproc2);
haftmann@27557
   269
    val spec = merge_spec (spec1, spec2);
haftmann@27557
   270
  in mk_exec (thmproc, spec) end;
haftmann@27557
   271
val empty_exec = mk_exec (mk_thmproc ((MetaSimplifier.empty_ss, MetaSimplifier.empty_ss), []),
haftmann@24844
   272
  mk_spec (Symtab.empty, (Symtab.empty, (Symtab.empty, Symtab.empty))));
haftmann@24219
   273
haftmann@25312
   274
fun the_thmproc (Exec { thmproc = Thmproc x, ...}) = x;
haftmann@24219
   275
fun the_spec (Exec { spec = Spec x, ...}) = x;
haftmann@24219
   276
val the_funcs = #funcs o the_spec;
haftmann@24219
   277
val the_dtyps = #dtyps o the_spec;
haftmann@24844
   278
val the_cases = #cases o the_spec;
haftmann@24219
   279
val map_thmproc = map_exec o apfst o map_thmproc;
haftmann@24219
   280
val map_funcs = map_exec o apsnd o map_spec o apfst;
haftmann@24844
   281
val map_dtyps = map_exec o apsnd o map_spec o apsnd o apfst;
haftmann@24844
   282
val map_cases = map_exec o apsnd o map_spec o apsnd o apsnd;
haftmann@24219
   283
haftmann@24219
   284
haftmann@24219
   285
(* data slots dependent on executable content *)
haftmann@24219
   286
haftmann@24219
   287
(*private copy avoids potential conflict of table exceptions*)
haftmann@24219
   288
structure Datatab = TableFun(type key = int val ord = int_ord);
haftmann@24219
   289
haftmann@24219
   290
local
haftmann@24219
   291
haftmann@24219
   292
type kind = {
haftmann@24219
   293
  empty: Object.T,
haftmann@27609
   294
  purge: theory -> string list -> Object.T -> Object.T
haftmann@24219
   295
};
haftmann@24219
   296
haftmann@24219
   297
val kinds = ref (Datatab.empty: kind Datatab.table);
haftmann@24219
   298
val kind_keys = ref ([]: serial list);
haftmann@24219
   299
haftmann@24219
   300
fun invoke f k = case Datatab.lookup (! kinds) k
haftmann@24219
   301
 of SOME kind => f kind
haftmann@24219
   302
  | NONE => sys_error "Invalid code data identifier";
haftmann@24219
   303
haftmann@24219
   304
in
haftmann@24219
   305
haftmann@27609
   306
fun declare_data empty purge =
haftmann@24219
   307
  let
haftmann@24219
   308
    val k = serial ();
haftmann@27609
   309
    val kind = {empty = empty, purge = purge};
haftmann@24219
   310
    val _ = change kinds (Datatab.update (k, kind));
haftmann@24219
   311
    val _ = change kind_keys (cons k);
haftmann@24219
   312
  in k end;
haftmann@24219
   313
haftmann@27609
   314
fun invoke_init k = invoke (fn kind => #empty kind) k;
haftmann@24219
   315
haftmann@27609
   316
fun invoke_purge_all thy cs =
haftmann@24219
   317
  fold (fn k => Datatab.map_entry k
haftmann@27609
   318
    (invoke (fn kind => #purge kind thy cs) k)) (! kind_keys);
haftmann@24219
   319
haftmann@24219
   320
end; (*local*)
haftmann@24219
   321
haftmann@24219
   322
haftmann@25312
   323
(** theory store **)
haftmann@24219
   324
haftmann@24219
   325
local
haftmann@24219
   326
haftmann@24219
   327
type data = Object.T Datatab.table;
haftmann@27609
   328
val empty_data = Datatab.empty : data;
haftmann@24219
   329
haftmann@24219
   330
structure CodeData = TheoryDataFun
haftmann@24219
   331
(
haftmann@24219
   332
  type T = exec * data ref;
haftmann@27609
   333
  val empty = (empty_exec, ref empty_data);
haftmann@24219
   334
  fun copy (exec, data) = (exec, ref (! data));
haftmann@24219
   335
  val extend = copy;
haftmann@24219
   336
  fun merge pp ((exec1, data1), (exec2, data2)) =
haftmann@27609
   337
    (merge_exec (exec1, exec2), ref empty_data);
haftmann@24219
   338
);
haftmann@24219
   339
wenzelm@26463
   340
val _ = Context.>> (Context.map_theory CodeData.init);
haftmann@24219
   341
haftmann@24219
   342
fun thy_data f thy = f ((snd o CodeData.get) thy);
haftmann@24219
   343
haftmann@24219
   344
fun get_ensure_init kind data_ref =
haftmann@24219
   345
  case Datatab.lookup (! data_ref) kind
haftmann@24219
   346
   of SOME x => x
haftmann@27609
   347
    | NONE => let val y = invoke_init kind
haftmann@24219
   348
        in (change data_ref (Datatab.update (kind, y)); y) end;
haftmann@24219
   349
haftmann@24219
   350
in
haftmann@24219
   351
haftmann@24219
   352
(* access to executable content *)
haftmann@24219
   353
haftmann@24844
   354
val the_exec = fst o CodeData.get;
haftmann@24219
   355
haftmann@24219
   356
fun map_exec_purge touched f thy =
haftmann@27609
   357
  CodeData.map (fn (exec, data) => (f exec, ref (case touched
haftmann@27609
   358
   of SOME cs => invoke_purge_all thy cs (! data)
haftmann@27609
   359
    | NONE => empty_data))) thy;
haftmann@24219
   360
haftmann@24219
   361
haftmann@24219
   362
(* access to data dependent on abstract executable content *)
haftmann@24219
   363
haftmann@24219
   364
fun get_data (kind, _, dest) = thy_data (get_ensure_init kind #> dest);
haftmann@24219
   365
haftmann@24219
   366
fun change_data (kind, mk, dest) =
haftmann@24219
   367
  let
haftmann@24219
   368
    fun chnge data_ref f =
haftmann@24219
   369
      let
haftmann@24219
   370
        val data = get_ensure_init kind data_ref;
haftmann@24219
   371
        val data' = f (dest data);
haftmann@24219
   372
      in (change data_ref (Datatab.update (kind, mk data')); data') end;
haftmann@24219
   373
  in thy_data chnge end;
haftmann@24219
   374
haftmann@24219
   375
fun change_yield_data (kind, mk, dest) =
haftmann@24219
   376
  let
haftmann@24219
   377
    fun chnge data_ref f =
haftmann@24219
   378
      let
haftmann@24219
   379
        val data = get_ensure_init kind data_ref;
haftmann@24219
   380
        val (x, data') = f (dest data);
haftmann@24219
   381
      in (x, (change data_ref (Datatab.update (kind, mk data')); data')) end;
haftmann@24219
   382
  in thy_data chnge end;
haftmann@24219
   383
haftmann@24219
   384
end; (*local*)
haftmann@24219
   385
haftmann@24219
   386
haftmann@24219
   387
(* print executable content *)
haftmann@24219
   388
haftmann@24219
   389
fun print_codesetup thy =
haftmann@24219
   390
  let
haftmann@24219
   391
    val ctxt = ProofContext.init thy;
haftmann@24844
   392
    val exec = the_exec thy;
haftmann@24219
   393
    fun pretty_func (s, lthms) =
haftmann@24219
   394
      (Pretty.block o Pretty.fbreaks) (
haftmann@24219
   395
        Pretty.str s :: pretty_sdthms ctxt lthms
haftmann@24219
   396
      );
haftmann@24219
   397
    fun pretty_dtyp (s, []) =
haftmann@24219
   398
          Pretty.str s
haftmann@24219
   399
      | pretty_dtyp (s, cos) =
haftmann@24219
   400
          (Pretty.block o Pretty.breaks) (
haftmann@24219
   401
            Pretty.str s
haftmann@24219
   402
            :: Pretty.str "="
haftmann@24219
   403
            :: separate (Pretty.str "|") (map (fn (c, []) => Pretty.str c
haftmann@24219
   404
                 | (c, tys) =>
haftmann@24219
   405
                     (Pretty.block o Pretty.breaks)
haftmann@24423
   406
                        (Pretty.str (CodeUnit.string_of_const thy c)
wenzelm@26947
   407
                          :: Pretty.str "of"
wenzelm@26947
   408
                          :: map (Pretty.quote o Syntax.pretty_typ_global thy) tys)) cos)
haftmann@24219
   409
          );
haftmann@27557
   410
    val pre = (#pre o the_thmproc) exec;
haftmann@27557
   411
    val post = (#post o the_thmproc) exec;
haftmann@27557
   412
    val functrans = (map fst o #functrans o the_thmproc) exec;
haftmann@27609
   413
    val funcs = the_funcs exec
haftmann@24423
   414
      |> Symtab.dest
haftmann@24219
   415
      |> (map o apfst) (CodeUnit.string_of_const thy)
haftmann@24219
   416
      |> sort (string_ord o pairself fst);
haftmann@24219
   417
    val dtyps = the_dtyps exec
haftmann@24219
   418
      |> Symtab.dest
wenzelm@26947
   419
      |> map (fn (dtco, (vs, cos)) =>
wenzelm@26947
   420
          (Syntax.string_of_typ_global thy (Type (dtco, map TFree vs)), cos))
haftmann@24219
   421
      |> sort (string_ord o pairself fst)
haftmann@24219
   422
  in
haftmann@24219
   423
    (Pretty.writeln o Pretty.chunks) [
haftmann@24219
   424
      Pretty.block (
haftmann@24219
   425
        Pretty.str "defining equations:"
haftmann@24219
   426
        :: Pretty.fbrk
haftmann@27609
   427
        :: (Pretty.fbreaks o map pretty_func) funcs
haftmann@24219
   428
      ),
haftmann@27557
   429
      Pretty.block [
haftmann@27557
   430
        Pretty.str "preprocessing simpset:",
haftmann@27557
   431
        Pretty.fbrk,
haftmann@27557
   432
        MetaSimplifier.pretty_ss pre
haftmann@27557
   433
      ],
haftmann@27557
   434
      Pretty.block [
haftmann@27557
   435
        Pretty.str "postprocessing simpset:",
haftmann@27557
   436
        Pretty.fbrk,
haftmann@27557
   437
        MetaSimplifier.pretty_ss post
haftmann@27557
   438
      ],
haftmann@24219
   439
      Pretty.block (
haftmann@27609
   440
        Pretty.str "function transformers:"
haftmann@24219
   441
        :: Pretty.fbrk
haftmann@27557
   442
        :: (Pretty.fbreaks o map Pretty.str) functrans
haftmann@25968
   443
      ),
haftmann@25968
   444
      Pretty.block (
haftmann@24219
   445
        Pretty.str "datatypes:"
haftmann@24219
   446
        :: Pretty.fbrk
haftmann@24219
   447
        :: (Pretty.fbreaks o map pretty_dtyp) dtyps
haftmann@24219
   448
      )
haftmann@24219
   449
    ]
haftmann@24219
   450
  end;
haftmann@24219
   451
haftmann@24219
   452
haftmann@24219
   453
haftmann@24219
   454
(** theorem transformation and certification **)
haftmann@24219
   455
haftmann@26970
   456
fun const_of thy = dest_Const o fst o strip_comb o fst o Logic.dest_equals
haftmann@26970
   457
  o ObjectLogic.drop_judgment thy o Thm.plain_prop_of;
haftmann@26970
   458
haftmann@26970
   459
fun const_of_func thy = AxClass.unoverload_const thy o const_of thy;
haftmann@26970
   460
haftmann@24219
   461
fun common_typ_funcs [] = []
haftmann@24219
   462
  | common_typ_funcs [thm] = [thm]
haftmann@26970
   463
  | common_typ_funcs (thms as thm :: _) = (*FIXME is too general*)
haftmann@24219
   464
      let
haftmann@24219
   465
        val thy = Thm.theory_of_thm thm;
haftmann@24219
   466
        fun incr_thm thm max =
haftmann@24219
   467
          let
haftmann@24219
   468
            val thm' = incr_indexes max thm;
haftmann@24219
   469
            val max' = Thm.maxidx_of thm' + 1;
haftmann@24219
   470
          in (thm', max') end;
haftmann@24219
   471
        val (thms', maxidx) = fold_map incr_thm thms 0;
haftmann@26970
   472
        val ty1 :: tys = map (snd o const_of thy) thms';
haftmann@24219
   473
        fun unify ty env = Sign.typ_unify thy (ty1, ty) env
haftmann@24219
   474
          handle Type.TUNIFY =>
haftmann@24219
   475
            error ("Type unificaton failed, while unifying defining equations\n"
haftmann@24219
   476
            ^ (cat_lines o map Display.string_of_thm) thms
haftmann@24219
   477
            ^ "\nwith types\n"
haftmann@24219
   478
            ^ (cat_lines o map (CodeUnit.string_of_typ thy)) (ty1 :: tys));
haftmann@24219
   479
        val (env, _) = fold unify tys (Vartab.empty, maxidx)
haftmann@24219
   480
        val instT = Vartab.fold (fn (x_i, (sort, ty)) =>
haftmann@24219
   481
          cons (Thm.ctyp_of thy (TVar (x_i, sort)), Thm.ctyp_of thy ty)) env [];
haftmann@24219
   482
      in map (Thm.instantiate (instT, [])) thms' end;
haftmann@24219
   483
haftmann@24219
   484
fun certify_const thy const thms =
haftmann@24219
   485
  let
haftmann@24423
   486
    fun cert thm = if const = const_of_func thy thm
haftmann@24219
   487
      then thm else error ("Wrong head of defining equation,\nexpected constant "
wenzelm@26928
   488
        ^ CodeUnit.string_of_const thy const ^ "\n" ^ Display.string_of_thm thm)
haftmann@24219
   489
  in map cert thms end;
haftmann@24219
   490
haftmann@24219
   491
haftmann@24219
   492
haftmann@24219
   493
(** operational sort algebra and class discipline **)
haftmann@24219
   494
haftmann@24219
   495
local
haftmann@24219
   496
haftmann@24219
   497
fun aggr_neutr f y [] = y
haftmann@24219
   498
  | aggr_neutr f y (x::xs) = aggr_neutr f (f y x) xs;
haftmann@24219
   499
haftmann@24219
   500
fun aggregate f [] = NONE
haftmann@24219
   501
  | aggregate f (x::xs) = SOME (aggr_neutr f x xs);
haftmann@24219
   502
haftmann@25462
   503
fun inter_sorts algebra =
haftmann@25462
   504
  aggregate (map2 (curry (Sorts.inter_sort algebra)));
haftmann@24219
   505
haftmann@24219
   506
fun specific_constraints thy (class, tyco) =
haftmann@24219
   507
  let
haftmann@24219
   508
    val vs = Name.invents Name.context "" (Sign.arity_number thy tyco);
wenzelm@24969
   509
    val classparams = (map fst o these o try (#params o AxClass.get_info thy)) class;
haftmann@24837
   510
    val funcs = classparams
haftmann@25597
   511
      |> map_filter (fn c => try (AxClass.param_of_inst thy) (c, tyco))
haftmann@24844
   512
      |> map (Symtab.lookup ((the_funcs o the_exec) thy))
haftmann@27609
   513
      |> (map o Option.map) (Susp.force o fst)
haftmann@24219
   514
      |> maps these
haftmann@24423
   515
      |> map (Thm.transfer thy)
haftmann@24423
   516
    fun sorts_of [Type (_, tys)] = map (snd o dest_TVar) tys
haftmann@24423
   517
      | sorts_of tys = map (snd o dest_TVar) tys;
haftmann@26970
   518
    val sorts = map (sorts_of o Sign.const_typargs thy o const_of thy) funcs;
haftmann@24219
   519
  in sorts end;
haftmann@24219
   520
haftmann@25462
   521
fun weakest_constraints thy algebra (class, tyco) =
haftmann@24219
   522
  let
haftmann@25462
   523
    val all_superclasses = Sorts.complete_sort algebra [class];
haftmann@25462
   524
  in case inter_sorts algebra (maps (fn class => specific_constraints thy (class, tyco)) all_superclasses)
haftmann@24219
   525
   of SOME sorts => sorts
haftmann@25462
   526
    | NONE => Sorts.mg_domain algebra tyco [class]
haftmann@24219
   527
  end;
haftmann@24219
   528
haftmann@25462
   529
fun strongest_constraints thy algebra (class, tyco) =
haftmann@24219
   530
  let
haftmann@24219
   531
    val all_subclasses = class :: Graph.all_preds ((#classes o Sorts.rep_algebra) algebra) [class];
haftmann@24219
   532
    val inst_subclasses = filter (can (Sorts.mg_domain algebra tyco) o single) all_subclasses;
haftmann@25462
   533
  in case inter_sorts algebra (maps (fn class => specific_constraints thy (class, tyco)) inst_subclasses)
haftmann@24219
   534
   of SOME sorts => sorts
haftmann@24219
   535
    | NONE => replicate
haftmann@25462
   536
        (Sign.arity_number thy tyco) (Sorts.minimize_sort algebra (Sorts.all_classes algebra))
haftmann@25462
   537
  end;
haftmann@25462
   538
haftmann@25462
   539
fun get_algebra thy (class, tyco) =
haftmann@25462
   540
  let
haftmann@25462
   541
    val base_algebra = Sign.classes_of thy;
haftmann@25462
   542
  in if can (Sorts.mg_domain base_algebra tyco) [class]
haftmann@25462
   543
    then base_algebra
haftmann@25462
   544
    else let
haftmann@25462
   545
      val superclasses = Sorts.super_classes base_algebra class;
haftmann@25462
   546
      val sorts = inter_sorts base_algebra
haftmann@25462
   547
          (map_filter (fn class => try (Sorts.mg_domain base_algebra tyco) [class]) superclasses)
haftmann@25462
   548
        |> the_default (replicate (Sign.arity_number thy tyco) [])
haftmann@25462
   549
    in
haftmann@25462
   550
      base_algebra
wenzelm@26947
   551
      |> Sorts.add_arities (Syntax.pp_global thy) (tyco, [(class, sorts)])
haftmann@25462
   552
    end
haftmann@24219
   553
  end;
haftmann@24219
   554
haftmann@24837
   555
fun gen_classparam_typ constr thy class (c, tyco) = 
haftmann@24219
   556
  let
haftmann@25462
   557
    val algebra = get_algebra thy (class, tyco);
wenzelm@24969
   558
    val cs = these (try (#params o AxClass.get_info thy) class);
haftmann@25462
   559
    val SOME ty = AList.lookup (op =) cs c;
wenzelm@24969
   560
    val sort_args = Name.names (Name.declare Name.aT Name.context) Name.aT
haftmann@25462
   561
      (constr thy algebra (class, tyco));
haftmann@24219
   562
    val ty_inst = Type (tyco, map TFree sort_args);
haftmann@24219
   563
  in Logic.varifyT (map_type_tfree (K ty_inst) ty) end;
haftmann@24219
   564
haftmann@24219
   565
fun retrieve_algebra thy operational =
wenzelm@26947
   566
  Sorts.subalgebra (Syntax.pp_global thy) operational
haftmann@25462
   567
    (weakest_constraints thy (Sign.classes_of thy))
haftmann@24219
   568
    (Sign.classes_of thy);
haftmann@24219
   569
haftmann@24219
   570
in
haftmann@24219
   571
haftmann@24219
   572
fun coregular_algebra thy = retrieve_algebra thy (K true) |> snd;
haftmann@24219
   573
fun operational_algebra thy =
haftmann@24219
   574
  let
haftmann@24219
   575
    fun add_iff_operational class =
wenzelm@24928
   576
      can (AxClass.get_info thy) class ? cons class;
haftmann@24219
   577
    val operational_classes = fold add_iff_operational (Sign.all_classes thy) []
haftmann@24219
   578
  in retrieve_algebra thy (member (op =) operational_classes) end;
haftmann@24219
   579
haftmann@24837
   580
val classparam_weakest_typ = gen_classparam_typ weakest_constraints;
haftmann@24837
   581
val classparam_strongest_typ = gen_classparam_typ strongest_constraints;
haftmann@24219
   582
haftmann@24219
   583
fun assert_func_typ thm =
haftmann@24219
   584
  let
haftmann@24219
   585
    val thy = Thm.theory_of_thm thm;
haftmann@24837
   586
    fun check_typ_classparam tyco (c, thm) =
haftmann@24219
   587
          let
haftmann@24423
   588
            val SOME class = AxClass.class_of_param thy c;
haftmann@26970
   589
            val (_, ty) = const_of thy thm;
haftmann@24837
   590
            val ty_decl = classparam_weakest_typ thy class (c, tyco);
haftmann@24837
   591
            val ty_strongest = classparam_strongest_typ thy class (c, tyco);
haftmann@24219
   592
            fun constrain thm = 
haftmann@24219
   593
              let
haftmann@24219
   594
                val max = Thm.maxidx_of thm + 1;
haftmann@24219
   595
                val ty_decl' = Logic.incr_tvar max ty_decl;
haftmann@26970
   596
                val (_, ty') = const_of thy thm;
haftmann@24219
   597
                val (env, _) = Sign.typ_unify thy (ty_decl', ty') (Vartab.empty, max);
haftmann@24219
   598
                val instT = Vartab.fold (fn (x_i, (sort, ty)) =>
haftmann@24219
   599
                  cons (Thm.ctyp_of thy (TVar (x_i, sort)), Thm.ctyp_of thy ty)) env [];
haftmann@24219
   600
              in Thm.instantiate (instT, []) thm end;
haftmann@24219
   601
          in if Sign.typ_instance thy (ty_strongest, ty)
haftmann@24219
   602
            then if Sign.typ_instance thy (ty, ty_decl)
haftmann@24219
   603
            then thm
haftmann@24219
   604
            else (warning ("Constraining type\n" ^ CodeUnit.string_of_typ thy ty
haftmann@24219
   605
              ^ "\nof defining equation\n"
wenzelm@26928
   606
              ^ Display.string_of_thm thm
haftmann@24219
   607
              ^ "\nto permitted most general type\n"
haftmann@24219
   608
              ^ CodeUnit.string_of_typ thy ty_decl);
haftmann@24219
   609
              constrain thm)
haftmann@24219
   610
            else CodeUnit.bad_thm ("Type\n" ^ CodeUnit.string_of_typ thy ty
haftmann@24219
   611
              ^ "\nof defining equation\n"
wenzelm@26928
   612
              ^ Display.string_of_thm thm
haftmann@24219
   613
              ^ "\nis incompatible with permitted least general type\n"
haftmann@24219
   614
              ^ CodeUnit.string_of_typ thy ty_strongest)
haftmann@24423
   615
          end;
haftmann@24423
   616
    fun check_typ_fun (c, thm) =
haftmann@24219
   617
      let
haftmann@26970
   618
        val (_, ty) = const_of thy thm;
haftmann@24219
   619
        val ty_decl = Sign.the_const_type thy c;
haftmann@24219
   620
      in if Sign.typ_equiv thy (Type.strip_sorts ty_decl, Type.strip_sorts ty)
haftmann@24219
   621
        then thm
haftmann@24219
   622
        else CodeUnit.bad_thm ("Type\n" ^ CodeUnit.string_of_typ thy ty
haftmann@24219
   623
           ^ "\nof defining equation\n"
wenzelm@26928
   624
           ^ Display.string_of_thm thm
haftmann@24219
   625
           ^ "\nis incompatible with declared function type\n"
haftmann@24219
   626
           ^ CodeUnit.string_of_typ thy ty_decl)
haftmann@24219
   627
      end;
haftmann@24423
   628
    fun check_typ (c, thm) =
haftmann@25597
   629
      case AxClass.inst_of_param thy c
haftmann@24837
   630
       of SOME (c, tyco) => check_typ_classparam tyco (c, thm)
haftmann@24423
   631
        | NONE => check_typ_fun (c, thm);
haftmann@24423
   632
  in check_typ (const_of_func thy thm, thm) end;
haftmann@24219
   633
haftmann@24283
   634
val mk_func = CodeUnit.error_thm (assert_func_typ o CodeUnit.mk_func);
haftmann@24624
   635
val mk_liberal_func = CodeUnit.warning_thm (assert_func_typ o CodeUnit.mk_func);
haftmann@24624
   636
val mk_default_func = CodeUnit.try_thm (assert_func_typ o CodeUnit.mk_func);
haftmann@24219
   637
haftmann@24219
   638
end;
haftmann@24219
   639
haftmann@24219
   640
haftmann@24219
   641
haftmann@24219
   642
(** interfaces and attributes **)
haftmann@24219
   643
haftmann@24624
   644
fun delete_force msg key xs =
haftmann@24624
   645
  if AList.defined (op =) xs key then AList.delete (op =) key xs
haftmann@24624
   646
  else error ("No such " ^ msg ^ ": " ^ quote key);
haftmann@24624
   647
haftmann@24423
   648
fun get_datatype thy tyco =
haftmann@24844
   649
  case Symtab.lookup ((the_dtyps o the_exec) thy) tyco
haftmann@24423
   650
   of SOME spec => spec
haftmann@24423
   651
    | NONE => Sign.arity_number thy tyco
wenzelm@24848
   652
        |> Name.invents Name.context Name.aT
haftmann@24423
   653
        |> map (rpair [])
haftmann@24423
   654
        |> rpair [];
haftmann@24423
   655
haftmann@24423
   656
fun get_datatype_of_constr thy c =
haftmann@24423
   657
  case (snd o strip_type o Sign.the_const_type thy) c
haftmann@24423
   658
   of Type (tyco, _) => if member (op =)
haftmann@24844
   659
       ((the_default [] o Option.map (map fst o snd) o Symtab.lookup ((the_dtyps o the_exec) thy)) tyco) c
haftmann@24423
   660
       then SOME tyco else NONE
haftmann@24423
   661
    | _ => NONE;
haftmann@24423
   662
haftmann@24423
   663
fun get_constr_typ thy c =
haftmann@24423
   664
  case get_datatype_of_constr thy c
haftmann@24423
   665
   of SOME tyco => let
haftmann@24423
   666
          val (vs, cos) = get_datatype thy tyco;
haftmann@24423
   667
          val SOME tys = AList.lookup (op =) cos c;
haftmann@24423
   668
          val ty = tys ---> Type (tyco, map TFree vs);
haftmann@24423
   669
        in SOME (Logic.varifyT ty) end
haftmann@24423
   670
    | NONE => NONE;
haftmann@24423
   671
haftmann@24844
   672
val get_case_data = Symtab.lookup o fst o the_cases o the_exec;
haftmann@24844
   673
haftmann@24844
   674
val is_undefined = Symtab.defined o snd o the_cases o the_exec;
haftmann@24844
   675
haftmann@24624
   676
fun add_func thm thy =
haftmann@24624
   677
  let
haftmann@24624
   678
    val func = mk_func thm;
haftmann@24624
   679
    val c = const_of_func thy func;
haftmann@24624
   680
    val _ = if (is_some o AxClass.class_of_param thy) c
haftmann@24624
   681
      then error ("Rejected polymorphic equation for overloaded constant:\n"
wenzelm@26928
   682
        ^ Display.string_of_thm thm)
haftmann@24624
   683
      else ();
haftmann@24624
   684
    val _ = if (is_some o get_datatype_of_constr thy) c
haftmann@24624
   685
      then error ("Rejected equation for datatype constructor:\n"
wenzelm@26928
   686
        ^ Display.string_of_thm func)
haftmann@24624
   687
      else ();
haftmann@24624
   688
  in
haftmann@24624
   689
    (map_exec_purge (SOME [c]) o map_funcs) (Symtab.map_default
haftmann@27609
   690
      (c, (Susp.value [], [])) (add_thm func)) thy
haftmann@24624
   691
  end;
haftmann@24219
   692
haftmann@24624
   693
fun add_liberal_func thm thy =
haftmann@24624
   694
  case mk_liberal_func thm
haftmann@24624
   695
   of SOME func => let
haftmann@24624
   696
          val c = const_of_func thy func
haftmann@24624
   697
        in if (is_some o AxClass.class_of_param thy) c
haftmann@24624
   698
          orelse (is_some o get_datatype_of_constr thy) c
haftmann@24624
   699
          then thy
haftmann@24624
   700
          else map_exec_purge (SOME [c]) (map_funcs
haftmann@24624
   701
            (Symtab.map_default
haftmann@27609
   702
              (c, (Susp.value [], [])) (add_thm func))) thy
haftmann@24624
   703
        end
haftmann@24624
   704
    | NONE => thy;
haftmann@24624
   705
haftmann@24624
   706
fun add_default_func thm thy =
haftmann@24624
   707
  case mk_default_func thm
haftmann@24624
   708
   of SOME func => let
haftmann@24624
   709
          val c = const_of_func thy func
haftmann@24624
   710
        in if (is_some o AxClass.class_of_param thy) c
haftmann@24624
   711
          orelse (is_some o get_datatype_of_constr thy) c
haftmann@24624
   712
          then thy
haftmann@24624
   713
          else map_exec_purge (SOME [c]) (map_funcs
haftmann@24624
   714
          (Symtab.map_default
haftmann@27609
   715
            (c, (Susp.value [], [])) (add_thm func))) thy
haftmann@24624
   716
        end
haftmann@24624
   717
    | NONE => thy;
haftmann@24219
   718
haftmann@24219
   719
fun del_func thm thy =
haftmann@24659
   720
  case mk_liberal_func thm
haftmann@24659
   721
   of SOME func => let
haftmann@24659
   722
          val c = const_of_func thy func;
haftmann@24659
   723
        in map_exec_purge (SOME [c]) (map_funcs
haftmann@27609
   724
          (Symtab.map_entry c (del_thm func))) thy
haftmann@24659
   725
        end
haftmann@24659
   726
    | NONE => thy;
haftmann@24219
   727
haftmann@26021
   728
fun del_funcs const = map_exec_purge (SOME [const])
haftmann@27609
   729
  (map_funcs (Symtab.map_entry const del_thms));
haftmann@26021
   730
haftmann@24219
   731
fun add_funcl (const, lthms) thy =
haftmann@24219
   732
  let
haftmann@24219
   733
    val lthms' = certificate thy (fn thy => certify_const thy const) lthms;
haftmann@24219
   734
      (*FIXME must check compatibility with sort algebra;
haftmann@24219
   735
        alas, naive checking results in non-termination!*)
haftmann@24219
   736
  in
haftmann@25312
   737
    map_exec_purge (SOME [const])
haftmann@27609
   738
      (map_funcs (Symtab.map_default (const, (Susp.value [], []))
haftmann@27609
   739
      (add_lthms lthms'))) thy
haftmann@24219
   740
  end;
haftmann@24219
   741
haftmann@24624
   742
val add_default_func_attr = Attrib.internal (fn _ => Thm.declaration_attribute
haftmann@24624
   743
  (fn thm => Context.mapping (add_default_func thm) I));
haftmann@24219
   744
haftmann@25485
   745
structure TypeInterpretation = InterpretationFun(type T = string * serial val eq = eq_snd (op =) : T * T -> bool);
haftmann@25462
   746
haftmann@24423
   747
fun add_datatype raw_cs thy =
haftmann@24219
   748
  let
haftmann@25597
   749
    val cs = map (fn c_ty as (_, ty) => (AxClass.unoverload_const thy c_ty, ty)) raw_cs;
haftmann@24423
   750
    val (tyco, vs_cos) = CodeUnit.constrset_of_consts thy cs;
haftmann@25462
   751
    val cs' = map fst (snd vs_cos);
haftmann@25462
   752
    val purge_cs = case Symtab.lookup ((the_dtyps o the_exec) thy) tyco
haftmann@25462
   753
     of SOME (vs, cos) => if null cos then NONE else SOME (cs' @ map fst cos)
haftmann@24423
   754
      | NONE => NONE;
haftmann@24219
   755
  in
haftmann@24219
   756
    thy
haftmann@25462
   757
    |> map_exec_purge purge_cs (map_dtyps (Symtab.update (tyco, vs_cos))
haftmann@24423
   758
        #> map_funcs (fold (Symtab.delete_safe o fst) cs))
haftmann@25485
   759
    |> TypeInterpretation.data (tyco, serial ())
haftmann@24219
   760
  end;
haftmann@24219
   761
haftmann@25485
   762
fun type_interpretation f =  TypeInterpretation.interpretation
haftmann@25485
   763
  (fn (tyco, _) => fn thy => f (tyco, get_datatype thy tyco) thy);
haftmann@25485
   764
haftmann@24423
   765
fun add_datatype_cmd raw_cs thy =
haftmann@24423
   766
  let
haftmann@24423
   767
    val cs = map (CodeUnit.read_bare_const thy) raw_cs;
haftmann@24423
   768
  in add_datatype cs thy end;
haftmann@24219
   769
haftmann@24844
   770
fun add_case thm thy =
haftmann@24844
   771
  let
haftmann@24844
   772
    val entry as (c, _) = CodeUnit.case_cert thm;
haftmann@24844
   773
  in
haftmann@24844
   774
    (map_exec_purge (SOME [c]) o map_cases o apfst) (Symtab.update entry) thy
haftmann@24844
   775
  end;
haftmann@24844
   776
haftmann@24844
   777
fun add_undefined c thy =
haftmann@24844
   778
  (map_exec_purge (SOME [c]) o map_cases o apsnd) (Symtab.update (c, ())) thy;
haftmann@24844
   779
haftmann@27557
   780
val map_pre = map_exec_purge NONE o map_thmproc o apfst o apfst;
haftmann@27557
   781
val map_post = map_exec_purge NONE o map_thmproc o apfst o apsnd;
haftmann@27557
   782
haftmann@27557
   783
fun add_inline thm thy = (map_pre o MetaSimplifier.add_simp)
haftmann@27557
   784
  (CodeUnit.error_thm CodeUnit.mk_rew thm) thy;
haftmann@27557
   785
    (*fully applied in order to get right context for mk_rew!*)
haftmann@27557
   786
haftmann@27557
   787
fun del_inline thm thy = (map_pre o MetaSimplifier.del_simp)
haftmann@27557
   788
  (CodeUnit.error_thm CodeUnit.mk_rew thm) thy;
haftmann@27557
   789
    (*fully applied in order to get right context for mk_rew!*)
haftmann@24219
   790
haftmann@27557
   791
fun add_post thm thy = (map_post o MetaSimplifier.add_simp)
haftmann@27557
   792
  (CodeUnit.error_thm CodeUnit.mk_rew thm) thy;
haftmann@27557
   793
    (*fully applied in order to get right context for mk_rew!*)
haftmann@24219
   794
haftmann@27557
   795
fun del_post thm thy = (map_post o MetaSimplifier.del_simp)
haftmann@27557
   796
  (CodeUnit.error_thm CodeUnit.mk_rew thm) thy;
haftmann@27557
   797
    (*fully applied in order to get right context for mk_rew!*)
haftmann@27557
   798
  
haftmann@27557
   799
fun add_functrans (name, f) =
haftmann@27557
   800
  (map_exec_purge NONE o map_thmproc o apsnd)
haftmann@24219
   801
    (AList.update (op =) (name, (serial (), f)));
haftmann@24219
   802
haftmann@27557
   803
fun del_functrans name =
haftmann@24219
   804
  (map_exec_purge NONE o map_thmproc o apsnd)
haftmann@27609
   805
    (delete_force "function transformer" name);
haftmann@24219
   806
wenzelm@26463
   807
val _ = Context.>> (Context.map_theory
haftmann@24219
   808
  (let
haftmann@24219
   809
    fun mk_attribute f = Thm.declaration_attribute (fn thm => Context.mapping (f thm) I);
haftmann@24219
   810
    fun add_simple_attribute (name, f) =
haftmann@24219
   811
      add_attribute (name, Scan.succeed (mk_attribute f));
haftmann@24219
   812
    fun add_del_attribute (name, (add, del)) =
haftmann@24219
   813
      add_attribute (name, Args.del |-- Scan.succeed (mk_attribute del)
haftmann@24219
   814
        || Scan.succeed (mk_attribute add))
haftmann@24219
   815
  in
haftmann@25462
   816
    TypeInterpretation.init
haftmann@25462
   817
    #> add_del_attribute ("func", (add_func, del_func))
haftmann@24219
   818
    #> add_del_attribute ("inline", (add_inline, del_inline))
haftmann@24219
   819
    #> add_del_attribute ("post", (add_post, del_post))
wenzelm@26463
   820
  end));
haftmann@24219
   821
haftmann@24219
   822
haftmann@24219
   823
(** post- and preprocessing **)
haftmann@24219
   824
haftmann@24219
   825
local
haftmann@24219
   826
haftmann@27609
   827
fun apply_functrans thy [] = []
haftmann@27609
   828
  | apply_functrans thy (thms as thm :: _) =
haftmann@24219
   829
      let
haftmann@24423
   830
        val const = const_of_func thy thm;
haftmann@27609
   831
        val functrans = (map (fn (_, (_, f)) => f thy) o #functrans
haftmann@27609
   832
          o the_thmproc o the_exec) thy;
haftmann@27609
   833
        val thms' = perhaps (perhaps_loop (perhaps_apply functrans)) thms;
haftmann@24219
   834
      in certify_const thy const thms' end;
haftmann@24219
   835
haftmann@24219
   836
fun rhs_conv conv thm =
haftmann@24219
   837
  let
haftmann@24219
   838
    val thm' = (conv o Thm.rhs_of) thm;
haftmann@24219
   839
  in Thm.transitive thm thm' end
haftmann@24219
   840
haftmann@24837
   841
fun term_of_conv thy f =
haftmann@24837
   842
  Thm.cterm_of thy
haftmann@24837
   843
  #> f
haftmann@24837
   844
  #> Thm.prop_of
haftmann@24837
   845
  #> Logic.dest_equals
haftmann@24837
   846
  #> snd;
haftmann@24837
   847
haftmann@24219
   848
in
haftmann@24219
   849
haftmann@24219
   850
fun preprocess thy thms =
haftmann@27582
   851
  let
haftmann@27582
   852
    val pre = (Simplifier.theory_context thy o #pre o the_thmproc o the_exec) thy;
haftmann@27582
   853
  in
haftmann@27582
   854
    thms
haftmann@27609
   855
    |> apply_functrans thy
haftmann@27582
   856
    |> map (CodeUnit.rewrite_func pre)
haftmann@27582
   857
    (*FIXME - must check gere: rewrite rule, defining equation, proper constant *)
haftmann@27582
   858
    |> map (AxClass.unoverload thy)
haftmann@27582
   859
    |> common_typ_funcs
haftmann@27582
   860
  end;
haftmann@26970
   861
haftmann@24219
   862
haftmann@24219
   863
fun preprocess_conv ct =
haftmann@24219
   864
  let
haftmann@24219
   865
    val thy = Thm.theory_of_cterm ct;
haftmann@27582
   866
    val pre = (Simplifier.theory_context thy o #pre o the_thmproc o the_exec) thy;
haftmann@24219
   867
  in
haftmann@24219
   868
    ct
haftmann@27582
   869
    |> Simplifier.rewrite pre
haftmann@25597
   870
    |> rhs_conv (AxClass.unoverload_conv thy)
haftmann@24219
   871
  end;
haftmann@24219
   872
haftmann@24837
   873
fun preprocess_term thy = term_of_conv thy preprocess_conv;
haftmann@24837
   874
haftmann@24219
   875
fun postprocess_conv ct =
haftmann@24219
   876
  let
haftmann@24219
   877
    val thy = Thm.theory_of_cterm ct;
haftmann@27582
   878
    val post = (Simplifier.theory_context thy o #post o the_thmproc o the_exec) thy;
haftmann@24219
   879
  in
haftmann@24219
   880
    ct
haftmann@25597
   881
    |> AxClass.overload_conv thy
haftmann@27582
   882
    |> rhs_conv (Simplifier.rewrite post)
haftmann@24219
   883
  end;
haftmann@24219
   884
haftmann@24837
   885
fun postprocess_term thy = term_of_conv thy postprocess_conv;
haftmann@24837
   886
haftmann@24219
   887
end; (*local*)
haftmann@24219
   888
haftmann@25597
   889
fun default_typ_proto thy c = case AxClass.inst_of_param thy c
haftmann@24837
   890
 of SOME (c, tyco) => classparam_weakest_typ thy ((the o AxClass.class_of_param thy) c)
haftmann@24423
   891
      (c, tyco) |> SOME
haftmann@24423
   892
  | NONE => (case AxClass.class_of_param thy c
haftmann@24423
   893
     of SOME class => SOME (Term.map_type_tvar
wenzelm@24848
   894
          (K (TVar ((Name.aT, 0), [class]))) (Sign.the_const_type thy c))
haftmann@24423
   895
      | NONE => get_constr_typ thy c);
haftmann@24219
   896
haftmann@24219
   897
local
haftmann@24219
   898
haftmann@24219
   899
fun get_funcs thy const =
haftmann@24844
   900
  Symtab.lookup ((the_funcs o the_exec) thy) const
haftmann@27609
   901
  |> Option.map (Susp.force o fst)
haftmann@24219
   902
  |> these
haftmann@24219
   903
  |> map (Thm.transfer thy);
haftmann@24219
   904
haftmann@24219
   905
in
haftmann@24219
   906
haftmann@24219
   907
fun these_funcs thy const =
haftmann@24219
   908
  let
haftmann@24219
   909
    fun drop_refl thy = filter_out (is_equal o Term.fast_term_ord o Logic.dest_equals
haftmann@24219
   910
      o ObjectLogic.drop_judgment thy o Thm.plain_prop_of);
haftmann@24219
   911
  in
haftmann@24219
   912
    get_funcs thy const
haftmann@24219
   913
    |> preprocess thy
haftmann@24219
   914
    |> drop_refl thy
haftmann@24219
   915
  end;
haftmann@24219
   916
haftmann@24423
   917
fun default_typ thy c = case default_typ_proto thy c
haftmann@26970
   918
 of SOME ty => CodeUnit.typscheme thy (c, ty)
haftmann@24423
   919
  | NONE => (case get_funcs thy c
haftmann@25597
   920
     of thm :: _ => snd (CodeUnit.head_func (AxClass.unoverload thy thm))
haftmann@26970
   921
      | [] => CodeUnit.typscheme thy (c, Sign.the_const_type thy c));
haftmann@24219
   922
haftmann@24219
   923
end; (*local*)
haftmann@24219
   924
haftmann@24219
   925
end; (*struct*)
haftmann@24219
   926
haftmann@24219
   927
haftmann@24219
   928
(** type-safe interfaces for data depedent on executable content **)
haftmann@24219
   929
haftmann@24219
   930
functor CodeDataFun(Data: CODE_DATA_ARGS): CODE_DATA =
haftmann@24219
   931
struct
haftmann@24219
   932
haftmann@24219
   933
type T = Data.T;
haftmann@24219
   934
exception Data of T;
haftmann@24219
   935
fun dest (Data x) = x
haftmann@24219
   936
haftmann@24219
   937
val kind = Code.declare_data (Data Data.empty)
haftmann@27609
   938
  (fn thy => fn cs => fn Data x => Data (Data.purge thy cs x));
haftmann@24219
   939
haftmann@24219
   940
val data_op = (kind, Data, dest);
haftmann@24219
   941
haftmann@24219
   942
val get = Code.get_data data_op;
haftmann@24219
   943
val change = Code.change_data data_op;
haftmann@24219
   944
fun change_yield thy = Code.change_yield_data data_op thy;
haftmann@24219
   945
haftmann@24219
   946
end;
haftmann@24219
   947
haftmann@24219
   948
structure Code : CODE =
haftmann@24219
   949
struct
haftmann@24219
   950
haftmann@24219
   951
open Code;
haftmann@24219
   952
haftmann@24219
   953
end;