src/HOL/Tools/recfun_codegen.ML
author wenzelm
Sat, 21 Jan 2006 23:02:14 +0100
changeset 18728 6790126ab5f6
parent 18708 4b3dadb4fe33
child 18931 427df66052a1
permissions -rw-r--r--
simplified type attribute;
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
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
     5
Code generator for recursive functions.
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
     6
*)
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
     7
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
     8
signature RECFUN_CODEGEN =
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
     9
sig
18728
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18708
diff changeset
    10
  val add: string option -> attribute
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18708
diff changeset
    11
  val del: attribute
18220
43cf5767f992 added codegenerator
haftmann
parents: 17412
diff changeset
    12
  val get_rec_equations: theory -> string * typ -> (term list * term) list * typ
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18220
diff changeset
    13
  val get_thm_equations: theory -> string * typ -> (thm list * typ) option
18708
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 18702
diff changeset
    14
  val setup: theory -> theory
12447
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
    15
end;
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
    16
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
    17
structure RecfunCodegen : RECFUN_CODEGEN =
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
    18
struct
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
    19
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
    20
open Codegen;
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
    21
16424
18a07ad8fea8 accomodate change of TheoryDataFun;
wenzelm
parents: 15700
diff changeset
    22
structure CodegenData = TheoryDataFun
18a07ad8fea8 accomodate change of TheoryDataFun;
wenzelm
parents: 15700
diff changeset
    23
(struct
12447
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
    24
  val name = "HOL/recfun_codegen";
16645
a152d6b21c31 Adapted to modular code generation.
berghofe
parents: 16424
diff changeset
    25
  type T = (thm * string option) list Symtab.table;
12447
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
    26
  val empty = Symtab.empty;
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
    27
  val copy = I;
16424
18a07ad8fea8 accomodate change of TheoryDataFun;
wenzelm
parents: 15700
diff changeset
    28
  val extend = I;
16645
a152d6b21c31 Adapted to modular code generation.
berghofe
parents: 16424
diff changeset
    29
  fun merge _ = Symtab.merge_multi' (Drule.eq_thm_prop o pairself fst);
12447
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
    30
  fun print _ _ = ();
16424
18a07ad8fea8 accomodate change of TheoryDataFun;
wenzelm
parents: 15700
diff changeset
    31
end);
12447
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
    32
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
    33
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
    34
val dest_eqn = HOLogic.dest_eq o HOLogic.dest_Trueprop;
15321
694f9d3ce90d Reimplemented some operations on "code lemma" table to avoid that code
berghofe
parents: 15257
diff changeset
    35
val lhs_of = fst o dest_eqn o prop_of;
12447
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
    36
val const_of = dest_Const o head_of o fst o dest_eqn;
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
    37
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
    38
fun warn thm = warning ("RecfunCodegen: Not a proper equation:\n" ^
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
    39
  string_of_thm thm);
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
    40
18728
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18708
diff changeset
    41
fun add optmod = Thm.declaration_attribute (fn thm => Context.map_theory (fn thy =>
12447
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
    42
  let
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);
15321
694f9d3ce90d Reimplemented some operations on "code lemma" table to avoid that code
berghofe
parents: 15257
diff changeset
    45
  in
694f9d3ce90d Reimplemented some operations on "code lemma" table to avoid that code
berghofe
parents: 15257
diff changeset
    46
    if Pattern.pattern (lhs_of thm) then
18728
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18708
diff changeset
    47
      CodegenData.put (Symtab.update_multi (s, (thm, optmod)) tab) thy
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18708
diff changeset
    48
    else (warn thm; thy)
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18708
diff changeset
    49
  end handle TERM _ => (warn thm; thy)));
12447
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
    50
18728
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18708
diff changeset
    51
val del = Thm.declaration_attribute (fn thm => Context.map_theory (fn thy =>
14196
be5e838f37bd Added "del" attribute for deleting equations.
berghofe
parents: 13105
diff changeset
    52
  let
be5e838f37bd Added "del" attribute for deleting equations.
berghofe
parents: 13105
diff changeset
    53
    val tab = CodegenData.get thy;
be5e838f37bd Added "del" attribute for deleting equations.
berghofe
parents: 13105
diff changeset
    54
    val (s, _) = const_of (prop_of thm);
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17261
diff changeset
    55
  in case Symtab.lookup tab s of
18728
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18708
diff changeset
    56
      NONE => thy
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18708
diff changeset
    57
    | SOME thms =>
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18708
diff changeset
    58
        CodegenData.put (Symtab.update (s, gen_rem (eq_thm o apfst fst) (thms, thm)) tab) thy
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18708
diff changeset
    59
  end handle TERM _ => (warn thm; thy)));
14196
be5e838f37bd Added "del" attribute for deleting equations.
berghofe
parents: 13105
diff changeset
    60
