src/Pure/Tools/codegen_funcgr.ML
author haftmann
Fri, 03 Nov 2006 14:22:45 +0100
changeset 21159 7f6bdffe3d06
parent 21129 8e621232a865
child 21196 42ee69856dd0
permissions -rw-r--r--
fixed problem with variable names
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
     1
(*  Title:      Pure/Tools/codegen_funcgr.ML
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
     2
    ID:         $Id$
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
     3
    Author:     Florian Haftmann, TU Muenchen
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
     4
20855
9f60d493c8fe clarified header comments
haftmann
parents: 20835
diff changeset
     5
Retrieving, normalizing and structuring code function theorems
9f60d493c8fe clarified header comments
haftmann
parents: 20835
diff changeset
     6
in graph with explicit dependencies.
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
     7
*)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
     8
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
     9
signature CODEGEN_FUNCGR =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    10
sig
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    11
  type T;
21120
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
    12
  val make: theory -> CodegenConsts.const list -> T
21129
8e621232a865 clarified make_term interface
haftmann
parents: 21120
diff changeset
    13
  val make_term: theory -> ((thm -> thm) -> cterm -> thm -> 'a) -> cterm -> 'a * T
21120
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
    14
  val funcs: T -> CodegenConsts.const -> thm list
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
    15
  val typ: T -> CodegenConsts.const -> typ
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
    16
  val deps: T -> CodegenConsts.const list -> CodegenConsts.const list list
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
    17
  val all: T -> CodegenConsts.const list
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
    18
  val norm_vars: theory -> thm list -> thm list
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    19
  val print_codethms: theory -> CodegenConsts.const list -> unit
20938
041badc7fcaf added keeping of funcgr
haftmann
parents: 20896
diff changeset
    20
  structure Constgraph : GRAPH
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    21
end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    22
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    23
structure CodegenFuncgr: CODEGEN_FUNCGR =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    24
struct
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    25
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    26
(** code data **)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    27
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    28
structure Consttab = CodegenConsts.Consttab;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    29
structure Constgraph = GraphFun (
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    30
  type key = CodegenConsts.const;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    31
  val ord = CodegenConsts.const_ord;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    32
);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    33
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    34
type T = (typ * thm list) Constgraph.T;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    35
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    36
structure Funcgr = CodeDataFun
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    37
(struct
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    38
  val name = "Pure/codegen_funcgr";
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    39
  type T = T;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    40
  val empty = Constgraph.empty;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    41
  fun merge _ _ = Constgraph.empty;
20938
041badc7fcaf added keeping of funcgr
haftmann
parents: 20896
diff changeset
    42
  fun purge _ NONE _ = Constgraph.empty
041badc7fcaf added keeping of funcgr
haftmann
parents: 20896
diff changeset
    43
    | purge _ (SOME cs) funcgr =
041badc7fcaf added keeping of funcgr
haftmann
parents: 20896
diff changeset
    44
        Constgraph.del_nodes ((Constgraph.all_succs funcgr 
041badc7fcaf added keeping of funcgr
haftmann
parents: 20896
diff changeset
    45
          o filter (can (Constgraph.get_node funcgr))) cs) funcgr;
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    46
end);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    47
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    48
val _ = Context.add_setup Funcgr.init;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    49
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    50
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    51
(** theorem purification **)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    52
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    53
fun abs_norm thy thm =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    54
  let
20705
da71d46b8b2f fixed some mess
haftmann
parents: 20600
diff changeset
    55
    fun eta_expand thm =
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    56
      let
20705
da71d46b8b2f fixed some mess
haftmann
parents: 20600
diff changeset
    57
        val lhs = (fst o Logic.dest_equals o Drule.plain_prop_of) thm;
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    58
        val tys = (fst o strip_type o fastype_of) lhs;
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
    59
        val ctxt = Name.make_context (map (fst o fst) (Term.add_vars lhs []));
20705
da71d46b8b2f fixed some mess
haftmann
parents: 20600
diff changeset
    60
        val vs_ct = map (fn (v, ty) => cterm_of thy (Var ((v, 0), ty)))
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
    61
          (Name.names ctxt "a" tys);
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    62
      in
20705
da71d46b8b2f fixed some mess
haftmann
parents: 20600
diff changeset
    63
        fold (fn ct => fn thm => Thm.combination thm (Thm.reflexive ct)) vs_ct thm
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    64
      end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    65
  in
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    66
    thm
20705
da71d46b8b2f fixed some mess
haftmann
parents: 20600
diff changeset
    67
    |> eta_expand
21120
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
    68
    |> Drule.fconv_rule Drule.beta_eta_conversion
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    69
  end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    70
21120
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
    71
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    72
fun canonical_tvars thy thm =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    73
  let
21159
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
    74
    fun tvars_subst_for thm = (fold_types o fold_atyps)
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
    75
      (fn TVar (v_i as (v, _), sort) => let
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
    76
            val v' = CodegenNames.purify_var v
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
    77
          in if v = v' then I
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
    78
          else insert (op =) (v_i, (v', sort)) end
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    79
        | _ => I) (prop_of thm) [];
21159
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
    80
    fun mk_inst (v_i, (v', sort)) (maxidx, acc) =
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
    81
      let
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
    82
        val ty = TVar (v_i, sort)
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
    83
      in
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
    84
        (maxidx + 1, (ctyp_of thy ty, ctyp_of thy (TVar ((v', maxidx), sort))) :: acc)
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
    85
      end;
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    86
    val maxidx = Thm.maxidx_of thm + 1;
21159
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
    87
    val (_, inst) = fold mk_inst (tvars_subst_for thm) (maxidx + 1, []);
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    88
  in Thm.instantiate (inst, []) thm end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    89
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    90
fun canonical_vars thy thm =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    91
  let
21159
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
    92
    fun vars_subst_for thm = fold_aterms
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
    93
      (fn Var (v_i as (v, _), ty) => let
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
    94
            val v' = CodegenNames.purify_var v
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
    95
          in if v = v' then I
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
    96
          else insert (op =) (v_i, (v', ty)) end
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    97
        | _ => I) (prop_of thm) [];
21159
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
    98
    fun mk_inst (v_i as (v, i), (v', ty)) (maxidx, acc) =
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
    99
      let
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
   100
        val t = Var (v_i, ty)
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
   101
      in
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
   102
        (maxidx + 1, (cterm_of thy t, cterm_of thy (Var ((v', maxidx), ty))) :: acc)
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
   103
      end;
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   104
    val maxidx = Thm.maxidx_of thm + 1;
21159
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
   105
    val (_, inst) = fold mk_inst (vars_subst_for thm) (maxidx + 1, []);
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   106
  in Thm.instantiate ([], inst) thm end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   107
21120
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   108
fun norm_vars thy thms =
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   109
  let
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   110
    fun burrow_thms f [] = []
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   111
      | burrow_thms f thms =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   112
          thms
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   113
          |> Conjunction.intr_list
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   114
          |> f
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   115
          |> Conjunction.elim_list;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   116
  in
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   117
    thms
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   118
    |> map (abs_norm thy)
21159
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
   119
    |> burrow_thms (canonical_tvars thy)
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
   120
    |> map (canonical_vars thy)
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
   121
    |> map Drule.zero_var_indexes
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   122
  end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   123
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   124
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   125
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   126
(** retrieval **)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   127
21120
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   128
fun funcs funcgr =
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   129
  these o Option.map snd o try (Constgraph.get_node funcgr);
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   130
21120
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   131
fun typ funcgr =
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   132
  fst o Constgraph.get_node funcgr;
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   133
21120
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   134
fun deps funcgr cs =
20835
haftmann
parents: 20705
diff changeset
   135
  let
haftmann
parents: 20705
diff changeset
   136
    val conn = Constgraph.strong_conn funcgr;
haftmann
parents: 20705
diff changeset
   137
    val order = rev conn;
haftmann
parents: 20705
diff changeset
   138
  in
haftmann
parents: 20705
diff changeset
   139
    (map o filter) (member (op =) (Constgraph.all_succs funcgr cs)) order
haftmann
parents: 20705
diff changeset
   140
    |> filter_out null
haftmann
parents: 20705
diff changeset
   141
  end;
haftmann
parents: 20705
diff changeset
   142
21120
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   143
fun all funcgr = Constgraph.keys funcgr;
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   144
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   145
local
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   146
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   147
fun add_things_of thy f (c, thms) =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   148
  (fold o fold_aterms)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   149
     (fn Const c_ty => let
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   150
            val c' = CodegenConsts.norm_of_typ thy c_ty
21120
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   151
          in if is_some c andalso CodegenConsts.eq_const (the c, c') then I
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   152
          else f (c', c_ty) end
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   153
       | _ => I) (maps (op :: o swap o apfst (snd o strip_comb)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   154
            o Logic.dest_equals o Drule.plain_prop_of) thms)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   155
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   156
fun rhs_of thy (c, thms) =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   157
  Consttab.empty
21120
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   158
  |> add_things_of thy (Consttab.update o rpair () o fst) (SOME c, thms)
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   159
  |> Consttab.keys;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   160
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   161
fun insts_of thy funcgr (c, ty) =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   162
  let
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   163
    val tys = Sign.const_typargs thy (c, ty);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   164
    val c' = CodegenConsts.norm thy (c, tys);
20705
da71d46b8b2f fixed some mess
haftmann
parents: 20600
diff changeset
   165
    val ty_decl = CodegenConsts.disc_typ_of_const thy
da71d46b8b2f fixed some mess
haftmann
parents: 20600
diff changeset
   166
      (fst o Constgraph.get_node funcgr o CodegenConsts.norm thy) (c, tys);
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   167
    val tys_decl = Sign.const_typargs thy (c, ty_decl);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   168
    fun constructor tyco xs class =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   169
      (tyco, class) :: maps (maps fst) xs;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   170
    fun variable (TVar (_, sort)) = map (pair []) sort
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   171
      | variable (TFree (_, sort)) = map (pair []) sort;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   172
    fun mk_inst ty (TVar (_, sort)) = cons (ty, sort)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   173
      | mk_inst ty (TFree (_, sort)) = cons (ty, sort)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   174
      | mk_inst (Type (tyco1, tys1)) (Type (tyco2, tys2)) =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   175
          if tyco1 <> tyco2 then error "bad instance"
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   176
          else fold2 mk_inst tys1 tys2;
21120
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   177
    val pp = Sign.pp thy;
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   178
    val algebra = Sign.classes_of thy;
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   179
    fun classrel (x, _) _ = x;
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   180
    fun of_sort_deriv (ty, sort) =
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   181
      Sorts.of_sort_derivation pp algebra
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   182
        { classrel = classrel, constructor = constructor, variable = variable }
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   183
        (ty, sort)
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   184
  in
21120
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   185
    flat (maps of_sort_deriv (fold2 mk_inst tys tys_decl []))
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   186
  end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   187
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   188
fun all_classops thy tyco class =
21120
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   189
  AxClass.params_of thy class
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   190
(*   |> tap (fn _ => writeln ("INST " ^ tyco ^ " - " ^ class))  *)
20705
da71d46b8b2f fixed some mess
haftmann
parents: 20600
diff changeset
   191
  |> AList.make (fn c => CodegenConsts.disc_typ_of_classop thy (c, [Type (tyco, [])]))
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   192
        (*typ_of_classop is very liberal in its type arguments*)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   193
  |> map (CodegenConsts.norm_of_typ thy);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   194
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   195
fun instdefs_of thy insts =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   196
  let
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   197
    val thy_classes = (#classes o Sorts.rep_algebra o Sign.classes_of) thy;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   198
  in
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   199
    Symtab.empty
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   200
    |> fold (fn (tyco, class) =>
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   201
        Symtab.map_default (tyco, []) (insert (op =) class)) insts
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   202
    |> (fn tab => Symtab.fold (fn (tyco, classes) => append (maps (all_classops thy tyco)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   203
         (Graph.all_succs thy_classes classes))) tab [])
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   204
  end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   205
21120
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   206
fun insts_of_thms thy funcgr (c, thms) =
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   207
  let
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   208
    val insts = add_things_of thy (fn (_, c_ty) => fold (insert (op =))
21120
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   209
      (insts_of thy funcgr c_ty)) (SOME c, thms) [];
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   210
  in instdefs_of thy insts end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   211
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   212
fun ensure_const thy funcgr c auxgr =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   213
  if can (Constgraph.get_node funcgr) c
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   214
    then (NONE, auxgr)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   215
  else if can (Constgraph.get_node auxgr) c
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   216
    then (SOME c, auxgr)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   217
  else if is_some (CodegenData.get_datatype_of_constr thy c) then
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   218
    auxgr
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   219
    |> Constgraph.new_node (c, [])
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   220
    |> pair (SOME c)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   221
  else let
21120
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   222
    val thms = norm_vars thy (CodegenData.these_funcs thy c);
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   223
    val rhs = rhs_of thy (c, thms);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   224
  in
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   225
    auxgr
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   226
    |> Constgraph.new_node (c, thms)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   227
    |> fold_map (ensure_const thy funcgr) rhs
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   228
    |-> (fn rhs' => fold (fn SOME c' => Constgraph.add_edge (c, c')
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   229
                           | NONE => I) rhs')
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   230
    |> pair (SOME c)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   231
  end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   232
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   233
fun specialize_typs thy funcgr eqss =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   234
  let
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   235
    fun max k [] = k
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   236
      | max k (l::ls) = max (if k < l then l else k) ls;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   237
    fun typscheme_of (c, ty) =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   238
      try (Constgraph.get_node funcgr) (CodegenConsts.norm_of_typ thy (c, ty))
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   239
      |> Option.map fst;
21120
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   240
    fun incr_indices (c:'a, thms) maxidx =
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   241
      let
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   242
        val thms' = map (Thm.incr_indexes maxidx) thms;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   243
        val maxidx' = Int.max
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   244
          (maxidx, max ~1 (map Thm.maxidx_of thms') + 1);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   245
      in ((c, thms'), maxidx') end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   246
    val tsig = Sign.tsig_of thy;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   247
    fun unify_const thms (c, ty) (env, maxidx) =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   248
      case typscheme_of (c, ty)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   249
       of SOME ty_decl => let
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   250
            val ty_decl' = Logic.incr_tvar maxidx ty_decl;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   251
            val maxidx' = Int.max (Term.maxidx_of_typ ty_decl' + 1, maxidx);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   252
          in Type.unify tsig (ty_decl', ty) (env, maxidx')
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   253
          handle TUNIFY => error ("Failed to instantiate\n"
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   254
            ^ (Sign.string_of_typ thy o Envir.norm_type env) ty_decl' ^ "\nto\n"
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   255
            ^ (Sign.string_of_typ thy o Envir.norm_type env) ty ^ ",\n"
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   256
            ^ "in function theorems\n"
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   257
            ^ cat_lines (map string_of_thm thms))
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   258
          end
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   259
        | NONE => (env, maxidx);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   260
    fun apply_unifier unif (c, []) = (c, [])
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   261
      | apply_unifier unif (c, thms as thm :: _) =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   262
          let
21120
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   263
            val tvars = Term.add_tvars (Thm.prop_of thm) [];
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   264
            fun mk_inst (v_i_sort as (v, _)) =
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   265
              let
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   266
                val ty = TVar v_i_sort;
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   267
              in
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   268
                pairself (Thm.ctyp_of thy) (ty,
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   269
                  TVar (v, (snd o dest_TVar o Envir.norm_type unif) ty))
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   270
              end;
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   271
            val instmap = map mk_inst tvars;
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   272
            val (thms' as thm' :: _) = map (Drule.zero_var_indexes o Thm.instantiate (instmap, [])) thms
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   273
            val _ = if fst c <> "" andalso not (Sign.typ_equiv thy (Type.strip_sorts (CodegenData.typ_func thy thm), Type.strip_sorts (CodegenData.typ_func thy thm')))
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   274
              then error ("illegal function type instantiation:\n" ^ Sign.string_of_typ thy (CodegenData.typ_func thy thm)
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   275
                ^ "\nto" ^ Sign.string_of_typ thy (CodegenData.typ_func thy thm'))
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   276
              else ();
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   277
          in (c, thms') end;
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   278
    fun rhs_of' thy (("", []), thms as [_]) =
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   279
          add_things_of thy (cons o snd) (NONE, thms) []
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   280
      | rhs_of' thy (c, thms) =
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   281
          add_things_of thy (cons o snd) (SOME c, thms) [];
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   282
    val (eqss', maxidx) =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   283
      fold_map incr_indices eqss 0;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   284
    val (unif, _) =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   285
      fold (fn (c, thms) => fold (unify_const thms) (rhs_of' thy (c, thms)))
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   286
        eqss' (Vartab.empty, maxidx);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   287
    val eqss'' =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   288
      map (apply_unifier unif) eqss';
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   289
  in eqss'' end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   290
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   291
fun merge_eqsyss thy raw_eqss funcgr =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   292
  let
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   293
    val eqss = specialize_typs thy funcgr raw_eqss;
20938
041badc7fcaf added keeping of funcgr
haftmann
parents: 20896
diff changeset
   294
    val tys = map (CodegenData.typ_funcs thy) eqss;
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   295
    val rhss = map (rhs_of thy) eqss;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   296
  in
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   297
    funcgr
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   298
    |> fold2 (fn (c, thms) => fn ty => Constgraph.new_node (c, (ty, thms))) eqss tys
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   299
    |> `(fn funcgr => map (insts_of_thms thy funcgr) eqss)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   300
    |-> (fn rhs_insts => fold2 (fn (c, _) => fn rhs_inst =>
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   301
          ensure_consts thy rhs_inst #> fold (curry Constgraph.add_edge c) rhs_inst) eqss rhs_insts)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   302
    |> fold2 (fn (c, _) => fn rhs => fold (curry Constgraph.add_edge c) rhs) eqss rhss
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   303
  end
20705
da71d46b8b2f fixed some mess
haftmann
parents: 20600
diff changeset
   304
and merge_new_eqsyss thy raw_eqss funcgr =
da71d46b8b2f fixed some mess
haftmann
parents: 20600
diff changeset
   305
  if exists (member CodegenConsts.eq_const (Constgraph.keys funcgr)) (map fst raw_eqss)
da71d46b8b2f fixed some mess
haftmann
parents: 20600
diff changeset
   306
  then funcgr
da71d46b8b2f fixed some mess
haftmann
parents: 20600
diff changeset
   307
  else merge_eqsyss thy raw_eqss funcgr
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   308
and ensure_consts thy cs funcgr =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   309
  fold (snd oo ensure_const thy funcgr) cs Constgraph.empty
20705
da71d46b8b2f fixed some mess
haftmann
parents: 20600
diff changeset
   310
  |> (fn auxgr => fold (merge_new_eqsyss thy)
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   311
       (map (AList.make (Constgraph.get_node auxgr))
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   312
       (rev (Constgraph.strong_conn auxgr))) funcgr);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   313
21120
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   314
fun drop_classes thy tfrees thm =
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   315
  let
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   316
(*     val _ = writeln ("DROP1 " ^ setmp show_types true string_of_thm thm);  *)
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   317
    val (_, thm') = Thm.varifyT' [] thm;
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   318
    val tvars = Term.add_tvars (Thm.prop_of thm') [];
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   319
(*     val _ = writeln ("DROP2 " ^ setmp show_types true string_of_thm thm');  *)
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   320
    val unconstr = map (Thm.ctyp_of thy o TVar) tvars;
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   321
    val instmap = map2 (fn (v_i, _) => fn (v, sort) => pairself (Thm.ctyp_of thy)
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   322
      (TVar (v_i, []), TFree (v, sort))) tvars tfrees;
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   323
  in
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   324
    thm'
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   325
    |> fold Thm.unconstrainT unconstr
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   326
    |> Thm.instantiate (instmap, [])
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   327
    |> Tactic.rule_by_tactic ((REPEAT o CHANGED o ALLGOALS o Tactic.resolve_tac) (AxClass.class_intros thy))
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   328
(*     |> tap (fn thm => writeln ("DROP3 " ^ setmp show_types true string_of_thm thm))  *)
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   329
  end;
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   330
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   331
in
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   332
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   333
val ensure_consts = ensure_consts;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   334
21120
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   335
fun make thy consts =
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   336
  Funcgr.change thy (ensure_consts thy consts);
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   337
21129
8e621232a865 clarified make_term interface
haftmann
parents: 21120
diff changeset
   338
fun make_term thy f ct =
21120
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   339
  let
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   340
    val _ = Sign.no_vars (Sign.pp thy) (Thm.term_of ct);
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   341
    val _ = Term.fold_types (Type.no_tvars #> K I) (Thm.term_of ct) ();
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   342
    val thm1 = CodegenData.preprocess_cterm thy ct;
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   343
(*     val _ = writeln ("THM1 " ^ setmp show_types true string_of_thm thm1);  *)
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   344
    val ct' = Drule.dest_equals_rhs (Thm.cprop_of thm1);
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   345
    val consts = CodegenConsts.consts_of thy (Thm.term_of ct');
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   346
    val funcgr = make thy consts;
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   347
    val (_, thm2) = Thm.varifyT' [] thm1;
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   348
(*     val _ = writeln ("THM2 " ^ setmp show_types true string_of_thm thm2);  *)
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   349
    val thm3 = Thm.reflexive (Drule.dest_equals_rhs (Thm.cprop_of thm2));
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   350
(*     val _ = writeln ("THM3 " ^ setmp show_types true string_of_thm thm3);  *)
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   351
    val [(_, [thm4])] = specialize_typs thy funcgr [(("", []), [thm3])];
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   352
(*     val _ = writeln ("THM4 " ^ setmp show_types true string_of_thm thm4);  *)
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   353
    val tfrees = Term.add_tfrees (Thm.prop_of thm1) [];
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   354
(*     val _ = writeln "TFREES";  *)
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   355
(*     val _ = (writeln o cat_lines o map (fn (v, sort) => v ^ "::" ^ Sign.string_of_sort thy sort)) tfrees;  *)
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   356
    fun inst thm =
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   357
      let
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   358
        val tvars = Term.add_tvars (Thm.prop_of thm) [];
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   359
(*         val _ = writeln "TVARS";  *)
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   360
(*         val _ = (writeln o cat_lines o map (fn ((v, i), sort) => v ^ "_" ^ string_of_int i ^ "::" ^ Sign.string_of_sort thy sort)) tvars;  *)
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   361
        val instmap = map2 (fn (v_i, sort) => fn (v, _) => pairself (Thm.ctyp_of thy)
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   362
          (TVar (v_i, sort), TFree (v, sort))) tvars tfrees;
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   363
      in Thm.instantiate (instmap, []) thm end;
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   364
    val thm5 = inst thm2;
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   365
    val thm6 = inst thm4;
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   366
(*     val _ = writeln ("THM5 " ^ setmp show_types true string_of_thm thm5);  *)
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   367
(*     val _ = writeln ("THM6 " ^ setmp show_types true string_of_thm thm6);  *)
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   368
    val ct'' = Drule.dest_equals_rhs (Thm.cprop_of thm6);
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   369
    val cs = fold_aterms (fn Const c => cons c | _ => I) (Thm.term_of ct'') [];
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   370
    val drop = drop_classes thy tfrees;
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   371
(*     val _ = writeln "ADD INST";  *)
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   372
    val funcgr' = ensure_consts thy
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   373
      (instdefs_of thy (fold (fold (insert (op =)) o insts_of thy funcgr) cs [])) funcgr
21129
8e621232a865 clarified make_term interface
haftmann
parents: 21120
diff changeset
   374
  in (f drop ct'' thm5, Funcgr.change thy (K funcgr')) end;
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   375
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   376
end; (*local*)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   377
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   378
fun print_funcgr thy funcgr =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   379
  AList.make (snd o Constgraph.get_node funcgr) (Constgraph.keys funcgr)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   380
  |> (map o apfst) (CodegenConsts.string_of_const thy)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   381
  |> sort (string_ord o pairself fst)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   382
  |> map (fn (s, thms) =>
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   383
       (Pretty.block o Pretty.fbreaks) (
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   384
         Pretty.str s
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   385
         :: map Display.pretty_thm thms
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   386
       ))
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   387
  |> Pretty.chunks
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   388
  |> Pretty.writeln;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   389
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   390
fun print_codethms thy consts =
21120
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   391
  make thy consts |> print_funcgr thy;
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   392
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   393
fun print_codethms_e thy cs =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   394
  print_codethms thy (map (CodegenConsts.read_const thy) cs);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   395
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   396
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   397
(** Isar **)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   398
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   399
structure P = OuterParse;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   400
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   401
val print_codethmsK = "print_codethms";
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   402
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   403
val print_codethmsP =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   404
  OuterSyntax.improper_command print_codethmsK "print code theorems of this theory" OuterKeyword.diag
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   405
    (Scan.option (P.$$$ "(" |-- Scan.repeat P.term --| P.$$$ ")")
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   406
      >> (fn NONE => CodegenData.print_thms
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   407
           | SOME cs => fn thy => print_codethms_e thy cs)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   408
      >> (fn f => Toplevel.no_timing o Toplevel.unknown_theory
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   409
      o Toplevel.keep (f o Toplevel.theory_of)));
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   410
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   411
val _ = OuterSyntax.add_parsers [print_codethmsP];
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   412
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   413
end; (*struct*)