src/HOL/Tools/recfun_codegen.ML
author haftmann
Thu Apr 06 16:09:54 2006 +0200 (2006-04-06)
changeset 19344 b4e00947c8a1
parent 19202 0b9eb4b0ad98
child 19575 2d9940cd52d3
permissions -rw-r--r--
added hook for codegen_theorems.ML
berghofe@12447
     1
(*  Title:      HOL/recfun_codegen.ML
berghofe@12447
     2
    ID:         $Id$
berghofe@12447
     3
    Author:     Stefan Berghofer, TU Muenchen
berghofe@12447
     4
berghofe@12447
     5
Code generator for recursive functions.
berghofe@12447
     6
*)
berghofe@12447
     7
berghofe@12447
     8
signature RECFUN_CODEGEN =
berghofe@12447
     9
sig
wenzelm@18728
    10
  val add: string option -> attribute
wenzelm@18728
    11
  val del: attribute
haftmann@18702
    12
  val get_thm_equations: theory -> string * typ -> (thm list * typ) option
wenzelm@18708
    13
  val setup: theory -> theory
berghofe@12447
    14
end;
berghofe@12447
    15
berghofe@12447
    16
structure RecfunCodegen : RECFUN_CODEGEN =
berghofe@12447
    17
struct
berghofe@12447
    18
berghofe@12447
    19
open Codegen;
berghofe@12447
    20
wenzelm@16424
    21
structure CodegenData = TheoryDataFun
wenzelm@16424
    22
(struct
berghofe@12447
    23
  val name = "HOL/recfun_codegen";
berghofe@16645
    24
  type T = (thm * string option) list Symtab.table;
berghofe@12447
    25
  val empty = Symtab.empty;
berghofe@12447
    26
  val copy = I;
wenzelm@16424
    27
  val extend = I;
wenzelm@18931
    28
  fun merge _ = Symtab.merge_list (Drule.eq_thm_prop o pairself fst);
berghofe@12447
    29
  fun print _ _ = ();
wenzelm@16424
    30
end);
berghofe@12447
    31
berghofe@12447
    32
berghofe@12447
    33
val dest_eqn = HOLogic.dest_eq o HOLogic.dest_Trueprop;
berghofe@15321
    34
val lhs_of = fst o dest_eqn o prop_of;
berghofe@12447
    35
val const_of = dest_Const o head_of o fst o dest_eqn;
berghofe@12447
    36
berghofe@12447
    37
fun warn thm = warning ("RecfunCodegen: Not a proper equation:\n" ^
berghofe@12447
    38
  string_of_thm thm);
berghofe@12447
    39
haftmann@19344
    40
fun add optmod = Thm.declaration_attribute (fn thm => Context.map_theory
berghofe@12447
    41
  let
haftmann@19344
    42
    fun add thm =
haftmann@19344
    43
      if Pattern.pattern (lhs_of thm) then
haftmann@19344
    44
        CodegenData.map
haftmann@19344
    45
          (Symtab.update_list ((fst o const_of o prop_of) thm, (thm, optmod)))
haftmann@19344
    46
      else tap (fn _ => warn thm)
haftmann@19344
    47
      handle TERM _ => tap (fn _ => warn thm);
berghofe@15321
    48
  in
haftmann@19344
    49
    add thm
haftmann@19344
    50
    #> CodegenTheorems.add_fun thm
haftmann@19344
    51
  end);
berghofe@12447
    52
haftmann@19344
    53
fun del_thm thm thy =
berghofe@14196
    54
  let
berghofe@14196
    55
    val tab = CodegenData.get thy;
berghofe@14196
    56
    val (s, _) = const_of (prop_of thm);
wenzelm@17412
    57
  in case Symtab.lookup tab s of
wenzelm@18728
    58
      NONE => thy
wenzelm@18728
    59
    | SOME thms =>
wenzelm@18728
    60
        CodegenData.put (Symtab.update (s, gen_rem (eq_thm o apfst fst) (thms, thm)) tab) thy
haftmann@19344
    61
  end handle TERM _ => (warn thm; thy);
haftmann@19344
    62
haftmann@19344
    63
