src/Tools/Code/code_preproc.ML
author haftmann
Wed Jul 08 08:18:07 2009 +0200 (2009-07-08)
changeset 31962 baa8dce5bc45
parent 31957 a9742afd403e
child 31971 8c1b845ed105
permissions -rw-r--r--
tuned structure Code internally
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@31125
    12
  val add_inline: 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@30947
    22
  val typ: code_graph -> string -> (string * sort) list * typ
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@30947
    26
  val eval_conv: theory -> (sort -> sort)
haftmann@30947
    27
    -> (code_algebra -> code_graph -> (string * sort) list -> term -> cterm -> thm) -> cterm -> thm
haftmann@30970
    28
  val eval: theory -> (sort -> sort) -> ((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);
haftmann@31599
    57
    in make_thmproc ((pre, post), functrans) end;
haftmann@31125
    58
haftmann@31125
    59
structure Code_Preproc_Data = TheoryDataFun
haftmann@31125
    60
(
haftmann@31125
    61
  type T = thmproc;
haftmann@31599
    62
  val empty = make_thmproc ((Simplifier.empty_ss, Simplifier.empty_ss), []);
haftmann@31125
    63
  fun copy spec = spec;
haftmann@31125
    64
  val extend = copy;
haftmann@31125
    65
  fun merge pp = 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@31125
    75
fun map_data f thy =
haftmann@31125
    76
  thy
haftmann@31125
    77
  |> Code.purge_data
haftmann@31125
    78
  |> (Code_Preproc_Data.map o map_thmproc) f;
haftmann@31125
    79
haftmann@31125
    80
val map_pre = map_data o apfst o apfst;
haftmann@31125
    81
val map_post = map_data o apfst o apsnd;
haftmann@31125
    82
haftmann@31125
    83
val add_inline = map_pre o MetaSimplifier.add_simp;
haftmann@31125
    84
val del_inline = map_pre o MetaSimplifier.del_simp;
haftmann@31125
    85
val add_post = map_post o MetaSimplifier.add_simp;
haftmann@31125
    86
val del_post = map_post o MetaSimplifier.del_simp;
haftmann@31125
    87
  
haftmann@31125
    88
fun add_functrans (name, f) = (map_data o apsnd)
haftmann@31125
    89
  (AList.update (op =) (name, (serial (), f)));
haftmann@31125
    90
haftmann@31125
    91
fun del_functrans name = (map_data o apsnd)
haftmann@31125
    92
  (delete_force "function transformer" name);
haftmann@31125
    93
haftmann@31125
    94
haftmann@31125
    95
(* post- and preprocessing *)
haftmann@31125
    96
haftmann@31125
    97
fun apply_functrans thy c _ [] = []
haftmann@31125
    98
  | apply_functrans thy c [] eqns = eqns
haftmann@31125
    99
  | apply_functrans thy c functrans eqns = eqns
haftmann@31125
   100
      |> perhaps (perhaps_loop (perhaps_apply functrans))
haftmann@31125
   101
      |> Code.assert_eqns_const thy c;
haftmann@31125
   102
haftmann@31125
   103
fun rhs_conv conv thm = Thm.transitive thm ((conv o Thm.rhs_of) thm);
haftmann@31125
   104
haftmann@31957
   105
fun eqn_conv conv =
haftmann@31957
   106
  let
haftmann@31957
   107
    fun lhs_conv ct = if can Thm.dest_comb ct
haftmann@31957
   108
      then Conv.combination_conv lhs_conv conv ct
haftmann@31957
   109
      else Conv.all_conv ct;
haftmann@31957
   110
  in Conv.combination_conv (Conv.arg_conv lhs_conv) conv end;
haftmann@31957
   111
haftmann@31957
   112
val rewrite_eqn = Conv.fconv_rule o eqn_conv o Simplifier.rewrite;
haftmann@31957
   113
haftmann@31125
   114
fun term_of_conv thy f =
haftmann@31125
   115
  Thm.cterm_of thy
haftmann@31125
   116
  #> f
haftmann@31125
   117
  #> Thm.prop_of
haftmann@31125
   118
  #> Logic.dest_equals
haftmann@31125
   119
  #> snd;
haftmann@31125
   120
haftmann@31125
   121
fun preprocess thy c eqns =
haftmann@31125
   122
  let
haftmann@31125
   123
    val pre = (Simplifier.theory_context thy o #pre o the_thmproc) thy;
haftmann@31125
   124
    val functrans = (map (fn (_, (_, f)) => f thy) o #functrans
haftmann@31125
   125
      o the_thmproc) thy;
haftmann@31125
   126
  in
haftmann@31125
   127
    eqns
haftmann@31125
   128
    |> apply_functrans thy c functrans
haftmann@31957
   129
    |> (map o apfst) (rewrite_eqn pre)
haftmann@31125
   130
    |> (map o apfst) (AxClass.unoverload thy)
haftmann@31125
   131
    |> map (Code.assert_eqn thy)
haftmann@31156
   132
    |> burrow_fst (Code.norm_args thy)
haftmann@31156
   133
    |> burrow_fst (Code.norm_varnames thy)
haftmann@31125
   134
  end;
haftmann@31125
   135
haftmann@31125
   136
fun preprocess_conv thy ct =
haftmann@31125
   137
  let
haftmann@31125
   138
    val pre = (Simplifier.theory_context thy o #pre o the_thmproc) thy;
haftmann@31125
   139
  in
haftmann@31125
   140
    ct
haftmann@31125
   141
    |> Simplifier.rewrite pre
haftmann@31125
   142
    |> rhs_conv (AxClass.unoverload_conv thy)
haftmann@31125
   143
  end;
haftmann@31125
   144
haftmann@31125
   145
fun postprocess_conv thy ct =
haftmann@31125
   146
  let
haftmann@31125
   147
    val post = (Simplifier.theory_context thy o #post o the_thmproc) thy;
haftmann@31125
   148
  in
haftmann@31125
   149
    ct
haftmann@31125
   150
    |> AxClass.overload_conv thy
haftmann@31125
   151
    |> rhs_conv (Simplifier.rewrite post)
haftmann@31125
   152
  end;
haftmann@31125
   153
haftmann@31125
   154
fun postprocess_term thy = term_of_conv thy (postprocess_conv thy);
haftmann@31125
   155
haftmann@31125
   156
fun print_codeproc thy =
haftmann@31125
   157
  let
haftmann@31125
   158
    val ctxt = ProofContext.init thy;
haftmann@31125
   159
    val pre = (#pre o the_thmproc) thy;
haftmann@31125
   160
    val post = (#post o the_thmproc) thy;
haftmann@31125
   161
    val functrans = (map fst o #functrans o the_thmproc) thy;
haftmann@31125
   162
  in
haftmann@31125
   163
    (Pretty.writeln o Pretty.chunks) [
haftmann@31125
   164
      Pretty.block [
haftmann@31125
   165
        Pretty.str "preprocessing simpset:",
haftmann@31125
   166
        Pretty.fbrk,
haftmann@31125
   167
        Simplifier.pretty_ss ctxt pre
haftmann@31125
   168
      ],
haftmann@31125
   169
      Pretty.block [
haftmann@31125
   170
        Pretty.str "postprocessing simpset:",
haftmann@31125
   171
        Pretty.fbrk,
haftmann@31125
   172
        Simplifier.pretty_ss ctxt post
haftmann@31125
   173
      ],
haftmann@31125
   174
      Pretty.block (
haftmann@31125
   175
        Pretty.str "function transformers:"
haftmann@31125
   176
        :: Pretty.fbrk
haftmann@31125
   177
        :: (Pretty.fbreaks o map Pretty.str) functrans
haftmann@31125
   178
      )
haftmann@31125
   179
    ]
haftmann@31125
   180
  end;
haftmann@31125
   181
haftmann@31125
   182
fun simple_functrans f thy eqns = case f thy (map fst eqns)
haftmann@31125
   183
 of SOME thms' => SOME (map (rpair (forall snd eqns)) thms')
haftmann@31125
   184
  | NONE => NONE;
haftmann@31125
   185
haftmann@31125
   186
haftmann@31125
   187
(** sort algebra and code equation graph types **)
haftmann@30010
   188
haftmann@30947
   189
type code_algebra = (sort -> sort) * Sorts.algebra;
haftmann@30947
   190
type code_graph = (((string * sort) list * typ) * (thm * bool) list) Graph.T;
haftmann@30010
   191
haftmann@30010
   192
fun eqns eqngr = these o Option.map snd o try (Graph.get_node eqngr);
haftmann@30010
   193
fun typ eqngr = fst o Graph.get_node eqngr;
haftmann@30010
   194
fun all eqngr = Graph.keys eqngr;
haftmann@30010
   195
haftmann@30010
   196
fun pretty thy eqngr =
haftmann@30010
   197
  AList.make (snd o Graph.get_node eqngr) (Graph.keys eqngr)
haftmann@31156
   198
  |> (map o apfst) (Code.string_of_const thy)
haftmann@30010
   199
  |> sort (string_ord o pairself fst)
haftmann@30010
   200
  |> map (fn (s, thms) =>
haftmann@30010
   201
       (Pretty.block o Pretty.fbreaks) (
haftmann@30010
   202
         Pretty.str s
haftmann@30010
   203
         :: map (Display.pretty_thm o fst) thms
haftmann@30010
   204
       ))
haftmann@30010
   205
  |> Pretty.chunks;
haftmann@30010
   206
haftmann@30010
   207
haftmann@30010
   208
(** the Waisenhaus algorithm **)
haftmann@30010
   209
haftmann@30010
   210
(* auxiliary *)
haftmann@30010
   211
haftmann@30942
   212
fun is_proper_class thy = can (AxClass.get_info thy);
haftmann@30942
   213
haftmann@30010
   214
fun complete_proper_sort thy =
haftmann@30942
   215
  Sign.complete_sort thy #> filter (is_proper_class thy);
haftmann@30010
   216
haftmann@30029
   217
fun inst_params thy tyco =
haftmann@30010
   218
  map (fn (c, _) => AxClass.param_of_inst thy (c, tyco))
haftmann@30029
   219
    o maps (#params o AxClass.get_info thy);
haftmann@30010
   220
haftmann@30010
   221
fun consts_of thy eqns = [] |> (fold o fold o fold_aterms)
haftmann@30010
   222
  (fn Const (c, ty) => insert (op =) (c, Sign.const_typargs thy (c, Logic.unvarifyT ty)) | _ => I)
haftmann@30010
   223
    (map (op :: o swap o apfst (snd o strip_comb) o Logic.dest_equals o Thm.plain_prop_of o fst) eqns);
haftmann@30010
   224
haftmann@31957
   225
fun default_typscheme_of thy c =
haftmann@31957
   226
  let
haftmann@31957
   227
    val ty = (snd o dest_Const o TermSubst.zero_var_indexes o curry Const c
haftmann@31957
   228
      o Type.strip_sorts o Sign.the_const_type thy) c;
haftmann@31957
   229
  in case AxClass.class_of_param thy c
haftmann@31957
   230
   of SOME class => ([(Name.aT, [class])], ty)
haftmann@31957
   231
    | NONE => Code.typscheme thy (c, ty)
haftmann@31957
   232
  end;
haftmann@31957
   233
haftmann@30010
   234
fun tyscm_rhss_of thy c eqns =
haftmann@30010
   235
  let
haftmann@31957
   236
    val tyscm = case eqns
haftmann@31957
   237
     of [] => default_typscheme_of thy c
haftmann@31156
   238
      | ((thm, _) :: _) => Code.typscheme_eqn thy thm;
haftmann@30010
   239
    val rhss = consts_of thy eqns;
haftmann@30010
   240
  in (tyscm, rhss) end;
haftmann@30010
   241
haftmann@30010
   242
haftmann@30010
   243
(* data structures *)
haftmann@30010
   244
haftmann@30010
   245
datatype const = Fun of string | Inst of class * string;
haftmann@30010
   246
haftmann@30010
   247
fun const_ord (Fun c1, Fun c2) = fast_string_ord (c1, c2)
haftmann@30010
   248
  | const_ord (Inst class_tyco1, Inst class_tyco2) =
haftmann@30010
   249
      prod_ord fast_string_ord fast_string_ord (class_tyco1, class_tyco2)
haftmann@30010
   250
  | const_ord (Fun _, Inst _) = LESS
haftmann@30010
   251
  | const_ord (Inst _, Fun _) = GREATER;
haftmann@30010
   252
haftmann@30010
   253
type var = const * int;
haftmann@30010
   254
haftmann@30010
   255
structure Vargraph =
haftmann@30010
   256
  GraphFun(type key = var val ord = prod_ord const_ord int_ord);
haftmann@30010
   257
haftmann@30054
   258
datatype styp = Tyco of string * styp list | Var of var | Free;
haftmann@30054
   259
haftmann@30054
   260
fun styp_of c_lhs (Type (tyco, tys)) = Tyco (tyco, map (styp_of c_lhs) tys)
haftmann@30054
   261
  | styp_of c_lhs (TFree (v, _)) = case c_lhs
haftmann@30054
   262
     of SOME (c, lhs) => Var (Fun c, find_index (fn (v', _) => v = v') lhs)
haftmann@30054
   263
      | NONE => Free;
haftmann@30010
   264
haftmann@30054
   265
type vardeps_data = ((string * styp list) list * class list) Vargraph.T
haftmann@30054
   266
  * (((string * sort) list * (thm * bool) list) Symtab.table
haftmann@30054
   267
    * (class * string) list);
haftmann@30010
   268
haftmann@30054
   269
val empty_vardeps_data : vardeps_data =
haftmann@30054
   270
  (Vargraph.empty, (Symtab.empty, []));
haftmann@30054
   271
haftmann@30876
   272
haftmann@30054
   273
(* retrieving equations and instances from the background context *)
haftmann@30010
   274
haftmann@30010
   275
fun obtain_eqns thy eqngr c =
haftmann@30010
   276
  case try (Graph.get_node eqngr) c
haftmann@30058
   277
   of SOME ((lhs, _), eqns) => ((lhs, []), [])
haftmann@30010
   278
    | NONE => let
haftmann@30010
   279
        val eqns = Code.these_eqns thy c
haftmann@31125
   280
          |> preprocess thy c;
haftmann@30010
   281
        val ((lhs, _), rhss) = tyscm_rhss_of thy c eqns;
haftmann@30010
   282
      in ((lhs, rhss), eqns) end;
haftmann@30010
   283
haftmann@30010
   284
fun obtain_instance thy arities (inst as (class, tyco)) =
haftmann@30010
   285
  case AList.lookup (op =) arities inst
haftmann@30010
   286
   of SOME classess => (classess, ([], []))
haftmann@30010
   287
    | NONE => let
haftmann@30029
   288
        val all_classes = complete_proper_sort thy [class];
haftmann@30029
   289
        val superclasses = remove (op =) class all_classes
haftmann@30010
   290
        val classess = map (complete_proper_sort thy)
haftmann@30010
   291
          (Sign.arity_sorts thy tyco [class]);
haftmann@30029
   292
        val inst_params = inst_params thy tyco all_classes;
haftmann@30010
   293
      in (classess, (superclasses, inst_params)) end;
haftmann@30010
   294
haftmann@30054
   295
haftmann@30054
   296
(* computing instantiations *)
haftmann@30054
   297
haftmann@30010
   298
fun add_classes thy arities eqngr c_k new_classes vardeps_data =
haftmann@30010
   299
  let
haftmann@30010
   300
    val (styps, old_classes) = Vargraph.get_node (fst vardeps_data) c_k;
haftmann@30010
   301
    val diff_classes = new_classes |> subtract (op =) old_classes;
haftmann@30010
   302
  in if null diff_classes then vardeps_data
haftmann@30010
   303
  else let
haftmann@30010
   304
    val c_ks = Vargraph.imm_succs (fst vardeps_data) c_k |> insert (op =) c_k;
haftmann@30010
   305
  in
haftmann@30010
   306
    vardeps_data
haftmann@30010
   307
    |> (apfst o Vargraph.map_node c_k o apsnd) (append diff_classes)
haftmann@31125
   308
    |> fold (fn styp => fold (ensure_typmatch_inst thy arities eqngr styp) new_classes) styps
haftmann@30010
   309
    |> fold (fn c_k => add_classes thy arities eqngr c_k diff_classes) c_ks
haftmann@30010
   310
  end end
haftmann@30010
   311
and add_styp thy arities eqngr c_k tyco_styps vardeps_data =
haftmann@30010
   312
  let
haftmann@30010
   313
    val (old_styps, classes) = Vargraph.get_node (fst vardeps_data) c_k;
haftmann@30010
   314
  in if member (op =) old_styps tyco_styps then vardeps_data
haftmann@30010
   315
  else
haftmann@30010
   316
    vardeps_data
haftmann@30010
   317
    |> (apfst o Vargraph.map_node c_k o apfst) (cons tyco_styps)
haftmann@31125
   318
    |> fold (ensure_typmatch_inst thy arities eqngr tyco_styps) classes
haftmann@30010
   319
  end
haftmann@30010
   320
and add_dep thy arities eqngr c_k c_k' vardeps_data =
haftmann@30010
   321
  let
haftmann@30010
   322
    val (_, classes) = Vargraph.get_node (fst vardeps_data) c_k;
haftmann@30010
   323
  in
haftmann@30010
   324
    vardeps_data
haftmann@30010
   325
    |> add_classes thy arities eqngr c_k' classes
haftmann@30010
   326
    |> apfst (Vargraph.add_edge (c_k, c_k'))
haftmann@30010
   327
  end
haftmann@31125
   328
and ensure_typmatch_inst thy arities eqngr (tyco, styps) class vardeps_data =
haftmann@30010
   329
  if can (Sign.arity_sorts thy tyco) [class]
haftmann@30010
   330
  then vardeps_data
haftmann@31125
   331
    |> ensure_inst thy arities eqngr (class, tyco)
haftmann@30010
   332
    |> fold_index (fn (k, styp) =>
haftmann@31125
   333
         ensure_typmatch thy arities eqngr styp (Inst (class, tyco), k)) styps
haftmann@30010
   334
  else vardeps_data (*permissive!*)
haftmann@31125
   335
and ensure_inst thy arities eqngr (inst as (class, tyco)) (vardeps_data as (_, (_, insts))) =
haftmann@30054
   336
  if member (op =) insts inst then vardeps_data
haftmann@30054
   337
  else let
haftmann@30010
   338
    val (classess, (superclasses, inst_params)) =
haftmann@30010
   339
      obtain_instance thy arities inst;
haftmann@30010
   340
  in
haftmann@30010
   341
    vardeps_data
haftmann@30054
   342
    |> (apsnd o apsnd) (insert (op =) inst)
haftmann@30083
   343
    |> fold_index (fn (k, _) =>
haftmann@30083
   344
         apfst (Vargraph.new_node ((Inst (class, tyco), k), ([] ,[])))) classess
haftmann@31125
   345
    |> fold (fn superclass => ensure_inst thy arities eqngr (superclass, tyco)) superclasses
haftmann@31125
   346
    |> fold (ensure_fun thy arities eqngr) inst_params
haftmann@30010
   347
    |> fold_index (fn (k, classes) =>
haftmann@30075
   348
         add_classes thy arities eqngr (Inst (class, tyco), k) classes
haftmann@30010
   349
         #> fold (fn superclass =>
haftmann@30010
   350
             add_dep thy arities eqngr (Inst (superclass, tyco), k)
haftmann@30010
   351
             (Inst (class, tyco), k)) superclasses
haftmann@30010
   352
         #> fold (fn inst_param =>
haftmann@30010
   353
             add_dep thy arities eqngr (Fun inst_param, k)
haftmann@30010
   354
             (Inst (class, tyco), k)
haftmann@30010
   355
             ) inst_params
haftmann@30010
   356
         ) classess
haftmann@30010
   357
  end
haftmann@31125
   358
and ensure_typmatch thy arities eqngr (Tyco tyco_styps) c_k vardeps_data =
haftmann@30054
   359
      vardeps_data
haftmann@30054
   360
      |> add_styp thy arities eqngr c_k tyco_styps
haftmann@31125
   361
  | ensure_typmatch thy arities eqngr (Var c_k') c_k vardeps_data =
haftmann@30054
   362
      vardeps_data
haftmann@30054
   363
      |> add_dep thy arities eqngr c_k c_k'
haftmann@31125
   364
  | ensure_typmatch thy arities eqngr Free c_k vardeps_data =
haftmann@30054
   365
      vardeps_data
haftmann@31125
   366
and ensure_rhs thy arities eqngr (c', styps) vardeps_data =
haftmann@30054
   367
  vardeps_data
haftmann@31125
   368
  |> ensure_fun thy arities eqngr c'
haftmann@30054
   369
  |> fold_index (fn (k, styp) =>
haftmann@31125
   370
       ensure_typmatch thy arities eqngr styp (Fun c', k)) styps
haftmann@31125
   371
and ensure_fun thy arities eqngr c (vardeps_data as (_, (eqntab, _))) =
haftmann@30054
   372
  if Symtab.defined eqntab c then vardeps_data
haftmann@30054
   373
  else let
haftmann@30010
   374
    val ((lhs, rhss), eqns) = obtain_eqns thy eqngr c;
haftmann@30054
   375
    val rhss' = (map o apsnd o map) (styp_of (SOME (c, lhs))) rhss;
haftmann@30010
   376
  in
haftmann@30010
   377
    vardeps_data
haftmann@30054
   378
    |> (apsnd o apfst) (Symtab.update_new (c, (lhs, eqns)))
haftmann@30083
   379
    |> fold_index (fn (k, _) =>
haftmann@30083
   380
         apfst (Vargraph.new_node ((Fun c, k), ([] ,[])))) lhs
haftmann@30010
   381
    |> fold_index (fn (k, (_, sort)) =>
haftmann@30083
   382
         add_classes thy arities eqngr (Fun c, k) (complete_proper_sort thy sort)) lhs
haftmann@31125
   383
    |> fold (ensure_rhs thy arities eqngr) rhss'
haftmann@30054
   384
  end;
haftmann@30010
   385
haftmann@30010
   386
haftmann@30010
   387
(* applying instantiations *)
haftmann@30010
   388
haftmann@30010
   389
fun dicts_of thy (proj_sort, algebra) (T, sort) =
haftmann@30010
   390
  let
haftmann@30010
   391
    fun class_relation (x, _) _ = x;
haftmann@30010
   392
    fun type_constructor tyco xs class =
haftmann@30054
   393
      inst_params thy tyco (Sorts.complete_sort algebra [class])
haftmann@30054
   394
        @ (maps o maps) fst xs;
haftmann@30010
   395
    fun type_variable (TFree (_, sort)) = map (pair []) (proj_sort sort);
haftmann@30010
   396
  in
haftmann@30010
   397
    flat (Sorts.of_sort_derivation (Syntax.pp_global thy) algebra
haftmann@30010
   398
      { class_relation = class_relation, type_constructor = type_constructor,
haftmann@30010
   399
        type_variable = type_variable } (T, proj_sort sort)
haftmann@30010
   400
       handle Sorts.CLASS_ERROR _ => [] (*permissive!*))
haftmann@30010
   401
  end;
haftmann@30010
   402
haftmann@31957
   403
fun inst_thm thy tvars' thm =
haftmann@31957
   404
  let
haftmann@31957
   405
    val tvars = (Term.add_tvars o Thm.prop_of) thm [];
haftmann@31957
   406
    val inter_sort = Sorts.inter_sort (Sign.classes_of thy);
haftmann@31957
   407
    fun mk_inst (tvar as (v, sort)) = case Vartab.lookup tvars' v
haftmann@31957
   408
     of SOME sort' => SOME (pairself (Thm.ctyp_of thy o TVar)
haftmann@31957
   409
          (tvar, (v, inter_sort (sort, sort'))))
haftmann@31957
   410
      | NONE => NONE;
haftmann@31957
   411
    val insts = map_filter mk_inst tvars;
haftmann@31957
   412
  in Thm.instantiate (insts, []) thm end;
haftmann@31957
   413
haftmann@30054
   414
fun add_arity thy vardeps (class, tyco) =
haftmann@30058
   415
  AList.default (op =)
haftmann@30054
   416
    ((class, tyco), map (fn k => (snd o Vargraph.get_node vardeps) (Inst (class, tyco), k))
haftmann@30054
   417
      (0 upto Sign.arity_number thy tyco - 1));
haftmann@30010
   418
haftmann@30942
   419
fun add_eqs thy vardeps (c, (proto_lhs, proto_eqns)) (rhss, eqngr) =
haftmann@30058
   420
  if can (Graph.get_node eqngr) c then (rhss, eqngr)
haftmann@30058
   421
  else let
haftmann@30010
   422
    val lhs = map_index (fn (k, (v, _)) =>
haftmann@30010
   423
      (v, snd (Vargraph.get_node vardeps (Fun c, k)))) proto_lhs;
haftmann@30010
   424
    val inst_tab = Vartab.empty |> fold (fn (v, sort) =>
haftmann@30010
   425
      Vartab.update ((v, 0), sort)) lhs;
haftmann@30010
   426
    val eqns = proto_eqns
haftmann@31957
   427
      |> (map o apfst) (inst_thm thy inst_tab);
haftmann@30054
   428
    val (tyscm, rhss') = tyscm_rhss_of thy c eqns;
haftmann@30054
   429
    val eqngr' = Graph.new_node (c, (tyscm, eqns)) eqngr;
haftmann@30054
   430
  in (map (pair c) rhss' @ rhss, eqngr') end;
haftmann@30010
   431
haftmann@30942
   432
fun extend_arities_eqngr thy cs ts (arities, eqngr) =
haftmann@30010
   433
  let
haftmann@30942
   434
    val cs_rhss = (fold o fold_aterms) (fn Const (c_ty as (c, _)) =>
haftmann@30942
   435
      insert (op =) (c, (map (styp_of NONE) o Sign.const_typargs thy) c_ty) | _ => I) ts [];
haftmann@30054
   436
    val (vardeps, (eqntab, insts)) = empty_vardeps_data
haftmann@31125
   437
      |> fold (ensure_fun thy arities eqngr) cs
haftmann@31125
   438
      |> fold (ensure_rhs thy arities eqngr) cs_rhss;
haftmann@30054
   439
    val arities' = fold (add_arity thy vardeps) insts arities;
haftmann@30064
   440
    val pp = Syntax.pp_global thy;
haftmann@30942
   441
    val algebra = Sorts.subalgebra pp (is_proper_class thy)
haftmann@30064
   442
      (AList.lookup (op =) arities') (Sign.classes_of thy);
haftmann@30942
   443
    val (rhss, eqngr') = Symtab.fold (add_eqs thy vardeps) eqntab ([], eqngr);
haftmann@30942
   444
    fun deps_of (c, rhs) = c :: maps (dicts_of thy algebra)
haftmann@30942
   445
      (rhs ~~ (map snd o fst o fst o Graph.get_node eqngr') c);
haftmann@30054
   446
    val eqngr'' = fold (fn (c, rhs) => fold
haftmann@30054
   447
      (curry Graph.add_edge c) (deps_of rhs)) rhss eqngr';
haftmann@30942
   448
  in (algebra, (arities', eqngr'')) end;
haftmann@30010
   449
haftmann@30010
   450
haftmann@31125
   451
(** store for preprocessed arities and code equations **)
haftmann@30010
   452
haftmann@31962
   453
structure Wellsorted = Code_Data_Fun
haftmann@30010
   454
(
haftmann@30947
   455
  type T = ((string * class) * sort list) list * code_graph;
haftmann@30010
   456
  val empty = ([], Graph.empty);
haftmann@30010
   457
  fun purge thy cs (arities, eqngr) =
haftmann@30010
   458
    let
haftmann@30010
   459
      val del_cs = ((Graph.all_preds eqngr
haftmann@30010
   460
        o filter (can (Graph.get_node eqngr))) cs);
haftmann@30050
   461
      val del_arities = del_cs
haftmann@30050
   462
        |> map_filter (AxClass.inst_of_param thy)
haftmann@30050
   463
        |> maps (fn (c, tyco) =>
haftmann@30050
   464
             (map (rpair tyco) o Sign.complete_sort thy o the_list
haftmann@30050
   465
               o AxClass.class_of_param thy) c);
haftmann@30010
   466
      val arities' = fold (AList.delete (op =)) del_arities arities;
haftmann@30010
   467
      val eqngr' = Graph.del_nodes del_cs eqngr;
haftmann@30010
   468
    in (arities', eqngr') end;
haftmann@30010
   469
);
haftmann@30010
   470
haftmann@30010
   471
haftmann@31125
   472
(** retrieval and evaluation interfaces **)
haftmann@30010
   473
haftmann@30942
   474
fun obtain thy cs ts = apsnd snd
haftmann@30942
   475
  (Wellsorted.change_yield thy (extend_arities_eqngr thy cs ts));
haftmann@30010
   476
haftmann@31063
   477
fun prepare_sorts_typ prep_sort
haftmann@31063
   478
  = map_type_tfree (fn (v, sort) => TFree (v, prep_sort sort));
haftmann@31063
   479
haftmann@31063
   480
fun prepare_sorts prep_sort (Const (c, ty)) =
haftmann@31063
   481
      Const (c, prepare_sorts_typ prep_sort ty)
haftmann@30947
   482
  | prepare_sorts prep_sort (t1 $ t2) =
haftmann@30947
   483
      prepare_sorts prep_sort t1 $ prepare_sorts prep_sort t2
haftmann@30947
   484
  | prepare_sorts prep_sort (Abs (v, ty, t)) =
haftmann@31063
   485
      Abs (v, prepare_sorts_typ prep_sort ty, prepare_sorts prep_sort t)
haftmann@30947
   486
  | prepare_sorts _ (t as Bound _) = t;
haftmann@30010
   487
haftmann@30947
   488
fun gen_eval thy cterm_of conclude_evaluation prep_sort evaluator proto_ct =
haftmann@30942
   489
  let
haftmann@31063
   490
    val pp = Syntax.pp_global thy;
haftmann@30942
   491
    val ct = cterm_of proto_ct;
haftmann@31063
   492
    val _ = (Sign.no_frees pp o map_types (K dummyT) o Sign.no_vars pp)
haftmann@30947
   493
      (Thm.term_of ct);
haftmann@31125
   494
    val thm = preprocess_conv thy ct;
haftmann@30942
   495
    val ct' = Thm.rhs_of thm;
haftmann@30942
   496
    val t' = Thm.term_of ct';
haftmann@30947
   497
    val vs = Term.add_tfrees t' [];
haftmann@30947
   498
    val consts = fold_aterms
haftmann@30947
   499
      (fn Const (c, _) => insert (op =) c | _ => I) t' [];
haftmann@31063
   500
 
haftmann@30947
   501
    val t'' = prepare_sorts prep_sort t';
haftmann@30942
   502
    val (algebra', eqngr') = obtain thy consts [t''];
haftmann@30947
   503
  in conclude_evaluation (evaluator algebra' eqngr' vs t'' ct') thm end;
haftmann@30947
   504
haftmann@30947
   505
fun simple_evaluator evaluator algebra eqngr vs t ct =
haftmann@30947
   506
  evaluator algebra eqngr vs t;
haftmann@30010
   507
haftmann@30942
   508
fun eval_conv thy =
haftmann@30942
   509
  let
haftmann@30947
   510
    fun conclude_evaluation thm2 thm1 =
haftmann@30942
   511
      let
haftmann@31125
   512
        val thm3 = postprocess_conv thy (Thm.rhs_of thm2);
haftmann@30942
   513
      in
haftmann@30942
   514
        Thm.transitive thm1 (Thm.transitive thm2 thm3) handle THM _ =>
haftmann@30942
   515
          error ("could not construct evaluation proof:\n"
haftmann@30942
   516
          ^ (cat_lines o map Display.string_of_thm) [thm1, thm2, thm3])
haftmann@30942
   517
      end;
haftmann@30947
   518
  in gen_eval thy I conclude_evaluation end;
haftmann@30010
   519
haftmann@30970
   520
fun eval thy prep_sort postproc evaluator = gen_eval thy (Thm.cterm_of thy)
haftmann@31125
   521
  (K o postproc (postprocess_term thy)) prep_sort (simple_evaluator evaluator);
haftmann@31125
   522
haftmann@31125
   523
haftmann@31125
   524
(** setup **)
haftmann@31125
   525
haftmann@31125
   526
val setup = 
haftmann@31125
   527
  let
haftmann@31125
   528
    fun mk_attribute f = Thm.declaration_attribute (fn thm => Context.mapping (f thm) I);
haftmann@31125
   529
    fun add_del_attribute (name, (add, del)) =
haftmann@31125
   530
      Code.add_attribute (name, Args.del |-- Scan.succeed (mk_attribute del)
haftmann@31125
   531
        || Scan.succeed (mk_attribute add))
haftmann@31125
   532
  in
haftmann@31125
   533
    add_del_attribute ("inline", (add_inline, del_inline))
haftmann@31125
   534
    #> add_del_attribute ("post", (add_post, del_post))
haftmann@31125
   535
    #> Code.add_attribute ("unfold", Scan.succeed (Thm.declaration_attribute
haftmann@31125
   536
       (fn thm => Context.mapping (Codegen.add_unfold thm #> add_inline thm) I)))
haftmann@31125
   537
  end;
haftmann@31125
   538
haftmann@31125
   539
val _ =
haftmann@31125
   540
  OuterSyntax.improper_command "print_codeproc" "print code preprocessor setup"
haftmann@31125
   541
  OuterKeyword.diag (Scan.succeed
haftmann@31125
   542
      (Toplevel.no_timing o Toplevel.unknown_theory o Toplevel.keep
haftmann@31125
   543
        (print_codeproc o Toplevel.theory_of)));
haftmann@30010
   544
haftmann@30010
   545
end; (*struct*)