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