src/Pure/Tools/codegen_data.ML
author haftmann
Thu, 25 Jan 2007 09:32:50 +0100
changeset 22184 a125f38a559a
parent 22050 859e5784c58c
child 22197 461130ccfef4
permissions -rw-r--r--
added explicit maintainance of coregular code theorems for overloaded constants
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
21338
56d55dd30311 cleaned up
haftmann
parents: 21284
diff changeset
    11
  val lazy: (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
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    14
  val add_func: thm -> theory -> theory
21128
7b2624686fc3 introduced CodegenData.add_func_legacy
haftmann
parents: 21119
diff changeset
    15
  val add_func_legacy: thm -> theory -> theory
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    16
  val del_func: thm -> theory -> theory
21338
56d55dd30311 cleaned up
haftmann
parents: 21284
diff changeset
    17
  val add_funcl: CodegenConsts.const * thm list Susp.T -> theory -> theory
56d55dd30311 cleaned up
haftmann
parents: 21284
diff changeset
    18
  val add_datatype: string * (((string * sort) list * (string * typ list) list) * thm list Susp.T)
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    19
    -> theory -> theory
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    20
  val del_datatype: string -> theory -> theory
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    21
  val add_inline: thm -> theory -> theory
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    22
  val del_inline: thm -> theory -> theory
22050
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
    23
  val add_inline_proc: string * (theory -> cterm list -> thm list) -> theory -> theory
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
    24
  val del_inline_proc: string -> theory -> theory
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
    25
  val add_preproc: string * (theory -> thm list -> thm list) -> theory -> theory
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
    26
  val del_preproc: string -> theory -> theory
22184
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
    27
  val coregular_algebra: theory -> Sorts.algebra
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
    28
  val operational_algebra: theory -> (sort -> sort) * Sorts.algebra
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    29
  val these_funcs: theory -> CodegenConsts.const -> thm list
22184
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
    30
  val tap_typ: theory -> CodegenConsts.const -> typ option
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    31
  val get_datatype: theory -> string
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    32
    -> ((string * sort) list * (string * typ list) list) option
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    33
  val get_datatype_of_constr: theory -> CodegenConsts.const -> string option
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    34
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    35
  val print_thms: theory -> unit
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    36
22033
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22020
diff changeset
    37
  val preprocess_cterm: cterm -> thm
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    38
20844
6792583aa463 changed preprocessing framework
haftmann
parents: 20704
diff changeset
    39
  val trace: bool ref
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    40
end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    41
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    42
signature PRIVATE_CODEGEN_DATA =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    43
sig
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    44
  include CODEGEN_DATA
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    45
  type data
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    46
  structure CodeData: THEORY_DATA
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    47
  val declare: string -> Object.T -> (Pretty.pp -> Object.T * Object.T -> Object.T)
20937
4297a44e26ae generalized purge
haftmann
parents: 20897
diff changeset
    48
    -> (theory option -> CodegenConsts.const list option -> Object.T -> Object.T) -> serial
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    49
  val init: serial -> theory -> theory
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    50
  val get: serial -> (Object.T -> 'a) -> data -> 'a
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    51
  val put: serial -> ('a -> Object.T) -> 'a -> data -> data
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    52
end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    53
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    54
structure CodegenData : PRIVATE_CODEGEN_DATA =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    55
struct
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    56
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    57
(** diagnostics **)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    58
20844
6792583aa463 changed preprocessing framework
haftmann
parents: 20704
diff changeset
    59
val trace = ref false;
6792583aa463 changed preprocessing framework
haftmann
parents: 20704
diff changeset
    60
fun tracing f x = (if !trace then Output.tracing (f x) else (); x);
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    61
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    62
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    63
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    64
(** lazy theorems, certificate theorems **)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    65
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    66
val eval_always = ref false;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    67
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    68
fun lazy f = if !eval_always
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    69
  then Susp.value (f ())
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    70
  else Susp.delay f;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    71
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    72
fun string_of_lthms r = case Susp.peek r
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    73
 of SOME thms => (map string_of_thm o rev) thms
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    74
  | NONE => ["[...]"];
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    75
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    76
fun pretty_lthms ctxt r = case Susp.peek r
21066
ce6759d1d0b4 code nofunc now permits theorems violating typing discipline
haftmann
parents: 20937
diff changeset
    77
 of SOME thms => map (ProofContext.pretty_thm ctxt) thms
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    78
  | NONE => [Pretty.str "[...]"];
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    79
20844
6792583aa463 changed preprocessing framework
haftmann
parents: 20704
diff changeset
    80
fun certificate thy f r =
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    81
  case Susp.peek r
20844
6792583aa463 changed preprocessing framework
haftmann
parents: 20704
diff changeset
    82
   of SOME thms => (Susp.value o f thy) thms
6792583aa463 changed preprocessing framework
haftmann
parents: 20704
diff changeset
    83
     | NONE => let
6792583aa463 changed preprocessing framework
haftmann
parents: 20704
diff changeset
    84
          val thy_ref = Theory.self_ref thy;
6792583aa463 changed preprocessing framework
haftmann
parents: 20704
diff changeset
    85
        in lazy (fn () => (f (Theory.deref thy_ref) o Susp.force) r) end;
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    86
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    87
fun merge' _ ([], []) = (false, [])
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    88
  | merge' _ ([], ys) = (true, ys)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    89
  | merge' eq (xs, ys) = fold_rev
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    90
      (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
    91
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    92
fun merge_alist eq_key eq (xys as (xs, ys)) =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    93
  if eq_list (eq_pair eq_key eq) (xs, ys)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    94
  then (false, xs)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    95
  else (true, AList.merge eq_key eq xys);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    96
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    97
val merge_thms = merge' eq_thm;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    98
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    99
fun merge_lthms (r1, r2) =
21119
5c7edac0c645 simplified preprocessor framework
haftmann
parents: 21066
diff changeset
   100
  if Susp.same (r1, r2)
5c7edac0c645 simplified preprocessor framework
haftmann
parents: 21066
diff changeset
   101
    then (false, r1)
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   102
  else case Susp.peek r1
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   103
   of SOME [] => (true, r2)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   104
    | _ => case Susp.peek r2
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   105
       of SOME [] => (true, r1)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   106
        | _ => (apsnd (lazy o K)) (merge_thms (Susp.force r1, Susp.force r2));
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   107
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   108
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   109
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   110
(** code theorems **)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   111
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   112
(* pairs of (selected, deleted) function theorems *)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   113
21338
56d55dd30311 cleaned up
haftmann
parents: 21284
diff changeset
   114
type sdthms = thm list Susp.T * thm list;
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   115
22006
9dc365b03573 dropped function theorems are considered as deleted
haftmann
parents: 21988
diff changeset
   116
fun add_drop_redundant thm (sels, dels) =
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   117
  let
22006
9dc365b03573 dropped function theorems are considered as deleted
haftmann
parents: 21988
diff changeset
   118
    val thy = Thm.theory_of_thm thm;
21284
36613fe4cf05 redundancy checkes includes eta-expansion
haftmann
parents: 21158
diff changeset
   119
    val args_of = snd o strip_comb o fst o Logic.dest_equals o Drule.plain_prop_of;
36613fe4cf05 redundancy checkes includes eta-expansion
haftmann
parents: 21158
diff changeset
   120
    val args = args_of thm;
36613fe4cf05 redundancy checkes includes eta-expansion
haftmann
parents: 21158
diff changeset
   121
    fun matches [] _ = true
36613fe4cf05 redundancy checkes includes eta-expansion
haftmann
parents: 21158
diff changeset
   122
      | matches (Var _ :: xs) [] = matches xs []
36613fe4cf05 redundancy checkes includes eta-expansion
haftmann
parents: 21158
diff changeset
   123
      | matches (_ :: _) [] = false
36613fe4cf05 redundancy checkes includes eta-expansion
haftmann
parents: 21158
diff changeset
   124
      | 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
   125
    fun drop thm' = not (matches args (args_of thm'))
9dc365b03573 dropped function theorems are considered as deleted
haftmann
parents: 21988
diff changeset
   126
      orelse (warning ("Dropping redundant function theorem\n" ^ string_of_thm thm'); false);
9dc365b03573 dropped function theorems are considered as deleted
haftmann
parents: 21988
diff changeset
   127
    val (keeps, drops) = List.partition drop sels;
9dc365b03573 dropped function theorems are considered as deleted
haftmann
parents: 21988
diff changeset
   128
  in (thm :: keeps, dels |> fold (insert eq_thm) drops |> remove eq_thm thm) end;
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   129
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   130
fun add_thm thm (sels, dels) =
22006
9dc365b03573 dropped function theorems are considered as deleted
haftmann
parents: 21988
diff changeset
   131
  apfst Susp.value (add_drop_redundant thm (Susp.force sels, dels));
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   132
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   133
fun add_lthms lthms (sels, []) =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   134
      (lazy (fn () => fold add_drop_redundant
22006
9dc365b03573 dropped function theorems are considered as deleted
haftmann
parents: 21988
diff changeset
   135
        (Susp.force lthms) (Susp.force sels, []) |> fst), [])
9dc365b03573 dropped function theorems are considered as deleted
haftmann
parents: 21988
diff changeset
   136
        (*FIXME*)
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   137
  | add_lthms lthms (sels, dels) =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   138
      fold add_thm (Susp.force lthms) (sels, dels);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   139
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   140
fun del_thm thm (sels, dels) =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   141
  (Susp.value (remove eq_thm thm (Susp.force sels)), thm :: dels);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   142
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   143
fun pretty_sdthms ctxt (sels, _) = pretty_lthms ctxt sels;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   144
21119
5c7edac0c645 simplified preprocessor framework
haftmann
parents: 21066
diff changeset
   145
fun merge_sdthms ((sels1, dels1), (sels2, dels2)) =
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   146
  let
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   147
    val (dels_t, dels) = merge_thms (dels1, dels2);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   148
  in if dels_t
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   149
    then let
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   150
      val (_, sels) = merge_thms (Susp.force sels1, subtract eq_thm dels1 (Susp.force sels2))
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   151
      val (_, dels) = merge_thms (dels1, subtract eq_thm (Susp.force sels1) dels2)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   152
    in (true, ((lazy o K) sels, dels)) end
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   153
    else let
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   154
      val (sels_t, sels) = merge_lthms (sels1, sels2)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   155
    in (sels_t, (sels, dels)) end
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   156
  end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   157
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   158
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   159
(** data structures **)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   160
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   161
structure Consttab = CodegenConsts.Consttab;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   162
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   163
datatype preproc = Preproc of {
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   164
  inlines: thm list,
22050
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   165
  inline_procs: (string * (serial * (theory -> cterm list -> thm list))) list,
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   166
  preprocs: (string * (serial * (theory -> thm list -> thm list))) list
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   167
};
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   168
21119
5c7edac0c645 simplified preprocessor framework
haftmann
parents: 21066
diff changeset
   169
fun mk_preproc ((inlines, inline_procs), preprocs) =
5c7edac0c645 simplified preprocessor framework
haftmann
parents: 21066
diff changeset
   170
  Preproc { inlines = inlines, inline_procs = inline_procs, preprocs = preprocs };
5c7edac0c645 simplified preprocessor framework
haftmann
parents: 21066
diff changeset
   171
fun map_preproc f (Preproc { inlines, inline_procs, preprocs }) =
5c7edac0c645 simplified preprocessor framework
haftmann
parents: 21066
diff changeset
   172
  mk_preproc (f ((inlines, inline_procs), preprocs));
5c7edac0c645 simplified preprocessor framework
haftmann
parents: 21066
diff changeset
   173
fun merge_preproc (Preproc { inlines = inlines1, inline_procs = inline_procs1, preprocs = preprocs1 },
5c7edac0c645 simplified preprocessor framework
haftmann
parents: 21066
diff changeset
   174
  Preproc { inlines = inlines2, inline_procs = inline_procs2, preprocs = preprocs2 }) =
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   175
    let
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   176
      val (touched1, inlines) = merge_thms (inlines1, inlines2);
22050
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   177
      val (touched2, inline_procs) = merge_alist (op =) (eq_fst (op =)) (inline_procs1, inline_procs2);
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   178
      val (touched3, preprocs) = merge_alist (op =) (eq_fst (op =)) (preprocs1, preprocs2);
21119
5c7edac0c645 simplified preprocessor framework
haftmann
parents: 21066
diff changeset
   179
    in (touched1 orelse touched2 orelse touched3,
5c7edac0c645 simplified preprocessor framework
haftmann
parents: 21066
diff changeset
   180
      mk_preproc ((inlines, inline_procs), preprocs)) end;
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   181
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   182
fun join_func_thms (tabs as (tab1, tab2)) =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   183
  let
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   184
    val cs1 = Consttab.keys tab1;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   185
    val cs2 = Consttab.keys tab2;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   186
    val cs' = filter (member CodegenConsts.eq_const cs2) cs1;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   187
    val cs'' = subtract (op =) cs' cs1 @ subtract (op =) cs' cs2;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   188
    val cs''' = ref [] : CodegenConsts.const list ref;
21119
5c7edac0c645 simplified preprocessor framework
haftmann
parents: 21066
diff changeset
   189
    fun merge c x = let val (touched, thms') = merge_sdthms x in
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   190
      (if touched then cs''' := cons c (!cs''') else (); thms') end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   191
  in (cs'' @ !cs''', Consttab.join merge tabs) end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   192
fun merge_funcs (thms1, thms2) =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   193
    let
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   194
      val (consts, thms) = join_func_thms (thms1, thms2);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   195
    in (SOME consts, thms) end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   196
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   197
val eq_string = op = : string * string -> bool;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   198
fun eq_dtyp (((vs1, cs1), _), ((vs2, cs2), _)) = 
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   199
  gen_eq_set (eq_pair eq_string (gen_eq_set eq_string)) (vs1, vs2)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   200
    andalso gen_eq_set (eq_pair eq_string (eq_list (is_equal o Term.typ_ord))) (cs1, cs2);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   201
fun merge_dtyps (tabs as (tab1, tab2)) =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   202
  let
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   203
    val tycos1 = Symtab.keys tab1;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   204
    val tycos2 = Symtab.keys tab2;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   205
    val tycos' = filter (member eq_string tycos2) tycos1;
20844
6792583aa463 changed preprocessing framework
haftmann
parents: 20704
diff changeset
   206
    val touched = not (gen_eq_set (op =) (tycos1, tycos2) andalso
6792583aa463 changed preprocessing framework
haftmann
parents: 20704
diff changeset
   207
      gen_eq_set (eq_pair (op =) (eq_dtyp))
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   208
      (AList.make (the o Symtab.lookup tab1) tycos',
20844
6792583aa463 changed preprocessing framework
haftmann
parents: 20704
diff changeset
   209
       AList.make (the o Symtab.lookup tab2) tycos'));
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   210
  in (touched, Symtab.merge (K true) tabs) end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   211
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   212
datatype spec = Spec of {
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   213
  funcs: sdthms Consttab.table,
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   214
  dconstrs: string Consttab.table,
21338
56d55dd30311 cleaned up
haftmann
parents: 21284
diff changeset
   215
  dtyps: (((string * sort) list * (string * typ list) list) * thm list Susp.T) Symtab.table
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   216
};
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   217
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   218
fun mk_spec ((funcs, dconstrs), dtyps) =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   219
  Spec { funcs = funcs, dconstrs = dconstrs, dtyps = dtyps };
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   220
fun map_spec f (Spec { funcs = funcs, dconstrs = dconstrs, dtyps = dtyps }) =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   221
  mk_spec (f ((funcs, dconstrs), dtyps));
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   222
fun merge_spec (Spec { funcs = funcs1, dconstrs = dconstrs1, dtyps = dtyps1 },
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   223
  Spec { funcs = funcs2, dconstrs = dconstrs2, dtyps = dtyps2 }) =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   224
  let
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   225
    val (touched_cs, funcs) = merge_funcs (funcs1, funcs2);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   226
    val dconstrs = Consttab.merge (K true) (dconstrs1, dconstrs2);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   227
    val (touched', dtyps) = merge_dtyps (dtyps1, dtyps2);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   228
    val touched = if touched' then NONE else touched_cs;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   229
  in (touched, mk_spec ((funcs, dconstrs), dtyps)) end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   230
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   231
datatype exec = Exec of {
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   232
  preproc: preproc,
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   233
  spec: spec
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   234
};
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   235
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   236
fun mk_exec (preproc, spec) =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   237
  Exec { preproc = preproc, spec = spec };
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   238
fun map_exec f (Exec { preproc = preproc, spec = spec }) =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   239
  mk_exec (f (preproc, spec));
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   240
fun merge_exec (Exec { preproc = preproc1, spec = spec1 },
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   241
  Exec { preproc = preproc2, spec = spec2 }) =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   242
  let
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   243
    val (touched', preproc) = merge_preproc (preproc1, preproc2);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   244
    val (touched_cs, spec) = merge_spec (spec1, spec2);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   245
    val touched = if touched' then NONE else touched_cs;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   246
  in (touched, mk_exec (preproc, spec)) end;
21119
5c7edac0c645 simplified preprocessor framework
haftmann
parents: 21066
diff changeset
   247
val empty_exec = mk_exec (mk_preproc (([], []), []),
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   248
  mk_spec ((Consttab.empty, Consttab.empty), Symtab.empty));
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   249
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   250
fun the_preproc (Exec { preproc = Preproc x, ...}) = x;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   251
fun the_spec (Exec { spec = Spec x, ...}) = x;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   252
val the_funcs = #funcs o the_spec;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   253
val the_dcontrs = #dconstrs o the_spec;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   254
val the_dtyps = #dtyps o the_spec;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   255
val map_preproc = map_exec o apfst o map_preproc;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   256
val map_funcs = map_exec o apsnd o map_spec o apfst o apfst;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   257
val map_dconstrs = map_exec o apsnd o map_spec o apfst o apsnd;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   258
val map_dtyps = map_exec o apsnd o map_spec o apsnd;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   259
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   260
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   261
(** code data structures **)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   262
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   263
(*private copy avoids potential conflict of table exceptions*)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   264
structure Datatab = TableFun(type key = int val ord = int_ord);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   265
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   266
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   267
(* data slots *)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   268
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   269
local
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   270
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   271
type kind = {
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   272
  name: string,
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   273
  empty: Object.T,
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   274
  merge: Pretty.pp -> Object.T * Object.T -> Object.T,
20937
4297a44e26ae generalized purge
haftmann
parents: 20897
diff changeset
   275
  purge: theory option -> CodegenConsts.const list option -> Object.T -> Object.T
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   276
};
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   277
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   278
val kinds = ref (Datatab.empty: kind Datatab.table);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   279
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   280
fun invoke meth_name meth_fn k =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   281
  (case Datatab.lookup (! kinds) k of
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   282
    SOME kind => meth_fn kind |> transform_failure (fn exn =>
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   283
      EXCEPTION (exn, "Code data method " ^ #name kind ^ "." ^ meth_name ^ " failed"))
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   284
  | NONE => sys_error ("Invalid code data identifier " ^ string_of_int k));
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   285
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   286
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   287
in
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   288
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   289
fun invoke_name k   = invoke "name" (K o #name) k ();
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   290
fun invoke_empty k  = invoke "empty" (K o #empty) k ();
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   291
fun invoke_merge pp = invoke "merge" (fn kind => #merge kind pp);
20937
4297a44e26ae generalized purge
haftmann
parents: 20897
diff changeset
   292
fun invoke_purge thy_opt cs = invoke "purge" (fn kind => #purge kind thy_opt cs);
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   293
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   294
fun declare name empty merge purge =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   295
  let
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   296
    val k = serial ();
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   297
    val kind = {name = name, empty = empty, merge = merge, purge = purge};
21962
279b129498b6 removed conditional combinator;
wenzelm
parents: 21835
diff changeset
   298
    val _ =
279b129498b6 removed conditional combinator;
wenzelm
parents: 21835
diff changeset
   299
      if Datatab.exists (equal name o #name o #2) (! kinds) then
279b129498b6 removed conditional combinator;
wenzelm
parents: 21835
diff changeset
   300
        warning ("Duplicate declaration of code data " ^ quote name)
279b129498b6 removed conditional combinator;
wenzelm
parents: 21835
diff changeset
   301
      else ();
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   302
    val _ = change kinds (Datatab.update (k, kind));
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   303
  in k end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   304
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   305
end; (*local*)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   306
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   307
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   308
(* theory store *)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   309
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   310
type data = Object.T Datatab.table;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   311
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   312
structure CodeData = TheoryDataFun
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   313
(struct
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   314
  val name = "Pure/codegen_data";
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   315
  type T = exec * data ref;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   316
  val empty = (empty_exec, ref Datatab.empty : data ref);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   317
  fun copy (exec, data) = (exec, ref (! data));
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   318
  val extend = copy;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   319
  fun merge pp ((exec1, data1), (exec2, data2)) =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   320
    let
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   321
      val (touched, exec) = merge_exec (exec1, exec2);
20937
4297a44e26ae generalized purge
haftmann
parents: 20897
diff changeset
   322
      val data1' = Datatab.map' (invoke_purge NONE touched) (! data1);
4297a44e26ae generalized purge
haftmann
parents: 20897
diff changeset
   323
      val data2' = Datatab.map' (invoke_purge NONE touched) (! data2);
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   324
      val data = Datatab.join (invoke_merge pp) (data1', data2');
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   325
    in (exec, ref data) end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   326
  fun print thy (exec, _) =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   327
    let
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   328
      val ctxt = ProofContext.init thy;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   329
      fun pretty_func (s, lthms) =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   330
        (Pretty.block o Pretty.fbreaks) (
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   331
          Pretty.str s :: pretty_sdthms ctxt lthms
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   332
        );
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   333
      fun pretty_dtyp (s, cos) =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   334
        (Pretty.block o Pretty.breaks) (
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   335
          Pretty.str s
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   336
          :: Pretty.str "="
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   337
          :: Pretty.separate "|" (map (fn (c, []) => Pretty.str c
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   338
               | (c, tys) =>
22184
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   339
                   (Pretty.block o Pretty.breaks)
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   340
                      (Pretty.str c :: Pretty.str "of" :: map (Pretty.quote o Sign.pretty_typ thy) tys)) cos)
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   341
        )
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   342
      val inlines = (#inlines o the_preproc) exec;
22050
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   343
      val inline_procs = (map fst o #inline_procs o the_preproc) exec;
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   344
      val preprocs = (map fst o #preprocs o the_preproc) exec;
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   345
      val funs = the_funcs exec
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   346
        |> Consttab.dest
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   347
        |> (map o apfst) (CodegenConsts.string_of_const thy)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   348
        |> sort (string_ord o pairself fst);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   349
      val dtyps = the_dtyps exec
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   350
        |> Symtab.dest
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   351
        |> map (fn (dtco, ((vs, cos), _)) => (Sign.string_of_typ thy (Type (dtco, map TFree vs)), cos))
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   352
        |> sort (string_ord o pairself fst)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   353
    in
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   354
      (Pretty.writeln o Pretty.block o Pretty.fbreaks) ([
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   355
        Pretty.str "code theorems:",
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   356
        Pretty.str "function theorems:" ] @
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   357
          map pretty_func funs @ [
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   358
        Pretty.block (
22050
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   359
          Pretty.str "inlining theorems:"
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   360
          :: Pretty.fbrk
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   361
          :: (Pretty.fbreaks o map (ProofContext.pretty_thm ctxt)) inlines
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   362
        ),
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   363
        Pretty.block (
22050
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   364
          Pretty.str "inlining procedures:"
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   365
          :: Pretty.fbrk
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   366
          :: (Pretty.fbreaks o map Pretty.str) inline_procs
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   367
        ),
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   368
        Pretty.block (
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   369
          Pretty.str "preprocessors:"
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   370
          :: Pretty.fbrk
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   371
          :: (Pretty.fbreaks o map Pretty.str) preprocs
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   372
        ),
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   373
        Pretty.block (
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   374
          Pretty.str "datatypes:"
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   375
          :: Pretty.fbrk
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   376
          :: (Pretty.fbreaks o map pretty_dtyp) dtyps
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   377
        )]
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   378
      )
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   379
    end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   380
end);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   381
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   382
fun print_thms thy = CodeData.print thy;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   383
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   384
fun init k = CodeData.map
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   385
  (fn (exec, data) => (exec, ref (Datatab.update (k, invoke_empty k) (! data))));
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   386
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   387
fun get k dest data =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   388
  (case Datatab.lookup data k of
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   389
    SOME x => (dest x handle Match =>
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   390
      error ("Failed to access code data " ^ quote (invoke_name k)))
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   391
  | NONE => error ("Uninitialized code data " ^ quote (invoke_name k)));
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   392
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   393
fun put k mk x = Datatab.update (k, mk x);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   394
20937
4297a44e26ae generalized purge
haftmann
parents: 20897
diff changeset
   395
fun map_exec_purge touched f thy =
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   396
  CodeData.map (fn (exec, data) => 
20937
4297a44e26ae generalized purge
haftmann
parents: 20897
diff changeset
   397
    (f exec, ref (Datatab.map' (invoke_purge (SOME thy) touched) (! data)))) thy;
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   398
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   399
val get_exec = fst o CodeData.get;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   400
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   401
val _ = Context.add_setup CodeData.init;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   402
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   403
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   404
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   405
(** theorem transformation and certification **)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   406
22033
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22020
diff changeset
   407
fun common_typ_funcs [] = []
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22020
diff changeset
   408
  | common_typ_funcs [thm] = [thm]
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22020
diff changeset
   409
  | common_typ_funcs thms =
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   410
      let
22033
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22020
diff changeset
   411
        val thy = Thm.theory_of_thm (hd thms)
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   412
        fun incr_thm thm max =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   413
          let
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   414
            val thm' = incr_indexes max thm;
21119
5c7edac0c645 simplified preprocessor framework
haftmann
parents: 21066
diff changeset
   415
            val max' = Thm.maxidx_of thm' + 1;
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   416
          in (thm', max') end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   417
        val (thms', maxidx) = fold_map incr_thm thms 0;
22033
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22020
diff changeset
   418
        val (ty1::tys) = map CodegenFunc.typ_func thms';
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   419
        fun unify ty env = Sign.typ_unify thy (ty1, ty) env
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   420
          handle Type.TUNIFY =>
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   421
            error ("Type unificaton failed, while unifying function equations\n"
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   422
            ^ (cat_lines o map Display.string_of_thm) thms
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   423
            ^ "\nwith types\n"
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   424
            ^ (cat_lines o map (Sign.string_of_typ thy)) (ty1 :: tys));
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   425
        val (env, _) = fold unify tys (Vartab.empty, maxidx)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   426
        val instT = Vartab.fold (fn (x_i, (sort, ty)) =>
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   427
          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
   428
      in map (Thm.instantiate (instT, [])) thms' end;
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   429
20844
6792583aa463 changed preprocessing framework
haftmann
parents: 20704
diff changeset
   430
fun certify_const thy c c_thms =
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   431
  let
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   432
    fun cert (c', thm) = if CodegenConsts.eq_const (c, c')
22033
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22020
diff changeset
   433
      then thm else error ("Wrong head of function equation,\nexpected constant "
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22020
diff changeset
   434
        ^ CodegenConsts.string_of_const thy c ^ "\n" ^ string_of_thm thm)
20844
6792583aa463 changed preprocessing framework
haftmann
parents: 20704
diff changeset
   435
  in map cert c_thms end;
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   436
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   437
fun mk_cos tyco vs cos =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   438
  let
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   439
    val dty = Type (tyco, map TFree vs);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   440
    fun mk_co (co, tys) = (Const (co, (tys ---> dty)), map I tys);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   441
  in map mk_co cos end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   442
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   443
fun mk_co_args (co, tys) ctxt =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   444
  let
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   445
    val names = Name.invents ctxt "a" (length tys);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   446
    val ctxt' = fold Name.declare names ctxt;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   447
    val vs = map2 (fn v => fn ty => Free (fst (v, 0), I ty)) names tys;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   448
  in (vs, ctxt') end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   449
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   450
fun check_freeness thy cos thms =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   451
  let
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   452
    val props = AList.make Drule.plain_prop_of thms;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   453
    fun sym_product [] = []
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   454
      | sym_product (x::xs) = map (pair x) xs @ sym_product xs;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   455
    val quodlibet =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   456
      let
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   457
        val judg = ObjectLogic.fixed_judgment (the_context ()) "x";
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   458
        val [free] = fold_aterms (fn v as Free _ => cons v | _ => I) judg [];
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   459
        val judg' = Term.subst_free [(free, Bound 0)] judg;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   460
        val prop = Type ("prop", []);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   461
        val prop' = fastype_of judg';
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   462
      in
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   463
        Const ("all", (prop' --> prop) --> prop) $ Abs ("P", prop', judg')
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   464
      end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   465
    fun check_inj (co, []) =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   466
          NONE
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   467
      | check_inj (co, tys) =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   468
          let
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   469
            val ((xs, ys), _) = Name.context
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   470
              |> mk_co_args (co, tys)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   471
              ||>> mk_co_args (co, tys);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   472
            val prem = Logic.mk_equals
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   473
              (list_comb (co, xs), list_comb (co, ys));
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   474
            val concl = Logic.mk_conjunction_list
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   475
              (map2 (curry Logic.mk_equals) xs ys);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   476
            val t = Logic.mk_implies (prem, concl);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   477
          in case find_first (curry Term.could_unify t o snd) props
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   478
           of SOME (thm, _) => SOME thm
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   479
            | NONE => error ("Could not prove injectiveness statement\n"
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   480
               ^ Sign.string_of_term thy t
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   481
               ^ "\nfor constructor "
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   482
               ^ CodegenConsts.string_of_const_typ thy (dest_Const co)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   483
               ^ "\nwith theorems\n" ^ cat_lines (map string_of_thm thms))
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   484
          end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   485
    fun check_dist ((co1, tys1), (co2, tys2)) =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   486
          let
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   487
            val ((xs1, xs2), _) = Name.context
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   488
              |> mk_co_args (co1, tys1)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   489
              ||>> mk_co_args (co2, tys2);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   490
            val prem = Logic.mk_equals
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   491
              (list_comb (co1, xs1), list_comb (co2, xs2));
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   492
            val t = Logic.mk_implies (prem, quodlibet);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   493
          in case find_first (curry Term.could_unify t o snd) props
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   494
           of SOME (thm, _) => thm
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   495
            | NONE => error ("Could not prove distinctness statement\n"
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   496
               ^ Sign.string_of_term thy t
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   497
               ^ "\nfor constructors "
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   498
               ^ CodegenConsts.string_of_const_typ thy (dest_Const co1)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   499
               ^ " and "
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   500
               ^ CodegenConsts.string_of_const_typ thy (dest_Const co2)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   501
               ^ "\nwith theorems\n" ^ cat_lines (map string_of_thm thms))
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   502
          end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   503
  in (map_filter check_inj cos, map check_dist (sym_product cos)) end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   504
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   505
fun certify_datatype thy dtco cs thms =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   506
  (op @) (check_freeness thy cs thms);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   507
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   508
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   509
22184
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   510
(** operational sort algebra and class discipline **)
22050
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   511
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   512
local
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   513
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   514
fun aggr_neutr f y [] = y
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   515
  | aggr_neutr f y (x::xs) = aggr_neutr f (f y x) xs;
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   516
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   517
fun aggregate f [] = NONE
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   518
  | aggregate f (x::xs) = SOME (aggr_neutr f x xs);
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   519
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   520
fun inter_sorts thy =
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   521
  let
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   522
    val algebra = Sign.classes_of thy;
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   523
    val inters = curry (Sorts.inter_sort algebra);
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   524
  in aggregate (map2 inters) end;
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   525
22184
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   526
fun specific_constraints thy (class, tyco) =
22050
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   527
  let
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   528
    val vs = Name.invents Name.context "" (Sign.arity_number thy tyco);
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   529
    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
   530
    val funcs = clsops
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   531
      |> map (fn (clsop, _) => CodegenConsts.norm thy (clsop, [Type (tyco, map (TFree o rpair []) vs)]))
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   532
      |> 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
   533
      |> (map o Option.map) (Susp.force o fst)
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   534
      |> maps these
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   535
      |> map (Thm.transfer thy);
22050
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   536
    val sorts = map (map (snd o dest_TVar) o snd o dest_Type o the_single
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   537
      o Sign.const_typargs thy o fst o CodegenFunc.dest_func) funcs;
22184
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   538
  in sorts end;
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   539
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   540
fun weakest_constraints thy (class, tyco) =
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   541
  let
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   542
    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
   543
  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
   544
   of SOME sorts => sorts
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   545
    | NONE => Sign.arity_sorts thy tyco [class]
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   546
  end;
22050
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   547
22184
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   548
fun strongest_constraints thy (class, tyco) =
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   549
  let
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   550
    val algebra = Sign.classes_of thy;
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   551
    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
   552
    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
   553
  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
   554
   of SOME sorts => sorts
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   555
    | NONE => replicate
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   556
        (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
   557
  end;
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   558
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   559
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
   560
  let
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   561
    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
   562
    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
   563
    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
   564
      (constr thy (class, tyco));
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   565
    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
   566
  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
   567
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   568
(*FIXME: make distinct step: building algebra from code theorems*)
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   569
fun retrieve_algebra thy operational =
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   570
  Sorts.subalgebra (Sign.pp thy) operational
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   571
    (weakest_constraints thy)
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   572
    (Sign.classes_of thy);
22050
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   573
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   574
in
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   575
22184
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   576
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
   577
fun operational_algebra thy =
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   578
  let
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   579
    fun add_iff_operational class classes =
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   580
      if (not o null o these o Option.map #params o try (AxClass.get_definition thy)) class
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   581
        orelse (length o gen_inter (op =))
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   582
          ((Sign.certify_sort thy o Sign.super_classes thy) class, classes) >= 2
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   583
      then class :: classes
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   584
      else classes;
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   585
    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
   586
  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
   587
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   588
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
   589
val classop_strongest_typ = gen_classop_typ strongest_constraints;
22050
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   590
22184
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   591
fun gen_mk_func_typ strict_functyp thm =
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   592
  let
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   593
    val thy = Thm.theory_of_thm thm;
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   594
    val raw_funcs = CodegenFunc.mk_func thm;
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   595
    val error_warning = if strict_functyp then error else warning #> K NONE;
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   596
    val string_of_typ = setmp show_sorts true (Sign.string_of_typ thy);
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   597
    fun check_typ_classop class (const as (c, [Type (tyco, _)]), thm) =
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   598
          let
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   599
            val ((_, ty), _) = CodegenFunc.dest_func thm;
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   600
            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
   601
            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
   602
            fun constrain thm = 
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   603
              let
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   604
                val max = Thm.maxidx_of thm + 1;
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   605
                val ty_decl' = Logic.incr_tvar max ty_decl;
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   606
                val ((_, ty'), _) = CodegenFunc.dest_func thm;
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   607
                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
   608
                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
   609
                  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
   610
              in Thm.instantiate (instT, []) thm end;
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   611
          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
   612
            then if Sign.typ_instance thy (ty, ty_decl)
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   613
            then SOME (const, thm)
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   614
            else (warning ("Constraining type\n" ^ string_of_typ ty
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   615
              ^ "\nof function theorem\n"
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   616
              ^ string_of_thm thm
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   617
              ^ "\nto permitted most general type\n"
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   618
              ^ string_of_typ ty_decl);
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   619
              SOME (const, constrain thm))
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   620
            else error_warning ("Type\n" ^ string_of_typ ty
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   621
              ^ "\nof function theorem\n"
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   622
              ^ string_of_thm thm
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   623
              ^ "\nis incompatible with permitted least general type\n"
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   624
              ^ string_of_typ ty_strongest)
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   625
          end
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   626
      | check_typ_classop class ((c, [_]), thm) =
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   627
          (if strict_functyp then error else warning #> K NONE)
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   628
           ("Illegal type for class operation " ^ quote c
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   629
           ^ "\nin function theorem\n"
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   630
           ^ string_of_thm thm);
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   631
    fun check_typ_fun (const as (c, _), thm) =
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   632
      let
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   633
        val ((_, ty), _) = CodegenFunc.dest_func thm;
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   634
        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
   635
      in if Sign.typ_equiv thy (Type.strip_sorts ty_decl, Type.strip_sorts ty)
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   636
        then SOME (const, thm)
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   637
        else error_warning ("Type\n" ^ string_of_typ ty
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   638
           ^ "\nof function theorem\n"
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   639
           ^ string_of_thm thm
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   640
           ^ "\nis incompatible declared function type\n"
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   641
           ^ string_of_typ ty_decl)
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   642
      end;
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   643
    fun check_typ (const as (c, tys), thm) =
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   644
      case AxClass.class_of_param thy c
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   645
       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
   646
        | NONE => check_typ_fun (const, thm);
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   647
    val funcs = map_filter check_typ raw_funcs;
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   648
  in funcs end;
22050
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   649
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   650
end;
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   651
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   652
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   653
(** interfaces **)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   654
22184
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   655
fun gen_add_func strict_functyp thm thy =
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   656
  let
22184
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   657
    val funcs = gen_mk_func_typ strict_functyp thm;
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   658
    val cs = map fst funcs;
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   659
  in
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   660
    map_exec_purge (SOME cs) (map_funcs 
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   661
     (fold (fn (c, thm) => Consttab.map_default
22184
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   662
       (c, (Susp.value [], [])) (add_thm thm)) funcs)) thy
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   663
  end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   664
22184
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   665
val add_func = gen_add_func true;
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   666
val add_func_legacy = gen_add_func false;
21128
7b2624686fc3 introduced CodegenData.add_func_legacy
haftmann
parents: 21119
diff changeset
   667
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   668
fun del_func thm thy =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   669
  let
22184
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   670
    val funcs = gen_mk_func_typ false thm;
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   671
    val cs = map fst funcs;
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   672
  in
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   673
    map_exec_purge (SOME cs) (map_funcs
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   674
     (fold (fn (c, thm) => Consttab.map_entry c
22184
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   675
       (del_thm thm)) funcs)) thy
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   676
  end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   677
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   678
fun add_funcl (c, lthms) thy =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   679
  let
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   680
    val c' = CodegenConsts.norm thy c;
22033
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22020
diff changeset
   681
    val lthms' = certificate thy (fn thy => certify_const thy c' o maps (CodegenFunc.mk_func)) lthms;
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   682
  in
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   683
    map_exec_purge (SOME [c]) (map_funcs (Consttab.map_default (c', (Susp.value [], []))
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   684
      (add_lthms lthms'))) thy
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   685
  end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   686
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   687
fun add_datatype (tyco, (vs_cos as (vs, cos), lthms)) thy =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   688
  let
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   689
    val cs = mk_cos tyco vs cos;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   690
    val consts = map (CodegenConsts.norm_of_typ thy o dest_Const o fst) cs;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   691
    val add =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   692
      map_dtyps (Symtab.update_new (tyco,
20844
6792583aa463 changed preprocessing framework
haftmann
parents: 20704
diff changeset
   693
        (vs_cos, certificate thy (fn thy => certify_datatype thy tyco cs) lthms)))
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   694
      #> map_dconstrs (fold (fn c => Consttab.update (c, tyco)) consts)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   695
  in map_exec_purge (SOME consts) add thy end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   696
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   697
fun del_datatype tyco thy =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   698
  let
20639
3aa960295c54 fixed bug
haftmann
parents: 20608
diff changeset
   699
    val SOME ((vs, cos), _) = Symtab.lookup ((the_dtyps o get_exec) thy) tyco;
3aa960295c54 fixed bug
haftmann
parents: 20608
diff changeset
   700
    val cs = mk_cos tyco vs cos;
3aa960295c54 fixed bug
haftmann
parents: 20608
diff changeset
   701
    val consts = map (CodegenConsts.norm_of_typ thy o dest_Const o fst) cs;
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   702
    val del =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   703
      map_dtyps (Symtab.delete tyco)
20639
3aa960295c54 fixed bug
haftmann
parents: 20608
diff changeset
   704
      #> map_dconstrs (fold Consttab.delete consts)
3aa960295c54 fixed bug
haftmann
parents: 20608
diff changeset
   705
  in map_exec_purge (SOME consts) del thy end;
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   706
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   707
fun add_inline thm thy =
22033
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22020
diff changeset
   708
  (map_exec_purge NONE o map_preproc o apfst o apfst) (fold (insert eq_thm) (CodegenFunc.mk_rew thm)) thy;
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   709
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   710
fun del_inline thm thy =
22033
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22020
diff changeset
   711
  (map_exec_purge NONE o map_preproc o apfst o apfst) (fold (remove eq_thm) (CodegenFunc.mk_rew thm)) thy ;
20844
6792583aa463 changed preprocessing framework
haftmann
parents: 20704
diff changeset
   712
22050
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   713
fun add_inline_proc (name, f) =
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   714
  (map_exec_purge NONE o map_preproc o apfst o apsnd) (AList.update (op =) (name, (serial (), f)));
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   715
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   716
fun del_inline_proc name =
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   717
  (map_exec_purge NONE o map_preproc o apfst o apsnd) (AList.delete (op =) name);
20844
6792583aa463 changed preprocessing framework
haftmann
parents: 20704
diff changeset
   718
22050
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   719
fun add_preproc (name, f) =
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   720
  (map_exec_purge NONE o map_preproc o apsnd) (AList.update (op =) (name, (serial (), f)));
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   721
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   722
fun del_preproc name =
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   723
  (map_exec_purge NONE o map_preproc o apsnd) (AList.delete (op =) name);
20844
6792583aa463 changed preprocessing framework
haftmann
parents: 20704
diff changeset
   724
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
6792583aa463 changed preprocessing framework
haftmann
parents: 20704
diff changeset
   742
        val thms' = f thy thms;
22033
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22020
diff changeset
   743
        val c = (CodegenConsts.norm_of_typ thy o fst o CodegenFunc.dest_func) thm;
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22020
diff changeset
   744
      in (certify_const thy c o map CodegenFunc.mk_head) thms' end;
20844
6792583aa463 changed preprocessing framework
haftmann
parents: 20704
diff changeset
   745
6792583aa463 changed preprocessing framework
haftmann
parents: 20704
diff changeset
   746
fun cmp_thms thy =
22033
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22020
diff changeset
   747
  make_ord (fn (thm1, thm2) => not (Sign.typ_instance thy (CodegenFunc.typ_func thm1, CodegenFunc.typ_func thm2)));
20844
6792583aa463 changed preprocessing framework
haftmann
parents: 20704
diff changeset
   748
6792583aa463 changed preprocessing framework
haftmann
parents: 20704
diff changeset
   749
fun rhs_conv conv thm =
6792583aa463 changed preprocessing framework
haftmann
parents: 20704
diff changeset
   750
  let
6792583aa463 changed preprocessing framework
haftmann
parents: 20704
diff changeset
   751
    val thm' = (conv o snd o Drule.dest_equals o Thm.cprop_of) thm;
6792583aa463 changed preprocessing framework
haftmann
parents: 20704
diff changeset
   752
  in Thm.transitive thm thm' end
6792583aa463 changed preprocessing framework
haftmann
parents: 20704
diff changeset
   753
6792583aa463 changed preprocessing framework
haftmann
parents: 20704
diff changeset
   754
in
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   755
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   756
fun preprocess thy thms =
20844
6792583aa463 changed preprocessing framework
haftmann
parents: 20704
diff changeset
   757
  thms
22050
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   758
  |> 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
   759
  |> map (CodegenFunc.rewrite_func ((#inlines o the_preproc o get_exec) thy))
22050
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   760
  |> fold (fn (_, (_, f)) => apply_inline_proc thy f) ((#inline_procs o the_preproc o get_exec) thy)
21835
84fd5de0691c whitespace correction
haftmann
parents: 21708
diff changeset
   761
(*FIXME - must check: rewrite rule, function equation, proper constant |> map (snd o check_func false thy) *)
20844
6792583aa463 changed preprocessing framework
haftmann
parents: 20704
diff changeset
   762
  |> sort (cmp_thms thy)
22033
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22020
diff changeset
   763
  |> common_typ_funcs;
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   764
22033
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22020
diff changeset
   765
fun preprocess_cterm ct =
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22020
diff changeset
   766
  let
22184
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   767
    val thy = Thm.theory_of_cterm ct;
22033
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22020
diff changeset
   768
  in
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22020
diff changeset
   769
    ct
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22020
diff changeset
   770
    |> Thm.reflexive
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22020
diff changeset
   771
    |> fold (rhs_conv o MetaSimplifier.rewrite false o single)
22184
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   772
        ((#inlines o the_preproc o get_exec) thy)
22050
859e5784c58c named preprocessorts
haftmann
parents: 22033
diff changeset
   773
    |> 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
   774
        ((#inline_procs o the_preproc o get_exec) thy)
22033
8e19bad4125f moved a lot to codegen_func.ML
haftmann
parents: 22020
diff changeset
   775
  end;
20844
6792583aa463 changed preprocessing framework
haftmann
parents: 20704
diff changeset
   776
6792583aa463 changed preprocessing framework
haftmann
parents: 20704
diff changeset
   777
end; (*local*)
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   778
22184
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   779
local
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   780
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   781
fun get_funcs thy const =
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   782
  Consttab.lookup ((the_funcs o get_exec) thy) const
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   783
  |> Option.map (Susp.force o fst)
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   784
  |> these
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   785
  |> map (Thm.transfer thy);
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   786
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   787
in
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   788
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   789
fun these_funcs thy const =
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   790
  let
22184
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   791
    fun get_prim_def_funcs (const as (c, tys)) =
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   792
      case CodegenConsts.find_def thy const
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   793
       of SOME (_, thm) =>
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   794
            thm
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   795
            |> Thm.transfer thy
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   796
            |> gen_mk_func_typ false
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   797
            |> map (CodegenFunc.expand_eta ~1 o snd)
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   798
        | NONE => []
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   799
    fun drop_refl thy = filter_out (is_equal o Term.fast_term_ord o Logic.dest_equals
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   800
      o ObjectLogic.drop_judgment thy o Drule.plain_prop_of);
22184
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   801
    val funcs = case get_funcs thy const
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   802
     of [] => get_prim_def_funcs const
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   803
    | funcs => funcs
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   804
  in
22184
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   805
    funcs
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   806
    |> preprocess thy
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   807
    |> drop_refl thy
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   808
  end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   809
22184
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   810
fun tap_typ thy const =
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   811
  case get_funcs thy const
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   812
   of thm :: _ => SOME (CodegenFunc.typ_func thm)
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   813
    | [] => NONE;
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   814
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   815
end; (*local*)
a125f38a559a added explicit maintainance of coregular code theorems for overloaded constants
haftmann
parents: 22050
diff changeset
   816
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   817
fun get_datatype thy tyco =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   818
  Symtab.lookup ((the_dtyps o get_exec) thy) tyco
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   819
  |> Option.map (fn (spec, thms) => (Susp.force thms; spec));
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   820
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   821
fun get_datatype_of_constr thy c =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   822
  Consttab.lookup ((the_dcontrs o get_exec) thy) c
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   823
  |> (Option.map o tap) (fn dtco => get_datatype thy dtco);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   824
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   825
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   826
(** code attributes **)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   827
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   828
local
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   829
  fun add_simple_attribute (name, f) =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   830
    (Codegen.add_attribute name o (Scan.succeed o Thm.declaration_attribute))
20897
3f8d2834b2c4 attribute: Context.mapping;
wenzelm
parents: 20855
diff changeset
   831
      (fn th => Context.mapping (f th) I);
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   832
in
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   833
  val _ = map (Context.add_setup o add_simple_attribute) [
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   834
    ("func", add_func),
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   835
    ("nofunc", del_func),
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   836
    ("unfold", (fn thm => Codegen.add_unfold thm #> add_inline thm)),
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   837
    ("inline", add_inline),
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   838
    ("noinline", del_inline)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   839
  ]
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   840
end; (*local*)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   841
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   842
end; (*struct*)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   843
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   844
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   845
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   846
(** type-safe interfaces for data depedent on executable content **)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   847
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   848
signature CODE_DATA_ARGS =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   849
sig
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   850
  val name: string
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   851
  type T
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   852
  val empty: T
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   853
  val merge: Pretty.pp -> T * T -> T
20937
4297a44e26ae generalized purge
haftmann
parents: 20897
diff changeset
   854
  val purge: theory option -> CodegenConsts.const list option -> T -> T
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   855
end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   856
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   857
signature CODE_DATA =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   858
sig
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   859
  type T
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   860
  val init: theory -> theory
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   861
  val get: theory -> T
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   862
  val change: theory -> (T -> T) -> T
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   863
  val change_yield: theory -> (T -> 'a * T) -> 'a * T
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   864
end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   865
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   866
functor CodeDataFun(Data: CODE_DATA_ARGS): CODE_DATA =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   867
struct
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   868
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   869
type T = Data.T;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   870
exception Data of T;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   871
fun dest (Data x) = x
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   872
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   873
val kind = CodegenData.declare Data.name (Data Data.empty)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   874
  (fn pp => fn (Data x1, Data x2) => Data (Data.merge pp (x1, x2)))
20937
4297a44e26ae generalized purge
haftmann
parents: 20897
diff changeset
   875
  (fn thy_opt => fn cs => fn Data x => Data (Data.purge thy_opt cs x));
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   876
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   877
val init = CodegenData.init kind;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   878
fun get thy = CodegenData.get kind dest ((! o snd o CodegenData.CodeData.get) thy);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   879
fun change thy f =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   880
  let
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   881
    val data_ref = (snd o CodegenData.CodeData.get) thy;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   882
    val x = (f o CodegenData.get kind dest o !) data_ref;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   883
    val data = CodegenData.put kind Data x (! data_ref);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   884
  in (data_ref := data; x) end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   885
fun change_yield thy f =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   886
  let
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   887
    val data_ref = (snd o CodegenData.CodeData.get) thy;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   888
    val (y, x) = (f o CodegenData.get kind dest o !) data_ref;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   889
    val data = CodegenData.put kind Data x (! data_ref);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   890
  in (data_ref := data; (y, x)) end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   891
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   892
end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   893
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   894
structure CodegenData : CODEGEN_DATA =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   895
struct
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   896
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   897
open CodegenData;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   898
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   899
end;