src/Pure/Isar/code.ML
author haftmann
Mon May 11 10:53:19 2009 +0200 (2009-05-11 ago)
changeset 31090 3be41b271023
parent 31088 36a011423fcc
child 31091 8316d22f10f5
permissions -rw-r--r--
clarified matter of "proper" flag in code equations
haftmann@24219
     1
(*  Title:      Pure/Isar/code.ML
haftmann@24219
     2
    Author:     Florian Haftmann, TU Muenchen
haftmann@24219
     3
haftmann@24219
     4
Abstract executable content of theory.  Management of data dependent on
haftmann@25485
     5
executable content.  Cache assumes non-concurrent processing of a single theory.
haftmann@24219
     6
*)
haftmann@24219
     7
haftmann@24219
     8
signature CODE =
haftmann@24219
     9
sig
haftmann@28368
    10
  val add_eqn: thm -> theory -> theory
haftmann@31088
    11
  val add_nbe_eqn: thm -> theory -> theory
haftmann@28368
    12
  val add_default_eqn: thm -> theory -> theory
haftmann@28703
    13
  val add_default_eqn_attribute: attribute
haftmann@28703
    14
  val add_default_eqn_attrib: Attrib.src
haftmann@28368
    15
  val del_eqn: thm -> theory -> theory
haftmann@28368
    16
  val del_eqns: string -> theory -> theory
wenzelm@28971
    17
  val add_eqnl: string * (thm * bool) list lazy -> theory -> theory
wenzelm@29302
    18
  val map_pre: (simpset -> simpset) -> theory -> theory
wenzelm@29302
    19
  val map_post: (simpset -> simpset) -> theory -> theory
haftmann@24219
    20
  val add_inline: thm -> theory -> theory
haftmann@28423
    21
  val add_functrans: string * (theory -> (thm * bool) list -> (thm * bool) list option) -> theory -> theory
haftmann@27557
    22
  val del_functrans: string -> theory -> theory
haftmann@31090
    23
  val simple_functrans: (theory -> thm list -> thm list option)
haftmann@31090
    24
    -> theory -> (thm * bool) list -> (thm * bool) list option
haftmann@24423
    25
  val add_datatype: (string * typ) list -> theory -> theory
haftmann@24423
    26
  val add_datatype_cmd: string list -> theory -> theory
haftmann@25485
    27
  val type_interpretation:
haftmann@25485
    28
    (string * ((string * sort) list * (string * typ list) list)
haftmann@25485
    29
      -> theory -> theory) -> theory -> theory
haftmann@24844
    30
  val add_case: thm -> theory -> theory
haftmann@24844
    31
  val add_undefined: string -> theory -> theory
haftmann@27675
    32
  val purge_data: theory -> theory
haftmann@24219
    33
haftmann@28368
    34
  val these_eqns: theory -> string -> (thm * bool) list
haftmann@28525
    35
  val these_raw_eqns: theory -> string -> (thm * bool) list
haftmann@24219
    36
  val get_datatype: theory -> string -> ((string * sort) list * (string * typ list) list)
haftmann@24423
    37
  val get_datatype_of_constr: theory -> string -> string option
haftmann@30023
    38
  val get_case_scheme: theory -> string -> (int * (int * string list)) option
haftmann@24844
    39
  val is_undefined: theory -> string -> bool
haftmann@28423
    40
  val default_typscheme: theory -> string -> (string * sort) list * typ
haftmann@24219
    41
haftmann@28423
    42
  val preprocess_conv: theory -> cterm -> thm
haftmann@24837
    43
  val preprocess_term: theory -> term -> term
haftmann@28423
    44
  val postprocess_conv: theory -> cterm -> thm
haftmann@24837
    45
  val postprocess_term: theory -> term -> term
haftmann@24219
    46
wenzelm@30513
    47
  val add_attribute: string * attribute parser -> theory -> theory
haftmann@24219
    48
haftmann@24219
    49
  val print_codesetup: theory -> unit
haftmann@24219
    50
end;
haftmann@24219
    51
haftmann@24219
    52
signature CODE_DATA_ARGS =
haftmann@24219
    53
sig
haftmann@24219
    54
  type T
haftmann@24219
    55
  val empty: T
haftmann@27609
    56
  val purge: theory -> string list -> T -> T
haftmann@24219
    57
end;
haftmann@24219
    58
haftmann@24219
    59
signature CODE_DATA =
haftmann@24219
    60
sig
haftmann@24219
    61
  type T
haftmann@24219
    62
  val get: theory -> T
haftmann@24219
    63
  val change: theory -> (T -> T) -> T
