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