src/HOL/Tools/recfun_codegen.ML
changeset 12447 e752c9aecdec
child 12556 5e2593ef0a44
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Tools/recfun_codegen.ML	Mon Dec 10 15:32:10 2001 +0100
     1.3 @@ -0,0 +1,146 @@
     1.4 +(*  Title:      HOL/recfun_codegen.ML
     1.5 +    ID:         $Id$
     1.6 +    Author:     Stefan Berghofer, TU Muenchen
     1.7 +    License:    GPL (GNU GENERAL PUBLIC LICENSE)
     1.8 +
     1.9 +Code generator for recursive functions.
    1.10 +*)
    1.11 +
    1.12 +signature RECFUN_CODEGEN =
    1.13 +sig
    1.14 +  val add: theory attribute
    1.15 +  val setup: (theory -> theory) list
    1.16 +end;
    1.17 +
    1.18 +structure RecfunCodegen : RECFUN_CODEGEN =
    1.19 +struct
    1.20 +
    1.21 +open Codegen;
    1.22 +
    1.23 +structure RecfunArgs =
    1.24 +struct
    1.25 +  val name = "HOL/recfun_codegen";
    1.26 +  type T = thm list Symtab.table;
    1.27 +  val empty = Symtab.empty;
    1.28 +  val copy = I;
    1.29 +  val prep_ext = I;
    1.30 +  val merge = Symtab.merge_multi eq_thm;
    1.31 +  fun print _ _ = ();
    1.32 +end;
    1.33 +
    1.34 +structure RecfunData = TheoryDataFun(RecfunArgs);
    1.35 +
    1.36 +val prop_of = #prop o rep_thm;
    1.37 +val dest_eqn = HOLogic.dest_eq o HOLogic.dest_Trueprop;
    1.38 +val const_of = dest_Const o head_of o fst o dest_eqn;
    1.39 +
    1.40 +fun warn thm = warning ("RecfunCodegen: Not a proper equation:\n" ^
    1.41 +  string_of_thm thm);
    1.42 +
    1.43 +fun add (p as (thy, thm)) =
    1.44 +  let
    1.45 +    val tsig = Sign.tsig_of (sign_of thy);
    1.46 +    val tab = RecfunData.get thy;
    1.47 +    val (s, _) = const_of (prop_of thm);
    1.48 +
    1.49 +    val matches = curry
    1.50 +      (Pattern.matches tsig o pairself (fst o dest_eqn o prop_of));
    1.51 +
    1.52 +  in (RecfunData.put (Symtab.update ((s,
    1.53 +    filter_out (matches thm) (if_none (Symtab.lookup (tab, s)) []) @ [thm]),
    1.54 +      tab)) thy, thm)
    1.55 +  end handle TERM _ => (warn thm; p) | Pattern.Pattern => (warn thm; p);
    1.56 +
    1.57 +fun get_equations thy (s, T) =
    1.58 +  (case Symtab.lookup (RecfunData.get thy, s) of
    1.59 +     None => []
    1.60 +   | Some thms => filter (fn thm => is_instance thy T
    1.61 +       (snd (const_of (prop_of thm)))) thms);
    1.62 +
    1.63 +fun mk_poly_id thy (s, T) = mk_const_id (sign_of thy) s ^
    1.64 +  (case get_defn thy s T of
    1.65 +     Some (_, Some i) => "_def" ^ string_of_int i
    1.66 +   | _ => "");
    1.67 +
    1.68 +exception EQN of string * typ * string;
    1.69 +
    1.70 +fun cycle g (xs, x) =
    1.71 +  if x mem xs then xs
    1.72 +  else foldl (cycle g) (x :: xs, flat (Graph.find_paths g (x, x)));
    1.73 +
    1.74 +fun add_rec_funs thy gr dep eqs =
    1.75 +  let
    1.76 +    fun dest_eq t =
    1.77 +      (mk_poly_id thy (const_of t), dest_eqn (rename_term t));
    1.78 +    val eqs' = map dest_eq eqs;
    1.79 +    val (dname, _) :: _ = eqs';
    1.80 +    val (s, T) = const_of (hd eqs);
    1.81 +
    1.82 +    fun mk_fundef fname prfx gr [] = (gr, [])
    1.83 +      | mk_fundef fname prfx gr ((fname', (lhs, rhs)) :: xs) =
    1.84 +      let
    1.85 +        val (gr1, pl) = invoke_codegen thy dname false (gr, lhs);
    1.86 +        val (gr2, pr) = invoke_codegen thy dname false (gr1, rhs);
    1.87 +        val (gr3, rest) = mk_fundef fname' "and " gr2 xs
    1.88 +      in
    1.89 +        (gr3, Pretty.blk (4, [Pretty.str (if fname = fname' then "  | " else prfx),
    1.90 +           pl, Pretty.str " =", Pretty.brk 1, pr]) :: rest)
    1.91 +      end;
    1.92 +
    1.93 +    fun put_code fundef = Graph.map_node dname
    1.94 +      (K (Some (EQN ("", dummyT, dname)), Pretty.string_of (Pretty.blk (0,
    1.95 +      separate Pretty.fbrk fundef @ [Pretty.str ";"])) ^ "\n\n"));
    1.96 +
    1.97 +  in
    1.98 +    (case try (Graph.get_node gr) dname of
    1.99 +       None =>
   1.100 +         let
   1.101 +           val gr1 = Graph.add_edge (dname, dep)
   1.102 +             (Graph.new_node (dname, (Some (EQN (s, T, "")), "")) gr);
   1.103 +           val (gr2, fundef) = mk_fundef "" "fun " gr1 eqs';
   1.104 +           val xs = cycle gr2 ([], dname);
   1.105 +           val cs = map (fn x => case Graph.get_node gr2 x of
   1.106 +               (Some (EQN (s, T, _)), _) => (s, T)
   1.107 +             | _ => error ("RecfunCodegen: illegal cyclic dependencies:\n" ^
   1.108 +                implode (separate ", " xs))) xs
   1.109 +         in (case xs of
   1.110 +             [_] => put_code fundef gr2
   1.111 +           | _ =>
   1.112 +             if not (dep mem xs) then
   1.113 +               let
   1.114 +                 val eqs'' = map (dest_eq o prop_of)
   1.115 +                   (flat (map (get_equations thy) cs));
   1.116 +                 val (gr3, fundef') = mk_fundef "" "fun "
   1.117 +                   (Graph.add_edge (dname, dep)
   1.118 +                     (foldr (uncurry Graph.new_node) (map (fn k =>
   1.119 +                       (k, (Some (EQN ("", dummyT, dname)), ""))) xs,
   1.120 +                         Graph.del_nodes xs gr2))) eqs''
   1.121 +               in put_code fundef' gr3 end
   1.122 +             else gr2)
   1.123 +         end
   1.124 +     | Some (Some (EQN (_, _, s)), _) =>
   1.125 +         if s = "" then
   1.126 +           if dname = dep then gr else Graph.add_edge (dname, dep) gr
   1.127 +         else if s = dep then gr else Graph.add_edge (s, dep) gr)
   1.128 +  end;
   1.129 +
   1.130 +fun recfun_codegen thy gr dep brack t = (case strip_comb t of
   1.131 +    (Const p, ts) => (case get_equations thy p of
   1.132 +       [] => None
   1.133 +     | eqns =>
   1.134 +        let val (gr', ps) = foldl_map (invoke_codegen thy dep true) (gr, ts)
   1.135 +        in
   1.136 +          Some (add_rec_funs thy gr' dep (map prop_of eqns),
   1.137 +            mk_app brack (Pretty.str (mk_poly_id thy p)) ps)
   1.138 +        end)
   1.139 +  | _ => None);
   1.140 +
   1.141 +
   1.142 +val setup =
   1.143 +  [RecfunData.init,
   1.144 +   add_codegen "recfun" recfun_codegen,
   1.145 +   Attrib.add_attributes [("code",
   1.146 +     (Attrib.no_args add, Attrib.no_args Attrib.undef_local_attribute),
   1.147 +     "declare equations to be used for code generation")]];
   1.148 +
   1.149 +end;