author | haftmann |
Mon, 22 Sep 2008 08:00:24 +0200 | |
changeset 28308 | d4396a28fb29 |
parent 27691 | ce171cbd4b93 |
child 28703 | aef727ef30e5 |
permissions | -rw-r--r-- |
28308 | 1 |
(* Title: HOL/Tools/old_primrec_package.ML |
25557 | 2 |
ID: $Id$ |
3 |
Author: Stefan Berghofer, TU Muenchen and Norbert Voelker, FernUni Hagen |
|
4 |
||
5 |
Package for defining functions on datatypes by primitive recursion. |
|
6 |
*) |
|
7 |
||
8 |
signature OLD_PRIMREC_PACKAGE = |
|
9 |
sig |
|
10 |
val unify_consts: theory -> term list -> term list -> term list * term list |
|
11 |
val add_primrec: string -> ((bstring * string) * Attrib.src list) list |
|
12 |
-> theory -> thm list * theory |
|
13 |
val add_primrec_unchecked: string -> ((bstring * string) * Attrib.src list) list |
|
14 |
-> theory -> thm list * theory |
|
15 |
val add_primrec_i: string -> ((bstring * term) * attribute list) list |
|
16 |
-> theory -> thm list * theory |
|
17 |
val add_primrec_unchecked_i: string -> ((bstring * term) * attribute list) list |
|
18 |
-> theory -> thm list * theory |
|
19 |
end; |
|
20 |
||
21 |
structure OldPrimrecPackage : OLD_PRIMREC_PACKAGE = |
|
22 |
struct |
|
23 |
||
24 |
open DatatypeAux; |
|
25 |
||
26 |
exception RecError of string; |
|
27 |
||
28 |
fun primrec_err s = error ("Primrec definition error:\n" ^ s); |
|
29 |
fun primrec_eq_err thy s eq = |
|
26939
1035c89b4c02
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26478
diff
changeset
|
30 |
primrec_err (s ^ "\nin\n" ^ quote (Syntax.string_of_term_global thy eq)); |
25557 | 31 |
|
32 |
||
33 |
(*the following code ensures that each recursive set always has the |
|
34 |
same type in all introduction rules*) |
|
35 |
fun unify_consts thy cs intr_ts = |
|
36 |
(let |
|
37 |
val add_term_consts_2 = fold_aterms (fn Const c => insert (op =) c | _ => I); |
|
38 |
fun varify (t, (i, ts)) = |
|
39 |
let val t' = map_types (Logic.incr_tvar (i + 1)) (snd (Type.varify [] t)) |
|
40 |
in (maxidx_of_term t', t'::ts) end; |
|
41 |
val (i, cs') = foldr varify (~1, []) cs; |
|
42 |
val (i', intr_ts') = foldr varify (i, []) intr_ts; |
|
43 |
val rec_consts = fold add_term_consts_2 cs' []; |
|
44 |
val intr_consts = fold add_term_consts_2 intr_ts' []; |
|
45 |
fun unify (cname, cT) = |
|
46 |
let val consts = map snd (filter (fn (c, _) => c = cname) intr_consts) |
|
47 |
in fold (Sign.typ_unify thy) ((replicate (length consts) cT) ~~ consts) end; |
|
48 |
val (env, _) = fold unify rec_consts (Vartab.empty, i'); |
|
49 |
val subst = Type.freeze o map_types (Envir.norm_type env) |
|
50 |
||
51 |
in (map subst cs', map subst intr_ts') |
|
52 |
end) handle Type.TUNIFY => |
|
53 |
(warning "Occurrences of recursive constant have non-unifiable types"; (cs, intr_ts)); |
|
54 |
||
55 |
||
56 |
(* preprocessing of equations *) |
|
57 |
||
58 |
fun process_eqn thy eq rec_fns = |
|
59 |
let |
|
60 |
val (lhs, rhs) = |
|
61 |
if null (term_vars eq) then |
|
62 |
HOLogic.dest_eq (HOLogic.dest_Trueprop eq) |
|
63 |
handle TERM _ => raise RecError "not a proper equation" |
|
64 |
else raise RecError "illegal schematic variable(s)"; |
|
65 |
||
66 |
val (recfun, args) = strip_comb lhs; |
|
67 |
val fnameT = dest_Const recfun handle TERM _ => |
|
68 |
raise RecError "function is not declared as constant in theory"; |
|
69 |
||
70 |
val (ls', rest) = take_prefix is_Free args; |
|
71 |
val (middle, rs') = take_suffix is_Free rest; |
|
72 |
val rpos = length ls'; |
|
73 |
||
74 |
val (constr, cargs') = if null middle then raise RecError "constructor missing" |
|
75 |
else strip_comb (hd middle); |
|
76 |
val (cname, T) = dest_Const constr |
|
77 |
handle TERM _ => raise RecError "ill-formed constructor"; |
|
78 |
val (tname, _) = dest_Type (body_type T) handle TYPE _ => |
|
79 |
raise RecError "cannot determine datatype associated with function" |
|
80 |
||
81 |
val (ls, cargs, rs) = |
|
82 |
(map dest_Free ls', map dest_Free cargs', map dest_Free rs') |
|
83 |
handle TERM _ => raise RecError "illegal argument in pattern"; |
|
84 |
val lfrees = ls @ rs @ cargs; |
|
85 |
||
86 |
fun check_vars _ [] = () |
|
87 |
| check_vars s vars = raise RecError (s ^ commas_quote (map fst vars)) |
|
88 |
in |
|
89 |
if length middle > 1 then |
|
90 |
raise RecError "more than one non-variable in pattern" |
|
91 |
else |
|
92 |
(check_vars "repeated variable names in pattern: " (duplicates (op =) lfrees); |
|
93 |
check_vars "extra variables on rhs: " |
|
94 |
(map dest_Free (term_frees rhs) \\ lfrees); |
|
95 |
case AList.lookup (op =) rec_fns fnameT of |
|
96 |
NONE => |
|
97 |
(fnameT, (tname, rpos, [(cname, (ls, cargs, rs, rhs, eq))]))::rec_fns |
|
98 |
| SOME (_, rpos', eqns) => |
|
99 |
if AList.defined (op =) eqns cname then |
|
100 |
raise RecError "constructor already occurred as pattern" |
|
101 |
else if rpos <> rpos' then |
|
102 |
raise RecError "position of recursive argument inconsistent" |
|
103 |
else |
|
104 |
AList.update (op =) (fnameT, (tname, rpos, (cname, (ls, cargs, rs, rhs, eq))::eqns)) |
|
105 |
rec_fns) |
|
106 |
end |
|
107 |
handle RecError s => primrec_eq_err thy s eq; |
|
108 |
||
109 |
fun process_fun thy descr rec_eqns (i, fnameT as (fname, _)) (fnameTs, fnss) = |
|
110 |
let |
|
111 |
val (_, (tname, _, constrs)) = List.nth (descr, i); |
|
112 |
||
113 |
(* substitute "fname ls x rs" by "y ls rs" for (x, (_, y)) in subs *) |
|
114 |
||
115 |
fun subst [] t fs = (t, fs) |
|
116 |
| subst subs (Abs (a, T, t)) fs = |
|
117 |
fs |
|
118 |
|> subst subs t |
|
119 |
|-> (fn t' => pair (Abs (a, T, t'))) |
|
120 |
| subst subs (t as (_ $ _)) fs = |
|
121 |
let |
|
122 |
val (f, ts) = strip_comb t; |
|
123 |
in |
|
124 |
if is_Const f andalso dest_Const f mem map fst rec_eqns then |
|
125 |
let |
|
126 |
val fnameT' as (fname', _) = dest_Const f; |
|
127 |
val (_, rpos, _) = the (AList.lookup (op =) rec_eqns fnameT'); |
|
128 |
val ls = Library.take (rpos, ts); |
|
129 |
val rest = Library.drop (rpos, ts); |
|
130 |
val (x', rs) = (hd rest, tl rest) |
|
131 |
handle Empty => raise RecError ("not enough arguments\ |
|
132 |
\ in recursive application\nof function " ^ quote fname' ^ " on rhs"); |
|
133 |
val (x, xs) = strip_comb x' |
|
134 |
in case AList.lookup (op =) subs x |
|
135 |
of NONE => |
|
136 |
fs |
|
137 |
|> fold_map (subst subs) ts |
|
138 |
|-> (fn ts' => pair (list_comb (f, ts'))) |
|
139 |
| SOME (i', y) => |
|
140 |
fs |
|
141 |
|> fold_map (subst subs) (xs @ ls @ rs) |
|
142 |
||> process_fun thy descr rec_eqns (i', fnameT') |
|
143 |
|-> (fn ts' => pair (list_comb (y, ts'))) |
|
144 |
end |
|
145 |
else |
|
146 |
fs |
|
147 |
|> fold_map (subst subs) (f :: ts) |
|
148 |
|-> (fn (f'::ts') => pair (list_comb (f', ts'))) |
|
149 |
end |
|
150 |
| subst _ t fs = (t, fs); |
|
151 |
||
152 |
(* translate rec equations into function arguments suitable for rec comb *) |
|
153 |
||
154 |
fun trans eqns (cname, cargs) (fnameTs', fnss', fns) = |
|
155 |
(case AList.lookup (op =) eqns cname of |
|
156 |
NONE => (warning ("No equation for constructor " ^ quote cname ^ |
|
157 |
"\nin definition of function " ^ quote fname); |
|
158 |
(fnameTs', fnss', (Const ("HOL.undefined", dummyT))::fns)) |
|
159 |
| SOME (ls, cargs', rs, rhs, eq) => |
|
160 |
let |
|
161 |
val recs = filter (is_rec_type o snd) (cargs' ~~ cargs); |
|
162 |
val rargs = map fst recs; |
|
163 |
val subs = map (rpair dummyT o fst) |
|
164 |
(rev (rename_wrt_term rhs rargs)); |
|
165 |
val (rhs', (fnameTs'', fnss'')) = |
|
166 |
(subst (map (fn ((x, y), z) => |
|
167 |
(Free x, (body_index y, Free z))) |
|
168 |
(recs ~~ subs)) rhs (fnameTs', fnss')) |
|
169 |
handle RecError s => primrec_eq_err thy s eq |
|
170 |
in (fnameTs'', fnss'', |
|
171 |
(list_abs_free (cargs' @ subs @ ls @ rs, rhs'))::fns) |
|
172 |
end) |
|
173 |
||
174 |
in (case AList.lookup (op =) fnameTs i of |
|
175 |
NONE => |
|
176 |
if exists (equal fnameT o snd) fnameTs then |
|
177 |
raise RecError ("inconsistent functions for datatype " ^ quote tname) |
|
178 |
else |
|
179 |
let |
|
180 |
val (_, _, eqns) = the (AList.lookup (op =) rec_eqns fnameT); |
|
181 |
val (fnameTs', fnss', fns) = fold_rev (trans eqns) constrs |
|
182 |
((i, fnameT)::fnameTs, fnss, []) |
|
183 |
in |
|
184 |
(fnameTs', (i, (fname, #1 (snd (hd eqns)), fns))::fnss') |
|
185 |
end |
|
186 |
| SOME fnameT' => |
|
187 |
if fnameT = fnameT' then (fnameTs, fnss) |
|
188 |
else raise RecError ("inconsistent functions for datatype " ^ quote tname)) |
|
189 |
end; |
|
190 |
||
191 |
||
192 |
(* prepare functions needed for definitions *) |
|
193 |
||
194 |
fun get_fns fns ((i : int, (tname, _, constrs)), rec_name) (fs, defs) = |
|
195 |
case AList.lookup (op =) fns i of |
|
196 |
NONE => |
|
197 |
let |
|
198 |
val dummy_fns = map (fn (_, cargs) => Const ("HOL.undefined", |
|
199 |
replicate ((length cargs) + (length (List.filter is_rec_type cargs))) |
|
200 |
dummyT ---> HOLogic.unitT)) constrs; |
|
201 |
val _ = warning ("No function definition for datatype " ^ quote tname) |
|
202 |
in |
|
203 |
(dummy_fns @ fs, defs) |
|
204 |
end |
|
205 |
| SOME (fname, ls, fs') => (fs' @ fs, (fname, ls, rec_name, tname) :: defs); |
|
206 |
||
207 |
||
208 |
(* make definition *) |
|
209 |
||
210 |
fun make_def thy fs (fname, ls, rec_name, tname) = |
|
211 |
let |
|
212 |
val rhs = fold_rev (fn T => fn t => Abs ("", T, t)) |
|
213 |
((map snd ls) @ [dummyT]) |
|
214 |
(list_comb (Const (rec_name, dummyT), |
|
215 |
fs @ map Bound (0 ::(length ls downto 1)))) |
|
216 |
val def_name = Sign.base_name fname ^ "_" ^ Sign.base_name tname ^ "_def"; |
|
217 |
val def_prop = |
|
218 |
singleton (Syntax.check_terms (ProofContext.init thy)) |
|
219 |
(Logic.mk_equals (Const (fname, dummyT), rhs)); |
|
220 |
in (def_name, def_prop) end; |
|
221 |
||
222 |
||
223 |
(* find datatypes which contain all datatypes in tnames' *) |
|
224 |
||
225 |
fun find_dts (dt_info : datatype_info Symtab.table) _ [] = [] |
|
226 |
| find_dts dt_info tnames' (tname::tnames) = |
|
227 |
(case Symtab.lookup dt_info tname of |
|
228 |
NONE => primrec_err (quote tname ^ " is not a datatype") |
|
229 |
| SOME dt => |
|
230 |
if tnames' subset (map (#1 o snd) (#descr dt)) then |
|
231 |
(tname, dt)::(find_dts dt_info tnames' tnames) |
|
232 |
else find_dts dt_info tnames' tnames); |
|
233 |
||
234 |
fun prepare_induct ({descr, induction, ...}: datatype_info) rec_eqns = |
|
235 |
let |
|
236 |
fun constrs_of (_, (_, _, cs)) = |
|
237 |
map (fn (cname:string, (_, cargs, _, _, _)) => (cname, map fst cargs)) cs; |
|
238 |
val params_of = these o AList.lookup (op =) (List.concat (map constrs_of rec_eqns)); |
|
239 |
in |
|
240 |
induction |
|
241 |
|> RuleCases.rename_params (map params_of (List.concat (map (map #1 o #3 o #2) descr))) |
|
242 |
|> RuleCases.save induction |
|
243 |
end; |
|
244 |
||
245 |
local |
|
246 |
||
247 |
fun gen_primrec_i note def alt_name eqns_atts thy = |
|
248 |
let |
|
249 |
val (eqns, atts) = split_list eqns_atts; |
|
250 |
val dt_info = DatatypePackage.get_datatypes thy; |
|
251 |
val rec_eqns = fold_rev (process_eqn thy o snd) eqns [] ; |
|
252 |
val tnames = distinct (op =) (map (#1 o snd) rec_eqns); |
|
253 |
val dts = find_dts dt_info tnames tnames; |
|
254 |
val main_fns = |
|
255 |
map (fn (tname, {index, ...}) => |
|
256 |
(index, |
|
257 |
(fst o the o find_first (fn f => (#1 o snd) f = tname)) rec_eqns)) |
|
258 |
dts; |
|
259 |
val {descr, rec_names, rec_rewrites, ...} = |
|
260 |
if null dts then |
|
261 |
primrec_err ("datatypes " ^ commas_quote tnames ^ "\nare not mutually recursive") |
|
262 |
else snd (hd dts); |
|
263 |
val (fnameTs, fnss) = |
|
264 |
fold_rev (process_fun thy descr rec_eqns) main_fns ([], []); |
|
265 |
val (fs, defs) = fold_rev (get_fns fnss) (descr ~~ rec_names) ([], []); |
|
266 |
val defs' = map (make_def thy fs) defs; |
|
267 |
val nameTs1 = map snd fnameTs; |
|
268 |
val nameTs2 = map fst rec_eqns; |
|
269 |
val _ = if gen_eq_set (op =) (nameTs1, nameTs2) then () |
|
270 |
else primrec_err ("functions " ^ commas_quote (map fst nameTs2) ^ |
|
271 |
"\nare not mutually recursive"); |
|
272 |
val primrec_name = |
|
273 |
if alt_name = "" then (space_implode "_" (map (Sign.base_name o #1) defs)) else alt_name; |
|
274 |
val (defs_thms', thy') = |
|
275 |
thy |
|
276 |
|> Sign.add_path primrec_name |
|
277 |
|> fold_map def (map (fn (name, t) => ((name, []), t)) defs'); |
|
278 |
val rewrites = (map mk_meta_eq rec_rewrites) @ map snd defs_thms'; |
|
279 |
val simps = map (fn (_, t) => Goal.prove_global thy' [] [] t |
|
280 |
(fn _ => EVERY [rewrite_goals_tac rewrites, rtac refl 1])) eqns; |
|
281 |
val (simps', thy'') = |
|
282 |
thy' |
|
283 |
|> fold_map note ((map fst eqns ~~ atts) ~~ map single simps); |
|
284 |
val simps'' = maps snd simps'; |
|
285 |
in |
|
286 |
thy'' |
|
287 |
|> note (("simps", [Simplifier.simp_add, RecfunCodegen.add_default]), simps'') |
|
288 |
|> snd |
|
289 |
|> note (("induct", []), [prepare_induct (#2 (hd dts)) rec_eqns]) |
|
290 |
|> snd |
|
291 |
|> Sign.parent_path |
|
292 |
|> pair simps'' |
|
293 |
end; |
|
294 |
||
295 |
fun gen_primrec note def alt_name eqns thy = |
|
296 |
let |
|
297 |
val ((names, strings), srcss) = apfst split_list (split_list eqns); |
|
298 |
val atts = map (map (Attrib.attribute thy)) srcss; |
|
299 |
val eqn_ts = map (fn s => Syntax.read_prop_global thy s |
|
300 |
handle ERROR msg => cat_error msg ("The error(s) above occurred for " ^ s)) strings; |
|
301 |
val rec_ts = map (fn eq => head_of (fst (HOLogic.dest_eq (HOLogic.dest_Trueprop eq))) |
|
302 |
handle TERM _ => primrec_eq_err thy "not a proper equation" eq) eqn_ts; |
|
303 |
val (_, eqn_ts') = unify_consts thy rec_ts eqn_ts |
|
304 |
in |
|
305 |
gen_primrec_i note def alt_name (names ~~ eqn_ts' ~~ atts) thy |
|
306 |
end; |
|
307 |
||
308 |
fun thy_note ((name, atts), thms) = |
|
309 |
PureThy.add_thmss [((name, thms), atts)] #-> (fn [thms] => pair (name, thms)); |
|
310 |
fun thy_def false ((name, atts), t) = |
|
27691
ce171cbd4b93
PureThy: dropped note_thmss_qualified, dropped _i suffix
haftmann
parents:
26939
diff
changeset
|
311 |
PureThy.add_defs false [((name, t), atts)] #-> (fn [thm] => pair (name, thm)) |
25557 | 312 |
| thy_def true ((name, atts), t) = |
27691
ce171cbd4b93
PureThy: dropped note_thmss_qualified, dropped _i suffix
haftmann
parents:
26939
diff
changeset
|
313 |
PureThy.add_defs_unchecked false [((name, t), atts)] #-> (fn [thm] => pair (name, thm)); |
25557 | 314 |
|
315 |
in |
|
316 |
||
317 |
val add_primrec = gen_primrec thy_note (thy_def false); |
|
318 |
val add_primrec_unchecked = gen_primrec thy_note (thy_def true); |
|
319 |
val add_primrec_i = gen_primrec_i thy_note (thy_def false); |
|
320 |
val add_primrec_unchecked_i = gen_primrec_i thy_note (thy_def true); |
|
321 |
fun gen_primrec note def alt_name specs = |
|
322 |
gen_primrec_i note def alt_name (map (fn ((name, t), atts) => ((name, atts), t)) specs); |
|
323 |
||
324 |
end; |
|
325 |
||
326 |
||
327 |
(* see primrecr_package.ML (* outer syntax *) |
|
328 |
||
329 |
local structure P = OuterParse and K = OuterKeyword in |
|
330 |
||
331 |
val opt_unchecked_name = |
|
332 |
Scan.optional (P.$$$ "(" |-- P.!!! |
|
333 |
(((P.$$$ "unchecked" >> K true) -- Scan.optional P.name "" || |
|
334 |
P.name >> pair false) --| P.$$$ ")")) (false, ""); |
|
335 |
||
336 |
val primrec_decl = |
|
337 |
opt_unchecked_name -- Scan.repeat1 (SpecParse.opt_thm_name ":" -- P.prop); |
|
338 |
||
339 |
val _ = |
|
340 |
OuterSyntax.command "primrec" "define primitive recursive functions on datatypes" K.thy_decl |
|
341 |
(primrec_decl >> (fn ((unchecked, alt_name), eqns) => |
|
342 |
Toplevel.theory (snd o |
|
343 |
(if unchecked then add_primrec_unchecked else add_primrec) alt_name |
|
344 |
(map P.triple_swap eqns)))); |
|
345 |
||
346 |
end;*) |
|
347 |
||
348 |
end; |