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