src/Tools/code/code_funcgr.ML
author haftmann
Thu, 25 Sep 2008 09:28:08 +0200
changeset 28350 715163ec93c0
parent 28338 e58ec46d50bc
child 28370 37f56e6e702d
permissions -rw-r--r--
non left-linear equations for nbe
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
28350
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28338
diff changeset
    12
  val funcs: T -> string -> (thm * bool) 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
26740
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
    17
  val eval_conv: theory -> (term -> term * (T -> term -> thm)) -> cterm -> thm
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
    18
  val eval_term: theory -> (term -> term * (T -> term -> 'a)) -> term -> 'a
26971
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
    19
  val timing: bool ref
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    20
end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    21
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents: 27609
diff changeset
    22
structure Code_Funcgr : CODE_FUNCGR =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    23
struct
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    24
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    25
(** the graph type **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    26
28350
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28338
diff changeset
    27
type T = (((string * sort) list * typ) * (thm * bool) list) Graph.T;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    28
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    29
fun funcs funcgr =
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
    30
  these o Option.map snd o try (Graph.get_node funcgr);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    31
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    32
fun typ funcgr =
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
    33
  fst o Graph.get_node funcgr;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    34
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
    35
fun all funcgr = Graph.keys funcgr;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    36
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    37
fun pretty thy funcgr =
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
    38
  AList.make (snd o Graph.get_node funcgr) (Graph.keys funcgr)
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents: 27609
diff changeset
    39
  |> (map o apfst) (Code_Unit.string_of_const thy)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    40
  |> sort (string_ord o pairself fst)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    41
  |> map (fn (s, thms) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    42
       (Pretty.block o Pretty.fbreaks) (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    43
         Pretty.str s
28350
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28338
diff changeset
    44
         :: map (Display.pretty_thm o fst) thms
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    45
       ))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    46
  |> Pretty.chunks;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    47
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    48
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    49
(** generic combinators **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    50
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    51
fun fold_consts f thms =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    52
  thms
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    53
  |> 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
    54
  |> (fold o fold_aterms) (fn Const c => f c | _ => I);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    55
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    56
fun consts_of (const, []) = []
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
    57
  | consts_of (const, thms as _ :: _) = 
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    58
      let
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
    59
        fun the_const (c, _) = if c = const then I else insert (op =) c
28350
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28338
diff changeset
    60
      in fold_consts the_const (map fst thms) [] end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    61
26971
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
    62
fun insts_of thy algebra tys sorts =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    63
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    64
    fun class_relation (x, _) _ = x;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    65
    fun type_constructor tyco xs class =
26971
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
    66
      (tyco, class) :: (maps o maps) fst xs;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    67
    fun type_variable (TVar (_, sort)) = map (pair []) sort
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    68
      | type_variable (TFree (_, sort)) = map (pair []) sort;
26971
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
    69
    fun of_sort_deriv ty sort =
26939
1035c89b4c02 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26928
diff changeset
    70
      Sorts.of_sort_derivation (Syntax.pp_global thy) algebra
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    71
        { class_relation = class_relation, type_constructor = type_constructor,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    72
          type_variable = type_variable }
26517
ef036a63f6e9 canonical meet_sort operation
haftmann
parents: 26331
diff changeset
    73
        (ty, sort) handle Sorts.CLASS_ERROR _ => [] (*permissive!*)
26971
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
    74
  in (flat o flat) (map2 of_sort_deriv tys sorts) end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    75
26971
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
    76
fun meets_of thy algebra =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    77
  let
26971
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
    78
    fun meet_of ty sort tab =
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
    79
      Sorts.meet_sort algebra (ty, sort) tab
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
    80
        handle Sorts.CLASS_ERROR _ => tab (*permissive!*);
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
    81
  in fold2 meet_of end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    82
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    83
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    84
(** graph algorithm **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    85
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    86
val timing = ref false;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    87
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    88
local
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    89
26971
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
    90
fun resort_thms thy algebra typ_of thms =
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
    91
  let
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
    92
    val cs = fold_consts (insert (op =)) thms [];
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
    93
    fun meets (c, ty) = case typ_of c
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
    94
       of SOME (vs, _) =>
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
    95
            meets_of thy algebra (Sign.const_typargs thy (c, ty)) (map snd vs)
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
    96
        | NONE => I;
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
    97
    val tab = fold meets cs Vartab.empty;
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents: 27609
diff changeset
    98
  in map (Code_Unit.inst_thm tab) thms end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    99
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   100
fun resort_funcss thy algebra funcgr =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   101
  let
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   102
    val typ_funcgr = try (fst o Graph.get_node funcgr);
28350
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28338
diff changeset
   103
    val resort_dep = (apsnd o burrow_fst) (resort_thms thy algebra typ_funcgr);
26997
40552bbac005 permissive wrt. instantiation of class operations
haftmann
parents: 26971
diff changeset
   104
    fun resort_rec typ_of (c, []) = (true, (c, []))
28350
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28338
diff changeset
   105
      | resort_rec typ_of (c, thms as (thm, _) :: _) = if is_some (AxClass.inst_of_param thy c)
26997
40552bbac005 permissive wrt. instantiation of class operations
haftmann
parents: 26971
diff changeset
   106
          then (true, (c, thms))
40552bbac005 permissive wrt. instantiation of class operations
haftmann
parents: 26971
diff changeset
   107
          else let
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents: 27609
diff changeset
   108
            val (_, (vs, ty)) = Code_Unit.head_func thm;
28350
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28338
diff changeset
   109
            val thms' as (thm', _) :: _ = burrow_fst (resort_thms thy algebra typ_of) thms
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents: 27609
diff changeset
   110
            val (_, (vs', ty')) = Code_Unit.head_func thm'; (*FIXME simplify check*)
26997
40552bbac005 permissive wrt. instantiation of class operations
haftmann
parents: 26971
diff changeset
   111
          in (Sign.typ_equiv thy (ty, ty'), (c, thms')) end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   112
    fun resort_recs funcss =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   113
      let
26971
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
   114
        fun typ_of c = case these (AList.lookup (op =) funcss c)
28350
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28338
diff changeset
   115
         of (thm, _) :: _ => (SOME o snd o Code_Unit.head_func) thm
26971
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
   116
          | [] => NONE;
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
   117
        val (unchangeds, funcss') = split_list (map (resort_rec typ_of) funcss);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   118
        val unchanged = fold (fn x => fn y => x andalso y) unchangeds true;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   119
      in (unchanged, funcss') end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   120
    fun resort_rec_until funcss =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   121
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   122
        val (unchanged, funcss') = resort_recs funcss;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   123
      in if unchanged then funcss' else resort_rec_until funcss' end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   124
  in map resort_dep #> resort_rec_until end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   125
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   126
fun instances_of thy algebra insts =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   127
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   128
    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
   129
    fun all_classparams tyco class =
24969
b38527eefb3b removed obsolete AxClass.params_of_class;
wenzelm
parents: 24835
diff changeset
   130
      these (try (#params o AxClass.get_info thy) class)
26517
ef036a63f6e9 canonical meet_sort operation
haftmann
parents: 26331
diff changeset
   131
      |> map_filter (fn (c, _) => try (AxClass.param_of_inst thy) (c, tyco))
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   132
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   133
    Symtab.empty
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   134
    |> fold (fn (tyco, class) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   135
        Symtab.map_default (tyco, []) (insert (op =) class)) insts
24835
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24713
diff changeset
   136
    |> (fn tab => Symtab.fold (fn (tyco, classes) => append (maps (all_classparams tyco)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   137
         (Graph.all_succs thy_classes classes))) tab [])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   138
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   139
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   140
fun instances_of_consts thy algebra funcgr consts =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   141
  let
26971
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
   142
    fun inst (cexpr as (c, ty)) = insts_of thy algebra
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
   143
      (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
   144
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   145
    []
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   146
    |> fold (fold (insert (op =)) o inst) consts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   147
    |> instances_of thy algebra
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   148
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   149
24283
haftmann
parents: 24219
diff changeset
   150
fun ensure_const' thy algebra funcgr const auxgr =
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   151
  if can (Graph.get_node funcgr) const
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   152
    then (NONE, auxgr)
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   153
  else if can (Graph.get_node auxgr) const
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   154
    then (SOME const, auxgr)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   155
  else if is_some (Code.get_datatype_of_constr thy const) then
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   156
    auxgr
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   157
    |> Graph.new_node (const, [])
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   158
    |> pair (SOME const)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   159
  else let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   160
    val thms = Code.these_funcs thy const
28350
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28338
diff changeset
   161
      |> burrow_fst Code_Unit.norm_args
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28338
diff changeset
   162
      |> burrow_fst (Code_Unit.norm_varnames Code_Name.purify_tvar Code_Name.purify_var);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   163
    val rhs = consts_of (const, thms);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   164
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   165
    auxgr
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   166
    |> Graph.new_node (const, thms)
24283
haftmann
parents: 24219
diff changeset
   167
    |> fold_map (ensure_const thy algebra funcgr) rhs
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   168
    |-> (fn rhs' => fold (fn SOME const' => Graph.add_edge (const, const')
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   169
                           | NONE => I) rhs')
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   170
    |> pair (SOME const)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   171
  end
24283
haftmann
parents: 24219
diff changeset
   172
and ensure_const thy algebra funcgr const =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   173
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   174
    val timeap = if !timing
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents: 27609
diff changeset
   175
      then Output.timeap_msg ("time for " ^ Code_Unit.string_of_const thy const)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   176
      else I;
24283
haftmann
parents: 24219
diff changeset
   177
  in timeap (ensure_const' thy algebra funcgr const) end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   178
24283
haftmann
parents: 24219
diff changeset
   179
fun merge_funcss thy algebra raw_funcss funcgr =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   180
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   181
    val funcss = raw_funcss
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   182
      |> resort_funcss thy algebra funcgr
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   183
      |> filter_out (can (Graph.get_node funcgr) o fst);
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   184
    fun typ_func c [] = Code.default_typ thy c
28350
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28338
diff changeset
   185
      | typ_func c (thms as (thm, _) :: _) = (snd o Code_Unit.head_func) thm;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   186
    fun add_funcs (const, thms) =
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   187
      Graph.new_node (const, (typ_func const thms, thms));
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   188
    fun add_deps (funcs as (const, thms)) funcgr =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   189
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   190
        val deps = consts_of funcs;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   191
        val insts = instances_of_consts thy algebra funcgr
28350
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28338
diff changeset
   192
          (fold_consts (insert (op =)) (map fst thms) []);
24219
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
        funcgr
26997
40552bbac005 permissive wrt. instantiation of class operations
haftmann
parents: 26971
diff changeset
   195
        |> ensure_consts thy algebra insts
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   196
        |> fold (curry Graph.add_edge const) deps
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   197
        |> fold (curry Graph.add_edge const) insts
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   198
       end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   199
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   200
    funcgr
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   201
    |> fold add_funcs funcss
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   202
    |> fold add_deps funcss
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   203
  end
26997
40552bbac005 permissive wrt. instantiation of class operations
haftmann
parents: 26971
diff changeset
   204
and ensure_consts thy algebra cs funcgr =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   205
  let
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   206
    val auxgr = Graph.empty
24283
haftmann
parents: 24219
diff changeset
   207
      |> fold (snd oo ensure_const thy algebra funcgr) cs;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   208
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   209
    funcgr
24283
haftmann
parents: 24219
diff changeset
   210
    |> fold (merge_funcss thy algebra)
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   211
         (map (AList.make (Graph.get_node auxgr))
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   212
         (rev (Graph.strong_conn auxgr)))
26997
40552bbac005 permissive wrt. instantiation of class operations
haftmann
parents: 26971
diff changeset
   213
  end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   214
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   215
in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   216
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   217
(** retrieval interfaces **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   218
26997
40552bbac005 permissive wrt. instantiation of class operations
haftmann
parents: 26971
diff changeset
   219
val ensure_consts = ensure_consts;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   220
26740
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   221
fun proto_eval thy cterm_of evaluator_fr evaluator proto_ct funcgr =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   222
  let
26740
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   223
    val ct = cterm_of proto_ct;
26939
1035c89b4c02 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26928
diff changeset
   224
    val _ = Sign.no_vars (Syntax.pp_global thy) (Thm.term_of ct);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   225
    val _ = Term.fold_types (Type.no_tvars #> K I) (Thm.term_of ct) ();
26740
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   226
    fun consts_of t = fold_aterms (fn Const c_ty => cons c_ty | _ => I)
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   227
      t [];
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   228
    val algebra = Code.coregular_algebra thy;
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   229
    val thm = Code.preprocess_conv ct;
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   230
    val ct' = Thm.rhs_of thm;
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   231
    val t' = Thm.term_of ct';
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   232
    val consts = map fst (consts_of t');
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   233
    val funcgr' = ensure_consts thy algebra consts funcgr;
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   234
    val (t'', evaluator') = apsnd evaluator_fr (evaluator t');
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   235
    val consts' = consts_of t'';
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   236
    val dicts = instances_of_consts thy algebra funcgr' consts';
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   237
    val funcgr'' = ensure_consts thy algebra dicts funcgr';
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   238
  in (evaluator' thm funcgr'' t'', funcgr'') end;
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   239
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   240
fun proto_eval_conv thy =
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   241
  let
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   242
    fun evaluator evaluator' thm1 funcgr t =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   243
      let
26740
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   244
        val thm2 = evaluator' funcgr t;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   245
        val thm3 = Code.postprocess_conv (Thm.rhs_of thm2);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   246
      in
26740
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   247
        Thm.transitive thm1 (Thm.transitive thm2 thm3) handle THM _ =>
26971
160117247294 more permissive preprocessor
haftmann
parents: 26939
diff changeset
   248
          error ("could not construct evaluation proof:\n"
26928
ca87aff1ad2d structure Display: less pervasive operations;
wenzelm
parents: 26740
diff changeset
   249
          ^ (cat_lines o map Display.string_of_thm) [thm1, thm2, thm3])
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   250
      end;
26740
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   251
  in proto_eval thy I evaluator end;
24283
haftmann
parents: 24219
diff changeset
   252
26740
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   253
fun proto_eval_term thy =
24283
haftmann
parents: 24219
diff changeset
   254
  let
26740
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   255
    fun evaluator evaluator' _ funcgr t = evaluator' funcgr t;
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   256
  in proto_eval thy (Thm.cterm_of thy) evaluator end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   257
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   258
end; (*local*)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   259
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   260
structure Funcgr = CodeDataFun
24713
8b3b6d09ef40 tuned functor application;
wenzelm
parents: 24423
diff changeset
   261
(
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   262
  type T = T;
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   263
  val empty = Graph.empty;
27609
b23c9ad0fe7d tuned code theorem bookkeeping
haftmann
parents: 27103
diff changeset
   264
  fun purge _ cs funcgr =
b23c9ad0fe7d tuned code theorem bookkeeping
haftmann
parents: 27103
diff changeset
   265
    Graph.del_nodes ((Graph.all_preds funcgr 
b23c9ad0fe7d tuned code theorem bookkeeping
haftmann
parents: 27103
diff changeset
   266
      o filter (can (Graph.get_node funcgr))) cs) funcgr;
24713
8b3b6d09ef40 tuned functor application;
wenzelm
parents: 24423
diff changeset
   267
);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   268
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   269
fun make thy =
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24283
diff changeset
   270
  Funcgr.change thy o ensure_consts thy (Code.coregular_algebra thy);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   271
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   272
fun eval_conv thy f =
26740
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   273
  fst o Funcgr.change_yield thy o proto_eval_conv thy f;
24283
haftmann
parents: 24219
diff changeset
   274
haftmann
parents: 24219
diff changeset
   275
fun eval_term thy f =
26740
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26642
diff changeset
   276
  fst o Funcgr.change_yield thy o proto_eval_term thy f;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   277
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 26997
diff changeset
   278
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 26997
diff changeset
   279
(** diagnostic commands **)
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 26997
diff changeset
   280
28350
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28338
diff changeset
   281
fun code_depgr thy consts =
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28338
diff changeset
   282
  let
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28338
diff changeset
   283
    val gr = make thy consts;
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28338
diff changeset
   284
    val select = Graph.all_succs gr consts;
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28338
diff changeset
   285
  in
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28338
diff changeset
   286
    gr
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28338
diff changeset
   287
    |> not (null consts) ? Graph.subgraph (member (op =) select) 
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28338
diff changeset
   288
    |> Graph.map_nodes ((apsnd o map o apfst) (AxClass.overload thy))
715163ec93c0 non left-linear equations for nbe
haftmann
parents: 28338
diff changeset
   289
  end;
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 26997
diff changeset
   290
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 26997
diff changeset
   291
fun code_thms thy = Pretty.writeln o pretty thy o code_depgr thy;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 26997
diff changeset
   292
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 26997
diff changeset
   293
fun code_deps thy consts =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 26997
diff changeset
   294
  let
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 26997
diff changeset
   295
    val gr = code_depgr thy consts;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 26997
diff changeset
   296
    fun mk_entry (const, (_, (_, parents))) =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 26997
diff changeset
   297
      let
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents: 27609
diff changeset
   298
        val name = Code_Unit.string_of_const thy const;
2b84d34c5d02 restructured and split code serializer module
haftmann
parents: 27609
diff changeset
   299
        val nameparents = map (Code_Unit.string_of_const thy) parents;
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 26997
diff changeset
   300
      in { name = name, ID = name, dir = "", unfold = true,
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 26997
diff changeset
   301
        path = "", parents = nameparents }
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 26997
diff changeset
   302
      end;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 26997
diff changeset
   303
    val prgr = Graph.fold ((fn x => fn xs => xs @ [x]) o mk_entry) gr [];
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 26997
diff changeset
   304
  in Present.display_graph prgr end;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 26997
diff changeset
   305
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 26997
diff changeset
   306
local
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 26997
diff changeset
   307
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 26997
diff changeset
   308
structure P = OuterParse
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 26997
diff changeset
   309
and K = OuterKeyword
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 26997
diff changeset
   310
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents: 27609
diff changeset
   311
fun code_thms_cmd thy = code_thms thy o op @ o Code_Name.read_const_exprs thy;
2b84d34c5d02 restructured and split code serializer module
haftmann
parents: 27609
diff changeset
   312
fun code_deps_cmd thy = code_deps thy o op @ o Code_Name.read_const_exprs thy;
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 26997
diff changeset
   313
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 26997
diff changeset
   314
in
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 26997
diff changeset
   315
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 26997
diff changeset
   316
val _ =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 26997
diff changeset
   317
  OuterSyntax.improper_command "code_thms" "print system of defining equations for code" OuterKeyword.diag
28338
e58ec46d50bc fixed outer syntax
haftmann
parents: 28054
diff changeset
   318
    (Scan.repeat P.term_group
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 26997
diff changeset
   319
      >> (fn cs => Toplevel.no_timing o Toplevel.unknown_theory
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 26997
diff changeset
   320
        o Toplevel.keep ((fn thy => code_thms_cmd thy cs) o Toplevel.theory_of)));
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 26997
diff changeset
   321
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 26997
diff changeset
   322
val _ =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 26997
diff changeset
   323
  OuterSyntax.improper_command "code_deps" "visualize dependencies of defining equations for code" OuterKeyword.diag
28338
e58ec46d50bc fixed outer syntax
haftmann
parents: 28054
diff changeset
   324
    (Scan.repeat P.term_group
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 26997
diff changeset
   325
      >> (fn cs => Toplevel.no_timing o Toplevel.unknown_theory
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 26997
diff changeset
   326
        o Toplevel.keep ((fn thy => code_deps_cmd thy cs) o Toplevel.theory_of)));
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 26997
diff changeset
   327
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 26997
diff changeset
   328
end;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 26997
diff changeset
   329
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   330
end; (*struct*)