src/Tools/Code/code_preproc.ML
author traytel
Thu Sep 29 09:37:59 2011 +0200 (2011-09-29)
changeset 45102 7bb89635eb51
parent 44338 700008399ee5
child 45189 80cb73210612
permissions -rw-r--r--
correct coercion generation in case of unknown map functions
haftmann@37744
     1
(*  Title:      Tools/Code/code_preproc.ML
haftmann@30010
     2
    Author:     Florian Haftmann, TU Muenchen
haftmann@30010
     3
haftmann@31125
     4
Preprocessing code equations into a well-sorted system
haftmann@31125
     5
in a graph with explicit dependencies.
haftmann@30010
     6
*)
haftmann@30010
     7
haftmann@31125
     8
signature CODE_PREPROC =
haftmann@30010
     9
sig
haftmann@31125
    10
  val map_pre: (simpset -> simpset) -> theory -> theory
haftmann@31125
    11
  val map_post: (simpset -> simpset) -> theory -> theory
haftmann@32072
    12
  val add_unfold: thm -> theory -> theory
haftmann@31125
    13
  val add_functrans: string * (theory -> (thm * bool) list -> (thm * bool) list option) -> theory -> theory
haftmann@31125
    14
  val del_functrans: string -> theory -> theory
haftmann@31125
    15
  val simple_functrans: (theory -> thm list -> thm list option)
haftmann@31125
    16
    -> theory -> (thm * bool) list -> (thm * bool) list option
haftmann@34893
    17
  val preprocess_functrans: theory -> (thm * bool) list -> (thm * bool) list
haftmann@31125
    18
  val print_codeproc: theory -> unit
haftmann@31125
    19
haftmann@30947
    20
  type code_algebra
haftmann@30947
    21
  type code_graph
haftmann@34891
    22
  val cert: code_graph -> string -> Code.cert
haftmann@32873
    23
  val sortargs: code_graph -> string -> sort list
haftmann@30947
    24
  val all: code_graph -> string list
haftmann@30947
    25
  val pretty: theory -> code_graph -> Pretty.T
haftmann@39398
    26
  val obtain: bool -> theory -> string list -> term list -> code_algebra * code_graph
haftmann@41184
    27
  val dynamic_conv: theory
haftmann@38670
    28
    -> (code_algebra -> code_graph -> (string * sort) list -> term -> conv) -> conv
haftmann@41184
    29
  val dynamic_value: theory -> ((term -> term) -> 'a -> 'a)
