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