15321
694f9d3ce90d Reimplemented some operations on "code lemma" table to avoid that code
berghofe
parents: 15257
diff changeset
    61
fun del_redundant thy eqs [] = eqs
694f9d3ce90d Reimplemented some operations on "code lemma" table to avoid that code
berghofe
parents: 15257
diff changeset
    62
  | del_redundant thy eqs (eq :: eqs') =
694f9d3ce90d Reimplemented some operations on "code lemma" table to avoid that code
berghofe
parents: 15257
diff changeset
    63
    let
694f9d3ce90d Reimplemented some operations on "code lemma" table to avoid that code
berghofe
parents: 15257
diff changeset
    64
      val matches = curry
17203
29b2563f5c11 refer to theory instead of low-level tsig;
wenzelm
parents: 17144
diff changeset
    65
        (Pattern.matches thy o pairself (lhs_of o fst))
15321
694f9d3ce90d Reimplemented some operations on "code lemma" table to avoid that code
berghofe
parents: 15257
diff changeset
    66
    in del_redundant thy (eq :: eqs) (filter_out (matches eq) eqs') end;
694f9d3ce90d Reimplemented some operations on "code lemma" table to avoid that code
berghofe
parents: 15257
diff changeset
    67
16645
a152d6b21c31 Adapted to modular code generation.
berghofe
parents: 16424
diff changeset
    68
fun get_equations thy defs (s, T) =
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17261
diff changeset
    69
  (case Symtab.lookup (CodegenData.get thy) s of
16645
a152d6b21c31 Adapted to modular code generation.
berghofe
parents: 16424
diff changeset
    70
     NONE => ([], "")
a152d6b21c31 Adapted to modular code generation.
berghofe
parents: 16424
diff changeset
    71
   | SOME thms => 
a152d6b21c31 Adapted to modular code generation.
berghofe
parents: 16424
diff changeset
    72
       let val thms' = del_redundant thy []
a152d6b21c31 Adapted to modular code generation.
berghofe
parents: 16424
diff changeset
    73
         (List.filter (fn (thm, _) => is_instance thy T
a152d6b21c31 Adapted to modular code generation.
berghofe
parents: 16424
diff changeset
    74
           (snd (const_of (prop_of thm)))) thms)
a152d6b21c31 Adapted to modular code generation.
berghofe
parents: 16424
diff changeset
    75
       in if null thms' then ([], "")
a152d6b21c31 Adapted to modular code generation.
berghofe
parents: 16424
diff changeset
    76
         else (preprocess thy (map fst thms'),
a152d6b21c31 Adapted to modular code generation.
berghofe
parents: 16424
diff changeset
    77
           case snd (snd (split_last thms')) of
a152d6b21c31 Adapted to modular code generation.
berghofe
parents: 16424
diff changeset
    78
               NONE => (case get_defn thy defs s T of
a152d6b21c31 Adapted to modular code generation.
berghofe
parents: 16424
diff changeset
    79
                   NONE => thyname_of_const s thy
a152d6b21c31 Adapted to modular code generation.
berghofe
parents: 16424
diff changeset
    80
                 | SOME ((_, (thyname, _)), _) => thyname)
a152d6b21c31 Adapted to modular code generation.
berghofe
parents: 16424
diff changeset
    81
             | SOME thyname => thyname)
a152d6b21c31 Adapted to modular code generation.
berghofe
parents: 16424
diff changeset
    82
       end);
12447
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
    83
18220
43cf5767f992 added codegenerator
haftmann
parents: 17412
diff changeset
    84
fun get_rec_equations thy (s, T) =
43cf5767f992 added codegenerator
haftmann
parents: 17412
diff changeset
    85
  Symtab.lookup (CodegenData.get thy) s
43cf5767f992 added codegenerator
haftmann
parents: 17412
diff changeset
    86
  |> Option.map (fn thms => 
43cf5767f992 added codegenerator
haftmann
parents: 17412
diff changeset
    87
       List.filter (fn (thm, _) => is_instance thy T ((snd o const_of o prop_of) thm)) thms
43cf5767f992 added codegenerator
haftmann
parents: 17412
diff changeset
    88
       |> del_redundant thy [])
43cf5767f992 added codegenerator
haftmann
parents: 17412
diff changeset
    89
  |> Option.mapPartial (fn thms => if null thms then NONE else SOME thms)
43cf5767f992 added codegenerator
haftmann
parents: 17412
diff changeset
    90
  |> Option.map (fn thms =>
43cf5767f992 added codegenerator
haftmann
parents: 17412
diff changeset
    91
       (preprocess thy (map fst thms),
43cf5767f992 added codegenerator
haftmann
parents: 17412
diff changeset
    92
          (snd o const_of o prop_of o fst o hd) thms))
43cf5767f992 added codegenerator
haftmann
parents: 17412
diff changeset
    93
  |> the_default ([], dummyT)
43cf5767f992 added codegenerator
haftmann
parents: 17412
diff changeset
    94
  |> apfst (map prop_of)
43cf5767f992 added codegenerator
haftmann
parents: 17412
diff changeset
    95
  |> apfst (map (Codegen.rename_term #> HOLogic.dest_Trueprop #> HOLogic.dest_eq #> apfst (strip_comb #> snd)))
43cf5767f992 added codegenerator
haftmann
parents: 17412
diff changeset
    96
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18220
diff changeset
    97
fun get_thm_equations thy (c, ty) =
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18220
diff changeset
    98
  Symtab.lookup (CodegenData.get thy) c
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18220
diff changeset
    99
  |> Option.map (fn thms => 
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18220
diff changeset
   100
       List.filter (fn (thm, _) => is_instance thy ty ((snd o const_of o prop_of) thm)) thms
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18220
diff changeset
   101
       |> del_redundant thy [])
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18220
diff changeset
   102
  |> Option.mapPartial (fn thms => if null thms then NONE else SOME thms)
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18220
diff changeset
   103
  |> Option.map (fn thms =>
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18220
diff changeset
   104
       (preprocess thy (map fst thms),
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18220
diff changeset
   105
          (snd o const_of o prop_of o fst o hd) thms))
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18220
diff changeset
   106
  |> (Option.map o apfst o map) (fn thm => thm RS HOL.eq_reflection);
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18220
diff changeset
   107
16645
a152d6b21c31 Adapted to modular code generation.
berghofe
parents: 16424
diff changeset
   108
fun mk_suffix thy defs (s, T) = (case get_defn thy defs s T of
17144
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16645
diff changeset
   109
  SOME (_, SOME i) => " def" ^ string_of_int i | _ => "");
12447
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
   110
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
   111
exception EQN of string * typ * string;
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
   112
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
   113
fun cycle g (xs, x) =
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
   114
  if x mem xs then xs
17144
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16645
diff changeset
   115
  else Library.foldl (cycle g) (x :: xs, List.concat (Graph.find_paths (fst g) (x, x)));
12447
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
   116
17144
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16645
diff changeset
   117
fun add_rec_funs thy defs gr dep eqs module =
12447
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
   118
  let
16645
a152d6b21c31 Adapted to modular code generation.
berghofe
parents: 16424
diff changeset
   119
    fun dest_eq t = (fst (const_of t) ^ mk_suffix thy defs (const_of t),
a152d6b21c31 Adapted to modular code generation.
berghofe
parents: 16424
diff changeset
   120
      dest_eqn (rename_term t));
12447
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
   121
    val eqs' = map dest_eq eqs;
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
   122
    val (dname, _) :: _ = eqs';
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
   123
    val (s, T) = const_of (hd eqs);
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
   124
17144
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16645
diff changeset
   125
    fun mk_fundef module fname prfx gr [] = (gr, [])
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16645
diff changeset
   126
      | mk_fundef module fname prfx gr ((fname', (lhs, rhs)) :: xs) =
12447
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
   127
      let
17144
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16645
diff changeset
   128
        val (gr1, pl) = invoke_codegen thy defs dname module false (gr, lhs);
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16645
diff changeset
   129
        val (gr2, pr) = invoke_codegen thy defs dname module false (gr1, rhs);
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16645
diff changeset
   130
        val (gr3, rest) = mk_fundef module fname' "and " gr2 xs
12447
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
   131
      in
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
   132
        (gr3, Pretty.blk (4, [Pretty.str (if fname = fname' then "  | " else prfx),
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
   133
           pl, Pretty.str " =", Pretty.brk 1, pr]) :: rest)
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
   134
      end;
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
   135
17144
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16645
diff changeset
   136
    fun put_code module fundef = map_node dname
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16645
diff changeset
   137
      (K (SOME (EQN ("", dummyT, dname)), module, Pretty.string_of (Pretty.blk (0,
12447
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
   138
      separate Pretty.fbrk fundef @ [Pretty.str ";"])) ^ "\n\n"));
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
   139
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
   140
  in
17144
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16645
diff changeset
   141
    (case try (get_node gr) dname of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15321
diff changeset
   142
       NONE =>
12447
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
   143
         let
17144
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16645
diff changeset
   144
           val gr1 = add_edge (dname, dep)
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16645
diff changeset
   145
             (new_node (dname, (SOME (EQN (s, T, "")), module, "")) gr);
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16645
diff changeset
   146
           val (gr2, fundef) = mk_fundef module "" "fun " gr1 eqs';
12447
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
   147
           val xs = cycle gr2 ([], dname);
17144
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16645
diff changeset
   148
           val cs = map (fn x => case get_node gr2 x of
16645
a152d6b21c31 Adapted to modular code generation.
berghofe
parents: 16424
diff changeset
   149
               (SOME (EQN (s, T, _)), _, _) => (s, T)
12447
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
   150
             | _ => error ("RecfunCodegen: illegal cyclic dependencies:\n" ^
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
   151
                implode (separate ", " xs))) xs
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
   152
         in (case xs of
17144
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16645
diff changeset
   153
             [_] => (put_code module fundef gr2, module)
12447
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
   154
           | _ =>
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
   155
             if not (dep mem xs) then
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
   156
               let
16645
a152d6b21c31 Adapted to modular code generation.
berghofe
parents: 16424
diff changeset
   157
                 val thmss as (_, thyname) :: _ = map (get_equations thy defs) cs;
17144
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16645
diff changeset
   158
                 val module' = if_library thyname module;
16645
a152d6b21c31 Adapted to modular code generation.
berghofe
parents: 16424
diff changeset
   159
                 val eqs'' = map (dest_eq o prop_of) (List.concat (map fst thmss));
17144
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16645
diff changeset
   160
                 val (gr3, fundef') = mk_fundef module' "" "fun "
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16645
diff changeset
   161
                   (add_edge (dname, dep)
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16645
diff changeset
   162
                     (foldr (uncurry new_node) (del_nodes xs gr2)
15574
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   163
                       (map (fn k =>
17144
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16645
diff changeset
   164
                         (k, (SOME (EQN ("", dummyT, dname)), module', ""))) xs))) eqs''
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16645
diff changeset
   165
               in (put_code module' fundef' gr3, module') end
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16645
diff changeset
   166
             else (gr2, module))
12447
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
   167
         end
17144
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16645
diff changeset
   168
     | SOME (SOME (EQN (_, _, s)), module', _) =>
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16645
diff changeset
   169
         (if s = "" then
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16645
diff changeset
   170
            if dname = dep then gr else add_edge (dname, dep) gr
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16645
diff changeset
   171
          else if s = dep then gr else add_edge (s, dep) gr,
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16645
diff changeset
   172
          module'))
12447
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
   173
  end;
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
   174
17144
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16645
diff changeset
   175
fun recfun_codegen thy defs gr dep module brack t = (case strip_comb t of
16645
a152d6b21c31 Adapted to modular code generation.
berghofe
parents: 16424
diff changeset
   176
    (Const (p as (s, T)), ts) => (case (get_equations thy defs p, get_assoc_code thy s T) of
a152d6b21c31 Adapted to modular code generation.
berghofe
parents: 16424
diff changeset
   177
       (([], _), _) => NONE
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15321
diff changeset
   178
     | (_, SOME _) => NONE
17144
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16645
diff changeset
   179
     | ((eqns, thyname), NONE) =>
16645
a152d6b21c31 Adapted to modular code generation.
berghofe
parents: 16424
diff changeset
   180
        let
17144
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16645
diff changeset
   181
          val module' = if_library thyname module;
16645
a152d6b21c31 Adapted to modular code generation.
berghofe
parents: 16424
diff changeset
   182
          val (gr', ps) = foldl_map
17144
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16645
diff changeset
   183
            (invoke_codegen thy defs dep module true) (gr, ts);
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16645
diff changeset
   184
          val suffix = mk_suffix thy defs p;
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16645
diff changeset
   185
          val (gr'', module'') =
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16645
diff changeset
   186
            add_rec_funs thy defs gr' dep (map prop_of eqns) module';
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16645
diff changeset
   187
          val (gr''', fname) = mk_const_id module'' (s ^ suffix) gr''
12447
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
   188
        in
17144
6642e0f96f44 Implemented incremental code generation.
berghofe
parents: 16645
diff changeset
   189
          SOME (gr''', mk_app brack (Pretty.str (mk_qual_id module fname)) ps)
12447
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
   190
        end)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15321
diff changeset
   191
  | _ => NONE);
12447
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
   192
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
   193
18708
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 18702
diff changeset
   194
val setup =
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 18702
diff changeset
   195
  CodegenData.init #>
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 18702
diff changeset
   196
  add_codegen "recfun" recfun_codegen #>
18220
43cf5767f992 added codegenerator
haftmann
parents: 17412
diff changeset
   197
  add_attribute ""
43cf5767f992 added codegenerator
haftmann
parents: 17412
diff changeset
   198
    (   Args.del |-- Scan.succeed del
18708
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 18702
diff changeset
   199
     || Scan.option (Args.$$$ "target" |-- Args.colon |-- Args.name) >> add) #>
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18220
diff changeset
   200
  CodegenPackage.add_eqextr
18708
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 18702
diff changeset
   201
    ("rec", fn thy => fn _ => get_thm_equations thy);
12447
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
   202
e752c9aecdec Code generator for recursive functions.
berghofe
parents:
diff changeset
   203
end;