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