author | schirmer |
Tue, 12 Jul 2005 23:42:11 +0200 | |
changeset 16783 | 26fccaaf9cb4 |
parent 16645 | a152d6b21c31 |
child 17144 | 6642e0f96f44 |
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 |
|
16645 | 10 |
val add: string option -> theory attribute |
11 |
val del: theory attribute |
|
12447 | 12 |
val setup: (theory -> theory) list |
13 |
end; |
|
14 |
||
15 |
structure RecfunCodegen : RECFUN_CODEGEN = |
|
16 |
struct |
|
17 |
||
18 |
open Codegen; |
|
19 |
||
16424 | 20 |
structure CodegenData = TheoryDataFun |
21 |
(struct |
|
12447 | 22 |
val name = "HOL/recfun_codegen"; |
16645 | 23 |
type T = (thm * string option) list Symtab.table; |
12447 | 24 |
val empty = Symtab.empty; |
25 |
val copy = I; |
|
16424 | 26 |
val extend = I; |
16645 | 27 |
fun merge _ = Symtab.merge_multi' (Drule.eq_thm_prop o pairself fst); |
12447 | 28 |
fun print _ _ = (); |
16424 | 29 |
end); |
12447 | 30 |
|
31 |
||
32 |
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
|
33 |
val lhs_of = fst o dest_eqn o prop_of; |
12447 | 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 |
||
16645 | 39 |
fun add optmod (p as (thy, thm)) = |
12447 | 40 |
let |
12556 | 41 |
val tab = CodegenData.get thy; |
12447 | 42 |
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
|
43 |
in |
694f9d3ce90d
Reimplemented some operations on "code lemma" table to avoid that code
berghofe
parents:
15257
diff
changeset
|
44 |
if Pattern.pattern (lhs_of thm) then |
694f9d3ce90d
Reimplemented some operations on "code lemma" table to avoid that code
berghofe
parents:
15257
diff
changeset
|
45 |
(CodegenData.put (Symtab.update ((s, |
16645 | 46 |
(thm, optmod) :: getOpt (Symtab.lookup (tab, s), [])), tab)) thy, thm) |
15321
694f9d3ce90d
Reimplemented some operations on "code lemma" table to avoid that code
berghofe
parents:
15257
diff
changeset
|
47 |
else (warn thm; p) |
694f9d3ce90d
Reimplemented some operations on "code lemma" table to avoid that code
berghofe
parents:
15257
diff
changeset
|
48 |
end handle TERM _ => (warn thm; p); |
12447 | 49 |
|
14196 | 50 |
fun del (p as (thy, thm)) = |
51 |
let |
|
52 |
val tab = CodegenData.get thy; |
|
53 |
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
|
54 |
in case Symtab.lookup (tab, s) of |
15531 | 55 |
NONE => p |
56 |
| SOME thms => (CodegenData.put (Symtab.update ((s, |
|
16645 | 57 |
gen_rem (eq_thm o apfst fst) (thms, thm)), tab)) thy, thm) |
14196 | 58 |
end handle TERM _ => (warn thm; p); |
59 |
||
15321
694f9d3ce90d
Reimplemented some operations on "code lemma" table to avoid that code
berghofe
parents:
15257
diff
changeset
|
60 |
fun del_redundant thy eqs [] = eqs |
694f9d3ce90d
Reimplemented some operations on "code lemma" table to avoid that code
berghofe
parents:
15257
diff
changeset
|
61 |
| del_redundant thy eqs (eq :: eqs') = |
694f9d3ce90d
Reimplemented some operations on "code lemma" table to avoid that code
berghofe
parents:
15257
diff
changeset
|
62 |
let |
694f9d3ce90d
Reimplemented some operations on "code lemma" table to avoid that code
berghofe
parents:
15257
diff
changeset
|
63 |
val tsig = Sign.tsig_of (sign_of thy); |
694f9d3ce90d
Reimplemented some operations on "code lemma" table to avoid that code
berghofe
parents:
15257
diff
changeset
|
64 |
val matches = curry |
16645 | 65 |
(Pattern.matches tsig 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) = |
12556 | 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 |
|
16645 | 84 |
fun mk_suffix thy defs (s, T) = (case get_defn thy defs s T of |
85 |
SOME (_, SOME i) => "_def" ^ string_of_int i | _ => ""); |
|
12447 | 86 |
|
87 |
exception EQN of string * typ * string; |
|
88 |
||
89 |
fun cycle g (xs, x) = |
|
90 |
if x mem xs then xs |
|
15570 | 91 |
else Library.foldl (cycle g) (x :: xs, List.concat (Graph.find_paths g (x, x))); |
12447 | 92 |
|
16645 | 93 |
fun add_rec_funs thy defs gr dep eqs thyname = |
12447 | 94 |
let |
16645 | 95 |
fun dest_eq t = (fst (const_of t) ^ mk_suffix thy defs (const_of t), |
96 |
dest_eqn (rename_term t)); |
|
12447 | 97 |
val eqs' = map dest_eq eqs; |
98 |
val (dname, _) :: _ = eqs'; |
|
99 |
val (s, T) = const_of (hd eqs); |
|
100 |
||
16645 | 101 |
fun mk_fundef thyname fname prfx gr [] = (gr, []) |
102 |
| mk_fundef thyname fname prfx gr ((fname', (lhs, rhs)) :: xs) = |
|
12447 | 103 |
let |
16645 | 104 |
val (gr1, pl) = invoke_codegen thy defs dname thyname false (gr, lhs); |
105 |
val (gr2, pr) = invoke_codegen thy defs dname thyname false (gr1, rhs); |
|
106 |
val (gr3, rest) = mk_fundef thyname fname' "and " gr2 xs |
|
12447 | 107 |
in |
108 |
(gr3, Pretty.blk (4, [Pretty.str (if fname = fname' then " | " else prfx), |
|
109 |
pl, Pretty.str " =", Pretty.brk 1, pr]) :: rest) |
|
110 |
end; |
|
111 |
||
16645 | 112 |
fun put_code thyname fundef = Graph.map_node dname |
113 |
(K (SOME (EQN ("", dummyT, dname)), thyname, Pretty.string_of (Pretty.blk (0, |
|
12447 | 114 |
separate Pretty.fbrk fundef @ [Pretty.str ";"])) ^ "\n\n")); |
115 |
||
116 |
in |
|
117 |
(case try (Graph.get_node gr) dname of |
|
15531 | 118 |
NONE => |
12447 | 119 |
let |
120 |
val gr1 = Graph.add_edge (dname, dep) |
|
16645 | 121 |
(Graph.new_node (dname, (SOME (EQN (s, T, "")), "", "")) gr); |
122 |
val (gr2, fundef) = mk_fundef thyname "" "fun " gr1 eqs'; |
|
12447 | 123 |
val xs = cycle gr2 ([], dname); |
124 |
val cs = map (fn x => case Graph.get_node gr2 x of |
|
16645 | 125 |
(SOME (EQN (s, T, _)), _, _) => (s, T) |
12447 | 126 |
| _ => error ("RecfunCodegen: illegal cyclic dependencies:\n" ^ |
127 |
implode (separate ", " xs))) xs |
|
128 |
in (case xs of |
|
16645 | 129 |
[_] => put_code thyname fundef gr2 |
12447 | 130 |
| _ => |
131 |
if not (dep mem xs) then |
|
132 |
let |
|
16645 | 133 |
val thmss as (_, thyname) :: _ = map (get_equations thy defs) cs; |
134 |
val eqs'' = map (dest_eq o prop_of) (List.concat (map fst thmss)); |
|
135 |
val (gr3, fundef') = mk_fundef thyname "" "fun " |
|
12447 | 136 |
(Graph.add_edge (dname, dep) |
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
137 |
(foldr (uncurry Graph.new_node) (Graph.del_nodes xs gr2) |
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
138 |
(map (fn k => |
16645 | 139 |
(k, (SOME (EQN ("", dummyT, dname)), thyname, ""))) xs))) eqs'' |
140 |
in put_code thyname fundef' gr3 end |
|
12447 | 141 |
else gr2) |
142 |
end |
|
16645 | 143 |
| SOME (SOME (EQN (_, _, s)), _, _) => |
12447 | 144 |
if s = "" then |
145 |
if dname = dep then gr else Graph.add_edge (dname, dep) gr |
|
146 |
else if s = dep then gr else Graph.add_edge (s, dep) gr) |
|
147 |
end; |
|
148 |
||
16645 | 149 |
fun recfun_codegen thy defs gr dep thyname brack t = (case strip_comb t of |
150 |
(Const (p as (s, T)), ts) => (case (get_equations thy defs p, get_assoc_code thy s T) of |
|
151 |
(([], _), _) => NONE |
|
15531 | 152 |
| (_, SOME _) => NONE |
16645 | 153 |
| ((eqns, thyname'), NONE) => |
154 |
let |
|
155 |
val (gr', ps) = foldl_map |
|
156 |
(invoke_codegen thy defs dep thyname true) (gr, ts); |
|
157 |
val suffix = mk_suffix thy defs p |
|
12447 | 158 |
in |
16645 | 159 |
SOME (add_rec_funs thy defs gr' dep (map prop_of eqns) thyname', |
160 |
mk_app brack (Pretty.str |
|
161 |
(mk_const_id (sign_of thy) thyname thyname' s ^ suffix)) ps) |
|
12447 | 162 |
end) |
15531 | 163 |
| _ => NONE); |
12447 | 164 |
|
165 |
||
166 |
val setup = |
|
12556 | 167 |
[CodegenData.init, |
12447 | 168 |
add_codegen "recfun" recfun_codegen, |
16645 | 169 |
add_attribute "" |
170 |
( Args.del |-- Scan.succeed del |
|
171 |
|| Scan.option (Args.$$$ "target" |-- Args.colon |-- Args.name) >> add)]; |
|
12447 | 172 |
|
173 |
end; |