src/Tools/Code/code_preproc.ML
author blanchet
Mon, 15 Sep 2014 10:49:07 +0200
changeset 58335 a5a3b576fcfb
parent 57430 020cea57eaa4
child 58893 9e0ecb66d6a7
permissions -rw-r--r--
generate 'code' attribute only if 'code' plugin is enabled
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37744
3daaf23b9ab4 tuned titles
haftmann
parents: 37442
diff changeset
     1
(*  Title:      Tools/Code/code_preproc.ML
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
     2
    Author:     Florian Haftmann, TU Muenchen
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
     3
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
     4
Preprocessing code equations into a well-sorted system
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
     5
in a graph with explicit dependencies.
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
     6
*)
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
     7
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
     8
signature CODE_PREPROC =
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
     9
sig
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51685
diff changeset
    10
  val map_pre: (Proof.context -> Proof.context) -> theory -> theory
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51685
diff changeset
    11
  val map_post: (Proof.context -> Proof.context) -> theory -> theory
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
    12
  val add_functrans: string * (Proof.context -> (thm * bool) list -> (thm * bool) list option) -> theory -> theory
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    13
  val del_functrans: string -> theory -> theory
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
    14
  val simple_functrans: (Proof.context -> thm list -> thm list option)
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
    15
    -> Proof.context -> (thm * bool) list -> (thm * bool) list option
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
    16
  val print_codeproc: Proof.context -> unit
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    17
30947
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
    18
  type code_algebra
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
    19
  type code_graph
34891
99b9a6290446 code certificates as integral part of code generation
haftmann
parents: 34251
diff changeset
    20
  val cert: code_graph -> string -> Code.cert
32873
333945c9ac6a tuned handling of type variable names further
haftmann
parents: 32872
diff changeset
    21
  val sortargs: code_graph -> string -> sort list
30947
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
    22
  val all: code_graph -> string list
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
    23
  val pretty: Proof.context -> code_graph -> Pretty.T
39398
2e30660a2e21 ignore code cache optionally; corrected scope of term value in static_eval_conv
haftmann
parents: 39133
diff changeset
    24
  val obtain: bool -> theory -> string list -> term list -> code_algebra * code_graph
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
    25
  val dynamic_conv: Proof.context
56920
d651b944c67e normalizing of type variables before evaluation with explicit resubstitution function: make nbe work with funny type variables like \<AA>;
haftmann
parents: 56334
diff changeset
    26
    -> (code_algebra -> code_graph -> term -> conv) -> conv
