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