src/Pure/Tools/codegen_funcgr.ML
author haftmann
Thu, 25 Jan 2007 09:32:51 +0100
changeset 22185 24bf0e403526
parent 22039 9bc8058250a7
child 22198 226d29db8e0a
permissions -rw-r--r--
tuned
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
22185
haftmann
parents: 22039
diff changeset
     5
Retrieving, normalizing and structuring defining equations
20855
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
22185
haftmann
parents: 22039
diff changeset
    13
  val make_consts: theory -> CodegenConsts.const list -> CodegenConsts.const list * T
21129
8e621232a865 clarified make_term interface
haftmann
parents: 21120
diff changeset
    14
  val make_term: theory -> ((thm -> thm) -> cterm -> thm -> 'a) -> cterm -> 'a * T
21120
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
    15
  val funcs: T -> CodegenConsts.const -> thm list
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
    16
  val typ: T -> CodegenConsts.const -> typ
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
    17
  val deps: T -> CodegenConsts.const list -> CodegenConsts.const list list
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
    18
  val all: T -> CodegenConsts.const list
22185
haftmann
parents: 22039
diff changeset
    19
  val norm_varnames: thm list -> thm list
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    20
  val print_codethms: theory -> CodegenConsts.const list -> unit
20938
041badc7fcaf added keeping of funcgr
haftmann
parents: 20896
diff changeset
    21
  structure Constgraph : GRAPH
22185
haftmann
parents: 22039
diff changeset
    22
  val timing: bool ref
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    23
end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    24
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    25
structure CodegenFuncgr: CODEGEN_FUNCGR =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    26
struct
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    27
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    28
(** code data **)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    29
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    30
structure Consttab = CodegenConsts.Consttab;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    31
structure Constgraph = GraphFun (
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    32
  type key = CodegenConsts.const;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    33
  val ord = CodegenConsts.const_ord;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    34
);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    35
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    36
type T = (typ * thm list) Constgraph.T;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    37
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    38
structure Funcgr = CodeDataFun
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    39
(struct
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    40
  val name = "Pure/codegen_funcgr";
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    41
  type T = T;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    42
  val empty = Constgraph.empty;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    43
  fun merge _ _ = Constgraph.empty;
20938
041badc7fcaf added keeping of funcgr
haftmann
parents: 20896
diff changeset
    44
  fun purge _ NONE _ = Constgraph.empty
041badc7fcaf added keeping of funcgr
haftmann
parents: 20896
diff changeset
    45
    | purge _ (SOME cs) funcgr =
21463
42dd50268c8b completed class parameter handling in axclass.ML
haftmann
parents: 21387
diff changeset
    46
        Constgraph.del_nodes ((Constgraph.all_preds funcgr 
20938
041badc7fcaf added keeping of funcgr
haftmann
parents: 20896
diff changeset
    47
          o filter (can (Constgraph.get_node funcgr))) cs) funcgr;
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    48
end);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    49
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    50
val _ = Context.add_setup Funcgr.init;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    51
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    52
22185
haftmann
parents: 22039
diff changeset
    53
(** retrieval **)
haftmann
parents: 22039
diff changeset
    54
haftmann
parents: 22039
diff changeset
    55
fun funcs funcgr =
haftmann
parents: 22039
diff changeset
    56
  these o Option.map snd o try (Constgraph.get_node funcgr);
haftmann
parents: 22039
diff changeset
    57
haftmann
parents: 22039
diff changeset
    58
fun typ funcgr =
haftmann
parents: 22039
diff changeset
    59
  fst o Constgraph.get_node funcgr;
haftmann
parents: 22039
diff changeset
    60
haftmann
parents: 22039
diff changeset
    61
fun deps funcgr cs =
haftmann
parents: 22039
diff changeset
    62
  let
haftmann
parents: 22039
diff changeset
    63
    val conn = Constgraph.strong_conn funcgr;
haftmann
parents: 22039
diff changeset
    64
    val order = rev conn;
haftmann
parents: 22039
diff changeset
    65
  in
haftmann
parents: 22039
diff changeset
    66
    (map o filter) (member (op =) (Constgraph.all_succs funcgr cs)) order
haftmann
parents: 22039
diff changeset
    67
    |> filter_out null
haftmann
parents: 22039
diff changeset
    68
  end;
haftmann
parents: 22039
diff changeset
    69
haftmann
parents: 22039
diff changeset
    70
fun all funcgr = Constgraph.keys funcgr;
haftmann
parents: 22039
diff changeset
    71
haftmann
parents: 22039
diff changeset
    72
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    73
(** theorem purification **)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    74
22039
9bc8058250a7 slight cleanups
haftmann
parents: 22021
diff changeset
    75
fun norm_args thms =
21989
0315ecfd3d5d eta-expansion now only to common maximum number of arguments
haftmann
parents: 21915
diff changeset
    76
  let
0315ecfd3d5d eta-expansion now only to common maximum number of arguments
haftmann
parents: 21915
diff changeset
    77
    val num_args_of = length o snd o strip_comb o fst o Logic.dest_equals;
0315ecfd3d5d eta-expansion now only to common maximum number of arguments
haftmann
parents: 21915
diff changeset
    78
    val k = fold (curry Int.max o num_args_of o Drule.plain_prop_of) thms 0;
0315ecfd3d5d eta-expansion now only to common maximum number of arguments
haftmann
parents: 21915
diff changeset
    79
  in
0315ecfd3d5d eta-expansion now only to common maximum number of arguments
haftmann
parents: 21915
diff changeset
    80
    thms
22039
9bc8058250a7 slight cleanups
haftmann
parents: 22021
diff changeset
    81
    |> map (CodegenFunc.expand_eta k)
21989
0315ecfd3d5d eta-expansion now only to common maximum number of arguments
haftmann
parents: 21915
diff changeset
    82
    |> map (Drule.fconv_rule Drule.beta_eta_conversion)
0315ecfd3d5d eta-expansion now only to common maximum number of arguments
haftmann
parents: 21915
diff changeset
    83
  end;
21120
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
    84
22039
9bc8058250a7 slight cleanups
haftmann
parents: 22021
diff changeset
    85
fun canonical_tvars thm =
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    86
  let
22039
9bc8058250a7 slight cleanups
haftmann
parents: 22021
diff changeset
    87
    val ctyp = Thm.ctyp_of (Thm.theory_of_thm thm);
21159
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
    88
    fun tvars_subst_for thm = (fold_types o fold_atyps)
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
    89
      (fn TVar (v_i as (v, _), sort) => let
21915
4e63c55f4cb4 different handling of type variable names
haftmann
parents: 21463
diff changeset
    90
            val v' = CodegenNames.purify_tvar v
21159
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
    91
          in if v = v' then I
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
    92
          else insert (op =) (v_i, (v', sort)) end
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
    93
        | _ => I) (prop_of thm) [];
21159
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
    94
    fun mk_inst (v_i, (v', sort)) (maxidx, acc) =
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
    95
      let
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
    96
        val ty = TVar (v_i, sort)
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
    97
      in
22039
9bc8058250a7 slight cleanups
haftmann
parents: 22021
diff changeset
    98
        (maxidx + 1, (ctyp ty, ctyp (TVar ((v', maxidx), sort))) :: acc)
21159
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
    99
      end;
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   100
    val maxidx = Thm.maxidx_of thm + 1;
21159
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
   101
    val (_, inst) = fold mk_inst (tvars_subst_for thm) (maxidx + 1, []);
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   102
  in Thm.instantiate (inst, []) thm end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   103
22039
9bc8058250a7 slight cleanups
haftmann
parents: 22021
diff changeset
   104
fun canonical_vars thm =
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   105
  let
22039
9bc8058250a7 slight cleanups
haftmann
parents: 22021
diff changeset
   106
    val cterm = Thm.cterm_of (Thm.theory_of_thm thm);
21159
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
   107
    fun vars_subst_for thm = fold_aterms
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
   108
      (fn Var (v_i as (v, _), ty) => let
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
   109
            val v' = CodegenNames.purify_var v
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
   110
          in if v = v' then I
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
   111
          else insert (op =) (v_i, (v', ty)) end
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   112
        | _ => I) (prop_of thm) [];
21159
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
   113
    fun mk_inst (v_i as (v, i), (v', ty)) (maxidx, acc) =
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
   114
      let
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
   115
        val t = Var (v_i, ty)
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
   116
      in
22039
9bc8058250a7 slight cleanups
haftmann
parents: 22021
diff changeset
   117
        (maxidx + 1, (cterm t, cterm (Var ((v', maxidx), ty))) :: acc)
21159
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
   118
      end;
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   119
    val maxidx = Thm.maxidx_of thm + 1;
21159
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
   120
    val (_, inst) = fold mk_inst (vars_subst_for thm) (maxidx + 1, []);
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   121
  in Thm.instantiate ([], inst) thm end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   122
22039
9bc8058250a7 slight cleanups
haftmann
parents: 22021
diff changeset
   123
fun canonical_absvars thm =
9bc8058250a7 slight cleanups
haftmann
parents: 22021
diff changeset
   124
  let
9bc8058250a7 slight cleanups
haftmann
parents: 22021
diff changeset
   125
    val t = Thm.prop_of thm;
9bc8058250a7 slight cleanups
haftmann
parents: 22021
diff changeset
   126
    val t' = Term.map_abs_vars CodegenNames.purify_var t;
9bc8058250a7 slight cleanups
haftmann
parents: 22021
diff changeset
   127
  in Thm.rename_boundvars t t' thm end;
9bc8058250a7 slight cleanups
haftmann
parents: 22021
diff changeset
   128
22185
haftmann
parents: 22039
diff changeset
   129
fun norm_varnames thms =
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   130
  let
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   131
    fun burrow_thms f [] = []
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   132
      | burrow_thms f thms =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   133
          thms
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   134
          |> Conjunction.intr_list
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   135
          |> f
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   136
          |> Conjunction.elim_list;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   137
  in
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   138
    thms
22039
9bc8058250a7 slight cleanups
haftmann
parents: 22021
diff changeset
   139
    |> norm_args
9bc8058250a7 slight cleanups
haftmann
parents: 22021
diff changeset
   140
    |> burrow_thms canonical_tvars
9bc8058250a7 slight cleanups
haftmann
parents: 22021
diff changeset
   141
    |> map canonical_vars
9bc8058250a7 slight cleanups
haftmann
parents: 22021
diff changeset
   142
    |> map canonical_absvars
21159
7f6bdffe3d06 fixed problem with variable names
haftmann
parents: 21129
diff changeset
   143
    |> map Drule.zero_var_indexes
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   144
  end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   145
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   146
22185
haftmann
parents: 22039
diff changeset
   147
(** generic combinators **)
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   148
22185
haftmann
parents: 22039
diff changeset
   149
fun fold_consts f thms =
haftmann
parents: 22039
diff changeset
   150
  thms
haftmann
parents: 22039
diff changeset
   151
  |> maps (op :: o swap o apfst (snd o strip_comb) o Logic.dest_equals o Drule.plain_prop_of)
haftmann
parents: 22039
diff changeset
   152
  |> (fold o fold_aterms) (fn Const c => f c | _ => I);
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   153
22185
haftmann
parents: 22039
diff changeset
   154
fun consts_of (const, []) = []
haftmann
parents: 22039
diff changeset
   155
  | consts_of (const, thms as thm :: _) = 
haftmann
parents: 22039
diff changeset
   156
      let
haftmann
parents: 22039
diff changeset
   157
        val thy = Thm.theory_of_thm thm;
haftmann
parents: 22039
diff changeset
   158
        val is_refl = curry CodegenConsts.eq_const const;
haftmann
parents: 22039
diff changeset
   159
        fun the_const c = case try (CodegenConsts.norm_of_typ thy) c
haftmann
parents: 22039
diff changeset
   160
         of SOME const => if is_refl const then I else insert CodegenConsts.eq_const const
haftmann
parents: 22039
diff changeset
   161
          | NONE => I
haftmann
parents: 22039
diff changeset
   162
      in fold_consts the_const thms [] end;
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   163
22185
haftmann
parents: 22039
diff changeset
   164
fun insts_of thy algebra c ty_decl ty =
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   165
  let
22185
haftmann
parents: 22039
diff changeset
   166
    val tys_decl = Sign.const_typargs thy (c, ty_decl);
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   167
    val tys = Sign.const_typargs thy (c, ty);
22185
haftmann
parents: 22039
diff changeset
   168
    fun classrel (x, _) _ = x;
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   169
    fun constructor tyco xs class =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   170
      (tyco, class) :: maps (maps fst) xs;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   171
    fun variable (TVar (_, sort)) = map (pair []) sort
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   172
      | variable (TFree (_, sort)) = map (pair []) sort;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   173
    fun mk_inst ty (TVar (_, sort)) = cons (ty, sort)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   174
      | mk_inst ty (TFree (_, sort)) = cons (ty, sort)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   175
      | mk_inst (Type (tyco1, tys1)) (Type (tyco2, tys2)) =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   176
          if tyco1 <> tyco2 then error "bad instance"
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   177
          else fold2 mk_inst tys1 tys2;
21120
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   178
    fun of_sort_deriv (ty, sort) =
22185
haftmann
parents: 22039
diff changeset
   179
      Sorts.of_sort_derivation (Sign.pp thy) algebra
21120
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   180
        { classrel = classrel, constructor = constructor, variable = variable }
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   181
        (ty, sort)
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   182
  in
21120
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   183
    flat (maps of_sort_deriv (fold2 mk_inst tys tys_decl []))
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   184
  end;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   185
22039
9bc8058250a7 slight cleanups
haftmann
parents: 22021
diff changeset
   186
22185
haftmann
parents: 22039
diff changeset
   187
(** graph algorithm **)
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   188
22185
haftmann
parents: 22039
diff changeset
   189
val timing = ref false;
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   190
22185
haftmann
parents: 22039
diff changeset
   191
local
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   192
22185
haftmann
parents: 22039
diff changeset
   193
exception INVALID of CodegenConsts.const list * string;
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   194
22185
haftmann
parents: 22039
diff changeset
   195
fun specialize_typs' thy funcgr funcss =
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   196
  let
22039
9bc8058250a7 slight cleanups
haftmann
parents: 22021
diff changeset
   197
    fun max xs = fold (curry Int.max) xs 0;
21387
5d3d340cb783 clarified code for building function equation system; explicit check of type discipline
haftmann
parents: 21196
diff changeset
   198
    fun incr_indices (c, thms) maxidx =
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   199
      let
21387
5d3d340cb783 clarified code for building function equation system; explicit check of type discipline
haftmann
parents: 21196
diff changeset
   200
        val thms' = map (Thm.incr_indexes (maxidx + 1)) thms;
5d3d340cb783 clarified code for building function equation system; explicit check of type discipline
haftmann
parents: 21196
diff changeset
   201
        val maxidx' = max (maxidx :: map Thm.maxidx_of thms');
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   202
      in ((c, thms'), maxidx') end;
22185
haftmann
parents: 22039
diff changeset
   203
    val (funcss', maxidx) =
haftmann
parents: 22039
diff changeset
   204
      fold_map incr_indices funcss 0;
haftmann
parents: 22039
diff changeset
   205
    fun typ_of_const (c, ty) = case try (CodegenConsts.norm_of_typ thy) (c, ty)
haftmann
parents: 22039
diff changeset
   206
     of SOME const => Option.map fst (try (Constgraph.get_node funcgr) const)
haftmann
parents: 22039
diff changeset
   207
      | NONE => NONE;
21387
5d3d340cb783 clarified code for building function equation system; explicit check of type discipline
haftmann
parents: 21196
diff changeset
   208
    fun unify_const (c, ty) (env, maxidx) =
22185
haftmann
parents: 22039
diff changeset
   209
      case typ_of_const (c, ty)
haftmann
parents: 22039
diff changeset
   210
       of SOME ty_decl => let
21387
5d3d340cb783 clarified code for building function equation system; explicit check of type discipline
haftmann
parents: 21196
diff changeset
   211
            val ty_decl' = Logic.incr_tvar (maxidx + 1) ty_decl;
5d3d340cb783 clarified code for building function equation system; explicit check of type discipline
haftmann
parents: 21196
diff changeset
   212
            val maxidx' = max [maxidx, Term.maxidx_of_typ ty_decl'];
5d3d340cb783 clarified code for building function equation system; explicit check of type discipline
haftmann
parents: 21196
diff changeset
   213
          in Type.unify (Sign.tsig_of thy) (ty_decl', ty) (env, maxidx')
5d3d340cb783 clarified code for building function equation system; explicit check of type discipline
haftmann
parents: 21196
diff changeset
   214
          handle TUNIFY => raise INVALID ([], setmp show_sorts true (setmp show_types true (fn f => f ())) (fn _ => ("Failed to instantiate\n"
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   215
            ^ (Sign.string_of_typ thy o Envir.norm_type env) ty_decl' ^ "\nto\n"
21387
5d3d340cb783 clarified code for building function equation system; explicit check of type discipline
haftmann
parents: 21196
diff changeset
   216
            ^ (Sign.string_of_typ thy o Envir.norm_type env) ty
5d3d340cb783 clarified code for building function equation system; explicit check of type discipline
haftmann
parents: 21196
diff changeset
   217
            ^ ",\nfor constant " ^ quote c
5d3d340cb783 clarified code for building function equation system; explicit check of type discipline
haftmann
parents: 21196
diff changeset
   218
            ^ "\nin function theorems\n"
22185
haftmann
parents: 22039
diff changeset
   219
            ^ (cat_lines o maps (map (Sign.string_of_term thy o map_types (Envir.norm_type env) o Thm.prop_of) o snd)) funcss')))
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   220
          end
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   221
        | NONE => (env, maxidx);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   222
    fun apply_unifier unif (c, []) = (c, [])
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   223
      | apply_unifier unif (c, thms as thm :: _) =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   224
          let
21120
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   225
            val tvars = Term.add_tvars (Thm.prop_of thm) [];
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   226
            fun mk_inst (v_i_sort as (v, _)) =
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   227
              let
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   228
                val ty = TVar v_i_sort;
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   229
              in
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   230
                pairself (Thm.ctyp_of thy) (ty,
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   231
                  TVar (v, (snd o dest_TVar o Envir.norm_type unif) ty))
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   232
              end;
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   233
            val instmap = map mk_inst tvars;
21387
5d3d340cb783 clarified code for building function equation system; explicit check of type discipline
haftmann
parents: 21196
diff changeset
   234
            val (thms' as thm' :: _) = map (Drule.zero_var_indexes o Thm.instantiate (instmap, [])) thms;
22039
9bc8058250a7 slight cleanups
haftmann
parents: 22021
diff changeset
   235
            val _ = case c of NONE => ()
9bc8058250a7 slight cleanups
haftmann
parents: 22021
diff changeset
   236
              | SOME c => (case pairself CodegenFunc.typ_func (thm, thm')
21989
0315ecfd3d5d eta-expansion now only to common maximum number of arguments
haftmann
parents: 21915
diff changeset
   237
               of (ty, ty') => if (is_none o AxClass.class_of_param thy o fst) c
0315ecfd3d5d eta-expansion now only to common maximum number of arguments
haftmann
parents: 21915
diff changeset
   238
                  andalso Sign.typ_equiv thy (Type.strip_sorts ty, Type.strip_sorts ty')
0315ecfd3d5d eta-expansion now only to common maximum number of arguments
haftmann
parents: 21915
diff changeset
   239
                  orelse Sign.typ_equiv thy (ty, ty')
0315ecfd3d5d eta-expansion now only to common maximum number of arguments
haftmann
parents: 21915
diff changeset
   240
                  then ()
22039
9bc8058250a7 slight cleanups
haftmann
parents: 22021
diff changeset
   241
                  else raise INVALID ([], "illegal function type instantiation:\n" ^ Sign.string_of_typ thy (CodegenFunc.typ_func thm)
9bc8058250a7 slight cleanups
haftmann
parents: 22021
diff changeset
   242
                    ^ "\nto " ^ Sign.string_of_typ thy (CodegenFunc.typ_func thm')
21989
0315ecfd3d5d eta-expansion now only to common maximum number of arguments
haftmann
parents: 21915
diff changeset
   243
                    ^ ",\nfor constant " ^ CodegenConsts.string_of_const thy c
0315ecfd3d5d eta-expansion now only to common maximum number of arguments
haftmann
parents: 21915
diff changeset
   244
                    ^ "\nin function theorems\n"
22039
9bc8058250a7 slight cleanups
haftmann
parents: 22021
diff changeset
   245
                    ^ (cat_lines o map string_of_thm) thms))
21120
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   246
          in (c, thms') end;
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   247
    val (unif, _) =
22185
haftmann
parents: 22039
diff changeset
   248
      fold (fn (_, thms) => fold unify_const (fold_consts (insert (op =)) thms []))
haftmann
parents: 22039
diff changeset
   249
        funcss' (Vartab.empty, maxidx);
haftmann
parents: 22039
diff changeset
   250
    val funcss'' = map (apply_unifier unif) funcss';
haftmann
parents: 22039
diff changeset
   251
  in funcss'' end;
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   252
22039
9bc8058250a7 slight cleanups
haftmann
parents: 22021
diff changeset
   253
fun specialize_typs thy funcgr =
9bc8058250a7 slight cleanups
haftmann
parents: 22021
diff changeset
   254
  (map o apfst) SOME
9bc8058250a7 slight cleanups
haftmann
parents: 22021
diff changeset
   255
  #> specialize_typs' thy funcgr
9bc8058250a7 slight cleanups
haftmann
parents: 22021
diff changeset
   256
  #> (map o apfst) the;
9bc8058250a7 slight cleanups
haftmann
parents: 22021
diff changeset
   257
22185
haftmann
parents: 22039
diff changeset
   258
fun classop_const thy algebra class classop tyco =
haftmann
parents: 22039
diff changeset
   259
  let
haftmann
parents: 22039
diff changeset
   260
    val sorts = Sorts.mg_domain algebra tyco [class]
haftmann
parents: 22039
diff changeset
   261
    val (var, _) = try (AxClass.params_of_class thy) class |> the_default ("'a", []);
haftmann
parents: 22039
diff changeset
   262
    val vs = Name.names (Name.declare var Name.context) "'a" sorts;
haftmann
parents: 22039
diff changeset
   263
    val arity_typ = Type (tyco, map TFree vs);
haftmann
parents: 22039
diff changeset
   264
  in (classop, [arity_typ]) end;
haftmann
parents: 22039
diff changeset
   265
haftmann
parents: 22039
diff changeset
   266
fun instances_of thy algebra insts =
haftmann
parents: 22039
diff changeset
   267
  let
haftmann
parents: 22039
diff changeset
   268
    val thy_classes = (#classes o Sorts.rep_algebra o Sign.classes_of) thy;
haftmann
parents: 22039
diff changeset
   269
    fun all_classops tyco class =
haftmann
parents: 22039
diff changeset
   270
      try (AxClass.params_of_class thy) class
haftmann
parents: 22039
diff changeset
   271
      |> Option.map snd
haftmann
parents: 22039
diff changeset
   272
      |> these
haftmann
parents: 22039
diff changeset
   273
      |> map (fn (c, _) => classop_const thy algebra class c tyco)
haftmann
parents: 22039
diff changeset
   274
      |> map (CodegenConsts.norm thy)
haftmann
parents: 22039
diff changeset
   275
  in
haftmann
parents: 22039
diff changeset
   276
    Symtab.empty
haftmann
parents: 22039
diff changeset
   277
    |> fold (fn (tyco, class) =>
haftmann
parents: 22039
diff changeset
   278
        Symtab.map_default (tyco, []) (insert (op =) class)) insts
haftmann
parents: 22039
diff changeset
   279
    |> (fn tab => Symtab.fold (fn (tyco, classes) => append (maps (all_classops tyco)
haftmann
parents: 22039
diff changeset
   280
         (Graph.all_succs thy_classes classes))) tab [])
haftmann
parents: 22039
diff changeset
   281
  end;
haftmann
parents: 22039
diff changeset
   282
haftmann
parents: 22039
diff changeset
   283
fun instances_of_consts thy algebra funcgr consts =
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   284
  let
22185
haftmann
parents: 22039
diff changeset
   285
    fun inst (const as (c, ty)) = case try (CodegenConsts.norm_of_typ thy) const
haftmann
parents: 22039
diff changeset
   286
     of SOME const => insts_of thy algebra c (fst (Constgraph.get_node funcgr const)) ty
haftmann
parents: 22039
diff changeset
   287
      | NONE => [];
haftmann
parents: 22039
diff changeset
   288
  in
haftmann
parents: 22039
diff changeset
   289
    []
haftmann
parents: 22039
diff changeset
   290
    |> fold (fold (insert (op =)) o inst) consts
haftmann
parents: 22039
diff changeset
   291
    |> instances_of thy algebra
haftmann
parents: 22039
diff changeset
   292
  end;
haftmann
parents: 22039
diff changeset
   293
haftmann
parents: 22039
diff changeset
   294
fun ensure_const' thy algebra funcgr const auxgr =
haftmann
parents: 22039
diff changeset
   295
  if can (Constgraph.get_node funcgr) const
haftmann
parents: 22039
diff changeset
   296
    then (NONE, auxgr)
haftmann
parents: 22039
diff changeset
   297
  else if can (Constgraph.get_node auxgr) const
haftmann
parents: 22039
diff changeset
   298
    then (SOME const, auxgr)
haftmann
parents: 22039
diff changeset
   299
  else if is_some (CodegenData.get_datatype_of_constr thy const) then
haftmann
parents: 22039
diff changeset
   300
    auxgr
haftmann
parents: 22039
diff changeset
   301
    |> Constgraph.new_node (const, [])
haftmann
parents: 22039
diff changeset
   302
    |> pair (SOME const)
haftmann
parents: 22039
diff changeset
   303
  else let
haftmann
parents: 22039
diff changeset
   304
    val thms = norm_varnames (CodegenData.these_funcs thy const);
haftmann
parents: 22039
diff changeset
   305
    val rhs = consts_of (const, thms);
haftmann
parents: 22039
diff changeset
   306
  in
haftmann
parents: 22039
diff changeset
   307
    auxgr
haftmann
parents: 22039
diff changeset
   308
    |> Constgraph.new_node (const, thms)
haftmann
parents: 22039
diff changeset
   309
    |> fold_map (ensure_const thy algebra funcgr) rhs
haftmann
parents: 22039
diff changeset
   310
    |-> (fn rhs' => fold (fn SOME const' => Constgraph.add_edge (const, const')
haftmann
parents: 22039
diff changeset
   311
                           | NONE => I) rhs')
haftmann
parents: 22039
diff changeset
   312
    |> pair (SOME const)
haftmann
parents: 22039
diff changeset
   313
  end
haftmann
parents: 22039
diff changeset
   314
and ensure_const thy algebra funcgr const =
haftmann
parents: 22039
diff changeset
   315
  let
haftmann
parents: 22039
diff changeset
   316
    val timeap = if !timing
haftmann
parents: 22039
diff changeset
   317
      then Output.timeap_msg ("time for " ^ CodegenConsts.string_of_const thy const)
haftmann
parents: 22039
diff changeset
   318
      else I;
haftmann
parents: 22039
diff changeset
   319
  in timeap (ensure_const' thy algebra funcgr const) end;
haftmann
parents: 22039
diff changeset
   320
haftmann
parents: 22039
diff changeset
   321
fun merge_funcss thy algebra raw_funcss funcgr =
haftmann
parents: 22039
diff changeset
   322
  let
haftmann
parents: 22039
diff changeset
   323
    val funcss = specialize_typs thy funcgr raw_funcss;
haftmann
parents: 22039
diff changeset
   324
    fun default_typ (const as (c, tys)) = case CodegenData.tap_typ thy const
haftmann
parents: 22039
diff changeset
   325
     of SOME ty => ty
haftmann
parents: 22039
diff changeset
   326
      | NONE => let
haftmann
parents: 22039
diff changeset
   327
          val ty = Sign.the_const_type thy c
haftmann
parents: 22039
diff changeset
   328
        in case AxClass.class_of_param thy c
haftmann
parents: 22039
diff changeset
   329
         of SOME class => let
haftmann
parents: 22039
diff changeset
   330
               val inst = case tys
haftmann
parents: 22039
diff changeset
   331
                of [Type (tyco, _)] => classop_const thy algebra class c tyco
haftmann
parents: 22039
diff changeset
   332
                      |> snd
haftmann
parents: 22039
diff changeset
   333
                      |> the_single
haftmann
parents: 22039
diff changeset
   334
                      |> Logic.varifyT
haftmann
parents: 22039
diff changeset
   335
                 | _ => TVar (("'a", 0), [class]);
haftmann
parents: 22039
diff changeset
   336
              in Term.map_type_tvar (K inst) ty end
haftmann
parents: 22039
diff changeset
   337
          | NONE => ty
haftmann
parents: 22039
diff changeset
   338
        end;
haftmann
parents: 22039
diff changeset
   339
    fun add_funcs (const, thms as thm :: _) =
haftmann
parents: 22039
diff changeset
   340
          Constgraph.new_node (const, (CodegenFunc.typ_func thm, thms))
haftmann
parents: 22039
diff changeset
   341
      | add_funcs (const, []) =
haftmann
parents: 22039
diff changeset
   342
          Constgraph.new_node (const, (default_typ const, []));
haftmann
parents: 22039
diff changeset
   343
    val _ = writeln ("constants " ^ (commas o map (CodegenConsts.string_of_const thy o fst)) funcss);
haftmann
parents: 22039
diff changeset
   344
    val _ = writeln ("funcs " ^ (cat_lines o maps (map string_of_thm o snd)) funcss);
haftmann
parents: 22039
diff changeset
   345
    fun add_deps (funcs as (const, thms)) funcgr =
haftmann
parents: 22039
diff changeset
   346
      let
haftmann
parents: 22039
diff changeset
   347
        val deps = consts_of funcs;
haftmann
parents: 22039
diff changeset
   348
        val _ = writeln ("constant " ^ CodegenConsts.string_of_const thy const);
haftmann
parents: 22039
diff changeset
   349
        val insts = instances_of_consts thy algebra funcgr
haftmann
parents: 22039
diff changeset
   350
          (fold_consts (insert (op =)) thms []);
haftmann
parents: 22039
diff changeset
   351
      in
haftmann
parents: 22039
diff changeset
   352
        funcgr
haftmann
parents: 22039
diff changeset
   353
        |> ensure_consts' thy algebra insts
haftmann
parents: 22039
diff changeset
   354
        |> fold (curry Constgraph.add_edge const) deps
haftmann
parents: 22039
diff changeset
   355
        |> fold (curry Constgraph.add_edge const) insts
haftmann
parents: 22039
diff changeset
   356
       end;
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   357
  in
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   358
    funcgr
22185
haftmann
parents: 22039
diff changeset
   359
    |> fold add_funcs funcss
haftmann
parents: 22039
diff changeset
   360
    |> fold add_deps funcss
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   361
  end
22185
haftmann
parents: 22039
diff changeset
   362
and ensure_consts' thy algebra cs funcgr =
haftmann
parents: 22039
diff changeset
   363
  fold (snd oo ensure_const thy algebra funcgr) cs Constgraph.empty
haftmann
parents: 22039
diff changeset
   364
  |> (fn auxgr => fold (merge_funcss thy algebra)
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   365
       (map (AList.make (Constgraph.get_node auxgr))
21387
5d3d340cb783 clarified code for building function equation system; explicit check of type discipline
haftmann
parents: 21196
diff changeset
   366
       (rev (Constgraph.strong_conn auxgr))) funcgr)
22185
haftmann
parents: 22039
diff changeset
   367
  handle INVALID (cs', msg) => raise INVALID (fold (insert CodegenConsts.eq_const) cs' cs, msg);
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   368
21120
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   369
fun drop_classes thy tfrees thm =
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   370
  let
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   371
    val (_, thm') = Thm.varifyT' [] thm;
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   372
    val tvars = Term.add_tvars (Thm.prop_of thm') [];
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   373
    val unconstr = map (Thm.ctyp_of thy o TVar) tvars;
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   374
    val instmap = map2 (fn (v_i, _) => fn (v, sort) => pairself (Thm.ctyp_of thy)
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   375
      (TVar (v_i, []), TFree (v, sort))) tvars tfrees;
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   376
  in
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   377
    thm'
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   378
    |> fold Thm.unconstrainT unconstr
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   379
    |> Thm.instantiate (instmap, [])
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   380
    |> 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
   381
  end;
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   382
22185
haftmann
parents: 22039
diff changeset
   383
fun ensure_consts thy consts funcgr =
haftmann
parents: 22039
diff changeset
   384
  let
haftmann
parents: 22039
diff changeset
   385
    val algebra = CodegenData.coregular_algebra thy
haftmann
parents: 22039
diff changeset
   386
  in ensure_consts' thy algebra consts funcgr
haftmann
parents: 22039
diff changeset
   387
    handle INVALID (cs', msg) => error (msg ^ ",\nwhile preprocessing equations for constant(s) "
haftmann
parents: 22039
diff changeset
   388
    ^ commas (map (CodegenConsts.string_of_const thy) cs'))
haftmann
parents: 22039
diff changeset
   389
  end;
haftmann
parents: 22039
diff changeset
   390
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   391
in
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   392
22185
haftmann
parents: 22039
diff changeset
   393
haftmann
parents: 22039
diff changeset
   394
(** graph retrieval **)
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   395
21120
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   396
fun make thy consts =
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   397
  Funcgr.change thy (ensure_consts thy consts);
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   398
22185
haftmann
parents: 22039
diff changeset
   399
fun make_consts thy consts =
haftmann
parents: 22039
diff changeset
   400
  let
haftmann
parents: 22039
diff changeset
   401
    val algebra = CodegenData.coregular_algebra thy;
haftmann
parents: 22039
diff changeset
   402
    fun try_const const funcgr =
haftmann
parents: 22039
diff changeset
   403
      (SOME const, ensure_consts' thy algebra [const] funcgr)
haftmann
parents: 22039
diff changeset
   404
      handle INVALID (cs', msg) => (NONE, funcgr);
haftmann
parents: 22039
diff changeset
   405
    val (consts', funcgr) = Funcgr.change_yield thy (fold_map try_const consts);
haftmann
parents: 22039
diff changeset
   406
  in (map_filter I consts', funcgr) end;
haftmann
parents: 22039
diff changeset
   407
21129
8e621232a865 clarified make_term interface
haftmann
parents: 21120
diff changeset
   408
fun make_term thy f ct =
21120
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   409
  let
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   410
    val _ = Sign.no_vars (Sign.pp thy) (Thm.term_of ct);
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   411
    val _ = Term.fold_types (Type.no_tvars #> K I) (Thm.term_of ct) ();
22039
9bc8058250a7 slight cleanups
haftmann
parents: 22021
diff changeset
   412
    val thm1 = CodegenData.preprocess_cterm ct;
21120
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   413
    val ct' = Drule.dest_equals_rhs (Thm.cprop_of thm1);
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   414
    val consts = CodegenConsts.consts_of thy (Thm.term_of ct');
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   415
    val funcgr = make thy consts;
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   416
    val (_, thm2) = Thm.varifyT' [] thm1;
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   417
    val thm3 = Thm.reflexive (Drule.dest_equals_rhs (Thm.cprop_of thm2));
22039
9bc8058250a7 slight cleanups
haftmann
parents: 22021
diff changeset
   418
    val [(_, [thm4])] = specialize_typs' thy funcgr [(NONE, [thm3])];
21120
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   419
    val tfrees = Term.add_tfrees (Thm.prop_of thm1) [];
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   420
    fun inst thm =
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   421
      let
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   422
        val tvars = Term.add_tvars (Thm.prop_of thm) [];
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   423
        val instmap = map2 (fn (v_i, sort) => fn (v, _) => pairself (Thm.ctyp_of thy)
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   424
          (TVar (v_i, sort), TFree (v, sort))) tvars tfrees;
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   425
      in Thm.instantiate (instmap, []) thm end;
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   426
    val thm5 = inst thm2;
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   427
    val thm6 = inst thm4;
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   428
    val ct'' = Drule.dest_equals_rhs (Thm.cprop_of thm6);
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   429
    val cs = fold_aterms (fn Const c => cons c | _ => I) (Thm.term_of ct'') [];
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   430
    val drop = drop_classes thy tfrees;
22185
haftmann
parents: 22039
diff changeset
   431
    val algebra = CodegenData.coregular_algebra thy;
haftmann
parents: 22039
diff changeset
   432
    val instdefs = instances_of_consts thy algebra funcgr cs;
22039
9bc8058250a7 slight cleanups
haftmann
parents: 22021
diff changeset
   433
    val funcgr' = ensure_consts thy instdefs funcgr;
21129
8e621232a865 clarified make_term interface
haftmann
parents: 21120
diff changeset
   434
  in (f drop ct'' thm5, Funcgr.change thy (K funcgr')) end;
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   435
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   436
end; (*local*)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   437
22185
haftmann
parents: 22039
diff changeset
   438
haftmann
parents: 22039
diff changeset
   439
(** diagnostics **)
haftmann
parents: 22039
diff changeset
   440
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   441
fun print_funcgr thy funcgr =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   442
  AList.make (snd o Constgraph.get_node funcgr) (Constgraph.keys funcgr)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   443
  |> (map o apfst) (CodegenConsts.string_of_const thy)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   444
  |> sort (string_ord o pairself fst)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   445
  |> map (fn (s, thms) =>
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   446
       (Pretty.block o Pretty.fbreaks) (
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   447
         Pretty.str s
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   448
         :: map Display.pretty_thm thms
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   449
       ))
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   450
  |> Pretty.chunks
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   451
  |> Pretty.writeln;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   452
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   453
fun print_codethms thy consts =
21120
e333c844b057 refined algorithm
haftmann
parents: 20938
diff changeset
   454
  make thy consts |> print_funcgr thy;
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   455
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   456
fun print_codethms_e thy cs =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   457
  print_codethms thy (map (CodegenConsts.read_const thy) cs);
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   458
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   459
22185
haftmann
parents: 22039
diff changeset
   460
(** Isar setup **)
20600
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   461
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   462
structure P = OuterParse;
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   463
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   464
val print_codethmsK = "print_codethms";
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   465
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   466
val print_codethmsP =
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   467
  OuterSyntax.improper_command print_codethmsK "print code theorems of this theory" OuterKeyword.diag
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   468
    (Scan.option (P.$$$ "(" |-- Scan.repeat P.term --| P.$$$ ")")
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   469
      >> (fn NONE => CodegenData.print_thms
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   470
           | SOME cs => fn thy => print_codethms_e thy cs)
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   471
      >> (fn f => Toplevel.no_timing o Toplevel.unknown_theory
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   472
      o Toplevel.keep (f o Toplevel.theory_of)));
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   473
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   474
val _ = OuterSyntax.add_parsers [print_codethmsP];
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   475
6d75e02ed285 added codegen_data
haftmann
parents:
diff changeset
   476
end; (*struct*)