src/Tools/code/code_wellsorted.ML
author haftmann
Mon, 02 Mar 2009 16:58:39 +0100
changeset 30202 2775062fd3a9
parent 30020 41a20af1fb77
child 30758 756088c52d10
permissions -rw-r--r--
reduced confusion code_funcgr vs. code_wellsorted
haftmann@29947
     1
(*  Title:      Tools/code/code_wellsorted.ML
haftmann@29947
     2
    Author:     Florian Haftmann, TU Muenchen
haftmann@29947
     3
haftmann@29998
     4
Producing well-sorted systems of code equations in a graph
haftmann@29947
     5
with explicit dependencies -- the Waisenhaus algorithm.
haftmann@29947
     6
*)
haftmann@29947
     7
haftmann@30202
     8
signature CODE_WELLSORTED =
haftmann@29947
     9
sig
haftmann@29947
    10
  type T
haftmann@29947
    11
  val eqns: T -> string -> (thm * bool) list
haftmann@29947
    12
  val typ: T -> string -> (string * sort) list * typ
haftmann@29947
    13
  val all: T -> string list
haftmann@29947
    14
  val pretty: theory -> T -> Pretty.T
haftmann@29947
    15
  val make: theory -> string list
haftmann@29947
    16
    -> ((sort -> sort) * Sorts.algebra) * T
haftmann@29947
    17
  val eval_conv: theory
haftmann@29947
    18
    -> (term -> term * (((sort -> sort) * Sorts.algebra) -> T -> thm)) -> cterm -> thm
haftmann@29947
    19
  val eval_term: theory
