src/Tools/code/code_wellsorted.ML
author haftmann
Mon, 04 May 2009 14:49:51 +0200
changeset 31036 64ff53fc0c0c
parent 30970 3fe2e418a071
child 31063 88aaab83b6fc
permissions -rw-r--r--
removed code_name module
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
     1
(*  Title:      Tools/code/code_wellsorted.ML
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
30061
c95e8f696b68 clarified status of variables in evaluation terms; tuned header
haftmann
parents: 30058
diff changeset
     4
Producing well-sorted systems of code equations in a graph
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
     5
with explicit dependencies -- the Waisenhaus algorithm.
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
30202
2775062fd3a9 reduced confusion code_funcgr vs. code_wellsorted
haftmann
parents: 30083
diff changeset
     8
signature CODE_WELLSORTED =
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
     9
sig
30947
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
    10
  type code_algebra
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
    11
  type code_graph
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
    12
  val eqns: code_graph -> string -> (thm * bool) list
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
    13
  val typ: code_graph -> string -> (string * sort) list * typ
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
    14
  val all: code_graph -> string list
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
    15
  val pretty: theory -> code_graph -> Pretty.T
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
    16
  val obtain: theory -> string list -> term list -> code_algebra * code_graph
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
    17
  val eval_conv: theory -> (sort -> sort)
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
    18
    -> (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
    19
  val eval: theory -> (sort -> sort) -> ((term -> term) -> 'a -> 'a)
30947
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
    20
    -> (code_algebra -> code_graph -> (string * sort) list -> term -> 'a) -> term -> 'a
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    21
end
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    22
30202
2775062fd3a9 reduced confusion code_funcgr vs. code_wellsorted
haftmann
parents: 30083
diff changeset
    23
structure Code_Wellsorted : CODE_WELLSORTED =
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    24
struct
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    25
30947
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
    26
(** the algebra and code equation graph types **)
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    27
30947
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
    28
type code_algebra = (sort -> sort) * Sorts.algebra;
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
    29
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
    30
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    31
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
    32
fun typ eqngr = fst o Graph.get_node eqngr;
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    33
fun all eqngr = Graph.keys eqngr;
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    34
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    35
fun pretty thy eqngr =
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    36
  AList.make (snd o Graph.get_node eqngr) (Graph.keys eqngr)
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    37
  |> (map o apfst) (Code_Unit.string_of_const thy)
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    38
  |> sort (string_ord o pairself fst)
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    39
  |> map (fn (s, thms) =>
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    40
       (Pretty.block o Pretty.fbreaks) (
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    41
         Pretty.str s
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    42
         :: map (Display.pretty_thm o fst) thms
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    43
       ))
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    44
  |> Pretty.chunks;
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    45
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    46
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    47
(** the Waisenhaus algorithm **)
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    48
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    49
(* auxiliary *)
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    50
30942
1e246776f876 diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents: 30876
diff changeset
    51
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
    52
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    53
fun complete_proper_sort thy =
30942
1e246776f876 diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents: 30876
diff changeset
    54
  Sign.complete_sort thy #> filter (is_proper_class thy);
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    55
30029
d14d0b4bf5b4 also consider superclasses properly
haftmann
parents: 30024
diff changeset
    56
fun inst_params thy tyco =
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    57
  map (fn (c, _) => AxClass.param_of_inst thy (c, tyco))
30029
d14d0b4bf5b4 also consider superclasses properly
haftmann
parents: 30024
diff changeset
    58
    o maps (#params o AxClass.get_info thy);
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    59
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    60
fun consts_of thy eqns = [] |> (fold o fold o fold_aterms)
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    61
  (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
    62
    (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
    63
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    64
fun tyscm_rhss_of thy c eqns =
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    65
  let
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    66
    val tyscm = case eqns of [] => Code.default_typscheme thy c
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    67
      | ((thm, _) :: _) => (snd o Code_Unit.head_eqn thy) thm;
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    68
    val rhss = consts_of thy eqns;
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    69
  in (tyscm, rhss) end;
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    70
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    71
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    72
(* data structures *)
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    73
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    74
datatype const = Fun of string | Inst of class * string;
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    75
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    76
fun const_ord (Fun c1, Fun c2) = fast_string_ord (c1, c2)
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    77
  | const_ord (Inst class_tyco1, Inst class_tyco2) =
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    78
      prod_ord fast_string_ord fast_string_ord (class_tyco1, class_tyco2)
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    79
  | const_ord (Fun _, Inst _) = LESS
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    80
  | const_ord (Inst _, Fun _) = GREATER;
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    81
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    82
type var = const * int;
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    83
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    84
structure Vargraph =
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    85
  GraphFun(type key = var val ord = prod_ord const_ord int_ord);
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    86
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
    87
datatype styp = Tyco of string * styp list | Var of var | Free;
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
    88
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
    89
fun styp_of c_lhs (Type (tyco, tys)) = Tyco (tyco, map (styp_of c_lhs) tys)
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
    90
  | styp_of c_lhs (TFree (v, _)) = case c_lhs
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
    91
     of SOME (c, lhs) => Var (Fun c, find_index (fn (v', _) => v = v') lhs)
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
    92
      | NONE => Free;
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    93
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
    94
type vardeps_data = ((string * styp list) list * class list) Vargraph.T
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
    95
  * (((string * sort) list * (thm * bool) list) Symtab.table
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
    96
    * (class * string) list);
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
    97
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
    98
val empty_vardeps_data : vardeps_data =
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
    99
  (Vargraph.empty, (Symtab.empty, []));
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   100
30876
613c2eb8aef6 tuned whitespace
haftmann
parents: 30769
diff changeset
   101
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   102
(* retrieving equations and instances from the background context *)
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   103
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   104
fun obtain_eqns thy eqngr c =
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   105
  case try (Graph.get_node eqngr) c
30058
f84c2412e870 more liberality needed
haftmann
parents: 30054
diff changeset
   106
   of SOME ((lhs, _), eqns) => ((lhs, []), [])
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   107
    | NONE => let
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   108
        val eqns = Code.these_eqns thy c
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   109
          |> burrow_fst (Code_Unit.norm_args thy)
31036
64ff53fc0c0c removed code_name module
haftmann
parents: 30970
diff changeset
   110
          |> burrow_fst (Code_Unit.norm_varnames thy);
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   111
        val ((lhs, _), rhss) = tyscm_rhss_of thy c eqns;
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   112
      in ((lhs, rhss), eqns) end;
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   113
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   114
fun obtain_instance thy arities (inst as (class, tyco)) =
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   115
  case AList.lookup (op =) arities inst
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   116
   of SOME classess => (classess, ([], []))
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   117
    | NONE => let
30029
d14d0b4bf5b4 also consider superclasses properly
haftmann
parents: 30024
diff changeset
   118
        val all_classes = complete_proper_sort thy [class];
d14d0b4bf5b4 also consider superclasses properly
haftmann
parents: 30024
diff changeset
   119
        val superclasses = remove (op =) class all_classes
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   120
        val classess = map (complete_proper_sort thy)
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   121
          (Sign.arity_sorts thy tyco [class]);
30029
d14d0b4bf5b4 also consider superclasses properly
haftmann
parents: 30024
diff changeset
   122
        val inst_params = inst_params thy tyco all_classes;
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   123
      in (classess, (superclasses, inst_params)) end;
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   124
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   125
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   126
(* computing instantiations *)
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   127
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   128
fun add_classes thy arities eqngr c_k new_classes vardeps_data =
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   129
  let
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   130
    val (styps, old_classes) = Vargraph.get_node (fst vardeps_data) c_k;
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   131
    val diff_classes = new_classes |> subtract (op =) old_classes;
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   132
  in if null diff_classes then vardeps_data
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   133
  else let
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   134
    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
   135
  in
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   136
    vardeps_data
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   137
    |> (apfst o Vargraph.map_node c_k o apsnd) (append diff_classes)
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   138
    |> fold (fn styp => fold (assert_typmatch_inst thy arities eqngr styp) new_classes) styps
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   139
    |> 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
   140
  end end
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   141
and add_styp thy arities eqngr c_k tyco_styps vardeps_data =
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   142
  let
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   143
    val (old_styps, classes) = Vargraph.get_node (fst vardeps_data) c_k;
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   144
  in if member (op =) old_styps tyco_styps then vardeps_data
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   145
  else
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   146
    vardeps_data
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   147
    |> (apfst o Vargraph.map_node c_k o apfst) (cons tyco_styps)
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   148
    |> fold (assert_typmatch_inst thy arities eqngr tyco_styps) classes
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   149
  end
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   150
and add_dep thy arities eqngr c_k c_k' vardeps_data =
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   151
  let
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   152
    val (_, classes) = Vargraph.get_node (fst vardeps_data) c_k;
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   153
  in
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   154
    vardeps_data
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   155
    |> add_classes thy arities eqngr c_k' classes
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   156
    |> apfst (Vargraph.add_edge (c_k, c_k'))
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   157
  end
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   158
and assert_typmatch_inst thy arities eqngr (tyco, styps) class vardeps_data =
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   159
  if can (Sign.arity_sorts thy tyco) [class]
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   160
  then vardeps_data
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   161
    |> assert_inst thy arities eqngr (class, tyco)
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   162
    |> fold_index (fn (k, styp) =>
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   163
         assert_typmatch thy arities eqngr styp (Inst (class, tyco), k)) styps
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   164
  else vardeps_data (*permissive!*)
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   165
and assert_inst thy arities eqngr (inst as (class, tyco)) (vardeps_data as (_, (_, insts))) =
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   166
  if member (op =) insts inst then vardeps_data
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   167
  else let
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   168
    val (classess, (superclasses, inst_params)) =
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   169
      obtain_instance thy arities inst;
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   170
  in
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   171
    vardeps_data
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   172
    |> (apsnd o apsnd) (insert (op =) inst)
30083
41a20af1fb77 robustified
haftmann
parents: 30075
diff changeset
   173
    |> fold_index (fn (k, _) =>
41a20af1fb77 robustified
haftmann
parents: 30075
diff changeset
   174
         apfst (Vargraph.new_node ((Inst (class, tyco), k), ([] ,[])))) classess
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   175
    |> fold (fn superclass => assert_inst thy arities eqngr (superclass, tyco)) superclasses
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   176
    |> fold (assert_fun thy arities eqngr) inst_params
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   177
    |> fold_index (fn (k, classes) =>
30075
ff5b4900d9a5 repaired order of variable node allocation
haftmann
parents: 30064
diff changeset
   178
         add_classes thy arities eqngr (Inst (class, tyco), k) classes
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   179
         #> fold (fn superclass =>
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   180
             add_dep thy arities eqngr (Inst (superclass, tyco), k)
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   181
             (Inst (class, tyco), k)) superclasses
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   182
         #> fold (fn inst_param =>
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   183
             add_dep thy arities eqngr (Fun inst_param, k)
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   184
             (Inst (class, tyco), k)
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   185
             ) inst_params
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   186
         ) classess
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   187
  end
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   188
and assert_typmatch thy arities eqngr (Tyco tyco_styps) c_k vardeps_data =
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   189
      vardeps_data
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   190
      |> add_styp thy arities eqngr c_k tyco_styps
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   191
  | assert_typmatch thy arities eqngr (Var c_k') c_k vardeps_data =
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   192
      vardeps_data
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   193
      |> add_dep thy arities eqngr c_k c_k'
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   194
  | assert_typmatch thy arities eqngr Free c_k vardeps_data =
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   195
      vardeps_data
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   196
and assert_rhs thy arities eqngr (c', styps) vardeps_data =
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   197
  vardeps_data
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   198
  |> assert_fun thy arities eqngr c'
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   199
  |> fold_index (fn (k, styp) =>
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   200
       assert_typmatch thy arities eqngr styp (Fun c', k)) styps
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   201
and assert_fun thy arities eqngr c (vardeps_data as (_, (eqntab, _))) =
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   202
  if Symtab.defined eqntab c then vardeps_data
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   203
  else let
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   204
    val ((lhs, rhss), eqns) = obtain_eqns thy eqngr c;
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   205
    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
   206
  in
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   207
    vardeps_data
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   208
    |> (apsnd o apfst) (Symtab.update_new (c, (lhs, eqns)))
30083
41a20af1fb77 robustified
haftmann
parents: 30075
diff changeset
   209
    |> fold_index (fn (k, _) =>
41a20af1fb77 robustified
haftmann
parents: 30075
diff changeset
   210
         apfst (Vargraph.new_node ((Fun c, k), ([] ,[])))) lhs
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   211
    |> fold_index (fn (k, (_, sort)) =>
30083
41a20af1fb77 robustified
haftmann
parents: 30075
diff changeset
   212
         add_classes thy arities eqngr (Fun c, k) (complete_proper_sort thy sort)) lhs
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   213
    |> fold (assert_rhs thy arities eqngr) rhss'
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   214
  end;
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   215
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   216
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   217
(* applying instantiations *)
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   218
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   219
fun dicts_of thy (proj_sort, algebra) (T, sort) =
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   220
  let
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   221
    fun class_relation (x, _) _ = x;
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   222
    fun type_constructor tyco xs class =
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   223
      inst_params thy tyco (Sorts.complete_sort algebra [class])
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   224
        @ (maps o maps) fst xs;
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   225
    fun type_variable (TFree (_, sort)) = map (pair []) (proj_sort sort);
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   226
  in
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   227
    flat (Sorts.of_sort_derivation (Syntax.pp_global thy) algebra
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   228
      { class_relation = class_relation, type_constructor = type_constructor,
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   229
        type_variable = type_variable } (T, proj_sort sort)
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   230
       handle Sorts.CLASS_ERROR _ => [] (*permissive!*))
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   231
  end;
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   232
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   233
fun add_arity thy vardeps (class, tyco) =
30058
f84c2412e870 more liberality needed
haftmann
parents: 30054
diff changeset
   234
  AList.default (op =)
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   235
    ((class, tyco), map (fn k => (snd o Vargraph.get_node vardeps) (Inst (class, tyco), k))
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   236
      (0 upto Sign.arity_number thy tyco - 1));
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   237
30942
1e246776f876 diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents: 30876
diff changeset
   238
fun add_eqs thy vardeps (c, (proto_lhs, proto_eqns)) (rhss, eqngr) =
30058
f84c2412e870 more liberality needed
haftmann
parents: 30054
diff changeset
   239
  if can (Graph.get_node eqngr) c then (rhss, eqngr)
f84c2412e870 more liberality needed
haftmann
parents: 30054
diff changeset
   240
  else let
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   241
    val lhs = map_index (fn (k, (v, _)) =>
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   242
      (v, snd (Vargraph.get_node vardeps (Fun c, k)))) proto_lhs;
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   243
    val inst_tab = Vartab.empty |> fold (fn (v, sort) =>
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   244
      Vartab.update ((v, 0), sort)) lhs;
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   245
    val eqns = proto_eqns
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   246
      |> (map o apfst) (Code_Unit.inst_thm thy inst_tab);
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   247
    val (tyscm, rhss') = tyscm_rhss_of thy c eqns;
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   248
    val eqngr' = Graph.new_node (c, (tyscm, eqns)) eqngr;
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   249
  in (map (pair c) rhss' @ rhss, eqngr') end;
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   250
30942
1e246776f876 diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents: 30876
diff changeset
   251
fun extend_arities_eqngr thy cs ts (arities, eqngr) =
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   252
  let
30942
1e246776f876 diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents: 30876
diff changeset
   253
    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
   254
      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
   255
    val (vardeps, (eqntab, insts)) = empty_vardeps_data
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   256
      |> fold (assert_fun thy arities eqngr) cs
30942
1e246776f876 diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents: 30876
diff changeset
   257
      |> fold (assert_rhs thy arities eqngr) cs_rhss;
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   258
    val arities' = fold (add_arity thy vardeps) insts arities;
30064
3cd19b113854 use canonical subalgebra projection
haftmann
parents: 30061
diff changeset
   259
    val pp = Syntax.pp_global thy;
30942
1e246776f876 diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents: 30876
diff changeset
   260
    val algebra = Sorts.subalgebra pp (is_proper_class thy)
30064
3cd19b113854 use canonical subalgebra projection
haftmann
parents: 30061
diff changeset
   261
      (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
   262
    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
   263
    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
   264
      (rhs ~~ (map snd o fst o fst o Graph.get_node eqngr') c);
30054
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   265
    val eqngr'' = fold (fn (c, rhs) => fold
36d7d337510e simplified evaluation
haftmann
parents: 30050
diff changeset
   266
      (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
   267
  in (algebra, (arities', eqngr'')) end;
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   268
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   269
30942
1e246776f876 diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents: 30876
diff changeset
   270
(** store **)
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   271
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   272
structure Wellsorted = CodeDataFun
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   273
(
30947
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
   274
  type T = ((string * class) * sort list) list * code_graph;
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   275
  val empty = ([], Graph.empty);
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   276
  fun purge thy cs (arities, eqngr) =
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   277
    let
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   278
      val del_cs = ((Graph.all_preds eqngr
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   279
        o filter (can (Graph.get_node eqngr))) cs);
30050
916a8427f65a first attempt to solve evaluation bootstrap problem
haftmann
parents: 30029
diff changeset
   280
      val del_arities = del_cs
916a8427f65a first attempt to solve evaluation bootstrap problem
haftmann
parents: 30029
diff changeset
   281
        |> map_filter (AxClass.inst_of_param thy)
916a8427f65a first attempt to solve evaluation bootstrap problem
haftmann
parents: 30029
diff changeset
   282
        |> maps (fn (c, tyco) =>
916a8427f65a first attempt to solve evaluation bootstrap problem
haftmann
parents: 30029
diff changeset
   283
             (map (rpair tyco) o Sign.complete_sort thy o the_list
916a8427f65a first attempt to solve evaluation bootstrap problem
haftmann
parents: 30029
diff changeset
   284
               o AxClass.class_of_param thy) c);
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   285
      val arities' = fold (AList.delete (op =)) del_arities arities;
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   286
      val eqngr' = Graph.del_nodes del_cs eqngr;
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   287
    in (arities', eqngr') end;
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   288
);
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   289
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   290
30942
1e246776f876 diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents: 30876
diff changeset
   291
(** retrieval interfaces **)
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   292
30942
1e246776f876 diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents: 30876
diff changeset
   293
fun obtain thy cs ts = apsnd snd
1e246776f876 diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents: 30876
diff changeset
   294
  (Wellsorted.change_yield thy (extend_arities_eqngr thy cs ts));
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   295
30947
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
   296
fun prepare_sorts prep_sort (Const (c, ty)) = Const (c, map_type_tfree
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
   297
      (fn (v, sort) => TFree (v, prep_sort sort)) ty)
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
   298
  | prepare_sorts prep_sort (t1 $ t2) =
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
   299
      prepare_sorts prep_sort t1 $ prepare_sorts prep_sort t2
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
   300
  | prepare_sorts prep_sort (Abs (v, ty, t)) =
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
   301
      Abs (v, Type.strip_sorts ty, prepare_sorts prep_sort t)
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
   302
  | prepare_sorts _ (Term.Free (v, ty)) = Term.Free (v, Type.strip_sorts ty)
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
   303
  | prepare_sorts _ (t as Bound _) = t;
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   304
30947
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
   305
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
   306
  let
1e246776f876 diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents: 30876
diff changeset
   307
    val ct = cterm_of proto_ct;
30947
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
   308
    val _ = (Term.map_types Type.no_tvars o Sign.no_vars (Syntax.pp_global thy))
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
   309
      (Thm.term_of ct);
30942
1e246776f876 diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents: 30876
diff changeset
   310
    val thm = Code.preprocess_conv thy ct;
1e246776f876 diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents: 30876
diff changeset
   311
    val ct' = Thm.rhs_of thm;
1e246776f876 diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents: 30876
diff changeset
   312
    val t' = Thm.term_of ct';
30947
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
   313
    val vs = Term.add_tfrees t' [];
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
   314
    val consts = fold_aterms
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
   315
      (fn Const (c, _) => insert (op =) c | _ => I) t' [];
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
   316
    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
   317
    val (algebra', eqngr') = obtain thy consts [t''];
30947
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
   318
  in conclude_evaluation (evaluator algebra' eqngr' vs t'' ct') thm end;
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
   319
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
   320
fun simple_evaluator evaluator algebra eqngr vs t ct =
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
   321
  evaluator algebra eqngr vs t;
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   322
30942
1e246776f876 diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents: 30876
diff changeset
   323
fun eval_conv thy =
1e246776f876 diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents: 30876
diff changeset
   324
  let
30947
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
   325
    fun conclude_evaluation thm2 thm1 =
30942
1e246776f876 diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents: 30876
diff changeset
   326
      let
1e246776f876 diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents: 30876
diff changeset
   327
        val thm3 = Code.postprocess_conv thy (Thm.rhs_of thm2);
1e246776f876 diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents: 30876
diff changeset
   328
      in
1e246776f876 diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents: 30876
diff changeset
   329
        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
   330
          error ("could not construct evaluation proof:\n"
1e246776f876 diagnostic commands now in code_thingol; tuned code of funny continuations
haftmann
parents: 30876
diff changeset
   331
          ^ (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
   332
      end;
30947
dd551284a300 re-engineering of evaluation conversions
haftmann
parents: 30942
diff changeset
   333
  in gen_eval thy I conclude_evaluation end;
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   334
30970
3fe2e418a071 generic postprocessing scheme for term evaluations
haftmann
parents: 30947
diff changeset
   335
fun eval thy prep_sort postproc evaluator = gen_eval thy (Thm.cterm_of thy)
3fe2e418a071 generic postprocessing scheme for term evaluations
haftmann
parents: 30947
diff changeset
   336
  (K o postproc (Code.postprocess_term thy)) prep_sort (simple_evaluator evaluator);
30010
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   337
862fc7751a15 tuned and incremental version of wellsorting algorithm
haftmann
parents:
diff changeset
   338
end; (*struct*)