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