author  wenzelm 
Sat, 21 Jan 2006 23:02:14 +0100  
changeset 18728  6790126ab5f6 
parent 18708  4b3dadb4fe33 
child 18931  427df66052a1 
permissions  rwrr 
12447  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 

18728  10 
val add: string option > attribute 
11 
val del: attribute 

18220  12 
val get_rec_equations: theory > string * typ > (term list * term) list * typ 
18702  13 
val get_thm_equations: theory > string * typ > (thm list * typ) option 
18708  14 
val setup: theory > theory 
12447  15 
end; 
16 

17 
structure RecfunCodegen : RECFUN_CODEGEN = 

18 
struct 

19 

20 
open Codegen; 

21 

16424  22 
structure CodegenData = TheoryDataFun 
23 
(struct 

12447  24 
val name = "HOL/recfun_codegen"; 
16645  25 
type T = (thm * string option) list Symtab.table; 
12447  26 
val empty = Symtab.empty; 
27 
val copy = I; 

16424  28 
val extend = I; 
16645  29 
fun merge _ = Symtab.merge_multi' (Drule.eq_thm_prop o pairself fst); 
12447  30 
fun print _ _ = (); 
16424  31 
end); 
12447  32 

33 

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  36 
val const_of = dest_Const o head_of o fst o dest_eqn; 
37 

38 
fun warn thm = warning ("RecfunCodegen: Not a proper equation:\n" ^ 

39 
string_of_thm thm); 

40 

18728  41 
fun add optmod = Thm.declaration_attribute (fn thm => Context.map_theory (fn thy => 
12447  42 
let 
12556  43 
val tab = CodegenData.get thy; 
12447  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  47 
CodegenData.put (Symtab.update_multi (s, (thm, optmod)) tab) thy 
48 
else (warn thm; thy) 

49 
end handle TERM _ => (warn thm; thy))); 

12447  50 

18728  51 
val del = Thm.declaration_attribute (fn thm => Context.map_theory (fn thy => 
14196  52 
let 
53 
val tab = CodegenData.get thy; 

54 
val (s, _) = const_of (prop_of thm); 

17412  55 
in case Symtab.lookup tab s of 
18728  56 
NONE => thy 
57 
 SOME thms => 

58 
CodegenData.put (Symtab.update (s, gen_rem (eq_thm o apfst fst) (thms, thm)) tab) thy 

59 
end handle TERM _ => (warn thm; thy))); 

