|
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)
|
|
|
109 |
| absterm (t,body) = Abs("rec", iT, abstract_over (t, body))
|
|
|
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);
|
|
|
118 |
(Const ("0", iT), #2 recursor_pair, Const ("0", iT)))
|
|
|
119 |
| Some (rhs, cargs', eq) =>
|
|
|
120 |
(rhs, inst_recursor (recursor_pair, cargs'), eq)
|
|
|
121 |
val allowed_terms = map use_fabs (#2 (strip_comb recursor_rhs))
|
|
|
122 |
val abs = foldr absterm (allowed_terms, rhs)
|
|
|
123 |
in
|
|
|
124 |
if !Ind_Syntax.trace then
|
|
|
125 |
writeln ("recursor_rhs = " ^
|
|
|
126 |
Sign.string_of_term (sign_of thy) recursor_rhs ^
|
|
|
127 |
"\nabs = " ^ Sign.string_of_term (sign_of thy) abs)
|
|
|
128 |
else();
|
|
|
129 |
if Logic.occs (fconst, abs) then
|
|
|
130 |
primrec_eq_err (sign_of thy)
|
|
|
131 |
("illegal recursive occurrences of " ^ fname)
|
|
|
132 |
eq
|
|
|
133 |
else abs :: cases
|
|
|
134 |
end
|
|
|
135 |
|
|
|
136 |
val recursor = head_of (#1 (hd recursor_pairs))
|
|
|
137 |
|
|
|
138 |
(** make definition **)
|
|
|
139 |
|
|
|
140 |
(*the recursive argument*)
|
|
|
141 |
val rec_arg = Free (variant (map #1 (ls@rs)) (Sign.base_name big_rec_name),
|
|
|
142 |
iT)
|
|
|
143 |
|
|
|
144 |
val def_tm = Logic.mk_equals
|
|
|
145 |
(subst_bound (rec_arg, fabs),
|
|
|
146 |
list_comb (recursor,
|
|
|
147 |
foldr add_case (cnames ~~ recursor_pairs, []))
|
|
|
148 |
$ rec_arg)
|
|
|
149 |
|
|
|
150 |
in
|
|
6065
|
151 |
if !Ind_Syntax.trace then
|
|
|
152 |
writeln ("primrec def:\n" ^
|
|
|
153 |
Sign.string_of_term (sign_of thy) def_tm)
|
|
|
154 |
else();
|
|
6050
|
155 |
(Sign.base_name fname ^ "_" ^ Sign.base_name big_rec_name ^ "_def",
|
|
|
156 |
def_tm)
|
|
|
157 |
end;
|
|
|
158 |
|
|
|
159 |
|
|
|
160 |
|
|
|
161 |
(* prepare functions needed for definitions *)
|
|
|
162 |
|
|
|
163 |
(*Each equation is paired with an optional name, which is "_" (ML wildcard)
|
|
|
164 |
if omitted.*)
|
|
|
165 |
fun add_primrec_i recursion_eqns thy =
|
|
|
166 |
let
|
|
|
167 |
val Some (fname, ftype, ls, rs, con_info, eqns) =
|
|
|
168 |
foldr (process_eqn thy) (map snd recursion_eqns, None);
|
|
|
169 |
val def = process_fun thy (fname, ftype, ls, rs, con_info, eqns)
|
|
|
170 |
val thy' = thy |> Theory.add_path (Sign.base_name (#1 def))
|
|
|
171 |
|> Theory.add_defs_i [def]
|
|
|
172 |
val rewrites = get_axiom thy' (#1 def) ::
|
|
|
173 |
map mk_meta_eq (#rec_rewrites con_info)
|
|
|
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'
|
|
6092
|
186 |
|> PureThy.add_thmss [(("simps", simps), [Simplifier.simp_add_global])]
|
|
|
187 |
|> 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 =
|
|
|
195 |
add_primrec_i (map (apsnd (readtm (sign_of thy) propT)) eqns) thy;
|
|
|
196 |
|
|
|
197 |
end;
|