5719
|
1 |
(* Title: HOL/Tools/primrec_package.ML
|
5178
|
2 |
ID: $Id$
|
5719
|
3 |
Author: Stefan Berghofer and Norbert Voelker
|
5178
|
4 |
Copyright 1998 TU Muenchen
|
|
5 |
|
6359
|
6 |
Package for defining functions on datatypes by primitive recursion.
|
|
7 |
|
|
8 |
TODO:
|
|
9 |
- add_primrec(_i): improve prep of args;
|
|
10 |
- quiet_mode (!?);
|
5178
|
11 |
*)
|
|
12 |
|
|
13 |
signature PRIMREC_PACKAGE =
|
|
14 |
sig
|
6359
|
15 |
val add_primrec: string -> ((string * string) * Args.src list) list
|
|
16 |
-> theory -> theory * thm list
|
|
17 |
val add_primrec_i: string -> ((string * term) * theory attribute list) list
|
|
18 |
-> theory -> theory * thm list
|
5178
|
19 |
end;
|
|
20 |
|
|
21 |
structure PrimrecPackage : 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 sign s eq =
|
|
30 |
primrec_err (s ^ "\nin equation\n" ^ Sign.string_of_term sign eq);
|
|
31 |
|
6359
|
32 |
|
5178
|
33 |
(* preprocessing of equations *)
|
|
34 |
|
|
35 |
fun process_eqn sign (eq, rec_fns) =
|
|
36 |
let
|
6032
|
37 |
val (lhs, rhs) =
|
|
38 |
if null (term_vars eq) then
|
|
39 |
HOLogic.dest_eq (HOLogic.dest_Trueprop eq)
|
|
40 |
handle _ => raise RecError "not a proper equation"
|
|
41 |
else raise RecError "illegal schematic variable(s)";
|
5178
|
42 |
|
|
43 |
val (recfun, args) = strip_comb lhs;
|
|
44 |
val (fname, _) = dest_Const recfun handle _ =>
|
|
45 |
raise RecError "function is not declared as constant in theory";
|
|
46 |
|
|
47 |
val (ls', rest) = take_prefix is_Free args;
|
|
48 |
val (middle, rs') = take_suffix is_Free rest;
|
|
49 |
val rpos = length ls';
|
|
50 |
|
|
51 |
val (constr, cargs') = if null middle then raise RecError "constructor missing"
|
|
52 |
else strip_comb (hd middle);
|
|
53 |
val (cname, T) = dest_Const constr
|
|
54 |
handle _ => raise RecError "ill-formed constructor";
|
|
55 |
val (tname, _) = dest_Type (body_type T) handle _ =>
|
|
56 |
raise RecError "cannot determine datatype associated with function"
|
|
57 |
|
6037
|
58 |
val (ls, cargs, rs) = (map dest_Free ls',
|
|
59 |
map dest_Free cargs',
|
|
60 |
map dest_Free rs')
|
5178
|
61 |
handle _ => raise RecError "illegal argument in pattern";
|
|
62 |
val lfrees = ls @ rs @ cargs;
|
|
63 |
|
|
64 |
in
|
|
65 |
if not (null (duplicates lfrees)) then
|
|
66 |
raise RecError "repeated variable name in pattern"
|
|
67 |
else if not ((map dest_Free (term_frees rhs)) subset lfrees) then
|
|
68 |
raise RecError "extra variables on rhs"
|
|
69 |
else if length middle > 1 then
|
|
70 |
raise RecError "more than one non-variable in pattern"
|
|
71 |
else (case assoc (rec_fns, fname) of
|
|
72 |
None =>
|
|
73 |
(fname, (tname, rpos, [(cname, (ls, cargs, rs, rhs, eq))]))::rec_fns
|
|
74 |
| Some (_, rpos', eqns) =>
|
|
75 |
if is_some (assoc (eqns, cname)) then
|
6037
|
76 |
raise RecError "constructor already occurred as pattern"
|
5178
|
77 |
else if rpos <> rpos' then
|
|
78 |
raise RecError "position of recursive argument inconsistent"
|
|
79 |
else
|
6037
|
80 |
overwrite (rec_fns,
|
|
81 |
(fname,
|
|
82 |
(tname, rpos,
|
|
83 |
(cname, (ls, cargs, rs, rhs, eq))::eqns))))
|
5178
|
84 |
end
|
|
85 |
handle RecError s => primrec_eq_err sign s eq;
|
|
86 |
|
|
87 |
fun process_fun sign descr rec_eqns ((i, fname), (fnames, fnss)) =
|
|
88 |
let
|
|
89 |
val (_, (tname, _, constrs)) = nth_elem (i, descr);
|
|
90 |
|
|
91 |
(* substitute "fname ls x rs" by "y ls rs" for (x, (_, y)) in subs *)
|
|
92 |
|
|
93 |
fun subst [] x = x
|
|
94 |
| subst subs (fs, Abs (a, T, t)) =
|
|
95 |
let val (fs', t') = subst subs (fs, t)
|
|
96 |
in (fs', Abs (a, T, t')) end
|
|
97 |
| subst subs (fs, t as (_ $ _)) =
|
|
98 |
let val (f, ts) = strip_comb t;
|
|
99 |
in
|
|
100 |
if is_Const f andalso (fst (dest_Const f)) mem (map fst rec_eqns) then
|
|
101 |
let
|
|
102 |
val (fname', _) = dest_Const f;
|
|
103 |
val (_, rpos, _) = the (assoc (rec_eqns, fname'));
|
|
104 |
val ls = take (rpos, ts);
|
|
105 |
val rest = drop (rpos, ts);
|
|
106 |
val (x, rs) = (hd rest, tl rest)
|
|
107 |
handle _ => raise RecError ("not enough arguments\
|
|
108 |
\ in recursive application\nof function " ^ fname' ^ " on rhs")
|
|
109 |
in
|
|
110 |
(case assoc (subs, x) of
|
|
111 |
None =>
|
|
112 |
let
|
|
113 |
val (fs', ts') = foldl_map (subst subs) (fs, ts)
|
|
114 |
in (fs', list_comb (f, ts')) end
|
|
115 |
| Some (i', y) =>
|
|
116 |
let
|
|
117 |
val (fs', ts') = foldl_map (subst subs) (fs, ls @ rs);
|
|
118 |
val fs'' = process_fun sign descr rec_eqns ((i', fname'), fs')
|
|
119 |
in (fs'', list_comb (y, ts'))
|
|
120 |
end)
|
|
121 |
end
|
|
122 |
else
|
|
123 |
let
|
|
124 |
val (fs', f'::ts') = foldl_map (subst subs) (fs, f::ts)
|
|
125 |
in (fs', list_comb (f', ts')) end
|
|
126 |
end
|
|
127 |
| subst _ x = x;
|
|
128 |
|
|
129 |
(* translate rec equations into function arguments suitable for rec comb *)
|
|
130 |
|
|
131 |
fun trans eqns ((cname, cargs), (fnames', fnss', fns)) =
|
|
132 |
(case assoc (eqns, cname) of
|
|
133 |
None => (warning ("no equation for constructor " ^ cname ^
|
|
134 |
"\nin definition of function " ^ fname);
|
|
135 |
(fnames', fnss', (Const ("arbitrary", dummyT))::fns))
|
|
136 |
| Some (ls, cargs', rs, rhs, eq) =>
|
|
137 |
let
|
|
138 |
val recs = filter (is_rec_type o snd) (cargs' ~~ cargs);
|
|
139 |
val rargs = map fst recs;
|
6037
|
140 |
val subs = map (rpair dummyT o fst)
|
|
141 |
(rev (rename_wrt_term rhs rargs));
|
|
142 |
val ((fnames'', fnss''), rhs') =
|
|
143 |
(subst (map (fn ((x, y), z) =>
|
|
144 |
(Free x, (dest_DtRec y, Free z)))
|
|
145 |
(recs ~~ subs))
|
|
146 |
((fnames', fnss'), rhs))
|
5178
|
147 |
handle RecError s => primrec_eq_err sign s eq
|
6037
|
148 |
in (fnames'', fnss'',
|
|
149 |
(list_abs_free (cargs' @ subs @ ls @ rs, rhs'))::fns)
|
5178
|
150 |
end)
|
|
151 |
|
|
152 |
in (case assoc (fnames, i) of
|
|
153 |
None =>
|
|
154 |
if exists (equal fname o snd) fnames then
|
|
155 |
raise RecError ("inconsistent functions for datatype " ^ tname)
|
|
156 |
else
|
|
157 |
let
|
|
158 |
val (_, _, eqns) = the (assoc (rec_eqns, fname));
|
|
159 |
val (fnames', fnss', fns) = foldr (trans eqns)
|
|
160 |
(constrs, ((i, fname)::fnames, fnss, []))
|
|
161 |
in
|
|
162 |
(fnames', (i, (fname, #1 (snd (hd eqns)), fns))::fnss')
|
|
163 |
end
|
|
164 |
| Some fname' =>
|
|
165 |
if fname = fname' then (fnames, fnss)
|
|
166 |
else raise RecError ("inconsistent functions for datatype " ^ tname))
|
|
167 |
end;
|
|
168 |
|
6359
|
169 |
|
5178
|
170 |
(* prepare functions needed for definitions *)
|
|
171 |
|
|
172 |
fun get_fns fns (((i, (tname, _, constrs)), rec_name), (fs, defs)) =
|
|
173 |
case assoc (fns, i) of
|
|
174 |
None =>
|
|
175 |
let
|
|
176 |
val dummy_fns = map (fn (_, cargs) => Const ("arbitrary",
|
|
177 |
replicate ((length cargs) + (length (filter is_rec_type cargs)))
|
|
178 |
dummyT ---> HOLogic.unitT)) constrs;
|
|
179 |
val _ = warning ("no function definition for datatype " ^ tname)
|
|
180 |
in
|
|
181 |
(dummy_fns @ fs, defs)
|
|
182 |
end
|
|
183 |
| Some (fname, ls, fs') => (fs' @ fs, (fname, ls, rec_name, tname)::defs);
|
|
184 |
|
6359
|
185 |
|
5178
|
186 |
(* make definition *)
|
|
187 |
|
|
188 |
fun make_def sign fs (fname, ls, rec_name, tname) =
|
|
189 |
let
|
6037
|
190 |
val rhs = foldr (fn (T, t) => Abs ("", T, t))
|
|
191 |
((map snd ls) @ [dummyT],
|
|
192 |
list_comb (Const (rec_name, dummyT),
|
|
193 |
fs @ map Bound (0 ::(length ls downto 1))));
|
5178
|
194 |
val defpair = (Sign.base_name fname ^ "_" ^ Sign.base_name tname ^ "_def",
|
6037
|
195 |
Logic.mk_equals (Const (fname, dummyT), rhs))
|
5178
|
196 |
in
|
|
197 |
inferT_axm sign defpair
|
|
198 |
end;
|
|
199 |
|
6359
|
200 |
|
5178
|
201 |
(* find datatypes which contain all datatypes in tnames' *)
|
|
202 |
|
|
203 |
fun find_dts (dt_info : datatype_info Symtab.table) _ [] = []
|
|
204 |
| find_dts dt_info tnames' (tname::tnames) =
|
|
205 |
(case Symtab.lookup (dt_info, tname) of
|
|
206 |
None => primrec_err (tname ^ " is not a datatype")
|
|
207 |
| Some dt =>
|
|
208 |
if tnames' subset (map (#1 o snd) (#descr dt)) then
|
|
209 |
(tname, dt)::(find_dts dt_info tnames' tnames)
|
|
210 |
else find_dts dt_info tnames' tnames);
|
|
211 |
|
6359
|
212 |
fun add_primrec_i alt_name eqns_atts thy =
|
5178
|
213 |
let
|
6359
|
214 |
val (eqns, atts) = split_list eqns_atts;
|
5178
|
215 |
val sg = sign_of thy;
|
|
216 |
val dt_info = DatatypePackage.get_datatypes thy;
|
5719
|
217 |
val rec_eqns = foldr (process_eqn sg) (map snd eqns, []);
|
5178
|
218 |
val tnames = distinct (map (#1 o snd) rec_eqns);
|
|
219 |
val dts = find_dts dt_info tnames tnames;
|
6037
|
220 |
val main_fns =
|
|
221 |
map (fn (tname, {index, ...}) =>
|
|
222 |
(index,
|
|
223 |
fst (the (find_first (fn f => #1 (snd f) = tname) rec_eqns))))
|
|
224 |
dts;
|
|
225 |
val {descr, rec_names, rec_rewrites, ...} =
|
|
226 |
if null dts then
|
6359
|
227 |
primrec_err ("datatypes " ^ commas_quote tnames ^
|
6037
|
228 |
"\nare not mutually recursive")
|
|
229 |
else snd (hd dts);
|
|
230 |
val (fnames, fnss) = foldr (process_fun sg descr rec_eqns)
|
|
231 |
(main_fns, ([], []));
|
5178
|
232 |
val (fs, defs) = foldr (get_fns fnss) (descr ~~ rec_names, ([], []));
|
|
233 |
val defs' = map (make_def sg fs) defs;
|
|
234 |
val names1 = map snd fnames;
|
|
235 |
val names2 = map fst rec_eqns;
|
5216
|
236 |
val thy' = thy |>
|
5719
|
237 |
Theory.add_path (if alt_name = "" then (space_implode "_"
|
|
238 |
(map (Sign.base_name o #1) defs)) else alt_name) |>
|
5216
|
239 |
(if eq_set (names1, names2) then Theory.add_defs_i defs'
|
6359
|
240 |
else primrec_err ("functions " ^ commas_quote names2 ^
|
5216
|
241 |
"\nare not mutually recursive"));
|
5553
|
242 |
val rewrites = (map mk_meta_eq rec_rewrites) @ (map (get_axiom thy' o fst) defs');
|
5178
|
243 |
val _ = writeln ("Proving equations for primrec function(s)\n" ^
|
6359
|
244 |
commas_quote names1 ^ " ...");
|
5719
|
245 |
val char_thms = map (fn (_, t) => prove_goalw_cterm rewrites (cterm_of (sign_of thy') t)
|
5178
|
246 |
(fn _ => [rtac refl 1])) eqns;
|
6092
|
247 |
val simps = char_thms;
|
6359
|
248 |
val thy'' =
|
|
249 |
thy'
|
|
250 |
|> PureThy.add_thmss [(("simps", simps), [Simplifier.simp_add_global])]
|
|
251 |
|> PureThy.add_thms ((map fst eqns ~~ simps) ~~ atts)
|
|
252 |
|> Theory.parent_path;
|
|
253 |
in (thy'', char_thms) end;
|
|
254 |
|
|
255 |
|
|
256 |
fun read_eqn thy ((name, s), srcs) =
|
|
257 |
((name, readtm (sign_of thy) propT s), map (Attrib.global_attribute thy) srcs);
|
|
258 |
|
|
259 |
fun add_primrec alt_name eqns thy = add_primrec_i alt_name (map (read_eqn thy) eqns) thy;
|
|
260 |
|
5178
|
261 |
|
6359
|
262 |
(* outer syntax *)
|
|
263 |
|
6384
|
264 |
local open OuterParse in
|
6359
|
265 |
|
|
266 |
val primrec_decl =
|
|
267 |
Scan.optional ($$$ "(" |-- name --| $$$ ")") "" --
|
|
268 |
Scan.repeat1 (opt_thm_name ":" -- term);
|
|
269 |
|
|
270 |
val primrecP =
|
6384
|
271 |
OuterSyntax.command "primrec" "define primitive recursive functions on datatypes"
|
6359
|
272 |
(primrec_decl >> (fn (alt_name, eqns) =>
|
|
273 |
Toplevel.theory (#1 o add_primrec alt_name (map (fn ((x, y), z) => ((x, z), y)) eqns))));
|
|
274 |
|
|
275 |
val _ = OuterSyntax.add_parsers [primrecP];
|
5178
|
276 |
|
|
277 |
end;
|
6384
|
278 |
|
|
279 |
|
|
280 |
end;
|