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