author | haftmann |
Tue, 28 Oct 2008 16:58:59 +0100 | |
changeset 28703 | aef727ef30e5 |
parent 28535 | 38fb0780b58b |
child 29272 | fb3ccf499df5 |
permissions | -rw-r--r-- |
24584 | 1 |
(* Title: HOL/Tools/recfun_codegen.ML |
12447 | 2 |
ID: $Id$ |
3 |
Author: Stefan Berghofer, TU Muenchen |
|
4 |
||
5 |
Code generator for recursive functions. |
|
6 |
*) |
|
7 |
||
8 |
signature RECFUN_CODEGEN = |
|
9 |
sig |
|
18708 | 10 |
val setup: theory -> theory |
12447 | 11 |
end; |
12 |
||
13 |
structure RecfunCodegen : RECFUN_CODEGEN = |
|
14 |
struct |
|
15 |
||
16 |
open Codegen; |
|
17 |
||
28522 | 18 |
structure ModuleData = TheoryDataFun |
22846 | 19 |
( |
28522 | 20 |
type T = string Symtab.table; |
12447 | 21 |
val empty = Symtab.empty; |
22 |
val copy = I; |
|
16424 | 23 |
val extend = I; |
28522 | 24 |
fun merge _ = Symtab.merge (K true); |
22846 | 25 |
); |
12447 | 26 |
|
28703 | 27 |
fun add_thm NONE thm thy = Code.add_eqn thm thy |
28 |
| add_thm (SOME module_name) thm thy = |
|
28522 | 29 |
case Code_Unit.warning_thm (Code_Unit.mk_eqn thy) thm |
30 |
of SOME (thm', _) => let val c = Code_Unit.const_eqn thm' |
|
31 |
in thy |
|
32 |
|> ModuleData.map (Symtab.update (c, module_name)) |
|
28703 | 33 |
|> Code.add_eqn thm' |
28522 | 34 |
end |
28703 | 35 |
| NONE => Code.add_eqn thm thy; |
24624
b8383b1bbae3
distinction between regular and default code theorems
haftmann
parents:
24584
diff
changeset
|
36 |
|
28522 | 37 |
fun meta_eq_to_obj_eq thy thm = |
38 |
let |
|
39 |
val T = (fastype_of o fst o Logic.dest_equals o Thm.prop_of) thm; |
|
40 |
in if Sign.of_sort thy (T, @{sort type}) |
|
41 |
then SOME (Conv.fconv_rule Drule.beta_eta_conversion (@{thm meta_eq_to_obj_eq} OF [thm])) |
|
42 |
else NONE |
|
43 |
end; |
|
19344 | 44 |
|
28522 | 45 |
fun expand_eta thy [] = [] |
46 |
| expand_eta thy (thms as thm :: _) = |
|
47 |
let |
|
48 |
val (_, ty) = Code_Unit.const_typ_eqn thm; |
|
49 |
in if (null o Term.typ_tvars) ty orelse (null o fst o strip_type) ty |
|
50 |
then thms |
|
51 |
else map (Code_Unit.expand_eta thy 1) thms |
|
52 |
end; |
|
14196 | 53 |
|
28522 | 54 |
fun retrieve_equations thy (c, T) = if c = @{const_name "op ="} then NONE else |
55 |
let |
|
56 |
val c' = AxClass.unoverload_const thy (c, T); |
|
57 |
val opt_name = Symtab.lookup (ModuleData.get thy) c'; |
|
58 |
val thms = Code.these_raw_eqns thy c' |
|
59 |
|> map_filter (fn (thm, linear) => if linear then SOME thm else NONE) |
|
60 |
|> expand_eta thy |
|
61 |
|> map (AxClass.overload thy) |
|
62 |
|> map_filter (meta_eq_to_obj_eq thy) |
|
63 |
|> Code_Unit.norm_varnames thy Code_Name.purify_tvar Code_Name.purify_var |
|
64 |
|> map (rpair opt_name) |
|
65 |
in if null thms then NONE else SOME thms end; |
|
66 |
||
67 |
val dest_eqn = HOLogic.dest_eq o HOLogic.dest_Trueprop; |
|
68 |
val const_of = dest_Const o head_of o fst o dest_eqn; |
|
15321
694f9d3ce90d
Reimplemented some operations on "code lemma" table to avoid that code
berghofe
parents:
15257
diff
changeset
|
69 |
|
16645 | 70 |
fun get_equations thy defs (s, T) = |
28522 | 71 |
(case retrieve_equations thy (s, T) of |
16645 | 72 |
NONE => ([], "") |
73 |
| SOME thms => |
|
28522 | 74 |
let val thms' = filter (fn (thm, _) => is_instance T |
75 |
(snd (const_of (prop_of thm)))) thms |
|
16645 | 76 |
in if null thms' then ([], "") |
77 |
else (preprocess thy (map fst thms'), |
|
78 |
case snd (snd (split_last thms')) of |
|
79 |
NONE => (case get_defn thy defs s T of |
|
27398
768da1da59d6
simplified retrieval of theory names of consts and types
haftmann
parents:
26975
diff
changeset
|
80 |
NONE => Codegen.thyname_of_const thy s |
16645 | 81 |
| SOME ((_, (thyname, _)), _) => thyname) |
82 |
| SOME thyname => thyname) |
|
83 |
end); |
|
12447 | 84 |
|
16645 | 85 |
fun mk_suffix thy defs (s, T) = (case get_defn thy defs s T of |
17144 | 86 |
SOME (_, SOME i) => " def" ^ string_of_int i | _ => ""); |
12447 | 87 |
|
88 |
exception EQN of string * typ * string; |
|
89 |
||
22887 | 90 |
fun cycle g (xs, x : string) = |
91 |
if member (op =) xs x then xs |
|
92 |
else Library.foldl (cycle g) (x :: xs, flat (Graph.all_paths (fst g) (x, x))); |
|
12447 | 93 |
|
28535 | 94 |
fun add_rec_funs thy defs dep module eqs gr = |
12447 | 95 |
let |
16645 | 96 |
fun dest_eq t = (fst (const_of t) ^ mk_suffix thy defs (const_of t), |
97 |
dest_eqn (rename_term t)); |
|
12447 | 98 |
val eqs' = map dest_eq eqs; |
99 |
val (dname, _) :: _ = eqs'; |
|
100 |
val (s, T) = const_of (hd eqs); |
|
101 |
||
28535 | 102 |
fun mk_fundef module fname first [] gr = ([], gr) |
103 |
| mk_fundef module fname first ((fname' : string, (lhs, rhs)) :: xs) gr = |
|
12447 | 104 |
let |
28535 | 105 |
val (pl, gr1) = invoke_codegen thy defs dname module false lhs gr; |
106 |
val (pr, gr2) = invoke_codegen thy defs dname module false rhs gr1; |
|
107 |
val (rest, gr3) = mk_fundef module fname' false xs gr2 ; |
|
108 |
val (ty, gr4) = invoke_tycodegen thy defs dname module false T gr3; |
|
109 |
val num_args = (length o snd o strip_comb) lhs; |
|
110 |
val prfx = if fname = fname' then " |" |
|
111 |
else if not first then "and" |
|
112 |
else if num_args = 0 then "val" |
|
113 |
else "fun"; |
|
114 |
val pl' = Pretty.breaks (str prfx |
|
115 |
:: (if num_args = 0 then [pl, str ":", ty] else [pl])); |
|
12447 | 116 |
in |
28535 | 117 |
(Pretty.blk (4, pl' |
118 |
@ [str " =", Pretty.brk 1, pr]) :: rest, gr4) |
|
12447 | 119 |
end; |
120 |
||
17144 | 121 |
fun put_code module fundef = map_node dname |
26975
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26928
diff
changeset
|
122 |
(K (SOME (EQN ("", dummyT, dname)), module, string_of (Pretty.blk (0, |
103dca19ef2e
Replaced Pretty.str and Pretty.string_of by specific functions (from Codegen) that
berghofe
parents:
26928
diff
changeset
|
123 |
separate Pretty.fbrk fundef @ [str ";"])) ^ "\n\n")); |
12447 | 124 |
|
125 |
in |
|
17144 | 126 |
(case try (get_node gr) dname of |
15531 | 127 |
NONE => |
12447 | 128 |
let |
17144 | 129 |
val gr1 = add_edge (dname, dep) |
130 |
(new_node (dname, (SOME (EQN (s, T, "")), module, "")) gr); |
|
28535 | 131 |
val (fundef, gr2) = mk_fundef module "" true eqs' gr1 ; |
12447 | 132 |
val xs = cycle gr2 ([], dname); |
17144 | 133 |
val cs = map (fn x => case get_node gr2 x of |
16645 | 134 |
(SOME (EQN (s, T, _)), _, _) => (s, T) |
12447 | 135 |
| _ => error ("RecfunCodegen: illegal cyclic dependencies:\n" ^ |
136 |
implode (separate ", " xs))) xs |
|
137 |
in (case xs of |
|
28535 | 138 |
[_] => (module, put_code module fundef gr2) |
12447 | 139 |
| _ => |
140 |
if not (dep mem xs) then |
|
141 |
let |
|
16645 | 142 |
val thmss as (_, thyname) :: _ = map (get_equations thy defs) cs; |
17144 | 143 |
val module' = if_library thyname module; |
16645 | 144 |
val eqs'' = map (dest_eq o prop_of) (List.concat (map fst thmss)); |
28535 | 145 |
val (fundef', gr3) = mk_fundef module' "" true eqs'' |
17144 | 146 |
(add_edge (dname, dep) |
147 |
(foldr (uncurry new_node) (del_nodes xs gr2) |
|
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
148 |
(map (fn k => |
28535 | 149 |
(k, (SOME (EQN ("", dummyT, dname)), module', ""))) xs))) |
150 |
in (module', put_code module' fundef' gr3) end |
|
151 |
else (module, gr2)) |
|
12447 | 152 |
end |
17144 | 153 |
| SOME (SOME (EQN (_, _, s)), module', _) => |
28535 | 154 |
(module', if s = "" then |
17144 | 155 |
if dname = dep then gr else add_edge (dname, dep) gr |
28535 | 156 |
else if s = dep then gr else add_edge (s, dep) gr)) |
12447 | 157 |
end; |
158 |
||
28535 | 159 |
fun recfun_codegen thy defs dep module brack t gr = (case strip_comb t of |
22921
475ff421a6a3
consts in consts_code Isar commands are now referred to by usual term syntax
haftmann
parents:
22887
diff
changeset
|
160 |
(Const (p as (s, T)), ts) => (case (get_equations thy defs p, get_assoc_code thy (s, T)) of |
16645 | 161 |
(([], _), _) => NONE |
15531 | 162 |
| (_, SOME _) => NONE |
17144 | 163 |
| ((eqns, thyname), NONE) => |
16645 | 164 |
let |
17144 | 165 |
val module' = if_library thyname module; |
28535 | 166 |
val (ps, gr') = fold_map |
167 |
(invoke_codegen thy defs dep module true) ts gr; |
|
17144 | 168 |
val suffix = mk_suffix thy defs p; |
28535 | 169 |
val (module'', gr'') = |
170 |
add_rec_funs thy defs dep module' (map prop_of eqns) gr'; |
|
171 |
val (fname, gr''') = mk_const_id module'' (s ^ suffix) gr'' |
|
12447 | 172 |
in |
28535 | 173 |
SOME (mk_app brack (str (mk_qual_id module fname)) ps, gr''') |
12447 | 174 |
end) |
15531 | 175 |
| _ => NONE); |
12447 | 176 |
|
28703 | 177 |
val setup = let |
178 |
fun add opt_module = Thm.declaration_attribute (fn thm => Context.mapping |
|
179 |
(add_thm opt_module thm) I); |
|
180 |
val del = Thm.declaration_attribute (fn thm => Context.mapping |
|
181 |
(Code.del_eqn thm) I); |
|
182 |
in |
|
24219 | 183 |
add_codegen "recfun" recfun_codegen |
184 |
#> Code.add_attribute ("", Args.del |-- Scan.succeed del |
|
28703 | 185 |
|| Scan.option (Args.$$$ "target" |-- Args.colon |-- Args.name) >> add) |
186 |
end; |
|
12447 | 187 |
|
188 |
end; |