author | wenzelm |
Mon, 25 Feb 2008 16:31:17 +0100 | |
changeset 26129 | 14f6dbb195c4 |
parent 25604 | 6c1714b9b805 |
child 26556 | 90b02960c8ce |
permissions | -rw-r--r-- |
5719 | 1 |
(* Title: HOL/Tools/primrec_package.ML |
5178 | 2 |
ID: $Id$ |
25557 | 3 |
Author: Stefan Berghofer, TU Muenchen; Norbert Voelker, FernUni Hagen; |
4 |
Florian Haftmann, TU Muenchen |
|
5178 | 5 |
|
6359 | 6 |
Package for defining functions on datatypes by primitive recursion. |
5178 | 7 |
*) |
8 |
||
9 |
signature PRIMREC_PACKAGE = |
|
10 |
sig |
|
25557 | 11 |
val add_primrec: (string * typ option * mixfix) list -> |
12 |
((bstring * Attrib.src list) * term) list -> local_theory -> thm list * local_theory |
|
5178 | 13 |
end; |
14 |
||
15 |
structure PrimrecPackage : PRIMREC_PACKAGE = |
|
16 |
struct |
|
17 |
||
18 |
open DatatypeAux; |
|
19 |
||
25557 | 20 |
exception PrimrecError of string * term option; |
6359 | 21 |
|
25557 | 22 |
fun primrec_error msg = raise PrimrecError (msg, NONE); |
23 |
fun primrec_error_eqn msg eqn = raise PrimrecError (msg, SOME eqn); |
|
23765
997e5fe47532
Function unify_consts moved from OldInductivePackage to PrimrecPackage.
berghofe
parents:
22728
diff
changeset
|
24 |
|
25557 | 25 |
fun message s = if ! Toplevel.debug then () else writeln s; |
23765
997e5fe47532
Function unify_consts moved from OldInductivePackage to PrimrecPackage.
berghofe
parents:
22728
diff
changeset
|
26 |
|
997e5fe47532
Function unify_consts moved from OldInductivePackage to PrimrecPackage.
berghofe
parents:
22728
diff
changeset
|
27 |
|
5178 | 28 |
(* preprocessing of equations *) |
29 |
||
25559 | 30 |
fun process_eqn is_fixed spec rec_fns = |
5178 | 31 |
let |
25566 | 32 |
val (vs, Ts) = split_list (strip_qnt_vars "all" spec); |
25557 | 33 |
val body = strip_qnt_body "all" spec; |
25566 | 34 |
val (vs', _) = Name.variants vs (Name.make_context (fold_aterms |
35 |
(fn Free (v, _) => insert (op =) v | _ => I) body [])); |
|
36 |
val eqn = curry subst_bounds (map2 (curry Free) vs' Ts |> rev) body; |
|
25557 | 37 |
val (lhs, rhs) = HOLogic.dest_eq (HOLogic.dest_Trueprop eqn) |
38 |
handle TERM _ => primrec_error "not a proper equation"; |
|
5178 | 39 |
val (recfun, args) = strip_comb lhs; |
25557 | 40 |
val fname = case recfun of Free (v, _) => if is_fixed v then v |
41 |
else primrec_error "illegal head of function equation" |
|
42 |
| _ => primrec_error "illegal head of function equation"; |
|
5178 | 43 |
|
44 |
val (ls', rest) = take_prefix is_Free args; |
|
45 |
val (middle, rs') = take_suffix is_Free rest; |
|
46 |
val rpos = length ls'; |
|
47 |
||
25557 | 48 |
val (constr, cargs') = if null middle then primrec_error "constructor missing" |
5178 | 49 |
else strip_comb (hd middle); |
50 |
val (cname, T) = dest_Const constr |
|
25557 | 51 |
handle TERM _ => primrec_error "ill-formed constructor"; |
7016
df54b5365477
- Now also supports arbitrarily branching datatypes.
berghofe
parents:
6729
diff
changeset
|
52 |
val (tname, _) = dest_Type (body_type T) handle TYPE _ => |
25557 | 53 |
primrec_error "cannot determine datatype associated with function" |
5178 | 54 |
|
20176 | 55 |
val (ls, cargs, rs) = |
56 |
(map dest_Free ls', map dest_Free cargs', map dest_Free rs') |
|
25557 | 57 |
handle TERM _ => primrec_error "illegal argument in pattern"; |
5178 | 58 |
val lfrees = ls @ rs @ cargs; |
59 |
||
12474 | 60 |
fun check_vars _ [] = () |
25557 | 61 |
| check_vars s vars = primrec_error (s ^ commas_quote (map fst vars)) eqn; |
5178 | 62 |
in |
22692 | 63 |
if length middle > 1 then |
25557 | 64 |
primrec_error "more than one non-variable in pattern" |
12474 | 65 |
else |
18964 | 66 |
(check_vars "repeated variable names in pattern: " (duplicates (op =) lfrees); |
12474 | 67 |
check_vars "extra variables on rhs: " |
25557 | 68 |
(map dest_Free (term_frees rhs) |> subtract (op =) lfrees |
25559 | 69 |
|> filter_out (is_fixed o fst)); |
25557 | 70 |
case AList.lookup (op =) rec_fns fname of |
15531 | 71 |
NONE => |
25557 | 72 |
(fname, (tname, rpos, [(cname, (ls, cargs, rs, rhs, eqn))]))::rec_fns |
15531 | 73 |
| SOME (_, rpos', eqns) => |
17184 | 74 |
if AList.defined (op =) eqns cname then |
25557 | 75 |
primrec_error "constructor already occurred as pattern" |
5178 | 76 |
else if rpos <> rpos' then |
25557 | 77 |
primrec_error "position of recursive argument inconsistent" |
5178 | 78 |
else |
25557 | 79 |
AList.update (op =) |
80 |
(fname, (tname, rpos, (cname, (ls, cargs, rs, rhs, eqn))::eqns)) |
|
17314 | 81 |
rec_fns) |
25557 | 82 |
end handle PrimrecError (msg, NONE) => primrec_error_eqn msg spec; |
5178 | 83 |
|
25557 | 84 |
fun process_fun descr eqns (i, fname) (fnames, fnss) = |
5178 | 85 |
let |
25557 | 86 |
val (_, (tname, _, constrs)) = nth descr i; |
5178 | 87 |
|
88 |
(* substitute "fname ls x rs" by "y ls rs" for (x, (_, y)) in subs *) |
|
89 |
||
21064 | 90 |
fun subst [] t fs = (t, fs) |
91 |
| subst subs (Abs (a, T, t)) fs = |
|
92 |
fs |
|
93 |
|> subst subs t |
|
94 |
|-> (fn t' => pair (Abs (a, T, t'))) |
|
95 |
| subst subs (t as (_ $ _)) fs = |
|
96 |
let |
|
97 |
val (f, ts) = strip_comb t; |
|
5178 | 98 |
in |
25557 | 99 |
if is_Free f |
100 |
andalso member (fn ((v, _), (w, _)) => v = w) eqns (dest_Free f) then |
|
5178 | 101 |
let |
25557 | 102 |
val (fname', _) = dest_Free f; |
103 |
val (_, rpos, _) = the (AList.lookup (op =) eqns fname'); |
|
104 |
val (ls, x' :: rs) = chop rpos ts |
|
105 |
handle Empty => primrec_error ("not enough arguments\ |
|
7016
df54b5365477
- Now also supports arbitrarily branching datatypes.
berghofe
parents:
6729
diff
changeset
|
106 |
\ in recursive application\nof function " ^ quote fname' ^ " on rhs"); |
df54b5365477
- Now also supports arbitrarily branching datatypes.
berghofe
parents:
6729
diff
changeset
|
107 |
val (x, xs) = strip_comb x' |
21064 | 108 |
in case AList.lookup (op =) subs x |
109 |
of NONE => |
|
110 |
fs |
|
111 |
|> fold_map (subst subs) ts |
|
112 |
|-> (fn ts' => pair (list_comb (f, ts'))) |
|
113 |
| SOME (i', y) => |
|
114 |
fs |
|
115 |
|> fold_map (subst subs) (xs @ ls @ rs) |
|
25557 | 116 |
||> process_fun descr eqns (i', fname') |
21064 | 117 |
|-> (fn ts' => pair (list_comb (y, ts'))) |
5178 | 118 |
end |
119 |
else |
|
21064 | 120 |
fs |
121 |
|> fold_map (subst subs) (f :: ts) |
|
122 |
|-> (fn (f'::ts') => pair (list_comb (f', ts'))) |
|
5178 | 123 |
end |
21064 | 124 |
| subst _ t fs = (t, fs); |
5178 | 125 |
|
126 |
(* translate rec equations into function arguments suitable for rec comb *) |
|
127 |
||
25557 | 128 |
fun trans eqns (cname, cargs) (fnames', fnss', fns) = |
17184 | 129 |
(case AList.lookup (op =) eqns cname of |
15531 | 130 |
NONE => (warning ("No equation for constructor " ^ quote cname ^ |
6427 | 131 |
"\nin definition of function " ^ quote fname); |
25557 | 132 |
(fnames', fnss', (Const ("HOL.undefined", dummyT))::fns)) |
15531 | 133 |
| SOME (ls, cargs', rs, rhs, eq) => |
5178 | 134 |
let |
21064 | 135 |
val recs = filter (is_rec_type o snd) (cargs' ~~ cargs); |
5178 | 136 |
val rargs = map fst recs; |
22692 | 137 |
val subs = map (rpair dummyT o fst) |
20176 | 138 |
(rev (rename_wrt_term rhs rargs)); |
25557 | 139 |
val (rhs', (fnames'', fnss'')) = (subst (map2 (fn (x, y) => fn z => |
140 |
(Free x, (body_index y, Free z))) recs subs) rhs (fnames', fnss')) |
|
141 |
handle PrimrecError (s, NONE) => primrec_error_eqn s eq |
|
142 |
in (fnames'', fnss'', |
|
20176 | 143 |
(list_abs_free (cargs' @ subs @ ls @ rs, rhs'))::fns) |
5178 | 144 |
end) |
145 |
||
25557 | 146 |
in (case AList.lookup (op =) fnames i of |
15531 | 147 |
NONE => |
25557 | 148 |
if exists (fn (_, v) => fname = v) fnames then |
149 |
primrec_error ("inconsistent functions for datatype " ^ quote tname) |
|
5178 | 150 |
else |
151 |
let |
|
25557 | 152 |
val (_, _, eqns) = the (AList.lookup (op =) eqns fname); |
153 |
val (fnames', fnss', fns) = fold_rev (trans eqns) constrs |
|
154 |
((i, fname)::fnames, fnss, []) |
|
5178 | 155 |
in |
25557 | 156 |
(fnames', (i, (fname, #1 (snd (hd eqns)), fns))::fnss') |
5178 | 157 |
end |
25557 | 158 |
| SOME fname' => |
159 |
if fname = fname' then (fnames, fnss) |
|
160 |
else primrec_error ("inconsistent functions for datatype " ^ quote tname)) |
|
5178 | 161 |
end; |
162 |
||
6359 | 163 |
|
5178 | 164 |
(* prepare functions needed for definitions *) |
165 |
||
21064 | 166 |
fun get_fns fns ((i : int, (tname, _, constrs)), rec_name) (fs, defs) = |
17184 | 167 |
case AList.lookup (op =) fns i of |
15531 | 168 |
NONE => |
5178 | 169 |
let |
22480 | 170 |
val dummy_fns = map (fn (_, cargs) => Const ("HOL.undefined", |
15570 | 171 |
replicate ((length cargs) + (length (List.filter is_rec_type cargs))) |
5178 | 172 |
dummyT ---> HOLogic.unitT)) constrs; |
6427 | 173 |
val _ = warning ("No function definition for datatype " ^ quote tname) |
5178 | 174 |
in |
175 |
(dummy_fns @ fs, defs) |
|
176 |
end |
|
21064 | 177 |
| SOME (fname, ls, fs') => (fs' @ fs, (fname, ls, rec_name, tname) :: defs); |
5178 | 178 |
|
6359 | 179 |
|
5178 | 180 |
(* make definition *) |
181 |
||
25557 | 182 |
fun make_def ctxt fixes fs (fname, ls, rec_name, tname) = |
5178 | 183 |
let |
25557 | 184 |
val raw_rhs = fold_rev (fn T => fn t => Abs ("", T, t)) |
21064 | 185 |
((map snd ls) @ [dummyT]) |
20176 | 186 |
(list_comb (Const (rec_name, dummyT), |
187 |
fs @ map Bound (0 ::(length ls downto 1)))) |
|
25557 | 188 |
val def_name = Thm.def_name (Sign.base_name fname); |
189 |
val rhs = singleton (Syntax.check_terms ctxt) raw_rhs; |
|
190 |
val SOME mfx = get_first |
|
191 |
(fn ((v, _), mfx) => if v = fname then SOME mfx else NONE) fixes; |
|
192 |
in ((fname, mfx), ((def_name, []), rhs)) end; |
|
5178 | 193 |
|
6359 | 194 |
|
5178 | 195 |
(* find datatypes which contain all datatypes in tnames' *) |
196 |
||
197 |
fun find_dts (dt_info : datatype_info Symtab.table) _ [] = [] |
|
198 |
| find_dts dt_info tnames' (tname::tnames) = |
|
17412 | 199 |
(case Symtab.lookup dt_info tname of |
25557 | 200 |
NONE => primrec_error (quote tname ^ " is not a datatype") |
15531 | 201 |
| SOME dt => |
5178 | 202 |
if tnames' subset (map (#1 o snd) (#descr dt)) then |
203 |
(tname, dt)::(find_dts dt_info tnames' tnames) |
|
204 |
else find_dts dt_info tnames' tnames); |
|
205 |
||
25557 | 206 |
|
207 |
(* primrec definition *) |
|
208 |
||
20841 | 209 |
local |
210 |
||
25557 | 211 |
fun prepare_spec prep_spec ctxt raw_fixes raw_spec = |
212 |
let |
|
25559 | 213 |
val ((fixes, spec), _) = prep_spec |
214 |
raw_fixes (map (single o apsnd single) raw_spec) ctxt |
|
215 |
in (fixes, map (apsnd the_single) spec) end; |
|
25557 | 216 |
|
25566 | 217 |
fun prove_spec ctxt names rec_rewrites defs = |
25557 | 218 |
let |
219 |
val rewrites = map mk_meta_eq rec_rewrites @ map (snd o snd) defs; |
|
220 |
fun tac _ = EVERY [rewrite_goals_tac rewrites, rtac refl 1]; |
|
25566 | 221 |
val _ = message ("Proving equations for primrec function(s) " ^ commas_quote names); |
25557 | 222 |
in map (fn (name_attr, t) => (name_attr, [Goal.prove ctxt [] [] t tac])) end; |
223 |
||
26129 | 224 |
fun gen_primrec set_group prep_spec raw_fixes raw_spec lthy = |
5178 | 225 |
let |
25557 | 226 |
val (fixes, spec) = prepare_spec prep_spec lthy raw_fixes raw_spec; |
25559 | 227 |
val eqns = fold_rev (process_eqn (fn v => Variable.is_fixed lthy v |
228 |
orelse exists (fn ((w, _), _) => v = w) fixes) o snd) spec []; |
|
25557 | 229 |
val tnames = distinct (op =) (map (#1 o snd) eqns); |
230 |
val dts = find_dts (DatatypePackage.get_datatypes |
|
231 |
(ProofContext.theory_of lthy)) tnames tnames; |
|
232 |
val main_fns = map (fn (tname, {index, ...}) => |
|
233 |
(index, (fst o the o find_first (fn (_, x) => #1 x = tname)) eqns)) dts; |
|
22692 | 234 |
val {descr, rec_names, rec_rewrites, ...} = |
25557 | 235 |
if null dts then primrec_error |
236 |
("datatypes " ^ commas_quote tnames ^ "\nare not mutually recursive") |
|
18362
e8b7e0a22727
removed thms 'swap' and 'nth_map' from ML toplevel
haftmann
parents:
18358
diff
changeset
|
237 |
else snd (hd dts); |
25557 | 238 |
val (fnames, fnss) = fold_rev (process_fun descr eqns) main_fns ([], []); |
21064 | 239 |
val (fs, defs) = fold_rev (get_fns fnss) (descr ~~ rec_names) ([], []); |
25566 | 240 |
val names1 = map snd fnames; |
241 |
val names2 = map fst eqns; |
|
242 |
val _ = if gen_eq_set (op =) (names1, names2) then () |
|
243 |
else primrec_error ("functions " ^ commas_quote names2 ^ |
|
25557 | 244 |
"\nare not mutually recursive"); |
245 |
val qualify = NameSpace.qualified |
|
246 |
(space_implode "_" (map (Sign.base_name o #1) defs)); |
|
25570 | 247 |
val simp_atts = map (Attrib.internal o K) |
248 |
[Simplifier.simp_add, RecfunCodegen.add NONE]; |
|
8480
50266d517b0c
Added new theory data slot for primrec equations.
berghofe
parents:
8432
diff
changeset
|
249 |
in |
25557 | 250 |
lthy |
26129 | 251 |
|> set_group ? LocalTheory.set_group (serial_string ()) |
25557 | 252 |
|> fold_map (LocalTheory.define Thm.definitionK o make_def lthy fixes fs) defs |
25566 | 253 |
|-> (fn defs => `(fn ctxt => prove_spec ctxt names1 rec_rewrites defs spec)) |
25557 | 254 |
|-> (fn simps => fold_map (LocalTheory.note Thm.theoremK) simps) |
255 |
|-> (fn simps' => LocalTheory.note Thm.theoremK |
|
256 |
((qualify "simps", simp_atts), maps snd simps')) |
|
25604 | 257 |
|>> snd |
25557 | 258 |
end handle PrimrecError (msg, some_eqn) => |
259 |
error ("Primrec definition error:\n" ^ msg ^ (case some_eqn |
|
260 |
of SOME eqn => "\nin\n" ^ quote (Syntax.string_of_term lthy eqn) |
|
261 |
| NONE => "")); |
|
20841 | 262 |
|
263 |
in |
|
264 |
||
26129 | 265 |
val add_primrec = gen_primrec false Specification.check_specification; |
266 |
val add_primrec_cmd = gen_primrec true Specification.read_specification; |
|
20841 | 267 |
|
22692 | 268 |
end; |
19688 | 269 |
|
5178 | 270 |
|
6359 | 271 |
(* outer syntax *) |
272 |
||
17057 | 273 |
local structure P = OuterParse and K = OuterKeyword in |
6359 | 274 |
|
19688 | 275 |
val opt_unchecked_name = |
276 |
Scan.optional (P.$$$ "(" |-- P.!!! |
|
277 |
(((P.$$$ "unchecked" >> K true) -- Scan.optional P.name "" || |
|
278 |
P.name >> pair false) --| P.$$$ ")")) (false, ""); |
|
279 |
||
25557 | 280 |
val old_primrec_decl = |
22101 | 281 |
opt_unchecked_name -- Scan.repeat1 (SpecParse.opt_thm_name ":" -- P.prop); |
6359 | 282 |
|
25557 | 283 |
fun pipe_error t = P.!!! (Scan.fail_with (K |
284 |
(cat_lines ["Equations must be separated by " ^ quote "|", quote t]))); |
|
285 |
||
286 |
val statement = SpecParse.opt_thm_name ":" -- P.prop --| Scan.ahead |
|
287 |
((P.term :-- pipe_error) || Scan.succeed ("","")); |
|
288 |
||
289 |
val statements = P.enum1 "|" statement; |
|
290 |
||
291 |
val primrec_decl = P.opt_target -- P.fixes --| P.$$$ "where" -- statements; |
|
292 |
||
24867 | 293 |
val _ = |
6723 | 294 |
OuterSyntax.command "primrec" "define primitive recursive functions on datatypes" K.thy_decl |
25557 | 295 |
((primrec_decl >> (fn ((opt_target, raw_fixes), raw_spec) => |
296 |
Toplevel.local_theory opt_target (add_primrec_cmd raw_fixes raw_spec #> snd))) |
|
297 |
|| (old_primrec_decl >> (fn ((unchecked, alt_name), eqns) => |
|
20176 | 298 |
Toplevel.theory (snd o |
25557 | 299 |
(if unchecked then OldPrimrecPackage.add_primrec_unchecked else OldPrimrecPackage.add_primrec) alt_name |
300 |
(map P.triple_swap eqns))))); |
|
6359 | 301 |
|
5178 | 302 |
end; |
6384 | 303 |
|
304 |
end; |