haftmann@29947
    20
    -> (term -> term * (((sort -> sort) * Sorts.algebra) -> T -> 'a)) -> term -> 'a
haftmann@29947
    21
end
haftmann@29947
    22
haftmann@30202
    23
structure Code_Wellsorted : CODE_WELLSORTED =
haftmann@29947
    24
struct
haftmann@29947
    25
haftmann@29947
    26
(** the equation graph type **)
haftmann@29947
    27
haftmann@29947
    28
type T = (((string * sort) list * typ) * (thm * bool) list) Graph.T;
haftmann@29947
    29
haftmann@29947
    30
fun eqns eqngr = these o Option.map snd o try (Graph.get_node eqngr);
haftmann@29947
    31
fun typ eqngr = fst o Graph.get_node eqngr;
haftmann@29947
    32
fun all eqngr = Graph.keys eqngr;
haftmann@29947
    33
haftmann@29947
    34
fun pretty thy eqngr =
haftmann@29947
    35
  AList.make (snd o Graph.get_node eqngr) (Graph.keys eqngr)
haftmann@29947
    36
  |> (map o apfst) (Code_Unit.string_of_const thy)
haftmann@29947
    37
  |> sort (string_ord o pairself fst)
haftmann@29947
    38
  |> map (fn (s, thms) =>
haftmann@29947
    39
       (Pretty.block o Pretty.fbreaks) (
haftmann@29947
    40
         Pretty.str s
haftmann@29947
    41
         :: map (Display.pretty_thm o fst) thms
haftmann@29947
    42
       ))
haftmann@29947
    43
  |> Pretty.chunks;
haftmann@29947
    44
haftmann@29947
    45
haftmann@29947
    46
(** the Waisenhaus algorithm **)
haftmann@29947
    47
haftmann@29947
    48
(* auxiliary *)
haftmann@29947
    49
haftmann@29947
    50
fun complete_proper_sort thy =
haftmann@29947
    51
  Sign.complete_sort thy #> filter (can (AxClass.get_info thy));
haftmann@29947
    52
haftmann@29966
    53
fun inst_params thy tyco =
haftmann@29947
    54
  map (fn (c, _) => AxClass.param_of_inst thy (c, tyco))
haftmann@29966
    55
    o maps (#params o AxClass.get_info thy);
haftmann@29947
    56
haftmann@29947
    57
fun consts_of thy eqns = [] |> (fold o fold o fold_aterms)
haftmann@29947
    58
  (fn Const (c, ty) => insert (op =) (c, Sign.const_typargs thy (c, Logic.unvarifyT ty)) | _ => I)
haftmann@29947
    59
    (map (op :: o swap o apfst (snd o strip_comb) o Logic.dest_equals o Thm.plain_prop_of o fst) eqns);
haftmann@29947
    60
haftmann@29947
    61
fun tyscm_rhss_of thy c eqns =
haftmann@29947
    62
  let
haftmann@29947
    63
    val tyscm = case eqns of [] => Code.default_typscheme thy c
haftmann@29947
    64
      | ((thm, _) :: _) => (snd o Code_Unit.head_eqn thy) thm;
haftmann@29947
    65
    val rhss = consts_of thy eqns;
haftmann@29947
    66
  in (tyscm, rhss) end;
haftmann@29947
    67
haftmann@29947
    68
haftmann@29947
    69
(* data structures *)
haftmann@29947
    70
haftmann@29947
    71
datatype const = Fun of string | Inst of class * string;
haftmann@29947
    72
haftmann@29947
    73
fun const_ord (Fun c1, Fun c2) = fast_string_ord (c1, c2)
haftmann@29947
    74
  | const_ord (Inst class_tyco1, Inst class_tyco2) =
haftmann@29947
    75
      prod_ord fast_string_ord fast_string_ord (class_tyco1, class_tyco2)
haftmann@29947
    76
  | const_ord (Fun _, Inst _) = LESS
haftmann@29947
    77
  | const_ord (Inst _, Fun _) = GREATER;
haftmann@29947
    78
haftmann@29947
    79
type var = const * int;
haftmann@29947
    80
haftmann@29947
    81
structure Vargraph =
haftmann@29947
    82
  GraphFun(type key = var val ord = prod_ord const_ord int_ord);
haftmann@29947
    83
haftmann@29991
    84
datatype styp = Tyco of string * styp list | Var of var | Free;
haftmann@29947
    85
haftmann@29991
    86
fun styp_of c_lhs (Type (tyco, tys)) = Tyco (tyco, map (styp_of c_lhs) tys)
haftmann@29991
    87
  | styp_of c_lhs (TFree (v, _)) = case c_lhs
haftmann@29991
    88
     of SOME (c, lhs) => Var (Fun c, find_index (fn (v', _) => v = v') lhs)
haftmann@29991
    89
      | NONE => Free;
haftmann@29947
    90
haftmann@29991
    91
type vardeps_data = ((string * styp list) list * class list) Vargraph.T
haftmann@29991
    92
  * (((string * sort) list * (thm * bool) list) Symtab.table
haftmann@29991
    93
    * (class * string) list);
haftmann@29991
    94
haftmann@29991
    95
val empty_vardeps_data : vardeps_data =
haftmann@29991
    96
  (Vargraph.empty, (Symtab.empty, []));
haftmann@29991
    97
haftmann@29991
    98
(* retrieving equations and instances from the background context *)
haftmann@29947
    99
haftmann@29947
   100
fun obtain_eqns thy eqngr c =
haftmann@29947
   101
  case try (Graph.get_node eqngr) c
haftmann@29995
   102
   of SOME ((lhs, _), eqns) => ((lhs, []), [])
haftmann@29947
   103
    | NONE => let
haftmann@29947
   104
        val eqns = Code.these_eqns thy c
haftmann@29947
   105
          |> burrow_fst (Code_Unit.norm_args thy)
haftmann@29947
   106
          |> burrow_fst (Code_Unit.norm_varnames thy Code_Name.purify_tvar Code_Name.purify_var);
haftmann@29947
   107
        val ((lhs, _), rhss) = tyscm_rhss_of thy c eqns;
haftmann@29947
   108
      in ((lhs, rhss), eqns) end;
haftmann@29947
   109
haftmann@29947
   110
fun obtain_instance thy arities (inst as (class, tyco)) =
haftmann@29947
   111
  case AList.lookup (op =) arities inst
haftmann@29947
   112
   of SOME classess => (classess, ([], []))
haftmann@29947
   113
    | NONE => let
haftmann@29966
   114
        val all_classes = complete_proper_sort thy [class];
haftmann@29966
   115
        val superclasses = remove (op =) class all_classes
haftmann@29947
   116
        val classess = map (complete_proper_sort thy)
haftmann@29947
   117
          (Sign.arity_sorts thy tyco [class]);
haftmann@29966
   118
        val inst_params = inst_params thy tyco all_classes;
haftmann@29947
   119
      in (classess, (superclasses, inst_params)) end;
haftmann@29947
   120
haftmann@29991
   121
haftmann@29991
   122
(* computing instantiations *)
haftmann@29991
   123
haftmann@29947
   124
fun add_classes thy arities eqngr c_k new_classes vardeps_data =
haftmann@29947
   125
  let
haftmann@29947
   126
    val (styps, old_classes) = Vargraph.get_node (fst vardeps_data) c_k;
haftmann@29947
   127
    val diff_classes = new_classes |> subtract (op =) old_classes;
haftmann@29947
   128
  in if null diff_classes then vardeps_data
haftmann@29947
   129
  else let
haftmann@29947
   130
    val c_ks = Vargraph.imm_succs (fst vardeps_data) c_k |> insert (op =) c_k;
haftmann@29947
   131
  in
haftmann@29947
   132
    vardeps_data
haftmann@29947
   133
    |> (apfst o Vargraph.map_node c_k o apsnd) (append diff_classes)
haftmann@29991
   134
    |> fold (fn styp => fold (assert_typmatch_inst thy arities eqngr styp) new_classes) styps
haftmann@29947
   135
    |> fold (fn c_k => add_classes thy arities eqngr c_k diff_classes) c_ks
haftmann@29947
   136
  end end
haftmann@29947
   137
and add_styp thy arities eqngr c_k tyco_styps vardeps_data =
haftmann@29947
   138
  let
haftmann@29947
   139
    val (old_styps, classes) = Vargraph.get_node (fst vardeps_data) c_k;
haftmann@29947
   140
  in if member (op =) old_styps tyco_styps then vardeps_data
haftmann@29947
   141
  else
haftmann@29947
   142
    vardeps_data
haftmann@29947
   143
    |> (apfst o Vargraph.map_node c_k o apfst) (cons tyco_styps)
haftmann@29991
   144
    |> fold (assert_typmatch_inst thy arities eqngr tyco_styps) classes
haftmann@29947
   145
  end
haftmann@29947
   146
and add_dep thy arities eqngr c_k c_k' vardeps_data =
haftmann@29947
   147
  let
haftmann@29947
   148
    val (_, classes) = Vargraph.get_node (fst vardeps_data) c_k;
haftmann@29947
   149
  in
haftmann@29947
   150
    vardeps_data
haftmann@29947
   151
    |> add_classes thy arities eqngr c_k' classes
haftmann@29947
   152
    |> apfst (Vargraph.add_edge (c_k, c_k'))
haftmann@29947
   153
  end
haftmann@29991
   154
and assert_typmatch_inst thy arities eqngr (tyco, styps) class vardeps_data =
haftmann@29947
   155
  if can (Sign.arity_sorts thy tyco) [class]
haftmann@29947
   156
  then vardeps_data
haftmann@29991
   157
    |> assert_inst thy arities eqngr (class, tyco)
haftmann@29947
   158
    |> fold_index (fn (k, styp) =>
haftmann@29991
   159
         assert_typmatch thy arities eqngr styp (Inst (class, tyco), k)) styps
haftmann@29947
   160
  else vardeps_data (*permissive!*)
haftmann@29991
   161
and assert_inst thy arities eqngr (inst as (class, tyco)) (vardeps_data as (_, (_, insts))) =
haftmann@29991
   162
  if member (op =) insts inst then vardeps_data
haftmann@29991
   163
  else let
haftmann@29947
   164
    val (classess, (superclasses, inst_params)) =
haftmann@29947
   165
      obtain_instance thy arities inst;
haftmann@29947
   166
  in
haftmann@29947
   167
    vardeps_data
haftmann@29991
   168
    |> (apsnd o apsnd) (insert (op =) inst)
haftmann@30020
   169
    |> fold_index (fn (k, _) =>
haftmann@30020
   170
         apfst (Vargraph.new_node ((Inst (class, tyco), k), ([] ,[])))) classess
haftmann@29991
   171
    |> fold (fn superclass => assert_inst thy arities eqngr (superclass, tyco)) superclasses
haftmann@29991
   172
    |> fold (assert_fun thy arities eqngr) inst_params
haftmann@29947
   173
    |> fold_index (fn (k, classes) =>
haftmann@30012
   174
         add_classes thy arities eqngr (Inst (class, tyco), k) classes
haftmann@29947
   175
         #> fold (fn superclass =>
haftmann@29947
   176
             add_dep thy arities eqngr (Inst (superclass, tyco), k)
haftmann@29947
   177
             (Inst (class, tyco), k)) superclasses
haftmann@29947
   178
         #> fold (fn inst_param =>
haftmann@29947
   179
             add_dep thy arities eqngr (Fun inst_param, k)
haftmann@29947
   180
             (Inst (class, tyco), k)
haftmann@29947
   181
             ) inst_params
haftmann@29947
   182
         ) classess
haftmann@29947
   183
  end
haftmann@29991
   184
and assert_typmatch thy arities eqngr (Tyco tyco_styps) c_k vardeps_data =
haftmann@29991
   185
      vardeps_data
haftmann@29991
   186
      |> add_styp thy arities eqngr c_k tyco_styps
haftmann@29991
   187
  | assert_typmatch thy arities eqngr (Var c_k') c_k vardeps_data =
haftmann@29991
   188
      vardeps_data
haftmann@29991
   189
      |> add_dep thy arities eqngr c_k c_k'
haftmann@29991
   190
  | assert_typmatch thy arities eqngr Free c_k vardeps_data =
haftmann@29991
   191
      vardeps_data
haftmann@29991
   192
and assert_rhs thy arities eqngr (c', styps) vardeps_data =
haftmann@29991
   193
  vardeps_data
haftmann@29991
   194
  |> assert_fun thy arities eqngr c'
haftmann@29991
   195
  |> fold_index (fn (k, styp) =>
haftmann@29991
   196
       assert_typmatch thy arities eqngr styp (Fun c', k)) styps
haftmann@29991
   197
and assert_fun thy arities eqngr c (vardeps_data as (_, (eqntab, _))) =
haftmann@29991
   198
  if Symtab.defined eqntab c then vardeps_data
haftmann@29991
   199
  else let
haftmann@29947
   200
    val ((lhs, rhss), eqns) = obtain_eqns thy eqngr c;
haftmann@29991
   201
    val rhss' = (map o apsnd o map) (styp_of (SOME (c, lhs))) rhss;
haftmann@29947
   202
  in
haftmann@29947
   203
    vardeps_data
haftmann@29991
   204
    |> (apsnd o apfst) (Symtab.update_new (c, (lhs, eqns)))
haftmann@30020
   205
    |> fold_index (fn (k, _) =>
haftmann@30020
   206
         apfst (Vargraph.new_node ((Fun c, k), ([] ,[])))) lhs
haftmann@29947
   207
    |> fold_index (fn (k, (_, sort)) =>
haftmann@30020
   208
         add_classes thy arities eqngr (Fun c, k) (complete_proper_sort thy sort)) lhs
haftmann@29991
   209
    |> fold (assert_rhs thy arities eqngr) rhss'
haftmann@29991
   210
  end;
haftmann@29947
   211
haftmann@29947
   212
haftmann@29947
   213
(* applying instantiations *)
haftmann@29947
   214
haftmann@29947
   215
fun dicts_of thy (proj_sort, algebra) (T, sort) =
haftmann@29947
   216
  let
haftmann@29947
   217
    fun class_relation (x, _) _ = x;
haftmann@29947
   218
    fun type_constructor tyco xs class =
haftmann@29991
   219
      inst_params thy tyco (Sorts.complete_sort algebra [class])
haftmann@29991
   220
        @ (maps o maps) fst xs;
haftmann@29947
   221
    fun type_variable (TFree (_, sort)) = map (pair []) (proj_sort sort);
haftmann@29947
   222
  in
haftmann@29947
   223
    flat (Sorts.of_sort_derivation (Syntax.pp_global thy) algebra
haftmann@29947
   224
      { class_relation = class_relation, type_constructor = type_constructor,
haftmann@29947
   225
        type_variable = type_variable } (T, proj_sort sort)
haftmann@29947
   226
       handle Sorts.CLASS_ERROR _ => [] (*permissive!*))
haftmann@29947
   227
  end;
haftmann@29947
   228
haftmann@29991
   229
fun add_arity thy vardeps (class, tyco) =
haftmann@29995
   230
  AList.default (op =)
haftmann@29991
   231
    ((class, tyco), map (fn k => (snd o Vargraph.get_node vardeps) (Inst (class, tyco), k))
haftmann@29991
   232
      (0 upto Sign.arity_number thy tyco - 1));
haftmann@29947
   233
haftmann@29991
   234
fun add_eqs thy (proj_sort, algebra) vardeps
haftmann@29991
   235
    (c, (proto_lhs, proto_eqns)) (rhss, eqngr) =
haftmann@29995
   236
  if can (Graph.get_node eqngr) c then (rhss, eqngr)
haftmann@29995
   237
  else let
haftmann@29947
   238
    val lhs = map_index (fn (k, (v, _)) =>
haftmann@29947
   239
      (v, snd (Vargraph.get_node vardeps (Fun c, k)))) proto_lhs;
haftmann@29947
   240
    val inst_tab = Vartab.empty |> fold (fn (v, sort) =>
haftmann@29947
   241
      Vartab.update ((v, 0), sort)) lhs;
haftmann@29947
   242
    val eqns = proto_eqns
haftmann@29947
   243
      |> (map o apfst) (Code_Unit.inst_thm thy inst_tab);
haftmann@29991
   244
    val (tyscm, rhss') = tyscm_rhss_of thy c eqns;
haftmann@29991
   245
    val eqngr' = Graph.new_node (c, (tyscm, eqns)) eqngr;
haftmann@29991
   246
  in (map (pair c) rhss' @ rhss, eqngr') end;
haftmann@29947
   247
haftmann@29991
   248
fun extend_arities_eqngr thy cs cs_rhss (arities, eqngr) =
haftmann@29947
   249
  let
haftmann@29991
   250
    val cs_rhss' = (map o apsnd o map) (styp_of NONE) cs_rhss;
haftmann@29991
   251
    val (vardeps, (eqntab, insts)) = empty_vardeps_data
haftmann@29991
   252
      |> fold (assert_fun thy arities eqngr) cs
haftmann@29991
   253
      |> fold (assert_rhs thy arities eqngr) cs_rhss';
haftmann@29991
   254
    val arities' = fold (add_arity thy vardeps) insts arities;
haftmann@30001
   255
    val pp = Syntax.pp_global thy;
haftmann@30001
   256
    val is_proper_class = can (AxClass.get_info thy);
haftmann@30001
   257
    val (proj_sort, algebra) = Sorts.subalgebra pp is_proper_class
haftmann@30001
   258
      (AList.lookup (op =) arities') (Sign.classes_of thy);
haftmann@29991
   259
    val (rhss, eqngr') = Symtab.fold
haftmann@29991
   260
      (add_eqs thy (proj_sort, algebra) vardeps) eqntab ([], eqngr);
haftmann@29991
   261
    fun deps_of (c, rhs) = c ::
haftmann@29991
   262
      maps (dicts_of thy (proj_sort, algebra))
haftmann@29991
   263
        (rhs ~~ (map snd o fst o fst o Graph.get_node eqngr') c);
haftmann@29991
   264
    val eqngr'' = fold (fn (c, rhs) => fold
haftmann@29991
   265
      (curry Graph.add_edge c) (deps_of rhs)) rhss eqngr';
haftmann@29991
   266
  in ((proj_sort, algebra), (arities', eqngr'')) end;
haftmann@29947
   267
haftmann@29947
   268
haftmann@29947
   269
(** retrieval interfaces **)
haftmann@29947
   270
haftmann@29947
   271
fun proto_eval thy cterm_of evaluator_lift evaluator proto_ct arities_eqngr =
haftmann@29947
   272
  let
haftmann@29947
   273
    val ct = cterm_of proto_ct;
haftmann@29947
   274
    val _ = Sign.no_vars (Syntax.pp_global thy) (Thm.term_of ct);
haftmann@29947
   275
    val _ = Term.fold_types (Type.no_tvars #> K I) (Thm.term_of ct) ();
haftmann@29947
   276
    fun consts_of t =
haftmann@29947
   277
      fold_aterms (fn Const c_ty => cons c_ty | _ => I) t [];
haftmann@29947
   278
    val thm = Code.preprocess_conv thy ct;
haftmann@29947
   279
    val ct' = Thm.rhs_of thm;
haftmann@29947
   280
    val t' = Thm.term_of ct';
haftmann@29991
   281
    val (t'', evaluator_eqngr) = evaluator t';
haftmann@29947
   282
    val consts = map fst (consts_of t');
haftmann@29947
   283
    val consts' = consts_of t'';
haftmann@29991
   284
    val const_matches' = fold (fn (c, ty) =>
haftmann@29998
   285
      insert (op =) (c, Sign.const_typargs thy (c, ty))) consts' [];
haftmann@29991
   286
    val (algebra', arities_eqngr') =
haftmann@29991
   287
      extend_arities_eqngr thy consts const_matches' arities_eqngr;
haftmann@29991
   288
  in
haftmann@29991
   289
    (evaluator_lift (evaluator_eqngr algebra') thm (snd arities_eqngr'),
haftmann@29991
   290
      arities_eqngr')
haftmann@29991
   291
  end;
haftmann@29947
   292
haftmann@29947
   293
fun proto_eval_conv thy =
haftmann@29947
   294
  let
haftmann@29947
   295
    fun evaluator_lift evaluator thm1 eqngr =
haftmann@29947
   296
      let
haftmann@29947
   297
        val thm2 = evaluator eqngr;
haftmann@29947
   298
        val thm3 = Code.postprocess_conv thy (Thm.rhs_of thm2);
haftmann@29947
   299
      in
haftmann@29947
   300
        Thm.transitive thm1 (Thm.transitive thm2 thm3) handle THM _ =>
haftmann@29947
   301
          error ("could not construct evaluation proof:\n"
haftmann@29947
   302
          ^ (cat_lines o map Display.string_of_thm) [thm1, thm2, thm3])
haftmann@29947
   303
      end;
haftmann@29947
   304
  in proto_eval thy I evaluator_lift end;
haftmann@29947
   305
haftmann@29947
   306
fun proto_eval_term thy =
haftmann@29947
   307
  let
haftmann@29947
   308
    fun evaluator_lift evaluator _ eqngr = evaluator eqngr;
haftmann@29947
   309
  in proto_eval thy (Thm.cterm_of thy) evaluator_lift end;
haftmann@29947
   310
haftmann@29947
   311
structure Wellsorted = CodeDataFun
haftmann@29947
   312
(
haftmann@29947
   313
  type T = ((string * class) * sort list) list * T;
haftmann@29947
   314
  val empty = ([], Graph.empty);
haftmann@29947
   315
  fun purge thy cs (arities, eqngr) =
haftmann@29947
   316
    let
haftmann@29947
   317
      val del_cs = ((Graph.all_preds eqngr
haftmann@29947
   318
        o filter (can (Graph.get_node eqngr))) cs);
haftmann@29987
   319
      val del_arities = del_cs
haftmann@29987
   320
        |> map_filter (AxClass.inst_of_param thy)
haftmann@29987
   321
        |> maps (fn (c, tyco) =>
haftmann@29987
   322
             (map (rpair tyco) o Sign.complete_sort thy o the_list
haftmann@29987
   323
               o AxClass.class_of_param thy) c);
haftmann@29947
   324
      val arities' = fold (AList.delete (op =)) del_arities arities;
haftmann@29947
   325
      val eqngr' = Graph.del_nodes del_cs eqngr;
haftmann@29947
   326
    in (arities', eqngr') end;
haftmann@29947
   327
);
haftmann@29947
   328
haftmann@29987
   329
fun make thy cs = apsnd snd
haftmann@29987
   330
  (Wellsorted.change_yield thy (extend_arities_eqngr thy cs []));
haftmann@29947
   331
haftmann@29947
   332
fun eval_conv thy f =
haftmann@29947
   333
  fst o Wellsorted.change_yield thy o proto_eval_conv thy f;
haftmann@29947
   334
haftmann@29947
   335
fun eval_term thy f =
haftmann@29947
   336
  fst o Wellsorted.change_yield thy o proto_eval_term thy f;
haftmann@29947
   337
haftmann@29947
   338
haftmann@29947
   339
(** diagnostic commands **)
haftmann@29947
   340
haftmann@29947
   341
fun code_depgr thy consts =
haftmann@29947
   342
  let
haftmann@29947
   343
    val (_, eqngr) = make thy consts;
haftmann@29947
   344
    val select = Graph.all_succs eqngr consts;
haftmann@29947
   345
  in
haftmann@29947
   346
    eqngr
haftmann@29947
   347
    |> not (null consts) ? Graph.subgraph (member (op =) select) 
haftmann@29947
   348
    |> Graph.map_nodes ((apsnd o map o apfst) (AxClass.overload thy))
haftmann@29947
   349
  end;
haftmann@29947
   350
haftmann@29947
   351
fun code_thms thy = Pretty.writeln o pretty thy o code_depgr thy;
haftmann@29947
   352
haftmann@29947
   353
fun code_deps thy consts =
haftmann@29947
   354
  let
haftmann@29947
   355
    val eqngr = code_depgr thy consts;
haftmann@29947
   356
    fun mk_entry (const, (_, (_, parents))) =
haftmann@29947
   357
      let
haftmann@29947
   358
        val name = Code_Unit.string_of_const thy const;
haftmann@29947
   359
        val nameparents = map (Code_Unit.string_of_const thy) parents;
haftmann@29947
   360
      in { name = name, ID = name, dir = "", unfold = true,
haftmann@29947
   361
        path = "", parents = nameparents }
haftmann@29947
   362
      end;
haftmann@29947
   363
    val prgr = Graph.fold ((fn x => fn xs => xs @ [x]) o mk_entry) eqngr [];
haftmann@29947
   364
  in Present.display_graph prgr end;
haftmann@29947
   365
haftmann@29947
   366
local
haftmann@29947
   367
haftmann@29947
   368
structure P = OuterParse
haftmann@29947
   369
and K = OuterKeyword
haftmann@29947
   370
haftmann@29947
   371
fun code_thms_cmd thy = code_thms thy o op @ o Code_Name.read_const_exprs thy;
haftmann@29947
   372
fun code_deps_cmd thy = code_deps thy o op @ o Code_Name.read_const_exprs thy;
haftmann@29947
   373
haftmann@29947
   374
in
haftmann@29947
   375
haftmann@29947
   376
val _ =
haftmann@29961
   377
  OuterSyntax.improper_command "code_thms" "print system of code equations for code" OuterKeyword.diag
haftmann@29947
   378
    (Scan.repeat P.term_group
haftmann@29947
   379
      >> (fn cs => Toplevel.no_timing o Toplevel.unknown_theory
haftmann@29947
   380
        o Toplevel.keep ((fn thy => code_thms_cmd thy cs) o Toplevel.theory_of)));
haftmann@29947
   381
haftmann@29947
   382
val _ =
haftmann@29961
   383
  OuterSyntax.improper_command "code_deps" "visualize dependencies of code equations for code" OuterKeyword.diag
haftmann@29947
   384
    (Scan.repeat P.term_group
haftmann@29947
   385
      >> (fn cs => Toplevel.no_timing o Toplevel.unknown_theory
haftmann@29947
   386
        o Toplevel.keep ((fn thy => code_deps_cmd thy cs) o Toplevel.theory_of)));
haftmann@29947
   387
haftmann@29947
   388
end;
haftmann@29947
   389
haftmann@29947
   390
end; (*struct*)