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