src/Tools/code/code_funcgr.ML
author haftmann
Mon, 26 May 2008 17:55:38 +0200
changeset 26997 40552bbac005
parent 26971 160117247294
child 27103 d8549f4d900b
permissions -rw-r--r--
permissive wrt. instantiation of class operations
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
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
    12
  val funcs: T -> string -> thm list
26971
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
    13
  val typ: T -> string -> (string * sort) list * typ
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
    14
  val all: T -> string list
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    15
  val pretty: theory -> T -> Pretty.T
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
    16
  val make: theory -> string list -> T
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
    17
  val make_consts: theory -> string list -> string list * T
26740
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
    18
  val eval_conv: theory -> (term -> term * (T -> term -> thm)) -> cterm -> thm
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
    19
  val eval_term: theory -> (term -> term * (T -> term -> 'a)) -> term -> 'a
26971
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
    20
  val timing: bool ref
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
26971
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
    28
type T = (((string * sort) list * 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
26971
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
    63
fun insts_of thy algebra tys sorts =
24219
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
    fun class_relation (x, _) _ = x;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    66
    fun type_constructor tyco xs class =
26971
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
    67
      (tyco, class) :: (maps o maps) fst xs;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    68
    fun type_variable (TVar (_, sort)) = map (pair []) sort
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    69
      | type_variable (TFree (_, sort)) = map (pair []) sort;
26971
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
    70
    fun of_sort_deriv ty sort =
26939
1035c89b4c02 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26928
diff changeset
    71
      Sorts.of_sort_derivation (Syntax.pp_global thy) algebra
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    72
        { class_relation = class_relation, type_constructor = type_constructor,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    73
          type_variable = type_variable }
26517
ef036a63f6e9 canonical meet_sort operation
haftmann
parents: 26331
diff changeset
    74
        (ty, sort) handle Sorts.CLASS_ERROR _ => [] (*permissive!*)
26971
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
    75
  in (flat o flat) (map2 of_sort_deriv tys sorts) end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    76
26971
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
    77
fun meets_of thy algebra =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    78
  let
26971
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
    79
    fun meet_of ty sort tab =
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
    80
      Sorts.meet_sort algebra (ty, sort) tab
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
    81
        handle Sorts.CLASS_ERROR _ => tab (*permissive!*);
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
    82
  in fold2 meet_of end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    83
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    84
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    85
(** graph algorithm **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    86
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    87
val timing = ref false;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    88
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    89
local
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    90
26971
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
    91
fun resort_thms thy algebra typ_of thms =
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
    92
  let
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
    93
    val cs = fold_consts (insert (op =)) thms [];
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
    94
    fun meets (c, ty) = case typ_of c
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
    95
       of SOME (vs, _) =>
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
    96
            meets_of thy algebra (Sign.const_typargs thy (c, ty)) (map snd vs)
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
    97
        | NONE => I;
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
    98
    val tab = fold meets cs Vartab.empty;
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
    99
  in map (CodeUnit.inst_thm tab) thms end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   100
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   101
fun resort_funcss thy algebra funcgr =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   102
  let
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   103
    val typ_funcgr = try (fst o Graph.get_node funcgr);
26971
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
   104
    val resort_dep = apsnd (resort_thms thy algebra typ_funcgr);
26997
40552bbac005 permissive wrt. instantiation of class operations
haftmann
parents: 26971
diff changeset
   105
    fun resort_rec typ_of (c, []) = (true, (c, []))
40552bbac005 permissive wrt. instantiation of class operations
haftmann
parents: 26971
diff changeset
   106
      | resort_rec typ_of (c, thms as thm :: _) = if is_some (AxClass.inst_of_param thy c)
40552bbac005 permissive wrt. instantiation of class operations
haftmann
parents: 26971
diff changeset
   107
          then (true, (c, thms))
40552bbac005 permissive wrt. instantiation of class operations
haftmann
parents: 26971
diff changeset
   108
          else let
26971
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
   109
            val (_, (vs, ty)) = CodeUnit.head_func thm;
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
   110
            val thms' as thm' :: _ = resort_thms thy algebra typ_of thms
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
   111
            val (_, (vs', ty')) = CodeUnit.head_func thm'; (*FIXME simplify check*)
26997
40552bbac005 permissive wrt. instantiation of class operations
haftmann
parents: 26971
diff changeset
   112
          in (Sign.typ_equiv thy (ty, ty'), (c, thms')) end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   113
    fun resort_recs funcss =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   114
      let
26971
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
   115
        fun typ_of c = case these (AList.lookup (op =) funcss c)
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
   116
         of thm :: _ => (SOME o snd o CodeUnit.head_func) thm
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
   117
          | [] => NONE;
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
   118
        val (unchangeds, funcss') = split_list (map (resort_rec typ_of) funcss);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   119
        val unchanged = fold (fn x => fn y => x andalso y) unchangeds true;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   120
      in (unchanged, funcss') end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   121
    fun resort_rec_until funcss =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   122
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   123
        val (unchanged, funcss') = resort_recs funcss;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   124
      in if unchanged then funcss' else resort_rec_until funcss' end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   125
  in map resort_dep #> resort_rec_until end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   126
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   127
fun instances_of thy algebra insts =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   128
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   129
    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
   130
    fun all_classparams tyco class =
24969
b38527eefb3b removed obsolete AxClass.params_of_class;
wenzelm
parents: 24835
diff changeset
   131
      these (try (#params o AxClass.get_info thy) class)
26517
ef036a63f6e9 canonical meet_sort operation
haftmann
parents: 26331
diff changeset
   132
      |> map_filter (fn (c, _) => try (AxClass.param_of_inst thy) (c, tyco))
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   133
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   134
    Symtab.empty
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   135
    |> fold (fn (tyco, class) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   136
        Symtab.map_default (tyco, []) (insert (op =) class)) insts
24835
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24713
diff changeset
   137
    |> (fn tab => Symtab.fold (fn (tyco, classes) => append (maps (all_classparams tyco)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   138
         (Graph.all_succs thy_classes classes))) tab [])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   139
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   140
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   141
fun instances_of_consts thy algebra funcgr consts =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   142
  let
26971
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
   143
    fun inst (cexpr as (c, ty)) = insts_of thy algebra
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
   144
      (Sign.const_typargs thy (c, ty)) ((map snd o fst) (typ funcgr c));
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   145
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   146
    []
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   147
    |> fold (fold (insert (op =)) o inst) consts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   148
    |> instances_of thy algebra
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   149
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   150
24283
haftmann
parents: 24219
diff changeset
   151
fun ensure_const' thy algebra funcgr const auxgr =
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   152
  if can (Graph.get_node funcgr) const
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   153
    then (NONE, auxgr)
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   154
  else if can (Graph.get_node auxgr) const
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   155
    then (SOME const, auxgr)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   156
  else if is_some (Code.get_datatype_of_constr thy const) then
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   157
    auxgr
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   158
    |> Graph.new_node (const, [])
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   159
    |> pair (SOME const)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   160
  else let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   161
    val thms = Code.these_funcs thy const
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   162
      |> CodeUnit.norm_args
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   163
      |> CodeUnit.norm_varnames CodeName.purify_tvar CodeName.purify_var;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   164
    val rhs = consts_of (const, thms);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   165
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   166
    auxgr
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   167
    |> Graph.new_node (const, thms)
24283
haftmann
parents: 24219
diff changeset
   168
    |> fold_map (ensure_const thy algebra funcgr) rhs
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   169
    |-> (fn rhs' => fold (fn SOME const' => Graph.add_edge (const, const')
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   170
                           | NONE => I) rhs')
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   171
    |> pair (SOME const)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   172
  end
24283
haftmann
parents: 24219
diff changeset
   173
and ensure_const thy algebra funcgr const =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   174
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   175
    val timeap = if !timing
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   176
      then Output.timeap_msg ("time for " ^ CodeUnit.string_of_const thy const)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   177
      else I;
24283
haftmann
parents: 24219
diff changeset
   178
  in timeap (ensure_const' thy algebra funcgr const) end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   179
24283
haftmann
parents: 24219
diff changeset
   180
fun merge_funcss thy algebra raw_funcss funcgr =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   181
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   182
    val funcss = raw_funcss
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   183
      |> resort_funcss thy algebra funcgr
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   184
      |> filter_out (can (Graph.get_node funcgr) o fst);
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   185
    fun typ_func c [] = Code.default_typ thy c
26997
40552bbac005 permissive wrt. instantiation of class operations
haftmann
parents: 26971
diff changeset
   186
      | typ_func c (thms as thm :: _) = (snd o CodeUnit.head_func) thm;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   187
    fun add_funcs (const, thms) =
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   188
      Graph.new_node (const, (typ_func const thms, thms));
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   189
    fun add_deps (funcs as (const, thms)) funcgr =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   190
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   191
        val deps = consts_of funcs;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   192
        val insts = instances_of_consts thy algebra funcgr
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   193
          (fold_consts (insert (op =)) thms []);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   194
      in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   195
        funcgr
26997
40552bbac005 permissive wrt. instantiation of class operations
haftmann
parents: 26971
diff changeset
   196
        |> ensure_consts thy algebra insts
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   197
        |> fold (curry Graph.add_edge const) deps
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   198
        |> fold (curry Graph.add_edge const) insts
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   199
       end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   200
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   201
    funcgr
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   202
    |> fold add_funcs funcss
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   203
    |> fold add_deps funcss
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   204
  end
26997
40552bbac005 permissive wrt. instantiation of class operations
haftmann
parents: 26971
diff changeset
   205
and ensure_consts thy algebra cs funcgr =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   206
  let
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   207
    val auxgr = Graph.empty
24283
haftmann
parents: 24219
diff changeset
   208
      |> fold (snd oo ensure_const thy algebra funcgr) cs;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   209
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   210
    funcgr
24283
haftmann
parents: 24219
diff changeset
   211
    |> fold (merge_funcss thy algebra)
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   212
         (map (AList.make (Graph.get_node auxgr))
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   213
         (rev (Graph.strong_conn auxgr)))
26997
40552bbac005 permissive wrt. instantiation of class operations
haftmann
parents: 26971
diff changeset
   214
  end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   215
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   216
in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   217
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   218
(** retrieval interfaces **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   219
26997
40552bbac005 permissive wrt. instantiation of class operations
haftmann
parents: 26971
diff changeset
   220
val ensure_consts = ensure_consts;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   221
24283
haftmann
parents: 24219
diff changeset
   222
fun check_consts thy consts funcgr =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   223
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   224
    val algebra = Code.coregular_algebra thy;
26997
40552bbac005 permissive wrt. instantiation of class operations
haftmann
parents: 26971
diff changeset
   225
    fun try_const const funcgr = (const, ensure_consts thy algebra [const] funcgr);
40552bbac005 permissive wrt. instantiation of class operations
haftmann
parents: 26971
diff changeset
   226
  in fold_map try_const consts funcgr end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   227
26740
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   228
fun proto_eval thy cterm_of evaluator_fr evaluator proto_ct funcgr =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   229
  let
26740
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   230
    val ct = cterm_of proto_ct;
26939
1035c89b4c02 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26928
diff changeset
   231
    val _ = Sign.no_vars (Syntax.pp_global thy) (Thm.term_of ct);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   232
    val _ = Term.fold_types (Type.no_tvars #> K I) (Thm.term_of ct) ();
26740
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   233
    fun consts_of t = fold_aterms (fn Const c_ty => cons c_ty | _ => I)
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   234
      t [];
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   235
    val algebra = Code.coregular_algebra thy;
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   236
    val thm = Code.preprocess_conv ct;
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   237
    val ct' = Thm.rhs_of thm;
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   238
    val t' = Thm.term_of ct';
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   239
    val consts = map fst (consts_of t');
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   240
    val funcgr' = ensure_consts thy algebra consts funcgr;
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   241
    val (t'', evaluator') = apsnd evaluator_fr (evaluator t');
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   242
    val consts' = consts_of t'';
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   243
    val dicts = instances_of_consts thy algebra funcgr' consts';
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   244
    val funcgr'' = ensure_consts thy algebra dicts funcgr';
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   245
  in (evaluator' thm funcgr'' t'', funcgr'') end;
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   246
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   247
fun proto_eval_conv thy =
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   248
  let
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   249
    fun evaluator evaluator' thm1 funcgr t =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   250
      let
26740
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   251
        val thm2 = evaluator' funcgr t;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   252
        val thm3 = Code.postprocess_conv (Thm.rhs_of thm2);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   253
      in
26740
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   254
        Thm.transitive thm1 (Thm.transitive thm2 thm3) handle THM _ =>
26971
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
   255
          error ("could not construct evaluation proof:\n"
26928
ca87aff1ad2d structure Display: less pervasive operations;
wenzelm
parents: 26740
diff changeset
   256
          ^ (cat_lines o map Display.string_of_thm) [thm1, thm2, thm3])
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   257
      end;
26740
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   258
  in proto_eval thy I evaluator end;
24283
haftmann
parents: 24219
diff changeset
   259
26740
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   260
fun proto_eval_term thy =
24283
haftmann
parents: 24219
diff changeset
   261
  let
26740
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   262
    fun evaluator evaluator' _ funcgr t = evaluator' funcgr t;
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   263
  in proto_eval thy (Thm.cterm_of thy) evaluator end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   264
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   265
end; (*local*)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   266
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   267
structure Funcgr = CodeDataFun
24713
8b3b6d09ef40 tuned functor application;
wenzelm
parents: 24423
diff changeset
   268
(
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   269
  type T = T;
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   270
  val empty = Graph.empty;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   271
  fun merge _ _ = Graph.empty;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   272
  fun purge _ NONE _ = Graph.empty
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   273
    | purge _ (SOME cs) funcgr =
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   274
        Graph.del_nodes ((Graph.all_preds funcgr 
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   275
          o filter (can (Graph.get_node funcgr))) cs) funcgr;
24713
8b3b6d09ef40 tuned functor application;
wenzelm
parents: 24423
diff changeset
   276
);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   277
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   278
fun make thy =
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   279
  Funcgr.change thy o ensure_consts thy (Code.coregular_algebra thy);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   280
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   281
fun make_consts thy =
24283
haftmann
parents: 24219
diff changeset
   282
  Funcgr.change_yield thy o check_consts thy;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   283
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   284
fun eval_conv thy f =
26740
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   285
  fst o Funcgr.change_yield thy o proto_eval_conv thy f;
24283
haftmann
parents: 24219
diff changeset
   286
haftmann
parents: 24219
diff changeset
   287
fun eval_term thy f =
26740
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   288
  fst o Funcgr.change_yield thy o proto_eval_term thy f;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   289
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   290
end; (*struct*)