14196  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  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  68 
fun get_equations thy defs (s, T) = 
17412  69 
(case Symtab.lookup (CodegenData.get thy) s of 
16645  70 
NONE => ([], "") 
71 
 SOME thms => 

72 
let val thms' = del_redundant thy [] 

73 
(List.filter (fn (thm, _) => is_instance thy T 

74 
(snd (const_of (prop_of thm)))) thms) 

75 
in if null thms' then ([], "") 

76 
else (preprocess thy (map fst thms'), 

77 
case snd (snd (split_last thms')) of 

78 
NONE => (case get_defn thy defs s T of 

79 
NONE => thyname_of_const s thy 

80 
 SOME ((_, (thyname, _)), _) => thyname) 

81 
 SOME thyname => thyname) 

82 
end); 

12447  83 

18220  84 
fun get_rec_equations thy (s, T) = 
85 
Symtab.lookup (CodegenData.get thy) s 

86 
> Option.map (fn thms => 

87 
List.filter (fn (thm, _) => is_instance thy T ((snd o const_of o prop_of) thm)) thms 

88 
> del_redundant thy []) 

89 
> Option.mapPartial (fn thms => if null thms then NONE else SOME thms) 

90 
> Option.map (fn thms => 

91 
(preprocess thy (map fst thms), 

92 
(snd o const_of o prop_of o fst o hd) thms)) 

93 
> the_default ([], dummyT) 

94 
> apfst (map prop_of) 

95 
> apfst (map (Codegen.rename_term #> HOLogic.dest_Trueprop #> HOLogic.dest_eq #> apfst (strip_comb #> snd))) 

96 

18702  97 
fun get_thm_equations thy (c, ty) = 
98 
Symtab.lookup (CodegenData.get thy) c 

99 
> Option.map (fn thms => 

100 
List.filter (fn (thm, _) => is_instance thy ty ((snd o const_of o prop_of) thm)) thms 

101 
> del_redundant thy []) 

102 
> Option.mapPartial (fn thms => if null thms then NONE else SOME thms) 

103 
> Option.map (fn thms => 

104 
(preprocess thy (map fst thms), 

105 
(snd o const_of o prop_of o fst o hd) thms)) 

106 
> (Option.map o apfst o map) (fn thm => thm RS HOL.eq_reflection); 

107 

16645  108 
fun mk_suffix thy defs (s, T) = (case get_defn thy defs s T of 
17144  109 
SOME (_, SOME i) => " def" ^ string_of_int i  _ => ""); 
12447  110 

111 
exception EQN of string * typ * string; 

112 

113 
fun cycle g (xs, x) = 

114 
if x mem xs then xs 

17144  115 
else Library.foldl (cycle g) (x :: xs, List.concat (Graph.find_paths (fst g) (x, x))); 
12447  116 

17144  117 
fun add_rec_funs thy defs gr dep eqs module = 
12447  118 
let 
16645  119 
fun dest_eq t = (fst (const_of t) ^ mk_suffix thy defs (const_of t), 
120 
dest_eqn (rename_term t)); 

12447  121 
val eqs' = map dest_eq eqs; 
122 
val (dname, _) :: _ = eqs'; 

123 
val (s, T) = const_of (hd eqs); 

124 

17144  125 
fun mk_fundef module fname prfx gr [] = (gr, []) 
126 
 mk_fundef module fname prfx gr ((fname', (lhs, rhs)) :: xs) = 

12447  127 
let 
17144  128 
val (gr1, pl) = invoke_codegen thy defs dname module false (gr, lhs); 
129 
val (gr2, pr) = invoke_codegen thy defs dname module false (gr1, rhs); 

130 
val (gr3, rest) = mk_fundef module fname' "and " gr2 xs 

12447  131 
in 
132 
(gr3, Pretty.blk (4, [Pretty.str (if fname = fname' then "  " else prfx), 

133 
pl, Pretty.str " =", Pretty.brk 1, pr]) :: rest) 

134 
end; 

135 

17144  136 
fun put_code module fundef = map_node dname 
137 
(K (SOME (EQN ("", dummyT, dname)), module, Pretty.string_of (Pretty.blk (0, 

12447  138 
separate Pretty.fbrk fundef @ [Pretty.str ";"])) ^ "\n\n")); 
139 

140 
in 

17144  141 
(case try (get_node gr) dname of 
15531  142 
NONE => 
12447  143 
let 
17144  144 
val gr1 = add_edge (dname, dep) 
145 
(new_node (dname, (SOME (EQN (s, T, "")), module, "")) gr); 

146 
val (gr2, fundef) = mk_fundef module "" "fun " gr1 eqs'; 

12447  147 
val xs = cycle gr2 ([], dname); 
17144  148 
val cs = map (fn x => case get_node gr2 x of 
16645  149 
(SOME (EQN (s, T, _)), _, _) => (s, T) 
12447  150 
 _ => error ("RecfunCodegen: illegal cyclic dependencies:\n" ^ 
151 
implode (separate ", " xs))) xs 

152 
in (case xs of 

17144  153 
[_] => (put_code module fundef gr2, module) 
12447  154 
 _ => 
155 
if not (dep mem xs) then 

156 
let 

16645  157 
val thmss as (_, thyname) :: _ = map (get_equations thy defs) cs; 
17144  158 
val module' = if_library thyname module; 
16645  159 
val eqs'' = map (dest_eq o prop_of) (List.concat (map fst thmss)); 
17144  160 
val (gr3, fundef') = mk_fundef module' "" "fun " 
161 
(add_edge (dname, dep) 

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  164 
(k, (SOME (EQN ("", dummyT, dname)), module', ""))) xs))) eqs'' 
165 
in (put_code module' fundef' gr3, module') end 

166 
else (gr2, module)) 

12447  167 
end 
17144  168 
 SOME (SOME (EQN (_, _, s)), module', _) => 
169 
(if s = "" then 

170 
if dname = dep then gr else add_edge (dname, dep) gr 

171 
else if s = dep then gr else add_edge (s, dep) gr, 

172 
module')) 

12447  173 
end; 
174 

17144  175 
fun recfun_codegen thy defs gr dep module brack t = (case strip_comb t of 
16645  176 
(Const (p as (s, T)), ts) => (case (get_equations thy defs p, get_assoc_code thy s T) of 
177 
(([], _), _) => NONE 

15531  178 
 (_, SOME _) => NONE 
17144  179 
 ((eqns, thyname), NONE) => 
16645  180 
let 
17144  181 
val module' = if_library thyname module; 
16645  182 
val (gr', ps) = foldl_map 
17144  183 
(invoke_codegen thy defs dep module true) (gr, ts); 
184 
val suffix = mk_suffix thy defs p; 

185 
val (gr'', module'') = 

186 
add_rec_funs thy defs gr' dep (map prop_of eqns) module'; 

187 
val (gr''', fname) = mk_const_id module'' (s ^ suffix) gr'' 

12447  188 
in 
17144  189 
SOME (gr''', mk_app brack (Pretty.str (mk_qual_id module fname)) ps) 
12447  190 
end) 
15531  191 
 _ => NONE); 
12447  192 

193 

18708  194 
val setup = 
195 
CodegenData.init #> 

196 
add_codegen "recfun" recfun_codegen #> 

18220  197 
add_attribute "" 
198 
( Args.del  Scan.succeed del 

18708  199 
 Scan.option (Args.$$$ "target"  Args.colon  Args.name) >> add) #> 
18702  200 
CodegenPackage.add_eqextr 
18708  201 
("rec", fn thy => fn _ => get_thm_equations thy); 
12447  202 

203 
end; 