author | wenzelm |
Thu, 30 Nov 2000 20:04:16 +0100 | |
changeset 10548 | e8c774c12105 |
parent 9329 | d2655dc8a4b4 |
child 12183 | c10cea75dd56 |
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 |
|
12 |
val add_primrec_i : (string * term) list -> theory -> theory * thm list |
|
13 |
val add_primrec : (string * string) list -> theory -> theory * thm list |
|
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 |
||
29 |
(* preprocessing of equations *) |
|
30 |
||
31 |
(*rec_fn_opt records equations already noted for this function*) |
|
32 |
fun process_eqn thy (eq, rec_fn_opt) = |
|
33 |
let |
|
6141 | 34 |
val (lhs, rhs) = |
35 |
if null (term_vars eq) then |
|
36 |
dest_eqn eq handle _ => raise RecError "not a proper equation" |
|
37 |
else raise RecError "illegal schematic variable(s)"; |
|
6050 | 38 |
|
39 |
val (recfun, args) = strip_comb lhs; |
|
40 |
val (fname, ftype) = dest_Const recfun handle _ => |
|
41 |
raise RecError "function is not declared as constant in theory"; |
|
42 |
||
43 |
val (ls_frees, rest) = take_prefix is_Free args; |
|
44 |
val (middle, rs_frees) = take_suffix is_Free rest; |
|
45 |
||
46 |
val (constr, cargs_frees) = |
|
47 |
if null middle then raise RecError "constructor missing" |
|
48 |
else strip_comb (hd middle); |
|
49 |
val (cname, _) = dest_Const constr |
|
50 |
handle _ => raise RecError "ill-formed constructor"; |
|
51 |
val con_info = the (Symtab.lookup (ConstructorsData.get thy, cname)) |
|
52 |
handle _ => |
|
53 |
raise RecError "cannot determine datatype associated with function" |
|
54 |
||
55 |
val (ls, cargs, rs) = (map dest_Free ls_frees, |
|
56 |
map dest_Free cargs_frees, |
|
57 |
map dest_Free rs_frees) |
|
58 |
handle _ => raise RecError "illegal argument in pattern"; |
|
59 |
val lfrees = ls @ rs @ cargs; |
|
60 |
||
61 |
(*Constructor, frees to left of pattern, pattern variables, |
|
62 |
frees to right of pattern, rhs of equation, full original equation. *) |
|
63 |
val new_eqn = (cname, (rhs, cargs, eq)) |
|
64 |
||
65 |
in |
|
66 |
if not (null (duplicates lfrees)) then |
|
67 |
raise RecError "repeated variable name in pattern" |
|
68 |
else if not ((map dest_Free (term_frees rhs)) subset lfrees) then |
|
69 |
raise RecError "extra variables on rhs" |
|
70 |
else if length middle > 1 then |
|
71 |
raise RecError "more than one non-variable in pattern" |
|
72 |
else case rec_fn_opt of |
|
73 |
None => Some (fname, ftype, ls, rs, con_info, [new_eqn]) |
|
74 |
| Some (fname', _, ls', rs', con_info': constructor_info, eqns) => |
|
75 |
if is_some (assoc (eqns, cname)) then |
|
76 |
raise RecError "constructor already occurred as pattern" |
|
77 |
else if (ls <> ls') orelse (rs <> rs') then |
|
78 |
raise RecError "non-recursive arguments are inconsistent" |
|
79 |
else if #big_rec_name con_info <> #big_rec_name con_info' then |
|
80 |
raise RecError ("Mixed datatypes for function " ^ fname) |
|
81 |
else if fname <> fname' then |
|
82 |
raise RecError ("inconsistent functions for datatype " ^ |
|
83 |
#big_rec_name con_info) |
|
84 |
else Some (fname, ftype, ls, rs, con_info, new_eqn::eqns) |
|
85 |
end |
|
86 |
handle RecError s => primrec_eq_err (sign_of thy) s eq; |
|
87 |
||
88 |
||
89 |
(*Instantiates a recursor equation with constructor arguments*) |
|
90 |
fun inst_recursor ((_ $ constr, rhs), cargs') = |
|
91 |
subst_atomic (#2 (strip_comb constr) ~~ map Free cargs') rhs; |
|
92 |
||
93 |
||
94 |
(*Convert a list of recursion equations into a recursor call*) |
|
95 |
fun process_fun thy (fname, ftype, ls, rs, con_info: constructor_info, eqns) = |
|
96 |
let |
|
97 |
val fconst = Const(fname, ftype) |
|
98 |
val fabs = list_comb (fconst, map Free ls @ [Bound 0] @ map Free rs) |
|
99 |
and {big_rec_name, constructors, rec_rewrites, ...} = con_info |
|
100 |
||
101 |
(*Replace X_rec(args,t) by fname(ls,t,rs) *) |
|
102 |
fun use_fabs (_ $ t) = subst_bound (t, fabs) |
|
103 |
| use_fabs t = t |
|
104 |
||
105 |
val cnames = map (#1 o dest_Const) constructors |
|
6141 | 106 |
and recursor_pairs = map (dest_eqn o concl_of) rec_rewrites |
6050 | 107 |
|
108 |
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
|
109 |
| absterm (t,body) = Abs("rec", Ind_Syntax.iT, abstract_over (t, body)) |
6050 | 110 |
|
111 |
(*Translate rec equations into function arguments suitable for recursor. |
|
112 |
Missing cases are replaced by 0 and all cases are put into order.*) |
|
113 |
fun add_case ((cname, recursor_pair), cases) = |
|
114 |
let val (rhs, recursor_rhs, eq) = |
|
115 |
case assoc (eqns, cname) of |
|
116 |
None => (warning ("no equation for constructor " ^ cname ^ |
|
117 |
"\nin definition of function " ^ fname); |
|
9179
44eabc57ed46
no longer depends upon a prior "open Ind_Syntax" from elsewhere
paulson
parents:
8819
diff
changeset
|
118 |
(Const ("0", Ind_Syntax.iT), |
44eabc57ed46
no longer depends upon a prior "open Ind_Syntax" from elsewhere
paulson
parents:
8819
diff
changeset
|
119 |
#2 recursor_pair, Const ("0", Ind_Syntax.iT))) |
6050 | 120 |
| Some (rhs, cargs', eq) => |
121 |
(rhs, inst_recursor (recursor_pair, cargs'), eq) |
|
122 |
val allowed_terms = map use_fabs (#2 (strip_comb recursor_rhs)) |
|
123 |
val abs = foldr absterm (allowed_terms, rhs) |
|
124 |
in |
|
125 |
if !Ind_Syntax.trace then |
|
126 |
writeln ("recursor_rhs = " ^ |
|
127 |
Sign.string_of_term (sign_of thy) recursor_rhs ^ |
|
128 |
"\nabs = " ^ Sign.string_of_term (sign_of thy) abs) |
|
129 |
else(); |
|
130 |
if Logic.occs (fconst, abs) then |
|
131 |
primrec_eq_err (sign_of thy) |
|
132 |
("illegal recursive occurrences of " ^ fname) |
|
133 |
eq |
|
134 |
else abs :: cases |
|
135 |
end |
|
136 |
||
137 |
val recursor = head_of (#1 (hd recursor_pairs)) |
|
138 |
||
139 |
(** make definition **) |
|
140 |
||
141 |
(*the recursive argument*) |
|
142 |
val rec_arg = Free (variant (map #1 (ls@rs)) (Sign.base_name big_rec_name), |
|
9179
44eabc57ed46
no longer depends upon a prior "open Ind_Syntax" from elsewhere
paulson
parents:
8819
diff
changeset
|
143 |
Ind_Syntax.iT) |
6050 | 144 |
|
145 |
val def_tm = Logic.mk_equals |
|
146 |
(subst_bound (rec_arg, fabs), |
|
147 |
list_comb (recursor, |
|
148 |
foldr add_case (cnames ~~ recursor_pairs, [])) |
|
149 |
$ rec_arg) |
|
150 |
||
151 |
in |
|
6065 | 152 |
if !Ind_Syntax.trace then |
153 |
writeln ("primrec def:\n" ^ |
|
154 |
Sign.string_of_term (sign_of thy) def_tm) |
|
155 |
else(); |
|
6050 | 156 |
(Sign.base_name fname ^ "_" ^ Sign.base_name big_rec_name ^ "_def", |
157 |
def_tm) |
|
158 |
end; |
|
159 |
||
160 |
||
161 |
||
162 |
(* prepare functions needed for definitions *) |
|
163 |
||
164 |
(*Each equation is paired with an optional name, which is "_" (ML wildcard) |
|
165 |
if omitted.*) |
|
166 |
fun add_primrec_i recursion_eqns thy = |
|
167 |
let |
|
168 |
val Some (fname, ftype, ls, rs, con_info, eqns) = |
|
169 |
foldr (process_eqn thy) (map snd recursion_eqns, None); |
|
170 |
val def = process_fun thy (fname, ftype, ls, rs, con_info, eqns) |
|
8438 | 171 |
val (thy', [def_thm]) = thy |> Theory.add_path (Sign.base_name (#1 def)) |
9329 | 172 |
|> (PureThy.add_defs_i false o map Thm.no_attributes) [def] |
8438 | 173 |
val rewrites = def_thm :: map mk_meta_eq (#rec_rewrites con_info) |
6050 | 174 |
val char_thms = |
6092 | 175 |
(if !Ind_Syntax.trace then (* FIXME message / quiet_mode (!?) *) |
6065 | 176 |
writeln ("Proving equations for primrec function " ^ fname) |
177 |
else (); |
|
6092 | 178 |
map (fn (_,t) => |
6065 | 179 |
prove_goalw_cterm rewrites |
180 |
(Ind_Syntax.traceIt "next primrec equation = " |
|
181 |
(cterm_of (sign_of thy') t)) |
|
182 |
(fn _ => [rtac refl 1])) |
|
183 |
recursion_eqns); |
|
6092 | 184 |
val simps = char_thms; |
6050 | 185 |
val thy'' = thy' |
8438 | 186 |
|> (#1 o PureThy.add_thmss [(("simps", simps), [Simplifier.simp_add_global])]) |
187 |
|> (#1 o PureThy.add_thms (map (rpair []) |
|
188 |
(filter_out (equal "_" o fst) (map fst recursion_eqns ~~ simps)))) |
|
6050 | 189 |
|> Theory.parent_path; |
190 |
in |
|
191 |
(thy'', char_thms) |
|
192 |
end; |
|
193 |
||
194 |
fun add_primrec eqns thy = |
|
8819 | 195 |
add_primrec_i (map (apsnd (Sign.simple_read_term (sign_of thy) propT)) eqns) thy; |
6050 | 196 |
|
197 |
end; |