haftmann@24219
    64
  val change_yield: theory -> (T -> 'a * T) -> 'a * T
haftmann@24219
    65
end;
haftmann@24219
    66
haftmann@24219
    67
signature PRIVATE_CODE =
haftmann@24219
    68
sig
haftmann@24219
    69
  include CODE
haftmann@27609
    70
  val declare_data: Object.T -> (theory -> string list -> Object.T -> Object.T)
haftmann@27609
    71
    -> serial
haftmann@24219
    72
  val get_data: serial * ('a -> Object.T) * (Object.T -> 'a)
haftmann@24219
    73
    -> theory -> 'a
haftmann@24219
    74
  val change_data: serial * ('a -> Object.T) * (Object.T -> 'a)
haftmann@24219
    75
    -> theory -> ('a -> 'a) -> 'a
haftmann@24219
    76
  val change_yield_data: serial * ('a -> Object.T) * (Object.T -> 'a)
haftmann@24219
    77
    -> theory -> ('a -> 'b * 'a) -> 'b * 'a
haftmann@24219
    78
end;
haftmann@24219
    79
haftmann@24219
    80
structure Code : PRIVATE_CODE =
haftmann@24219
    81
struct
haftmann@24219
    82
haftmann@25312
    83
(** code attributes **)
haftmann@25312
    84
haftmann@25312
    85
structure CodeAttr = TheoryDataFun (
wenzelm@30513
    86
  type T = (string * attribute parser) list;
haftmann@25312
    87
  val empty = [];
haftmann@25312
    88
  val copy = I;
haftmann@25312
    89
  val extend = I;
haftmann@26970
    90
  fun merge _ = AList.merge (op = : string * string -> bool) (K true);
haftmann@25312
    91
);
haftmann@24219
    92
haftmann@25312
    93
fun add_attribute (attr as (name, _)) =
haftmann@25312
    94
  let
haftmann@28562
    95
    fun add_parser ("", parser) attrs = attrs |> rev |> AList.update (op =) ("", parser) |> rev
haftmann@25312
    96
      | add_parser (name, parser) attrs = (name, Args.$$$ name |-- parser) :: attrs;
haftmann@28562
    97
  in CodeAttr.map (fn attrs => if not (name = "") andalso AList.defined (op =) attrs name
haftmann@28562
    98
    then error ("Code attribute " ^ name ^ " already declared") else add_parser attr attrs)
haftmann@25312
    99
  end;
haftmann@25312
   100
wenzelm@30722
   101
val _ = Context.>> (Context.map_theory
wenzelm@30722
   102
  (Attrib.setup (Binding.name "code")
wenzelm@30722
   103
    (Scan.peek (fn context =>
wenzelm@30722
   104
      List.foldr op || Scan.fail (map snd (CodeAttr.get (Context.theory_of context)))))
wenzelm@30722
   105
    "declare theorems for code generation"));
wenzelm@30722
   106
haftmann@25312
   107
haftmann@25312
   108
haftmann@28143
   109
(** logical and syntactical specification of executable code **)
haftmann@24219
   110
haftmann@30023
   111
(* code equations *)
haftmann@28695
   112
wenzelm@28971
   113
type eqns = bool * (thm * bool) list lazy;
haftmann@31088
   114
  (*default flag, theorems with proper flag (perhaps lazy)*)
haftmann@24219
   115
wenzelm@28673
   116
fun pretty_lthms ctxt r = case Lazy.peek r
wenzelm@28672
   117
 of SOME thms => map (ProofContext.pretty_thm ctxt o fst) (Exn.release thms)
haftmann@24219
   118
  | NONE => [Pretty.str "[...]"];
haftmann@24219
   119
haftmann@24219
   120
fun certificate thy f r =
wenzelm@28673
   121
  case Lazy.peek r
wenzelm@28673
   122
   of SOME thms => (Lazy.value o f thy) (Exn.release thms)
haftmann@24844
   123
    | NONE => let
haftmann@24844
   124
        val thy_ref = Theory.check_thy thy;
wenzelm@28673
   125
      in Lazy.lazy (fn () => (f (Theory.deref thy_ref) o Lazy.force) r) end;
haftmann@24219
   126
haftmann@31088
   127
fun add_drop_redundant thy (thm, proper) thms =
haftmann@24219
   128
  let
haftmann@24219
   129
    val args_of = snd o strip_comb o fst o Logic.dest_equals o Thm.plain_prop_of;
haftmann@24219
   130
    val args = args_of thm;
haftmann@28403
   131
    val incr_idx = Logic.incr_indexes ([], Thm.maxidx_of thm + 1);
haftmann@28350
   132
    fun matches_args args' = length args <= length args' andalso
haftmann@28403
   133
      Pattern.matchess thy (args, (map incr_idx o curry Library.take (length args)) args');
haftmann@31088
   134
    fun drop (thm', proper') = if (proper orelse not proper')
haftmann@28359
   135
      andalso matches_args (args_of thm') then 
haftmann@30023
   136
        (warning ("Code generator: dropping redundant code equation\n" ^ Display.string_of_thm thm'); true)
haftmann@28350
   137
      else false;
haftmann@31088
   138
  in (thm, proper) :: filter_out drop thms end;
haftmann@24219
   139
wenzelm@28673
   140
fun add_thm thy _ thm (false, thms) = (false, Lazy.map_force (add_drop_redundant thy thm) thms)
wenzelm@28673
   141
  | add_thm thy true thm (true, thms) = (true, Lazy.map_force (fn thms => thms @ [thm]) thms)
wenzelm@28673
   142
  | add_thm thy false thm (true, thms) = (false, Lazy.value [thm]);
haftmann@24219
   143
haftmann@28143
   144
fun add_lthms lthms _ = (false, lthms);
haftmann@25312
   145
wenzelm@28673
   146
fun del_thm thm = (apsnd o Lazy.map_force) (remove (eq_fst Thm.eq_thm_prop) (thm, true));
haftmann@25312
   147
haftmann@28143
   148
haftmann@28143
   149
(* specification data *)
haftmann@28143
   150
haftmann@24219
   151
datatype spec = Spec of {
haftmann@28695
   152
  concluded_history: bool,
haftmann@28695
   153
  eqns: ((bool * eqns) * (serial * eqns) list) Symtab.table
haftmann@28695
   154
    (*with explicit history*),
haftmann@28695
   155
  dtyps: ((serial * ((string * sort) list * (string * typ list) list)) list) Symtab.table
haftmann@28695
   156
    (*with explicit history*),
haftmann@30076
   157
  cases: (int * (int * string list)) Symtab.table * unit Symtab.table
haftmann@24219
   158
};
haftmann@24219
   159
haftmann@28695
   160
fun mk_spec ((concluded_history, eqns), (dtyps, cases)) =
haftmann@28695
   161
  Spec { concluded_history = concluded_history, eqns = eqns, dtyps = dtyps, cases = cases };
haftmann@28695
   162
fun map_spec f (Spec { concluded_history = concluded_history, eqns = eqns,
haftmann@28695
   163
  dtyps = dtyps, cases = cases }) =
haftmann@28695
   164
  mk_spec (f ((concluded_history, eqns), (dtyps, cases)));
haftmann@28695
   165
fun merge_spec (Spec { concluded_history = _, eqns = eqns1, dtyps = dtyps1, cases = (cases1, undefs1) },
haftmann@28695
   166
  Spec { concluded_history = _, eqns = eqns2, dtyps = dtyps2, cases = (cases2, undefs2) }) =
haftmann@24219
   167
  let
haftmann@28695
   168
    fun merge_eqns ((_, history1), (_, history2)) =
haftmann@28695
   169
      let
haftmann@28695
   170
        val raw_history = AList.merge (op =) (K true) (history1, history2)
haftmann@28695
   171
        val filtered_history = filter_out (fst o snd) raw_history
haftmann@28695
   172
        val history = if null filtered_history
haftmann@28695
   173
          then raw_history else filtered_history;
haftmann@28695
   174
      in ((false, (snd o hd) history), history) end;
haftmann@28695
   175
    val eqns = Symtab.join (K merge_eqns) (eqns1, eqns2);
haftmann@28695
   176
    val dtyps = Symtab.join (K (AList.merge (op =) (K true))) (dtyps1, dtyps2);
haftmann@27609
   177
    val cases = (Symtab.merge (K true) (cases1, cases2),
haftmann@27609
   178
      Symtab.merge (K true) (undefs1, undefs2));
haftmann@28695
   179
  in mk_spec ((false, eqns), (dtyps, cases)) end;
haftmann@24219
   180
haftmann@25312
   181
haftmann@25312
   182
(* pre- and postprocessor *)
haftmann@25312
   183
haftmann@25312
   184
datatype thmproc = Thmproc of {
wenzelm@29302
   185
  pre: simpset,
wenzelm@29302
   186
  post: simpset,
haftmann@28423
   187
  functrans: (string * (serial * (theory -> (thm * bool) list -> (thm * bool) list option))) list
haftmann@25312
   188
};
haftmann@25312
   189
haftmann@27557
   190
fun mk_thmproc ((pre, post), functrans) =
haftmann@27557
   191
  Thmproc { pre = pre, post = post, functrans = functrans };
haftmann@27557
   192
fun map_thmproc f (Thmproc { pre, post, functrans }) =
haftmann@27557
   193
  mk_thmproc (f ((pre, post), functrans));
haftmann@27557
   194
fun merge_thmproc (Thmproc { pre = pre1, post = post1, functrans = functrans1 },
haftmann@27557
   195
  Thmproc { pre = pre2, post = post2, functrans = functrans2 }) =
haftmann@25312
   196
    let
wenzelm@29302
   197
      val pre = Simplifier.merge_ss (pre1, pre2);
wenzelm@29302
   198
      val post = Simplifier.merge_ss (post1, post2);
haftmann@27557
   199
      val functrans = AList.merge (op =) (eq_fst (op =)) (functrans1, functrans2);
haftmann@27557
   200
    in mk_thmproc ((pre, post), functrans) end;
haftmann@25312
   201
haftmann@24219
   202
datatype exec = Exec of {
haftmann@24219
   203
  thmproc: thmproc,
haftmann@24219
   204
  spec: spec
haftmann@24219
   205
};
haftmann@24219
   206
haftmann@28143
   207
haftmann@28143
   208
(* code setup data *)
haftmann@28143
   209
haftmann@24219
   210
fun mk_exec (thmproc, spec) =
haftmann@24219
   211
  Exec { thmproc = thmproc, spec = spec };
haftmann@24219
   212
fun map_exec f (Exec { thmproc = thmproc, spec = spec }) =
haftmann@24219
   213
  mk_exec (f (thmproc, spec));
haftmann@27557
   214
fun merge_exec (Exec { thmproc = thmproc1, spec = spec1 },
haftmann@24219
   215
  Exec { thmproc = thmproc2, spec = spec2 }) =
haftmann@24219
   216
  let
haftmann@27557
   217
    val thmproc = merge_thmproc (thmproc1, thmproc2);
haftmann@27557
   218
    val spec = merge_spec (spec1, spec2);
haftmann@27557
   219
  in mk_exec (thmproc, spec) end;
wenzelm@29302
   220
val empty_exec = mk_exec (mk_thmproc ((Simplifier.empty_ss, Simplifier.empty_ss), []),
haftmann@28695
   221
  mk_spec ((false, Symtab.empty), (Symtab.empty, (Symtab.empty, Symtab.empty))));
haftmann@24219
   222
haftmann@25312
   223
fun the_thmproc (Exec { thmproc = Thmproc x, ...}) = x;
haftmann@24219
   224
fun the_spec (Exec { spec = Spec x, ...}) = x;
haftmann@28368
   225
val the_eqns = #eqns o the_spec;
haftmann@24219
   226
val the_dtyps = #dtyps o the_spec;
haftmann@24844
   227
val the_cases = #cases o the_spec;
haftmann@24219
   228
val map_thmproc = map_exec o apfst o map_thmproc;
haftmann@28695
   229
val map_concluded_history = map_exec o apsnd o map_spec o apfst o apfst;
haftmann@28695
   230
val map_eqns = map_exec o apsnd o map_spec o apfst o apsnd;
haftmann@24844
   231
val map_dtyps = map_exec o apsnd o map_spec o apsnd o apfst;
haftmann@24844
   232
val map_cases = map_exec o apsnd o map_spec o apsnd o apsnd;
haftmann@24219
   233
haftmann@24219
   234
haftmann@24219
   235
(* data slots dependent on executable content *)
haftmann@24219
   236
haftmann@24219
   237
(*private copy avoids potential conflict of table exceptions*)
haftmann@24219
   238
structure Datatab = TableFun(type key = int val ord = int_ord);
haftmann@24219
   239
haftmann@24219
   240
local
haftmann@24219
   241
haftmann@24219
   242
type kind = {
haftmann@24219
   243
  empty: Object.T,
haftmann@27609
   244
  purge: theory -> string list -> Object.T -> Object.T
haftmann@24219
   245
};
haftmann@24219
   246
haftmann@24219
   247
val kinds = ref (Datatab.empty: kind Datatab.table);
haftmann@24219
   248
val kind_keys = ref ([]: serial list);
haftmann@24219
   249
haftmann@24219
   250
fun invoke f k = case Datatab.lookup (! kinds) k
haftmann@24219
   251
 of SOME kind => f kind
haftmann@24219
   252
  | NONE => sys_error "Invalid code data identifier";
haftmann@24219
   253
haftmann@24219
   254
in
haftmann@24219
   255
haftmann@27609
   256
fun declare_data empty purge =
haftmann@24219
   257
  let
haftmann@24219
   258
    val k = serial ();
haftmann@27609
   259
    val kind = {empty = empty, purge = purge};
haftmann@24219
   260
    val _ = change kinds (Datatab.update (k, kind));
haftmann@24219
   261
    val _ = change kind_keys (cons k);
haftmann@24219
   262
  in k end;
haftmann@24219
   263
haftmann@27609
   264
fun invoke_init k = invoke (fn kind => #empty kind) k;
haftmann@24219
   265
haftmann@27609
   266
fun invoke_purge_all thy cs =
haftmann@24219
   267
  fold (fn k => Datatab.map_entry k
haftmann@27609
   268
    (invoke (fn kind => #purge kind thy cs) k)) (! kind_keys);
haftmann@24219
   269
haftmann@24219
   270
end; (*local*)
haftmann@24219
   271
haftmann@24219
   272
haftmann@25312
   273
(** theory store **)
haftmann@24219
   274
haftmann@24219
   275
local
haftmann@24219
   276
haftmann@24219
   277
type data = Object.T Datatab.table;
haftmann@27609
   278
val empty_data = Datatab.empty : data;
haftmann@24219
   279
haftmann@24219
   280
structure CodeData = TheoryDataFun
haftmann@24219
   281
(
haftmann@24219
   282
  type T = exec * data ref;
haftmann@27609
   283
  val empty = (empty_exec, ref empty_data);
haftmann@24219
   284
  fun copy (exec, data) = (exec, ref (! data));
haftmann@24219
   285
  val extend = copy;
haftmann@24219
   286
  fun merge pp ((exec1, data1), (exec2, data2)) =
haftmann@27609
   287
    (merge_exec (exec1, exec2), ref empty_data);
haftmann@24219
   288
);
haftmann@24219
   289
haftmann@24219
   290
fun thy_data f thy = f ((snd o CodeData.get) thy);
haftmann@24219
   291
haftmann@24219
   292
fun get_ensure_init kind data_ref =
haftmann@24219
   293
  case Datatab.lookup (! data_ref) kind
haftmann@24219
   294
   of SOME x => x
haftmann@27609
   295
    | NONE => let val y = invoke_init kind
haftmann@24219
   296
        in (change data_ref (Datatab.update (kind, y)); y) end;
haftmann@24219
   297
haftmann@24219
   298
in
haftmann@24219
   299
haftmann@24219
   300
(* access to executable content *)
haftmann@24219
   301
haftmann@24844
   302
val the_exec = fst o CodeData.get;
haftmann@24219
   303
haftmann@27983
   304
fun complete_class_params thy cs =
haftmann@27983
   305
  fold (fn c => case AxClass.inst_of_param thy c
haftmann@27983
   306
   of NONE => insert (op =) c
haftmann@27983
   307
    | SOME (c', _) => insert (op =) c' #> insert (op =) c) cs [];
haftmann@27983
   308
haftmann@24219
   309
fun map_exec_purge touched f thy =
haftmann@27609
   310
  CodeData.map (fn (exec, data) => (f exec, ref (case touched
haftmann@27983
   311
   of SOME cs => invoke_purge_all thy (complete_class_params thy cs) (! data)
haftmann@27609
   312
    | NONE => empty_data))) thy;
haftmann@24219
   313
haftmann@27675
   314
val purge_data = (CodeData.map o apsnd) (K (ref empty_data));
haftmann@27675
   315
haftmann@24219
   316
haftmann@28695
   317
(* tackling equation history *)
haftmann@28695
   318
haftmann@28695
   319
fun get_eqns thy c =
haftmann@28695
   320
  Symtab.lookup ((the_eqns o the_exec) thy) c
haftmann@28695
   321
  |> Option.map (Lazy.force o snd o snd o fst)
haftmann@28695
   322
  |> these;
haftmann@28695
   323
haftmann@28695
   324
fun continue_history thy = if (#concluded_history o the_spec o the_exec) thy
haftmann@28695
   325
  then thy
haftmann@28695
   326
    |> (CodeData.map o apfst o map_concluded_history) (K false)
haftmann@28695
   327
    |> SOME
haftmann@28695
   328
  else NONE;
haftmann@28695
   329
haftmann@28695
   330
fun conclude_history thy = if (#concluded_history o the_spec o the_exec) thy
haftmann@28695
   331
  then NONE
haftmann@28695
   332
  else thy
haftmann@28695
   333
    |> (CodeData.map o apfst)
haftmann@28695
   334
        ((map_eqns o Symtab.map) (fn ((changed, current), history) =>
haftmann@28695
   335
          ((false, current),
haftmann@28695
   336
            if changed then (serial (), current) :: history else history))
haftmann@28695
   337
        #> map_concluded_history (K true))
haftmann@28695
   338
    |> SOME;
haftmann@28695
   339
haftmann@28695
   340
val _ = Context.>> (Context.map_theory (CodeData.init
haftmann@28695
   341
  #> Theory.at_begin continue_history
haftmann@28695
   342
  #> Theory.at_end conclude_history));
haftmann@28695
   343
haftmann@28695
   344
haftmann@24219
   345
(* access to data dependent on abstract executable content *)
haftmann@24219
   346
haftmann@24219
   347
fun get_data (kind, _, dest) = thy_data (get_ensure_init kind #> dest);
haftmann@24219
   348
haftmann@24219
   349
fun change_data (kind, mk, dest) =
haftmann@24219
   350
  let
haftmann@24219
   351
    fun chnge data_ref f =
haftmann@24219
   352
      let
haftmann@24219
   353
        val data = get_ensure_init kind data_ref;
haftmann@24219
   354
        val data' = f (dest data);
haftmann@24219
   355
      in (change data_ref (Datatab.update (kind, mk data')); data') end;
haftmann@24219
   356
  in thy_data chnge end;
haftmann@24219
   357
haftmann@24219
   358
fun change_yield_data (kind, mk, dest) =
haftmann@24219
   359
  let
haftmann@24219
   360
    fun chnge data_ref f =
haftmann@24219
   361
      let
haftmann@24219
   362
        val data = get_ensure_init kind data_ref;
haftmann@24219
   363
        val (x, data') = f (dest data);
haftmann@24219
   364
      in (x, (change data_ref (Datatab.update (kind, mk data')); data')) end;
haftmann@24219
   365
  in thy_data chnge end;
haftmann@24219
   366
haftmann@24219
   367
end; (*local*)
haftmann@24219
   368
haftmann@24219
   369
haftmann@24219
   370
(* print executable content *)
haftmann@24219
   371
haftmann@24219
   372
fun print_codesetup thy =
haftmann@24219
   373
  let
haftmann@24219
   374
    val ctxt = ProofContext.init thy;
haftmann@24844
   375
    val exec = the_exec thy;
haftmann@28368
   376
    fun pretty_eqn (s, (_, lthms)) =
haftmann@24219
   377
      (Pretty.block o Pretty.fbreaks) (
haftmann@28143
   378
        Pretty.str s :: pretty_lthms ctxt lthms
haftmann@24219
   379
      );
haftmann@24219
   380
    fun pretty_dtyp (s, []) =
haftmann@24219
   381
          Pretty.str s
haftmann@24219
   382
      | pretty_dtyp (s, cos) =
haftmann@24219
   383
          (Pretty.block o Pretty.breaks) (
haftmann@24219
   384
            Pretty.str s
haftmann@24219
   385
            :: Pretty.str "="
haftmann@28403
   386
            :: separate (Pretty.str "|") (map (fn (c, []) => Pretty.str (Code_Unit.string_of_const thy c)
haftmann@24219
   387
                 | (c, tys) =>
haftmann@24219
   388
                     (Pretty.block o Pretty.breaks)
haftmann@28054
   389
                        (Pretty.str (Code_Unit.string_of_const thy c)
wenzelm@26947
   390
                          :: Pretty.str "of"
wenzelm@26947
   391
                          :: map (Pretty.quote o Syntax.pretty_typ_global thy) tys)) cos)
haftmann@24219
   392
          );
haftmann@27557
   393
    val pre = (#pre o the_thmproc) exec;
haftmann@27557
   394
    val post = (#post o the_thmproc) exec;
haftmann@27557
   395
    val functrans = (map fst o #functrans o the_thmproc) exec;
haftmann@28368
   396
    val eqns = the_eqns exec
haftmann@24423
   397
      |> Symtab.dest
haftmann@28054
   398
      |> (map o apfst) (Code_Unit.string_of_const thy)
haftmann@28695
   399
      |> (map o apsnd) (snd o fst)
haftmann@24219
   400
      |> sort (string_ord o pairself fst);
haftmann@24219
   401
    val dtyps = the_dtyps exec
haftmann@24219
   402
      |> Symtab.dest
haftmann@28695
   403
      |> map (fn (dtco, (_, (vs, cos)) :: _) =>
wenzelm@26947
   404
          (Syntax.string_of_typ_global thy (Type (dtco, map TFree vs)), cos))
haftmann@24219
   405
      |> sort (string_ord o pairself fst)
haftmann@24219
   406
  in
haftmann@24219
   407
    (Pretty.writeln o Pretty.chunks) [
haftmann@24219
   408
      Pretty.block (
haftmann@30023
   409
        Pretty.str "code equations:"
haftmann@24219
   410
        :: Pretty.fbrk
haftmann@28368
   411
        :: (Pretty.fbreaks o map pretty_eqn) eqns
haftmann@24219
   412
      ),
haftmann@27557
   413
      Pretty.block [
haftmann@27557
   414
        Pretty.str "preprocessing simpset:",
haftmann@27557
   415
        Pretty.fbrk,
wenzelm@30357
   416
        Simplifier.pretty_ss ctxt pre
haftmann@27557
   417
      ],
haftmann@27557
   418
      Pretty.block [
haftmann@27557
   419
        Pretty.str "postprocessing simpset:",
haftmann@27557
   420
        Pretty.fbrk,
wenzelm@30357
   421
        Simplifier.pretty_ss ctxt post
haftmann@27557
   422
      ],
haftmann@24219
   423
      Pretty.block (
haftmann@27609
   424
        Pretty.str "function transformers:"
haftmann@24219
   425
        :: Pretty.fbrk
haftmann@27557
   426
        :: (Pretty.fbreaks o map Pretty.str) functrans
haftmann@25968
   427
      ),
haftmann@25968
   428
      Pretty.block (
haftmann@24219
   429
        Pretty.str "datatypes:"
haftmann@24219
   430
        :: Pretty.fbrk
haftmann@24219
   431
        :: (Pretty.fbreaks o map pretty_dtyp) dtyps
haftmann@24219
   432
      )
haftmann@24219
   433
    ]
haftmann@24219
   434
  end;
haftmann@24219
   435
haftmann@24219
   436
haftmann@24219
   437
(** theorem transformation and certification **)
haftmann@24219
   438
haftmann@28423
   439
fun common_typ_eqns thy [] = []
haftmann@28423
   440
  | common_typ_eqns thy [thm] = [thm]
haftmann@28423
   441
  | common_typ_eqns thy (thms as thm :: _) = (*FIXME is too general*)
haftmann@24219
   442
      let
haftmann@24219
   443
        fun incr_thm thm max =
haftmann@24219
   444
          let
haftmann@24219
   445
            val thm' = incr_indexes max thm;
haftmann@24219
   446
            val max' = Thm.maxidx_of thm' + 1;
haftmann@24219
   447
          in (thm', max') end;
haftmann@24219
   448
        val (thms', maxidx) = fold_map incr_thm thms 0;
haftmann@28423
   449
        val ty1 :: tys = map (snd o Code_Unit.const_typ_eqn) thms';
haftmann@24219
   450
        fun unify ty env = Sign.typ_unify thy (ty1, ty) env
haftmann@24219
   451
          handle Type.TUNIFY =>
haftmann@30023
   452
            error ("Type unificaton failed, while unifying code equations\n"
haftmann@24219
   453
            ^ (cat_lines o map Display.string_of_thm) thms
haftmann@24219
   454
            ^ "\nwith types\n"
haftmann@28054
   455
            ^ (cat_lines o map (Code_Unit.string_of_typ thy)) (ty1 :: tys));
haftmann@24219
   456
        val (env, _) = fold unify tys (Vartab.empty, maxidx)
haftmann@24219
   457
        val instT = Vartab.fold (fn (x_i, (sort, ty)) =>
haftmann@24219
   458
          cons (Thm.ctyp_of thy (TVar (x_i, sort)), Thm.ctyp_of thy ty)) env [];
haftmann@24219
   459
      in map (Thm.instantiate (instT, [])) thms' end;
haftmann@24219
   460
haftmann@24219
   461
haftmann@24219
   462
(** interfaces and attributes **)
haftmann@24219
   463
haftmann@24624
   464
fun delete_force msg key xs =
haftmann@24624
   465
  if AList.defined (op =) xs key then AList.delete (op =) key xs
haftmann@24624
   466
  else error ("No such " ^ msg ^ ": " ^ quote key);
haftmann@24624
   467
haftmann@24423
   468
fun get_datatype thy tyco =
haftmann@28695
   469
  case these (Symtab.lookup ((the_dtyps o the_exec) thy) tyco)
haftmann@28695
   470
   of (_, spec) :: _ => spec
haftmann@28695
   471
    | [] => Sign.arity_number thy tyco
wenzelm@24848
   472
        |> Name.invents Name.context Name.aT
haftmann@24423
   473
        |> map (rpair [])
haftmann@24423
   474
        |> rpair [];
haftmann@24423
   475
haftmann@24423
   476
fun get_datatype_of_constr thy c =
haftmann@24423
   477
  case (snd o strip_type o Sign.the_const_type thy) c
haftmann@28695
   478
   of Type (tyco, _) => if member (op =) ((map fst o snd o get_datatype thy) tyco) c
haftmann@24423
   479
       then SOME tyco else NONE
haftmann@24423
   480
    | _ => NONE;
haftmann@24423
   481
haftmann@31088
   482
fun is_constr thy = is_some o get_datatype_of_constr thy;
haftmann@31088
   483
haftmann@31090
   484
fun assert_eqn thy = Code_Unit.assert_eqn thy (is_constr thy);
haftmann@28708
   485
haftmann@31090
   486
fun assert_eqns_const thy c eqns =
haftmann@28708
   487
  let
haftmann@31088
   488
    fun cert (eqn as (thm, _)) = if c = Code_Unit.const_eqn thy thm
haftmann@30023
   489
      then eqn else error ("Wrong head of code equation,\nexpected constant "
haftmann@28708
   490
        ^ Code_Unit.string_of_const thy c ^ "\n" ^ Display.string_of_thm thm)
haftmann@31090
   491
  in map (cert o assert_eqn thy) eqns end;
haftmann@28708
   492
haftmann@28695
   493
fun change_eqns delete c f = (map_exec_purge (SOME [c]) o map_eqns
haftmann@28695
   494
  o (if delete then Symtab.map_entry c else Symtab.map_default (c, ((false, (true, Lazy.value [])), [])))
haftmann@28695
   495
    o apfst) (fn (_, eqns) => (true, f eqns));
haftmann@24844
   496
haftmann@31090
   497
fun gen_add_eqn default (eqn as (thm, _)) thy =
haftmann@31090
   498
  let val c = Code_Unit.const_eqn thy thm
haftmann@31090
   499
  in change_eqns false c (add_thm thy default eqn) thy end;
haftmann@31090
   500
haftmann@31090
   501
fun add_eqn thm thy =
haftmann@31090
   502
  gen_add_eqn false (Code_Unit.mk_eqn thy (is_constr thy) (thm, true)) thy;
haftmann@31090
   503
haftmann@31090
   504
fun add_default_eqn thm thy =
haftmann@31090
   505
  case Code_Unit.mk_eqn_liberal thy (is_constr thy) thm
haftmann@31090
   506
   of SOME eqn => gen_add_eqn true eqn thy
haftmann@24624
   507
    | NONE => thy;
haftmann@24624
   508
haftmann@31090
   509
fun add_nbe_eqn thm thy =
haftmann@31090
   510
  gen_add_eqn false (Code_Unit.mk_eqn thy (is_constr thy) (thm, false)) thy;
haftmann@26021
   511
haftmann@28368
   512
fun add_eqnl (c, lthms) thy =
haftmann@24219
   513
  let
haftmann@31090
   514
    val lthms' = certificate thy (fn thy => assert_eqns_const thy c) lthms;
haftmann@28695
   515
  in change_eqns false c (add_lthms lthms') thy end;
haftmann@24219
   516
haftmann@28703
   517
val add_default_eqn_attribute = Thm.declaration_attribute
haftmann@28703
   518
  (fn thm => Context.mapping (add_default_eqn thm) I);
haftmann@28703
   519
val add_default_eqn_attrib = Attrib.internal (K add_default_eqn_attribute);
haftmann@24219
   520
haftmann@31090
   521
fun del_eqn thm thy = case Code_Unit.mk_eqn_liberal thy (is_constr thy) thm
haftmann@31088
   522
 of SOME (thm, _) => change_eqns true (Code_Unit.const_eqn thy thm) (del_thm thm) thy
haftmann@28695
   523
  | NONE => thy;
haftmann@28695
   524
haftmann@28695
   525
fun del_eqns c = change_eqns true c (K (false, Lazy.value []));
haftmann@28695
   526
haftmann@30076
   527
fun get_case_scheme thy = Symtab.lookup ((fst o the_cases o the_exec) thy);
haftmann@28695
   528
haftmann@28695
   529
val is_undefined = Symtab.defined o snd o the_cases o the_exec;
haftmann@28695
   530
haftmann@25485
   531
structure TypeInterpretation = InterpretationFun(type T = string * serial val eq = eq_snd (op =) : T * T -> bool);
haftmann@25462
   532
haftmann@24423
   533
fun add_datatype raw_cs thy =
haftmann@24219
   534
  let
haftmann@25597
   535
    val cs = map (fn c_ty as (_, ty) => (AxClass.unoverload_const thy c_ty, ty)) raw_cs;
haftmann@28054
   536
    val (tyco, vs_cos) = Code_Unit.constrset_of_consts thy cs;
haftmann@30076
   537
    val old_cs = (map fst o snd o get_datatype thy) tyco;
haftmann@30076
   538
    fun drop_outdated_cases cases = fold Symtab.delete_safe
haftmann@30076
   539
      (Symtab.fold (fn (c, (_, (_, cos))) =>
haftmann@30076
   540
        if exists (member (op =) old_cs) cos
haftmann@30076
   541
          then insert (op =) c else I) cases []) cases;
haftmann@24219
   542
  in
haftmann@24219
   543
    thy
haftmann@28703
   544
    |> map_exec_purge NONE
haftmann@28695
   545
        ((map_dtyps o Symtab.map_default (tyco, [])) (cons (serial (), vs_cos))
haftmann@30076
   546
        #> map_eqns (fold (Symtab.delete_safe o fst) cs)
haftmann@30076
   547
        #> (map_cases o apfst) drop_outdated_cases)
haftmann@25485
   548
    |> TypeInterpretation.data (tyco, serial ())
haftmann@24219
   549
  end;
haftmann@24219
   550
haftmann@25485
   551
fun type_interpretation f =  TypeInterpretation.interpretation
haftmann@25485
   552
  (fn (tyco, _) => fn thy => f (tyco, get_datatype thy tyco) thy);
haftmann@25485
   553
haftmann@24423
   554
fun add_datatype_cmd raw_cs thy =
haftmann@24423
   555
  let
haftmann@28054
   556
    val cs = map (Code_Unit.read_bare_const thy) raw_cs;
haftmann@24423
   557
  in add_datatype cs thy end;
haftmann@24219
   558
haftmann@24844
   559
fun add_case thm thy =
haftmann@24844
   560
  let
haftmann@30076
   561
    val (c, (k, case_pats)) = Code_Unit.case_cert thm;
haftmann@31088
   562
    val _ = case filter_out (is_constr thy) case_pats
haftmann@30076
   563
     of [] => ()
haftmann@30076
   564
      | cs => error ("Non-constructor(s) in case certificate: " ^ commas (map quote cs));
haftmann@30076
   565
    val entry = (1 + Int.max (1, length case_pats), (k, case_pats))
haftmann@30076
   566
  in (map_exec_purge (SOME [c]) o map_cases o apfst) (Symtab.update (c, entry)) thy end;
haftmann@24844
   567
haftmann@24844
   568
fun add_undefined c thy =
haftmann@24844
   569
  (map_exec_purge (SOME [c]) o map_cases o apsnd) (Symtab.update (c, ())) thy;
haftmann@24844
   570
haftmann@27557
   571
val map_pre = map_exec_purge NONE o map_thmproc o apfst o apfst;
haftmann@27557
   572
val map_post = map_exec_purge NONE o map_thmproc o apfst o apsnd;
haftmann@27557
   573
haftmann@28525
   574
val add_inline = map_pre o MetaSimplifier.add_simp;
haftmann@28525
   575
val del_inline = map_pre o MetaSimplifier.del_simp;
haftmann@28525
   576
val add_post = map_post o MetaSimplifier.add_simp;
haftmann@28525
   577
val del_post = map_post o MetaSimplifier.del_simp;
haftmann@27557
   578
  
haftmann@27557
   579
fun add_functrans (name, f) =
haftmann@27557
   580
  (map_exec_purge NONE o map_thmproc o apsnd)
haftmann@24219
   581
    (AList.update (op =) (name, (serial (), f)));
haftmann@24219
   582
haftmann@27557
   583
fun del_functrans name =
haftmann@24219
   584
  (map_exec_purge NONE o map_thmproc o apsnd)
haftmann@27609
   585
    (delete_force "function transformer" name);
haftmann@24219
   586
haftmann@31090
   587
fun simple_functrans f thy eqns = case f thy (map fst eqns)
haftmann@31090
   588
 of SOME thms' => SOME (map (rpair (forall snd eqns)) thms')
haftmann@31090
   589
  | NONE => NONE;
haftmann@31090
   590
wenzelm@26463
   591
val _ = Context.>> (Context.map_theory
haftmann@24219
   592
  (let
haftmann@24219
   593
    fun mk_attribute f = Thm.declaration_attribute (fn thm => Context.mapping (f thm) I);
haftmann@24219
   594
    fun add_simple_attribute (name, f) =
haftmann@24219
   595
      add_attribute (name, Scan.succeed (mk_attribute f));
haftmann@24219
   596
    fun add_del_attribute (name, (add, del)) =
haftmann@24219
   597
      add_attribute (name, Args.del |-- Scan.succeed (mk_attribute del)
haftmann@24219
   598
        || Scan.succeed (mk_attribute add))
haftmann@24219
   599
  in
haftmann@25462
   600
    TypeInterpretation.init
haftmann@28562
   601
    #> add_del_attribute ("", (add_eqn, del_eqn))
haftmann@31088
   602
    #> add_simple_attribute ("nbe", add_nbe_eqn)
haftmann@24219
   603
    #> add_del_attribute ("inline", (add_inline, del_inline))
haftmann@24219
   604
    #> add_del_attribute ("post", (add_post, del_post))
wenzelm@26463
   605
  end));
haftmann@24219
   606
haftmann@24219
   607
haftmann@24219
   608
(** post- and preprocessing **)
haftmann@24219
   609
haftmann@24219
   610
local
haftmann@24219
   611
haftmann@28423
   612
fun apply_functrans thy c _ [] = []
haftmann@28423
   613
  | apply_functrans thy c [] eqns = eqns
haftmann@28423
   614
  | apply_functrans thy c functrans eqns = eqns
haftmann@28423
   615
      |> perhaps (perhaps_loop (perhaps_apply functrans))
haftmann@31090
   616
      |> assert_eqns_const thy c;
haftmann@24219
   617
haftmann@28423
   618
fun rhs_conv conv thm = Thm.transitive thm ((conv o Thm.rhs_of) thm);
haftmann@24219
   619
haftmann@24837
   620
fun term_of_conv thy f =
haftmann@24837
   621
  Thm.cterm_of thy
haftmann@24837
   622
  #> f
haftmann@24837
   623
  #> Thm.prop_of
haftmann@24837
   624
  #> Logic.dest_equals
haftmann@24837
   625
  #> snd;
haftmann@24837
   626
haftmann@31088
   627
fun preprocess thy c eqns =
haftmann@27582
   628
  let
haftmann@27582
   629
    val pre = (Simplifier.theory_context thy o #pre o the_thmproc o the_exec) thy;
haftmann@31088
   630
    val functrans = (map (fn (_, (_, f)) => f thy) o #functrans
haftmann@31088
   631
      o the_thmproc o the_exec) thy;
haftmann@27582
   632
  in
haftmann@28423
   633
    eqns
haftmann@28423
   634
    |> apply_functrans thy c functrans
haftmann@28368
   635
    |> (map o apfst) (Code_Unit.rewrite_eqn pre)
haftmann@28525
   636
    |> (map o apfst) (AxClass.unoverload thy)
haftmann@31090
   637
    |> map (assert_eqn thy)
haftmann@28423
   638
    |> burrow_fst (common_typ_eqns thy)
haftmann@27582
   639
  end;
haftmann@26970
   640
haftmann@28423
   641
in
haftmann@28423
   642
haftmann@28423
   643
fun preprocess_conv thy ct =
haftmann@24219
   644
  let
haftmann@27582
   645
    val pre = (Simplifier.theory_context thy o #pre o the_thmproc o the_exec) thy;
haftmann@24219
   646
  in
haftmann@24219
   647
    ct
haftmann@27582
   648
    |> Simplifier.rewrite pre
haftmann@25597
   649
    |> rhs_conv (AxClass.unoverload_conv thy)
haftmann@24219
   650
  end;
haftmann@24219
   651
haftmann@28423
   652
fun preprocess_term thy = term_of_conv thy (preprocess_conv thy);
haftmann@24837
   653
haftmann@28423
   654
fun postprocess_conv thy ct =
haftmann@24219
   655
  let
haftmann@27582
   656
    val post = (Simplifier.theory_context thy o #post o the_thmproc o the_exec) thy;
haftmann@24219
   657
  in
haftmann@24219
   658
    ct
haftmann@25597
   659
    |> AxClass.overload_conv thy
haftmann@27582
   660
    |> rhs_conv (Simplifier.rewrite post)
haftmann@24219
   661
  end;
haftmann@24219
   662
haftmann@28423
   663
fun postprocess_term thy = term_of_conv thy (postprocess_conv thy);
haftmann@24219
   664
haftmann@28525
   665
fun these_raw_eqns thy c =
haftmann@28525
   666
  get_eqns thy c
haftmann@28695
   667
  |> (map o apfst) (Thm.transfer thy)
haftmann@28525
   668
  |> burrow_fst (common_typ_eqns thy);
haftmann@28525
   669
haftmann@28423
   670
fun these_eqns thy c =
haftmann@31088
   671
  get_eqns thy c
haftmann@31088
   672
  |> (map o apfst) (Thm.transfer thy)
haftmann@31088
   673
  |> preprocess thy c;
haftmann@24219
   674
haftmann@28525
   675
fun default_typscheme thy c =
haftmann@28525
   676
  let
haftmann@30023
   677
    fun the_const_typscheme c = (curry (Code_Unit.typscheme thy) c o snd o dest_Const
haftmann@30023
   678
      o TermSubst.zero_var_indexes o curry Const "" o Sign.the_const_type thy) c;
haftmann@30023
   679
    fun strip_sorts (vs, ty) = (map (fn (v, _) => (v, [])) vs, ty);
haftmann@28423
   680
  in case AxClass.class_of_param thy c
haftmann@30023
   681
   of SOME class => ([(Name.aT, [class])], snd (the_const_typscheme c))
haftmann@31088
   682
    | NONE => if is_constr thy c
haftmann@30023
   683
        then strip_sorts (the_const_typscheme c)
haftmann@30023
   684
        else case get_eqns thy c
haftmann@31088
   685
         of (thm, _) :: _ => (Code_Unit.typscheme_eqn thy o Drule.zero_var_indexes) thm
haftmann@30023
   686
          | [] => strip_sorts (the_const_typscheme c) end;
haftmann@24219
   687
haftmann@24219
   688
end; (*local*)
haftmann@24219
   689
haftmann@24219
   690
end; (*struct*)
haftmann@24219
   691
haftmann@24219
   692
haftmann@24219
   693
(** type-safe interfaces for data depedent on executable content **)
haftmann@24219
   694
haftmann@24219
   695
functor CodeDataFun(Data: CODE_DATA_ARGS): CODE_DATA =
haftmann@24219
   696
struct
haftmann@24219
   697
haftmann@24219
   698
type T = Data.T;
haftmann@24219
   699
exception Data of T;
haftmann@24219
   700
fun dest (Data x) = x
haftmann@24219
   701
haftmann@24219
   702
val kind = Code.declare_data (Data Data.empty)
haftmann@27609
   703
  (fn thy => fn cs => fn Data x => Data (Data.purge thy cs x));
haftmann@24219
   704
haftmann@24219
   705
val data_op = (kind, Data, dest);
haftmann@24219
   706
haftmann@24219
   707
val get = Code.get_data data_op;
haftmann@24219
   708
val change = Code.change_data data_op;
haftmann@24219
   709
fun change_yield thy = Code.change_yield_data data_op thy;
haftmann@24219
   710
haftmann@24219
   711
end;
haftmann@24219
   712
haftmann@28143
   713
structure Code : CODE = struct open Code; end;