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