src/HOL/Tools/recfun_codegen.ML
author wenzelm
Thu Sep 15 17:16:56 2005 +0200 (2005-09-15)
changeset 17412 e26cb20ef0cc
parent 17261 193b84a70ca4
child 18220 43cf5767f992
permissions -rw-r--r--
TableFun/Symtab: curried lookup and update;
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
wenzelm@17412
    45
      (CodegenData.put (Symtab.update_multi (s, (thm, optmod)) tab) thy, thm)
berghofe@15321
    46
    else (warn thm; p)
berghofe@15321
    47
  end handle TERM _ => (warn thm; p);
berghofe@12447
    48
berghofe@14196
    49
fun del (p as (thy, thm)) =
berghofe@14196
    50
  let
berghofe@14196
    51
    val tab = CodegenData.get thy;
berghofe@14196
    52
    val (s, _) = const_of (prop_of thm);
wenzelm@17412
    53
  in case Symtab.lookup tab s of
skalberg@15531
    54
      NONE => p
wenzelm@17412
    55
    | SOME thms => (CodegenData.put (Symtab.update (s,
wenzelm@17261
    56
        gen_rem (eq_thm o apfst fst) (thms, thm)) tab) thy, thm)
berghofe@14196
    57
  end handle TERM _ => (warn thm; p);
berghofe@14196
    58
berghofe@15321
    59
