src/HOL/Tools/recfun_codegen.ML
author skalberg
Sun Feb 13 17:15:14 2005 +0100 (2005-02-13)
changeset 15531 08c8dad8e399
parent 15321 694f9d3ce90d
child 15570 8d8c70b41bab
permissions -rw-r--r--
Deleted Library.option type.
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
berghofe@12556
    19
structure CodegenArgs =
berghofe@12447
    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;
berghofe@12447
    25
  val prep_ext = I;
berghofe@15321
    26
  val merge = Symtab.merge_multi' Drule.eq_thm_prop;
berghofe@12447
    27
  fun print _ _ = ();
berghofe@12447
    28
end;
berghofe@12447
    29
berghofe@12556
    30
structure CodegenData = TheoryDataFun(CodegenArgs);
berghofe@12447
    31
berghofe@12447
    32
val prop_of = #prop o rep_thm;
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
berghofe@12447
    40
fun add (p as (thy, thm)) =
berghofe@12447
    41
  let
berghofe@12556
    42
    val tab = CodegenData.get thy;
berghofe@12447
    43
    val (s, _) = const_of (prop_of thm);
berghofe@15321
    44
  in
berghofe@15321
    45
    if Pattern.pattern (lhs_of thm) then
berghofe@15321
    46
      (CodegenData.put (Symtab.update ((s,
berghofe@15321
    47
        thm :: if_none (Symtab.lookup (tab, s)) []), tab)) thy, thm)
berghofe@15321
    48
    else (warn thm; p)
berghofe@15321
    49
  end handle TERM _ => (warn thm; p);
berghofe@12447
    50
berghofe@14196
    51
fun del (p as (thy, thm)) =
berghofe@14196
    52
  let
berghofe@14196
    53
    val tab = CodegenData.get thy;
berghofe@14196
    54
    val (s, _) = const_of (prop_of thm);
berghofe@15321
    55
  in case Symtab.lookup (tab, s) of
skalberg@15531
    56
      NONE => p
skalberg@15531
    57
    | SOME thms => (CodegenData.put (Symtab.update ((s,
berghofe@15321
    58
        gen_rem eq_thm (thms, thm)), tab)) thy, thm)
berghofe@14196
    59
  end handle TERM _ => (warn thm; p);
berghofe@14196
    60
berghofe@15321
    61
