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