src/Tools/code/code_funcgr.ML
author haftmann
Mon, 02 Mar 2009 16:58:39 +0100
changeset 30202 2775062fd3a9
parent 29961 5e9d471afef3
child 30242 aea5d7fa7ef5
permissions -rw-r--r--
reduced confusion code_funcgr vs. code_wellsorted
haftmann@24219
     1
(*  Title:      Tools/code/code_funcgr.ML
haftmann@24219
     2
    Author:     Florian Haftmann, TU Muenchen
haftmann@24219
     3
haftmann@29899
     4
Retrieving, normalizing and structuring code equations in graph
haftmann@24219
     5
with explicit dependencies.
haftmann@30202
     6
haftmann@30202
     7
Legacy.  To be replaced by Tools/code/code_wellsorted.ML
haftmann@24219
     8
*)
haftmann@24219
     9
haftmann@30202
    10
signature CODE_WELLSORTED =
haftmann@24219
    11
sig
haftmann@24219
    12
  type T
haftmann@28370
    13
  val eqns: T -> string -> (thm * bool) list
haftmann@26971
    14
  val typ: T -> string -> (string * sort) list * typ
haftmann@24423
    15
  val all: T -> string list
haftmann@24219
    16
  val pretty: theory -> T -> Pretty.T
haftmann@28924
    17
  val make: theory -> string list
haftmann@28924
    18
    -> ((sort -> sort) * Sorts.algebra) * T
haftmann@28924
    19
  val eval_conv: theory
haftmann@28924
    20
    -> (term -> term * (((sort -> sort) * Sorts.algebra) -> T -> thm)) -> cterm -> thm
haftmann@28924
    21
  val eval_term: theory
