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