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