author | paulson |
Thu, 13 Jun 1996 14:25:45 +0200 | |
changeset 1792 | 75c54074cd8c |
parent 1500 | b2de3b3277b8 |
child 1835 | 07eee14f5bd4 |
permissions | -rw-r--r-- |
1460 | 1 |
(* Title: Pure/logic.ML |
0 | 2 |
ID: $Id$ |
1460 | 3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
0 | 4 |
Copyright Cambridge University 1992 |
5 |
||
6 |
Supporting code for defining the abstract type "thm" |
|
7 |
*) |
|
8 |
||
9 |
infix occs; |
|
10 |
||
11 |
signature LOGIC = |
|
12 |
sig |
|
1460 | 13 |
val assum_pairs : term -> (term*term)list |
14 |
val auto_rename : bool ref |
|
15 |
val close_form : term -> term |
|
16 |
val count_prems : term * int -> int |
|
17 |
val dest_equals : term -> term * term |
|
18 |
val dest_flexpair : term -> term * term |
|
19 |
val dest_implies : term -> term * term |
|
20 |
val dest_inclass : term -> typ * class |
|
21 |
val dest_type : term -> typ |
|
22 |
val flatten_params : int -> term -> term |
|
23 |
val freeze_vars : term -> term |
|
24 |
val incr_indexes : typ list * int -> term -> term |
|
25 |
val lift_fns : term * int -> (term -> term) * (term -> term) |
|
26 |
val list_flexpairs : (term*term)list * term -> term |
|
27 |
val list_implies : term list * term -> term |
|
0 | 28 |
val list_rename_params: string list * term -> term |
637 | 29 |
val is_equals : term -> bool |
1460 | 30 |
val mk_equals : term * term -> term |
31 |
val mk_flexpair : term * term -> term |
|
32 |
val mk_implies : term * term -> term |
|
33 |
val mk_inclass : typ * class -> term |
|
34 |
val mk_type : typ -> term |
|
35 |
val occs : term * term -> bool |
|
36 |
val rule_of : (term*term)list * term list * term -> term |
|
37 |
val set_rename_prefix : string -> unit |
|
38 |
val skip_flexpairs : term -> term |
|
0 | 39 |
val strip_assums_concl: term -> term |
1460 | 40 |
val strip_assums_hyp : term -> term list |
41 |
val strip_flexpairs : term -> (term*term)list * term |
|
42 |
val strip_horn : term -> (term*term)list * term list * term |
|
43 |
val strip_imp_concl : term -> term |
|
44 |
val strip_imp_prems : term -> term list |
|
45 |
val strip_params : term -> (string * typ) list |
|
46 |
val strip_prems : int * term list * term -> term list * term |
|
47 |
val thaw_vars : term -> term |
|
48 |
val unvarify : term -> term |
|
49 |
val varify : term -> term |
|
0 | 50 |
end; |
51 |
||
1500 | 52 |
structure Logic : LOGIC = |
0 | 53 |
struct |
398
41f279b477e2
added mk_type, dest_type, mk_inclass, dest_inclass (for axclasses);
wenzelm
parents:
210
diff
changeset
|
54 |
|
0 | 55 |
(*** Abstract syntax operations on the meta-connectives ***) |
56 |
||
57 |
(** equality **) |
|
58 |
||
64
0bbe5d86cb38
logic/mk_equals,mk_flexpair: now calls fastype_of instead of type_of.
lcp
parents:
0
diff
changeset
|
59 |
(*Make an equality.*) |
0bbe5d86cb38
logic/mk_equals,mk_flexpair: now calls fastype_of instead of type_of.
lcp
parents:
0
diff
changeset
|
60 |
fun mk_equals(t,u) = equals(fastype_of t) $ t $ u; |
0 | 61 |
|
62 |
fun dest_equals (Const("==",_) $ t $ u) = (t,u) |
|
63 |
| dest_equals t = raise TERM("dest_equals", [t]); |
|
64 |
||
637 | 65 |
fun is_equals (Const ("==", _) $ _ $ _) = true |
66 |
| is_equals _ = false; |
|
67 |
||
68 |
||
0 | 69 |
(** implies **) |
70 |
||
71 |
fun mk_implies(A,B) = implies $ A $ B; |
|
72 |
||
73 |
fun dest_implies (Const("==>",_) $ A $ B) = (A,B) |
|
74 |
| dest_implies A = raise TERM("dest_implies", [A]); |
|
75 |
||
76 |
(** nested implications **) |
|
77 |
||
78 |
(* [A1,...,An], B goes to A1==>...An==>B *) |
|
79 |
fun list_implies ([], B) = B : term |
|
80 |
| list_implies (A::AS, B) = implies $ A $ list_implies(AS,B); |
|
81 |
||
82 |
(* A1==>...An==>B goes to [A1,...,An], where B is not an implication *) |
|
83 |
fun strip_imp_prems (Const("==>", _) $ A $ B) = A :: strip_imp_prems B |
|
84 |
| strip_imp_prems _ = []; |
|
85 |
||
86 |
(* A1==>...An==>B goes to B, where B is not an implication *) |
|
87 |
fun strip_imp_concl (Const("==>", _) $ A $ B) = strip_imp_concl B |
|
88 |
| strip_imp_concl A = A : term; |
|
89 |
||
90 |
(*Strip and return premises: (i, [], A1==>...Ai==>B) |
|
1460 | 91 |
goes to ([Ai, A(i-1),...,A1] , B) (REVERSED) |
0 | 92 |
if i<0 or else i too big then raises TERM*) |
93 |
fun strip_prems (0, As, B) = (As, B) |
|
94 |
| strip_prems (i, As, Const("==>", _) $ A $ B) = |
|
1460 | 95 |
strip_prems (i-1, A::As, B) |
0 | 96 |
| strip_prems (_, As, A) = raise TERM("strip_prems", A::As); |
97 |
||
98 |
(*Count premises -- quicker than (length ostrip_prems) *) |
|
99 |
fun count_prems (Const("==>", _) $ A $ B, n) = count_prems (B,n+1) |
|
100 |
| count_prems (_,n) = n; |
|
101 |
||
102 |
(** flex-flex constraints **) |
|
103 |
||
64
0bbe5d86cb38
logic/mk_equals,mk_flexpair: now calls fastype_of instead of type_of.
lcp
parents:
0
diff
changeset
|
104 |
(*Make a constraint.*) |
0bbe5d86cb38
logic/mk_equals,mk_flexpair: now calls fastype_of instead of type_of.
lcp
parents:
0
diff
changeset
|
105 |
fun mk_flexpair(t,u) = flexpair(fastype_of t) $ t $ u; |
0 | 106 |
|
107 |
fun dest_flexpair (Const("=?=",_) $ t $ u) = (t,u) |
|
108 |
| dest_flexpair t = raise TERM("dest_flexpair", [t]); |
|
109 |
||
110 |
(*make flexflex antecedents: ( [(a1,b1),...,(an,bn)] , C ) |
|
111 |
goes to (a1=?=b1) ==>...(an=?=bn)==>C *) |
|
112 |
fun list_flexpairs ([], A) = A |
|
113 |
| list_flexpairs ((t,u)::pairs, A) = |
|
1460 | 114 |
implies $ (mk_flexpair(t,u)) $ list_flexpairs(pairs,A); |
0 | 115 |
|
116 |
(*Make the object-rule tpairs==>As==>B *) |
|
117 |
fun rule_of (tpairs, As, B) = list_flexpairs(tpairs, list_implies(As, B)); |
|
118 |
||
119 |
(*Remove and return flexflex pairs: |
|
1460 | 120 |
(a1=?=b1)==>...(an=?=bn)==>C to ( [(a1,b1),...,(an,bn)] , C ) |
0 | 121 |
[Tail recursive in order to return a pair of results] *) |
122 |
fun strip_flex_aux (pairs, Const("==>", _) $ (Const("=?=",_)$t$u) $ C) = |
|
123 |
strip_flex_aux ((t,u)::pairs, C) |
|
124 |
| strip_flex_aux (pairs,C) = (rev pairs, C); |
|
125 |
||
126 |
fun strip_flexpairs A = strip_flex_aux([], A); |
|
127 |
||
128 |
(*Discard flexflex pairs*) |
|
129 |
fun skip_flexpairs (Const("==>", _) $ (Const("=?=",_)$_$_) $ C) = |
|
1460 | 130 |
skip_flexpairs C |
0 | 131 |
| skip_flexpairs C = C; |
132 |
||
133 |
(*strip a proof state (Horn clause): |
|
134 |
(a1==b1)==>...(am==bm)==>B1==>...Bn==>C |
|
135 |
goes to ( [(a1,b1),...,(am,bm)] , [B1,...,Bn] , C) *) |
|
136 |
fun strip_horn A = |
|
137 |
let val (tpairs,horn) = strip_flexpairs A |
|
138 |
in (tpairs, strip_imp_prems horn, strip_imp_concl horn) end; |
|
139 |
||
398
41f279b477e2
added mk_type, dest_type, mk_inclass, dest_inclass (for axclasses);
wenzelm
parents:
210
diff
changeset
|
140 |
(** types as terms **) |
41f279b477e2
added mk_type, dest_type, mk_inclass, dest_inclass (for axclasses);
wenzelm
parents:
210
diff
changeset
|
141 |
|
41f279b477e2
added mk_type, dest_type, mk_inclass, dest_inclass (for axclasses);
wenzelm
parents:
210
diff
changeset
|
142 |
fun mk_type ty = Const ("TYPE", itselfT ty); |
41f279b477e2
added mk_type, dest_type, mk_inclass, dest_inclass (for axclasses);
wenzelm
parents:
210
diff
changeset
|
143 |
|
41f279b477e2
added mk_type, dest_type, mk_inclass, dest_inclass (for axclasses);
wenzelm
parents:
210
diff
changeset
|
144 |
fun dest_type (Const ("TYPE", Type ("itself", [ty]))) = ty |
41f279b477e2
added mk_type, dest_type, mk_inclass, dest_inclass (for axclasses);
wenzelm
parents:
210
diff
changeset
|
145 |
| dest_type t = raise TERM ("dest_type", [t]); |
41f279b477e2
added mk_type, dest_type, mk_inclass, dest_inclass (for axclasses);
wenzelm
parents:
210
diff
changeset
|
146 |
|
447 | 147 |
(** class constraints **) |
398
41f279b477e2
added mk_type, dest_type, mk_inclass, dest_inclass (for axclasses);
wenzelm
parents:
210
diff
changeset
|
148 |
|
41f279b477e2
added mk_type, dest_type, mk_inclass, dest_inclass (for axclasses);
wenzelm
parents:
210
diff
changeset
|
149 |
fun mk_inclass (ty, c) = |
41f279b477e2
added mk_type, dest_type, mk_inclass, dest_inclass (for axclasses);
wenzelm
parents:
210
diff
changeset
|
150 |
Const (Sign.const_of_class c, itselfT ty --> propT) $ mk_type ty; |
41f279b477e2
added mk_type, dest_type, mk_inclass, dest_inclass (for axclasses);
wenzelm
parents:
210
diff
changeset
|
151 |
|
41f279b477e2
added mk_type, dest_type, mk_inclass, dest_inclass (for axclasses);
wenzelm
parents:
210
diff
changeset
|
152 |
fun dest_inclass (t as Const (c_class, _) $ ty) = |
41f279b477e2
added mk_type, dest_type, mk_inclass, dest_inclass (for axclasses);
wenzelm
parents:
210
diff
changeset
|
153 |
((dest_type ty, Sign.class_of_const c_class) |
41f279b477e2
added mk_type, dest_type, mk_inclass, dest_inclass (for axclasses);
wenzelm
parents:
210
diff
changeset
|
154 |
handle TERM _ => raise TERM ("dest_inclass", [t])) |
41f279b477e2
added mk_type, dest_type, mk_inclass, dest_inclass (for axclasses);
wenzelm
parents:
210
diff
changeset
|
155 |
| dest_inclass t = raise TERM ("dest_inclass", [t]); |
41f279b477e2
added mk_type, dest_type, mk_inclass, dest_inclass (for axclasses);
wenzelm
parents:
210
diff
changeset
|
156 |
|
0 | 157 |
|
158 |
(*** Low-level term operations ***) |
|
159 |
||
160 |
(*Does t occur in u? Or is alpha-convertible to u? |
|
161 |
The term t must contain no loose bound variables*) |
|
162 |
fun t occs u = (t aconv u) orelse |
|
163 |
(case u of |
|
164 |
Abs(_,_,body) => t occs body |
|
1460 | 165 |
| f$t' => t occs f orelse t occs t' |
166 |
| _ => false); |
|
0 | 167 |
|
168 |
(*Close up a formula over all free variables by quantification*) |
|
169 |
fun close_form A = |
|
170 |
list_all_free (map dest_Free (sort atless (term_frees A)), |
|
1460 | 171 |
A); |
0 | 172 |
|
173 |
||
174 |
(*Freeze all (T)Vars by turning them into (T)Frees*) |
|
175 |
fun freeze_vars(Var(ixn,T)) = Free(Syntax.string_of_vname ixn, |
|
176 |
Type.freeze_vars T) |
|
177 |
| freeze_vars(Const(a,T)) = Const(a,Type.freeze_vars T) |
|
178 |
| freeze_vars(Free(a,T)) = Free(a,Type.freeze_vars T) |
|
179 |
| freeze_vars(s$t) = freeze_vars s $ freeze_vars t |
|
180 |
| freeze_vars(Abs(a,T,t)) = Abs(a,Type.freeze_vars T,freeze_vars t) |
|
181 |
| freeze_vars(b) = b; |
|
182 |
||
183 |
(*Reverse the effect of freeze_vars*) |
|
184 |
fun thaw_vars(Const(a,T)) = Const(a,Type.thaw_vars T) |
|
185 |
| thaw_vars(Free(a,T)) = |
|
186 |
let val T' = Type.thaw_vars T |
|
187 |
in case explode a of |
|
1460 | 188 |
"?"::vn => let val (ixn,_) = Syntax.scan_varname vn |
0 | 189 |
in Var(ixn,T') end |
1460 | 190 |
| _ => Free(a,T') |
0 | 191 |
end |
192 |
| thaw_vars(Abs(a,T,t)) = Abs(a,Type.thaw_vars T, thaw_vars t) |
|
193 |
| thaw_vars(s$t) = thaw_vars s $ thaw_vars t |
|
194 |
| thaw_vars(b) = b; |
|
195 |
||
196 |
||
197 |
(*** Specialized operations for resolution... ***) |
|
198 |
||
199 |
(*For all variables in the term, increment indexnames and lift over the Us |
|
200 |
result is ?Gidx(B.(lev+n-1),...,B.lev) where lev is abstraction level *) |
|
201 |
fun incr_indexes (Us: typ list, inc:int) t = |
|
202 |
let fun incr (Var ((a,i), T), lev) = |
|
1460 | 203 |
Unify.combound (Var((a, i+inc), Us---> incr_tvar inc T), |
204 |
lev, length Us) |
|
205 |
| incr (Abs (a,T,body), lev) = |
|
206 |
Abs (a, incr_tvar inc T, incr(body,lev+1)) |
|
207 |
| incr (Const(a,T),_) = Const(a, incr_tvar inc T) |
|
208 |
| incr (Free(a,T),_) = Free(a, incr_tvar inc T) |
|
209 |
| incr (f$t, lev) = incr(f,lev) $ incr(t,lev) |
|
210 |
| incr (t,lev) = t |
|
0 | 211 |
in incr(t,0) end; |
212 |
||
213 |
(*Make lifting functions from subgoal and increment. |
|
214 |
lift_abs operates on tpairs (unification constraints) |
|
215 |
lift_all operates on propositions *) |
|
216 |
fun lift_fns (B,inc) = |
|
217 |
let fun lift_abs (Us, Const("==>", _) $ _ $ B) u = lift_abs (Us,B) u |
|
1460 | 218 |
| lift_abs (Us, Const("all",_)$Abs(a,T,t)) u = |
219 |
Abs(a, T, lift_abs (T::Us, t) u) |
|
220 |
| lift_abs (Us, _) u = incr_indexes(rev Us, inc) u |
|
0 | 221 |
fun lift_all (Us, Const("==>", _) $ A $ B) u = |
1460 | 222 |
implies $ A $ lift_all (Us,B) u |
223 |
| lift_all (Us, Const("all",_)$Abs(a,T,t)) u = |
|
224 |
all T $ Abs(a, T, lift_all (T::Us,t) u) |
|
225 |
| lift_all (Us, _) u = incr_indexes(rev Us, inc) u; |
|
0 | 226 |
in (lift_abs([],B), lift_all([],B)) end; |
227 |
||
228 |
(*Strips assumptions in goal, yielding list of hypotheses. *) |
|
229 |
fun strip_assums_hyp (Const("==>", _) $ H $ B) = H :: strip_assums_hyp B |
|
230 |
| strip_assums_hyp (Const("all",_)$Abs(a,T,t)) = strip_assums_hyp t |
|
231 |
| strip_assums_hyp B = []; |
|
232 |
||
233 |
(*Strips assumptions in goal, yielding conclusion. *) |
|
234 |
fun strip_assums_concl (Const("==>", _) $ H $ B) = strip_assums_concl B |
|
235 |
| strip_assums_concl (Const("all",_)$Abs(a,T,t)) = strip_assums_concl t |
|
236 |
| strip_assums_concl B = B; |
|
237 |
||
238 |
(*Make a list of all the parameters in a subgoal, even if nested*) |
|
239 |
fun strip_params (Const("==>", _) $ H $ B) = strip_params B |
|
240 |
| strip_params (Const("all",_)$Abs(a,T,t)) = (a,T) :: strip_params t |
|
241 |
| strip_params B = []; |
|
242 |
||
243 |
(*Removes the parameters from a subgoal and renumber bvars in hypotheses, |
|
244 |
where j is the total number of parameters (precomputed) |
|
245 |
If n>0 then deletes assumption n. *) |
|
246 |
fun remove_params j n A = |
|
247 |
if j=0 andalso n<=0 then A (*nothing left to do...*) |
|
248 |
else case A of |
|
249 |
Const("==>", _) $ H $ B => |
|
1460 | 250 |
if n=1 then (remove_params j (n-1) B) |
251 |
else implies $ (incr_boundvars j H) $ (remove_params j (n-1) B) |
|
0 | 252 |
| Const("all",_)$Abs(a,T,t) => remove_params (j-1) n t |
253 |
| _ => if n>0 then raise TERM("remove_params", [A]) |
|
254 |
else A; |
|
255 |
||
256 |
(** Auto-renaming of parameters in subgoals **) |
|
257 |
||
258 |
val auto_rename = ref false |
|
259 |
and rename_prefix = ref "ka"; |
|
260 |
||
261 |
(*rename_prefix is not exported; it is set by this function.*) |
|
262 |
fun set_rename_prefix a = |
|
263 |
if a<>"" andalso forall is_letter (explode a) |
|
264 |
then (rename_prefix := a; auto_rename := true) |
|
265 |
else error"rename prefix must be nonempty and consist of letters"; |
|
266 |
||
267 |
(*Makes parameters in a goal have distinctive names (not guaranteed unique!) |
|
268 |
A name clash could cause the printer to rename bound vars; |
|
269 |
then res_inst_tac would not work properly.*) |
|
270 |
fun rename_vars (a, []) = [] |
|
271 |
| rename_vars (a, (_,T)::vars) = |
|
272 |
(a,T) :: rename_vars (bump_string a, vars); |
|
273 |
||
274 |
(*Move all parameters to the front of the subgoal, renaming them apart; |
|
275 |
if n>0 then deletes assumption n. *) |
|
276 |
fun flatten_params n A = |
|
277 |
let val params = strip_params A; |
|
1460 | 278 |
val vars = if !auto_rename |
279 |
then rename_vars (!rename_prefix, params) |
|
280 |
else variantlist(map #1 params,[]) ~~ map #2 params |
|
0 | 281 |
in list_all (vars, remove_params (length vars) n A) |
282 |
end; |
|
283 |
||
284 |
(*Makes parameters in a goal have the names supplied by the list cs.*) |
|
285 |
fun list_rename_params (cs, Const("==>", _) $ A $ B) = |
|
286 |
implies $ A $ list_rename_params (cs, B) |
|
287 |
| list_rename_params (c::cs, Const("all",_)$Abs(_,T,t)) = |
|
288 |
all T $ Abs(c, T, list_rename_params (cs, t)) |
|
289 |
| list_rename_params (cs, B) = B; |
|
290 |
||
291 |
(*Strips assumptions in goal yielding ( [Hn,...,H1], [xm,...,x1], B ) |
|
292 |
where H1,...,Hn are the hypotheses and x1...xm are the parameters. *) |
|
293 |
fun strip_assums_aux (Hs, params, Const("==>", _) $ H $ B) = |
|
1460 | 294 |
strip_assums_aux (H::Hs, params, B) |
0 | 295 |
| strip_assums_aux (Hs, params, Const("all",_)$Abs(a,T,t)) = |
1460 | 296 |
strip_assums_aux (Hs, (a,T)::params, t) |
0 | 297 |
| strip_assums_aux (Hs, params, B) = (Hs, params, B); |
298 |
||
299 |
fun strip_assums A = strip_assums_aux ([],[],A); |
|
300 |
||
301 |
||
302 |
(*Produces disagreement pairs, one for each assumption proof, in order. |
|
303 |
A is the first premise of the lifted rule, and thus has the form |
|
304 |
H1 ==> ... Hk ==> B and the pairs are (H1,B),...,(Hk,B) *) |
|
305 |
fun assum_pairs A = |
|
306 |
let val (Hs, params, B) = strip_assums A |
|
307 |
val D = Unify.rlist_abs(params, B) |
|
308 |
fun pairrev ([],pairs) = pairs |
|
309 |
| pairrev (H::Hs,pairs) = |
|
1460 | 310 |
pairrev(Hs, (Unify.rlist_abs(params,H), D) :: pairs) |
0 | 311 |
in pairrev (Hs,[]) (*WAS: map pair (rev Hs) *) |
312 |
end; |
|
313 |
||
314 |
||
315 |
(*Converts Frees to Vars and TFrees to TVars so that axioms can be written |
|
316 |
without (?) everywhere*) |
|
317 |
fun varify (Const(a,T)) = Const(a, Type.varifyT T) |
|
318 |
| varify (Free(a,T)) = Var((a,0), Type.varifyT T) |
|
319 |
| varify (Var(ixn,T)) = Var(ixn, Type.varifyT T) |
|
320 |
| varify (Abs (a,T,body)) = Abs (a, Type.varifyT T, varify body) |
|
321 |
| varify (f$t) = varify f $ varify t |
|
322 |
| varify t = t; |
|
323 |
||
546 | 324 |
(*Inverse of varify. Converts axioms back to their original form.*) |
585 | 325 |
fun unvarify (Const(a,T)) = Const(a, Type.unvarifyT T) |
326 |
| unvarify (Var((a,0), T)) = Free(a, Type.unvarifyT T) |
|
327 |
| unvarify (Var(ixn,T)) = Var(ixn, Type.unvarifyT T) (*non-0 index!*) |
|
328 |
| unvarify (Abs (a,T,body)) = Abs (a, Type.unvarifyT T, unvarify body) |
|
546 | 329 |
| unvarify (f$t) = unvarify f $ unvarify t |
330 |
| unvarify t = t; |
|
331 |
||
0 | 332 |
end; |