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