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