src/HOL/Tools/recfun_codegen.ML
author krauss
Fri Nov 24 13:44:51 2006 +0100 (2006-11-24)
changeset 21512 3786eb1b69d6
parent 21410 c212b002fc8c
child 22360 26ead7ed4f4b
permissions -rw-r--r--
Lemma "fundef_default_value" uses predicate instead of set.
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
wenzelm@18728
    10
  val add: string option -> attribute
wenzelm@18728
    11
  val del: attribute
wenzelm@18708
    12
  val setup: theory -> theory
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
haftmann@20597
    20
structure RecCodegenData = TheoryDataFun
wenzelm@16424
    21
(struct
berghofe@12447
    22
  val name = "HOL/recfun_codegen";
berghofe@16645
    23
  type T = (thm * string option) list Symtab.table;
berghofe@12447
    24
  val empty = Symtab.empty;
berghofe@12447
    25
  val copy = I;
wenzelm@16424
    26
  val extend = I;
wenzelm@18931
    27
  fun merge _ = Symtab.merge_list (Drule.eq_thm_prop o pairself fst);
berghofe@12447
    28
  fun print _ _ = ();
wenzelm@16424
    29
end);
berghofe@12447
    30
berghofe@12447
    31
berghofe@12447
    32
val dest_eqn = HOLogic.dest_eq o HOLogic.dest_Trueprop;
berghofe@15321
    33
val lhs_of = fst o dest_eqn o prop_of;
berghofe@12447
    34
val const_of = dest_Const o head_of o fst o dest_eqn;
berghofe@12447
    35
berghofe@12447
    36
fun warn thm = warning ("RecfunCodegen: Not a proper equation:\n" ^
berghofe@12447
    37
  string_of_thm thm);
berghofe@12447
    38
haftmann@21410
    39
fun add optmod =
berghofe@12447
    40
  let
haftmann@19344
    41
    fun add thm =
haftmann@19344
    42
      if Pattern.pattern (lhs_of thm) then
haftmann@20597
    43
        RecCodegenData.map
haftmann@19344
    44
          (Symtab.update_list ((fst o const_of o prop_of) thm, (thm, optmod)))
haftmann@19344
    45
      else tap (fn _ => warn thm)
haftmann@19344
    46
      handle TERM _ => tap (fn _ => warn thm);
berghofe@15321
    47
  in
