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