val del = Thm.declaration_attribute
haftmann@19344
    64
  (fn thm => Context.map_theory (del_thm thm #> CodegenTheorems.del_def thm))
berghofe@14196
    65
berghofe@15321
    66
fun del_redundant thy eqs [] = eqs
berghofe@15321
    67
  | del_redundant thy eqs (eq :: eqs') =
berghofe@15321
    68
    let
berghofe@15321
    69
      val matches = curry
wenzelm@17203
    70
        (Pattern.matches thy o pairself (lhs_of o fst))
berghofe@15321
    71
    in del_redundant thy (eq :: eqs) (filter_out (matches eq) eqs') end;
berghofe@15321
    72
berghofe@16645
    73
fun get_equations thy defs (s, T) =
wenzelm@17412
    74
  (case Symtab.lookup (CodegenData.get thy) s of
berghofe@16645
    75
     NONE => ([], "")
berghofe@16645
    76
   | SOME thms => 
berghofe@16645
    77
       let val thms' = del_redundant thy []
berghofe@16645
    78
         (List.filter (fn (thm, _) => is_instance thy T
berghofe@16645
    79
           (snd (const_of (prop_of thm)))) thms)
berghofe@16645
    80
       in if null thms' then ([], "")
berghofe@16645
    81
         else (preprocess thy (map fst thms'),
berghofe@16645
    82
           case snd (snd (split_last thms')) of
berghofe@16645
    83
               NONE => (case get_defn thy defs s T of
berghofe@16645
    84
                   NONE => thyname_of_const s thy
berghofe@16645
    85
                 | SOME ((_, (thyname, _)), _) => thyname)
berghofe@16645
    86
             | SOME thyname => thyname)
berghofe@16645
    87
       end);
berghofe@12447
    88
haftmann@18702
    89
fun get_thm_equations thy (c, ty) =
haftmann@18702
    90
  Symtab.lookup (CodegenData.get thy) c
haftmann@18702
    91
  |> Option.map (fn thms => 
haftmann@18702
    92
       List.filter (fn (thm, _) => is_instance thy ty ((snd o const_of o prop_of) thm)) thms
haftmann@18702
    93
       |> del_redundant thy [])
haftmann@18702
    94
  |> Option.mapPartial (fn thms => if null thms then NONE else SOME thms)
haftmann@18702
    95
  |> Option.map (fn thms =>
haftmann@18702
    96
       (preprocess thy (map fst thms),
haftmann@18702
    97
          (snd o const_of o prop_of o fst o hd) thms))
haftmann@18702
    98
  |> (Option.map o apfst o map) (fn thm => thm RS HOL.eq_reflection);
haftmann@18702
    99
berghofe@16645
   100
fun mk_suffix thy defs (s, T) = (case get_defn thy defs s T of
berghofe@17144
   101
  SOME (_, SOME i) => " def" ^ string_of_int i | _ => "");
berghofe@12447
   102
berghofe@12447
   103
exception EQN of string * typ * string;
berghofe@12447
   104
berghofe@12447
   105
fun cycle g (xs, x) =
berghofe@12447
   106
  if x mem xs then xs
berghofe@17144
   107
  else Library.foldl (cycle g) (x :: xs, List.concat (Graph.find_paths (fst g) (x, x)));
berghofe@12447
   108
berghofe@17144
   109
fun add_rec_funs thy defs gr dep eqs module =
berghofe@12447
   110
  let
berghofe@16645
   111
    fun dest_eq t = (fst (const_of t) ^ mk_suffix thy defs (const_of t),
berghofe@16645
   112
      dest_eqn (rename_term t));
berghofe@12447
   113
    val eqs' = map dest_eq eqs;
berghofe@12447
   114
    val (dname, _) :: _ = eqs';
berghofe@12447
   115
    val (s, T) = const_of (hd eqs);
berghofe@12447
   116
berghofe@17144
   117
    fun mk_fundef module fname prfx gr [] = (gr, [])
berghofe@17144
   118
      | mk_fundef module fname prfx gr ((fname', (lhs, rhs)) :: xs) =
berghofe@12447
   119
      let
berghofe@17144
   120
        val (gr1, pl) = invoke_codegen thy defs dname module false (gr, lhs);
berghofe@17144
   121
        val (gr2, pr) = invoke_codegen thy defs dname module false (gr1, rhs);
berghofe@17144
   122
        val (gr3, rest) = mk_fundef module fname' "and " gr2 xs
berghofe@12447
   123
      in
berghofe@12447
   124
        (gr3, Pretty.blk (4, [Pretty.str (if fname = fname' then "  | " else prfx),
berghofe@12447
   125
           pl, Pretty.str " =", Pretty.brk 1, pr]) :: rest)
berghofe@12447
   126
      end;
berghofe@12447
   127
berghofe@17144
   128
    fun put_code module fundef = map_node dname
berghofe@17144
   129
      (K (SOME (EQN ("", dummyT, dname)), module, Pretty.string_of (Pretty.blk (0,
berghofe@12447
   130
      separate Pretty.fbrk fundef @ [Pretty.str ";"])) ^ "\n\n"));
berghofe@12447
   131
berghofe@12447
   132
  in
berghofe@17144
   133
    (case try (get_node gr) dname of
skalberg@15531
   134
       NONE =>
berghofe@12447
   135
         let
berghofe@17144
   136
           val gr1 = add_edge (dname, dep)
berghofe@17144
   137
             (new_node (dname, (SOME (EQN (s, T, "")), module, "")) gr);
berghofe@17144
   138
           val (gr2, fundef) = mk_fundef module "" "fun " gr1 eqs';
berghofe@12447
   139
           val xs = cycle gr2 ([], dname);
berghofe@17144
   140
           val cs = map (fn x => case get_node gr2 x of
berghofe@16645
   141
               (SOME (EQN (s, T, _)), _, _) => (s, T)
berghofe@12447
   142
             | _ => error ("RecfunCodegen: illegal cyclic dependencies:\n" ^
berghofe@12447
   143
                implode (separate ", " xs))) xs
berghofe@12447
   144
         in (case xs of
berghofe@17144
   145
             [_] => (put_code module fundef gr2, module)
berghofe@12447
   146
           | _ =>
berghofe@12447
   147
             if not (dep mem xs) then
berghofe@12447
   148
               let
berghofe@16645
   149
                 val thmss as (_, thyname) :: _ = map (get_equations thy defs) cs;
berghofe@17144
   150
                 val module' = if_library thyname module;
berghofe@16645
   151
                 val eqs'' = map (dest_eq o prop_of) (List.concat (map fst thmss));
berghofe@17144
   152
                 val (gr3, fundef') = mk_fundef module' "" "fun "
berghofe@17144
   153
                   (add_edge (dname, dep)
berghofe@17144
   154
                     (foldr (uncurry new_node) (del_nodes xs gr2)
skalberg@15574
   155
                       (map (fn k =>
berghofe@17144
   156
                         (k, (SOME (EQN ("", dummyT, dname)), module', ""))) xs))) eqs''
berghofe@17144
   157
               in (put_code module' fundef' gr3, module') end
berghofe@17144
   158
             else (gr2, module))
berghofe@12447
   159
         end
berghofe@17144
   160
     | SOME (SOME (EQN (_, _, s)), module', _) =>
berghofe@17144
   161
         (if s = "" then
berghofe@17144
   162
            if dname = dep then gr else add_edge (dname, dep) gr
berghofe@17144
   163
          else if s = dep then gr else add_edge (s, dep) gr,
berghofe@17144
   164
          module'))
berghofe@12447
   165
  end;
berghofe@12447
   166
berghofe@17144
   167
fun recfun_codegen thy defs gr dep module brack t = (case strip_comb t of
berghofe@16645
   168
    (Const (p as (s, T)), ts) => (case (get_equations thy defs p, get_assoc_code thy s T) of
berghofe@16645
   169
       (([], _), _) => NONE
skalberg@15531
   170
     | (_, SOME _) => NONE
berghofe@17144
   171
     | ((eqns, thyname), NONE) =>
berghofe@16645
   172
        let
berghofe@17144
   173
          val module' = if_library thyname module;
berghofe@16645
   174
          val (gr', ps) = foldl_map
berghofe@17144
   175
            (invoke_codegen thy defs dep module true) (gr, ts);
berghofe@17144
   176
          val suffix = mk_suffix thy defs p;
berghofe@17144
   177
          val (gr'', module'') =
berghofe@17144
   178
            add_rec_funs thy defs gr' dep (map prop_of eqns) module';
berghofe@17144
   179
          val (gr''', fname) = mk_const_id module'' (s ^ suffix) gr''
berghofe@12447
   180
        in
berghofe@17144
   181
          SOME (gr''', mk_app brack (Pretty.str (mk_qual_id module fname)) ps)
berghofe@12447
   182
        end)
skalberg@15531
   183
  | _ => NONE);
berghofe@12447
   184
berghofe@12447
   185
wenzelm@18708
   186
val setup =
wenzelm@18708
   187
  CodegenData.init #>
wenzelm@18708
   188
  add_codegen "recfun" recfun_codegen #>
haftmann@18220
   189
  add_attribute ""
haftmann@19344
   190
    (Args.del |-- Scan.succeed del
haftmann@19344
   191
     || Scan.option (Args.$$$ "target" |-- Args.colon |-- Args.name) >> add);
berghofe@12447
   192
berghofe@12447
   193
end;