fun del_redundant thy eqs [] = eqs
berghofe@15321
    62
  | del_redundant thy eqs (eq :: eqs') =
berghofe@15321
    63
    let
berghofe@15321
    64
      val tsig = Sign.tsig_of (sign_of thy);
berghofe@15321
    65
      val matches = curry
berghofe@15321
    66
        (Pattern.matches tsig o pairself lhs_of)
berghofe@15321
    67
    in del_redundant thy (eq :: eqs) (filter_out (matches eq) eqs') end;
berghofe@15321
    68
berghofe@12447
    69
fun get_equations thy (s, T) =
berghofe@12556
    70
  (case Symtab.lookup (CodegenData.get thy, s) of
skalberg@15531
    71
     NONE => []
skalberg@15531
    72
   | SOME thms => preprocess thy (del_redundant thy []
berghofe@15321
    73
       (filter (fn thm => is_instance thy T
berghofe@15321
    74
         (snd (const_of (prop_of thm)))) thms)));
berghofe@12447
    75
berghofe@12447
    76
fun mk_poly_id thy (s, T) = mk_const_id (sign_of thy) s ^
berghofe@12447
    77
  (case get_defn thy s T of
skalberg@15531
    78
     SOME (_, SOME i) => "_def" ^ string_of_int i
berghofe@12447
    79
   | _ => "");
berghofe@12447
    80
berghofe@12447
    81
exception EQN of string * typ * string;
berghofe@12447
    82
berghofe@12447
    83
fun cycle g (xs, x) =
berghofe@12447
    84
  if x mem xs then xs
berghofe@12447
    85
  else foldl (cycle g) (x :: xs, flat (Graph.find_paths g (x, x)));
berghofe@12447
    86
berghofe@12447
    87
fun add_rec_funs thy gr dep eqs =
berghofe@12447
    88
  let
berghofe@12447
    89
    fun dest_eq t =
berghofe@12447
    90
      (mk_poly_id thy (const_of t), dest_eqn (rename_term t));
berghofe@12447
    91
    val eqs' = map dest_eq eqs;
berghofe@12447
    92
    val (dname, _) :: _ = eqs';
berghofe@12447
    93
    val (s, T) = const_of (hd eqs);
berghofe@12447
    94
berghofe@12447
    95
    fun mk_fundef fname prfx gr [] = (gr, [])
berghofe@12447
    96
      | mk_fundef fname prfx gr ((fname', (lhs, rhs)) :: xs) =
berghofe@12447
    97
      let
berghofe@12447
    98
        val (gr1, pl) = invoke_codegen thy dname false (gr, lhs);
berghofe@12447
    99
        val (gr2, pr) = invoke_codegen thy dname false (gr1, rhs);
berghofe@12447
   100
        val (gr3, rest) = mk_fundef fname' "and " gr2 xs
berghofe@12447
   101
      in
berghofe@12447
   102
        (gr3, Pretty.blk (4, [Pretty.str (if fname = fname' then "  | " else prfx),
berghofe@12447
   103
           pl, Pretty.str " =", Pretty.brk 1, pr]) :: rest)
berghofe@12447
   104
      end;
berghofe@12447
   105
berghofe@12447
   106
    fun put_code fundef = Graph.map_node dname
skalberg@15531
   107
      (K (SOME (EQN ("", dummyT, dname)), Pretty.string_of (Pretty.blk (0,
berghofe@12447
   108
      separate Pretty.fbrk fundef @ [Pretty.str ";"])) ^ "\n\n"));
berghofe@12447
   109
berghofe@12447
   110
  in
berghofe@12447
   111
    (case try (Graph.get_node gr) dname of
skalberg@15531
   112
       NONE =>
berghofe@12447
   113
         let
berghofe@12447
   114
           val gr1 = Graph.add_edge (dname, dep)
skalberg@15531
   115
             (Graph.new_node (dname, (SOME (EQN (s, T, "")), "")) gr);
berghofe@12447
   116
           val (gr2, fundef) = mk_fundef "" "fun " gr1 eqs';
berghofe@12447
   117
           val xs = cycle gr2 ([], dname);
berghofe@12447
   118
           val cs = map (fn x => case Graph.get_node gr2 x of
skalberg@15531
   119
               (SOME (EQN (s, T, _)), _) => (s, T)
berghofe@12447
   120
             | _ => error ("RecfunCodegen: illegal cyclic dependencies:\n" ^
berghofe@12447
   121
                implode (separate ", " xs))) xs
berghofe@12447
   122
         in (case xs of
berghofe@12447
   123
             [_] => put_code fundef gr2
berghofe@12447
   124
           | _ =>
berghofe@12447
   125
             if not (dep mem xs) then
berghofe@12447
   126
               let
berghofe@12447
   127
                 val eqs'' = map (dest_eq o prop_of)
berghofe@12447
   128
                   (flat (map (get_equations thy) cs));
berghofe@12447
   129
                 val (gr3, fundef') = mk_fundef "" "fun "
berghofe@12447
   130
                   (Graph.add_edge (dname, dep)
berghofe@12447
   131
                     (foldr (uncurry Graph.new_node) (map (fn k =>
skalberg@15531
   132
                       (k, (SOME (EQN ("", dummyT, dname)), ""))) xs,
berghofe@12447
   133
                         Graph.del_nodes xs gr2))) eqs''
berghofe@12447
   134
               in put_code fundef' gr3 end
berghofe@12447
   135
             else gr2)
berghofe@12447
   136
         end
skalberg@15531
   137
     | SOME (SOME (EQN (_, _, s)), _) =>
berghofe@12447
   138
         if s = "" then
berghofe@12447
   139
           if dname = dep then gr else Graph.add_edge (dname, dep) gr
berghofe@12447
   140
         else if s = dep then gr else Graph.add_edge (s, dep) gr)
berghofe@12447
   141
  end;
berghofe@12447
   142
berghofe@12447
   143
fun recfun_codegen thy gr dep brack t = (case strip_comb t of
berghofe@15257
   144
    (Const (p as (s, T)), ts) => (case (get_equations thy p, get_assoc_code thy s T) of
skalberg@15531
   145
       ([], _) => NONE
skalberg@15531
   146
     | (_, SOME _) => NONE
skalberg@15531
   147
     | (eqns, NONE) =>
berghofe@12447
   148
        let val (gr', ps) = foldl_map (invoke_codegen thy dep true) (gr, ts)
berghofe@12447
   149
        in
skalberg@15531
   150
          SOME (add_rec_funs thy gr' dep (map prop_of eqns),
berghofe@12447
   151
            mk_app brack (Pretty.str (mk_poly_id thy p)) ps)
berghofe@12447
   152
        end)
skalberg@15531
   153
  | _ => NONE);
berghofe@12447
   154
berghofe@12447
   155
berghofe@12447
   156
val setup =
berghofe@12556
   157
  [CodegenData.init,
berghofe@12447
   158
   add_codegen "recfun" recfun_codegen,
berghofe@14196
   159
   add_attribute "" (Args.del |-- Scan.succeed del || Scan.succeed add)];
berghofe@12447
   160
berghofe@12447
   161
end;