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