src/Tools/code/code_funcgr.ML
author wenzelm
Thu, 11 Oct 2007 16:05:47 +0200
changeset 24969 b38527eefb3b
parent 24835 8c26128f8997
child 25103 1ee419a5a30f
permissions -rw-r--r--
removed obsolete AxClass.params_of_class; tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     1
(*  Title:      Tools/code/code_funcgr.ML
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     2
    ID:         $Id$
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     3
    Author:     Florian Haftmann, TU Muenchen
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     4
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     5
Retrieving, normalizing and structuring defining equations in graph
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     6
with explicit dependencies.
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     7
*)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     8
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     9
signature CODE_FUNCGR =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    10
sig
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    11
  type T
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    12
  val timing: bool ref
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
    13
  val funcs: T -> string -> thm list
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
    14
  val typ: T -> string -> typ
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
    15
  val all: T -> string list
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    16
  val pretty: theory -> T -> Pretty.T
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
    17
  val make: theory -> string list -> T
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
    18
  val make_consts: theory -> string list -> string list * T
24283
haftmann
parents: 24219
diff changeset
    19
  val eval_conv: theory -> (T -> cterm -> thm) -> cterm -> thm
24835
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24713
diff changeset
    20
  val eval_term: theory -> (T -> term -> 'a) -> term -> 'a
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    21
end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    22
24283
haftmann
parents: 24219
diff changeset
    23
structure CodeFuncgr : CODE_FUNCGR =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    24
struct
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    25
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    26
(** the graph type **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    27
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
    28
type T = (typ * thm list) Graph.T;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    29
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    30
fun funcs funcgr =
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
    31
  these o Option.map snd o try (Graph.get_node funcgr);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    32
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    33
fun typ funcgr =
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
    34
  fst o Graph.get_node funcgr;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    35
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
    36
fun all funcgr = Graph.keys funcgr;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    37
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    38
fun pretty thy funcgr =
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
    39
  AList.make (snd o Graph.get_node funcgr) (Graph.keys funcgr)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    40
  |> (map o apfst) (CodeUnit.string_of_const thy)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    41
  |> sort (string_ord o pairself fst)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    42
  |> map (fn (s, thms) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    43
       (Pretty.block o Pretty.fbreaks) (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    44
         Pretty.str s
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    45
         :: map Display.pretty_thm thms
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    46
       ))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    47
  |> Pretty.chunks;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    48
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    49
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    50
(** generic combinators **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    51
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    52
fun fold_consts f thms =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    53
  thms
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    54
  |> maps (op :: o swap o apfst (snd o strip_comb) o Logic.dest_equals o Thm.plain_prop_of)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    55
  |> (fold o fold_aterms) (fn Const c => f c | _ => I);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    56
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    57
fun consts_of (const, []) = []
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
    58
  | consts_of (const, thms as _ :: _) = 
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    59
      let
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
    60
        fun the_const (c, _) = if c = const then I else insert (op =) c
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    61
      in fold_consts the_const thms [] end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    62
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    63
fun insts_of thy algebra c ty_decl ty =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    64
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    65
    val tys_decl = Sign.const_typargs thy (c, ty_decl);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    66
    val tys = Sign.const_typargs thy (c, ty);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    67
    fun class_relation (x, _) _ = x;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    68
    fun type_constructor tyco xs class =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    69
      (tyco, class) :: maps (maps fst) xs;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    70
    fun type_variable (TVar (_, sort)) = map (pair []) sort
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    71
      | type_variable (TFree (_, sort)) = map (pair []) sort;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    72
    fun mk_inst ty (TVar (_, sort)) = cons (ty, sort)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    73
      | mk_inst ty (TFree (_, sort)) = cons (ty, sort)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    74
      | mk_inst (Type (_, tys1)) (Type (_, tys2)) = fold2 mk_inst tys1 tys2;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    75
    fun of_sort_deriv (ty, sort) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    76
      Sorts.of_sort_derivation (Sign.pp thy) algebra
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    77
        { class_relation = class_relation, type_constructor = type_constructor,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    78
          type_variable = type_variable }
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    79
        (ty, sort)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    80
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    81
    flat (maps of_sort_deriv (fold2 mk_inst tys tys_decl []))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    82
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    83
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    84
fun drop_classes thy tfrees thm =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    85
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    86
    val (_, thm') = Thm.varifyT' [] thm;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    87
    val tvars = Term.add_tvars (Thm.prop_of thm') [];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    88
    val unconstr = map (Thm.ctyp_of thy o TVar) tvars;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    89
    val instmap = map2 (fn (v_i, _) => fn (v, sort) => pairself (Thm.ctyp_of thy)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    90
      (TVar (v_i, []), TFree (v, sort))) tvars tfrees;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    91
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    92
    thm'
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    93
    |> fold Thm.unconstrainT unconstr
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    94
    |> Thm.instantiate (instmap, [])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    95
    |> Tactic.rule_by_tactic ((REPEAT o CHANGED o ALLGOALS o Tactic.resolve_tac) (AxClass.class_intros thy))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    96
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    97
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    98
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    99
(** graph algorithm **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   100
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   101
val timing = ref false;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   102
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   103
local
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   104
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   105
exception INVALID of string list * string;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   106
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   107
fun resort_thms algebra tap_typ [] = []
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   108
  | resort_thms algebra tap_typ (thms as thm :: _) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   109
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   110
        val thy = Thm.theory_of_thm thm;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   111
        val cs = fold_consts (insert (op =)) thms [];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   112
        fun match_const c (ty, ty_decl) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   113
          let
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   114
            val tys = Sign.const_typargs thy (c, ty);
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   115
            val sorts = map (snd o dest_TVar) (Sign.const_typargs thy (c, ty_decl));
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   116
          in fold2 (curry (CodeUnit.typ_sort_inst algebra)) tys sorts end;
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   117
        fun match (c, ty) =
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   118
          case tap_typ c
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   119
           of SOME ty_decl => match_const c (ty, ty_decl)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   120
            | NONE => I;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   121
        val tvars = fold match cs Vartab.empty;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   122
      in map (CodeUnit.inst_thm tvars) thms end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   123
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   124
fun resort_funcss thy algebra funcgr =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   125
  let
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   126
    val typ_funcgr = try (fst o Graph.get_node funcgr);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   127
    fun resort_dep (const, thms) = (const, resort_thms algebra typ_funcgr thms)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   128
      handle Sorts.CLASS_ERROR e => raise INVALID ([const], Sorts.msg_class_error (Sign.pp thy) e
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   129
                    ^ ",\nfor constant " ^ CodeUnit.string_of_const thy const
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   130
                    ^ "\nin defining equations\n"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   131
                    ^ (cat_lines o map string_of_thm) thms)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   132
    fun resort_rec tap_typ (const, []) = (true, (const, []))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   133
      | resort_rec tap_typ (const, thms as thm :: _) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   134
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   135
            val (_, ty) = CodeUnit.head_func thm;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   136
            val thms' as thm' :: _ = resort_thms algebra tap_typ thms
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   137
            val (_, ty') = CodeUnit.head_func thm';
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   138
          in (Sign.typ_equiv thy (ty, ty'), (const, thms')) end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   139
    fun resort_recs funcss =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   140
      let
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   141
        fun tap_typ c =
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   142
          AList.lookup (op =) funcss c
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   143
          |> these
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   144
          |> try hd
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   145
          |> Option.map (snd o CodeUnit.head_func);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   146
        val (unchangeds, funcss') = split_list (map (resort_rec tap_typ) funcss);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   147
        val unchanged = fold (fn x => fn y => x andalso y) unchangeds true;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   148
      in (unchanged, funcss') end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   149
    fun resort_rec_until funcss =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   150
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   151
        val (unchanged, funcss') = resort_recs funcss;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   152
      in if unchanged then funcss' else resort_rec_until funcss' end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   153
  in map resort_dep #> resort_rec_until end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   154
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   155
fun instances_of thy algebra insts =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   156
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   157
    val thy_classes = (#classes o Sorts.rep_algebra o Sign.classes_of) thy;
24835
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24713
diff changeset
   158
    fun all_classparams tyco class =
24969
b38527eefb3b removed obsolete AxClass.params_of_class;
wenzelm
parents: 24835
diff changeset
   159
      these (try (#params o AxClass.get_info thy) class)
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   160
      |> map (fn (c, _) => Class.inst_const thy (c, tyco))
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   161
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   162
    Symtab.empty
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   163
    |> fold (fn (tyco, class) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   164
        Symtab.map_default (tyco, []) (insert (op =) class)) insts
24835
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24713
diff changeset
   165
    |> (fn tab => Symtab.fold (fn (tyco, classes) => append (maps (all_classparams tyco)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   166
         (Graph.all_succs thy_classes classes))) tab [])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   167
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   168
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   169
fun instances_of_consts thy algebra funcgr consts =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   170
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   171
    fun inst (cexpr as (c, ty)) = insts_of thy algebra c
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   172
      ((fst o Graph.get_node funcgr) c) ty handle CLASS_ERROR => [];
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   173
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   174
    []
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   175
    |> fold (fold (insert (op =)) o inst) consts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   176
    |> instances_of thy algebra
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   177
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   178
24283
haftmann
parents: 24219
diff changeset
   179
fun ensure_const' thy algebra funcgr const auxgr =
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   180
  if can (Graph.get_node funcgr) const
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   181
    then (NONE, auxgr)
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   182
  else if can (Graph.get_node auxgr) const
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   183
    then (SOME const, auxgr)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   184
  else if is_some (Code.get_datatype_of_constr thy const) then
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   185
    auxgr
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   186
    |> Graph.new_node (const, [])
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   187
    |> pair (SOME const)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   188
  else let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   189
    val thms = Code.these_funcs thy const
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   190
      |> CodeUnit.norm_args
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   191
      |> CodeUnit.norm_varnames CodeName.purify_tvar CodeName.purify_var;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   192
    val rhs = consts_of (const, thms);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   193
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   194
    auxgr
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   195
    |> Graph.new_node (const, thms)
24283
haftmann
parents: 24219
diff changeset
   196
    |> fold_map (ensure_const thy algebra funcgr) rhs
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   197
    |-> (fn rhs' => fold (fn SOME const' => Graph.add_edge (const, const')
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   198
                           | NONE => I) rhs')
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   199
    |> pair (SOME const)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   200
  end
24283
haftmann
parents: 24219
diff changeset
   201
and ensure_const thy algebra funcgr const =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   202
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   203
    val timeap = if !timing
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   204
      then Output.timeap_msg ("time for " ^ CodeUnit.string_of_const thy const)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   205
      else I;
24283
haftmann
parents: 24219
diff changeset
   206
  in timeap (ensure_const' thy algebra funcgr const) end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   207
24283
haftmann
parents: 24219
diff changeset
   208
fun merge_funcss thy algebra raw_funcss funcgr =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   209
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   210
    val funcss = raw_funcss
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   211
      |> resort_funcss thy algebra funcgr
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   212
      |> filter_out (can (Graph.get_node funcgr) o fst);
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   213
    fun typ_func c [] = Code.default_typ thy c
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   214
      | typ_func c (thms as thm :: _) = case Class.param_const thy c
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   215
         of SOME (c', tyco) => 
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   216
              let
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   217
                val (_, ty) = CodeUnit.head_func thm;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   218
                val SOME class = AxClass.class_of_param thy c';
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   219
                val sorts_decl = Sorts.mg_domain algebra tyco [class];
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   220
                val tys = Sign.const_typargs thy (c, ty);
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   221
                val sorts = map (snd o dest_TVar) tys;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   222
              in if sorts = sorts_decl then ty
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   223
                else raise INVALID ([c], "Illegal instantation for class operation "
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   224
                  ^ CodeUnit.string_of_const thy c
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   225
                  ^ "\nin defining equations\n"
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   226
                  ^ (cat_lines o map string_of_thm) thms)
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   227
              end
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   228
          | NONE => (snd o CodeUnit.head_func) thm;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   229
    fun add_funcs (const, thms) =
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   230
      Graph.new_node (const, (typ_func const thms, thms));
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   231
    fun add_deps (funcs as (const, thms)) funcgr =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   232
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   233
        val deps = consts_of funcs;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   234
        val insts = instances_of_consts thy algebra funcgr
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   235
          (fold_consts (insert (op =)) thms []);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   236
      in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   237
        funcgr
24283
haftmann
parents: 24219
diff changeset
   238
        |> ensure_consts' thy algebra insts
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   239
        |> fold (curry Graph.add_edge const) deps
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   240
        |> fold (curry Graph.add_edge const) insts
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   241
       end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   242
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   243
    funcgr
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   244
    |> fold add_funcs funcss
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   245
    |> fold add_deps funcss
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   246
  end
24283
haftmann
parents: 24219
diff changeset
   247
and ensure_consts' thy algebra cs funcgr =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   248
  let
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   249
    val auxgr = Graph.empty
24283
haftmann
parents: 24219
diff changeset
   250
      |> fold (snd oo ensure_const thy algebra funcgr) cs;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   251
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   252
    funcgr
24283
haftmann
parents: 24219
diff changeset
   253
    |> fold (merge_funcss thy algebra)
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   254
         (map (AList.make (Graph.get_node auxgr))
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   255
         (rev (Graph.strong_conn auxgr)))
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   256
  end handle INVALID (cs', msg)
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   257
    => raise INVALID (fold (insert (op =)) cs' cs, msg);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   258
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   259
in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   260
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   261
(** retrieval interfaces **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   262
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   263
fun ensure_consts thy algebra consts funcgr =
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   264
  ensure_consts' thy algebra consts funcgr
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   265
    handle INVALID (cs', msg) => error (msg ^ ",\nwhile preprocessing equations for constant(s) "
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   266
    ^ commas (map (CodeUnit.string_of_const thy) cs'));
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   267
24283
haftmann
parents: 24219
diff changeset
   268
fun check_consts thy consts funcgr =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   269
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   270
    val algebra = Code.coregular_algebra thy;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   271
    fun try_const const funcgr =
24283
haftmann
parents: 24219
diff changeset
   272
      (SOME const, ensure_consts' thy algebra [const] funcgr)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   273
      handle INVALID (cs', msg) => (NONE, funcgr);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   274
    val (consts', funcgr') = fold_map try_const consts funcgr;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   275
  in (map_filter I consts', funcgr') end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   276
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   277
fun raw_eval thy f ct funcgr =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   278
  let
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   279
    val algebra = Code.coregular_algebra thy;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   280
    fun consts_of ct = fold_aterms (fn Const c_ty => cons c_ty | _ => I)
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   281
      (Thm.term_of ct) [];
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   282
    val _ = Sign.no_vars (Sign.pp thy) (Thm.term_of ct);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   283
    val _ = Term.fold_types (Type.no_tvars #> K I) (Thm.term_of ct) ();
24283
haftmann
parents: 24219
diff changeset
   284
    val thm1 = Code.preprocess_conv ct;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   285
    val ct' = Thm.rhs_of thm1;
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   286
    val cs = map fst (consts_of ct');
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   287
    val funcgr' = ensure_consts thy algebra cs funcgr;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   288
    val (_, thm2) = Thm.varifyT' [] thm1;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   289
    val thm3 = Thm.reflexive (Thm.rhs_of thm2);
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   290
    val [thm4] = resort_thms algebra (try (fst o Graph.get_node funcgr')) [thm3];
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   291
    val tfrees = Term.add_tfrees (Thm.prop_of thm1) [];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   292
    fun inst thm =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   293
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   294
        val tvars = Term.add_tvars (Thm.prop_of thm) [];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   295
        val instmap = map2 (fn (v_i, sort) => fn (v, _) => pairself (Thm.ctyp_of thy)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   296
          (TVar (v_i, sort), TFree (v, sort))) tvars tfrees;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   297
      in Thm.instantiate (instmap, []) thm end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   298
    val thm5 = inst thm2;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   299
    val thm6 = inst thm4;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   300
    val ct'' = Thm.rhs_of thm6;
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   301
    val c_exprs = consts_of ct'';
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   302
    val drop = drop_classes thy tfrees;
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   303
    val instdefs = instances_of_consts thy algebra funcgr' c_exprs;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   304
    val funcgr'' = ensure_consts thy algebra instdefs funcgr';
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   305
  in (f drop thm5 funcgr'' ct'' , funcgr'') end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   306
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   307
fun raw_eval_conv thy conv =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   308
  let
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   309
    fun conv' drop_classes thm1 funcgr ct =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   310
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   311
        val thm2 = conv funcgr ct;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   312
        val thm3 = Code.postprocess_conv (Thm.rhs_of thm2);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   313
        val thm23 = drop_classes (Thm.transitive thm2 thm3);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   314
      in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   315
        Thm.transitive thm1 thm23 handle THM _ =>
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   316
          error ("could not construct proof:\n"
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   317
          ^ (cat_lines o map string_of_thm) [thm1, thm2, thm3])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   318
      end;
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   319
  in raw_eval thy conv' end;
24283
haftmann
parents: 24219
diff changeset
   320
24835
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24713
diff changeset
   321
fun raw_eval_term thy f t =
24283
haftmann
parents: 24219
diff changeset
   322
  let
24835
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24713
diff changeset
   323
    fun f' _ _ funcgr ct = f funcgr (Thm.term_of ct);
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24713
diff changeset
   324
  in raw_eval thy f' (Thm.cterm_of thy t) end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   325
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   326
end; (*local*)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   327
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   328
structure Funcgr = CodeDataFun
24713
8b3b6d09ef40 tuned functor application;
wenzelm
parents: 24423
diff changeset
   329
(
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   330
  type T = T;
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   331
  val empty = Graph.empty;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   332
  fun merge _ _ = Graph.empty;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   333
  fun purge _ NONE _ = Graph.empty
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   334
    | purge _ (SOME cs) funcgr =
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   335
        Graph.del_nodes ((Graph.all_preds funcgr 
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   336
          o filter (can (Graph.get_node funcgr))) cs) funcgr;
24713
8b3b6d09ef40 tuned functor application;
wenzelm
parents: 24423
diff changeset
   337
);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   338
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   339
fun make thy =
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   340
  Funcgr.change thy o ensure_consts thy (Code.coregular_algebra thy);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   341
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   342
fun make_consts thy =
24283
haftmann
parents: 24219
diff changeset
   343
  Funcgr.change_yield thy o check_consts thy;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   344
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   345
fun eval_conv thy f =
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   346
  fst o Funcgr.change_yield thy o raw_eval_conv thy f;
24283
haftmann
parents: 24219
diff changeset
   347
haftmann
parents: 24219
diff changeset
   348
fun eval_term thy f =
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   349
  fst o Funcgr.change_yield thy o raw_eval_term thy f;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   350
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   351
end; (*struct*)