src/HOL/Tools/recfun_codegen.ML
author berghofe
Mon Sep 22 16:14:58 2003 +0200 (2003-09-22)
changeset 14196 be5e838f37bd
parent 13105 3d1e7a199bdc
child 14981 e73f8140af78
permissions -rw-r--r--
Added "del" attribute for deleting equations.
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@12556
    20
structure CodegenArgs =
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;
wenzelm@13105
    27
  val merge = Symtab.merge_multi Drule.eq_thm_prop;
berghofe@12447
    28
  fun print _ _ = ();
berghofe@12447
    29
end;
berghofe@12447
    30
berghofe@12556
    31
structure CodegenData = TheoryDataFun(CodegenArgs);
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@12556
    43
    val tab = CodegenData.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@12556
    49
  in (CodegenData.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@14196
    54
fun del (p as (thy, thm)) =
berghofe@14196
    55
  let
berghofe@14196
    56
    val tab = CodegenData.get thy;
berghofe@14196
    57
    val (s, _) = const_of (prop_of thm);
berghofe@14196
    58
  in (CodegenData.put (Symtab.update ((s,
berghofe@14196
    59
    gen_rem eq_thm (if_none (Symtab.lookup (tab, s)) [], thm)),
berghofe@14196
    60
      tab)) thy, thm)
berghofe@14196
    61
  end handle TERM _ => (warn thm; p);
berghofe@14196
    62
berghofe@12447
    63
fun get_equations thy (s, T) =
berghofe@12556
    64
  (case Symtab.lookup (CodegenData.get thy, s) of
berghofe@12447
    65
     None => []
berghofe@12447
    66
   | Some thms => filter (fn thm => is_instance thy T
berghofe@12447
    67
       (snd (const_of (prop_of thm)))) thms);
berghofe@12447
    68
berghofe@12447
    69
fun mk_poly_id thy (s, T) = mk_const_id (sign_of thy) s ^
berghofe@12447
    70
  (case get_defn thy s T of
berghofe@12447
    71
     Some (_, Some i) => "_def" ^ string_of_int i
berghofe@12447
    72
   | _ => "");
berghofe@12447
    73
berghofe@12447
    74
exception EQN of string * typ * string;
berghofe@12447
    75
berghofe@12447
    76
fun cycle g (xs, x) =
berghofe@12447
    77
  if x mem xs then xs
berghofe@12447
    78
  else foldl (cycle g) (x :: xs, flat (Graph.find_paths g (x, x)));
berghofe@12447
    79
berghofe@12447
    80
fun add_rec_funs thy gr dep eqs =
berghofe@12447
    81
  let
berghofe@12447
    82
    fun dest_eq t =
berghofe@12447
    83
      (mk_poly_id thy (const_of t), dest_eqn (rename_term t));
berghofe@12447
    84
    val eqs' = map dest_eq eqs;
berghofe@12447
    85
    val (dname, _) :: _ = eqs';
berghofe@12447
    86
    val (s, T) = const_of (hd eqs);
berghofe@12447
    87
berghofe@12447
    88
    fun mk_fundef fname prfx gr [] = (gr, [])
berghofe@12447
    89
      | mk_fundef fname prfx gr ((fname', (lhs, rhs)) :: xs) =
berghofe@12447
    90
      let
berghofe@12447
    91
        val (gr1, pl) = invoke_codegen thy dname false (gr, lhs);
berghofe@12447
    92
        val (gr2, pr) = invoke_codegen thy dname false (gr1, rhs);
berghofe@12447
    93
        val (gr3, rest) = mk_fundef fname' "and " gr2 xs
berghofe@12447
    94
      in
berghofe@12447
    95
        (gr3, Pretty.blk (4, [Pretty.str (if fname = fname' then "  | " else prfx),
berghofe@12447
    96
           pl, Pretty.str " =", Pretty.brk 1, pr]) :: rest)
berghofe@12447
    97
      end;
berghofe@12447
    98
berghofe@12447
    99
    fun put_code fundef = Graph.map_node dname
berghofe@12447
   100
      (K (Some (EQN ("", dummyT, dname)), Pretty.string_of (Pretty.blk (0,
berghofe@12447
   101
      separate Pretty.fbrk fundef @ [Pretty.str ";"])) ^ "\n\n"));
berghofe@12447
   102
berghofe@12447
   103
  in
berghofe@12447
   104
    (case try (Graph.get_node gr) dname of
berghofe@12447
   105
       None =>
berghofe@12447
   106
         let
berghofe@12447
   107
           val gr1 = Graph.add_edge (dname, dep)
berghofe@12447
   108
             (Graph.new_node (dname, (Some (EQN (s, T, "")), "")) gr);
berghofe@12447
   109
           val (gr2, fundef) = mk_fundef "" "fun " gr1 eqs';
berghofe@12447
   110
           val xs = cycle gr2 ([], dname);
berghofe@12447
   111
           val cs = map (fn x => case Graph.get_node gr2 x of
berghofe@12447
   112
               (Some (EQN (s, T, _)), _) => (s, T)
berghofe@12447
   113
             | _ => error ("RecfunCodegen: illegal cyclic dependencies:\n" ^
berghofe@12447
   114
                implode (separate ", " xs))) xs
berghofe@12447
   115
         in (case xs of
berghofe@12447
   116
             [_] => put_code fundef gr2
berghofe@12447
   117
           | _ =>
berghofe@12447
   118
             if not (dep mem xs) then
berghofe@12447
   119
               let
berghofe@12447
   120
                 val eqs'' = map (dest_eq o prop_of)
berghofe@12447
   121
                   (flat (map (get_equations thy) cs));
berghofe@12447
   122
                 val (gr3, fundef') = mk_fundef "" "fun "
berghofe@12447
   123
                   (Graph.add_edge (dname, dep)
berghofe@12447
   124
                     (foldr (uncurry Graph.new_node) (map (fn k =>
berghofe@12447
   125
                       (k, (Some (EQN ("", dummyT, dname)), ""))) xs,
berghofe@12447
   126
                         Graph.del_nodes xs gr2))) eqs''
berghofe@12447
   127
               in put_code fundef' gr3 end
berghofe@12447
   128
             else gr2)
berghofe@12447
   129
         end
berghofe@12447
   130
     | Some (Some (EQN (_, _, s)), _) =>
berghofe@12447
   131
         if s = "" then
berghofe@12447
   132
           if dname = dep then gr else Graph.add_edge (dname, dep) gr
berghofe@12447
   133
         else if s = dep then gr else Graph.add_edge (s, dep) gr)
berghofe@12447
   134
  end;
berghofe@12447
   135
berghofe@12447
   136
fun recfun_codegen thy gr dep brack t = (case strip_comb t of
berghofe@12447
   137
    (Const p, ts) => (case get_equations thy p of
berghofe@12447
   138
       [] => None
berghofe@12447
   139
     | eqns =>
berghofe@12447
   140
        let val (gr', ps) = foldl_map (invoke_codegen thy dep true) (gr, ts)
berghofe@12447
   141
        in
berghofe@12447
   142
          Some (add_rec_funs thy gr' dep (map prop_of eqns),
berghofe@12447
   143
            mk_app brack (Pretty.str (mk_poly_id thy p)) ps)
berghofe@12447
   144
        end)
berghofe@12447
   145
  | _ => None);
berghofe@12447
   146
berghofe@12447
   147
berghofe@12447
   148
val setup =
berghofe@12556
   149
  [CodegenData.init,
berghofe@12447
   150
   add_codegen "recfun" recfun_codegen,
berghofe@14196
   151
   add_attribute "" (Args.del |-- Scan.succeed del || Scan.succeed add)];
berghofe@12447
   152
berghofe@12447
   153
end;