haftmann@30947
    30
    -> (code_algebra -> code_graph -> (string * sort) list -> term -> 'a) -> term -> 'a
haftmann@41184
    31
  val static_conv: theory -> string list
haftmann@41346
    32
    -> (code_algebra -> code_graph -> (string * sort) list -> term -> conv) -> conv
haftmann@41184
    33
  val static_value: theory -> ((term -> term) -> 'a -> 'a) -> string list
haftmann@41346
    34
    -> (code_algebra -> code_graph -> (string * sort) list -> term -> 'a) -> term -> 'a
haftmann@31125
    35
haftmann@31125
    36
  val setup: theory -> theory
haftmann@30010
    37
end
haftmann@30010
    38
haftmann@31125
    39
structure Code_Preproc : CODE_PREPROC =
haftmann@30010
    40
struct
haftmann@30010
    41
haftmann@31125
    42
(** preprocessor administration **)
haftmann@31125
    43
haftmann@31125
    44
(* theory data *)
haftmann@31125
    45
haftmann@31125
    46
datatype thmproc = Thmproc of {
haftmann@31125
    47
  pre: simpset,
haftmann@31125
    48
  post: simpset,
haftmann@31125
    49
  functrans: (string * (serial * (theory -> (thm * bool) list -> (thm * bool) list option))) list
haftmann@31125
    50
};
haftmann@31125
    51
haftmann@31599
    52
fun make_thmproc ((pre, post), functrans) =
haftmann@31125
    53
  Thmproc { pre = pre, post = post, functrans = functrans };
haftmann@31125
    54
fun map_thmproc f (Thmproc { pre, post, functrans }) =
haftmann@31599
    55
  make_thmproc (f ((pre, post), functrans));
haftmann@31125
    56
fun merge_thmproc (Thmproc { pre = pre1, post = post1, functrans = functrans1 },
haftmann@31125
    57
  Thmproc { pre = pre2, post = post2, functrans = functrans2 }) =
haftmann@31125
    58
    let
haftmann@31125
    59
      val pre = Simplifier.merge_ss (pre1, pre2);
haftmann@31125
    60
      val post = Simplifier.merge_ss (post1, post2);
haftmann@34891
    61
      val functrans = AList.merge (op =) (eq_fst (op =)) (functrans1, functrans2)
haftmann@34891
    62
        handle AList.DUP => error ("Duplicate function transformer");
haftmann@31599
    63
    in make_thmproc ((pre, post), functrans) end;
haftmann@31125
    64
wenzelm@33522
    65
structure Code_Preproc_Data = Theory_Data
haftmann@31125
    66
(
haftmann@31125
    67
  type T = thmproc;
haftmann@31599
    68
  val empty = make_thmproc ((Simplifier.empty_ss, Simplifier.empty_ss), []);
wenzelm@33522
    69
  val extend = I;
wenzelm@33522
    70
  val merge = merge_thmproc;
haftmann@31125
    71
);
haftmann@31125
    72
haftmann@31125
    73
fun the_thmproc thy = case Code_Preproc_Data.get thy
haftmann@31125
    74
 of Thmproc x => x;
haftmann@31125
    75
haftmann@31125
    76
fun delete_force msg key xs =
haftmann@31125
    77
  if AList.defined (op =) xs key then AList.delete (op =) key xs
haftmann@31125
    78
  else error ("No such " ^ msg ^ ": " ^ quote key);
haftmann@31125
    79
haftmann@38669
    80
val map_data = Code_Preproc_Data.map o map_thmproc;
haftmann@31125
    81
haftmann@32072
    82
val map_pre_post = map_data o apfst;
haftmann@32072
    83
val map_pre = map_pre_post o apfst;
haftmann@32072
    84
val map_post = map_pre_post o apsnd;
haftmann@31125
    85
wenzelm@41226
    86
val add_unfold = map_pre o Simplifier.add_simp;
wenzelm@41226
    87
val del_unfold = map_pre o Simplifier.del_simp;
wenzelm@41226
    88
val add_post = map_post o Simplifier.add_simp;
wenzelm@41226
    89
val del_post = map_post o Simplifier.del_simp;
haftmann@32072
    90
haftmann@32072
    91
fun add_unfold_post raw_thm thy =
haftmann@32072
    92
  let
wenzelm@42361
    93
    val thm = Local_Defs.meta_rewrite_rule (Proof_Context.init_global thy) raw_thm;
haftmann@32072
    94
    val thm_sym = Thm.symmetric thm;
haftmann@32072
    95
  in
haftmann@32072
    96
    thy |> map_pre_post (fn (pre, post) =>
wenzelm@41226
    97
      (pre |> Simplifier.add_simp thm, post |> Simplifier.add_simp thm_sym))
haftmann@32072
    98
  end;
haftmann@32072
    99
haftmann@31125
   100
fun add_functrans (name, f) = (map_data o apsnd)
haftmann@31125
   101
  (AList.update (op =) (name, (serial (), f)));
haftmann@31125
   102
haftmann@31125
   103
fun del_functrans name = (map_data o apsnd)
haftmann@31125
   104
  (delete_force "function transformer" name);
haftmann@31125
   105
haftmann@31125
   106
haftmann@31125
   107
(* post- and preprocessing *)
haftmann@31125
   108
haftmann@39604
   109
fun no_variables_conv conv ct =
haftmann@39604
   110
  let
haftmann@39604
   111
    val cert = Thm.cterm_of (Thm.theory_of_cterm ct);
haftmann@39604
   112
    val all_vars = fold_aterms (fn t as Free _ => insert (op aconvc) (cert t)
haftmann@39604
   113
      | t as Var _ => insert (op aconvc) (cert t)
haftmann@39604
   114
      | _ => I) (Thm.term_of ct) [];
haftmann@39604
   115
    fun apply_beta var thm = Thm.combination thm (Thm.reflexive var)
haftmann@39604
   116
      |> Conv.fconv_rule (Conv.arg_conv (Conv.try_conv (Thm.beta_conversion false)))
haftmann@39604
   117
      |> Conv.fconv_rule (Conv.arg1_conv (Thm.beta_conversion false));
haftmann@39604
   118
  in
haftmann@39604
   119
    ct
haftmann@39604
   120
    |> fold_rev Thm.cabs all_vars
haftmann@39604
   121
    |> conv
haftmann@39604
   122
    |> fold apply_beta all_vars
haftmann@39604
   123
  end;
haftmann@39604
   124
haftmann@33942
   125
fun trans_conv_rule conv thm = Thm.transitive thm ((conv o Thm.rhs_of) thm);
haftmann@31125
   126
haftmann@34891
   127
fun eqn_conv conv ct =
haftmann@31957
   128
  let
haftmann@31957
   129
    fun lhs_conv ct = if can Thm.dest_comb ct
haftmann@31957
   130
      then Conv.combination_conv lhs_conv conv ct
haftmann@31957
   131
      else Conv.all_conv ct;
haftmann@34891
   132
  in Conv.combination_conv (Conv.arg_conv lhs_conv) conv ct end;
haftmann@31957
   133
haftmann@31957
   134
val rewrite_eqn = Conv.fconv_rule o eqn_conv o Simplifier.rewrite;
haftmann@31957
   135
haftmann@41189
   136
fun term_of_conv thy conv =
haftmann@31125
   137
  Thm.cterm_of thy
haftmann@41189
   138
  #> conv
haftmann@31125
   139
  #> Thm.prop_of
haftmann@31125
   140
  #> Logic.dest_equals
haftmann@31125
   141
  #> snd;
haftmann@31125
   142
haftmann@41189
   143
fun term_of_conv_resubst thy conv t =
haftmann@41189
   144
  let
haftmann@41189
   145
    val all_vars = fold_aterms (fn t as Free _ => insert (op aconv) t
haftmann@41189
   146
      | t as Var _ => insert (op aconv) t
haftmann@41189
   147
      | _ => I) t [];
haftmann@41189
   148
    val resubst = curry (Term.betapplys o swap) all_vars;
haftmann@41189
   149
  in (resubst, term_of_conv thy conv (fold_rev lambda all_vars t)) end;
haftmann@41189
   150
haftmann@41189
   151
haftmann@34893
   152
fun preprocess_functrans thy = 
haftmann@31125
   153
  let
haftmann@31125
   154
    val functrans = (map (fn (_, (_, f)) => f thy) o #functrans
haftmann@31125
   155
      o the_thmproc) thy;
haftmann@34893
   156
  in perhaps (perhaps_loop (perhaps_apply functrans)) end;
haftmann@34893
   157
haftmann@34893
   158
fun preprocess thy =
haftmann@34893
   159
  let
bulwahn@43122
   160
    val ctxt = Proof_Context.init_global thy;
wenzelm@35232
   161
    val pre = (Simplifier.global_context thy o #pre o the_thmproc) thy;
haftmann@31125
   162
  in
haftmann@34893
   163
    preprocess_functrans thy
bulwahn@43122
   164
    #> (map o apfst) (singleton (Variable.trade (K (map (rewrite_eqn pre))) ctxt)) 
haftmann@31125
   165
  end;
haftmann@31125
   166
haftmann@41189
   167
fun preprocess_conv thy =
haftmann@31125
   168
  let
wenzelm@35232
   169
    val pre = (Simplifier.global_context thy o #pre o the_thmproc) thy;
haftmann@31125
   170
  in
haftmann@41189
   171
    Simplifier.rewrite pre
haftmann@41189
   172
    #> trans_conv_rule (AxClass.unoverload_conv thy)
haftmann@31125
   173
  end;
haftmann@31125
   174
haftmann@41189
   175
fun preprocess_term thy = term_of_conv_resubst thy (preprocess_conv thy);
haftmann@39475
   176
haftmann@41189
   177
fun postprocess_conv thy =
haftmann@31125
   178
  let
wenzelm@35232
   179
    val post = (Simplifier.global_context thy o #post o the_thmproc) thy;
haftmann@31125
   180
  in
haftmann@41189
   181
    AxClass.overload_conv thy
haftmann@41189
   182
    #> trans_conv_rule (Simplifier.rewrite post)
haftmann@31125
   183
  end;
haftmann@31125
   184
haftmann@32544
   185
fun postprocess_term thy = term_of_conv thy (postprocess_conv thy);
haftmann@31125
   186
haftmann@31125
   187
fun print_codeproc thy =
haftmann@31125
   188
  let
wenzelm@42361
   189
    val ctxt = Proof_Context.init_global thy;
haftmann@31125
   190
    val pre = (#pre o the_thmproc) thy;
haftmann@31125
   191
    val post = (#post o the_thmproc) thy;
haftmann@31125
   192
    val functrans = (map fst o #functrans o the_thmproc) thy;
haftmann@31125
   193
  in
haftmann@31125
   194
    (Pretty.writeln o Pretty.chunks) [
haftmann@31125
   195
      Pretty.block [
haftmann@31125
   196
        Pretty.str "preprocessing simpset:",
haftmann@31125
   197
        Pretty.fbrk,
haftmann@31125
   198
        Simplifier.pretty_ss ctxt pre
haftmann@31125
   199
      ],
haftmann@31125
   200
      Pretty.block [
haftmann@31125
   201
        Pretty.str "postprocessing simpset:",
haftmann@31125
   202
        Pretty.fbrk,
haftmann@31125
   203
        Simplifier.pretty_ss ctxt post
haftmann@31125
   204
      ],
haftmann@31125
   205
      Pretty.block (
haftmann@31125
   206
        Pretty.str "function transformers:"
haftmann@31125
   207
        :: Pretty.fbrk
haftmann@31125
   208
        :: (Pretty.fbreaks o map Pretty.str) functrans
haftmann@31125
   209
      )
haftmann@31125
   210
    ]
haftmann@31125
   211
  end;
haftmann@31125
   212
haftmann@31125
   213
fun simple_functrans f thy eqns = case f thy (map fst eqns)
haftmann@31125
   214
 of SOME thms' => SOME (map (rpair (forall snd eqns)) thms')
haftmann@31125
   215
  | NONE => NONE;
haftmann@31125
   216
haftmann@31125
   217
haftmann@31125
   218
(** sort algebra and code equation graph types **)
haftmann@30010
   219
haftmann@30947
   220
type code_algebra = (sort -> sort) * Sorts.algebra;
haftmann@34891
   221
type code_graph = ((string * sort) list * Code.cert) Graph.T;
haftmann@30010
   222
haftmann@39604
   223
fun get_node eqngr const = Graph.get_node eqngr const
haftmann@39604
   224
  handle Graph.UNDEF _ => error ("No such constant in code equation graph: " ^ quote const);
haftmann@39604
   225
haftmann@39604
   226
fun cert eqngr = snd o get_node eqngr;
haftmann@39604
   227
fun sortargs eqngr = map snd o fst o get_node eqngr;
haftmann@30010
   228
fun all eqngr = Graph.keys eqngr;
haftmann@30010
   229
haftmann@30010
   230
fun pretty thy eqngr =
haftmann@30010
   231
  AList.make (snd o Graph.get_node eqngr) (Graph.keys eqngr)
haftmann@31156
   232
  |> (map o apfst) (Code.string_of_const thy)
haftmann@30010
   233
  |> sort (string_ord o pairself fst)
haftmann@34895
   234
  |> map (fn (s, cert) => (Pretty.block o Pretty.fbreaks) (Pretty.str s :: Code.pretty_cert thy cert))
haftmann@30010
   235
  |> Pretty.chunks;
haftmann@30010
   236
haftmann@30010
   237
haftmann@30010
   238
(** the Waisenhaus algorithm **)
haftmann@30010
   239
haftmann@30010
   240
(* auxiliary *)
haftmann@30010
   241
haftmann@30942
   242
fun is_proper_class thy = can (AxClass.get_info thy);
haftmann@30942
   243
haftmann@30010
   244
fun complete_proper_sort thy =
haftmann@30942
   245
  Sign.complete_sort thy #> filter (is_proper_class thy);
haftmann@30010
   246
haftmann@30029
   247
fun inst_params thy tyco =
haftmann@30010
   248
  map (fn (c, _) => AxClass.param_of_inst thy (c, tyco))
haftmann@30029
   249
    o maps (#params o AxClass.get_info thy);
haftmann@30010
   250
haftmann@30010
   251
haftmann@30010
   252
(* data structures *)
haftmann@30010
   253
haftmann@30010
   254
datatype const = Fun of string | Inst of class * string;
haftmann@30010
   255
haftmann@30010
   256
fun const_ord (Fun c1, Fun c2) = fast_string_ord (c1, c2)
haftmann@30010
   257
  | const_ord (Inst class_tyco1, Inst class_tyco2) =
haftmann@30010
   258
      prod_ord fast_string_ord fast_string_ord (class_tyco1, class_tyco2)
haftmann@30010
   259
  | const_ord (Fun _, Inst _) = LESS
haftmann@30010
   260
  | const_ord (Inst _, Fun _) = GREATER;
haftmann@30010
   261
haftmann@30010
   262
type var = const * int;
haftmann@30010
   263
haftmann@30010
   264
structure Vargraph =
wenzelm@31971
   265
  Graph(type key = var val ord = prod_ord const_ord int_ord);
haftmann@30010
   266
haftmann@30054
   267
datatype styp = Tyco of string * styp list | Var of var | Free;
haftmann@30054
   268
haftmann@30054
   269
fun styp_of c_lhs (Type (tyco, tys)) = Tyco (tyco, map (styp_of c_lhs) tys)
haftmann@30054
   270
  | styp_of c_lhs (TFree (v, _)) = case c_lhs
haftmann@30054
   271
     of SOME (c, lhs) => Var (Fun c, find_index (fn (v', _) => v = v') lhs)
haftmann@30054
   272
      | NONE => Free;
haftmann@30010
   273
haftmann@30054
   274
type vardeps_data = ((string * styp list) list * class list) Vargraph.T
haftmann@34891
   275
  * (((string * sort) list * Code.cert) Symtab.table
haftmann@30054
   276
    * (class * string) list);
haftmann@30010
   277
haftmann@30054
   278
val empty_vardeps_data : vardeps_data =
haftmann@30054
   279
  (Vargraph.empty, (Symtab.empty, []));
haftmann@30054
   280
haftmann@30876
   281
haftmann@30054
   282
(* retrieving equations and instances from the background context *)
haftmann@30010
   283
haftmann@30010
   284
fun obtain_eqns thy eqngr c =
haftmann@30010
   285
  case try (Graph.get_node eqngr) c
haftmann@34891
   286
   of SOME (lhs, cert) => ((lhs, []), cert)
haftmann@30010
   287
    | NONE => let
haftmann@34891
   288
        val cert = Code.get_cert thy (preprocess thy) c;
haftmann@35224
   289
        val (lhs, rhss) = Code.typargs_deps_of_cert thy cert;
haftmann@34891
   290
      in ((lhs, rhss), cert) end;
haftmann@30010
   291
haftmann@30010
   292
fun obtain_instance thy arities (inst as (class, tyco)) =
haftmann@30010
   293
  case AList.lookup (op =) arities inst
haftmann@30010
   294
   of SOME classess => (classess, ([], []))
haftmann@30010
   295
    | NONE => let
haftmann@30029
   296
        val all_classes = complete_proper_sort thy [class];
haftmann@37384
   297
        val super_classes = remove (op =) class all_classes;
haftmann@30010
   298
        val classess = map (complete_proper_sort thy)
haftmann@30010
   299
          (Sign.arity_sorts thy tyco [class]);
haftmann@30029
   300
        val inst_params = inst_params thy tyco all_classes;
haftmann@37384
   301
      in (classess, (super_classes, inst_params)) end;
haftmann@30010
   302
haftmann@30054
   303
haftmann@30054
   304
(* computing instantiations *)
haftmann@30054
   305
haftmann@30010
   306
fun add_classes thy arities eqngr c_k new_classes vardeps_data =
haftmann@30010
   307
  let
haftmann@30010
   308
    val (styps, old_classes) = Vargraph.get_node (fst vardeps_data) c_k;
haftmann@30010
   309
    val diff_classes = new_classes |> subtract (op =) old_classes;
haftmann@30010
   310
  in if null diff_classes then vardeps_data
haftmann@30010
   311
  else let
wenzelm@44338
   312
    val c_ks = Vargraph.immediate_succs (fst vardeps_data) c_k |> insert (op =) c_k;
haftmann@30010
   313
  in
haftmann@30010
   314
    vardeps_data
haftmann@30010
   315
    |> (apfst o Vargraph.map_node c_k o apsnd) (append diff_classes)
haftmann@31125
   316
    |> fold (fn styp => fold (ensure_typmatch_inst thy arities eqngr styp) new_classes) styps
haftmann@30010
   317
    |> fold (fn c_k => add_classes thy arities eqngr c_k diff_classes) c_ks
haftmann@30010
   318
  end end
haftmann@37384
   319
and add_styp thy arities eqngr c_k new_tyco_styps vardeps_data =
haftmann@30010
   320
  let
haftmann@37384
   321
    val (old_tyco_stypss, classes) = Vargraph.get_node (fst vardeps_data) c_k;
haftmann@37384
   322
  in if member (op =) old_tyco_stypss new_tyco_styps then vardeps_data
haftmann@30010
   323
  else
haftmann@30010
   324
    vardeps_data
haftmann@37384
   325
    |> (apfst o Vargraph.map_node c_k o apfst) (cons new_tyco_styps)
haftmann@37384
   326
    |> fold (ensure_typmatch_inst thy arities eqngr new_tyco_styps) classes
haftmann@30010
   327
  end
haftmann@30010
   328
and add_dep thy arities eqngr c_k c_k' vardeps_data =
haftmann@30010
   329
  let
haftmann@30010
   330
    val (_, classes) = Vargraph.get_node (fst vardeps_data) c_k;
haftmann@30010
   331
  in
haftmann@30010
   332
    vardeps_data
haftmann@30010
   333
    |> add_classes thy arities eqngr c_k' classes
haftmann@30010
   334
    |> apfst (Vargraph.add_edge (c_k, c_k'))
haftmann@30010
   335
  end
haftmann@31125
   336
and ensure_typmatch_inst thy arities eqngr (tyco, styps) class vardeps_data =
haftmann@30010
   337
  if can (Sign.arity_sorts thy tyco) [class]
haftmann@30010
   338
  then vardeps_data
haftmann@31125
   339
    |> ensure_inst thy arities eqngr (class, tyco)
haftmann@30010
   340
    |> fold_index (fn (k, styp) =>
haftmann@31125
   341
         ensure_typmatch thy arities eqngr styp (Inst (class, tyco), k)) styps
haftmann@30010
   342
  else vardeps_data (*permissive!*)
haftmann@31125
   343
and ensure_inst thy arities eqngr (inst as (class, tyco)) (vardeps_data as (_, (_, insts))) =
haftmann@30054
   344
  if member (op =) insts inst then vardeps_data
haftmann@30054
   345
  else let
haftmann@37384
   346
    val (classess, (super_classes, inst_params)) =
haftmann@30010
   347
      obtain_instance thy arities inst;
haftmann@30010
   348
  in
haftmann@30010
   349
    vardeps_data
haftmann@30054
   350
    |> (apsnd o apsnd) (insert (op =) inst)
haftmann@30083
   351
    |> fold_index (fn (k, _) =>
haftmann@30083
   352
         apfst (Vargraph.new_node ((Inst (class, tyco), k), ([] ,[])))) classess
haftmann@37384
   353
    |> fold (fn super_class => ensure_inst thy arities eqngr (super_class, tyco)) super_classes
haftmann@31125
   354
    |> fold (ensure_fun thy arities eqngr) inst_params
haftmann@30010
   355
    |> fold_index (fn (k, classes) =>
haftmann@30075
   356
         add_classes thy arities eqngr (Inst (class, tyco), k) classes
haftmann@37384
   357
         #> fold (fn super_class =>
haftmann@37384
   358
             add_dep thy arities eqngr (Inst (super_class, tyco), k)
haftmann@37384
   359
             (Inst (class, tyco), k)) super_classes
haftmann@30010
   360
         #> fold (fn inst_param =>
haftmann@30010
   361
             add_dep thy arities eqngr (Fun inst_param, k)
haftmann@30010
   362
             (Inst (class, tyco), k)
haftmann@30010
   363
             ) inst_params
haftmann@30010
   364
         ) classess
haftmann@30010
   365
  end
haftmann@31125
   366
and ensure_typmatch thy arities eqngr (Tyco tyco_styps) c_k vardeps_data =
haftmann@30054
   367
      vardeps_data
haftmann@30054
   368
      |> add_styp thy arities eqngr c_k tyco_styps
haftmann@31125
   369
  | ensure_typmatch thy arities eqngr (Var c_k') c_k vardeps_data =
haftmann@30054
   370
      vardeps_data
haftmann@30054
   371
      |> add_dep thy arities eqngr c_k c_k'
haftmann@31125
   372
  | ensure_typmatch thy arities eqngr Free c_k vardeps_data =
haftmann@30054
   373
      vardeps_data
haftmann@31125
   374
and ensure_rhs thy arities eqngr (c', styps) vardeps_data =
haftmann@30054
   375
  vardeps_data
haftmann@31125
   376
  |> ensure_fun thy arities eqngr c'
haftmann@30054
   377
  |> fold_index (fn (k, styp) =>
haftmann@31125
   378
       ensure_typmatch thy arities eqngr styp (Fun c', k)) styps
haftmann@31125
   379
and ensure_fun thy arities eqngr c (vardeps_data as (_, (eqntab, _))) =
haftmann@30054
   380
  if Symtab.defined eqntab c then vardeps_data
haftmann@30054
   381
  else let
haftmann@30010
   382
    val ((lhs, rhss), eqns) = obtain_eqns thy eqngr c;
haftmann@30054
   383
    val rhss' = (map o apsnd o map) (styp_of (SOME (c, lhs))) rhss;
haftmann@30010
   384
  in
haftmann@30010
   385
    vardeps_data
haftmann@30054
   386
    |> (apsnd o apfst) (Symtab.update_new (c, (lhs, eqns)))
haftmann@30083
   387
    |> fold_index (fn (k, _) =>
haftmann@30083
   388
         apfst (Vargraph.new_node ((Fun c, k), ([] ,[])))) lhs
haftmann@30010
   389
    |> fold_index (fn (k, (_, sort)) =>
haftmann@30083
   390
         add_classes thy arities eqngr (Fun c, k) (complete_proper_sort thy sort)) lhs
haftmann@31125
   391
    |> fold (ensure_rhs thy arities eqngr) rhss'
haftmann@30054
   392
  end;
haftmann@30010
   393
haftmann@30010
   394
haftmann@30010
   395
(* applying instantiations *)
haftmann@30010
   396
haftmann@30010
   397
fun dicts_of thy (proj_sort, algebra) (T, sort) =
haftmann@30010
   398
  let
haftmann@30010
   399
    fun class_relation (x, _) _ = x;
wenzelm@36102
   400
    fun type_constructor (tyco, _) xs class =
haftmann@30054
   401
      inst_params thy tyco (Sorts.complete_sort algebra [class])
haftmann@30054
   402
        @ (maps o maps) fst xs;
haftmann@30010
   403
    fun type_variable (TFree (_, sort)) = map (pair []) (proj_sort sort);
haftmann@30010
   404
  in
wenzelm@32795
   405
    flat (Sorts.of_sort_derivation algebra
wenzelm@36102
   406
      { class_relation = K class_relation, type_constructor = type_constructor,
haftmann@30010
   407
        type_variable = type_variable } (T, proj_sort sort)
haftmann@30010
   408
       handle Sorts.CLASS_ERROR _ => [] (*permissive!*))
haftmann@30010
   409
  end;
haftmann@30010
   410
haftmann@30054
   411
fun add_arity thy vardeps (class, tyco) =
haftmann@33063
   412
  AList.default (op =) ((class, tyco),
haftmann@34891
   413
    map_range (fn k => (snd o Vargraph.get_node vardeps) (Inst (class, tyco), k))
haftmann@34891
   414
      (Sign.arity_number thy tyco));
haftmann@30010
   415
haftmann@34891
   416
fun add_cert thy vardeps (c, (proto_lhs, proto_cert)) (rhss, eqngr) =
haftmann@30058
   417
  if can (Graph.get_node eqngr) c then (rhss, eqngr)
haftmann@30058
   418
  else let
haftmann@30010
   419
    val lhs = map_index (fn (k, (v, _)) =>
haftmann@30010
   420
      (v, snd (Vargraph.get_node vardeps (Fun c, k)))) proto_lhs;
haftmann@38291
   421
    val cert = Code.constrain_cert thy (map (Sign.minimize_sort thy o snd) lhs) proto_cert;
haftmann@35224
   422
    val (vs, rhss') = Code.typargs_deps_of_cert thy cert;
haftmann@34891
   423
    val eqngr' = Graph.new_node (c, (vs, cert)) eqngr;
haftmann@30054
   424
  in (map (pair c) rhss' @ rhss, eqngr') end;
haftmann@30010
   425
haftmann@32873
   426
fun extend_arities_eqngr thy cs ts (arities, (eqngr : code_graph)) =
haftmann@30010
   427
  let
haftmann@30942
   428
    val cs_rhss = (fold o fold_aterms) (fn Const (c_ty as (c, _)) =>
haftmann@30942
   429
      insert (op =) (c, (map (styp_of NONE) o Sign.const_typargs thy) c_ty) | _ => I) ts [];
haftmann@30054
   430
    val (vardeps, (eqntab, insts)) = empty_vardeps_data
haftmann@31125
   431
      |> fold (ensure_fun thy arities eqngr) cs
haftmann@31125
   432
      |> fold (ensure_rhs thy arities eqngr) cs_rhss;
haftmann@30054
   433
    val arities' = fold (add_arity thy vardeps) insts arities;
wenzelm@42387
   434
    val algebra = Sorts.subalgebra (Syntax.init_pretty_global thy) (is_proper_class thy)
haftmann@30064
   435
      (AList.lookup (op =) arities') (Sign.classes_of thy);
haftmann@34891
   436
    val (rhss, eqngr') = Symtab.fold (add_cert thy vardeps) eqntab ([], eqngr);
haftmann@30942
   437
    fun deps_of (c, rhs) = c :: maps (dicts_of thy algebra)
haftmann@32873
   438
      (rhs ~~ sortargs eqngr' c);
haftmann@30054
   439
    val eqngr'' = fold (fn (c, rhs) => fold
haftmann@30054
   440
      (curry Graph.add_edge c) (deps_of rhs)) rhss eqngr';
haftmann@30942
   441
  in (algebra, (arities', eqngr'')) end;
haftmann@30010
   442
haftmann@30010
   443
haftmann@31125
   444
(** store for preprocessed arities and code equations **)
haftmann@30010
   445
haftmann@34173
   446
structure Wellsorted = Code_Data
haftmann@30010
   447
(
haftmann@30947
   448
  type T = ((string * class) * sort list) list * code_graph;
haftmann@30010
   449
  val empty = ([], Graph.empty);
haftmann@30010
   450
);
haftmann@30010
   451
haftmann@30010
   452
haftmann@31125
   453
(** retrieval and evaluation interfaces **)
haftmann@30010
   454
haftmann@39398
   455
fun obtain ignore_cache thy consts ts = apsnd snd
haftmann@39398
   456
  (Wellsorted.change_yield (if ignore_cache then NONE else SOME thy) (extend_arities_eqngr thy consts ts));
haftmann@38670
   457
haftmann@38670
   458
fun dest_cterm ct = let val t = Thm.term_of ct in (Term.add_tfrees t [], t) end;
haftmann@30010
   459
haftmann@41184
   460
fun dynamic_conv thy conv = no_variables_conv (fn ct =>
haftmann@30942
   461
  let
haftmann@39475
   462
    val thm1 = preprocess_conv thy ct;
haftmann@39475
   463
    val ct' = Thm.rhs_of thm1;
haftmann@38670
   464
    val (vs', t') = dest_cterm ct';
haftmann@30947
   465
    val consts = fold_aterms
haftmann@30947
   466
      (fn Const (c, _) => insert (op =) c | _ => I) t' [];
haftmann@39398
   467
    val (algebra', eqngr') = obtain false thy consts [t'];
haftmann@39475
   468
    val thm2 = conv algebra' eqngr' vs' t' ct';
haftmann@39475
   469
    val thm3 = postprocess_conv thy (Thm.rhs_of thm2);
haftmann@39475
   470
  in
haftmann@39475
   471
    Thm.transitive thm1 (Thm.transitive thm2 thm3) handle THM _ =>
haftmann@39475
   472
      error ("could not construct evaluation proof:\n"
haftmann@39475
   473
      ^ (cat_lines o map (Display.string_of_thm_global thy)) [thm1, thm2, thm3])
haftmann@39604
   474
  end);
haftmann@30947
   475
haftmann@41184
   476
fun dynamic_value thy postproc evaluator t =
haftmann@30942
   477
  let
haftmann@39604
   478
    val (resubst, t') = preprocess_term thy t;
haftmann@39475
   479
    val vs' = Term.add_tfrees t' [];
haftmann@39475
   480
    val consts = fold_aterms
haftmann@39475
   481
      (fn Const (c, _) => insert (op =) c | _ => I) t' [];
haftmann@39475
   482
    val (algebra', eqngr') = obtain false thy consts [t'];
haftmann@39475
   483
  in
haftmann@40176
   484
    t'
haftmann@40176
   485
    |> evaluator algebra' eqngr' vs'
haftmann@39604
   486
    |> postproc (postprocess_term thy o resubst)
haftmann@39475
   487
  end;
haftmann@31125
   488
haftmann@41184
   489
fun static_conv thy consts conv =
haftmann@38670
   490
  let
haftmann@39398
   491
    val (algebra, eqngr) = obtain true thy consts [];
haftmann@41346
   492
    val conv' = conv algebra eqngr;
haftmann@39398
   493
  in
haftmann@41189
   494
    no_variables_conv ((preprocess_conv thy)
haftmann@41189
   495
      then_conv (fn ct => uncurry conv' (dest_cterm ct) ct)
haftmann@41189
   496
      then_conv (postprocess_conv thy))
haftmann@39398
   497
  end;
haftmann@38670
   498
haftmann@41184
   499
fun static_value thy postproc consts evaluator =
haftmann@39475
   500
  let
haftmann@39475
   501
    val (algebra, eqngr) = obtain true thy consts [];
haftmann@39475
   502
    val evaluator' = evaluator algebra eqngr;
haftmann@41189
   503
  in 
haftmann@41189
   504
    preprocess_term thy
haftmann@41189
   505
    #-> (fn resubst => fn t => t
haftmann@41346
   506
      |> evaluator' (Term.add_tfrees t [])
haftmann@41189
   507
      |> postproc (postprocess_term thy o resubst))
haftmann@39475
   508
  end;
haftmann@39475
   509
haftmann@31125
   510
haftmann@31125
   511
(** setup **)
haftmann@31125
   512
haftmann@31125
   513
val setup = 
haftmann@31125
   514
  let
haftmann@31125
   515
    fun mk_attribute f = Thm.declaration_attribute (fn thm => Context.mapping (f thm) I);
haftmann@32072
   516
    fun add_del_attribute_parser add del =
haftmann@31998
   517
      Attrib.add_del (mk_attribute add) (mk_attribute del);
haftmann@32072
   518
    fun both f g thm = f thm #> g thm;
haftmann@31125
   519
  in
haftmann@32072
   520
    Attrib.setup @{binding code_unfold} (add_del_attribute_parser 
haftmann@32072
   521
       (both Codegen.add_unfold add_unfold) (both Codegen.del_unfold del_unfold))
haftmann@32072
   522
        "preprocessing equations for code generators"
haftmann@32072
   523
    #> Attrib.setup @{binding code_inline} (add_del_attribute_parser add_unfold del_unfold)
haftmann@31998
   524
        "preprocessing equations for code generator"
haftmann@32072
   525
    #> Attrib.setup @{binding code_post} (add_del_attribute_parser add_post del_post)
haftmann@31998
   526
        "postprocessing equations for code generator"
haftmann@32072
   527
    #> Attrib.setup @{binding code_unfold_post} (Scan.succeed (mk_attribute add_unfold_post))
haftmann@32072
   528
        "pre- and postprocessing equations for code generator"
haftmann@31125
   529
  end;
haftmann@31125
   530
haftmann@31125
   531
val _ =
wenzelm@36960
   532
  Outer_Syntax.improper_command "print_codeproc" "print code preprocessor setup"
wenzelm@36960
   533
  Keyword.diag (Scan.succeed
haftmann@31125
   534
      (Toplevel.no_timing o Toplevel.unknown_theory o Toplevel.keep
haftmann@31125
   535
        (print_codeproc o Toplevel.theory_of)));
haftmann@30010
   536
haftmann@30010
   537
end; (*struct*)