56968
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
    27
  val dynamic_value: Proof.context -> ((term -> term) -> 'a -> 'b)
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
    28
    -> (code_algebra -> code_graph -> term -> 'a) -> term -> 'b
56973
62da80041afd syntactic means to prevent accidental mixup of static and dynamic context
haftmann
parents: 56971
diff changeset
    29
  val static_conv: { ctxt: Proof.context, consts: string list }
62da80041afd syntactic means to prevent accidental mixup of static and dynamic context
haftmann
parents: 56971
diff changeset
    30
    -> ({ algebra: code_algebra, eqngr: code_graph } -> Proof.context -> term -> conv)
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
    31
    -> Proof.context -> conv
56973
62da80041afd syntactic means to prevent accidental mixup of static and dynamic context
haftmann
parents: 56971
diff changeset
    32
  val static_value: { ctxt: Proof.context, lift_postproc: ((term -> term) -> 'a -> 'b), consts: string list }
62da80041afd syntactic means to prevent accidental mixup of static and dynamic context
haftmann
parents: 56971
diff changeset
    33
    -> ({ algebra: code_algebra, eqngr: code_graph } -> Proof.context -> term -> 'a)
56968
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
    34
    -> Proof.context -> term -> 'b
57430
020cea57eaa4 tracing facilities for the code generator preprocessor
haftmann
parents: 57429
diff changeset
    35
020cea57eaa4 tracing facilities for the code generator preprocessor
haftmann
parents: 57429
diff changeset
    36
  val trace_none: Context.generic -> Context.generic
020cea57eaa4 tracing facilities for the code generator preprocessor
haftmann
parents: 57429
diff changeset
    37
  val trace_all: Context.generic -> Context.generic
020cea57eaa4 tracing facilities for the code generator preprocessor
haftmann
parents: 57429
diff changeset
    38
  val trace_only: string list -> Context.generic -> Context.generic
020cea57eaa4 tracing facilities for the code generator preprocessor
haftmann
parents: 57429
diff changeset
    39
  val trace_only_ext: string list -> Context.generic -> Context.generic
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    40
end
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    41
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    42
structure Code_Preproc : CODE_PREPROC =
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    43
struct
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    44
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    45
(** preprocessor administration **)
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    46
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    47
(* theory data *)
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    48
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    49
datatype thmproc = Thmproc of {
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    50
  pre: simpset,
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    51
  post: simpset,
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
    52
  functrans: (string * (serial * (Proof.context -> (thm * bool) list -> (thm * bool) list option))) list
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    53
};
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    54
31599
97b4d289c646 tuned make/map/merge combinators
haftmann
parents: 31156
diff changeset
    55
fun make_thmproc ((pre, post), functrans) =
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    56
  Thmproc { pre = pre, post = post, functrans = functrans };
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    57
fun map_thmproc f (Thmproc { pre, post, functrans }) =
31599
97b4d289c646 tuned make/map/merge combinators
haftmann
parents: 31156
diff changeset
    58
  make_thmproc (f ((pre, post), functrans));
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    59
fun merge_thmproc (Thmproc { pre = pre1, post = post1, functrans = functrans1 },
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    60
  Thmproc { pre = pre2, post = post2, functrans = functrans2 }) =
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    61
    let
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    62
      val pre = Simplifier.merge_ss (pre1, pre2);
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    63
      val post = Simplifier.merge_ss (post1, post2);
34891
99b9a6290446 code certificates as integral part of code generation
haftmann
parents: 34251
diff changeset
    64
      val functrans = AList.merge (op =) (eq_fst (op =)) (functrans1, functrans2)
99b9a6290446 code certificates as integral part of code generation
haftmann
parents: 34251
diff changeset
    65
        handle AList.DUP => error ("Duplicate function transformer");
31599
97b4d289c646 tuned make/map/merge combinators
haftmann
parents: 31156
diff changeset
    66
    in make_thmproc ((pre, post), functrans) end;
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    67
33522
737589bb9bb8 adapted Theory_Data;
wenzelm
parents: 33063
diff changeset
    68
structure Code_Preproc_Data = Theory_Data
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    69
(
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    70
  type T = thmproc;
31599
97b4d289c646 tuned make/map/merge combinators
haftmann
parents: 31156
diff changeset
    71
  val empty = make_thmproc ((Simplifier.empty_ss, Simplifier.empty_ss), []);
33522
737589bb9bb8 adapted Theory_Data;
wenzelm
parents: 33063
diff changeset
    72
  val extend = I;
737589bb9bb8 adapted Theory_Data;
wenzelm
parents: 33063
diff changeset
    73
  val merge = merge_thmproc;
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    74
);
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    75
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    76
fun the_thmproc thy = case Code_Preproc_Data.get thy
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    77
 of Thmproc x => x;
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    78
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    79
fun delete_force msg key xs =
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    80
  if AList.defined (op =) xs key then AList.delete (op =) key xs
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    81
  else error ("No such " ^ msg ^ ": " ^ quote key);
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    82
38669
9ff76d0f0610 refined and unified naming convention for dynamic code evaluation techniques
haftmann
parents: 38291
diff changeset
    83
val map_data = Code_Preproc_Data.map o map_thmproc;
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    84
32072
d4bff63bcbf1 added code_unfold_post attribute
haftmann
parents: 31998
diff changeset
    85
val map_pre_post = map_data o apfst;
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51685
diff changeset
    86
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51685
diff changeset
    87
fun map_simpset which f thy =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51685
diff changeset
    88
  map_pre_post (which (simpset_map (Proof_Context.init_global thy) f)) thy;
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51685
diff changeset
    89
val map_pre = map_simpset apfst;
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51685
diff changeset
    90
val map_post = map_simpset apsnd;
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    91
56929
40213e24c8c4 delete attribute for code abbrev
haftmann
parents: 56920
diff changeset
    92
fun process_unfold add_del = map_pre o add_del;
40213e24c8c4 delete attribute for code abbrev
haftmann
parents: 56920
diff changeset
    93
fun process_post add_del = map_post o add_del;
32072
d4bff63bcbf1 added code_unfold_post attribute
haftmann
parents: 31998
diff changeset
    94
56929
40213e24c8c4 delete attribute for code abbrev
haftmann
parents: 56920
diff changeset
    95
fun process_abbrev add_del raw_thm thy =
32072
d4bff63bcbf1 added code_unfold_post attribute
haftmann
parents: 31998
diff changeset
    96
  let
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51685
diff changeset
    97
    val ctxt = Proof_Context.init_global thy;
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51685
diff changeset
    98
    val thm = Local_Defs.meta_rewrite_rule ctxt raw_thm;
32072
d4bff63bcbf1 added code_unfold_post attribute
haftmann
parents: 31998
diff changeset
    99
    val thm_sym = Thm.symmetric thm;
d4bff63bcbf1 added code_unfold_post attribute
haftmann
parents: 31998
diff changeset
   100
  in
d4bff63bcbf1 added code_unfold_post attribute
haftmann
parents: 31998
diff changeset
   101
    thy |> map_pre_post (fn (pre, post) =>
56929
40213e24c8c4 delete attribute for code abbrev
haftmann
parents: 56920
diff changeset
   102
      (pre |> simpset_map ctxt (add_del thm_sym),
40213e24c8c4 delete attribute for code abbrev
haftmann
parents: 56920
diff changeset
   103
       post |> simpset_map ctxt (add_del thm)))
32072
d4bff63bcbf1 added code_unfold_post attribute
haftmann
parents: 31998
diff changeset
   104
  end;
d4bff63bcbf1 added code_unfold_post attribute
haftmann
parents: 31998
diff changeset
   105
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   106
fun add_functrans (name, f) = (map_data o apsnd)
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   107
  (AList.update (op =) (name, (serial (), f)));
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   108
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   109
fun del_functrans name = (map_data o apsnd)
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   110
  (delete_force "function transformer" name);
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   111
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   112
56968
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   113
(* algebra of sandwiches: cterm transformations with pending postprocessors *)
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   114
56976
dc01225a2f77 clarified stylized status of sandwich algebra
haftmann
parents: 56973
diff changeset
   115
fun matches_transitive eq1 eq2 = Thm.rhs_of eq1 aconvc Thm.lhs_of eq2;
dc01225a2f77 clarified stylized status of sandwich algebra
haftmann
parents: 56973
diff changeset
   116
56968
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   117
fun trans_comb eq1 eq2 =
56976
dc01225a2f77 clarified stylized status of sandwich algebra
haftmann
parents: 56973
diff changeset
   118
  (*explicit assertions: evaluation conversion stacks are error-prone*)
dc01225a2f77 clarified stylized status of sandwich algebra
haftmann
parents: 56973
diff changeset
   119
  if Thm.is_reflexive eq1 then (@{assert} (matches_transitive eq1 eq2); eq2)
dc01225a2f77 clarified stylized status of sandwich algebra
haftmann
parents: 56973
diff changeset
   120
  else if Thm.is_reflexive eq2 then (@{assert} (matches_transitive eq1 eq2); eq1)
56968
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   121
  else Thm.transitive eq1 eq2;
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   122
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   123
fun trans_conv_rule conv eq = trans_comb eq (conv (Thm.rhs_of eq));
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   124
56976
dc01225a2f77 clarified stylized status of sandwich algebra
haftmann
parents: 56973
diff changeset
   125
structure Sandwich : sig
dc01225a2f77 clarified stylized status of sandwich algebra
haftmann
parents: 56973
diff changeset
   126
  type T = Proof.context -> cterm -> (thm -> thm) * cterm;
dc01225a2f77 clarified stylized status of sandwich algebra
haftmann
parents: 56973
diff changeset
   127
  val chain: T -> T -> T
dc01225a2f77 clarified stylized status of sandwich algebra
haftmann
parents: 56973
diff changeset
   128
  val lift: (Proof.context -> cterm -> (cterm -> thm) * thm) -> T
dc01225a2f77 clarified stylized status of sandwich algebra
haftmann
parents: 56973
diff changeset
   129
  val conversion: T -> (Proof.context -> term -> conv) -> Proof.context -> conv;
dc01225a2f77 clarified stylized status of sandwich algebra
haftmann
parents: 56973
diff changeset
   130
  val evaluation: T -> ((term -> term) -> 'a -> 'b) ->
dc01225a2f77 clarified stylized status of sandwich algebra
haftmann
parents: 56973
diff changeset
   131
    (Proof.context -> term -> 'a) -> Proof.context -> term -> 'b;
dc01225a2f77 clarified stylized status of sandwich algebra
haftmann
parents: 56973
diff changeset
   132
end = struct
dc01225a2f77 clarified stylized status of sandwich algebra
haftmann
parents: 56973
diff changeset
   133
dc01225a2f77 clarified stylized status of sandwich algebra
haftmann
parents: 56973
diff changeset
   134
type T = Proof.context -> cterm -> (thm -> thm) * cterm;
56968
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   135
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   136
fun chain sandwich2 sandwich1 ctxt =
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   137
  sandwich1 ctxt
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   138
  ##>> sandwich2 ctxt
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   139
  #>> (op o);
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   140
56976
dc01225a2f77 clarified stylized status of sandwich algebra
haftmann
parents: 56973
diff changeset
   141
fun lift conv_sandwhich ctxt ct =
56968
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   142
  let
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   143
    val (postproc_conv, eq) = conv_sandwhich ctxt ct;
56976
dc01225a2f77 clarified stylized status of sandwich algebra
haftmann
parents: 56973
diff changeset
   144
    fun potentail_trans_comb eq1 eq2 =
dc01225a2f77 clarified stylized status of sandwich algebra
haftmann
parents: 56973
diff changeset
   145
      if matches_transitive eq1 eq2 then trans_comb eq1 eq2 else eq2;
dc01225a2f77 clarified stylized status of sandwich algebra
haftmann
parents: 56973
diff changeset
   146
        (*weakened protocol for plain term evaluation*)
dc01225a2f77 clarified stylized status of sandwich algebra
haftmann
parents: 56973
diff changeset
   147
  in (trans_conv_rule postproc_conv o potentail_trans_comb eq, Thm.rhs_of eq) end;
56968
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   148
56976
dc01225a2f77 clarified stylized status of sandwich algebra
haftmann
parents: 56973
diff changeset
   149
fun conversion sandwich conv ctxt ct =
56968
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   150
  let
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   151
    val (postproc, ct') = sandwich ctxt ct;
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   152
  in postproc (conv ctxt (term_of ct') ct') end;
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   153
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   154
fun evaluation sandwich lift_postproc eval ctxt t =
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   155
  let
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   156
    val cert = Thm.cterm_of (Proof_Context.theory_of ctxt);
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   157
    val (postproc, ct') = sandwich ctxt (cert t);
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   158
  in
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   159
    term_of ct'
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   160
    |> eval ctxt
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   161
    |> lift_postproc (term_of o Thm.rhs_of o postproc o Thm.reflexive o cert)
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   162
  end;
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   163
56976
dc01225a2f77 clarified stylized status of sandwich algebra
haftmann
parents: 56973
diff changeset
   164
end;
dc01225a2f77 clarified stylized status of sandwich algebra
haftmann
parents: 56973
diff changeset
   165
56968
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   166
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   167
(* post- and preprocessing *)
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   168
56968
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   169
fun normalized_tfrees_sandwich ctxt ct =
56967
c3746e999805 normalize type variables of evaluation term by conversion
haftmann
parents: 56929
diff changeset
   170
  let
c3746e999805 normalize type variables of evaluation term by conversion
haftmann
parents: 56929
diff changeset
   171
    val cert = cterm_of (Proof_Context.theory_of ctxt);
c3746e999805 normalize type variables of evaluation term by conversion
haftmann
parents: 56929
diff changeset
   172
    val t = term_of ct;
c3746e999805 normalize type variables of evaluation term by conversion
haftmann
parents: 56929
diff changeset
   173
    val vs_original = fold_term_types (K (fold_atyps (insert (eq_fst op =)
c3746e999805 normalize type variables of evaluation term by conversion
haftmann
parents: 56929
diff changeset
   174
      o dest_TFree))) t [];
c3746e999805 normalize type variables of evaluation term by conversion
haftmann
parents: 56929
diff changeset
   175
    val vs_normalized = Name.invent_names Name.context Name.aT (map snd vs_original);
c3746e999805 normalize type variables of evaluation term by conversion
haftmann
parents: 56929
diff changeset
   176
    val normalize = map_type_tfree (TFree o the o AList.lookup (op =) (vs_original ~~ vs_normalized));
c3746e999805 normalize type variables of evaluation term by conversion
haftmann
parents: 56929
diff changeset
   177
    val normalization =
c3746e999805 normalize type variables of evaluation term by conversion
haftmann
parents: 56929
diff changeset
   178
      map2 (fn (v, sort) => fn (v', _) => (((v', 0), sort), TFree (v, sort))) vs_original vs_normalized;
c3746e999805 normalize type variables of evaluation term by conversion
haftmann
parents: 56929
diff changeset
   179
  in
56971
f4942eb3bb03 optimization for trivial cases
haftmann
parents: 56970
diff changeset
   180
    if eq_list (eq_fst (op =)) (vs_normalized, vs_original)
f4942eb3bb03 optimization for trivial cases
haftmann
parents: 56970
diff changeset
   181
    then (I, ct)
f4942eb3bb03 optimization for trivial cases
haftmann
parents: 56970
diff changeset
   182
    else (Thm.certify_instantiate (normalization, []) o Thm.varifyT_global, cert (map_types normalize t))
56967
c3746e999805 normalize type variables of evaluation term by conversion
haftmann
parents: 56929
diff changeset
   183
  end;
c3746e999805 normalize type variables of evaluation term by conversion
haftmann
parents: 56929
diff changeset
   184
56968
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   185
fun no_variables_sandwich ctxt ct =
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   186
  let
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   187
    val thy = Proof_Context.theory_of ctxt;
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   188
    val cert = Thm.cterm_of thy;
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   189
    val all_vars = fold_aterms (fn t as Free _ => insert (op aconvc) (cert t)
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   190
      | t as Var _ => insert (op aconvc) (cert t)
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   191
      | _ => I) (Thm.term_of ct) [];
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   192
    fun apply_beta var thm = Thm.combination thm (Thm.reflexive var)
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   193
      |> Conv.fconv_rule (Conv.arg_conv (Conv.try_conv (Thm.beta_conversion false)))
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   194
      |> Conv.fconv_rule (Conv.arg1_conv (Thm.beta_conversion false));
56971
f4942eb3bb03 optimization for trivial cases
haftmann
parents: 56970
diff changeset
   195
  in
f4942eb3bb03 optimization for trivial cases
haftmann
parents: 56970
diff changeset
   196
    if null all_vars
f4942eb3bb03 optimization for trivial cases
haftmann
parents: 56970
diff changeset
   197
    then (I, ct)
f4942eb3bb03 optimization for trivial cases
haftmann
parents: 56970
diff changeset
   198
    else (fold apply_beta all_vars, fold_rev Thm.lambda all_vars ct)
f4942eb3bb03 optimization for trivial cases
haftmann
parents: 56970
diff changeset
   199
  end;
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   200
56968
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   201
fun simplifier_conv_sandwich ctxt =
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   202
  let
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   203
    val thy = Proof_Context.theory_of ctxt;
56968
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   204
    val pre = (#pre o the_thmproc) thy;
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   205
    val post = (#post o the_thmproc) thy;
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   206
    fun pre_conv ctxt' =
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   207
      Simplifier.rewrite (put_simpset pre ctxt')
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   208
      #> trans_conv_rule (Axclass.unoverload_conv (Proof_Context.theory_of ctxt'))
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   209
    fun post_conv ctxt' =
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   210
      Axclass.overload_conv (Proof_Context.theory_of ctxt')
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   211
      #> trans_conv_rule (Simplifier.rewrite (put_simpset post ctxt'))
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   212
  in fn ctxt' => pre_conv ctxt' #> pair (post_conv ctxt') end;
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   213
56976
dc01225a2f77 clarified stylized status of sandwich algebra
haftmann
parents: 56973
diff changeset
   214
fun simplifier_sandwich ctxt = Sandwich.lift (simplifier_conv_sandwich ctxt);
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   215
56968
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   216
fun value_sandwich ctxt =
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   217
  normalized_tfrees_sandwich
56976
dc01225a2f77 clarified stylized status of sandwich algebra
haftmann
parents: 56973
diff changeset
   218
  |> Sandwich.chain no_variables_sandwich
dc01225a2f77 clarified stylized status of sandwich algebra
haftmann
parents: 56973
diff changeset
   219
  |> Sandwich.chain (simplifier_sandwich ctxt);
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   220
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   221
fun print_codeproc ctxt =
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   222
  let
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   223
    val thy = Proof_Context.theory_of ctxt;
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   224
    val pre = (#pre o the_thmproc) thy;
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   225
    val post = (#post o the_thmproc) thy;
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   226
    val functrans = (map fst o #functrans o the_thmproc) thy;
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   227
  in
56334
6b3739fee456 some shortcuts for chunks, which sometimes avoid bulky string output;
wenzelm
parents: 55757
diff changeset
   228
    Pretty.writeln_chunks [
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   229
      Pretty.block [
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   230
        Pretty.str "preprocessing simpset:",
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   231
        Pretty.fbrk,
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51685
diff changeset
   232
        Simplifier.pretty_simpset (put_simpset pre ctxt)
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   233
      ],
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   234
      Pretty.block [
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   235
        Pretty.str "postprocessing simpset:",
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   236
        Pretty.fbrk,
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51685
diff changeset
   237
        Simplifier.pretty_simpset (put_simpset post ctxt)
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   238
      ],
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   239
      Pretty.block (
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   240
        Pretty.str "function transformers:"
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   241
        :: Pretty.fbrk
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   242
        :: (Pretty.fbreaks o map Pretty.str) functrans
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   243
      )
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   244
    ]
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   245
  end;
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   246
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   247
fun simple_functrans f ctxt eqns = case f ctxt (map fst eqns)
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   248
 of SOME thms' => SOME (map (rpair (forall snd eqns)) thms')
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   249
  | NONE => NONE;
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   250
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   251
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   252
(** sort algebra and code equation graph types **)
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   253
30947
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
   254
type code_algebra = (sort -> sort) * Sorts.algebra;
34891
99b9a6290446 code certificates as integral part of code generation
haftmann
parents: 34251
diff changeset
   255
type code_graph = ((string * sort) list * Code.cert) Graph.T;
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   256
39604
f17fb9ccb836 avoid frees and vars in terms to be evaluated by abstracting and applying
haftmann
parents: 39475
diff changeset
   257
fun get_node eqngr const = Graph.get_node eqngr const
f17fb9ccb836 avoid frees and vars in terms to be evaluated by abstracting and applying
haftmann
parents: 39475
diff changeset
   258
  handle Graph.UNDEF _ => error ("No such constant in code equation graph: " ^ quote const);
f17fb9ccb836 avoid frees and vars in terms to be evaluated by abstracting and applying
haftmann
parents: 39475
diff changeset
   259
f17fb9ccb836 avoid frees and vars in terms to be evaluated by abstracting and applying
haftmann
parents: 39475
diff changeset
   260
fun cert eqngr = snd o get_node eqngr;
f17fb9ccb836 avoid frees and vars in terms to be evaluated by abstracting and applying
haftmann
parents: 39475
diff changeset
   261
fun sortargs eqngr = map snd o fst o get_node eqngr;
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   262
fun all eqngr = Graph.keys eqngr;
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   263
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   264
fun pretty ctxt eqngr =
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   265
  let
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   266
    val thy = Proof_Context.theory_of ctxt;
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   267
  in
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   268
    AList.make (snd o Graph.get_node eqngr) (Graph.keys eqngr)
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   269
    |> (map o apfst) (Code.string_of_const thy)
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   270
    |> sort (string_ord o pairself fst)
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   271
    |> map (fn (s, cert) => (Pretty.block o Pretty.fbreaks) (Pretty.str s :: Code.pretty_cert thy cert))
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   272
    |> Pretty.chunks
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   273
  end;
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   274
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   275
57430
020cea57eaa4 tracing facilities for the code generator preprocessor
haftmann
parents: 57429
diff changeset
   276
(** simplifier tracing **)
020cea57eaa4 tracing facilities for the code generator preprocessor
haftmann
parents: 57429
diff changeset
   277
020cea57eaa4 tracing facilities for the code generator preprocessor
haftmann
parents: 57429
diff changeset
   278
structure Trace_Switch = Generic_Data
020cea57eaa4 tracing facilities for the code generator preprocessor
haftmann
parents: 57429
diff changeset
   279
(
020cea57eaa4 tracing facilities for the code generator preprocessor
haftmann
parents: 57429
diff changeset
   280
  type T = string list option;
020cea57eaa4 tracing facilities for the code generator preprocessor
haftmann
parents: 57429
diff changeset
   281
  val empty = SOME [];
020cea57eaa4 tracing facilities for the code generator preprocessor
haftmann
parents: 57429
diff changeset
   282
  val extend = I;
020cea57eaa4 tracing facilities for the code generator preprocessor
haftmann
parents: 57429
diff changeset
   283
  fun merge (NONE, d2) = NONE
020cea57eaa4 tracing facilities for the code generator preprocessor
haftmann
parents: 57429
diff changeset
   284
    | merge (d1, NONE) = NONE
020cea57eaa4 tracing facilities for the code generator preprocessor
haftmann
parents: 57429
diff changeset
   285
    | merge (SOME cs1, SOME cs2) = SOME (Library.merge (op =) (cs1, cs2));
020cea57eaa4 tracing facilities for the code generator preprocessor
haftmann
parents: 57429
diff changeset
   286
);
020cea57eaa4 tracing facilities for the code generator preprocessor
haftmann
parents: 57429
diff changeset
   287
020cea57eaa4 tracing facilities for the code generator preprocessor
haftmann
parents: 57429
diff changeset
   288
val trace_none = Trace_Switch.put (SOME []);
020cea57eaa4 tracing facilities for the code generator preprocessor
haftmann
parents: 57429
diff changeset
   289
020cea57eaa4 tracing facilities for the code generator preprocessor
haftmann
parents: 57429
diff changeset
   290
val trace_all = Trace_Switch.put NONE;
020cea57eaa4 tracing facilities for the code generator preprocessor
haftmann
parents: 57429
diff changeset
   291
020cea57eaa4 tracing facilities for the code generator preprocessor
haftmann
parents: 57429
diff changeset
   292
fun gen_trace_only prep_const raw_cs context =
020cea57eaa4 tracing facilities for the code generator preprocessor
haftmann
parents: 57429
diff changeset
   293
  let
020cea57eaa4 tracing facilities for the code generator preprocessor
haftmann
parents: 57429
diff changeset
   294
    val cs = map (prep_const (Context.theory_of context)) raw_cs;
020cea57eaa4 tracing facilities for the code generator preprocessor
haftmann
parents: 57429
diff changeset
   295
  in Trace_Switch.put (SOME cs) context end;
020cea57eaa4 tracing facilities for the code generator preprocessor
haftmann
parents: 57429
diff changeset
   296
020cea57eaa4 tracing facilities for the code generator preprocessor
haftmann
parents: 57429
diff changeset
   297
val trace_only = gen_trace_only (K I);
020cea57eaa4 tracing facilities for the code generator preprocessor
haftmann
parents: 57429
diff changeset
   298
val trace_only_ext = gen_trace_only Code.read_const;
020cea57eaa4 tracing facilities for the code generator preprocessor
haftmann
parents: 57429
diff changeset
   299
020cea57eaa4 tracing facilities for the code generator preprocessor
haftmann
parents: 57429
diff changeset
   300
fun switch_trace c ctxt =
020cea57eaa4 tracing facilities for the code generator preprocessor
haftmann
parents: 57429
diff changeset
   301
  let
020cea57eaa4 tracing facilities for the code generator preprocessor
haftmann
parents: 57429
diff changeset
   302
    val d = Trace_Switch.get (Context.Proof ctxt);
020cea57eaa4 tracing facilities for the code generator preprocessor
haftmann
parents: 57429
diff changeset
   303
    val switch = case d of NONE => true | SOME cs => member (op =) cs c;
020cea57eaa4 tracing facilities for the code generator preprocessor
haftmann
parents: 57429
diff changeset
   304
    val _ = if switch
020cea57eaa4 tracing facilities for the code generator preprocessor
haftmann
parents: 57429
diff changeset
   305
      then tracing ("Preprocessing function equations for "
020cea57eaa4 tracing facilities for the code generator preprocessor
haftmann
parents: 57429
diff changeset
   306
        ^ Code.string_of_const (Proof_Context.theory_of ctxt) c)
020cea57eaa4 tracing facilities for the code generator preprocessor
haftmann
parents: 57429
diff changeset
   307
      else ();
020cea57eaa4 tracing facilities for the code generator preprocessor
haftmann
parents: 57429
diff changeset
   308
  in Config.put simp_trace switch ctxt end;
020cea57eaa4 tracing facilities for the code generator preprocessor
haftmann
parents: 57429
diff changeset
   309
020cea57eaa4 tracing facilities for the code generator preprocessor
haftmann
parents: 57429
diff changeset
   310
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   311
(** the Waisenhaus algorithm **)
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   312
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   313
(* auxiliary *)
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   314
51685
385ef6706252 more standard module name Axclass (according to file name);
wenzelm
parents: 51658
diff changeset
   315
fun is_proper_class thy = can (Axclass.get_info thy);
30942
1e246776f876 diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents: 30876
diff changeset
   316
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   317
fun complete_proper_sort thy =
30942
1e246776f876 diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents: 30876
diff changeset
   318
  Sign.complete_sort thy #> filter (is_proper_class thy);
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   319
30029
d14d0b4bf5b4 also consider superclasses properly
haftmann
parents: 30024
diff changeset
   320
fun inst_params thy tyco =
51685
385ef6706252 more standard module name Axclass (according to file name);
wenzelm
parents: 51658
diff changeset
   321
  map (fn (c, _) => Axclass.param_of_inst thy (c, tyco))
385ef6706252 more standard module name Axclass (according to file name);
wenzelm
parents: 51658
diff changeset
   322
    o maps (#params o Axclass.get_info thy);
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   323
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   324
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   325
(* data structures *)
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   326
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   327
datatype const = Fun of string | Inst of class * string;
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   328
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   329
fun const_ord (Fun c1, Fun c2) = fast_string_ord (c1, c2)
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   330
  | const_ord (Inst class_tyco1, Inst class_tyco2) =
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   331
      prod_ord fast_string_ord fast_string_ord (class_tyco1, class_tyco2)
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   332
  | const_ord (Fun _, Inst _) = LESS
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   333
  | const_ord (Inst _, Fun _) = GREATER;
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   334
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   335
type var = const * int;
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   336
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   337
structure Vargraph =
31971
8c1b845ed105 renamed functor TableFun to Table, and GraphFun to Graph;
wenzelm
parents: 31962
diff changeset
   338
  Graph(type key = var val ord = prod_ord const_ord int_ord);
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   339
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   340
datatype styp = Tyco of string * styp list | Var of var | Free;
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   341
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   342
fun styp_of c_lhs (Type (tyco, tys)) = Tyco (tyco, map (styp_of c_lhs) tys)
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   343
  | styp_of c_lhs (TFree (v, _)) = case c_lhs
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   344
     of SOME (c, lhs) => Var (Fun c, find_index (fn (v', _) => v = v') lhs)
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   345
      | NONE => Free;
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   346
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   347
type vardeps_data = ((string * styp list) list * class list) Vargraph.T
34891
99b9a6290446 code certificates as integral part of code generation
haftmann
parents: 34251
diff changeset
   348
  * (((string * sort) list * Code.cert) Symtab.table
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   349
    * (class * string) list);
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   350
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   351
val empty_vardeps_data : vardeps_data =
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   352
  (Vargraph.empty, (Symtab.empty, []));
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   353
30876
613c2eb8aef6 tuned whitespace
haftmann
parents: 30769
diff changeset
   354
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   355
(* retrieving equations and instances from the background context *)
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   356
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   357
fun obtain_eqns ctxt eqngr c =
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   358
  case try (Graph.get_node eqngr) c
34891
99b9a6290446 code certificates as integral part of code generation
haftmann
parents: 34251
diff changeset
   359
   of SOME (lhs, cert) => ((lhs, []), cert)
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   360
    | NONE => let
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   361
        val thy = Proof_Context.theory_of ctxt;
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   362
        val functrans = (map (fn (_, (_, f)) => f ctxt)
48075
ec5e62b868eb apply preprocessing simpset also to rhs of abstract code equations
haftmann
parents: 47005
diff changeset
   363
          o #functrans o the_thmproc) thy;
57430
020cea57eaa4 tracing facilities for the code generator preprocessor
haftmann
parents: 57429
diff changeset
   364
        val cert = Code.get_cert (switch_trace c ctxt) functrans c;
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   365
        val (lhs, rhss) =
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   366
          Code.typargs_deps_of_cert thy cert;
34891
99b9a6290446 code certificates as integral part of code generation
haftmann
parents: 34251
diff changeset
   367
      in ((lhs, rhss), cert) end;
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   368
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   369
fun obtain_instance ctxt arities (inst as (class, tyco)) =
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   370
  case AList.lookup (op =) arities inst
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   371
   of SOME classess => (classess, ([], []))
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   372
    | NONE => let
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   373
        val thy = Proof_Context.theory_of ctxt;
30029
d14d0b4bf5b4 also consider superclasses properly
haftmann
parents: 30024
diff changeset
   374
        val all_classes = complete_proper_sort thy [class];
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 36960
diff changeset
   375
        val super_classes = remove (op =) class all_classes;
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   376
        val classess = map (complete_proper_sort thy)
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   377
          (Sign.arity_sorts thy tyco [class]);
30029
d14d0b4bf5b4 also consider superclasses properly
haftmann
parents: 30024
diff changeset
   378
        val inst_params = inst_params thy tyco all_classes;
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 36960
diff changeset
   379
      in (classess, (super_classes, inst_params)) end;
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   380
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   381
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   382
(* computing instantiations *)
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   383
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   384
fun add_classes ctxt arities eqngr c_k new_classes vardeps_data =
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   385
  let
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   386
    val (styps, old_classes) = Vargraph.get_node (fst vardeps_data) c_k;
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   387
    val diff_classes = new_classes |> subtract (op =) old_classes;
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   388
  in if null diff_classes then vardeps_data
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   389
  else let
44338
700008399ee5 refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
wenzelm
parents: 43122
diff changeset
   390
    val c_ks = Vargraph.immediate_succs (fst vardeps_data) c_k |> insert (op =) c_k;
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   391
  in
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   392
    vardeps_data
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   393
    |> (apfst o Vargraph.map_node c_k o apsnd) (append diff_classes)
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   394
    |> fold (fn styp => fold (ensure_typmatch_inst ctxt arities eqngr styp) new_classes) styps
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   395
    |> fold (fn c_k => add_classes ctxt arities eqngr c_k diff_classes) c_ks
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   396
  end end
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   397
and add_styp ctxt arities eqngr c_k new_tyco_styps vardeps_data =
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   398
  let
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 36960
diff changeset
   399
    val (old_tyco_stypss, classes) = Vargraph.get_node (fst vardeps_data) c_k;
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 36960
diff changeset
   400
  in if member (op =) old_tyco_stypss new_tyco_styps then vardeps_data
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   401
  else
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   402
    vardeps_data
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 36960
diff changeset
   403
    |> (apfst o Vargraph.map_node c_k o apfst) (cons new_tyco_styps)
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   404
    |> fold (ensure_typmatch_inst ctxt arities eqngr new_tyco_styps) classes
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   405
  end
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   406
and add_dep ctxt arities eqngr c_k c_k' vardeps_data =
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   407
  let
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   408
    val (_, classes) = Vargraph.get_node (fst vardeps_data) c_k;
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   409
  in
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   410
    vardeps_data
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   411
    |> add_classes ctxt arities eqngr c_k' classes
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   412
    |> apfst (Vargraph.add_edge (c_k, c_k'))
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   413
  end
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   414
and ensure_typmatch_inst ctxt arities eqngr (tyco, styps) class vardeps_data =
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   415
  if can (Sign.arity_sorts (Proof_Context.theory_of ctxt) tyco) [class]
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   416
  then vardeps_data
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   417
    |> ensure_inst ctxt arities eqngr (class, tyco)
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   418
    |> fold_index (fn (k, styp) =>
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   419
         ensure_typmatch ctxt arities eqngr styp (Inst (class, tyco), k)) styps
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   420
  else vardeps_data (*permissive!*)
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   421
and ensure_inst ctxt arities eqngr (inst as (class, tyco)) (vardeps_data as (_, (_, insts))) =
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   422
  if member (op =) insts inst then vardeps_data
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   423
  else let
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 36960
diff changeset
   424
    val (classess, (super_classes, inst_params)) =
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   425
      obtain_instance ctxt arities inst;
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   426
  in
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   427
    vardeps_data
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   428
    |> (apsnd o apsnd) (insert (op =) inst)
30083
41a20af1fb77 robustified
haftmann
parents: 30075
diff changeset
   429
    |> fold_index (fn (k, _) =>
41a20af1fb77 robustified
haftmann
parents: 30075
diff changeset
   430
         apfst (Vargraph.new_node ((Inst (class, tyco), k), ([] ,[])))) classess
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   431
    |> fold (fn super_class => ensure_inst ctxt arities eqngr (super_class, tyco)) super_classes
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   432
    |> fold (ensure_fun ctxt arities eqngr) inst_params
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   433
    |> fold_index (fn (k, classes) =>
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   434
         add_classes ctxt arities eqngr (Inst (class, tyco), k) classes
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 36960
diff changeset
   435
         #> fold (fn super_class =>
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   436
             add_dep ctxt arities eqngr (Inst (super_class, tyco), k)
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 36960
diff changeset
   437
             (Inst (class, tyco), k)) super_classes
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   438
         #> fold (fn inst_param =>
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   439
             add_dep ctxt arities eqngr (Fun inst_param, k)
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   440
             (Inst (class, tyco), k)
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   441
             ) inst_params
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   442
         ) classess
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   443
  end
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   444
and ensure_typmatch ctxt arities eqngr (Tyco tyco_styps) c_k vardeps_data =
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   445
      vardeps_data
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   446
      |> add_styp ctxt arities eqngr c_k tyco_styps
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   447
  | ensure_typmatch ctxt arities eqngr (Var c_k') c_k vardeps_data =
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   448
      vardeps_data
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   449
      |> add_dep ctxt arities eqngr c_k c_k'
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   450
  | ensure_typmatch ctxt arities eqngr Free c_k vardeps_data =
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   451
      vardeps_data
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   452
and ensure_rhs ctxt arities eqngr (c', styps) vardeps_data =
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   453
  vardeps_data
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   454
  |> ensure_fun ctxt arities eqngr c'
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   455
  |> fold_index (fn (k, styp) =>
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   456
       ensure_typmatch ctxt arities eqngr styp (Fun c', k)) styps
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   457
and ensure_fun ctxt arities eqngr c (vardeps_data as (_, (eqntab, _))) =
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   458
  if Symtab.defined eqntab c then vardeps_data
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   459
  else let
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   460
    val ((lhs, rhss), eqns) = obtain_eqns ctxt eqngr c;
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   461
    val rhss' = (map o apsnd o map) (styp_of (SOME (c, lhs))) rhss;
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   462
  in
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   463
    vardeps_data
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   464
    |> (apsnd o apfst) (Symtab.update_new (c, (lhs, eqns)))
30083
41a20af1fb77 robustified
haftmann
parents: 30075
diff changeset
   465
    |> fold_index (fn (k, _) =>
41a20af1fb77 robustified
haftmann
parents: 30075
diff changeset
   466
         apfst (Vargraph.new_node ((Fun c, k), ([] ,[])))) lhs
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   467
    |> fold_index (fn (k, (_, sort)) => add_classes ctxt arities eqngr (Fun c, k)
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   468
         (complete_proper_sort (Proof_Context.theory_of ctxt) sort)) lhs
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   469
    |> fold (ensure_rhs ctxt arities eqngr) rhss'
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   470
  end;
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   471
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   472
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   473
(* applying instantiations *)
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   474
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   475
fun dicts_of ctxt (proj_sort, algebra) (T, sort) =
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   476
  let
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   477
    val thy = Proof_Context.theory_of ctxt;
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   478
    fun class_relation (x, _) _ = x;
36102
a51d1d154c71 of_sort_derivation: pass-through full type information -- following the version by krauss/schropp;
wenzelm
parents: 35624
diff changeset
   479
    fun type_constructor (tyco, _) xs class =
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   480
      inst_params thy tyco (Sorts.complete_sort algebra [class])
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   481
        @ (maps o maps) fst xs;
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   482
    fun type_variable (TFree (_, sort)) = map (pair []) (proj_sort sort);
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   483
  in
32795
a0f38d8d633a Sorts.of_sort_derivation: no pp here;
wenzelm
parents: 32544
diff changeset
   484
    flat (Sorts.of_sort_derivation algebra
36102
a51d1d154c71 of_sort_derivation: pass-through full type information -- following the version by krauss/schropp;
wenzelm
parents: 35624
diff changeset
   485
      { class_relation = K class_relation, type_constructor = type_constructor,
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   486
        type_variable = type_variable } (T, proj_sort sort)
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   487
       handle Sorts.CLASS_ERROR _ => [] (*permissive!*))
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   488
  end;
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   489
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   490
fun add_arity ctxt vardeps (class, tyco) =
33063
4d462963a7db map_range (and map_index) combinator
haftmann
parents: 32873
diff changeset
   491
  AList.default (op =) ((class, tyco),
34891
99b9a6290446 code certificates as integral part of code generation
haftmann
parents: 34251
diff changeset
   492
    map_range (fn k => (snd o Vargraph.get_node vardeps) (Inst (class, tyco), k))
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   493
      (Sign.arity_number (Proof_Context.theory_of ctxt) tyco));
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   494
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   495
fun add_cert ctxt vardeps (c, (proto_lhs, proto_cert)) (rhss, eqngr) =
30058
f84c2412e870 more liberality needed
haftmann
parents: 30054
diff changeset
   496
  if can (Graph.get_node eqngr) c then (rhss, eqngr)
f84c2412e870 more liberality needed
haftmann
parents: 30054
diff changeset
   497
  else let
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   498
    val thy = Proof_Context.theory_of ctxt;
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   499
    val lhs = map_index (fn (k, (v, _)) =>
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   500
      (v, snd (Vargraph.get_node vardeps (Fun c, k)))) proto_lhs;
49971
8b50286c36d3 close code theorems explicitly after preprocessing
haftmann
parents: 48075
diff changeset
   501
    val cert = proto_cert
8b50286c36d3 close code theorems explicitly after preprocessing
haftmann
parents: 48075
diff changeset
   502
      |> Code.constrain_cert thy (map (Sign.minimize_sort thy o snd) lhs)
8b50286c36d3 close code theorems explicitly after preprocessing
haftmann
parents: 48075
diff changeset
   503
      |> Code.conclude_cert;
35224
1c9866c5f6fb simplified
haftmann
parents: 34895
diff changeset
   504
    val (vs, rhss') = Code.typargs_deps_of_cert thy cert;
34891
99b9a6290446 code certificates as integral part of code generation
haftmann
parents: 34251
diff changeset
   505
    val eqngr' = Graph.new_node (c, (vs, cert)) eqngr;
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   506
  in (map (pair c) rhss' @ rhss, eqngr') end;
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   507
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   508
fun extend_arities_eqngr raw_ctxt cs ts (arities, (eqngr : code_graph)) =
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   509
  let
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   510
    val thy = Proof_Context.theory_of raw_ctxt;
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   511
    val {pre, ...} = the_thmproc thy;
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   512
    val ctxt = put_simpset pre raw_ctxt;
30942
1e246776f876 diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents: 30876
diff changeset
   513
    val cs_rhss = (fold o fold_aterms) (fn Const (c_ty as (c, _)) =>
1e246776f876 diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents: 30876
diff changeset
   514
      insert (op =) (c, (map (styp_of NONE) o Sign.const_typargs thy) c_ty) | _ => I) ts [];
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   515
    val (vardeps, (eqntab, insts)) = empty_vardeps_data
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   516
      |> fold (ensure_fun ctxt arities eqngr) cs
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   517
      |> fold (ensure_rhs ctxt arities eqngr) cs_rhss;
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   518
    val arities' = fold (add_arity ctxt vardeps) insts arities;
47005
421760a1efe7 maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
wenzelm
parents: 46961
diff changeset
   519
    val algebra = Sorts.subalgebra (Context.pretty_global thy) (is_proper_class thy)
30064
3cd19b113854 use canonical subalgebra projection
haftmann
parents: 30061
diff changeset
   520
      (AList.lookup (op =) arities') (Sign.classes_of thy);
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   521
    val (rhss, eqngr') = Symtab.fold (add_cert ctxt vardeps) eqntab ([], eqngr);
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   522
    fun deps_of (c, rhs) = c :: maps (dicts_of ctxt algebra)
32873
333945c9ac6a tuned handling of type variable names further
haftmann
parents: 32872
diff changeset
   523
      (rhs ~~ sortargs eqngr' c);
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   524
    val eqngr'' = fold (fn (c, rhs) => fold
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   525
      (curry Graph.add_edge c) (deps_of rhs)) rhss eqngr';
30942
1e246776f876 diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents: 30876
diff changeset
   526
  in (algebra, (arities', eqngr'')) end;
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   527
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   528
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   529
(** store for preprocessed arities and code equations **)
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   530
34173
458ced35abb8 reduced code generator cache to the baremost minimum
haftmann
parents: 33942
diff changeset
   531
structure Wellsorted = Code_Data
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   532
(
30947
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
   533
  type T = ((string * class) * sort list) list * code_graph;
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   534
  val empty = ([], Graph.empty);
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   535
);
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   536
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   537
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   538
(** retrieval and evaluation interfaces **)
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   539
39398
2e30660a2e21 ignore code cache optionally; corrected scope of term value in static_eval_conv
haftmann
parents: 39133
diff changeset
   540
fun obtain ignore_cache thy consts ts = apsnd snd
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   541
  (Wellsorted.change_yield (if ignore_cache then NONE else SOME thy)
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   542
    (extend_arities_eqngr (Proof_Context.init_global thy) consts ts));
38670
3c7db0192db9 added static_eval_conv
haftmann
parents: 38669
diff changeset
   543
56968
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   544
fun dynamic_evaluator eval ctxt t =
30942
1e246776f876 diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents: 30876
diff changeset
   545
  let
30947
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
   546
    val consts = fold_aterms
56968
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   547
      (fn Const (c, _) => insert (op =) c | _ => I) t [];
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   548
    val (algebra, eqngr) = obtain false (Proof_Context.theory_of ctxt) consts [t];
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   549
  in eval algebra eqngr t end;
30947
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
   550
56968
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   551
fun dynamic_conv ctxt conv =
56976
dc01225a2f77 clarified stylized status of sandwich algebra
haftmann
parents: 56973
diff changeset
   552
  Sandwich.conversion (value_sandwich ctxt) (dynamic_evaluator conv) ctxt;
56968
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   553
d2b1d95eb722 unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
haftmann
parents: 56967
diff changeset
   554
fun dynamic_value ctxt lift_postproc evaluator =
56976
dc01225a2f77 clarified stylized status of sandwich algebra
haftmann
parents: 56973
diff changeset
   555
  Sandwich.evaluation (value_sandwich ctxt) lift_postproc (dynamic_evaluator evaluator) ctxt;
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   556
56973
62da80041afd syntactic means to prevent accidental mixup of static and dynamic context
haftmann
parents: 56971
diff changeset
   557
fun static_conv { ctxt, consts } conv =
38670
3c7db0192db9 added static_eval_conv
haftmann
parents: 38669
diff changeset
   558
  let
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   559
    val (algebra, eqngr) = obtain true (Proof_Context.theory_of ctxt) consts [];
56976
dc01225a2f77 clarified stylized status of sandwich algebra
haftmann
parents: 56973
diff changeset
   560
  in Sandwich.conversion (value_sandwich ctxt) (conv { algebra = algebra, eqngr = eqngr }) end;
38670
3c7db0192db9 added static_eval_conv
haftmann
parents: 38669
diff changeset
   561
56973
62da80041afd syntactic means to prevent accidental mixup of static and dynamic context
haftmann
parents: 56971
diff changeset
   562
fun static_value { ctxt, lift_postproc, consts } evaluator =
39475
9cc1ba3c5706 separation of static and dynamic thy context
haftmann
parents: 39398
diff changeset
   563
  let
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   564
    val (algebra, eqngr) = obtain true (Proof_Context.theory_of ctxt) consts [];
56976
dc01225a2f77 clarified stylized status of sandwich algebra
haftmann
parents: 56973
diff changeset
   565
  in Sandwich.evaluation (value_sandwich ctxt) lift_postproc (evaluator { algebra = algebra, eqngr = eqngr }) end;
39475
9cc1ba3c5706 separation of static and dynamic thy context
haftmann
parents: 39398
diff changeset
   566
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   567
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   568
(** setup **)
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   569
56970
a3f911785efa modernized setup
haftmann
parents: 56968
diff changeset
   570
val _ =
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   571
  let
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   572
    fun mk_attribute f = Thm.declaration_attribute (fn thm => Context.mapping (f thm) I);
56929
40213e24c8c4 delete attribute for code abbrev
haftmann
parents: 56920
diff changeset
   573
    fun add_del_attribute_parser process =
40213e24c8c4 delete attribute for code abbrev
haftmann
parents: 56920
diff changeset
   574
      Attrib.add_del (mk_attribute (process Simplifier.add_simp))
40213e24c8c4 delete attribute for code abbrev
haftmann
parents: 56920
diff changeset
   575
        (mk_attribute (process Simplifier.del_simp));
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   576
  in
56970
a3f911785efa modernized setup
haftmann
parents: 56968
diff changeset
   577
    Context.>> (Context.map_theory
a3f911785efa modernized setup
haftmann
parents: 56968
diff changeset
   578
      (Attrib.setup @{binding code_unfold} (add_del_attribute_parser process_unfold)
45189
80cb73210612 removing declaration of code_unfold to address the old code generator
bulwahn
parents: 44338
diff changeset
   579
        "preprocessing equations for code generator"
56970
a3f911785efa modernized setup
haftmann
parents: 56968
diff changeset
   580
      #> Attrib.setup @{binding code_post} (add_del_attribute_parser process_post)
31998
2c7a24f74db9 code attributes use common underscore convention
haftmann
parents: 31977
diff changeset
   581
        "postprocessing equations for code generator"
56970
a3f911785efa modernized setup
haftmann
parents: 56968
diff changeset
   582
      #> Attrib.setup @{binding code_abbrev} (add_del_attribute_parser process_abbrev)
a3f911785efa modernized setup
haftmann
parents: 56968
diff changeset
   583
        "post- and preprocessing equations for code generator"))
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   584
  end;
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   585
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   586
val _ =
46961
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 46497
diff changeset
   587
  Outer_Syntax.improper_command @{command_spec "print_codeproc"} "print code preprocessor setup"
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   588
    (Scan.succeed (Toplevel.unknown_context o Toplevel.keep (print_codeproc o Toplevel.context_of)));
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   589
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   590
end; (*struct*)