author | wenzelm |
Sat, 29 Mar 2008 13:03:09 +0100 | |
changeset 26478 | 9d1029ce0e13 |
parent 24867 | e5b55d7be9bb |
child 26939 | 1035c89b4c02 |
permissions | -rw-r--r-- |
6050 | 1 |
(* Title: ZF/Tools/primrec_package.ML |
2 |
ID: $Id$ |
|
3 |
Author: Stefan Berghofer and Norbert Voelker |
|
4 |
Copyright 1998 TU Muenchen |
|
5 |
ZF version by Lawrence C Paulson (Cambridge) |
|
6 |
||
7 |
Package for defining functions on datatypes by primitive recursion |
|
8 |
*) |
|
9 |
||
10 |
signature PRIMREC_PACKAGE = |
|
11 |
sig |
|
15703 | 12 |
val add_primrec: ((bstring * string) * Attrib.src list) list -> theory -> theory * thm list |
18728 | 13 |
val add_primrec_i: ((bstring * term) * attribute list) list -> theory -> theory * thm list |
6050 | 14 |
end; |
15 |
||
16 |
structure PrimrecPackage : PRIMREC_PACKAGE = |
|
17 |
struct |
|
18 |
||
19 |
exception RecError of string; |
|
20 |
||
6141 | 21 |
(*Remove outer Trueprop and equality sign*) |
22 |
val dest_eqn = FOLogic.dest_eq o FOLogic.dest_Trueprop; |
|
6050 | 23 |
|
24 |
fun primrec_err s = error ("Primrec definition error:\n" ^ s); |
|
25 |
||
26 |
fun primrec_eq_err sign s eq = |
|
27 |
primrec_err (s ^ "\nin equation\n" ^ Sign.string_of_term sign eq); |
|
28 |
||
12183 | 29 |
|
6050 | 30 |
(* preprocessing of equations *) |
31 |
||
32 |
(*rec_fn_opt records equations already noted for this function*) |
|
12183 | 33 |
fun process_eqn thy (eq, rec_fn_opt) = |
6050 | 34 |
let |
12183 | 35 |
val (lhs, rhs) = |
36 |
if null (term_vars eq) then |
|
12203 | 37 |
dest_eqn eq handle TERM _ => raise RecError "not a proper equation" |
12183 | 38 |
else raise RecError "illegal schematic variable(s)"; |
6050 | 39 |
|
40 |
val (recfun, args) = strip_comb lhs; |
|
12203 | 41 |
val (fname, ftype) = dest_Const recfun handle TERM _ => |
6050 | 42 |
raise RecError "function is not declared as constant in theory"; |
43 |
||
44 |
val (ls_frees, rest) = take_prefix is_Free args; |
|
45 |
val (middle, rs_frees) = take_suffix is_Free rest; |
|
46 |
||
12183 | 47 |
val (constr, cargs_frees) = |
6050 | 48 |
if null middle then raise RecError "constructor missing" |
49 |
else strip_comb (hd middle); |
|
50 |
val (cname, _) = dest_Const constr |
|
12203 | 51 |
handle TERM _ => raise RecError "ill-formed constructor"; |
17412 | 52 |
val con_info = the (Symtab.lookup (ConstructorsData.get thy) cname) |
15531 | 53 |
handle Option => |
6050 | 54 |
raise RecError "cannot determine datatype associated with function" |
55 |
||
12183 | 56 |
val (ls, cargs, rs) = (map dest_Free ls_frees, |
57 |
map dest_Free cargs_frees, |
|
58 |
map dest_Free rs_frees) |
|
12203 | 59 |
handle TERM _ => raise RecError "illegal argument in pattern"; |
6050 | 60 |
val lfrees = ls @ rs @ cargs; |
61 |
||
62 |
(*Constructor, frees to left of pattern, pattern variables, |
|
63 |
frees to right of pattern, rhs of equation, full original equation. *) |
|
64 |
val new_eqn = (cname, (rhs, cargs, eq)) |
|
65 |
||
66 |
in |
|
18973 | 67 |
if has_duplicates (op =) lfrees then |
12183 | 68 |
raise RecError "repeated variable name in pattern" |
6050 | 69 |
else if not ((map dest_Free (term_frees rhs)) subset lfrees) then |
70 |
raise RecError "extra variables on rhs" |
|
12183 | 71 |
else if length middle > 1 then |
6050 | 72 |
raise RecError "more than one non-variable in pattern" |
73 |
else case rec_fn_opt of |
|
15531 | 74 |
NONE => SOME (fname, ftype, ls, rs, con_info, [new_eqn]) |
75 |
| SOME (fname', _, ls', rs', con_info': constructor_info, eqns) => |
|
17314 | 76 |
if AList.defined (op =) eqns cname then |
12183 | 77 |
raise RecError "constructor already occurred as pattern" |
78 |
else if (ls <> ls') orelse (rs <> rs') then |
|
79 |
raise RecError "non-recursive arguments are inconsistent" |
|
80 |
else if #big_rec_name con_info <> #big_rec_name con_info' then |
|
81 |
raise RecError ("Mixed datatypes for function " ^ fname) |
|
82 |
else if fname <> fname' then |
|
83 |
raise RecError ("inconsistent functions for datatype " ^ |
|
84 |
#big_rec_name con_info) |
|
15531 | 85 |
else SOME (fname, ftype, ls, rs, con_info, new_eqn::eqns) |
6050 | 86 |
end |
20342 | 87 |
handle RecError s => primrec_eq_err thy s eq; |
6050 | 88 |
|
89 |
||
90 |
(*Instantiates a recursor equation with constructor arguments*) |
|
12183 | 91 |
fun inst_recursor ((_ $ constr, rhs), cargs') = |
6050 | 92 |
subst_atomic (#2 (strip_comb constr) ~~ map Free cargs') rhs; |
93 |
||
94 |
||
95 |
(*Convert a list of recursion equations into a recursor call*) |
|
96 |
fun process_fun thy (fname, ftype, ls, rs, con_info: constructor_info, eqns) = |
|
97 |
let |
|
98 |
val fconst = Const(fname, ftype) |
|
99 |
val fabs = list_comb (fconst, map Free ls @ [Bound 0] @ map Free rs) |
|
100 |
and {big_rec_name, constructors, rec_rewrites, ...} = con_info |
|
101 |
||
102 |
(*Replace X_rec(args,t) by fname(ls,t,rs) *) |
|
103 |
fun use_fabs (_ $ t) = subst_bound (t, fabs) |
|
104 |
| use_fabs t = t |
|
105 |
||
106 |
val cnames = map (#1 o dest_Const) constructors |
|
6141 | 107 |
and recursor_pairs = map (dest_eqn o concl_of) rec_rewrites |
6050 | 108 |
|
109 |
fun absterm (Free(a,T), body) = absfree (a,T,body) |
|
9179
44eabc57ed46
no longer depends upon a prior "open Ind_Syntax" from elsewhere
paulson
parents:
8819
diff
changeset
|
110 |
| absterm (t,body) = Abs("rec", Ind_Syntax.iT, abstract_over (t, body)) |
6050 | 111 |
|
112 |
(*Translate rec equations into function arguments suitable for recursor. |
|
113 |
Missing cases are replaced by 0 and all cases are put into order.*) |
|
114 |
fun add_case ((cname, recursor_pair), cases) = |
|
12183 | 115 |
let val (rhs, recursor_rhs, eq) = |
17314 | 116 |
case AList.lookup (op =) eqns cname of |
15531 | 117 |
NONE => (warning ("no equation for constructor " ^ cname ^ |
12183 | 118 |
"\nin definition of function " ^ fname); |
119 |
(Const ("0", Ind_Syntax.iT), |
|
120 |
#2 recursor_pair, Const ("0", Ind_Syntax.iT))) |
|
15531 | 121 |
| SOME (rhs, cargs', eq) => |
12183 | 122 |
(rhs, inst_recursor (recursor_pair, cargs'), eq) |
123 |
val allowed_terms = map use_fabs (#2 (strip_comb recursor_rhs)) |
|
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
124 |
val abs = foldr absterm rhs allowed_terms |
12183 | 125 |
in |
6050 | 126 |
if !Ind_Syntax.trace then |
12183 | 127 |
writeln ("recursor_rhs = " ^ |
20342 | 128 |
Sign.string_of_term thy recursor_rhs ^ |
129 |
"\nabs = " ^ Sign.string_of_term thy abs) |
|
6050 | 130 |
else(); |
12183 | 131 |
if Logic.occs (fconst, abs) then |
20342 | 132 |
primrec_eq_err thy |
12183 | 133 |
("illegal recursive occurrences of " ^ fname) |
134 |
eq |
|
135 |
else abs :: cases |
|
6050 | 136 |
end |
137 |
||
138 |
val recursor = head_of (#1 (hd recursor_pairs)) |
|
139 |
||
140 |
(** make definition **) |
|
141 |
||
142 |
(*the recursive argument*) |
|
20071
8f3e1ddb50e6
replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents:
20046
diff
changeset
|
143 |
val rec_arg = Free (Name.variant (map #1 (ls@rs)) (Sign.base_name big_rec_name), |
12183 | 144 |
Ind_Syntax.iT) |
6050 | 145 |
|
146 |
val def_tm = Logic.mk_equals |
|
12183 | 147 |
(subst_bound (rec_arg, fabs), |
148 |
list_comb (recursor, |
|
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
149 |
foldr add_case [] (cnames ~~ recursor_pairs)) |
12183 | 150 |
$ rec_arg) |
6050 | 151 |
|
152 |
in |
|
6065 | 153 |
if !Ind_Syntax.trace then |
12183 | 154 |
writeln ("primrec def:\n" ^ |
20342 | 155 |
Sign.string_of_term thy def_tm) |
6065 | 156 |
else(); |
6050 | 157 |
(Sign.base_name fname ^ "_" ^ Sign.base_name big_rec_name ^ "_def", |
158 |
def_tm) |
|
159 |
end; |
|
160 |
||
161 |
||
162 |
(* prepare functions needed for definitions *) |
|
163 |
||
12183 | 164 |
fun add_primrec_i args thy = |
6050 | 165 |
let |
12183 | 166 |
val ((eqn_names, eqn_terms), eqn_atts) = apfst split_list (split_list args); |
15531 | 167 |
val SOME (fname, ftype, ls, rs, con_info, eqns) = |
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
168 |
foldr (process_eqn thy) NONE eqn_terms; |
12183 | 169 |
val def = process_fun thy (fname, ftype, ls, rs, con_info, eqns); |
170 |
||
18358 | 171 |
val ([def_thm], thy1) = thy |
24712
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24707
diff
changeset
|
172 |
|> Sign.add_path (Sign.base_name fname) |
12183 | 173 |
|> (PureThy.add_defs_i false o map Thm.no_attributes) [def]; |
174 |
||
8438 | 175 |
val rewrites = def_thm :: map mk_meta_eq (#rec_rewrites con_info) |
12183 | 176 |
val eqn_thms = |
17985 | 177 |
eqn_terms |> map (fn t => |
20046 | 178 |
Goal.prove_global thy1 [] [] (Ind_Syntax.traceIt "next primrec equation = " thy1 t) |
26478 | 179 |
(fn _ => EVERY [rewrite_goals_tac rewrites, rtac refl 1])); |
12183 | 180 |
|
18377 | 181 |
val (eqn_thms', thy2) = |
182 |
thy1 |
|
183 |
|> PureThy.add_thms ((eqn_names ~~ eqn_thms) ~~ eqn_atts); |
|
184 |
val (_, thy3) = |
|
185 |
thy2 |
|
18728 | 186 |
|> PureThy.add_thmss [(("simps", eqn_thms'), [Simplifier.simp_add])] |
24712
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24707
diff
changeset
|
187 |
||> Sign.parent_path; |
12183 | 188 |
in (thy3, eqn_thms') end; |
189 |
||
190 |
fun add_primrec args thy = |
|
191 |
add_primrec_i (map (fn ((name, s), srcs) => |
|
24707 | 192 |
((name, Syntax.read_prop_global thy s), map (Attrib.attribute thy) srcs)) |
12183 | 193 |
args) thy; |
194 |
||
195 |
||
196 |
(* outer syntax *) |
|
6050 | 197 |
|
24867 | 198 |
structure P = OuterParse and K = OuterKeyword; |
12183 | 199 |
|
24867 | 200 |
val _ = |
12183 | 201 |
OuterSyntax.command "primrec" "define primitive recursive functions on datatypes" K.thy_decl |
24867 | 202 |
(Scan.repeat1 (SpecParse.opt_thm_name ":" -- P.prop) |
203 |
>> (Toplevel.theory o (#1 oo (add_primrec o map P.triple_swap)))); |
|
6050 | 204 |
|
205 |
end; |
|
12183 | 206 |