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