src/Tools/Code/code_preproc.ML
author wenzelm
Wed, 12 Mar 2014 21:58:48 +0100
changeset 56069 451d5b73f8cf
parent 55757 9fc71814b8c1
child 56334 6b3739fee456
permissions -rw-r--r--
simplified programming interface to define ML antiquotations -- NB: the transformed context ignores updates of the context parser; added command 'print_ML_antiquotations';
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
32072
d4bff63bcbf1 added code_unfold_post attribute
haftmann
parents: 31998
diff changeset
    12
  val add_unfold: thm -> theory -> theory
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
    13
  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
    14
  val del_functrans: string -> theory -> theory
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
    15
  val simple_functrans: (Proof.context -> thm list -> thm list option)
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
    16
    -> Proof.context -> (thm * bool) list -> (thm * bool) list option
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
    17
  val print_codeproc: Proof.context -> unit
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    18
30947
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
    19
  type code_algebra
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
    20
  type code_graph
34891
99b9a6290446 code certificates as integral part of code generation
haftmann
parents: 34251
diff changeset
    21
  val cert: code_graph -> string -> Code.cert
32873
333945c9ac6a tuned handling of type variable names further
haftmann
parents: 32872
diff changeset
    22
  val sortargs: code_graph -> string -> sort list
30947
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
    23
  val all: code_graph -> string list
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
    24
  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
    25
  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
    26
  val dynamic_conv: Proof.context
38670
3c7db0192db9 added static_eval_conv
haftmann
parents: 38669
diff changeset
    27
    -> (code_algebra -> code_graph -> (string * sort) list -> term -> conv) -> conv
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
    28
  val dynamic_value: Proof.context -> ((term -> term) -> 'a -> 'a)
