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