haftmann@21410
    48
    Thm.declaration_attribute (fn thm => Context.mapping
haftmann@21410
    49
      (add thm #> CodegenData.add_func_legacy thm) I)
haftmann@21410
    50
  end;
berghofe@12447
    51
haftmann@19344
    52
fun del_thm thm thy =
berghofe@14196
    53
  let
haftmann@20597
    54
    val tab = RecCodegenData.get thy;
berghofe@14196
    55
    val (s, _) = const_of (prop_of thm);
wenzelm@17412
    56
  in case Symtab.lookup tab s of
wenzelm@18728
    57
      NONE => thy
wenzelm@18728
    58
    | SOME thms =>
haftmann@20951
    59
        RecCodegenData.put (Symtab.update (s, remove (eq_thm o apsnd fst) thm thms) tab) thy
haftmann@19344
    60
  end handle TERM _ => (warn thm; thy);
haftmann@19344
    61
haftmann@19344
    62
val del = Thm.declaration_attribute
wenzelm@20897
    63
  (fn thm => Context.mapping (del_thm thm #> CodegenData.del_func thm) I)
berghofe@14196
    64
berghofe@15321
    65
fun del_redundant thy eqs [] = eqs
berghofe@15321
    66
  | del_redundant thy eqs (eq :: eqs') =
berghofe@15321
    67
    let
berghofe@15321
    68
      val matches = curry
wenzelm@17203
    69
        (Pattern.matches thy o pairself (lhs_of o fst))
berghofe@15321
    70
    in del_redundant thy (eq :: eqs) (filter_out (matches eq) eqs') end;
berghofe@15321
    71
berghofe@16645
    72
fun get_equations thy defs (s, T) =
haftmann@20597
    73
  (case Symtab.lookup (RecCodegenData.get thy) s of
berghofe@16645
    74
     NONE => ([], "")
berghofe@16645
    75
   | SOME thms => 
berghofe@16645
    76
       let val thms' = del_redundant thy []
berghofe@16645
    77
         (List.filter (fn (thm, _) => is_instance thy T
berghofe@16645
    78
           (snd (const_of (prop_of thm)))) thms)
berghofe@16645
    79
       in if null thms' then ([], "")
berghofe@16645
    80
         else (preprocess thy (map fst thms'),
berghofe@16645
    81
           case snd (snd (split_last thms')) of
berghofe@16645
    82
               NONE => (case get_defn thy defs s T of
berghofe@16645
    83
                   NONE => thyname_of_const s thy
berghofe@16645
    84
                 | SOME ((_, (thyname, _)), _) => thyname)
berghofe@16645
    85
             | SOME thyname => thyname)
berghofe@16645
    86
       end);
berghofe@12447
    87
berghofe@16645
    88
fun mk_suffix thy defs (s, T) = (case get_defn thy defs s T of
berghofe@17144
    89
  SOME (_, SOME i) => " def" ^ string_of_int i | _ => "");
berghofe@12447
    90
berghofe@12447
    91
exception EQN of string * typ * string;
berghofe@12447
    92
berghofe@12447
    93
fun cycle g (xs, x) =
berghofe@12447
    94
  if x mem xs then xs
berghofe@20680
    95
  else Library.foldl (cycle g) (x :: xs, List.concat (Graph.all_paths (fst g) (x, x)));
berghofe@12447
    96
berghofe@17144
    97
fun add_rec_funs thy defs gr dep eqs module =
berghofe@12447
    98
  let
berghofe@16645
    99
    fun dest_eq t = (fst (const_of t) ^ mk_suffix thy defs (const_of t),
berghofe@16645
   100
      dest_eqn (rename_term t));
berghofe@12447
   101
    val eqs' = map dest_eq eqs;
berghofe@12447
   102
    val (dname, _) :: _ = eqs';
berghofe@12447
   103
    val (s, T) = const_of (hd eqs);
berghofe@12447
   104
berghofe@17144
   105
    fun mk_fundef module fname prfx gr [] = (gr, [])
haftmann@21410
   106
      | mk_fundef module fname prfx gr ((fname' : string, (lhs, rhs)) :: xs) =
berghofe@12447
   107
      let
berghofe@17144
   108
        val (gr1, pl) = invoke_codegen thy defs dname module false (gr, lhs);
berghofe@17144
   109
        val (gr2, pr) = invoke_codegen thy defs dname module false (gr1, rhs);
berghofe@17144
   110
        val (gr3, rest) = mk_fundef module fname' "and " gr2 xs
berghofe@12447
   111
      in
berghofe@12447
   112
        (gr3, Pretty.blk (4, [Pretty.str (if fname = fname' then "  | " else prfx),
berghofe@12447
   113
           pl, Pretty.str " =", Pretty.brk 1, pr]) :: rest)
berghofe@12447
   114
      end;
berghofe@12447
   115
berghofe@17144
   116
    fun put_code module fundef = map_node dname
berghofe@17144
   117
      (K (SOME (EQN ("", dummyT, dname)), module, Pretty.string_of (Pretty.blk (0,
berghofe@12447
   118
      separate Pretty.fbrk fundef @ [Pretty.str ";"])) ^ "\n\n"));
berghofe@12447
   119
berghofe@12447
   120
  in
berghofe@17144
   121
    (case try (get_node gr) dname of
skalberg@15531
   122
       NONE =>
berghofe@12447
   123
         let
berghofe@17144
   124
           val gr1 = add_edge (dname, dep)
berghofe@17144
   125
             (new_node (dname, (SOME (EQN (s, T, "")), module, "")) gr);
berghofe@17144
   126
           val (gr2, fundef) = mk_fundef module "" "fun " gr1 eqs';
berghofe@12447
   127
           val xs = cycle gr2 ([], dname);
berghofe@17144
   128
           val cs = map (fn x => case get_node gr2 x of
berghofe@16645
   129
               (SOME (EQN (s, T, _)), _, _) => (s, T)
berghofe@12447
   130
             | _ => error ("RecfunCodegen: illegal cyclic dependencies:\n" ^
berghofe@12447
   131
                implode (separate ", " xs))) xs
berghofe@12447
   132
         in (case xs of
berghofe@17144
   133
             [_] => (put_code module fundef gr2, module)
berghofe@12447
   134
           | _ =>
berghofe@12447
   135
             if not (dep mem xs) then
berghofe@12447
   136
               let
berghofe@16645
   137
                 val thmss as (_, thyname) :: _ = map (get_equations thy defs) cs;
berghofe@17144
   138
                 val module' = if_library thyname module;
berghofe@16645
   139
                 val eqs'' = map (dest_eq o prop_of) (List.concat (map fst thmss));
berghofe@17144
   140
                 val (gr3, fundef') = mk_fundef module' "" "fun "
berghofe@17144
   141
                   (add_edge (dname, dep)
berghofe@17144
   142
                     (foldr (uncurry new_node) (del_nodes xs gr2)
skalberg@15574
   143
                       (map (fn k =>
berghofe@17144
   144
                         (k, (SOME (EQN ("", dummyT, dname)), module', ""))) xs))) eqs''
berghofe@17144
   145
               in (put_code module' fundef' gr3, module') end
berghofe@17144
   146
             else (gr2, module))
berghofe@12447
   147
         end
berghofe@17144
   148
     | SOME (SOME (EQN (_, _, s)), module', _) =>
berghofe@17144
   149
         (if s = "" then
berghofe@17144
   150
            if dname = dep then gr else add_edge (dname, dep) gr
berghofe@17144
   151
          else if s = dep then gr else add_edge (s, dep) gr,
berghofe@17144
   152
          module'))
berghofe@12447
   153
  end;
berghofe@12447
   154
berghofe@17144
   155
fun recfun_codegen thy defs gr dep module brack t = (case strip_comb t of
berghofe@16645
   156
    (Const (p as (s, T)), ts) => (case (get_equations thy defs p, get_assoc_code thy s T) of
berghofe@16645
   157
       (([], _), _) => NONE
skalberg@15531
   158
     | (_, SOME _) => NONE
berghofe@17144
   159
     | ((eqns, thyname), NONE) =>
berghofe@16645
   160
        let
berghofe@17144
   161
          val module' = if_library thyname module;
berghofe@16645
   162
          val (gr', ps) = foldl_map
berghofe@17144
   163
            (invoke_codegen thy defs dep module true) (gr, ts);
berghofe@17144
   164
          val suffix = mk_suffix thy defs p;
berghofe@17144
   165
          val (gr'', module'') =
berghofe@17144
   166
            add_rec_funs thy defs gr' dep (map prop_of eqns) module';
berghofe@17144
   167
          val (gr''', fname) = mk_const_id module'' (s ^ suffix) gr''
berghofe@12447
   168
        in
berghofe@17144
   169
          SOME (gr''', mk_app brack (Pretty.str (mk_qual_id module fname)) ps)
berghofe@12447
   170
        end)
skalberg@15531
   171
  | _ => NONE);
berghofe@12447
   172
berghofe@12447
   173
wenzelm@18708
   174
val setup =
haftmann@20597
   175
  RecCodegenData.init #>
wenzelm@18708
   176
  add_codegen "recfun" recfun_codegen #>
haftmann@18220
   177
  add_attribute ""
haftmann@19344
   178
    (Args.del |-- Scan.succeed del
haftmann@21128
   179
     || Scan.option (Args.$$$ "target" |-- Args.colon |-- Args.name) >> add);
berghofe@12447
   180
berghofe@12447
   181
end;