src/HOL/Tools/recfun_codegen.ML
author berghofe
Mon Dec 10 15:32:10 2001 +0100 (2001-12-10)
changeset 12447 e752c9aecdec
child 12556 5e2593ef0a44
permissions -rw-r--r--
Code generator for recursive functions.
berghofe@12447
     1
(*  Title:      HOL/recfun_codegen.ML
berghofe@12447
     2
    ID:         $Id$
berghofe@12447
     3
    Author:     Stefan Berghofer, TU Muenchen
berghofe@12447
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
berghofe@12447
     5
berghofe@12447
     6
Code generator for recursive functions.
berghofe@12447
     7
*)
berghofe@12447
     8
berghofe@12447
     9
signature RECFUN_CODEGEN =
berghofe@12447
    10
sig
berghofe@12447
    11
  val add: 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
berghofe@12447
    20
structure RecfunArgs =
berghofe@12447
    21
struct
berghofe@12447
    22
  val name = "HOL/recfun_codegen";
berghofe@12447
    23
  type T = thm list Symtab.table;
berghofe@12447
    24
  val empty = Symtab.empty;
berghofe@12447
    25
  val copy = I;
berghofe@12447
    26
  val prep_ext = I;
berghofe@12447
    27
  val merge = Symtab.merge_multi eq_thm;
berghofe@12447
    28
  fun print _ _ = ();
berghofe@12447
    29
end;
berghofe@12447
    30
berghofe@12447
    31
structure RecfunData = TheoryDataFun(RecfunArgs);
berghofe@12447
    32
berghofe@12447
    33
val prop_of = #prop o rep_thm;
berghofe@12447
    34
val dest_eqn = HOLogic.dest_eq o HOLogic.dest_Trueprop;
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
berghofe@12447
    40
fun add (p as (thy, thm)) =
berghofe@12447
    41
  let
berghofe@12447
    42
    val tsig = Sign.tsig_of (sign_of thy);
berghofe@12447
    43
    val tab = RecfunData.get thy;
berghofe@12447
    44
    val (s, _) = const_of (prop_of thm);
berghofe@12447
    45
berghofe@12447
    46
    val matches = curry
berghofe@12447
    47
      (Pattern.matches tsig o pairself (fst o dest_eqn o prop_of));
berghofe@12447
    48
berghofe@12447
    49
  in (RecfunData.put (Symtab.update ((s,
berghofe@12447
    50
    filter_out (matches thm) (if_none (Symtab.lookup (tab, s)) []) @ [thm]),
berghofe@12447
    51
      tab)) thy, thm)
berghofe@12447
    52
  end handle TERM _ => (warn thm; p) | Pattern.Pattern => (warn thm; p);
berghofe@12447
    53
berghofe@12447
    54
fun get_equations thy (s, T) =
berghofe@12447
    55
  (case Symtab.lookup (RecfunData.get thy, s) of
berghofe@12447
    56
     None => []
berghofe@12447
    57
   | Some thms => filter (fn thm => is_instance thy T
berghofe@12447
    58
       (snd (const_of (prop_of thm)))) thms);
berghofe@12447
    59
berghofe@12447
    60
fun mk_poly_id thy (s, T) = mk_const_id (sign_of thy) s ^
berghofe@12447
    61
  (case get_defn thy s T of
berghofe@12447
    62
     Some (_, Some i) => "_def" ^ string_of_int i
berghofe@12447
    63
   | _ => "");
berghofe@12447
    64
berghofe@12447
    65
exception EQN of string * typ * string;
berghofe@12447
    66
berghofe@12447
    67
fun cycle g (xs, x) =
berghofe@12447
    68
  if x mem xs then xs
berghofe@12447
    69
  else foldl (cycle g) (x :: xs, flat (Graph.find_paths g (x, x)));
berghofe@12447
    70
berghofe@12447
    71
fun add_rec_funs thy gr dep eqs =
berghofe@12447
    72
  let
berghofe@12447
    73
    fun dest_eq t =
berghofe@12447
    74
      (mk_poly_id thy (const_of t), dest_eqn (rename_term t));
berghofe@12447
    75
    val eqs' = map dest_eq eqs;
berghofe@12447
    76
    val (dname, _) :: _ = eqs';
berghofe@12447
    77
    val (s, T) = const_of (hd eqs);
berghofe@12447
    78
berghofe@12447
    79
    fun mk_fundef fname prfx gr [] = (gr, [])