30947
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
    29
    -> (code_algebra -> code_graph -> (string * sort) list -> term -> 'a) -> term -> 'a
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
    30
  val static_conv: Proof.context -> string list
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
    31
    -> (code_algebra -> code_graph -> Proof.context -> (string * sort) list -> term -> conv)
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
    32
    -> Proof.context -> conv
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
    33
  val static_value: Proof.context -> ((term -> term) -> 'a -> 'a) -> string list
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
    34
    -> (code_algebra -> code_graph -> Proof.context -> (string * sort) list -> term -> 'a)
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
    35
    -> Proof.context -> term -> 'a
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    36
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    37
  val setup: theory -> theory
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    38
end
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    39
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    40
structure Code_Preproc : CODE_PREPROC =
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    41
struct
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    42
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    43
(** preprocessor administration **)
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    44
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    45
(* theory data *)
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
datatype thmproc = Thmproc of {
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    48
  pre: simpset,
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    49
  post: simpset,
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
    50
  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
    51
};
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    52
31599
97b4d289c646 tuned make/map/merge combinators
haftmann
parents: 31156
diff changeset
    53
fun make_thmproc ((pre, post), functrans) =
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    54
  Thmproc { pre = pre, post = post, functrans = functrans };
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    55
fun map_thmproc f (Thmproc { pre, post, functrans }) =
31599
97b4d289c646 tuned make/map/merge combinators
haftmann
parents: 31156
diff changeset
    56
  make_thmproc (f ((pre, post), functrans));
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    57
fun merge_thmproc (Thmproc { pre = pre1, post = post1, functrans = functrans1 },
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    58
  Thmproc { pre = pre2, post = post2, functrans = functrans2 }) =
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    59
    let
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    60
      val pre = Simplifier.merge_ss (pre1, pre2);
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    61
      val post = Simplifier.merge_ss (post1, post2);
34891
99b9a6290446 code certificates as integral part of code generation
haftmann
parents: 34251
diff changeset
    62
      val functrans = AList.merge (op =) (eq_fst (op =)) (functrans1, functrans2)
99b9a6290446 code certificates as integral part of code generation
haftmann
parents: 34251
diff changeset
    63
        handle AList.DUP => error ("Duplicate function transformer");
31599
97b4d289c646 tuned make/map/merge combinators
haftmann
parents: 31156
diff changeset
    64
    in make_thmproc ((pre, post), functrans) end;
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    65
33522
737589bb9bb8 adapted Theory_Data;
wenzelm
parents: 33063
diff changeset
    66
structure Code_Preproc_Data = Theory_Data
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    67
(
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    68
  type T = thmproc;
31599
97b4d289c646 tuned make/map/merge combinators
haftmann
parents: 31156
diff changeset
    69
  val empty = make_thmproc ((Simplifier.empty_ss, Simplifier.empty_ss), []);
33522
737589bb9bb8 adapted Theory_Data;
wenzelm
parents: 33063
diff changeset
    70
  val extend = I;
737589bb9bb8 adapted Theory_Data;
wenzelm
parents: 33063
diff changeset
    71
  val merge = merge_thmproc;
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    72
);
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    73
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    74
fun the_thmproc thy = case Code_Preproc_Data.get thy
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    75
 of Thmproc x => x;
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    76
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    77
fun delete_force msg key xs =
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    78
  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
    79
  else error ("No such " ^ msg ^ ": " ^ quote key);
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    80
38669
9ff76d0f0610 refined and unified naming convention for dynamic code evaluation techniques
haftmann
parents: 38291
diff changeset
    81
val map_data = Code_Preproc_Data.map o map_thmproc;
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    82
32072
d4bff63bcbf1 added code_unfold_post attribute
haftmann
parents: 31998
diff changeset
    83
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
    84
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51685
diff changeset
    85
fun map_simpset which f thy =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51685
diff changeset
    86
  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
    87
val map_pre = map_simpset apfst;
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51685
diff changeset
    88
val map_post = map_simpset apsnd;
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
    89
41226
adcb9a1198e7 clarified exports of structure Simplifier;
wenzelm
parents: 41189
diff changeset
    90
val add_unfold = map_pre o Simplifier.add_simp;
adcb9a1198e7 clarified exports of structure Simplifier;
wenzelm
parents: 41189
diff changeset
    91
val del_unfold = map_pre o Simplifier.del_simp;
adcb9a1198e7 clarified exports of structure Simplifier;
wenzelm
parents: 41189
diff changeset
    92
val add_post = map_post o Simplifier.add_simp;
adcb9a1198e7 clarified exports of structure Simplifier;
wenzelm
parents: 41189
diff changeset
    93
val del_post = map_post o Simplifier.del_simp;
32072
d4bff63bcbf1 added code_unfold_post attribute
haftmann
parents: 31998
diff changeset
    94
46026
83caa4f4bd56 semiring_numeral_0_eq_0, semiring_numeral_1_eq_1 now [simp], superseeding corresponding simp rules on type nat
haftmann
parents: 45230
diff changeset
    95
fun add_code_abbrev 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) =>
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51685
diff changeset
   102
      (pre |> simpset_map ctxt (Simplifier.add_simp thm_sym),
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51685
diff changeset
   103
       post |> simpset_map ctxt (Simplifier.add_simp 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
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   113
(* post- and preprocessing *)
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   114
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   115
fun no_variables_conv ctxt conv ct =
39604
f17fb9ccb836 avoid frees and vars in terms to be evaluated by abstracting and applying
haftmann
parents: 39475
diff changeset
   116
  let
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   117
    val thy = Proof_Context.theory_of ctxt;
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   118
    val cert = Thm.cterm_of thy;
39604
f17fb9ccb836 avoid frees and vars in terms to be evaluated by abstracting and applying
haftmann
parents: 39475
diff changeset
   119
    val all_vars = fold_aterms (fn t as Free _ => insert (op aconvc) (cert t)
f17fb9ccb836 avoid frees and vars in terms to be evaluated by abstracting and applying
haftmann
parents: 39475
diff changeset
   120
      | t as Var _ => insert (op aconvc) (cert t)
f17fb9ccb836 avoid frees and vars in terms to be evaluated by abstracting and applying
haftmann
parents: 39475
diff changeset
   121
      | _ => I) (Thm.term_of ct) [];
f17fb9ccb836 avoid frees and vars in terms to be evaluated by abstracting and applying
haftmann
parents: 39475
diff changeset
   122
    fun apply_beta var thm = Thm.combination thm (Thm.reflexive var)
f17fb9ccb836 avoid frees and vars in terms to be evaluated by abstracting and applying
haftmann
parents: 39475
diff changeset
   123
      |> Conv.fconv_rule (Conv.arg_conv (Conv.try_conv (Thm.beta_conversion false)))
f17fb9ccb836 avoid frees and vars in terms to be evaluated by abstracting and applying
haftmann
parents: 39475
diff changeset
   124
      |> Conv.fconv_rule (Conv.arg1_conv (Thm.beta_conversion false));
f17fb9ccb836 avoid frees and vars in terms to be evaluated by abstracting and applying
haftmann
parents: 39475
diff changeset
   125
  in
f17fb9ccb836 avoid frees and vars in terms to be evaluated by abstracting and applying
haftmann
parents: 39475
diff changeset
   126
    ct
46497
89ccf66aa73d renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
wenzelm
parents: 46026
diff changeset
   127
    |> fold_rev Thm.lambda all_vars
39604
f17fb9ccb836 avoid frees and vars in terms to be evaluated by abstracting and applying
haftmann
parents: 39475
diff changeset
   128
    |> conv
f17fb9ccb836 avoid frees and vars in terms to be evaluated by abstracting and applying
haftmann
parents: 39475
diff changeset
   129
    |> fold apply_beta all_vars
f17fb9ccb836 avoid frees and vars in terms to be evaluated by abstracting and applying
haftmann
parents: 39475
diff changeset
   130
  end;
f17fb9ccb836 avoid frees and vars in terms to be evaluated by abstracting and applying
haftmann
parents: 39475
diff changeset
   131
33942
haftmann
parents: 33699
diff changeset
   132
fun trans_conv_rule conv thm = Thm.transitive thm ((conv o Thm.rhs_of) thm);
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   133
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   134
fun term_of_conv ctxt conv =
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   135
  Thm.cterm_of (Proof_Context.theory_of ctxt)
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   136
  #> conv ctxt
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   137
  #> Thm.prop_of
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   138
  #> Logic.dest_equals
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   139
  #> snd;
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   140
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   141
fun term_of_conv_resubst ctxt conv t =
41189
ba1eac745c5a more appropriate closures for static evaluation
haftmann
parents: 41184
diff changeset
   142
  let
ba1eac745c5a more appropriate closures for static evaluation
haftmann
parents: 41184
diff changeset
   143
    val all_vars = fold_aterms (fn t as Free _ => insert (op aconv) t
ba1eac745c5a more appropriate closures for static evaluation
haftmann
parents: 41184
diff changeset
   144
      | t as Var _ => insert (op aconv) t
ba1eac745c5a more appropriate closures for static evaluation
haftmann
parents: 41184
diff changeset
   145
      | _ => I) t [];
ba1eac745c5a more appropriate closures for static evaluation
haftmann
parents: 41184
diff changeset
   146
    val resubst = curry (Term.betapplys o swap) all_vars;
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   147
  in (resubst, term_of_conv ctxt conv (fold_rev lambda all_vars t)) end;
52736
317663b422bb more correct context for dynamic invocations of static code conversions etc.
haftmann
parents: 51717
diff changeset
   148
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   149
fun preprocess_conv ctxt =
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   150
  let
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   151
    val thy = Proof_Context.theory_of ctxt;
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   152
    val ss = (#pre o the_thmproc) thy;
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   153
  in fn ctxt' =>
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   154
    Simplifier.rewrite (put_simpset ss ctxt')
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   155
    #> trans_conv_rule (Axclass.unoverload_conv (Proof_Context.theory_of ctxt'))
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   156
  end;
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   157
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   158
fun preprocess_term ctxt =
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   159
  let
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   160
    val conv = preprocess_conv ctxt;
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   161
  in fn ctxt' => term_of_conv_resubst ctxt' conv end;
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   162
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   163
fun postprocess_conv ctxt =
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   164
  let
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   165
    val thy = Proof_Context.theory_of ctxt;
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   166
    val ss = (#post o the_thmproc) thy;
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   167
  in fn ctxt' =>
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   168
    Axclass.overload_conv (Proof_Context.theory_of ctxt')
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   169
    #> trans_conv_rule (Simplifier.rewrite (put_simpset ss ctxt'))
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   170
  end;
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   171
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   172
fun postprocess_term ctxt =
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   173
  let
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   174
    val conv = postprocess_conv ctxt;
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   175
  in fn ctxt' => term_of_conv ctxt' conv end;
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   176
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   177
fun print_codeproc ctxt =
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   178
  let
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   179
    val thy = Proof_Context.theory_of ctxt;
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   180
    val pre = (#pre o the_thmproc) thy;
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   181
    val post = (#post o the_thmproc) thy;
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   182
    val functrans = (map fst o #functrans o the_thmproc) thy;
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   183
  in
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   184
    (Pretty.writeln o Pretty.chunks) [
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   185
      Pretty.block [
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   186
        Pretty.str "preprocessing simpset:",
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   187
        Pretty.fbrk,
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51685
diff changeset
   188
        Simplifier.pretty_simpset (put_simpset pre ctxt)
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   189
      ],
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   190
      Pretty.block [
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   191
        Pretty.str "postprocessing simpset:",
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   192
        Pretty.fbrk,
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51685
diff changeset
   193
        Simplifier.pretty_simpset (put_simpset post ctxt)
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   194
      ],
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   195
      Pretty.block (
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   196
        Pretty.str "function transformers:"
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   197
        :: Pretty.fbrk
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   198
        :: (Pretty.fbreaks o map Pretty.str) functrans
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   199
      )
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   200
    ]
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   201
  end;
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   202
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   203
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
   204
 of SOME thms' => SOME (map (rpair (forall snd eqns)) thms')
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   205
  | NONE => NONE;
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   206
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   207
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   208
(** sort algebra and code equation graph types **)
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   209
30947
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
   210
type code_algebra = (sort -> sort) * Sorts.algebra;
34891
99b9a6290446 code certificates as integral part of code generation
haftmann
parents: 34251
diff changeset
   211
type code_graph = ((string * sort) list * Code.cert) Graph.T;
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   212
39604
f17fb9ccb836 avoid frees and vars in terms to be evaluated by abstracting and applying
haftmann
parents: 39475
diff changeset
   213
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
   214
  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
   215
f17fb9ccb836 avoid frees and vars in terms to be evaluated by abstracting and applying
haftmann
parents: 39475
diff changeset
   216
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
   217
fun sortargs eqngr = map snd o fst o get_node eqngr;
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   218
fun all eqngr = Graph.keys eqngr;
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   219
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   220
fun pretty ctxt eqngr =
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   221
  let
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   222
    val thy = Proof_Context.theory_of ctxt;
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   223
  in
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   224
    AList.make (snd o Graph.get_node eqngr) (Graph.keys eqngr)
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   225
    |> (map o apfst) (Code.string_of_const thy)
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   226
    |> sort (string_ord o pairself fst)
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   227
    |> 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
   228
    |> Pretty.chunks
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   229
  end;
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   230
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   231
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   232
(** the Waisenhaus algorithm **)
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   233
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   234
(* auxiliary *)
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   235
51685
385ef6706252 more standard module name Axclass (according to file name);
wenzelm
parents: 51658
diff changeset
   236
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
   237
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   238
fun complete_proper_sort thy =
30942
1e246776f876 diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents: 30876
diff changeset
   239
  Sign.complete_sort thy #> filter (is_proper_class thy);
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   240
30029
d14d0b4bf5b4 also consider superclasses properly
haftmann
parents: 30024
diff changeset
   241
fun inst_params thy tyco =
51685
385ef6706252 more standard module name Axclass (according to file name);
wenzelm
parents: 51658
diff changeset
   242
  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
   243
    o maps (#params o Axclass.get_info thy);
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   244
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   245
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   246
(* data structures *)
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   247
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   248
datatype const = Fun of string | Inst of class * string;
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   249
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   250
fun const_ord (Fun c1, Fun c2) = fast_string_ord (c1, c2)
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   251
  | const_ord (Inst class_tyco1, Inst class_tyco2) =
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   252
      prod_ord fast_string_ord fast_string_ord (class_tyco1, class_tyco2)
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   253
  | const_ord (Fun _, Inst _) = LESS
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   254
  | const_ord (Inst _, Fun _) = GREATER;
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   255
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   256
type var = const * int;
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   257
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   258
structure Vargraph =
31971
8c1b845ed105 renamed functor TableFun to Table, and GraphFun to Graph;
wenzelm
parents: 31962
diff changeset
   259
  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
   260
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   261
datatype styp = Tyco of string * styp list | Var of var | Free;
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   262
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   263
fun styp_of c_lhs (Type (tyco, tys)) = Tyco (tyco, map (styp_of c_lhs) tys)
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   264
  | styp_of c_lhs (TFree (v, _)) = case c_lhs
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   265
     of SOME (c, lhs) => Var (Fun c, find_index (fn (v', _) => v = v') lhs)
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   266
      | NONE => Free;
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   267
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   268
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
   269
  * (((string * sort) list * Code.cert) Symtab.table
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   270
    * (class * string) list);
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   271
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   272
val empty_vardeps_data : vardeps_data =
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   273
  (Vargraph.empty, (Symtab.empty, []));
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   274
30876
613c2eb8aef6 tuned whitespace
haftmann
parents: 30769
diff changeset
   275
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   276
(* retrieving equations and instances from the background context *)
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   277
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   278
fun obtain_eqns ctxt eqngr c =
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   279
  case try (Graph.get_node eqngr) c
34891
99b9a6290446 code certificates as integral part of code generation
haftmann
parents: 34251
diff changeset
   280
   of SOME (lhs, cert) => ((lhs, []), cert)
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   281
    | NONE => let
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   282
        val thy = Proof_Context.theory_of ctxt;
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   283
        val functrans = (map (fn (_, (_, f)) => f ctxt)
48075
ec5e62b868eb apply preprocessing simpset also to rhs of abstract code equations
haftmann
parents: 47005
diff changeset
   284
          o #functrans o the_thmproc) thy;
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   285
        val cert = Code.get_cert thy { functrans = functrans, ss = simpset_of ctxt } c; (*FIXME*)
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   286
        val (lhs, rhss) =
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   287
          Code.typargs_deps_of_cert thy cert;
34891
99b9a6290446 code certificates as integral part of code generation
haftmann
parents: 34251
diff changeset
   288
      in ((lhs, rhss), cert) end;
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   289
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   290
fun obtain_instance ctxt arities (inst as (class, tyco)) =
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   291
  case AList.lookup (op =) arities inst
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   292
   of SOME classess => (classess, ([], []))
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   293
    | NONE => let
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   294
        val thy = Proof_Context.theory_of ctxt;
30029
d14d0b4bf5b4 also consider superclasses properly
haftmann
parents: 30024
diff changeset
   295
        val all_classes = complete_proper_sort thy [class];
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 36960
diff changeset
   296
        val super_classes = remove (op =) class all_classes;
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   297
        val classess = map (complete_proper_sort thy)
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   298
          (Sign.arity_sorts thy tyco [class]);
30029
d14d0b4bf5b4 also consider superclasses properly
haftmann
parents: 30024
diff changeset
   299
        val inst_params = inst_params thy tyco all_classes;
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 36960
diff changeset
   300
      in (classess, (super_classes, inst_params)) end;
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   301
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   302
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   303
(* computing instantiations *)
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   304
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   305
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
   306
  let
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   307
    val (styps, old_classes) = Vargraph.get_node (fst vardeps_data) c_k;
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   308
    val diff_classes = new_classes |> subtract (op =) old_classes;
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   309
  in if null diff_classes then vardeps_data
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   310
  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
   311
    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
   312
  in
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   313
    vardeps_data
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   314
    |> (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
   315
    |> 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
   316
    |> 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
   317
  end end
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   318
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
   319
  let
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 36960
diff changeset
   320
    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
   321
  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
   322
  else
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   323
    vardeps_data
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 36960
diff changeset
   324
    |> (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
   325
    |> fold (ensure_typmatch_inst ctxt arities eqngr new_tyco_styps) classes
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   326
  end
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   327
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
   328
  let
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   329
    val (_, classes) = Vargraph.get_node (fst vardeps_data) c_k;
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   330
  in
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   331
    vardeps_data
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   332
    |> add_classes ctxt arities eqngr c_k' classes
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   333
    |> apfst (Vargraph.add_edge (c_k, c_k'))
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   334
  end
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   335
and ensure_typmatch_inst ctxt arities eqngr (tyco, styps) class vardeps_data =
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   336
  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
   337
  then vardeps_data
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   338
    |> ensure_inst ctxt arities eqngr (class, tyco)
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   339
    |> fold_index (fn (k, styp) =>
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   340
         ensure_typmatch ctxt arities eqngr styp (Inst (class, tyco), k)) styps
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   341
  else vardeps_data (*permissive!*)
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   342
and ensure_inst ctxt arities eqngr (inst as (class, tyco)) (vardeps_data as (_, (_, insts))) =
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   343
  if member (op =) insts inst then vardeps_data
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   344
  else let
37384
5aba26803073 more consistent naming aroud type classes and instances
haftmann
parents: 36960
diff changeset
   345
    val (classess, (super_classes, inst_params)) =
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   346
      obtain_instance ctxt arities inst;
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   347
  in
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   348
    vardeps_data
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   349
    |> (apsnd o apsnd) (insert (op =) inst)
30083
41a20af1fb77 robustified
haftmann
parents: 30075
diff changeset
   350
    |> fold_index (fn (k, _) =>
41a20af1fb77 robustified
haftmann
parents: 30075
diff changeset
   351
         apfst (Vargraph.new_node ((Inst (class, tyco), k), ([] ,[])))) classess
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   352
    |> 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
   353
    |> fold (ensure_fun ctxt arities eqngr) inst_params
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   354
    |> fold_index (fn (k, classes) =>
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   355
         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
   356
         #> fold (fn super_class =>
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   357
             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
   358
             (Inst (class, tyco), k)) super_classes
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   359
         #> fold (fn inst_param =>
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   360
             add_dep ctxt arities eqngr (Fun inst_param, k)
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   361
             (Inst (class, tyco), k)
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   362
             ) inst_params
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   363
         ) classess
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   364
  end
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   365
and ensure_typmatch ctxt arities eqngr (Tyco tyco_styps) c_k vardeps_data =
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   366
      vardeps_data
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   367
      |> add_styp ctxt arities eqngr c_k tyco_styps
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   368
  | ensure_typmatch ctxt arities eqngr (Var c_k') c_k vardeps_data =
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   369
      vardeps_data
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   370
      |> add_dep ctxt arities eqngr c_k c_k'
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   371
  | ensure_typmatch ctxt arities eqngr Free c_k vardeps_data =
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   372
      vardeps_data
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   373
and ensure_rhs ctxt arities eqngr (c', styps) vardeps_data =
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   374
  vardeps_data
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   375
  |> ensure_fun ctxt arities eqngr c'
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   376
  |> fold_index (fn (k, styp) =>
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   377
       ensure_typmatch ctxt arities eqngr styp (Fun c', k)) styps
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   378
and ensure_fun ctxt arities eqngr c (vardeps_data as (_, (eqntab, _))) =
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   379
  if Symtab.defined eqntab c then vardeps_data
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   380
  else let
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   381
    val ((lhs, rhss), eqns) = obtain_eqns ctxt eqngr c;
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   382
    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
   383
  in
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   384
    vardeps_data
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   385
    |> (apsnd o apfst) (Symtab.update_new (c, (lhs, eqns)))
30083
41a20af1fb77 robustified
haftmann
parents: 30075
diff changeset
   386
    |> fold_index (fn (k, _) =>
41a20af1fb77 robustified
haftmann
parents: 30075
diff changeset
   387
         apfst (Vargraph.new_node ((Fun c, k), ([] ,[])))) lhs
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   388
    |> 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
   389
         (complete_proper_sort (Proof_Context.theory_of ctxt) sort)) lhs
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   390
    |> fold (ensure_rhs ctxt arities eqngr) rhss'
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   391
  end;
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   392
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   393
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   394
(* applying instantiations *)
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   395
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   396
fun dicts_of ctxt (proj_sort, algebra) (T, sort) =
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   397
  let
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   398
    val thy = Proof_Context.theory_of ctxt;
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   399
    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
   400
    fun type_constructor (tyco, _) xs class =
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   401
      inst_params thy tyco (Sorts.complete_sort algebra [class])
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   402
        @ (maps o maps) fst xs;
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   403
    fun type_variable (TFree (_, sort)) = map (pair []) (proj_sort sort);
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   404
  in
32795
a0f38d8d633a Sorts.of_sort_derivation: no pp here;
wenzelm
parents: 32544
diff changeset
   405
    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
   406
      { class_relation = K class_relation, type_constructor = type_constructor,
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   407
        type_variable = type_variable } (T, proj_sort sort)
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   408
       handle Sorts.CLASS_ERROR _ => [] (*permissive!*))
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   409
  end;
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   410
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   411
fun add_arity ctxt vardeps (class, tyco) =
33063
4d462963a7db map_range (and map_index) combinator
haftmann
parents: 32873
diff changeset
   412
  AList.default (op =) ((class, tyco),
34891
99b9a6290446 code certificates as integral part of code generation
haftmann
parents: 34251
diff changeset
   413
    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
   414
      (Sign.arity_number (Proof_Context.theory_of ctxt) tyco));
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   415
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   416
fun add_cert ctxt vardeps (c, (proto_lhs, proto_cert)) (rhss, eqngr) =
30058
f84c2412e870 more liberality needed
haftmann
parents: 30054
diff changeset
   417
  if can (Graph.get_node eqngr) c then (rhss, eqngr)
f84c2412e870 more liberality needed
haftmann
parents: 30054
diff changeset
   418
  else let
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   419
    val thy = Proof_Context.theory_of ctxt;
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   420
    val lhs = map_index (fn (k, (v, _)) =>
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   421
      (v, snd (Vargraph.get_node vardeps (Fun c, k)))) proto_lhs;
49971
8b50286c36d3 close code theorems explicitly after preprocessing
haftmann
parents: 48075
diff changeset
   422
    val cert = proto_cert
8b50286c36d3 close code theorems explicitly after preprocessing
haftmann
parents: 48075
diff changeset
   423
      |> Code.constrain_cert thy (map (Sign.minimize_sort thy o snd) lhs)
8b50286c36d3 close code theorems explicitly after preprocessing
haftmann
parents: 48075
diff changeset
   424
      |> Code.conclude_cert;
35224
1c9866c5f6fb simplified
haftmann
parents: 34895
diff changeset
   425
    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
   426
    val eqngr' = Graph.new_node (c, (vs, cert)) eqngr;
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   427
  in (map (pair c) rhss' @ rhss, eqngr') end;
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   428
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   429
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
   430
  let
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   431
    val thy = Proof_Context.theory_of raw_ctxt;
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   432
    val {pre, ...} = the_thmproc thy;
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   433
    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
   434
    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
   435
      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
   436
    val (vardeps, (eqntab, insts)) = empty_vardeps_data
55364
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   437
      |> fold (ensure_fun ctxt arities eqngr) cs
4d26690379b1 build up preprocessing context only once
haftmann
parents: 54929
diff changeset
   438
      |> fold (ensure_rhs ctxt arities eqngr) cs_rhss;
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   439
    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
   440
    val algebra = Sorts.subalgebra (Context.pretty_global thy) (is_proper_class thy)
30064
3cd19b113854 use canonical subalgebra projection
haftmann
parents: 30061
diff changeset
   441
      (AList.lookup (op =) arities') (Sign.classes_of thy);
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   442
    val (rhss, eqngr') = Symtab.fold (add_cert ctxt vardeps) eqntab ([], eqngr);
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   443
    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
   444
      (rhs ~~ sortargs eqngr' c);
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   445
    val eqngr'' = fold (fn (c, rhs) => fold
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   446
      (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
   447
  in (algebra, (arities', eqngr'')) end;
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   448
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   449
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   450
(** store for preprocessed arities and code equations **)
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   451
34173
458ced35abb8 reduced code generator cache to the baremost minimum
haftmann
parents: 33942
diff changeset
   452
structure Wellsorted = Code_Data
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   453
(
30947
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
   454
  type T = ((string * class) * sort list) list * code_graph;
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   455
  val empty = ([], Graph.empty);
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   456
);
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   457
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   458
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   459
(** retrieval and evaluation interfaces **)
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   460
39398
2e30660a2e21 ignore code cache optionally; corrected scope of term value in static_eval_conv
haftmann
parents: 39133
diff changeset
   461
fun obtain ignore_cache thy consts ts = apsnd snd
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   462
  (Wellsorted.change_yield (if ignore_cache then NONE else SOME thy)
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   463
    (extend_arities_eqngr (Proof_Context.init_global thy) consts ts));
38670
3c7db0192db9 added static_eval_conv
haftmann
parents: 38669
diff changeset
   464
3c7db0192db9 added static_eval_conv
haftmann
parents: 38669
diff changeset
   465
fun dest_cterm ct = let val t = Thm.term_of ct in (Term.add_tfrees t [], t) end;
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   466
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   467
fun dynamic_conv ctxt conv = no_variables_conv ctxt (fn ct =>
30942
1e246776f876 diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents: 30876
diff changeset
   468
  let
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   469
    val thm1 = preprocess_conv ctxt ctxt ct;
39475
9cc1ba3c5706 separation of static and dynamic thy context
haftmann
parents: 39398
diff changeset
   470
    val ct' = Thm.rhs_of thm1;
38670
3c7db0192db9 added static_eval_conv
haftmann
parents: 38669
diff changeset
   471
    val (vs', t') = dest_cterm ct';
30947
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
   472
    val consts = fold_aterms
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
   473
      (fn Const (c, _) => insert (op =) c | _ => I) t' [];
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   474
    val (algebra', eqngr') = obtain false (Proof_Context.theory_of ctxt) consts [t'];
39475
9cc1ba3c5706 separation of static and dynamic thy context
haftmann
parents: 39398
diff changeset
   475
    val thm2 = conv algebra' eqngr' vs' t' ct';
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   476
    val thm3 = postprocess_conv ctxt ctxt (Thm.rhs_of thm2);
39475
9cc1ba3c5706 separation of static and dynamic thy context
haftmann
parents: 39398
diff changeset
   477
  in
9cc1ba3c5706 separation of static and dynamic thy context
haftmann
parents: 39398
diff changeset
   478
    Thm.transitive thm1 (Thm.transitive thm2 thm3) handle THM _ =>
9cc1ba3c5706 separation of static and dynamic thy context
haftmann
parents: 39398
diff changeset
   479
      error ("could not construct evaluation proof:\n"
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   480
      ^ (cat_lines o map (Display.string_of_thm ctxt)) [thm1, thm2, thm3])
39604
f17fb9ccb836 avoid frees and vars in terms to be evaluated by abstracting and applying
haftmann
parents: 39475
diff changeset
   481
  end);
30947
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
   482
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   483
fun dynamic_value ctxt postproc evaluator t =
30942
1e246776f876 diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents: 30876
diff changeset
   484
  let
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   485
    val (resubst, t') = preprocess_term ctxt ctxt t;
39475
9cc1ba3c5706 separation of static and dynamic thy context
haftmann
parents: 39398
diff changeset
   486
    val vs' = Term.add_tfrees t' [];
9cc1ba3c5706 separation of static and dynamic thy context
haftmann
parents: 39398
diff changeset
   487
    val consts = fold_aterms
9cc1ba3c5706 separation of static and dynamic thy context
haftmann
parents: 39398
diff changeset
   488
      (fn Const (c, _) => insert (op =) c | _ => I) t' [];
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   489
    val (algebra', eqngr') = obtain false (Proof_Context.theory_of ctxt) consts [t'];
39475
9cc1ba3c5706 separation of static and dynamic thy context
haftmann
parents: 39398
diff changeset
   490
  in
40176
haftmann
parents: 40167
diff changeset
   491
    t'
haftmann
parents: 40167
diff changeset
   492
    |> evaluator algebra' eqngr' vs'
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   493
    |> postproc (postprocess_term ctxt ctxt o resubst)
39475
9cc1ba3c5706 separation of static and dynamic thy context
haftmann
parents: 39398
diff changeset
   494
  end;
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   495
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   496
fun static_conv ctxt consts conv =
38670
3c7db0192db9 added static_eval_conv
haftmann
parents: 38669
diff changeset
   497
  let
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   498
    val (algebra, eqngr) = obtain true (Proof_Context.theory_of ctxt) consts [];
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   499
    val pre_conv = preprocess_conv ctxt;
41346
6673f6fa94ca canonical handling of theory context argument
haftmann
parents: 41226
diff changeset
   500
    val conv' = conv algebra eqngr;
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   501
    val post_conv = postprocess_conv ctxt;
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   502
  in fn ctxt' => no_variables_conv ctxt' ((pre_conv ctxt')
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   503
    then_conv (fn ct => uncurry (conv' ctxt') (dest_cterm ct) ct)
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   504
    then_conv (post_conv ctxt'))
39398
2e30660a2e21 ignore code cache optionally; corrected scope of term value in static_eval_conv
haftmann
parents: 39133
diff changeset
   505
  end;
38670
3c7db0192db9 added static_eval_conv
haftmann
parents: 38669
diff changeset
   506
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   507
fun static_value ctxt postproc consts evaluator =
39475
9cc1ba3c5706 separation of static and dynamic thy context
haftmann
parents: 39398
diff changeset
   508
  let
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   509
    val (algebra, eqngr) = obtain true (Proof_Context.theory_of ctxt) consts [];
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   510
    val preproc = preprocess_term ctxt;
39475
9cc1ba3c5706 separation of static and dynamic thy context
haftmann
parents: 39398
diff changeset
   511
    val evaluator' = evaluator algebra eqngr;
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   512
    val postproc' = postprocess_term ctxt;
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   513
  in fn ctxt' => 
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   514
    preproc ctxt'
41189
ba1eac745c5a more appropriate closures for static evaluation
haftmann
parents: 41184
diff changeset
   515
    #-> (fn resubst => fn t => t
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   516
      |> evaluator' ctxt' (Term.add_tfrees t [])
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 55364
diff changeset
   517
      |> postproc (postproc' ctxt' o resubst))
39475
9cc1ba3c5706 separation of static and dynamic thy context
haftmann
parents: 39398
diff changeset
   518
  end;
9cc1ba3c5706 separation of static and dynamic thy context
haftmann
parents: 39398
diff changeset
   519
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   520
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   521
(** setup **)
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   522
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   523
val setup = 
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   524
  let
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   525
    fun mk_attribute f = Thm.declaration_attribute (fn thm => Context.mapping (f thm) I);
32072
d4bff63bcbf1 added code_unfold_post attribute
haftmann
parents: 31998
diff changeset
   526
    fun add_del_attribute_parser add del =
31998
2c7a24f74db9 code attributes use common underscore convention
haftmann
parents: 31977
diff changeset
   527
      Attrib.add_del (mk_attribute add) (mk_attribute del);
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   528
  in
45189
80cb73210612 removing declaration of code_unfold to address the old code generator
bulwahn
parents: 44338
diff changeset
   529
    Attrib.setup @{binding code_unfold} (add_del_attribute_parser add_unfold del_unfold)
80cb73210612 removing declaration of code_unfold to address the old code generator
bulwahn
parents: 44338
diff changeset
   530
        "preprocessing equations for code generator"
32072
d4bff63bcbf1 added code_unfold_post attribute
haftmann
parents: 31998
diff changeset
   531
    #> Attrib.setup @{binding code_post} (add_del_attribute_parser add_post del_post)
31998
2c7a24f74db9 code attributes use common underscore convention
haftmann
parents: 31977
diff changeset
   532
        "postprocessing equations for code generator"
46026
83caa4f4bd56 semiring_numeral_0_eq_0, semiring_numeral_1_eq_1 now [simp], superseeding corresponding simp rules on type nat
haftmann
parents: 45230
diff changeset
   533
    #> Attrib.setup @{binding code_abbrev} (Scan.succeed (mk_attribute add_code_abbrev))
83caa4f4bd56 semiring_numeral_0_eq_0, semiring_numeral_1_eq_1 now [simp], superseeding corresponding simp rules on type nat
haftmann
parents: 45230
diff changeset
   534
        "post- and preprocessing equations for code generator"
31125
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   535
  end;
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   536
80218ee73167 transferred code generator preprocessor into separate module
haftmann
parents: 31089
diff changeset
   537
val _ =
46961
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 46497
diff changeset
   538
  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
   539
    (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
   540
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   541
end; (*struct*)