fun del_redundant thy eqs [] = eqs
berghofe@15321
    60
  | del_redundant thy eqs (eq :: eqs') =
berghofe@15321
    61
    let
berghofe@15321
    62
      val matches = curry
wenzelm@17203
    63
        (Pattern.matches thy o pairself (lhs_of o fst))
berghofe@15321
    64
    in del_redundant thy (eq :: eqs) (filter_out (matches eq) eqs') end;
berghofe@15321
    65
berghofe@16645
    66
fun get_equations thy defs (s, T) =
wenzelm@17412
    67
  (case Symtab.lookup (CodegenData.get thy) s of
berghofe@16645
    68
     NONE => ([], "")
berghofe@16645
    69
   | SOME thms => 
berghofe@16645
    70
       let val thms' = del_redundant thy []
berghofe@16645
    71
         (List.filter (fn (thm, _) => is_instance thy T
berghofe@16645
    72
           (snd (const_of (prop_of thm)))) thms)
berghofe@16645
    73
       in if null thms' then ([], "")
berghofe@16645
    74
         else (preprocess thy (map fst thms'),
berghofe@16645
    75
           case snd (snd (split_last thms')) of
berghofe@16645
    76
               NONE => (case get_defn thy defs s T of
berghofe@16645
    77
                   NONE => thyname_of_const s thy
berghofe@16645
    78
                 | SOME ((_, (thyname, _)), _) => thyname)
berghofe@16645
    79
             | SOME thyname => thyname)
berghofe@16645
    80
       end);
berghofe@12447
    81
berghofe@16645
    82
fun mk_suffix thy defs (s, T) = (case get_defn thy defs s T of
berghofe@17144
    83
  SOME (_, SOME i) => " def" ^ string_of_int i | _ => "");
berghofe@12447
    84
berghofe@12447
    85
exception EQN of string * typ * string;
berghofe@12447
    86
berghofe@12447
    87
fun cycle g (xs, x) =
berghofe@12447
    88
  if x mem xs then xs
berghofe@17144
    89
  else Library.foldl (cycle g) (x :: xs, List.concat (Graph.find_paths (fst g) (x, x)));
berghofe@12447
    90
berghofe@17144
    91
fun add_rec_funs thy defs gr dep eqs module =
berghofe@12447
    92
  let
berghofe@16645
    93
    fun dest_eq t = (fst (const_of t) ^ mk_suffix thy defs (const_of t),
berghofe@16645
    94
      dest_eqn (rename_term t));
berghofe@12447
    95
    val eqs' = map dest_eq eqs;
berghofe@12447
    96
    val (dname, _) :: _ = eqs';
berghofe@12447
    97
    val (s, T) = const_of (hd eqs);
berghofe@12447
    98
berghofe@17144
    99
    fun mk_fundef module fname prfx gr [] = (gr, [])
berghofe@17144
   100
      | mk_fundef module fname prfx gr ((fname', (lhs, rhs)) :: xs) =
berghofe@12447
   101
      let
berghofe@17144
   102
        val (gr1, pl) = invoke_codegen thy defs dname module false (gr, lhs);
berghofe@17144
   103
        val (gr2, pr) = invoke_codegen thy defs dname module false (gr1, rhs);
berghofe@17144
   104
        val (gr3, rest) = mk_fundef module fname' "and " gr2 xs
berghofe@12447
   105
      in
berghofe@12447
   106
        (gr3, Pretty.blk (4, [Pretty.str (if fname = fname' then "  | " else prfx),
berghofe@12447
   107
           pl, Pretty.str " =", Pretty.brk 1, pr]) :: rest)
berghofe@12447
   108
      end;
berghofe@12447
   109
berghofe@17144
   110
    fun put_code module fundef = map_node dname
berghofe@17144
   111
      (K (SOME (EQN ("", dummyT, dname)), module, Pretty.string_of (Pretty.blk (0,
berghofe@12447
   112
      separate Pretty.fbrk fundef @ [Pretty.str ";"])) ^ "\n\n"));
berghofe@12447
   113
berghofe@12447
   114
  in
berghofe@17144
   115
    (case try (get_node gr) dname of
skalberg@15531
   116
       NONE =>
berghofe@12447
   117
         let
berghofe@17144
   118
           val gr1 = add_edge (dname, dep)
berghofe@17144
   119
             (new_node (dname, (SOME (EQN (s, T, "")), module, "")) gr);
berghofe@17144
   120
           val (gr2, fundef) = mk_fundef module "" "fun " gr1 eqs';
berghofe@12447
   121
           val xs = cycle gr2 ([], dname);
berghofe@17144
   122
           val cs = map (fn x => case get_node gr2 x of
berghofe@16645
   123
               (SOME (EQN (s, T, _)), _, _) => (s, T)
berghofe@12447
   124
             | _ => error ("RecfunCodegen: illegal cyclic dependencies:\n" ^
berghofe@12447
   125
                implode (separate ", " xs))) xs
berghofe@12447
   126
         in (case xs of
berghofe@17144
   127
             [_] => (put_code module fundef gr2, module)
berghofe@12447
   128
           | _ =>
berghofe@12447
   129
             if not (dep mem xs) then
berghofe@12447
   130
               let
berghofe@16645
   131
                 val thmss as (_, thyname) :: _ = map (get_equations thy defs) cs;
berghofe@17144
   132
                 val module' = if_library thyname module;
berghofe@16645
   133
                 val eqs'' = map (dest_eq o prop_of) (List.concat (map fst thmss));
berghofe@17144
   134
                 val (gr3, fundef') = mk_fundef module' "" "fun "
berghofe@17144
   135
                   (add_edge (dname, dep)
berghofe@17144
   136
                     (foldr (uncurry new_node) (del_nodes xs gr2)
skalberg@15574
   137
                       (map (fn k =>
berghofe@17144
   138
                         (k, (SOME (EQN ("", dummyT, dname)), module', ""))) xs))) eqs''
berghofe@17144
   139
               in (put_code module' fundef' gr3, module') end
berghofe@17144
   140
             else (gr2, module))
berghofe@12447
   141
         end
berghofe@17144
   142
     | SOME (SOME (EQN (_, _, s)), module', _) =>
berghofe@17144
   143
         (if s = "" then
berghofe@17144
   144
            if dname = dep then gr else add_edge (dname, dep) gr
berghofe@17144
   145
          else if s = dep then gr else add_edge (s, dep) gr,
berghofe@17144
   146
          module'))
berghofe@12447
   147
  end;
berghofe@12447
   148
berghofe@17144
   149
fun recfun_codegen thy defs gr dep module brack t = (case strip_comb t of
berghofe@16645
   150
    (Const (p as (s, T)), ts) => (case (get_equations thy defs p, get_assoc_code thy s T) of
berghofe@16645
   151
       (([], _), _) => NONE
skalberg@15531
   152
     | (_, SOME _) => NONE
berghofe@17144
   153
     | ((eqns, thyname), NONE) =>
berghofe@16645
   154
        let
berghofe@17144
   155
          val module' = if_library thyname module;
berghofe@16645
   156
          val (gr', ps) = foldl_map
berghofe@17144
   157
            (invoke_codegen thy defs dep module true) (gr, ts);
berghofe@17144
   158
          val suffix = mk_suffix thy defs p;
berghofe@17144
   159
          val (gr'', module'') =
berghofe@17144
   160
            add_rec_funs thy defs gr' dep (map prop_of eqns) module';
berghofe@17144
   161
          val (gr''', fname) = mk_const_id module'' (s ^ suffix) gr''
berghofe@12447
   162
        in
berghofe@17144
   163
          SOME (gr''', mk_app brack (Pretty.str (mk_qual_id module fname)) ps)
berghofe@12447
   164
        end)
skalberg@15531
   165
  | _ => NONE);
berghofe@12447
   166
berghofe@12447
   167
berghofe@12447
   168
val setup =
berghofe@12556
   169
  [CodegenData.init,
berghofe@12447
   170
   add_codegen "recfun" recfun_codegen,
berghofe@16645
   171
   add_attribute ""
berghofe@16645
   172
     (   Args.del |-- Scan.succeed del
berghofe@16645
   173
      || Scan.option (Args.$$$ "target" |-- Args.colon |-- Args.name) >> add)];
berghofe@12447
   174
berghofe@12447
   175
end;