src/Tools/Code/code_preproc.ML
author haftmann
Mon Jan 04 14:09:56 2010 +0100 (2010-01-04)
changeset 34244 03f8dcab55f3
parent 34173 458ced35abb8
child 34251 cd642bb91f64
permissions -rw-r--r--
code cache without copy; tuned
haftmann@31125
     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@31125
    17
  val print_codeproc: theory -> unit
haftmann@31125
    18
haftmann@30947
    19
  type code_algebra
haftmann@30947
    20
  type code_graph
haftmann@30947
    21
  val eqns: code_graph -> string -> (thm * bool) list
haftmann@32873
    22
  val sortargs: code_graph -> string -> sort list
haftmann@30947
    23
  val all: code_graph -> string list
haftmann@30947
    24
  val pretty: theory -> code_graph -> Pretty.T
haftmann@30947
    25
  val obtain: theory -> string list -> term list -> code_algebra * code_graph
haftmann@32123
    26
  val eval_conv: theory
haftmann@30947
    27
    -> (code_algebra -> code_graph -> (string * sort) list -> term -> cterm -> thm) -> cterm -> thm
haftmann@32123
    28
  val eval: theory -> ((term -> term) -> 'a -> 'a)
haftmann@30947
    29
    -> (code_algebra -> code_graph -> (string * sort) list -> term -> 'a) -> term -> 'a
haftmann@31125
    30
haftmann@31125
    31
  val setup: theory -> theory
haftmann@30010
    32
end
haftmann@30010
    33
haftmann@31125
    34
structure Code_Preproc : CODE_PREPROC =
haftmann@30010
    35
struct
haftmann@30010
    36
haftmann@31125
    37
(** preprocessor administration **)
haftmann@31125
    38
haftmann@31125
    39
(* theory data *)
haftmann@31125
    40
haftmann@31125
    41
datatype thmproc = Thmproc of {
haftmann@31125
    42
  pre: simpset,
haftmann@31125
    43
  post: simpset,
haftmann@31125
    44
  functrans: (string * (serial * (theory -> (thm * bool) list -> (thm * bool) list option))) list
haftmann@31125
    45
};
haftmann@31125
    46
haftmann@31599
    47
fun make_thmproc ((pre, post), functrans) =
haftmann@31125
    48
  Thmproc { pre = pre, post = post, functrans = functrans };
haftmann@31125
    49
fun map_thmproc f (Thmproc { pre, post, functrans }) =
haftmann@31599
    50
  make_thmproc (f ((pre, post), functrans));
haftmann@31125
    51
fun merge_thmproc (Thmproc { pre = pre1, post = post1, functrans = functrans1 },
haftmann@31125
    52
  Thmproc { pre = pre2, post = post2, functrans = functrans2 }) =
haftmann@31125
    53
    let
haftmann@31125
    54
      val pre = Simplifier.merge_ss (pre1, pre2);
haftmann@31125
    55
      val post = Simplifier.merge_ss (post1, post2);
haftmann@31125
    56
      val functrans = AList.merge (op =) (eq_fst (op =)) (functrans1, functrans2);
wenzelm@33699
    57
        (* FIXME handle AList.DUP (!?) *)
haftmann@31599
    58
    in make_thmproc ((pre, post), functrans) end;
haftmann@31125
    59
wenzelm@33522
    60
structure Code_Preproc_Data = Theory_Data
haftmann@31125
    61
(
haftmann@31125
    62
  type T = thmproc;
haftmann@31599
    63
  val empty = make_thmproc ((Simplifier.empty_ss, Simplifier.empty_ss), []);
wenzelm@33522
    64
  val extend = I;
wenzelm@33522
    65
  val merge = merge_thmproc;
haftmann@31125
    66
);
haftmann@31125
    67
haftmann@31125
    68
fun the_thmproc thy = case Code_Preproc_Data.get thy
haftmann@31125
    69
 of Thmproc x => x;
haftmann@31125
    70
haftmann@31125
    71
fun delete_force msg key xs =
haftmann@31125
    72
  if AList.defined (op =) xs key then AList.delete (op =) key xs
haftmann@31125
    73
  else error ("No such " ^ msg ^ ": " ^ quote key);
haftmann@31125
    74
haftmann@32544
    75
fun map_data f = Code.purge_data
haftmann@32544
    76
  #> (Code_Preproc_Data.map o map_thmproc) f;
haftmann@31125
    77
haftmann@32072
    78
val map_pre_post = map_data o apfst;
haftmann@32072
    79
val map_pre = map_pre_post o apfst;
haftmann@32072
    80
val map_post = map_pre_post o apsnd;
haftmann@31125
    81
haftmann@32072
    82
val add_unfold = map_pre o MetaSimplifier.add_simp;
haftmann@32072
    83
val del_unfold = map_pre o MetaSimplifier.del_simp;
haftmann@31125
    84
val add_post = map_post o MetaSimplifier.add_simp;
haftmann@31125
    85
val del_post = map_post o MetaSimplifier.del_simp;
haftmann@32072
    86
haftmann@32072
    87
fun add_unfold_post raw_thm thy =
haftmann@32072
    88
  let
haftmann@32072
    89
    val thm = LocalDefs.meta_rewrite_rule (ProofContext.init thy) raw_thm;
haftmann@32072
    90
    val thm_sym = Thm.symmetric thm;
haftmann@32072
    91
  in
haftmann@32072
    92
    thy |> map_pre_post (fn (pre, post) =>
haftmann@32072
    93
      (pre |> MetaSimplifier.add_simp thm, post |> MetaSimplifier.del_simp thm_sym))
haftmann@32072
    94
  end;
haftmann@32072
    95
haftmann@31125
    96
fun add_functrans (name, f) = (map_data o apsnd)
haftmann@31125
    97
  (AList.update (op =) (name, (serial (), f)));
haftmann@31125
    98
haftmann@31125
    99
fun del_functrans name = (map_data o apsnd)
haftmann@31125
   100
  (delete_force "function transformer" name);
haftmann@31125
   101
haftmann@31125
   102
haftmann@31125
   103
(* post- and preprocessing *)
haftmann@31125
   104
haftmann@31125
   105
fun apply_functrans thy c _ [] = []
haftmann@31125
   106
  | apply_functrans thy c [] eqns = eqns
haftmann@31125
   107
  | apply_functrans thy c functrans eqns = eqns
haftmann@31125
   108
      |> perhaps (perhaps_loop (perhaps_apply functrans))
haftmann@32349
   109
      |> Code.assert_eqns_const thy c
haftmann@32349
   110
      (*FIXME in future, the check here should be more accurate wrt. type schemes
haftmann@32349
   111
      -- perhaps by means of upcoming code certificates with a corresponding
haftmann@32349
   112
         preprocessor protocol*);
haftmann@31125
   113
haftmann@33942
   114
fun trans_conv_rule conv thm = Thm.transitive thm ((conv o Thm.rhs_of) thm);
haftmann@31125
   115
haftmann@31957
   116
fun eqn_conv conv =
haftmann@31957
   117
  let
haftmann@31957
   118
    fun lhs_conv ct = if can Thm.dest_comb ct
haftmann@31957
   119
      then Conv.combination_conv lhs_conv conv ct
haftmann@31957
   120
      else Conv.all_conv ct;
haftmann@31957
   121
  in Conv.combination_conv (Conv.arg_conv lhs_conv) conv end;
haftmann@31957
   122
haftmann@31957
   123
val rewrite_eqn = Conv.fconv_rule o eqn_conv o Simplifier.rewrite;
haftmann@31957
   124
haftmann@31125
   125
fun term_of_conv thy f =
haftmann@31125
   126
  Thm.cterm_of thy
haftmann@31125
   127
  #> f
haftmann@31125
   128
  #> Thm.prop_of
haftmann@31125
   129
  #> Logic.dest_equals
haftmann@31125
   130
  #> snd;
haftmann@31125
   131
haftmann@31125
   132
fun preprocess thy c eqns =
haftmann@31125
   133
  let
haftmann@31125
   134
    val pre = (Simplifier.theory_context thy o #pre o the_thmproc) thy;
haftmann@31125
   135
    val functrans = (map (fn (_, (_, f)) => f thy) o #functrans
haftmann@31125
   136
      o the_thmproc) thy;
haftmann@31125
   137
  in
haftmann@31125
   138
    eqns
haftmann@31125
   139
    |> apply_functrans thy c functrans
haftmann@31957
   140
    |> (map o apfst) (rewrite_eqn pre)
haftmann@31125
   141
    |> (map o apfst) (AxClass.unoverload thy)
haftmann@31125
   142
    |> map (Code.assert_eqn thy)
haftmann@31125
   143
  end;
haftmann@31125
   144
haftmann@31125
   145
fun preprocess_conv thy ct =
haftmann@31125
   146
  let
haftmann@31125
   147
    val pre = (Simplifier.theory_context thy o #pre o the_thmproc) thy;
haftmann@31125
   148
  in
haftmann@31125
   149
    ct
haftmann@31125
   150
    |> Simplifier.rewrite pre
haftmann@33942
   151
    |> trans_conv_rule (AxClass.unoverload_conv thy)
haftmann@31125
   152
  end;
haftmann@31125
   153
haftmann@31125
   154
fun postprocess_conv thy ct =
haftmann@31125
   155
  let
haftmann@31125
   156
    val post = (Simplifier.theory_context thy o #post o the_thmproc) thy;
haftmann@31125
   157
  in
haftmann@31125
   158
    ct
haftmann@31125
   159
    |> AxClass.overload_conv thy
haftmann@33942
   160
    |> trans_conv_rule (Simplifier.rewrite post)
haftmann@31125
   161
  end;
haftmann@31125
   162
haftmann@32544
   163
fun postprocess_term thy = term_of_conv thy (postprocess_conv thy);
haftmann@31125
   164
haftmann@31125
   165
fun print_codeproc thy =
haftmann@31125
   166
  let
haftmann@31125
   167
    val ctxt = ProofContext.init thy;
haftmann@31125
   168
    val pre = (#pre o the_thmproc) thy;
haftmann@31125
   169
    val post = (#post o the_thmproc) thy;
haftmann@31125
   170
    val functrans = (map fst o #functrans o the_thmproc) thy;
haftmann@31125
   171
  in
haftmann@31125
   172
    (Pretty.writeln o Pretty.chunks) [
haftmann@31125
   173
      Pretty.block [
haftmann@31125
   174
        Pretty.str "preprocessing simpset:",
haftmann@31125
   175
        Pretty.fbrk,
haftmann@31125
   176
        Simplifier.pretty_ss ctxt pre
haftmann@31125
   177
      ],
haftmann@31125
   178
      Pretty.block [
haftmann@31125
   179
        Pretty.str "postprocessing simpset:",
haftmann@31125
   180
        Pretty.fbrk,
haftmann@31125
   181
        Simplifier.pretty_ss ctxt post
haftmann@31125
   182
      ],
haftmann@31125
   183
      Pretty.block (
haftmann@31125
   184
        Pretty.str "function transformers:"
haftmann@31125
   185
        :: Pretty.fbrk
haftmann@31125
   186
        :: (Pretty.fbreaks o map Pretty.str) functrans
haftmann@31125
   187
      )
haftmann@31125
   188
    ]
haftmann@31125
   189
  end;
haftmann@31125
   190
haftmann@31125
   191
fun simple_functrans f thy eqns = case f thy (map fst eqns)
haftmann@31125
   192
 of SOME thms' => SOME (map (rpair (forall snd eqns)) thms')
haftmann@31125
   193
  | NONE => NONE;
haftmann@31125
   194
haftmann@31125
   195
haftmann@31125
   196
(** sort algebra and code equation graph types **)
haftmann@30010
   197
haftmann@30947
   198
type code_algebra = (sort -> sort) * Sorts.algebra;
haftmann@32873
   199
type code_graph = ((string * sort) list * (thm * bool) list) Graph.T;
haftmann@30010
   200
haftmann@30010
   201
fun eqns eqngr = these o Option.map snd o try (Graph.get_node eqngr);
haftmann@32873
   202
fun sortargs eqngr = map snd o fst o Graph.get_node eqngr
haftmann@30010
   203
fun all eqngr = Graph.keys eqngr;
haftmann@30010
   204
haftmann@30010
   205
fun pretty thy eqngr =
haftmann@30010
   206
  AList.make (snd o Graph.get_node eqngr) (Graph.keys eqngr)
haftmann@31156
   207
  |> (map o apfst) (Code.string_of_const thy)
haftmann@30010
   208
  |> sort (string_ord o pairself fst)
haftmann@30010
   209
  |> map (fn (s, thms) =>
haftmann@30010
   210
       (Pretty.block o Pretty.fbreaks) (
haftmann@30010
   211
         Pretty.str s
wenzelm@32091
   212
         :: map (Display.pretty_thm_global thy o fst) thms
haftmann@30010
   213
       ))
haftmann@30010
   214
  |> Pretty.chunks;
haftmann@30010
   215
haftmann@30010
   216
haftmann@30010
   217
(** the Waisenhaus algorithm **)
haftmann@30010
   218
haftmann@30010
   219
(* auxiliary *)
haftmann@30010
   220
haftmann@30942
   221
fun is_proper_class thy = can (AxClass.get_info thy);
haftmann@30942
   222
haftmann@30010
   223
fun complete_proper_sort thy =
haftmann@30942
   224
  Sign.complete_sort thy #> filter (is_proper_class thy);
haftmann@30010
   225
haftmann@30029
   226
fun inst_params thy tyco =
haftmann@30010
   227
  map (fn (c, _) => AxClass.param_of_inst thy (c, tyco))
haftmann@30029
   228
    o maps (#params o AxClass.get_info thy);
haftmann@30010
   229
haftmann@32873
   230
fun typscheme_rhss thy c eqns =
haftmann@32873
   231
  let
haftmann@32873
   232
    val tyscm = Code.typscheme_eqns thy c (map fst eqns);
haftmann@32873
   233
    val rhss = [] |> (fold o fold o fold_aterms)
haftmann@32873
   234
      (fn Const (c, ty) => insert (op =) (c, Sign.const_typargs thy (c, Logic.unvarifyT ty)) | _ => I)
haftmann@32873
   235
        (map (op :: o swap o apfst (snd o strip_comb) o Logic.dest_equals o Thm.plain_prop_of o fst) eqns);
haftmann@32873
   236
  in (tyscm, rhss) end;
haftmann@32873
   237
haftmann@30010
   238
haftmann@30010
   239
(* data structures *)
haftmann@30010
   240
haftmann@30010
   241
datatype const = Fun of string | Inst of class * string;
haftmann@30010
   242
haftmann@30010
   243
fun const_ord (Fun c1, Fun c2) = fast_string_ord (c1, c2)
haftmann@30010
   244
  | const_ord (Inst class_tyco1, Inst class_tyco2) =
haftmann@30010
   245
      prod_ord fast_string_ord fast_string_ord (class_tyco1, class_tyco2)
haftmann@30010
   246
  | const_ord (Fun _, Inst _) = LESS
haftmann@30010
   247
  | const_ord (Inst _, Fun _) = GREATER;
haftmann@30010
   248
haftmann@30010
   249
type var = const * int;
haftmann@30010
   250
haftmann@30010
   251
structure Vargraph =
wenzelm@31971
   252
  Graph(type key = var val ord = prod_ord const_ord int_ord);
haftmann@30010
   253
haftmann@30054
   254
datatype styp = Tyco of string * styp list | Var of var | Free;
haftmann@30054
   255
haftmann@30054
   256
fun styp_of c_lhs (Type (tyco, tys)) = Tyco (tyco, map (styp_of c_lhs) tys)
haftmann@30054
   257
  | styp_of c_lhs (TFree (v, _)) = case c_lhs
haftmann@30054
   258
     of SOME (c, lhs) => Var (Fun c, find_index (fn (v', _) => v = v') lhs)
haftmann@30054
   259
      | NONE => Free;
haftmann@30010
   260
haftmann@30054
   261
type vardeps_data = ((string * styp list) list * class list) Vargraph.T
haftmann@30054
   262
  * (((string * sort) list * (thm * bool) list) Symtab.table
haftmann@30054
   263
    * (class * string) list);
haftmann@30010
   264
haftmann@30054
   265
val empty_vardeps_data : vardeps_data =
haftmann@30054
   266
  (Vargraph.empty, (Symtab.empty, []));
haftmann@30054
   267
haftmann@30876
   268
haftmann@30054
   269
(* retrieving equations and instances from the background context *)
haftmann@30010
   270
haftmann@30010
   271
fun obtain_eqns thy eqngr c =
haftmann@30010
   272
  case try (Graph.get_node eqngr) c
haftmann@32873
   273
   of SOME (lhs, eqns) => ((lhs, []), [])
haftmann@30010
   274
    | NONE => let
haftmann@30010
   275
        val eqns = Code.these_eqns thy c
haftmann@31125
   276
          |> preprocess thy c;
haftmann@32873
   277
        val ((lhs, _), rhss) = typscheme_rhss thy c eqns;
haftmann@30010
   278
      in ((lhs, rhss), eqns) end;
haftmann@30010
   279
haftmann@30010
   280
fun obtain_instance thy arities (inst as (class, tyco)) =
haftmann@30010
   281
  case AList.lookup (op =) arities inst
haftmann@30010
   282
   of SOME classess => (classess, ([], []))
haftmann@30010
   283
    | NONE => let
haftmann@30029
   284
        val all_classes = complete_proper_sort thy [class];
haftmann@30029
   285
        val superclasses = remove (op =) class all_classes
haftmann@30010
   286
        val classess = map (complete_proper_sort thy)
haftmann@30010
   287
          (Sign.arity_sorts thy tyco [class]);
haftmann@30029
   288
        val inst_params = inst_params thy tyco all_classes;
haftmann@30010
   289
      in (classess, (superclasses, inst_params)) end;
haftmann@30010
   290
haftmann@30054
   291
haftmann@30054
   292
(* computing instantiations *)
haftmann@30054
   293
haftmann@30010
   294
fun add_classes thy arities eqngr c_k new_classes vardeps_data =
haftmann@30010
   295
  let
haftmann@30010
   296
    val (styps, old_classes) = Vargraph.get_node (fst vardeps_data) c_k;
haftmann@30010
   297
    val diff_classes = new_classes |> subtract (op =) old_classes;
haftmann@30010
   298
  in if null diff_classes then vardeps_data
haftmann@30010
   299
  else let
haftmann@30010
   300
    val c_ks = Vargraph.imm_succs (fst vardeps_data) c_k |> insert (op =) c_k;
haftmann@30010
   301
  in
haftmann@30010
   302
    vardeps_data
haftmann@30010
   303
    |> (apfst o Vargraph.map_node c_k o apsnd) (append diff_classes)
haftmann@31125
   304
    |> fold (fn styp => fold (ensure_typmatch_inst thy arities eqngr styp) new_classes) styps
haftmann@30010
   305
    |> fold (fn c_k => add_classes thy arities eqngr c_k diff_classes) c_ks
haftmann@30010
   306
  end end
haftmann@30010
   307
and add_styp thy arities eqngr c_k tyco_styps vardeps_data =
haftmann@30010
   308
  let
haftmann@30010
   309
    val (old_styps, classes) = Vargraph.get_node (fst vardeps_data) c_k;
haftmann@30010
   310
  in if member (op =) old_styps tyco_styps then vardeps_data
haftmann@30010
   311
  else
haftmann@30010
   312
    vardeps_data
haftmann@30010
   313
    |> (apfst o Vargraph.map_node c_k o apfst) (cons tyco_styps)
haftmann@31125
   314
    |> fold (ensure_typmatch_inst thy arities eqngr tyco_styps) classes
haftmann@30010
   315
  end
haftmann@30010
   316
and add_dep thy arities eqngr c_k c_k' vardeps_data =
haftmann@30010
   317
  let
haftmann@30010
   318
    val (_, classes) = Vargraph.get_node (fst vardeps_data) c_k;
haftmann@30010
   319
  in
haftmann@30010
   320
    vardeps_data
haftmann@30010
   321
    |> add_classes thy arities eqngr c_k' classes
haftmann@30010
   322
    |> apfst (Vargraph.add_edge (c_k, c_k'))
haftmann@30010
   323
  end
haftmann@31125
   324
and ensure_typmatch_inst thy arities eqngr (tyco, styps) class vardeps_data =
haftmann@30010
   325
  if can (Sign.arity_sorts thy tyco) [class]
haftmann@30010
   326
  then vardeps_data
haftmann@31125
   327
    |> ensure_inst thy arities eqngr (class, tyco)
haftmann@30010
   328
    |> fold_index (fn (k, styp) =>
haftmann@31125
   329
         ensure_typmatch thy arities eqngr styp (Inst (class, tyco), k)) styps
haftmann@30010
   330
  else vardeps_data (*permissive!*)
haftmann@31125
   331
and ensure_inst thy arities eqngr (inst as (class, tyco)) (vardeps_data as (_, (_, insts))) =
haftmann@30054
   332
  if member (op =) insts inst then vardeps_data
haftmann@30054
   333
  else let
haftmann@30010
   334
    val (classess, (superclasses, inst_params)) =
haftmann@30010
   335
      obtain_instance thy arities inst;
haftmann@30010
   336
  in
haftmann@30010
   337
    vardeps_data
haftmann@30054
   338
    |> (apsnd o apsnd) (insert (op =) inst)
haftmann@30083
   339
    |> fold_index (fn (k, _) =>
haftmann@30083
   340
         apfst (Vargraph.new_node ((Inst (class, tyco), k), ([] ,[])))) classess
haftmann@31125
   341
    |> fold (fn superclass => ensure_inst thy arities eqngr (superclass, tyco)) superclasses
haftmann@31125
   342
    |> fold (ensure_fun thy arities eqngr) inst_params
haftmann@30010
   343
    |> fold_index (fn (k, classes) =>
haftmann@30075
   344
         add_classes thy arities eqngr (Inst (class, tyco), k) classes
haftmann@30010
   345
         #> fold (fn superclass =>
haftmann@30010
   346
             add_dep thy arities eqngr (Inst (superclass, tyco), k)
haftmann@30010
   347
             (Inst (class, tyco), k)) superclasses
haftmann@30010
   348
         #> fold (fn inst_param =>
haftmann@30010
   349
             add_dep thy arities eqngr (Fun inst_param, k)
haftmann@30010
   350
             (Inst (class, tyco), k)
haftmann@30010
   351
             ) inst_params
haftmann@30010
   352
         ) classess
haftmann@30010
   353
  end
haftmann@31125
   354
and ensure_typmatch thy arities eqngr (Tyco tyco_styps) c_k vardeps_data =
haftmann@30054
   355
      vardeps_data
haftmann@30054
   356
      |> add_styp thy arities eqngr c_k tyco_styps
haftmann@31125
   357
  | ensure_typmatch thy arities eqngr (Var c_k') c_k vardeps_data =
haftmann@30054
   358
      vardeps_data
haftmann@30054
   359
      |> add_dep thy arities eqngr c_k c_k'
haftmann@31125
   360
  | ensure_typmatch thy arities eqngr Free c_k vardeps_data =
haftmann@30054
   361
      vardeps_data
haftmann@31125
   362
and ensure_rhs thy arities eqngr (c', styps) vardeps_data =
haftmann@30054
   363
  vardeps_data
haftmann@31125
   364
  |> ensure_fun thy arities eqngr c'
haftmann@30054
   365
  |> fold_index (fn (k, styp) =>
haftmann@31125
   366
       ensure_typmatch thy arities eqngr styp (Fun c', k)) styps
haftmann@31125
   367
and ensure_fun thy arities eqngr c (vardeps_data as (_, (eqntab, _))) =
haftmann@30054
   368
  if Symtab.defined eqntab c then vardeps_data
haftmann@30054
   369
  else let
haftmann@30010
   370
    val ((lhs, rhss), eqns) = obtain_eqns thy eqngr c;
haftmann@30054
   371
    val rhss' = (map o apsnd o map) (styp_of (SOME (c, lhs))) rhss;
haftmann@30010
   372
  in
haftmann@30010
   373
    vardeps_data
haftmann@30054
   374
    |> (apsnd o apfst) (Symtab.update_new (c, (lhs, eqns)))
haftmann@30083
   375
    |> fold_index (fn (k, _) =>
haftmann@30083
   376
         apfst (Vargraph.new_node ((Fun c, k), ([] ,[])))) lhs
haftmann@30010
   377
    |> fold_index (fn (k, (_, sort)) =>
haftmann@30083
   378
         add_classes thy arities eqngr (Fun c, k) (complete_proper_sort thy sort)) lhs
haftmann@31125
   379
    |> fold (ensure_rhs thy arities eqngr) rhss'
haftmann@30054
   380
  end;
haftmann@30010
   381
haftmann@30010
   382
haftmann@30010
   383
(* applying instantiations *)
haftmann@30010
   384
haftmann@30010
   385
fun dicts_of thy (proj_sort, algebra) (T, sort) =
haftmann@30010
   386
  let
haftmann@30010
   387
    fun class_relation (x, _) _ = x;
haftmann@30010
   388
    fun type_constructor tyco xs class =
haftmann@30054
   389
      inst_params thy tyco (Sorts.complete_sort algebra [class])
haftmann@30054
   390
        @ (maps o maps) fst xs;
haftmann@30010
   391
    fun type_variable (TFree (_, sort)) = map (pair []) (proj_sort sort);
haftmann@30010
   392
  in
wenzelm@32795
   393
    flat (Sorts.of_sort_derivation algebra
haftmann@30010
   394
      { class_relation = class_relation, type_constructor = type_constructor,
haftmann@30010
   395
        type_variable = type_variable } (T, proj_sort sort)
haftmann@30010
   396
       handle Sorts.CLASS_ERROR _ => [] (*permissive!*))
haftmann@30010
   397
  end;
haftmann@30010
   398
haftmann@31957
   399
fun inst_thm thy tvars' thm =
haftmann@31957
   400
  let
haftmann@31957
   401
    val tvars = (Term.add_tvars o Thm.prop_of) thm [];
haftmann@31957
   402
    val inter_sort = Sorts.inter_sort (Sign.classes_of thy);
haftmann@31957
   403
    fun mk_inst (tvar as (v, sort)) = case Vartab.lookup tvars' v
haftmann@31957
   404
     of SOME sort' => SOME (pairself (Thm.ctyp_of thy o TVar)
haftmann@31957
   405
          (tvar, (v, inter_sort (sort, sort'))))
haftmann@31957
   406
      | NONE => NONE;
haftmann@31957
   407
    val insts = map_filter mk_inst tvars;
haftmann@31957
   408
  in Thm.instantiate (insts, []) thm end;
haftmann@31957
   409
haftmann@30054
   410
fun add_arity thy vardeps (class, tyco) =
haftmann@33063
   411
  AList.default (op =) ((class, tyco),
haftmann@33063
   412
    map_range (fn k => (snd o Vargraph.get_node vardeps) (Inst (class, tyco), k)) (Sign.arity_number thy tyco));
haftmann@30010
   413
haftmann@30942
   414
fun add_eqs thy vardeps (c, (proto_lhs, proto_eqns)) (rhss, eqngr) =
haftmann@30058
   415
  if can (Graph.get_node eqngr) c then (rhss, eqngr)
haftmann@30058
   416
  else let
haftmann@30010
   417
    val lhs = map_index (fn (k, (v, _)) =>
haftmann@30010
   418
      (v, snd (Vargraph.get_node vardeps (Fun c, k)))) proto_lhs;
haftmann@30010
   419
    val inst_tab = Vartab.empty |> fold (fn (v, sort) =>
haftmann@30010
   420
      Vartab.update ((v, 0), sort)) lhs;
haftmann@30010
   421
    val eqns = proto_eqns
haftmann@31957
   422
      |> (map o apfst) (inst_thm thy inst_tab);
haftmann@32873
   423
    val ((vs, _), rhss') = typscheme_rhss thy c eqns;
haftmann@32873
   424
    val eqngr' = Graph.new_node (c, (vs, eqns)) eqngr;
haftmann@30054
   425
  in (map (pair c) rhss' @ rhss, eqngr') end;
haftmann@30010
   426
haftmann@32873
   427
fun extend_arities_eqngr thy cs ts (arities, (eqngr : code_graph)) =
haftmann@30010
   428
  let
haftmann@30942
   429
    val cs_rhss = (fold o fold_aterms) (fn Const (c_ty as (c, _)) =>
haftmann@30942
   430
      insert (op =) (c, (map (styp_of NONE) o Sign.const_typargs thy) c_ty) | _ => I) ts [];
haftmann@30054
   431
    val (vardeps, (eqntab, insts)) = empty_vardeps_data
haftmann@31125
   432
      |> fold (ensure_fun thy arities eqngr) cs
haftmann@31125
   433
      |> fold (ensure_rhs thy arities eqngr) cs_rhss;
haftmann@30054
   434
    val arities' = fold (add_arity thy vardeps) insts arities;
haftmann@30064
   435
    val pp = Syntax.pp_global thy;
haftmann@30942
   436
    val algebra = Sorts.subalgebra pp (is_proper_class thy)
haftmann@30064
   437
      (AList.lookup (op =) arities') (Sign.classes_of thy);
haftmann@30942
   438
    val (rhss, eqngr') = Symtab.fold (add_eqs thy vardeps) eqntab ([], eqngr);
haftmann@30942
   439
    fun deps_of (c, rhs) = c :: maps (dicts_of thy algebra)
haftmann@32873
   440
      (rhs ~~ sortargs eqngr' c);
haftmann@30054
   441
    val eqngr'' = fold (fn (c, rhs) => fold
haftmann@30054
   442
      (curry Graph.add_edge c) (deps_of rhs)) rhss eqngr';
haftmann@30942
   443
  in (algebra, (arities', eqngr'')) end;
haftmann@30010
   444
haftmann@30010
   445
haftmann@31125
   446
(** store for preprocessed arities and code equations **)
haftmann@30010
   447
haftmann@34173
   448
structure Wellsorted = Code_Data
haftmann@30010
   449
(
haftmann@30947
   450
  type T = ((string * class) * sort list) list * code_graph;
haftmann@30010
   451
  val empty = ([], Graph.empty);
haftmann@30010
   452
);
haftmann@30010
   453
haftmann@30010
   454
haftmann@31125
   455
(** retrieval and evaluation interfaces **)
haftmann@30010
   456
haftmann@34244
   457
fun obtain theory cs ts = apsnd snd
haftmann@34244
   458
  (Wellsorted.change_yield theory (fn thy => extend_arities_eqngr thy cs ts));
haftmann@30010
   459
haftmann@32123
   460
fun gen_eval thy cterm_of conclude_evaluation evaluator proto_ct =
haftmann@30942
   461
  let
haftmann@31063
   462
    val pp = Syntax.pp_global thy;
haftmann@30942
   463
    val ct = cterm_of proto_ct;
haftmann@31063
   464
    val _ = (Sign.no_frees pp o map_types (K dummyT) o Sign.no_vars pp)
haftmann@30947
   465
      (Thm.term_of ct);
haftmann@31125
   466
    val thm = preprocess_conv thy ct;
haftmann@30942
   467
    val ct' = Thm.rhs_of thm;
haftmann@30942
   468
    val t' = Thm.term_of ct';
haftmann@30947
   469
    val vs = Term.add_tfrees t' [];
haftmann@30947
   470
    val consts = fold_aterms
haftmann@30947
   471
      (fn Const (c, _) => insert (op =) c | _ => I) t' [];
haftmann@32544
   472
    val (algebra', eqngr') = obtain thy consts [t'];
haftmann@32544
   473
  in conclude_evaluation (evaluator algebra' eqngr' vs t' ct') thm end;
haftmann@30947
   474
haftmann@30947
   475
fun simple_evaluator evaluator algebra eqngr vs t ct =
haftmann@30947
   476
  evaluator algebra eqngr vs t;
haftmann@30010
   477
haftmann@30942
   478
fun eval_conv thy =
haftmann@30942
   479
  let
haftmann@30947
   480
    fun conclude_evaluation thm2 thm1 =
haftmann@30942
   481
      let
haftmann@31125
   482
        val thm3 = postprocess_conv thy (Thm.rhs_of thm2);
haftmann@30942
   483
      in
haftmann@30942
   484
        Thm.transitive thm1 (Thm.transitive thm2 thm3) handle THM _ =>
haftmann@30942
   485
          error ("could not construct evaluation proof:\n"
wenzelm@32091
   486
          ^ (cat_lines o map (Display.string_of_thm_global thy)) [thm1, thm2, thm3])
haftmann@30942
   487
      end;
haftmann@30947
   488
  in gen_eval thy I conclude_evaluation end;
haftmann@30010
   489
haftmann@32123
   490
fun eval thy postproc evaluator = gen_eval thy (Thm.cterm_of thy)
haftmann@32123
   491
  (K o postproc (postprocess_term thy)) (simple_evaluator evaluator);
haftmann@31125
   492
haftmann@31125
   493
haftmann@31125
   494
(** setup **)
haftmann@31125
   495
haftmann@31125
   496
val setup = 
haftmann@31125
   497
  let
haftmann@31125
   498
    fun mk_attribute f = Thm.declaration_attribute (fn thm => Context.mapping (f thm) I);
haftmann@32072
   499
    fun add_del_attribute_parser add del =
haftmann@31998
   500
      Attrib.add_del (mk_attribute add) (mk_attribute del);
haftmann@32072
   501
    fun both f g thm = f thm #> g thm;
haftmann@31125
   502
  in
haftmann@32072
   503
    Attrib.setup @{binding code_unfold} (add_del_attribute_parser 
haftmann@32072
   504
       (both Codegen.add_unfold add_unfold) (both Codegen.del_unfold del_unfold))
haftmann@32072
   505
        "preprocessing equations for code generators"
haftmann@32072
   506
    #> Attrib.setup @{binding code_inline} (add_del_attribute_parser add_unfold del_unfold)
haftmann@31998
   507
        "preprocessing equations for code generator"
haftmann@32072
   508
    #> Attrib.setup @{binding code_post} (add_del_attribute_parser add_post del_post)
haftmann@31998
   509
        "postprocessing equations for code generator"
haftmann@32072
   510
    #> Attrib.setup @{binding code_unfold_post} (Scan.succeed (mk_attribute add_unfold_post))
haftmann@32072
   511
        "pre- and postprocessing equations for code generator"
haftmann@31125
   512
  end;
haftmann@31125
   513
haftmann@31125
   514
val _ =
haftmann@31125
   515
  OuterSyntax.improper_command "print_codeproc" "print code preprocessor setup"
haftmann@31125
   516
  OuterKeyword.diag (Scan.succeed
haftmann@31125
   517
      (Toplevel.no_timing o Toplevel.unknown_theory o Toplevel.keep
haftmann@31125
   518
        (print_codeproc o Toplevel.theory_of)));
haftmann@30010
   519
haftmann@30010
   520
end; (*struct*)