berghofe@12447
    80
      | mk_fundef fname prfx gr ((fname', (lhs, rhs)) :: xs) =
berghofe@12447
    81
      let
berghofe@12447
    82
        val (gr1, pl) = invoke_codegen thy dname false (gr, lhs);
berghofe@12447
    83
        val (gr2, pr) = invoke_codegen thy dname false (gr1, rhs);
berghofe@12447
    84
        val (gr3, rest) = mk_fundef fname' "and " gr2 xs
berghofe@12447
    85
      in
berghofe@12447
    86
        (gr3, Pretty.blk (4, [Pretty.str (if fname = fname' then "  | " else prfx),
berghofe@12447
    87
           pl, Pretty.str " =", Pretty.brk 1, pr]) :: rest)
berghofe@12447
    88
      end;
berghofe@12447
    89
berghofe@12447
    90
    fun put_code fundef = Graph.map_node dname
berghofe@12447
    91
      (K (Some (EQN ("", dummyT, dname)), Pretty.string_of (Pretty.blk (0,
berghofe@12447
    92
      separate Pretty.fbrk fundef @ [Pretty.str ";"])) ^ "\n\n"));
berghofe@12447
    93
berghofe@12447
    94
  in
berghofe@12447
    95
    (case try (Graph.get_node gr) dname of
berghofe@12447
    96
       None =>
berghofe@12447
    97
         let
berghofe@12447
    98
           val gr1 = Graph.add_edge (dname, dep)
berghofe@12447
    99
             (Graph.new_node (dname, (Some (EQN (s, T, "")), "")) gr);
berghofe@12447
   100
           val (gr2, fundef) = mk_fundef "" "fun " gr1 eqs';
berghofe@12447
   101
           val xs = cycle gr2 ([], dname);
berghofe@12447
   102
           val cs = map (fn x => case Graph.get_node gr2 x of
berghofe@12447
   103
               (Some (EQN (s, T, _)), _) => (s, T)
berghofe@12447
   104
             | _ => error ("RecfunCodegen: illegal cyclic dependencies:\n" ^
berghofe@12447
   105
                implode (separate ", " xs))) xs
berghofe@12447
   106
         in (case xs of
berghofe@12447
   107
             [_] => put_code fundef gr2
berghofe@12447
   108
           | _ =>
berghofe@12447
   109
             if not (dep mem xs) then
berghofe@12447
   110
               let
berghofe@12447
   111
                 val eqs'' = map (dest_eq o prop_of)
berghofe@12447
   112
                   (flat (map (get_equations thy) cs));
berghofe@12447
   113
                 val (gr3, fundef') = mk_fundef "" "fun "
berghofe@12447
   114
                   (Graph.add_edge (dname, dep)
berghofe@12447
   115
                     (foldr (uncurry Graph.new_node) (map (fn k =>
berghofe@12447
   116
                       (k, (Some (EQN ("", dummyT, dname)), ""))) xs,
berghofe@12447
   117
                         Graph.del_nodes xs gr2))) eqs''
berghofe@12447
   118
               in put_code fundef' gr3 end
berghofe@12447
   119
             else gr2)
berghofe@12447
   120
         end
berghofe@12447
   121
     | Some (Some (EQN (_, _, s)), _) =>
berghofe@12447
   122
         if s = "" then
berghofe@12447
   123
           if dname = dep then gr else Graph.add_edge (dname, dep) gr
berghofe@12447
   124
         else if s = dep then gr else Graph.add_edge (s, dep) gr)
berghofe@12447
   125
  end;
berghofe@12447
   126
berghofe@12447
   127
fun recfun_codegen thy gr dep brack t = (case strip_comb t of
berghofe@12447
   128
    (Const p, ts) => (case get_equations thy p of
berghofe@12447
   129
       [] => None
berghofe@12447
   130
     | eqns =>
berghofe@12447
   131
        let val (gr', ps) = foldl_map (invoke_codegen thy dep true) (gr, ts)
berghofe@12447
   132
        in
berghofe@12447
   133
          Some (add_rec_funs thy gr' dep (map prop_of eqns),
berghofe@12447
   134
            mk_app brack (Pretty.str (mk_poly_id thy p)) ps)
berghofe@12447
   135
        end)
berghofe@12447
   136
  | _ => None);
berghofe@12447
   137
berghofe@12447
   138
berghofe@12447
   139
val setup =
berghofe@12447
   140
  [RecfunData.init,
berghofe@12447
   141
   add_codegen "recfun" recfun_codegen,
berghofe@12447
   142
   Attrib.add_attributes [("code",
berghofe@12447
   143
     (Attrib.no_args add, Attrib.no_args Attrib.undef_local_attribute),
berghofe@12447
   144
     "declare equations to be used for code generation")]];
berghofe@12447
   145
berghofe@12447
   146
end;