author | haftmann |
Tue, 07 Jul 2009 17:37:00 +0200 | |
changeset 31954 | 8db19c99b00a |
parent 31156 | 90fed3d4430f |
child 31958 | 2133f596c520 |
permissions | -rw-r--r-- |
24584 | 1 |
(* Title: HOL/Tools/recfun_codegen.ML |
12447 | 2 |
Author: Stefan Berghofer, TU Muenchen |
3 |
||
4 |
Code generator for recursive functions. |
|
5 |
*) |
|
6 |
||
7 |
signature RECFUN_CODEGEN = |
|
8 |
sig |
|
18708 | 9 |
val setup: theory -> theory |
12447 | 10 |
end; |
11 |
||
12 |
structure RecfunCodegen : RECFUN_CODEGEN = |
|
13 |
struct |
|
14 |
||
15 |
open Codegen; |
|
16 |
||
28522 | 17 |
structure ModuleData = TheoryDataFun |
22846 | 18 |
( |
28522 | 19 |
type T = string Symtab.table; |
12447 | 20 |
val empty = Symtab.empty; |
21 |
val copy = I; |
|
16424 | 22 |
val extend = I; |
28522 | 23 |
fun merge _ = Symtab.merge (K true); |
22846 | 24 |
); |
12447 | 25 |
|
28703 | 26 |
fun add_thm NONE thm thy = Code.add_eqn thm thy |
27 |
| add_thm (SOME module_name) thm thy = |
|
31090
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31087
diff
changeset
|
28 |
let |
31156 | 29 |
val (thm', _) = Code.mk_eqn thy (K false) (thm, true) |
31090
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31087
diff
changeset
|
30 |
in |
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31087
diff
changeset
|
31 |
thy |
31156 | 32 |
|> ModuleData.map (Symtab.update (Code.const_eqn thy thm', module_name)) |
31090
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31087
diff
changeset
|
33 |
|> Code.add_eqn thm' |
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31087
diff
changeset
|
34 |
end; |
24624
b8383b1bbae3
distinction between regular and default code theorems
haftmann
parents:
24584
diff
changeset
|
35 |
|
28522 | 36 |
fun expand_eta thy [] = [] |
37 |
| expand_eta thy (thms as thm :: _) = |
|
38 |
let |
|
31156 | 39 |
val (_, ty) = Code.const_typ_eqn thm; |
29272 | 40 |
in if null (Term.add_tvarsT ty []) orelse (null o fst o strip_type) ty |
28522 | 41 |
then thms |
31156 | 42 |
else map (Code.expand_eta thy 1) thms |
28522 | 43 |
end; |
14196 | 44 |
|
28522 | 45 |
fun retrieve_equations thy (c, T) = if c = @{const_name "op ="} then NONE else |
46 |
let |
|
47 |
val c' = AxClass.unoverload_const thy (c, T); |
|
48 |
val opt_name = Symtab.lookup (ModuleData.get thy) c'; |
|
31125
80218ee73167
transferred code generator preprocessor into separate module
haftmann
parents:
31090
diff
changeset
|
49 |
val thms = Code.these_eqns thy c' |
28522 | 50 |
|> map_filter (fn (thm, linear) => if linear then SOME thm else NONE) |
51 |
|> expand_eta thy |
|
31156 | 52 |
|> Code.norm_varnames thy |
28522 | 53 |
|> map (rpair opt_name) |
54 |
in if null thms then NONE else SOME thms end; |
|
55 |
||
31954
8db19c99b00a
Stefan Berghofer's code generator uses Pure equality instead of HOL equality now
haftmann
parents:
31156
diff
changeset
|
56 |
val dest_eqn = Logic.dest_equals; |
28522 | 57 |
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
|
58 |
|
16645 | 59 |
fun get_equations thy defs (s, T) = |
28522 | 60 |
(case retrieve_equations thy (s, T) of |
16645 | 61 |
NONE => ([], "") |
62 |
| SOME thms => |
|
28522 | 63 |
let val thms' = filter (fn (thm, _) => is_instance T |
64 |
(snd (const_of (prop_of thm)))) thms |
|
16645 | 65 |
in if null thms' then ([], "") |
66 |
else (preprocess thy (map fst thms'), |
|
67 |
case snd (snd (split_last thms')) of |
|
68 |
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
|
69 |
NONE => Codegen.thyname_of_const thy s |
16645 | 70 |
| SOME ((_, (thyname, _)), _) => thyname) |
71 |
| SOME thyname => thyname) |
|
72 |
end); |
|
12447 | 73 |
|
16645 | 74 |
fun mk_suffix thy defs (s, T) = (case get_defn thy defs s T of |
17144 | 75 |
SOME (_, SOME i) => " def" ^ string_of_int i | _ => ""); |
12447 | 76 |
|
77 |
exception EQN of string * typ * string; |
|
78 |
||
22887 | 79 |
fun cycle g (xs, x : string) = |
80 |
if member (op =) xs x then xs |
|
81 |
else Library.foldl (cycle g) (x :: xs, flat (Graph.all_paths (fst g) (x, x))); |
|
12447 | 82 |
|
28535 | 83 |
fun add_rec_funs thy defs dep module eqs gr = |
12447 | 84 |
let |
16645 | 85 |
fun dest_eq t = (fst (const_of t) ^ mk_suffix thy defs (const_of t), |
86 |
dest_eqn (rename_term t)); |
|
12447 | 87 |
val eqs' = map dest_eq eqs; |
88 |
val (dname, _) :: _ = eqs'; |
|
89 |
val (s, T) = const_of (hd eqs); |
|
90 |
||
28535 | 91 |
fun mk_fundef module fname first [] gr = ([], gr) |
92 |
| mk_fundef module fname first ((fname' : string, (lhs, rhs)) :: xs) gr = |
|
12447 | 93 |
let |
28535 | 94 |
val (pl, gr1) = invoke_codegen thy defs dname module false lhs gr; |
95 |
val (pr, gr2) = invoke_codegen thy defs dname module false rhs gr1; |
|
96 |
val (rest, gr3) = mk_fundef module fname' false xs gr2 ; |
|
97 |
val (ty, gr4) = invoke_tycodegen thy defs dname module false T gr3; |
|
98 |
val num_args = (length o snd o strip_comb) lhs; |
|
99 |
val prfx = if fname = fname' then " |" |
|
100 |
else if not first then "and" |
|
101 |
else if num_args = 0 then "val" |
|
102 |
else "fun"; |
|
103 |
val pl' = Pretty.breaks (str prfx |
|
104 |
:: (if num_args = 0 then [pl, str ":", ty] else [pl])); |
|
12447 | 105 |
in |
28535 | 106 |
(Pretty.blk (4, pl' |
107 |
@ [str " =", Pretty.brk 1, pr]) :: rest, gr4) |
|
12447 | 108 |
end; |
109 |
||
17144 | 110 |
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
|
111 |
(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
|
112 |
separate Pretty.fbrk fundef @ [str ";"])) ^ "\n\n")); |
12447 | 113 |
|
114 |
in |
|
17144 | 115 |
(case try (get_node gr) dname of |
15531 | 116 |
NONE => |
12447 | 117 |
let |
17144 | 118 |
val gr1 = add_edge (dname, dep) |
119 |
(new_node (dname, (SOME (EQN (s, T, "")), module, "")) gr); |
|
28535 | 120 |
val (fundef, gr2) = mk_fundef module "" true eqs' gr1 ; |
12447 | 121 |
val xs = cycle gr2 ([], dname); |
17144 | 122 |
val cs = map (fn x => case get_node gr2 x of |
16645 | 123 |
(SOME (EQN (s, T, _)), _, _) => (s, T) |
12447 | 124 |
| _ => error ("RecfunCodegen: illegal cyclic dependencies:\n" ^ |
125 |
implode (separate ", " xs))) xs |
|
126 |
in (case xs of |
|
28535 | 127 |
[_] => (module, put_code module fundef gr2) |
12447 | 128 |
| _ => |
129 |
if not (dep mem xs) then |
|
130 |
let |
|
16645 | 131 |
val thmss as (_, thyname) :: _ = map (get_equations thy defs) cs; |
17144 | 132 |
val module' = if_library thyname module; |
16645 | 133 |
val eqs'' = map (dest_eq o prop_of) (List.concat (map fst thmss)); |
28535 | 134 |
val (fundef', gr3) = mk_fundef module' "" true eqs'' |
17144 | 135 |
(add_edge (dname, dep) |
30190 | 136 |
(List.foldr (uncurry new_node) (del_nodes xs gr2) |
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
137 |
(map (fn k => |
28535 | 138 |
(k, (SOME (EQN ("", dummyT, dname)), module', ""))) xs))) |
139 |
in (module', put_code module' fundef' gr3) end |
|
140 |
else (module, gr2)) |
|
12447 | 141 |
end |
17144 | 142 |
| SOME (SOME (EQN (_, _, s)), module', _) => |
28535 | 143 |
(module', if s = "" then |
17144 | 144 |
if dname = dep then gr else add_edge (dname, dep) gr |
28535 | 145 |
else if s = dep then gr else add_edge (s, dep) gr)) |
12447 | 146 |
end; |
147 |
||
28535 | 148 |
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
|
149 |
(Const (p as (s, T)), ts) => (case (get_equations thy defs p, get_assoc_code thy (s, T)) of |
16645 | 150 |
(([], _), _) => NONE |
15531 | 151 |
| (_, SOME _) => NONE |
17144 | 152 |
| ((eqns, thyname), NONE) => |
16645 | 153 |
let |
17144 | 154 |
val module' = if_library thyname module; |
28535 | 155 |
val (ps, gr') = fold_map |
156 |
(invoke_codegen thy defs dep module true) ts gr; |
|
17144 | 157 |
val suffix = mk_suffix thy defs p; |
28535 | 158 |
val (module'', gr'') = |
159 |
add_rec_funs thy defs dep module' (map prop_of eqns) gr'; |
|
160 |
val (fname, gr''') = mk_const_id module'' (s ^ suffix) gr'' |
|
12447 | 161 |
in |
28535 | 162 |
SOME (mk_app brack (str (mk_qual_id module fname)) ps, gr''') |
12447 | 163 |
end) |
15531 | 164 |
| _ => NONE); |
12447 | 165 |
|
28703 | 166 |
val setup = let |
167 |
fun add opt_module = Thm.declaration_attribute (fn thm => Context.mapping |
|
168 |
(add_thm opt_module thm) I); |
|
169 |
val del = Thm.declaration_attribute (fn thm => Context.mapping |
|
170 |
(Code.del_eqn thm) I); |
|
171 |
in |
|
24219 | 172 |
add_codegen "recfun" recfun_codegen |
173 |
#> Code.add_attribute ("", Args.del |-- Scan.succeed del |
|
28703 | 174 |
|| Scan.option (Args.$$$ "target" |-- Args.colon |-- Args.name) >> add) |
175 |
end; |
|
12447 | 176 |
|
177 |
end; |