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