author | paulson |
Fri, 05 Nov 1999 12:45:37 +0100 | |
changeset 7999 | 7acf6eb8eec1 |
parent 5278 | a903b66822e2 |
child 8624 | 69619f870939 |
permissions | -rw-r--r-- |
3192 | 1 |
(* Title: Subst/Unify |
3266
89e5f4163663
Removed rprod from the WF relation; simplified proofs;
paulson
parents:
3241
diff
changeset
|
2 |
ID: $Id$ |
3192 | 3 |
Author: Konrad Slind, Cambridge University Computer Laboratory |
4 |
Copyright 1997 University of Cambridge |
|
5 |
||
6 |
Unification algorithm |
|
7 |
*) |
|
8 |
||
9 |
(*--------------------------------------------------------------------------- |
|
10 |
* This file defines a nested unification algorithm, then proves that it |
|
11 |
* terminates, then proves 2 correctness theorems: that when the algorithm |
|
12 |
* succeeds, it 1) returns an MGU; and 2) returns an idempotent substitution. |
|
13 |
* Although the proofs may seem long, they are actually quite direct, in that |
|
14 |
* the correctness and termination properties are not mingled as much as in |
|
15 |
* previous proofs of this algorithm. |
|
16 |
* |
|
17 |
* Our approach for nested recursive functions is as follows: |
|
18 |
* |
|
19 |
* 0. Prove the wellfoundedness of the termination relation. |
|
20 |
* 1. Prove the non-nested termination conditions. |
|
21 |
* 2. Eliminate (0) and (1) from the recursion equations and the |
|
22 |
* induction theorem. |
|
23 |
* 3. Prove the nested termination conditions by using the induction |
|
24 |
* theorem from (2) and by using the recursion equations from (2). |
|
25 |
* These are constrained by the nested termination conditions, but |
|
26 |
* things work out magically (by wellfoundedness of the termination |
|
27 |
* relation). |
|
28 |
* 4. Eliminate the nested TCs from the results of (2). |
|
29 |
* 5. Prove further correctness properties using the results of (4). |
|
30 |
* |
|
31 |
* Deeper nestings require iteration of steps (3) and (4). |
|
32 |
*---------------------------------------------------------------------------*) |
|
33 |
||
34 |
open Unify; |
|
35 |
||
36 |
||
37 |
||
38 |
(*--------------------------------------------------------------------------- |
|
39 |
* The non-nested TC plus the wellfoundedness of unifyRel. |
|
40 |
*---------------------------------------------------------------------------*) |
|
41 |
Tfl.tgoalw Unify.thy [] unify.rules; |
|
42 |
(* Wellfoundedness of unifyRel *) |
|
4089 | 43 |
by (simp_tac (simpset() addsimps [unifyRel_def, |
3192 | 44 |
wf_inv_image, wf_lex_prod, wf_finite_psubset, |
3299 | 45 |
wf_measure]) 1); |
3192 | 46 |
(* TC *) |
3724 | 47 |
by Safe_tac; |
4089 | 48 |
by (simp_tac (simpset() addsimps [finite_psubset_def, finite_vars_of, |
3266
89e5f4163663
Removed rprod from the WF relation; simplified proofs;
paulson
parents:
3241
diff
changeset
|
49 |
lex_prod_def, measure_def, inv_image_def]) 1); |
3192 | 50 |
by (rtac (monotone_vars_of RS (subset_iff_psubset_eq RS iffD1) RS disjE) 1); |
51 |
by (Blast_tac 1); |
|
4089 | 52 |
by (asm_simp_tac (simpset() addsimps [less_eq, less_add_Suc1]) 1); |
3192 | 53 |
qed "tc0"; |
54 |
||
55 |
||
56 |
(*--------------------------------------------------------------------------- |
|
57 |
* Termination proof. |
|
58 |
*---------------------------------------------------------------------------*) |
|
59 |
||
5069 | 60 |
Goalw [unifyRel_def, measure_def] "trans unifyRel"; |
3241 | 61 |
by (REPEAT (resolve_tac [trans_inv_image, trans_lex_prod, |
62 |
trans_finite_psubset, trans_less_than, |
|
3266
89e5f4163663
Removed rprod from the WF relation; simplified proofs;
paulson
parents:
3241
diff
changeset
|
63 |
trans_inv_image] 1)); |
3192 | 64 |
qed "trans_unifyRel"; |
65 |
||
66 |
||
67 |
(*--------------------------------------------------------------------------- |
|
68 |
* The following lemma is used in the last step of the termination proof for |
|
69 |
* the nested call in Unify. Loosely, it says that unifyRel doesn't care |
|
70 |
* about term structure. |
|
71 |
*---------------------------------------------------------------------------*) |
|
5069 | 72 |
Goalw [unifyRel_def,lex_prod_def, inv_image_def] |
5119 | 73 |
"((X,Y), (Comb A (Comb B C), Comb D (Comb E F))) : unifyRel ==> \ |
74 |
\ ((X,Y), (Comb (Comb A B) C, Comb (Comb D E) F)) : unifyRel"; |
|
4089 | 75 |
by (asm_full_simp_tac (simpset() addsimps [measure_def, |
3192 | 76 |
less_eq, inv_image_def,add_assoc]) 1); |
77 |
by (subgoal_tac "(vars_of A Un vars_of B Un vars_of C Un \ |
|
78 |
\ (vars_of D Un vars_of E Un vars_of F)) = \ |
|
79 |
\ (vars_of A Un (vars_of B Un vars_of C) Un \ |
|
80 |
\ (vars_of D Un (vars_of E Un vars_of F)))" 1); |
|
81 |
by (Blast_tac 2); |
|
82 |
by (Asm_simp_tac 1); |
|
83 |
qed "Rassoc"; |
|
84 |
||
85 |
||
86 |
(*--------------------------------------------------------------------------- |
|
87 |
* This lemma proves the nested termination condition for the base cases |
|
88 |
* 3, 4, and 6. |
|
89 |
*---------------------------------------------------------------------------*) |
|
5278 | 90 |
Goal "~(Var x <: M) ==> \ |
3192 | 91 |
\ ((N1 <| [(x,M)], N2 <| [(x,M)]), (Comb M N1, Comb(Var x) N2)) : unifyRel \ |
92 |
\ & ((N1 <| [(x,M)], N2 <| [(x,M)]), (Comb(Var x) N1, Comb M N2)) : unifyRel"; |
|
93 |
by (case_tac "Var x = M" 1); |
|
94 |
by (hyp_subst_tac 1); |
|
95 |
by (Simp_tac 1); |
|
96 |
by (case_tac "x: (vars_of N1 Un vars_of N2)" 1); |
|
97 |
(*uterm_less case*) |
|
98 |
by (asm_simp_tac |
|
4089 | 99 |
(simpset() addsimps [less_eq, unifyRel_def, lex_prod_def, |
3192 | 100 |
measure_def, inv_image_def]) 1); |
101 |
by (Blast_tac 1); |
|
102 |
(*finite_psubset case*) |
|
103 |
by (simp_tac |
|
4089 | 104 |
(simpset() addsimps [unifyRel_def, lex_prod_def, |
3192 | 105 |
measure_def, inv_image_def]) 1); |
4089 | 106 |
by (simp_tac (simpset() addsimps [finite_psubset_def, finite_vars_of, |
3192 | 107 |
psubset_def, set_eq_subset]) 1); |
108 |
by (Blast_tac 1); |
|
109 |
(** LEVEL 9 **) |
|
110 |
(*Final case, also finite_psubset*) |
|
111 |
by (simp_tac |
|
4089 | 112 |
(simpset() addsimps [finite_vars_of, unifyRel_def, finite_psubset_def, |
3192 | 113 |
lex_prod_def, measure_def, inv_image_def]) 1); |
114 |
by (cut_inst_tac [("s","[(x,M)]"), ("v", "x"), ("t","N2")] Var_elim 1); |
|
115 |
by (cut_inst_tac [("s","[(x,M)]"), ("v", "x"), ("t","N1")] Var_elim 3); |
|
4089 | 116 |
by (ALLGOALS (asm_simp_tac(simpset() addsimps [srange_iff, vars_iff_occseq]))); |
3192 | 117 |
by (REPEAT_FIRST (resolve_tac [conjI, disjI1, psubsetI])); |
118 |
by (ALLGOALS (asm_full_simp_tac |
|
4089 | 119 |
(simpset() addsimps [srange_iff, set_eq_subset]))); |
3192 | 120 |
by (ALLGOALS |
4089 | 121 |
(fast_tac (claset() addEs [Var_intro RS disjE] |
122 |
addss (simpset() addsimps [srange_iff])))); |
|
3192 | 123 |
qed "var_elimR"; |
124 |
||
125 |
||
126 |
(*--------------------------------------------------------------------------- |
|
3266
89e5f4163663
Removed rprod from the WF relation; simplified proofs;
paulson
parents:
3241
diff
changeset
|
127 |
* Eliminate tc0 from the recursion equations and the induction theorem. |
89e5f4163663
Removed rprod from the WF relation; simplified proofs;
paulson
parents:
3241
diff
changeset
|
128 |
*---------------------------------------------------------------------------*) |
3392 | 129 |
val wfr = tc0 RS conjunct1 |
130 |
and tc = tc0 RS conjunct2; |
|
3266
89e5f4163663
Removed rprod from the WF relation; simplified proofs;
paulson
parents:
3241
diff
changeset
|
131 |
val unifyRules0 = map (normalize_thm [fn th => wfr RS th, fn th => tc RS th]) |
89e5f4163663
Removed rprod from the WF relation; simplified proofs;
paulson
parents:
3241
diff
changeset
|
132 |
unify.rules; |
89e5f4163663
Removed rprod from the WF relation; simplified proofs;
paulson
parents:
3241
diff
changeset
|
133 |
|
89e5f4163663
Removed rprod from the WF relation; simplified proofs;
paulson
parents:
3241
diff
changeset
|
134 |
val unifyInduct0 = [wfr,tc] MRS unify.induct; |
89e5f4163663
Removed rprod from the WF relation; simplified proofs;
paulson
parents:
3241
diff
changeset
|
135 |
|
89e5f4163663
Removed rprod from the WF relation; simplified proofs;
paulson
parents:
3241
diff
changeset
|
136 |
|
89e5f4163663
Removed rprod from the WF relation; simplified proofs;
paulson
parents:
3241
diff
changeset
|
137 |
(*--------------------------------------------------------------------------- |
3192 | 138 |
* The nested TC. Proved by recursion induction. |
139 |
*---------------------------------------------------------------------------*) |
|
3266
89e5f4163663
Removed rprod from the WF relation; simplified proofs;
paulson
parents:
3241
diff
changeset
|
140 |
val [_,_,tc3] = unify.tcs; |
3241 | 141 |
goalw_cterm [] (cterm_of (sign_of Unify.thy) (HOLogic.mk_Trueprop tc3)); |
3192 | 142 |
(*--------------------------------------------------------------------------- |
143 |
* The extracted TC needs the scope of its quantifiers adjusted, so our |
|
144 |
* first step is to restrict the scopes of N1 and N2. |
|
145 |
*---------------------------------------------------------------------------*) |
|
146 |
by (subgoal_tac "!M1 M2 theta. \ |
|
3299 | 147 |
\ unify(M1, M2) = Some theta --> \ |
3192 | 148 |
\ (!N1 N2. ((N1<|theta, N2<|theta), Comb M1 N1, Comb M2 N2) : unifyRel)" 1); |
149 |
by (Blast_tac 1); |
|
150 |
by (rtac allI 1); |
|
151 |
by (rtac allI 1); |
|
152 |
(* Apply induction *) |
|
3299 | 153 |
by (res_inst_tac [("u","M1"),("v","M2")] unifyInduct0 1); |
3192 | 154 |
by (ALLGOALS |
4686 | 155 |
(asm_simp_tac (simpset() addsimps (var_elimR::unifyRules0)))); |
3192 | 156 |
(*Const-Const case*) |
157 |
by (simp_tac |
|
4089 | 158 |
(simpset() addsimps [unifyRel_def, lex_prod_def, measure_def, |
3266
89e5f4163663
Removed rprod from the WF relation; simplified proofs;
paulson
parents:
3241
diff
changeset
|
159 |
inv_image_def, less_eq]) 1); |
3192 | 160 |
(** LEVEL 7 **) |
161 |
(*Comb-Comb case*) |
|
5184 | 162 |
by (asm_simp_tac (simpset() addsplits [option.split]) 1); |
3334 | 163 |
by (strip_tac 1); |
3192 | 164 |
by (rtac (trans_unifyRel RS transD) 1); |
165 |
by (Blast_tac 1); |
|
166 |
by (simp_tac (HOL_ss addsimps [subst_Comb RS sym]) 1); |
|
167 |
by (rtac Rassoc 1); |
|
168 |
by (Blast_tac 1); |
|
3266
89e5f4163663
Removed rprod from the WF relation; simplified proofs;
paulson
parents:
3241
diff
changeset
|
169 |
qed_spec_mp "unify_TC"; |
3192 | 170 |
|
171 |
||
172 |
(*--------------------------------------------------------------------------- |
|
173 |
* Now for elimination of nested TC from unify.rules and induction. |
|
174 |
*---------------------------------------------------------------------------*) |
|
175 |
||
176 |
(*Desired rule, copied from the theory file. Could it be made available?*) |
|
5278 | 177 |
Goal "unify(Comb M1 N1, Comb M2 N2) = \ |
3192 | 178 |
\ (case unify(M1,M2) \ |
3299 | 179 |
\ of None => None \ |
180 |
\ | Some theta => (case unify(N1 <| theta, N2 <| theta) \ |
|
181 |
\ of None => None \ |
|
182 |
\ | Some sigma => Some (theta <> sigma)))"; |
|
4089 | 183 |
by (asm_simp_tac (simpset() addsimps (unify_TC::unifyRules0) |
5184 | 184 |
addsplits [option.split]) 1); |
3192 | 185 |
qed "unifyCombComb"; |
186 |
||
187 |
||
188 |
val unifyRules = rev (unifyCombComb :: tl (rev unifyRules0)); |
|
189 |
||
190 |
Addsimps unifyRules; |
|
191 |
||
3266
89e5f4163663
Removed rprod from the WF relation; simplified proofs;
paulson
parents:
3241
diff
changeset
|
192 |
bind_thm ("unifyInduct", |
89e5f4163663
Removed rprod from the WF relation; simplified proofs;
paulson
parents:
3241
diff
changeset
|
193 |
rule_by_tactic |
4089 | 194 |
(ALLGOALS (full_simp_tac (simpset() addsimps [unify_TC]))) |
3266
89e5f4163663
Removed rprod from the WF relation; simplified proofs;
paulson
parents:
3241
diff
changeset
|
195 |
unifyInduct0); |
3192 | 196 |
|
197 |
||
198 |
(*--------------------------------------------------------------------------- |
|
199 |
* Correctness. Notice that idempotence is not needed to prove that the |
|
200 |
* algorithm terminates and is not needed to prove the algorithm correct, |
|
201 |
* if you are only interested in an MGU. This is in contrast to the |
|
202 |
* approach of M&W, who used idempotence and MGU-ness in the termination proof. |
|
203 |
*---------------------------------------------------------------------------*) |
|
204 |
||
5069 | 205 |
Goal "!theta. unify(M,N) = Some theta --> MGUnifier theta M N"; |
3299 | 206 |
by (res_inst_tac [("u","M"),("v","N")] unifyInduct 1); |
4686 | 207 |
by (ALLGOALS Asm_simp_tac); |
3192 | 208 |
(*Const-Const case*) |
4089 | 209 |
by (simp_tac (simpset() addsimps [MGUnifier_def,Unifier_def]) 1); |
3192 | 210 |
(*Const-Var case*) |
211 |
by (stac mgu_sym 1); |
|
4089 | 212 |
by (simp_tac (simpset() addsimps [MGUnifier_Var]) 1); |
3192 | 213 |
(*Var-M case*) |
4089 | 214 |
by (simp_tac (simpset() addsimps [MGUnifier_Var]) 1); |
3192 | 215 |
(*Comb-Var case*) |
216 |
by (stac mgu_sym 1); |
|
4089 | 217 |
by (simp_tac (simpset() addsimps [MGUnifier_Var]) 1); |
3334 | 218 |
(** LEVEL 8 **) |
3192 | 219 |
(*Comb-Comb case*) |
5184 | 220 |
by (asm_simp_tac (simpset() addsplits [option.split]) 1); |
3334 | 221 |
by (strip_tac 1); |
222 |
by (rotate_tac ~2 1); |
|
223 |
by (asm_full_simp_tac |
|
4089 | 224 |
(simpset() addsimps [MGUnifier_def, Unifier_def, MoreGeneral_def]) 1); |
4153 | 225 |
by (Safe_tac THEN rename_tac "theta sigma gamma" 1); |
3334 | 226 |
by (eres_inst_tac [("x","gamma")] allE 1 THEN mp_tac 1); |
227 |
by (etac exE 1 THEN rename_tac "delta" 1); |
|
3192 | 228 |
by (eres_inst_tac [("x","delta")] allE 1); |
229 |
by (subgoal_tac "N1 <| theta <| delta = N2 <| theta <| delta" 1); |
|
230 |
(*Proving the subgoal*) |
|
4089 | 231 |
by (full_simp_tac (simpset() addsimps [subst_eq_iff]) 2 |
232 |
THEN blast_tac (claset() addIs [trans,sym] delrules [impCE]) 2); |
|
233 |
by (blast_tac (claset() addIs [subst_trans, subst_cong, |
|
3192 | 234 |
comp_assoc RS subst_sym]) 1); |
235 |
qed_spec_mp "unify_gives_MGU"; |
|
236 |
||
237 |
||
238 |
(*--------------------------------------------------------------------------- |
|
239 |
* Unify returns idempotent substitutions, when it succeeds. |
|
240 |
*---------------------------------------------------------------------------*) |
|
5069 | 241 |
Goal "!theta. unify(M,N) = Some theta --> Idem theta"; |
3299 | 242 |
by (res_inst_tac [("u","M"),("v","N")] unifyInduct 1); |
3334 | 243 |
by (ALLGOALS |
244 |
(asm_simp_tac |
|
5184 | 245 |
(simpset() addsimps [Var_Idem] addsplits [option.split]))); |
3192 | 246 |
(*Comb-Comb case*) |
4153 | 247 |
by Safe_tac; |
3334 | 248 |
by (REPEAT (dtac spec 1 THEN mp_tac 1)); |
4089 | 249 |
by (safe_tac (claset() addSDs [rewrite_rule [MGUnifier_def] unify_gives_MGU])); |
3192 | 250 |
by (rtac Idem_comp 1); |
251 |
by (atac 1); |
|
252 |
by (atac 1); |
|
4089 | 253 |
by (best_tac (claset() addss (simpset() addsimps |
3266
89e5f4163663
Removed rprod from the WF relation; simplified proofs;
paulson
parents:
3241
diff
changeset
|
254 |
[MoreGeneral_def, subst_eq_iff, Idem_def])) 1); |
3192 | 255 |
qed_spec_mp "unify_gives_Idem"; |
256 |