haftmann@28924
    22
    -> (term -> term * (((sort -> sort) * Sorts.algebra) -> T -> 'a)) -> term -> 'a
haftmann@26971
    23
  val timing: bool ref
haftmann@24219
    24
end
haftmann@24219
    25
haftmann@30202
    26
structure Code_Wellsorted : CODE_WELLSORTED =
haftmann@24219
    27
struct
haftmann@24219
    28
haftmann@24219
    29
(** the graph type **)
haftmann@24219
    30
haftmann@28350
    31
type T = (((string * sort) list * typ) * (thm * bool) list) Graph.T;
haftmann@24219
    32
haftmann@28370
    33
fun eqns funcgr =
haftmann@24423
    34
  these o Option.map snd o try (Graph.get_node funcgr);
haftmann@24219
    35
haftmann@24219
    36
fun typ funcgr =
haftmann@24423
    37
  fst o Graph.get_node funcgr;
haftmann@24219
    38
haftmann@24423
    39
fun all funcgr = Graph.keys funcgr;
haftmann@24219
    40
haftmann@24219
    41
fun pretty thy funcgr =
haftmann@24423
    42
  AList.make (snd o Graph.get_node funcgr) (Graph.keys funcgr)
haftmann@28054
    43
  |> (map o apfst) (Code_Unit.string_of_const thy)
haftmann@24219
    44
  |> sort (string_ord o pairself fst)
haftmann@24219
    45
  |> map (fn (s, thms) =>
haftmann@24219
    46
       (Pretty.block o Pretty.fbreaks) (
haftmann@24219
    47
         Pretty.str s
haftmann@28350
    48
         :: map (Display.pretty_thm o fst) thms
haftmann@24219
    49
       ))
haftmann@24219
    50
  |> Pretty.chunks;
haftmann@24219
    51
haftmann@24219
    52
haftmann@24219
    53
(** generic combinators **)
haftmann@24219
    54
haftmann@24219
    55
fun fold_consts f thms =
haftmann@24219
    56
  thms
haftmann@24219
    57
  |> maps (op :: o swap o apfst (snd o strip_comb) o Logic.dest_equals o Thm.plain_prop_of)
haftmann@24219
    58
  |> (fold o fold_aterms) (fn Const c => f c | _ => I);
haftmann@24219
    59
haftmann@24219
    60
fun consts_of (const, []) = []
haftmann@24423
    61
  | consts_of (const, thms as _ :: _) = 
haftmann@24219
    62
      let
haftmann@24423
    63
        fun the_const (c, _) = if c = const then I else insert (op =) c
haftmann@28350
    64
      in fold_consts the_const (map fst thms) [] end;
haftmann@24219
    65
haftmann@26971
    66
fun insts_of thy algebra tys sorts =
haftmann@24219
    67
  let
haftmann@24219
    68
    fun class_relation (x, _) _ = x;
haftmann@24219
    69
    fun type_constructor tyco xs class =
haftmann@26971
    70
      (tyco, class) :: (maps o maps) fst xs;
haftmann@24219
    71
    fun type_variable (TVar (_, sort)) = map (pair []) sort
haftmann@24219
    72
      | type_variable (TFree (_, sort)) = map (pair []) sort;
haftmann@26971
    73
    fun of_sort_deriv ty sort =
wenzelm@26939
    74
      Sorts.of_sort_derivation (Syntax.pp_global thy) algebra
haftmann@24219
    75
        { class_relation = class_relation, type_constructor = type_constructor,
haftmann@24219
    76
          type_variable = type_variable }
haftmann@26517
    77
        (ty, sort) handle Sorts.CLASS_ERROR _ => [] (*permissive!*)
haftmann@26971
    78
  in (flat o flat) (map2 of_sort_deriv tys sorts) end;
haftmann@24219
    79
haftmann@26971
    80
fun meets_of thy algebra =
haftmann@24219
    81
  let
haftmann@26971
    82
    fun meet_of ty sort tab =
haftmann@26971
    83
      Sorts.meet_sort algebra (ty, sort) tab
haftmann@26971
    84
        handle Sorts.CLASS_ERROR _ => tab (*permissive!*);
haftmann@26971
    85
  in fold2 meet_of end;
haftmann@24219
    86
haftmann@24219
    87
haftmann@24219
    88
(** graph algorithm **)
haftmann@24219
    89
haftmann@24219
    90
val timing = ref false;
haftmann@24219
    91
haftmann@24219
    92
local
haftmann@24219
    93
haftmann@26971
    94
fun resort_thms thy algebra typ_of thms =
haftmann@26971
    95
  let
haftmann@26971
    96
    val cs = fold_consts (insert (op =)) thms [];
haftmann@26971
    97
    fun meets (c, ty) = case typ_of c
haftmann@26971
    98
       of SOME (vs, _) =>
haftmann@26971
    99
            meets_of thy algebra (Sign.const_typargs thy (c, ty)) (map snd vs)
haftmann@26971
   100
        | NONE => I;
haftmann@26971
   101
    val tab = fold meets cs Vartab.empty;
haftmann@28423
   102
  in map (Code_Unit.inst_thm thy tab) thms end;
haftmann@24219
   103
haftmann@28370
   104
fun resort_eqnss thy algebra funcgr =
haftmann@24219
   105
  let
haftmann@24423
   106
    val typ_funcgr = try (fst o Graph.get_node funcgr);
haftmann@28350
   107
    val resort_dep = (apsnd o burrow_fst) (resort_thms thy algebra typ_funcgr);
haftmann@26997
   108
    fun resort_rec typ_of (c, []) = (true, (c, []))
haftmann@28350
   109
      | resort_rec typ_of (c, thms as (thm, _) :: _) = if is_some (AxClass.inst_of_param thy c)
haftmann@26997
   110
          then (true, (c, thms))
haftmann@26997
   111
          else let
haftmann@28423
   112
            val (_, (vs, ty)) = Code_Unit.head_eqn thy thm;
haftmann@28350
   113
            val thms' as (thm', _) :: _ = burrow_fst (resort_thms thy algebra typ_of) thms
haftmann@28423
   114
            val (_, (vs', ty')) = Code_Unit.head_eqn thy thm'; (*FIXME simplify check*)
haftmann@26997
   115
          in (Sign.typ_equiv thy (ty, ty'), (c, thms')) end;
haftmann@28370
   116
    fun resort_recs eqnss =
haftmann@24219
   117
      let
haftmann@28370
   118
        fun typ_of c = case these (AList.lookup (op =) eqnss c)
haftmann@28423
   119
         of (thm, _) :: _ => (SOME o snd o Code_Unit.head_eqn thy) thm
haftmann@26971
   120
          | [] => NONE;
haftmann@28370
   121
        val (unchangeds, eqnss') = split_list (map (resort_rec typ_of) eqnss);
haftmann@24219
   122
        val unchanged = fold (fn x => fn y => x andalso y) unchangeds true;
haftmann@28370
   123
      in (unchanged, eqnss') end;
haftmann@28370
   124
    fun resort_rec_until eqnss =
haftmann@24219
   125
      let
haftmann@28370
   126
        val (unchanged, eqnss') = resort_recs eqnss;
haftmann@28370
   127
      in if unchanged then eqnss' else resort_rec_until eqnss' end;
haftmann@24219
   128
  in map resort_dep #> resort_rec_until end;
haftmann@24219
   129
haftmann@24219
   130
fun instances_of thy algebra insts =
haftmann@24219
   131
  let
haftmann@24219
   132
    val thy_classes = (#classes o Sorts.rep_algebra o Sign.classes_of) thy;
haftmann@24835
   133
    fun all_classparams tyco class =
wenzelm@24969
   134
      these (try (#params o AxClass.get_info thy) class)
haftmann@26517
   135
      |> map_filter (fn (c, _) => try (AxClass.param_of_inst thy) (c, tyco))
haftmann@24219
   136
  in
haftmann@24219
   137
    Symtab.empty
haftmann@24219
   138
    |> fold (fn (tyco, class) =>
haftmann@24219
   139
        Symtab.map_default (tyco, []) (insert (op =) class)) insts
haftmann@24835
   140
    |> (fn tab => Symtab.fold (fn (tyco, classes) => append (maps (all_classparams tyco)
haftmann@24219
   141
         (Graph.all_succs thy_classes classes))) tab [])
haftmann@24219
   142
  end;
haftmann@24219
   143
haftmann@24219
   144
fun instances_of_consts thy algebra funcgr consts =
haftmann@24219
   145
  let
haftmann@26971
   146
    fun inst (cexpr as (c, ty)) = insts_of thy algebra
haftmann@26971
   147
      (Sign.const_typargs thy (c, ty)) ((map snd o fst) (typ funcgr c));
haftmann@24219
   148
  in
haftmann@24219
   149
    []
haftmann@24219
   150
    |> fold (fold (insert (op =)) o inst) consts
haftmann@24219
   151
    |> instances_of thy algebra
haftmann@24219
   152
  end;
haftmann@24219
   153
haftmann@24283
   154
fun ensure_const' thy algebra funcgr const auxgr =
haftmann@24423
   155
  if can (Graph.get_node funcgr) const
haftmann@24219
   156
    then (NONE, auxgr)
haftmann@24423
   157
  else if can (Graph.get_node auxgr) const
haftmann@24219
   158
    then (SOME const, auxgr)
haftmann@24219
   159
  else if is_some (Code.get_datatype_of_constr thy const) then
haftmann@24219
   160
    auxgr
haftmann@24423
   161
    |> Graph.new_node (const, [])
haftmann@24219
   162
    |> pair (SOME const)
haftmann@24219
   163
  else let
haftmann@28370
   164
    val thms = Code.these_eqns thy const
haftmann@28423
   165
      |> burrow_fst (Code_Unit.norm_args thy)
haftmann@28423
   166
      |> burrow_fst (Code_Unit.norm_varnames thy Code_Name.purify_tvar Code_Name.purify_var);
haftmann@24219
   167
    val rhs = consts_of (const, thms);
haftmann@24219
   168
  in
haftmann@24219
   169
    auxgr
haftmann@24423
   170
    |> Graph.new_node (const, thms)
haftmann@24283
   171
    |> fold_map (ensure_const thy algebra funcgr) rhs
haftmann@24423
   172
    |-> (fn rhs' => fold (fn SOME const' => Graph.add_edge (const, const')
haftmann@24219
   173
                           | NONE => I) rhs')
haftmann@24219
   174
    |> pair (SOME const)
haftmann@24219
   175
  end
haftmann@24283
   176
and ensure_const thy algebra funcgr const =
haftmann@24219
   177
  let
haftmann@24219
   178
    val timeap = if !timing
haftmann@28054
   179
      then Output.timeap_msg ("time for " ^ Code_Unit.string_of_const thy const)
haftmann@24219
   180
      else I;
haftmann@24283
   181
  in timeap (ensure_const' thy algebra funcgr const) end;
haftmann@24219
   182
haftmann@28370
   183
fun merge_eqnss thy algebra raw_eqnss funcgr =
haftmann@24219
   184
  let
haftmann@28370
   185
    val eqnss = raw_eqnss
haftmann@28370
   186
      |> resort_eqnss thy algebra funcgr
haftmann@24423
   187
      |> filter_out (can (Graph.get_node funcgr) o fst);
haftmann@28423
   188
    fun typ_eqn c [] = Code.default_typscheme thy c
haftmann@28423
   189
      | typ_eqn c (thms as (thm, _) :: _) = (snd o Code_Unit.head_eqn thy) thm;
haftmann@28370
   190
    fun add_eqns (const, thms) =
haftmann@28370
   191
      Graph.new_node (const, (typ_eqn const thms, thms));
haftmann@28370
   192
    fun add_deps (eqns as (const, thms)) funcgr =
haftmann@24219
   193
      let
haftmann@28370
   194
        val deps = consts_of eqns;
haftmann@24219
   195
        val insts = instances_of_consts thy algebra funcgr
haftmann@28350
   196
          (fold_consts (insert (op =)) (map fst thms) []);
haftmann@24219
   197
      in
haftmann@24219
   198
        funcgr
haftmann@26997
   199
        |> ensure_consts thy algebra insts
haftmann@24423
   200
        |> fold (curry Graph.add_edge const) deps
haftmann@24423
   201
        |> fold (curry Graph.add_edge const) insts
haftmann@24219
   202
       end;
haftmann@24219
   203
  in
haftmann@24219
   204
    funcgr
haftmann@28370
   205
    |> fold add_eqns eqnss
haftmann@28370
   206
    |> fold add_deps eqnss
haftmann@24219
   207
  end
haftmann@26997
   208
and ensure_consts thy algebra cs funcgr =
haftmann@24219
   209
  let
haftmann@24423
   210
    val auxgr = Graph.empty
haftmann@24283
   211
      |> fold (snd oo ensure_const thy algebra funcgr) cs;
haftmann@24219
   212
  in
haftmann@24219
   213
    funcgr
haftmann@28370
   214
    |> fold (merge_eqnss thy algebra)
haftmann@24423
   215
         (map (AList.make (Graph.get_node auxgr))
haftmann@24423
   216
         (rev (Graph.strong_conn auxgr)))
haftmann@26997
   217
  end;
haftmann@24219
   218
haftmann@24219
   219
in
haftmann@24219
   220
haftmann@24219
   221
(** retrieval interfaces **)
haftmann@24219
   222
haftmann@26997
   223
val ensure_consts = ensure_consts;
haftmann@24219
   224
haftmann@28724
   225
fun proto_eval thy cterm_of evaluator_lift evaluator proto_ct funcgr =
haftmann@24219
   226
  let
haftmann@26740
   227
    val ct = cterm_of proto_ct;
wenzelm@26939
   228
    val _ = Sign.no_vars (Syntax.pp_global thy) (Thm.term_of ct);
haftmann@24219
   229
    val _ = Term.fold_types (Type.no_tvars #> K I) (Thm.term_of ct) ();
haftmann@28724
   230
    fun consts_of t =
haftmann@28724
   231
      fold_aterms (fn Const c_ty => cons c_ty | _ => I) t [];
haftmann@26740
   232
    val algebra = Code.coregular_algebra thy;
haftmann@28423
   233
    val thm = Code.preprocess_conv thy ct;
haftmann@26740
   234
    val ct' = Thm.rhs_of thm;
haftmann@26740
   235
    val t' = Thm.term_of ct';
haftmann@26740
   236
    val consts = map fst (consts_of t');
haftmann@26740
   237
    val funcgr' = ensure_consts thy algebra consts funcgr;
haftmann@28724
   238
    val (t'', evaluator_funcgr) = evaluator t';
haftmann@26740
   239
    val consts' = consts_of t'';
haftmann@26740
   240
    val dicts = instances_of_consts thy algebra funcgr' consts';
haftmann@26740
   241
    val funcgr'' = ensure_consts thy algebra dicts funcgr';
haftmann@28924
   242
  in (evaluator_lift (evaluator_funcgr (Code.operational_algebra thy)) thm funcgr'', funcgr'') end;
haftmann@26740
   243
haftmann@26740
   244
fun proto_eval_conv thy =
haftmann@26740
   245
  let
haftmann@28724
   246
    fun evaluator_lift evaluator thm1 funcgr =
haftmann@24219
   247
      let
haftmann@28724
   248
        val thm2 = evaluator funcgr;
haftmann@28423
   249
        val thm3 = Code.postprocess_conv thy (Thm.rhs_of thm2);
haftmann@24219
   250
      in
haftmann@26740
   251
        Thm.transitive thm1 (Thm.transitive thm2 thm3) handle THM _ =>
haftmann@26971
   252
          error ("could not construct evaluation proof:\n"
wenzelm@26928
   253
          ^ (cat_lines o map Display.string_of_thm) [thm1, thm2, thm3])
haftmann@24219
   254
      end;
haftmann@28724
   255
  in proto_eval thy I evaluator_lift end;
haftmann@24283
   256
haftmann@26740
   257
fun proto_eval_term thy =
haftmann@24283
   258
  let
haftmann@28724
   259
    fun evaluator_lift evaluator _ funcgr = evaluator funcgr;
haftmann@28724
   260
  in proto_eval thy (Thm.cterm_of thy) evaluator_lift end;
haftmann@24219
   261
haftmann@24219
   262
end; (*local*)
haftmann@24219
   263
haftmann@24219
   264
structure Funcgr = CodeDataFun
wenzelm@24713
   265
(
haftmann@24219
   266
  type T = T;
haftmann@24423
   267
  val empty = Graph.empty;
haftmann@27609
   268
  fun purge _ cs funcgr =
haftmann@27609
   269
    Graph.del_nodes ((Graph.all_preds funcgr 
haftmann@27609
   270
      o filter (can (Graph.get_node funcgr))) cs) funcgr;
wenzelm@24713
   271
);
haftmann@24219
   272
haftmann@24219
   273
fun make thy =
haftmann@28924
   274
  pair (Code.operational_algebra thy)
haftmann@28924
   275
  o Funcgr.change thy o ensure_consts thy (Code.coregular_algebra thy);
haftmann@24219
   276
haftmann@24219
   277
fun eval_conv thy f =
haftmann@26740
   278
  fst o Funcgr.change_yield thy o proto_eval_conv thy f;
haftmann@24283
   279
haftmann@24283
   280
fun eval_term thy f =
haftmann@26740
   281
  fst o Funcgr.change_yield thy o proto_eval_term thy f;
haftmann@24219
   282
haftmann@27103
   283
haftmann@27103
   284
(** diagnostic commands **)
haftmann@27103
   285
haftmann@28350
   286
fun code_depgr thy consts =
haftmann@28350
   287
  let
haftmann@28924
   288
    val (_, gr) = make thy consts;
haftmann@28350
   289
    val select = Graph.all_succs gr consts;
haftmann@28350
   290
  in
haftmann@28350
   291
    gr
haftmann@28350
   292
    |> not (null consts) ? Graph.subgraph (member (op =) select) 
haftmann@28350
   293
    |> Graph.map_nodes ((apsnd o map o apfst) (AxClass.overload thy))
haftmann@28350
   294
  end;
haftmann@27103
   295
haftmann@27103
   296
fun code_thms thy = Pretty.writeln o pretty thy o code_depgr thy;
haftmann@27103
   297
haftmann@27103
   298
fun code_deps thy consts =
haftmann@27103
   299
  let
haftmann@27103
   300
    val gr = code_depgr thy consts;
haftmann@27103
   301
    fun mk_entry (const, (_, (_, parents))) =
haftmann@27103
   302
      let
haftmann@28054
   303
        val name = Code_Unit.string_of_const thy const;
haftmann@28054
   304
        val nameparents = map (Code_Unit.string_of_const thy) parents;
haftmann@27103
   305
      in { name = name, ID = name, dir = "", unfold = true,
haftmann@27103
   306
        path = "", parents = nameparents }
haftmann@27103
   307
      end;
haftmann@27103
   308
    val prgr = Graph.fold ((fn x => fn xs => xs @ [x]) o mk_entry) gr [];
haftmann@27103
   309
  in Present.display_graph prgr end;
haftmann@27103
   310
haftmann@27103
   311
local
haftmann@27103
   312
haftmann@27103
   313
structure P = OuterParse
haftmann@27103
   314
and K = OuterKeyword
haftmann@27103
   315
haftmann@28054
   316
fun code_thms_cmd thy = code_thms thy o op @ o Code_Name.read_const_exprs thy;
haftmann@28054
   317
fun code_deps_cmd thy = code_deps thy o op @ o Code_Name.read_const_exprs thy;
haftmann@27103
   318
haftmann@27103
   319
in
haftmann@27103
   320
haftmann@27103
   321
val _ =
haftmann@29961
   322
  OuterSyntax.improper_command "code_thms" "print system of code equations for code" OuterKeyword.diag
haftmann@28338
   323
    (Scan.repeat P.term_group
haftmann@27103
   324
      >> (fn cs => Toplevel.no_timing o Toplevel.unknown_theory
haftmann@27103
   325
        o Toplevel.keep ((fn thy => code_thms_cmd thy cs) o Toplevel.theory_of)));
haftmann@27103
   326
haftmann@27103
   327
val _ =
haftmann@29961
   328
  OuterSyntax.improper_command "code_deps" "visualize dependencies of code equations for code" OuterKeyword.diag
haftmann@28338
   329
    (Scan.repeat P.term_group
haftmann@27103
   330
      >> (fn cs => Toplevel.no_timing o Toplevel.unknown_theory
haftmann@27103
   331
        o Toplevel.keep ((fn thy => code_deps_cmd thy cs) o Toplevel.theory_of)));
haftmann@27103
   332
haftmann@27103
   333
end;
haftmann@27103
   334
haftmann@24219
   335
end; (*struct*)