104 |
104 |
105 lemma Var_in_subst: |
105 lemma Var_in_subst: |
106 "v \<in> vars_of t \<Longrightarrow> w \<in> vars_of (t \<lhd> (v, Var(w)) # s)" |
106 "v \<in> vars_of t \<Longrightarrow> w \<in> vars_of (t \<lhd> (v, Var(w)) # s)" |
107 by (induct t) auto |
107 by (induct t) auto |
108 |
108 |
|
109 lemma remove_var: "v \<notin> vars_of s \<Longrightarrow> v \<notin> vars_of (t \<lhd> [(v, s)])" |
|
110 by (induct t) simp_all |
|
111 |
109 lemma subst_refl[iff]: "s \<doteq> s" |
112 lemma subst_refl[iff]: "s \<doteq> s" |
110 by (auto simp:subst_eq_def) |
113 by (auto simp:subst_eq_def) |
111 |
114 |
112 lemma subst_sym[sym]: "\<lbrakk>s1 \<doteq> s2\<rbrakk> \<Longrightarrow> s2 \<doteq> s1" |
115 lemma subst_sym[sym]: "\<lbrakk>s1 \<doteq> s2\<rbrakk> \<Longrightarrow> s2 \<doteq> s1" |
113 by (auto simp:subst_eq_def) |
116 by (auto simp:subst_eq_def) |
114 |
117 |
115 lemma subst_trans[trans]: "\<lbrakk>s1 \<doteq> s2; s2 \<doteq> s3\<rbrakk> \<Longrightarrow> s1 \<doteq> s3" |
118 lemma subst_trans[trans]: "\<lbrakk>s1 \<doteq> s2; s2 \<doteq> s3\<rbrakk> \<Longrightarrow> s1 \<doteq> s3" |
116 by (auto simp:subst_eq_def) |
119 by (auto simp:subst_eq_def) |
117 |
120 |
|
121 |
118 text {* Composition of Substitutions *} |
122 text {* Composition of Substitutions *} |
119 |
|
120 |
123 |
121 lemma comp_Nil[simp]: "\<sigma> \<lozenge> [] = \<sigma>" |
124 lemma comp_Nil[simp]: "\<sigma> \<lozenge> [] = \<sigma>" |
122 by (induct \<sigma>) auto |
125 by (induct \<sigma>) auto |
123 |
126 |
124 lemma subst_comp[simp]: "t \<lhd> (r \<lozenge> s) = t \<lhd> r \<lhd> s" |
127 lemma subst_comp[simp]: "t \<lhd> (r \<lozenge> s) = t \<lhd> r \<lhd> s" |
137 by auto |
140 by auto |
138 |
141 |
139 lemma subst_cong: "\<lbrakk>\<sigma> \<doteq> \<sigma>'; \<theta> \<doteq> \<theta>'\<rbrakk> \<Longrightarrow> (\<sigma> \<lozenge> \<theta>) \<doteq> (\<sigma>' \<lozenge> \<theta>')" |
142 lemma subst_cong: "\<lbrakk>\<sigma> \<doteq> \<sigma>'; \<theta> \<doteq> \<theta>'\<rbrakk> \<Longrightarrow> (\<sigma> \<lozenge> \<theta>) \<doteq> (\<sigma>' \<lozenge> \<theta>')" |
140 by (auto simp: subst_eq_def) |
143 by (auto simp: subst_eq_def) |
141 |
144 |
|
145 lemma var_self: "[(v, Var v)] \<doteq> []" |
|
146 proof |
|
147 fix t show "t \<lhd> [(v, Var v)] = t \<lhd> []" |
|
148 by (induct t) simp_all |
|
149 qed |
|
150 |
|
151 lemma var_same[simp]: "[(v, t)] \<doteq> [] \<longleftrightarrow> t = Var v" |
|
152 by (metis assoc.simps(2) subst.simps(1) subst_eq_def var_self) |
142 |
153 |
143 |
154 |
144 subsection {* Specification: Most general unifiers *} |
155 subsection {* Specification: Most general unifiers *} |
145 |
156 |
146 definition Unifier :: "'a subst \<Rightarrow> 'a trm \<Rightarrow> 'a trm \<Rightarrow> bool" |
157 definition Unifier :: "'a subst \<Rightarrow> 'a trm \<Rightarrow> 'a trm \<Rightarrow> bool" |
157 |
168 |
158 lemma MGU_sym[sym]: |
169 lemma MGU_sym[sym]: |
159 "MGU \<sigma> s t \<Longrightarrow> MGU \<sigma> t s" |
170 "MGU \<sigma> s t \<Longrightarrow> MGU \<sigma> t s" |
160 by (auto simp:MGU_def Unifier_def) |
171 by (auto simp:MGU_def Unifier_def) |
161 |
172 |
|
173 lemma MGU_Var: |
|
174 assumes "\<not> Var v \<prec> t" |
|
175 shows "MGU [(v,t)] (Var v) t" |
|
176 proof (intro MGUI exI) |
|
177 show "Var v \<lhd> [(v,t)] = t \<lhd> [(v,t)]" using assms |
|
178 by (metis assoc.simps(2) repl_invariance subst.simps(1) subst_Nil vars_iff_occseq) |
|
179 next |
|
180 fix \<theta> assume th: "Var v \<lhd> \<theta> = t \<lhd> \<theta>" |
|
181 show "\<theta> \<doteq> [(v,t)] \<lozenge> \<theta>" |
|
182 proof |
|
183 fix s show "s \<lhd> \<theta> = s \<lhd> [(v,t)] \<lozenge> \<theta>" using th |
|
184 by (induct s) auto |
|
185 qed |
|
186 qed |
|
187 |
|
188 lemma MGU_Const: "MGU [] (Const c) (Const d) \<longleftrightarrow> c = d" |
|
189 by (auto simp: MGU_def Unifier_def) |
|
190 |
162 |
191 |
163 definition Idem :: "'a subst \<Rightarrow> bool" |
192 definition Idem :: "'a subst \<Rightarrow> bool" |
164 where "Idem s \<longleftrightarrow> (s \<lozenge> s) \<doteq> s" |
193 where "Idem s \<longleftrightarrow> (s \<lozenge> s) \<doteq> s" |
165 |
194 |
166 |
195 |
187 Some \<theta> \<Rightarrow> (case unify (N \<lhd> \<theta>) (N' \<lhd> \<theta>) |
216 Some \<theta> \<Rightarrow> (case unify (N \<lhd> \<theta>) (N' \<lhd> \<theta>) |
188 of None \<Rightarrow> None | |
217 of None \<Rightarrow> None | |
189 Some \<sigma> \<Rightarrow> Some (\<theta> \<lozenge> \<sigma>)))" |
218 Some \<sigma> \<Rightarrow> Some (\<theta> \<lozenge> \<sigma>)))" |
190 by pat_completeness auto |
219 by pat_completeness auto |
191 |
220 |
192 declare unify.psimps[simp] |
|
193 |
|
194 subsection {* Partial correctness *} |
|
195 |
|
196 text {* Some lemmas about occs and MGU: *} |
|
197 |
|
198 lemma subst_no_occs: "\<not> Var v \<prec> t \<Longrightarrow> Var v \<noteq> t |
|
199 \<Longrightarrow> t \<lhd> [(v,s)] = t" |
|
200 by (induct t) auto |
|
201 |
|
202 lemma MGU_Var[intro]: |
|
203 assumes no_occs: "\<not> Var v \<prec> t" |
|
204 shows "MGU [(v,t)] (Var v) t" |
|
205 proof (intro MGUI exI) |
|
206 show "Var v \<lhd> [(v,t)] = t \<lhd> [(v,t)]" using no_occs |
|
207 by (cases "Var v = t", auto simp:subst_no_occs) |
|
208 next |
|
209 fix \<theta> assume th: "Var v \<lhd> \<theta> = t \<lhd> \<theta>" |
|
210 show "\<theta> \<doteq> [(v,t)] \<lozenge> \<theta>" |
|
211 proof |
|
212 fix s show "s \<lhd> \<theta> = s \<lhd> [(v,t)] \<lozenge> \<theta>" using th |
|
213 by (induct s) auto |
|
214 qed |
|
215 qed |
|
216 |
|
217 declare MGU_Var[symmetric, intro] |
|
218 |
|
219 lemma MGU_Const[simp]: "MGU [] (Const c) (Const d) = (c = d)" |
|
220 unfolding MGU_def Unifier_def |
|
221 by auto |
|
222 |
|
223 text {* If unification terminates, then it computes most general unifiers: *} |
|
224 |
|
225 lemma unify_partial_correctness: |
|
226 assumes "unify_dom (M, N)" |
|
227 assumes "unify M N = Some \<sigma>" |
|
228 shows "MGU \<sigma> M N" |
|
229 using assms |
|
230 proof (induct M N arbitrary: \<sigma>) |
|
231 case (7 M N M' N' \<sigma>) -- "The interesting case" |
|
232 |
|
233 then obtain \<theta>1 \<theta>2 |
|
234 where "unify M M' = Some \<theta>1" |
|
235 and "unify (N \<lhd> \<theta>1) (N' \<lhd> \<theta>1) = Some \<theta>2" |
|
236 and \<sigma>: "\<sigma> = \<theta>1 \<lozenge> \<theta>2" |
|
237 and MGU_inner: "MGU \<theta>1 M M'" |
|
238 and MGU_outer: "MGU \<theta>2 (N \<lhd> \<theta>1) (N' \<lhd> \<theta>1)" |
|
239 by (auto split:option.split_asm) |
|
240 |
|
241 show ?case |
|
242 proof |
|
243 from MGU_inner and MGU_outer |
|
244 have "M \<lhd> \<theta>1 = M' \<lhd> \<theta>1" |
|
245 and "N \<lhd> \<theta>1 \<lhd> \<theta>2 = N' \<lhd> \<theta>1 \<lhd> \<theta>2" |
|
246 unfolding MGU_def Unifier_def |
|
247 by auto |
|
248 thus "M \<cdot> N \<lhd> \<sigma> = M' \<cdot> N' \<lhd> \<sigma>" unfolding \<sigma> |
|
249 by simp |
|
250 next |
|
251 fix \<sigma>' assume "M \<cdot> N \<lhd> \<sigma>' = M' \<cdot> N' \<lhd> \<sigma>'" |
|
252 hence "M \<lhd> \<sigma>' = M' \<lhd> \<sigma>'" |
|
253 and Ns: "N \<lhd> \<sigma>' = N' \<lhd> \<sigma>'" by auto |
|
254 |
|
255 with MGU_inner obtain \<delta> |
|
256 where eqv: "\<sigma>' \<doteq> \<theta>1 \<lozenge> \<delta>" |
|
257 unfolding MGU_def Unifier_def |
|
258 by auto |
|
259 |
|
260 from Ns have "N \<lhd> \<theta>1 \<lhd> \<delta> = N' \<lhd> \<theta>1 \<lhd> \<delta>" |
|
261 by (simp add:subst_eq_dest[OF eqv]) |
|
262 |
|
263 with MGU_outer obtain \<rho> |
|
264 where eqv2: "\<delta> \<doteq> \<theta>2 \<lozenge> \<rho>" |
|
265 unfolding MGU_def Unifier_def |
|
266 by auto |
|
267 |
|
268 have "\<sigma>' \<doteq> \<sigma> \<lozenge> \<rho>" unfolding \<sigma> |
|
269 by (rule subst_eq_intro, auto simp:subst_eq_dest[OF eqv] subst_eq_dest[OF eqv2]) |
|
270 thus "\<exists>\<gamma>. \<sigma>' \<doteq> \<sigma> \<lozenge> \<gamma>" .. |
|
271 qed |
|
272 qed (auto split:split_if_asm) -- "Solve the remaining cases automatically" |
|
273 |
|
274 |
|
275 subsection {* Properties used in termination proof *} |
221 subsection {* Properties used in termination proof *} |
276 |
222 |
277 |
223 |
278 text {* Elimination of variables by a substitution: *} |
224 text {* Elimination of variables by a substitution: *} |
279 |
225 |
284 by (auto simp:elim_def) |
230 by (auto simp:elim_def) |
285 |
231 |
286 lemma elim_dest[dest]: "elim \<sigma> v \<Longrightarrow> v \<notin> vars_of (t \<lhd> \<sigma>)" |
232 lemma elim_dest[dest]: "elim \<sigma> v \<Longrightarrow> v \<notin> vars_of (t \<lhd> \<sigma>)" |
287 by (auto simp:elim_def) |
233 by (auto simp:elim_def) |
288 |
234 |
289 lemma elim_eqv: "\<sigma> \<doteq> \<theta> \<Longrightarrow> elim \<sigma> x = elim \<theta> x" |
235 lemma elim_eq: "\<sigma> \<doteq> \<theta> \<Longrightarrow> elim \<sigma> x = elim \<theta> x" |
290 by (auto simp:elim_def subst_eq_def) |
236 by (auto simp:elim_def subst_eq_def) |
291 |
|
292 |
|
293 text {* Replacing a variable by itself yields an identity subtitution: *} |
|
294 |
|
295 lemma var_self[intro]: "[(v, Var v)] \<doteq> []" |
|
296 proof |
|
297 fix t show "t \<lhd> [(v, Var v)] = t \<lhd> []" |
|
298 by (induct t) simp_all |
|
299 qed |
|
300 |
|
301 lemma var_same: "([(v, t)] \<doteq> []) = (t = Var v)" |
|
302 proof |
|
303 assume t_v: "t = Var v" |
|
304 thus "[(v, t)] \<doteq> []" |
|
305 by auto |
|
306 next |
|
307 assume id: "[(v, t)] \<doteq> []" |
|
308 show "t = Var v" |
|
309 proof - |
|
310 have "t = Var v \<lhd> [(v, t)]" by simp |
|
311 also from id have "\<dots> = Var v \<lhd> []" .. |
|
312 finally show ?thesis by simp |
|
313 qed |
|
314 qed |
|
315 |
|
316 text {* A lemma about occs and elim *} |
|
317 |
|
318 lemma remove_var: |
|
319 assumes [simp]: "v \<notin> vars_of s" |
|
320 shows "v \<notin> vars_of (t \<lhd> [(v, s)])" |
|
321 by (induct t) simp_all |
|
322 |
237 |
323 lemma occs_elim: "\<not> Var v \<prec> t |
238 lemma occs_elim: "\<not> Var v \<prec> t |
324 \<Longrightarrow> elim [(v,t)] v \<or> [(v,t)] \<doteq> []" |
239 \<Longrightarrow> elim [(v,t)] v \<or> [(v,t)] \<doteq> []" |
325 proof (induct t) |
240 by (metis elim_intro remove_var var_same vars_iff_occseq) |
326 case (Var x) |
|
327 show ?case |
|
328 proof cases |
|
329 assume "v = x" |
|
330 thus ?thesis |
|
331 by (simp add:var_same) |
|
332 next |
|
333 assume neq: "v \<noteq> x" |
|
334 have "elim [(v, Var x)] v" |
|
335 by (auto intro!:remove_var simp:neq) |
|
336 thus ?thesis .. |
|
337 qed |
|
338 next |
|
339 case (Const c) |
|
340 have "elim [(v, Const c)] v" |
|
341 by (auto intro!:remove_var) |
|
342 thus ?case .. |
|
343 next |
|
344 case (Comb M N) |
|
345 |
|
346 hence ih1: "elim [(v, M)] v \<or> [(v, M)] \<doteq> []" |
|
347 and ih2: "elim [(v, N)] v \<or> [(v, N)] \<doteq> []" |
|
348 and nonoccs: "Var v \<noteq> M" "Var v \<noteq> N" |
|
349 by auto |
|
350 |
|
351 from nonoccs have "\<not> [(v,M)] \<doteq> []" |
|
352 by (simp add:var_same) |
|
353 with ih1 have "elim [(v, M)] v" by blast |
|
354 hence "v \<notin> vars_of (Var v \<lhd> [(v,M)])" .. |
|
355 hence not_in_M: "v \<notin> vars_of M" by simp |
|
356 |
|
357 from nonoccs have "\<not> [(v,N)] \<doteq> []" |
|
358 by (simp add:var_same) |
|
359 with ih2 have "elim [(v, N)] v" by blast |
|
360 hence "v \<notin> vars_of (Var v \<lhd> [(v,N)])" .. |
|
361 hence not_in_N: "v \<notin> vars_of N" by simp |
|
362 |
|
363 have "elim [(v, M \<cdot> N)] v" |
|
364 proof |
|
365 fix t |
|
366 show "v \<notin> vars_of (t \<lhd> [(v, M \<cdot> N)])" |
|
367 proof (induct t) |
|
368 case (Var x) thus ?case by (simp add: not_in_M not_in_N) |
|
369 qed auto |
|
370 qed |
|
371 thus ?case .. |
|
372 qed |
|
373 |
241 |
374 text {* The result of a unification never introduces new variables: *} |
242 text {* The result of a unification never introduces new variables: *} |
|
243 |
|
244 declare unify.psimps[simp] |
375 |
245 |
376 lemma unify_vars: |
246 lemma unify_vars: |
377 assumes "unify_dom (M, N)" |
247 assumes "unify_dom (M, N)" |
378 assumes "unify M N = Some \<sigma>" |
248 assumes "unify M N = Some \<sigma>" |
379 shows "vars_of (t \<lhd> \<sigma>) \<subseteq> vars_of M \<union> vars_of N \<union> vars_of t" |
249 shows "vars_of (t \<lhd> \<sigma>) \<subseteq> vars_of M \<union> vars_of N \<union> vars_of t" |
501 finally have "\<sigma> \<doteq> \<theta>1" . |
371 finally have "\<sigma> \<doteq> \<theta>1" . |
502 |
372 |
503 from ih1 show ?thesis |
373 from ih1 show ?thesis |
504 proof |
374 proof |
505 assume "\<exists>v\<in>vars_of M \<union> vars_of M'. elim \<theta>1 v" |
375 assume "\<exists>v\<in>vars_of M \<union> vars_of M'. elim \<theta>1 v" |
506 with elim_eqv[OF `\<sigma> \<doteq> \<theta>1`] |
376 with elim_eq[OF `\<sigma> \<doteq> \<theta>1`] |
507 show ?thesis by auto |
377 show ?thesis by auto |
508 next |
378 next |
509 note `\<sigma> \<doteq> \<theta>1` |
379 note `\<sigma> \<doteq> \<theta>1` |
510 also assume "\<theta>1 \<doteq> []" |
380 also assume "\<theta>1 \<doteq> []" |
511 finally show ?thesis .. |
381 finally show ?thesis .. |
512 qed |
382 qed |
513 qed |
383 qed |
514 qed |
384 qed |
515 |
385 |
|
386 declare unify.psimps[simp del] |
516 |
387 |
517 subsection {* Termination proof *} |
388 subsection {* Termination proof *} |
518 |
389 |
519 termination unify |
390 termination unify |
520 proof |
391 proof |
521 let ?R = "measures [\<lambda>(M,N). card (vars_of M \<union> vars_of N), |
392 let ?R = "measures [\<lambda>(M,N). card (vars_of M \<union> vars_of N), |
522 \<lambda>(M, N). size M]" |
393 \<lambda>(M, N). size M]" |
523 show "wf ?R" by simp |
394 show "wf ?R" by simp |
524 |
395 |
525 fix M N M' N' |
396 fix M N M' N' :: "'a trm" |
526 show "((M, M'), (M \<cdot> N, M' \<cdot> N')) \<in> ?R" -- "Inner call" |
397 show "((M, M'), (M \<cdot> N, M' \<cdot> N')) \<in> ?R" -- "Inner call" |
527 by (rule measures_lesseq) (auto intro: card_mono) |
398 by (rule measures_lesseq) (auto intro: card_mono) |
528 |
399 |
529 fix \<theta> -- "Outer call" |
400 fix \<theta> -- "Outer call" |
530 assume inner: "unify_dom (M, M')" |
401 assume inner: "unify_dom (M, M')" |
553 thus ?thesis |
424 thus ?thesis |
554 by (auto intro!: measures_less intro: psubset_card_mono) |
425 by (auto intro!: measures_less intro: psubset_card_mono) |
555 qed |
426 qed |
556 qed |
427 qed |
557 |
428 |
558 declare unify.psimps[simp del] |
429 |
|
430 subsection {* Other Properties *} |
|
431 |
|
432 lemma unify_computes_MGU: |
|
433 "unify M N = Some \<sigma> \<Longrightarrow> MGU \<sigma> M N" |
|
434 using assms |
|
435 proof (induct M N arbitrary: \<sigma> rule: unify.induct) |
|
436 case (7 M N M' N' \<sigma>) -- "The interesting case" |
|
437 |
|
438 then obtain \<theta>1 \<theta>2 |
|
439 where "unify M M' = Some \<theta>1" |
|
440 and "unify (N \<lhd> \<theta>1) (N' \<lhd> \<theta>1) = Some \<theta>2" |
|
441 and \<sigma>: "\<sigma> = \<theta>1 \<lozenge> \<theta>2" |
|
442 and MGU_inner: "MGU \<theta>1 M M'" |
|
443 and MGU_outer: "MGU \<theta>2 (N \<lhd> \<theta>1) (N' \<lhd> \<theta>1)" |
|
444 by (auto split:option.split_asm) |
|
445 |
|
446 show ?case |
|
447 proof |
|
448 from MGU_inner and MGU_outer |
|
449 have "M \<lhd> \<theta>1 = M' \<lhd> \<theta>1" |
|
450 and "N \<lhd> \<theta>1 \<lhd> \<theta>2 = N' \<lhd> \<theta>1 \<lhd> \<theta>2" |
|
451 unfolding MGU_def Unifier_def |
|
452 by auto |
|
453 thus "M \<cdot> N \<lhd> \<sigma> = M' \<cdot> N' \<lhd> \<sigma>" unfolding \<sigma> |
|
454 by simp |
|
455 next |
|
456 fix \<sigma>' assume "M \<cdot> N \<lhd> \<sigma>' = M' \<cdot> N' \<lhd> \<sigma>'" |
|
457 hence "M \<lhd> \<sigma>' = M' \<lhd> \<sigma>'" |
|
458 and Ns: "N \<lhd> \<sigma>' = N' \<lhd> \<sigma>'" by auto |
|
459 |
|
460 with MGU_inner obtain \<delta> |
|
461 where eqv: "\<sigma>' \<doteq> \<theta>1 \<lozenge> \<delta>" |
|
462 unfolding MGU_def Unifier_def |
|
463 by auto |
|
464 |
|
465 from Ns have "N \<lhd> \<theta>1 \<lhd> \<delta> = N' \<lhd> \<theta>1 \<lhd> \<delta>" |
|
466 by (simp add:subst_eq_dest[OF eqv]) |
|
467 |
|
468 with MGU_outer obtain \<rho> |
|
469 where eqv2: "\<delta> \<doteq> \<theta>2 \<lozenge> \<rho>" |
|
470 unfolding MGU_def Unifier_def |
|
471 by auto |
|
472 |
|
473 have "\<sigma>' \<doteq> \<sigma> \<lozenge> \<rho>" unfolding \<sigma> |
|
474 by (rule subst_eq_intro, auto simp:subst_eq_dest[OF eqv] subst_eq_dest[OF eqv2]) |
|
475 thus "\<exists>\<gamma>. \<sigma>' \<doteq> \<sigma> \<lozenge> \<gamma>" .. |
|
476 qed |
|
477 qed (auto simp: MGU_Const intro: MGU_Var MGU_Var[symmetric] split: split_if_asm) |
|
478 |
|
479 |
559 |
480 |
560 end |
481 end |