src/Tools/code/code_funcgr_new.ML
author wenzelm
Sun, 11 Jan 2009 21:49:59 +0100
changeset 29450 ac7f67be7f1f
parent 28925 1cb9596498c0
child 29963 590e0db3a267
permissions -rw-r--r--
tuned categories;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28925
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
     1
(*  Title:      Tools/code/code_funcgr.ML
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
     2
    ID:         $Id$
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
     3
    Author:     Florian Haftmann, TU Muenchen
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
     4
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
     5
Retrieving, well-sorting and structuring defining equations in graph
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
     6
with explicit dependencies.
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
     7
*)
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
     8
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
     9
signature CODE_FUNCGR =
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    10
sig
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    11
  type T
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    12
  val eqns: T -> string -> (thm * bool) list
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    13
  val typ: T -> string -> (string * sort) list * typ
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    14
  val all: T -> string list
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    15
  val pretty: theory -> T -> Pretty.T
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    16
  val make: theory -> string list
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    17
    -> ((sort -> sort) * Sorts.algebra) * T
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    18
  val eval_conv: theory
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    19
    -> (term -> term * (((sort -> sort) * Sorts.algebra) -> T -> thm)) -> cterm -> thm
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    20
  val eval_term: theory
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    21
    -> (term -> term * (((sort -> sort) * Sorts.algebra) -> T -> 'a)) -> term -> 'a
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    22
end
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    23
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    24
structure Code_Funcgr : CODE_FUNCGR =
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    25
struct
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    26
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    27
(** the graph type **)
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    28
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    29
type T = (((string * sort) list * typ) * (thm * bool) list) Graph.T;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    30
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    31
fun eqns funcgr =
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    32
  these o Option.map snd o try (Graph.get_node funcgr);
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    33
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    34
fun typ funcgr =
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    35
  fst o Graph.get_node funcgr;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    36
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    37
fun all funcgr = Graph.keys funcgr;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    38
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    39
fun pretty thy funcgr =
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    40
  AList.make (snd o Graph.get_node funcgr) (Graph.keys funcgr)
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    41
  |> (map o apfst) (Code_Unit.string_of_const thy)
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    42
  |> sort (string_ord o pairself fst)
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    43
  |> map (fn (s, thms) =>
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    44
       (Pretty.block o Pretty.fbreaks) (
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    45
         Pretty.str s
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    46
         :: map (Display.pretty_thm o fst) thms
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    47
       ))
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    48
  |> Pretty.chunks;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    49
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    50
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    51
(** generic combinators **)
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    52
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    53
fun fold_consts f thms =
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    54
  thms
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    55
  |> maps (op :: o swap o apfst (snd o strip_comb) o Logic.dest_equals o Thm.plain_prop_of)
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    56
  |> (fold o fold_aterms) (fn Const c => f c | _ => I);
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    57
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    58
fun consts_of (const, []) = []
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    59
  | consts_of (const, thms as _ :: _) = 
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    60
      let
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    61
        fun the_const (c, _) = if c = const then I else insert (op =) c
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    62
      in fold_consts the_const (map fst thms) [] end;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    63
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    64
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    65
(** graph algorithm **)
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    66
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    67
(* some nonsense -- FIXME *)
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    68
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    69
fun lhs_rhss_of thy c =
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    70
  let
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    71
    val eqns = Code.these_eqns thy c
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    72
      |> burrow_fst (Code_Unit.norm_args thy)
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    73
      |> burrow_fst (Code_Unit.norm_varnames thy Code_Name.purify_tvar Code_Name.purify_var);
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    74
    val (lhs, _) = case eqns of [] => Code.default_typscheme thy c
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    75
      | ((thm, _) :: _) => (snd o Code_Unit.head_eqn thy) thm;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    76
    val rhss = fold_consts (fn (c, ty) =>
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    77
      insert (op =) (c, Sign.const_typargs thy (c, Logic.unvarifyT ty))) (map fst eqns) [];
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    78
  in (lhs, rhss) end;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    79
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    80
fun inst_params thy tyco class =
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    81
  map (fn (c, _) => AxClass.param_of_inst thy (c, tyco))
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    82
    ((#params o AxClass.get_info thy) class);
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    83
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    84
fun complete_proper_sort thy sort =
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    85
  Sign.complete_sort thy sort |> filter (can (AxClass.get_info thy));
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    86
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    87
fun minimal_proper_sort thy sort =
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    88
  complete_proper_sort thy sort |> Sign.minimize_sort thy;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    89
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    90
fun dicts_of thy algebra (T, sort) =
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    91
  let
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    92
    fun class_relation (x, _) _ = x;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    93
    fun type_constructor tyco xs class =
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    94
      inst_params thy tyco class @ (maps o maps) fst xs;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    95
    fun type_variable (TFree (_, sort)) = map (pair []) sort;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    96
  in
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    97
    flat (Sorts.of_sort_derivation (Syntax.pp_global thy) algebra
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    98
      { class_relation = class_relation, type_constructor = type_constructor,
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
    99
        type_variable = type_variable } (T, minimal_proper_sort thy sort)
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   100
       handle Sorts.CLASS_ERROR _ => [] (*permissive!*))
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   101
  end;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   102
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   103
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   104
(* data structures *)
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   105
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   106
datatype const = Fun of string | Inst of class * string;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   107
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   108
fun const_ord (Fun c1, Fun c2) = fast_string_ord (c1, c2)
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   109
  | const_ord (Inst class_tyco1, Inst class_tyco2) =
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   110
      prod_ord fast_string_ord fast_string_ord (class_tyco1, class_tyco2)
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   111
  | const_ord (Fun _, Inst _) = LESS
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   112
  | const_ord (Inst _, Fun _) = GREATER;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   113
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   114
type var = const * int;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   115
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   116
structure Vargraph =
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   117
  GraphFun(type key = var val ord = prod_ord const_ord int_ord);
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   118
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   119
datatype styp = Tyco of string * styp list | Var of var;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   120
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   121
type vardeps = const list * ((string * styp list) list * class list) Vargraph.T;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   122
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   123
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   124
(* computing instantiations -- FIXME does not consider existing things *)
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   125
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   126
fun add_classes thy c_k new_classes vardeps =
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   127
  let
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   128
    val _ = tracing "add_classes";
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   129
    val (styps, old_classes) = Vargraph.get_node (snd vardeps) c_k;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   130
    val diff_classes = new_classes |> subtract (op =) old_classes;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   131
  in if null diff_classes then vardeps
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   132
  else let
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   133
    val c_ks = Vargraph.imm_succs (snd vardeps) c_k |> insert (op =) c_k;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   134
  in
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   135
    vardeps
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   136
    |> (apsnd o Vargraph.map_node c_k o apsnd) (append diff_classes)
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   137
    |> fold (fn styp => fold (add_typmatch_inst thy styp) new_classes) styps
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   138
    |> fold (fn c_k => add_classes thy c_k diff_classes) c_ks
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   139
  end end
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   140
and add_styp thy c_k tyco_styps vardeps =
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   141
  let
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   142
    val _ = tracing "add_styp";
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   143
    val (old_styps, classes) = Vargraph.get_node (snd vardeps) c_k;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   144
  in if member (op =) old_styps tyco_styps then vardeps
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   145
  else
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   146
    vardeps
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   147
    |> (apsnd o Vargraph.map_node c_k o apfst) (cons tyco_styps)
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   148
    |> fold (add_typmatch_inst thy tyco_styps) classes
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   149
  end
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   150
and add_dep thy c_k c_k' vardeps =
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   151
  let
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   152
    val _ = tracing ("add_dep " ^ makestring c_k ^ " -> " ^ makestring c_k');
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   153
    val (_, classes) = Vargraph.get_node (snd vardeps) c_k;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   154
  in
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   155
    vardeps
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   156
    |> add_classes thy c_k' classes
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   157
    |> apsnd (Vargraph.add_edge (c_k, c_k'))
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   158
  end
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   159
and add_typmatch_inst thy (tyco, styps) class vardeps = if can (Sign.arity_sorts thy tyco) [class]
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   160
  then vardeps
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   161
    |> tap (fn _ => tracing "add_typmatch_inst")
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   162
    |> assert thy (Inst (class, tyco))
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   163
    |> fold_index (fn (k, styp) =>
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   164
         add_typmatch thy styp (Inst (class, tyco), k)) styps
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   165
  else vardeps (*permissive!*)
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   166
and add_typmatch thy (Var c_k') c_k vardeps =
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   167
      vardeps
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   168
      |> tap (fn _ => tracing "add_typmatch (Inst)")
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   169
      |> add_dep thy c_k c_k'
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   170
  | add_typmatch thy (Tyco tyco_styps) c_k vardeps =
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   171
      vardeps
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   172
      |> tap (fn _ => tracing "add_typmatch (Tyco)")
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   173
      |> add_styp thy c_k tyco_styps
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   174
and add_inst thy (class, tyco) vardeps =
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   175
  let
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   176
    val _ = tracing ("add_inst " ^ tyco ^ "::" ^ class);
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   177
    val superclasses = complete_proper_sort thy
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   178
      (Sign.super_classes thy class);
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   179
    val classess = map (complete_proper_sort thy)
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   180
      (Sign.arity_sorts thy tyco [class]);
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   181
    val inst_params = inst_params thy tyco class;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   182
  in
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   183
    vardeps
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   184
    |> fold (fn superclass => assert thy (Inst (superclass, tyco))) superclasses
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   185
    |> fold (assert thy o Fun) inst_params
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   186
    |> fold_index (fn (k, classes) =>
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   187
         apsnd (Vargraph.default_node ((Inst (class, tyco), k), ([] ,[])))
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   188
         #> add_classes thy (Inst (class, tyco), k) classes
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   189
         #> fold (fn superclass =>
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   190
             add_dep thy (Inst (superclass, tyco), k)
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   191
             (Inst (class, tyco), k)) superclasses
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   192
         #> fold (fn inst_param =>
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   193
             add_dep thy (Fun inst_param, k)
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   194
             (Inst (class, tyco), k)
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   195
             ) inst_params
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   196
         ) classess
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   197
  end
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   198
and add_const thy c vardeps =
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   199
  let
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   200
    val _ = tracing "add_const";
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   201
    val (lhs, rhss) = lhs_rhss_of thy c;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   202
    fun styp_of (Type (tyco, tys)) = Tyco (tyco, map styp_of tys)
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   203
      | styp_of (TFree (v, _)) = Var (Fun c, find_index (fn (v', _) => v = v') lhs);
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   204
    val rhss' = (map o apsnd o map) styp_of rhss;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   205
  in
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   206
    vardeps
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   207
    |> fold_index (fn (k, (_, sort)) =>
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   208
         apsnd (Vargraph.default_node ((Fun c, k), ([] ,[])))
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   209
         #> add_classes thy (Fun c, k) (complete_proper_sort thy sort)) lhs
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   210
    |> fold (assert thy o Fun o fst) rhss'
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   211
    |> fold (fn (c', styps) => fold_index (fn (k', styp) =>
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   212
         add_typmatch thy styp (Fun c', k')) styps) rhss'
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   213
  end
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   214
and assert thy c (vardeps as (asserted, _)) =
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   215
  if member (op =) asserted c then vardeps
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   216
  else case c
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   217
   of Fun const => vardeps |> apfst (cons c) |> add_const thy const
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   218
    | Inst inst => vardeps |> apfst (cons c) |> add_inst thy inst;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   219
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   220
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   221
(* applying instantiations *)
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   222
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   223
fun algebra_of thy vardeps =
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   224
  let
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   225
    val pp = Syntax.pp_global thy;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   226
    val thy_algebra = Sign.classes_of thy;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   227
    val is_proper = can (AxClass.get_info thy);
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   228
    val arities = Vargraph.fold (fn ((Fun _, _), _) => I
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   229
      | ((Inst (class, tyco), k), ((_, classes), _)) =>
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   230
          AList.map_default (op =)
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   231
            ((tyco, class), replicate (Sign.arity_number thy tyco) [])
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   232
              (nth_map k (K classes))) vardeps [];
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   233
    val classrels = Sorts.classrels_of thy_algebra
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   234
      |> filter (is_proper o fst)
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   235
      |> (map o apsnd) (filter is_proper);
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   236
    fun add_arity (tyco, class) = case AList.lookup (op =) arities (tyco, class)
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   237
     of SOME sorts => Sorts.add_arities pp (tyco, [(class, sorts)])
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   238
      | NONE => if Sign.arity_number thy tyco = 0
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   239
          then (tracing (tyco ^ "::" ^ class); Sorts.add_arities pp (tyco, [(class, [])]))
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   240
          else I;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   241
    val instances = Sorts.instances_of thy_algebra
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   242
      |> filter (is_proper o snd)
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   243
  in
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   244
    Sorts.empty_algebra
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   245
    |> fold (Sorts.add_class pp) classrels
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   246
    |> fold add_arity instances
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   247
  end;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   248
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   249
fun add_eqs thy algebra vardeps c gr =
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   250
  let
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   251
    val eqns = Code.these_eqns thy c
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   252
      |> burrow_fst (Code_Unit.norm_args thy)
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   253
      |> burrow_fst (Code_Unit.norm_varnames thy Code_Name.purify_tvar Code_Name.purify_var);
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   254
    val (vs, _) = case eqns of [] => Code.default_typscheme thy c
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   255
      | ((thm, _) :: _) => (snd o Code_Unit.head_eqn thy) thm;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   256
    val inst = Vartab.empty |> fold_index (fn (k, (v, _)) =>
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   257
      Vartab.update ((v, 0), snd (Vargraph.get_node vardeps (Fun c, k)))) vs;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   258
    val eqns' = eqns
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   259
      |> (map o apfst) (Code_Unit.inst_thm thy inst);
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   260
    val tyscm = case eqns' of [] => Code.default_typscheme thy c
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   261
      | ((thm, _) :: _) => (snd o Code_Unit.head_eqn thy) thm;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   262
    val _ = tracing ("tyscm " ^ makestring (map snd (fst tyscm)));
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   263
    val rhss = fold_consts (fn (c, ty) =>
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   264
      insert (op =) (c, Sign.const_typargs thy (c, Logic.unvarifyT ty))) (map fst eqns') [];
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   265
  in
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   266
    gr
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   267
    |> Graph.new_node (c, (tyscm, eqns'))
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   268
    |> fold (fn (c', Ts) => ensure_eqs_dep thy algebra vardeps c c'
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   269
        #-> (fn (vs, _) =>
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   270
          fold2 (ensure_match thy algebra vardeps c) Ts (map snd vs))) rhss
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   271
    |> pair tyscm
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   272
  end
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   273
and ensure_match thy algebra vardeps c T sort gr =
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   274
  gr
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   275
  |> fold (fn c' => ensure_eqs_dep thy algebra vardeps c c' #> snd)
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   276
       (dicts_of thy algebra (T, sort))
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   277
and ensure_eqs_dep thy algebra vardeps c c' gr =
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   278
  gr
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   279
  |> ensure_eqs thy algebra vardeps c'
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   280
  ||> Graph.add_edge (c, c')
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   281
and ensure_eqs thy algebra vardeps c gr =
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   282
  case try (Graph.get_node gr) c
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   283
   of SOME (tyscm, _) => (tyscm, gr)
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   284
    | NONE => add_eqs thy algebra vardeps c gr;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   285
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   286
fun extend_graph thy cs gr =
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   287
  let
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   288
    val _ = tracing ("extending with " ^ commas cs);
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   289
    val _ = tracing "obtaining instantiations";
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   290
    val (_, vardeps) = fold (assert thy o Fun) cs ([], Vargraph.empty)
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   291
    val _ = tracing "obtaining algebra";
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   292
    val algebra = algebra_of thy vardeps;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   293
    val _ = tracing "obtaining equations";
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   294
    val (_, gr) = fold_map (ensure_eqs thy algebra vardeps) cs gr;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   295
    val _ = tracing "sort projection";
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   296
    val minimal_proper_sort = fn sort => sort
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   297
      |> Sorts.complete_sort (Sign.classes_of thy)
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   298
      |> filter (can (AxClass.get_info thy))
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   299
      |> Sorts.minimize_sort algebra;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   300
  in ((minimal_proper_sort, algebra), gr) end;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   301
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   302
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   303
(** retrieval interfaces **)
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   304
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   305
fun proto_eval thy cterm_of evaluator_lift evaluator proto_ct funcgr =
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   306
  let
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   307
    val ct = cterm_of proto_ct;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   308
    val _ = Sign.no_vars (Syntax.pp_global thy) (Thm.term_of ct);
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   309
    val _ = Term.fold_types (Type.no_tvars #> K I) (Thm.term_of ct) ();
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   310
    fun consts_of t =
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   311
      fold_aterms (fn Const c_ty => cons c_ty | _ => I) t [];
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   312
    val thm = Code.preprocess_conv thy ct;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   313
    val ct' = Thm.rhs_of thm;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   314
    val t' = Thm.term_of ct';
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   315
    val consts = map fst (consts_of t');
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   316
    val (algebra', funcgr') = extend_graph thy consts funcgr;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   317
    val (t'', evaluator_funcgr) = evaluator t';
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   318
    val consts' = consts_of t'';
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   319
    val const_matches = fold (fn (c, ty) =>
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   320
      insert (op =) (Sign.const_typargs thy (c, Logic.unvarifyT ty), c)) consts' [];
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   321
    val typ_matches = maps (fn (tys, c) => tys ~~ map snd (fst (fst (Graph.get_node funcgr' c))))
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   322
      const_matches;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   323
    val dicts = maps (dicts_of thy (snd algebra')) typ_matches;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   324
    val (algebra'', funcgr'') = extend_graph thy dicts funcgr';
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   325
  in (evaluator_lift (evaluator_funcgr algebra'') thm funcgr'', funcgr'') end;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   326
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   327
fun proto_eval_conv thy =
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   328
  let
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   329
    fun evaluator_lift evaluator thm1 funcgr =
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   330
      let
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   331
        val thm2 = evaluator funcgr;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   332
        val thm3 = Code.postprocess_conv thy (Thm.rhs_of thm2);
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   333
      in
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   334
        Thm.transitive thm1 (Thm.transitive thm2 thm3) handle THM _ =>
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   335
          error ("could not construct evaluation proof:\n"
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   336
          ^ (cat_lines o map Display.string_of_thm) [thm1, thm2, thm3])
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   337
      end;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   338
  in proto_eval thy I evaluator_lift end;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   339
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   340
fun proto_eval_term thy =
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   341
  let
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   342
    fun evaluator_lift evaluator _ funcgr = evaluator funcgr;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   343
  in proto_eval thy (Thm.cterm_of thy) evaluator_lift end;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   344
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   345
structure Funcgr = CodeDataFun
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   346
(
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   347
  type T = T;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   348
  val empty = Graph.empty;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   349
  fun purge _ cs funcgr =
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   350
    Graph.del_nodes ((Graph.all_preds funcgr 
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   351
      o filter (can (Graph.get_node funcgr))) cs) funcgr;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   352
);
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   353
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   354
fun make thy = Funcgr.change_yield thy o extend_graph thy;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   355
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   356
fun eval_conv thy f =
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   357
  fst o Funcgr.change_yield thy o proto_eval_conv thy f;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   358
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   359
fun eval_term thy f =
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   360
  fst o Funcgr.change_yield thy o proto_eval_term thy f;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   361
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   362
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   363
(** diagnostic commands **)
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   364
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   365
fun code_depgr thy consts =
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   366
  let
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   367
    val (_, gr) = make thy consts;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   368
    val select = Graph.all_succs gr consts;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   369
  in
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   370
    gr
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   371
    |> not (null consts) ? Graph.subgraph (member (op =) select) 
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   372
    |> Graph.map_nodes ((apsnd o map o apfst) (AxClass.overload thy))
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   373
  end;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   374
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   375
fun code_thms thy = Pretty.writeln o pretty thy o code_depgr thy;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   376
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   377
fun code_deps thy consts =
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   378
  let
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   379
    val gr = code_depgr thy consts;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   380
    fun mk_entry (const, (_, (_, parents))) =
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   381
      let
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   382
        val name = Code_Unit.string_of_const thy const;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   383
        val nameparents = map (Code_Unit.string_of_const thy) parents;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   384
      in { name = name, ID = name, dir = "", unfold = true,
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   385
        path = "", parents = nameparents }
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   386
      end;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   387
    val prgr = Graph.fold ((fn x => fn xs => xs @ [x]) o mk_entry) gr [];
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   388
  in Present.display_graph prgr end;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   389
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   390
local
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   391
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   392
structure P = OuterParse
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   393
and K = OuterKeyword
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   394
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   395
fun code_thms_cmd thy = code_thms thy o op @ o Code_Name.read_const_exprs thy;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   396
fun code_deps_cmd thy = code_deps thy o op @ o Code_Name.read_const_exprs thy;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   397
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   398
in
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   399
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   400
val _ =
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   401
  OuterSyntax.improper_command "code_thms" "print system of defining equations for code" OuterKeyword.diag
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   402
    (Scan.repeat P.term_group
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   403
      >> (fn cs => Toplevel.no_timing o Toplevel.unknown_theory
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   404
        o Toplevel.keep ((fn thy => code_thms_cmd thy cs) o Toplevel.theory_of)));
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   405
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   406
val _ =
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   407
  OuterSyntax.improper_command "code_deps" "visualize dependencies of defining equations for code" OuterKeyword.diag
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   408
    (Scan.repeat P.term_group
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   409
      >> (fn cs => Toplevel.no_timing o Toplevel.unknown_theory
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   410
        o Toplevel.keep ((fn thy => code_deps_cmd thy cs) o Toplevel.theory_of)));
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   411
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   412
end;
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   413
1cb9596498c0 experimental implementation of a well-sorting algorithm
haftmann
parents:
diff changeset
   414
end; (*struct*)