
krauss@22999: (*  ID:         $Id$
krauss@22999:     Author:     Alexander Krauss, Technische Universitaet Muenchen
krauss@22999: *)
krauss@22999: 
krauss@22999: header {* Case study: Unification Algorithm *}
krauss@22999: 
krauss@22999: (*<*)theory Unification
krauss@22999: imports Main
krauss@22999: begin(*>*)
krauss@22999: 
krauss@22999: text {* 
krauss@22999:   This is a formalization of a first-order unification
krauss@22999:   algorithm. It uses the new "function" package to define recursive
krauss@22999:   functions, which allows a better treatment of nested recursion. 
krauss@22999: 
krauss@22999:   This is basically a modernized version of a previous formalization
krauss@22999:   by Konrad Slind (see: HOL/Subst/Unify.thy), which itself builds on
krauss@22999:   previous work by Paulson and Manna & Waldinger (for details, see
krauss@22999:   there).
krauss@22999: 
krauss@22999:   Unlike that formalization, where the proofs of termination and
krauss@22999:   some partial correctness properties are intertwined, we can prove
krauss@22999:   partial correctness and termination separately.
krauss@22999: *}
krauss@22999: 
krauss@22999: subsection {* Basic definitions *}
krauss@22999: 
krauss@22999: datatype 'a trm = 
krauss@22999:   Var 'a 
krauss@22999:   | Const 'a
krauss@22999:   | App "'a trm" "'a trm" (infix "\<cdot>" 60)
krauss@22999: 
krauss@22999: types
krauss@22999:   'a subst = "('a \<times> 'a trm) list"
krauss@22999: 
krauss@22999: text {* Applying a substitution to a variable: *}
krauss@22999: 
krauss@22999: fun assoc :: "'a \<Rightarrow> 'b \<Rightarrow> ('a \<times> 'b) list \<Rightarrow> 'b"
krauss@22999: where
krauss@22999:   "assoc x d [] = d"
krauss@22999: | "assoc x d ((p,q)#t) = (if x = p then q else assoc x d t)"
krauss@22999: 
krauss@22999: text {* Applying a substitution to a term: *}
krauss@22999: 
krauss@22999: fun apply_subst :: "'a trm \<Rightarrow> 'a subst \<Rightarrow> 'a trm" (infixl "\<triangleleft>" 60)
krauss@22999: where
krauss@22999:   "(Var v) \<triangleleft> s = assoc v (Var v) s"
krauss@22999: | "(Const c) \<triangleleft> s = (Const c)"
krauss@22999: | "(M \<cdot> N) \<triangleleft> s = (M \<triangleleft> s) \<cdot> (N \<triangleleft> s)"
krauss@22999: 
krauss@22999: text {* Composition of substitutions: *}
krauss@22999: 
krauss@22999: fun
krauss@22999:   "compose" :: "'a subst \<Rightarrow> 'a subst \<Rightarrow> 'a subst" (infixl "\<bullet>" 80)
krauss@22999: where
krauss@22999:   "[] \<bullet> bl = bl"
krauss@22999: | "((a,b) # al) \<bullet> bl = (a, b \<triangleleft> bl) # (al \<bullet> bl)"
krauss@22999: 
krauss@22999: text {* Equivalence of substitutions: *}
krauss@22999: 
krauss@22999: definition eqv (infix "=\<^sub>s" 50)
krauss@22999: where
krauss@22999:   "s1 =\<^sub>s s2 \<equiv> \<forall>t. t \<triangleleft> s1 = t \<triangleleft> s2" 
krauss@22999: 
krauss@22999: subsection {* Basic lemmas *}
krauss@22999: 
krauss@22999: lemma apply_empty[simp]: "t \<triangleleft> [] = t"
krauss@22999: by (induct t) auto
krauss@22999: 
krauss@22999: lemma compose_empty[simp]: "\<sigma> \<bullet> [] = \<sigma>"
krauss@22999: by (induct \<sigma>) auto
krauss@22999: 
krauss@22999: lemma apply_compose[simp]: "t \<triangleleft> (s1 \<bullet> s2) = t \<triangleleft> s1 \<triangleleft> s2"
krauss@22999: proof (induct t)
krauss@22999:   case App thus ?case by simp
krauss@22999: next 
krauss@22999:   case Const thus ?case by simp
krauss@22999: next
krauss@22999:   case (Var v) thus ?case
krauss@22999:   proof (induct s1)
krauss@22999:     case Nil show ?case by simp
krauss@22999:   next
krauss@22999:     case (Cons p s1s) thus ?case by (cases p, simp)
krauss@22999:   qed
krauss@22999: qed
krauss@22999: 
krauss@22999: lemma eqv_refl[intro]: "s =\<^sub>s s"
krauss@22999:   by (auto simp:eqv_def)
krauss@22999: 
krauss@22999: lemma eqv_trans[trans]: "\<lbrakk>s1 =\<^sub>s s2; s2 =\<^sub>s s3\<rbrakk> \<Longrightarrow> s1 =\<^sub>s s3"
krauss@22999:   by (auto simp:eqv_def)
krauss@22999: 
krauss@22999: lemma eqv_sym[sym]: "\<lbrakk>s1 =\<^sub>s s2\<rbrakk> \<Longrightarrow> s2 =\<^sub>s s1"
krauss@22999:   by (auto simp:eqv_def)
krauss@22999: 
krauss@22999: lemma eqv_intro[intro]: "(\<And>t. t \<triangleleft> \<sigma> = t \<triangleleft> \<theta>) \<Longrightarrow> \<sigma> =\<^sub>s \<theta>"
krauss@22999:   by (auto simp:eqv_def)
krauss@22999: 
krauss@22999: lemma eqv_dest[dest]: "s1 =\<^sub>s s2 \<Longrightarrow> t \<triangleleft> s1 = t \<triangleleft> s2"
krauss@22999:   by (auto simp:eqv_def)
krauss@22999: 
krauss@22999: lemma compose_eqv: "\<lbrakk>\<sigma> =\<^sub>s \<sigma>'; \<theta> =\<^sub>s \<theta>'\<rbrakk> \<Longrightarrow> (\<sigma> \<bullet> \<theta>) =\<^sub>s (\<sigma>' \<bullet> \<theta>')"
krauss@22999:   by (auto simp:eqv_def)
krauss@22999: 
krauss@22999: lemma compose_assoc: "(a \<bullet> b) \<bullet> c =\<^sub>s a \<bullet> (b \<bullet> c)"
krauss@22999:   by auto
krauss@22999: 
krauss@22999: subsection {* Specification: Most general unifiers *}
krauss@22999: 
krauss@22999: definition
krauss@22999:   "Unifier \<sigma> t u \<equiv> (t\<triangleleft>\<sigma> = u\<triangleleft>\<sigma>)"
krauss@22999: 
krauss@22999: definition
krauss@22999:   "MGU \<sigma> t u \<equiv> Unifier \<sigma> t u \<and> (\<forall>\<theta>. Unifier \<theta> t u 
krauss@22999:   \<longrightarrow> (\<exists>\<gamma>. \<theta> =\<^sub>s \<sigma> \<bullet> \<gamma>))"
krauss@22999: 
krauss@22999: lemma MGUI[intro]:
krauss@22999:   "\<lbrakk>t \<triangleleft> \<sigma> = u \<triangleleft> \<sigma>; \<And>\<theta>. t \<triangleleft> \<theta> = u \<triangleleft> \<theta> \<Longrightarrow> \<exists>\<gamma>. \<theta> =\<^sub>s \<sigma> \<bullet> \<gamma>\<rbrakk>
krauss@22999:   \<Longrightarrow> MGU \<sigma> t u"
krauss@22999:   by (simp only:Unifier_def MGU_def, auto)
krauss@22999: 
krauss@22999: lemma MGU_sym[sym]:
krauss@22999:   "MGU \<sigma> s t \<Longrightarrow> MGU \<sigma> t s"
krauss@22999:   by (auto simp:MGU_def Unifier_def)
krauss@22999: 
krauss@22999: 
krauss@22999: subsection {* The unification algorithm *}
krauss@22999: 
krauss@22999: text {* Occurs check: Proper subterm relation *}
krauss@22999: 
krauss@22999: fun occ :: "'a trm \<Rightarrow> 'a trm \<Rightarrow> bool"
krauss@22999: where
krauss@22999:   "occ u (Var v) = False"
krauss@22999: | "occ u (Const c) = False"
krauss@22999: | "occ u (M \<cdot> N) = (u = M \<or> u = N \<or> occ u M \<or> occ u N)"
krauss@22999: 
krauss@22999: text {* The unification algorithm: *}
krauss@22999: 
krauss@22999: function unify :: "'a trm \<Rightarrow> 'a trm \<Rightarrow> 'a subst option"
krauss@22999: where
krauss@22999:   "unify (Const c) (M \<cdot> N)   = None"
krauss@22999: | "unify (M \<cdot> N)   (Const c) = None"
krauss@22999: | "unify (Const c) (Var v)   = Some [(v, Const c)]"
krauss@22999: | "unify (M \<cdot> N)   (Var v)   = (if (occ (Var v) (M \<cdot> N)) 
krauss@22999:                                         then None
krauss@22999:                                         else Some [(v, M \<cdot> N)])"
krauss@22999: | "unify (Var v)   M         = (if (occ (Var v) M) 
krauss@22999:                                         then None
krauss@22999:                                         else Some [(v, M)])"
krauss@22999: | "unify (Const c) (Const d) = (if c=d then Some [] else None)"
krauss@22999: | "unify (M \<cdot> N) (M' \<cdot> N') = (case unify M M' of
krauss@22999:                                     None \<Rightarrow> None |
krauss@22999:                                     Some \<theta> \<Rightarrow> (case unify (N \<triangleleft> \<theta>) (N' \<triangleleft> \<theta>)
krauss@22999:                                       of None \<Rightarrow> None |
krauss@22999:                                          Some \<sigma> \<Rightarrow> Some (\<theta> \<bullet> \<sigma>)))"
krauss@22999:   by pat_completeness auto
krauss@22999: 
krauss@22999: 
krauss@22999: subsection {* Partial correctness *}
krauss@22999: 
krauss@22999: text {* Some lemmas about occ and MGU: *}
krauss@22999: 
krauss@22999: lemma subst_no_occ: "\<not>occ (Var v) t \<Longrightarrow> Var v \<noteq> t
krauss@22999:   \<Longrightarrow> t \<triangleleft> [(v,s)] = t"
krauss@22999: by (induct t) auto
krauss@22999: 
krauss@22999: lemma MGU_Var[intro]: 
krauss@22999:   assumes no_occ: "\<not>occ (Var v) t"
krauss@22999:   shows "MGU [(v,t)] (Var v) t"
krauss@22999: proof (intro MGUI exI)
krauss@22999:   show "Var v \<triangleleft> [(v,t)] = t \<triangleleft> [(v,t)]" using no_occ
krauss@22999:     by (cases "Var v = t", auto simp:subst_no_occ)
krauss@22999: next
krauss@22999:   fix \<theta> assume th: "Var v \<triangleleft> \<theta> = t \<triangleleft> \<theta>" 
krauss@22999:   show "\<theta> =\<^sub>s [(v,t)] \<bullet> \<theta>" 
krauss@22999:   proof
krauss@22999:     fix s show "s \<triangleleft> \<theta> = s \<triangleleft> [(v,t)] \<bullet> \<theta>" using th 
krauss@22999:       by (induct s, auto)
krauss@22999:   qed
krauss@22999: qed
krauss@22999: 
krauss@22999: declare MGU_Var[symmetric, intro]
krauss@22999: 
krauss@22999: lemma MGU_Const[simp]: "MGU [] (Const c) (Const d) = (c = d)"
krauss@22999:   unfolding MGU_def Unifier_def
krauss@22999:   by auto
krauss@22999:   
krauss@22999: text {* If unification terminates, then it computes most general unifiers: *}
krauss@22999: 
krauss@22999: lemma unify_partial_correctness:
krauss@22999:   assumes "unify_dom (M, N)"
krauss@22999:   assumes "unify M N = Some \<sigma>"
krauss@22999:   shows "MGU \<sigma> M N"
krauss@22999: using prems
krauss@22999: proof (induct M N arbitrary: \<sigma>)
krauss@22999:   case (7 M N M' N' \<sigma>) -- "The interesting case"
krauss@22999: 
krauss@22999:   then obtain \<theta>1 \<theta>2 
krauss@22999:     where "unify M M' = Some \<theta>1"
krauss@22999:     and "unify (N \<triangleleft> \<theta>1) (N' \<triangleleft> \<theta>1) = Some \<theta>2"
krauss@22999:     and \<sigma>: "\<sigma> = \<theta>1 \<bullet> \<theta>2"
krauss@22999:     and MGU_inner: "MGU \<theta>1 M M'" 
krauss@22999:     and MGU_outer: "MGU \<theta>2 (N \<triangleleft> \<theta>1) (N' \<triangleleft> \<theta>1)"
krauss@22999:     by (auto split:option.split_asm)
krauss@22999: 
krauss@22999:   show ?case
krauss@22999:   proof
krauss@22999:     from MGU_inner and MGU_outer
krauss@22999:     have "M \<triangleleft> \<theta>1 = M' \<triangleleft> \<theta>1" 
krauss@22999:       and "N \<triangleleft> \<theta>1 \<triangleleft> \<theta>2 = N' \<triangleleft> \<theta>1 \<triangleleft> \<theta>2"
krauss@22999:       unfolding MGU_def Unifier_def
krauss@22999:       by auto
krauss@22999:     thus "M \<cdot> N \<triangleleft> \<sigma> = M' \<cdot> N' \<triangleleft> \<sigma>" unfolding \<sigma>
krauss@22999:       by simp
krauss@22999:   next
krauss@22999:     fix \<sigma>' assume "M \<cdot> N \<triangleleft> \<sigma>' = M' \<cdot> N' \<triangleleft> \<sigma>'"
krauss@22999:     hence "M \<triangleleft> \<sigma>' = M' \<triangleleft> \<sigma>'"
krauss@22999:       and Ns: "N \<triangleleft> \<sigma>' = N' \<triangleleft> \<sigma>'" by auto
krauss@22999: 
krauss@22999:     with MGU_inner obtain \<delta>
krauss@22999:       where eqv: "\<sigma>' =\<^sub>s \<theta>1 \<bullet> \<delta>"
krauss@22999:       unfolding MGU_def Unifier_def
krauss@22999:       by auto
krauss@22999: 
krauss@22999:     from Ns have "N \<triangleleft> \<theta>1 \<triangleleft> \<delta> = N' \<triangleleft> \<theta>1 \<triangleleft> \<delta>"
krauss@22999:       by (simp add:eqv_dest[OF eqv])
krauss@22999: 
krauss@22999:     with MGU_outer obtain \<rho>
krauss@22999:       where eqv2: "\<delta> =\<^sub>s \<theta>2 \<bullet> \<rho>"
krauss@22999:       unfolding MGU_def Unifier_def
krauss@22999:       by auto
krauss@22999:     
krauss@22999:     have "\<sigma>' =\<^sub>s \<sigma> \<bullet> \<rho>" unfolding \<sigma>
krauss@22999:       by (rule eqv_intro, auto simp:eqv_dest[OF eqv] 
krauss@22999: 	eqv_dest[OF eqv2])
krauss@22999:     thus "\<exists>\<gamma>. \<sigma>' =\<^sub>s \<sigma> \<bullet> \<gamma>" ..
krauss@22999:   qed
krauss@22999: qed (auto split:split_if_asm) -- "Solve the remaining cases automatically"
krauss@22999: 
krauss@22999: 
krauss@22999: subsection {* Properties used in termination proof *}
krauss@22999: 
krauss@22999: text {* The variables of a term: *}
krauss@22999: 
krauss@22999: fun vars_of:: "'a trm \<Rightarrow> 'a set"
krauss@22999: where
krauss@22999:   "vars_of (Var v) = { v }"
krauss@22999: | "vars_of (Const c) = {}"
krauss@22999: | "vars_of (M \<cdot> N) = vars_of M \<union> vars_of N"
krauss@22999: 
krauss@22999: lemma vars_of_finite[intro]: "finite (vars_of t)"
krauss@22999:   by (induct t) simp_all
krauss@22999: 
krauss@22999: text {* Elimination of variables by a substitution: *}
krauss@22999: 
krauss@22999: definition
krauss@22999:   "elim \<sigma> v \<equiv> \<forall>t. v \<notin> vars_of (t \<triangleleft> \<sigma>)"
krauss@22999: 
krauss@22999: lemma elim_intro[intro]: "(\<And>t. v \<notin> vars_of (t \<triangleleft> \<sigma>)) \<Longrightarrow> elim \<sigma> v"
krauss@22999:   by (auto simp:elim_def)
krauss@22999: 
krauss@22999: lemma elim_dest[dest]: "elim \<sigma> v \<Longrightarrow> v \<notin> vars_of (t \<triangleleft> \<sigma>)"
krauss@22999:   by (auto simp:elim_def)
krauss@22999: 
krauss@22999: lemma elim_eqv: "\<sigma> =\<^sub>s \<theta> \<Longrightarrow> elim \<sigma> x = elim \<theta> x"
krauss@22999:   by (auto simp:elim_def eqv_def)
krauss@22999: 
krauss@22999: 
krauss@22999: text {* Replacing a variable by itself yields an identity subtitution: *}
krauss@22999: 
krauss@22999: lemma var_self[intro]: "[(v, Var v)] =\<^sub>s []"
krauss@22999: proof
krauss@22999:   fix t show "t \<triangleleft> [(v, Var v)] = t \<triangleleft> []"
krauss@22999:     by (induct t) simp_all
krauss@22999: qed
krauss@22999: 
krauss@22999: lemma var_same: "(t = Var v) = ([(v, t)] =\<^sub>s [])"
krauss@22999: proof
krauss@22999:   assume t_v: "t = Var v"
krauss@22999:   thus "[(v, t)] =\<^sub>s []"
krauss@22999:     by auto
krauss@22999: next
krauss@22999:   assume id: "[(v, t)] =\<^sub>s []"
krauss@22999:   show "t = Var v"
krauss@22999:   proof -
krauss@22999:     have "t = Var v \<triangleleft> [(v, t)]" by simp
krauss@22999:     also from id have "\<dots> = Var v \<triangleleft> []" ..
krauss@22999:     finally show ?thesis by simp
krauss@22999:   qed
krauss@22999: qed
krauss@22999: 
krauss@22999: text {* A lemma about occ and elim *}
krauss@22999: 
krauss@22999: lemma remove_var:
krauss@22999:   assumes [simp]: "v \<notin> vars_of s"
krauss@22999:   shows "v \<notin> vars_of (t \<triangleleft> [(v, s)])"
krauss@22999:   by (induct t) simp_all
krauss@22999: 
krauss@22999: lemma occ_elim: "\<not>occ (Var v) t 
krauss@22999:   \<Longrightarrow> elim [(v,t)] v \<or> [(v,t)] =\<^sub>s []"
krauss@22999: proof (induct t)
krauss@22999:   case (Var x)
krauss@22999:   show ?case
krauss@22999:   proof cases
krauss@22999:     assume "v = x"
krauss@22999:     thus ?thesis
krauss@22999:       by (simp add:var_same[symmetric])
krauss@22999:   next
krauss@22999:     assume neq: "v \<noteq> x"
krauss@22999:     have "elim [(v, Var x)] v"
krauss@22999:       by (auto intro!:remove_var simp:neq)
krauss@22999:     thus ?thesis ..
krauss@22999:   qed
krauss@22999: next
krauss@22999:   case (Const c)
krauss@22999:   have "elim [(v, Const c)] v"
krauss@22999:     by (auto intro!:remove_var)
krauss@22999:   thus ?case ..
krauss@22999: next
krauss@22999:   case (App M N)
krauss@22999:   
krauss@22999:   hence ih1: "elim [(v, M)] v \<or> [(v, M)] =\<^sub>s []"
krauss@22999:     and ih2: "elim [(v, N)] v \<or> [(v, N)] =\<^sub>s []"
krauss@22999:     and nonocc: "Var v \<noteq> M" "Var v \<noteq> N"
krauss@22999:     by auto
krauss@22999: 
krauss@22999:   from nonocc have "\<not> [(v,M)] =\<^sub>s []"
krauss@22999:     by (simp add:var_same[symmetric])
krauss@22999:   with ih1 have "elim [(v, M)] v" by blast
krauss@22999:   hence "v \<notin> vars_of (Var v \<triangleleft> [(v,M)])" ..
krauss@22999:   hence not_in_M: "v \<notin> vars_of M" by simp
krauss@22999: 
krauss@22999:   from nonocc have "\<not> [(v,N)] =\<^sub>s []"
krauss@22999:     by (simp add:var_same[symmetric])
krauss@22999:   with ih2 have "elim [(v, N)] v" by blast
krauss@22999:   hence "v \<notin> vars_of (Var v \<triangleleft> [(v,N)])" ..
krauss@22999:   hence not_in_N: "v \<notin> vars_of N" by simp
krauss@22999: 
krauss@22999:   have "elim [(v, M \<cdot> N)] v"
krauss@22999:   proof 
krauss@22999:     fix t 
krauss@22999:     show "v \<notin> vars_of (t \<triangleleft> [(v, M \<cdot> N)])"
krauss@22999:     proof (induct t)
krauss@22999:       case (Var x) thus ?case by (simp add: not_in_M not_in_N)
krauss@22999:     qed auto
krauss@22999:   qed
krauss@22999:   thus ?case ..
krauss@22999: qed
krauss@22999: 
krauss@22999: text {* The result of a unification never introduces new variables: *}
krauss@22999: 
krauss@22999: lemma unify_vars: 
krauss@22999:   assumes "unify_dom (M, N)"
krauss@22999:   assumes "unify M N = Some \<sigma>"
krauss@22999:   shows "vars_of (t \<triangleleft> \<sigma>) \<subseteq> vars_of M \<union> vars_of N \<union> vars_of t"
krauss@22999:   (is "?P M N \<sigma> t")
krauss@22999: using prems
krauss@22999: proof (induct M N arbitrary:\<sigma> t)
krauss@22999:   case (3 c v) 
krauss@22999:   hence "\<sigma> = [(v, Const c)]" by simp
krauss@22999:   thus ?case by (induct t, auto)
krauss@22999: next
krauss@22999:   case (4 M N v) 
krauss@22999:   hence "\<not>occ (Var v) (M\<cdot>N)" by (cases "occ (Var v) (M\<cdot>N)", auto)
krauss@22999:   with prems have "\<sigma> = [(v, M\<cdot>N)]" by simp
krauss@22999:   thus ?case by (induct t, auto)
krauss@22999: next
krauss@22999:   case (5 v M)
krauss@22999:   hence "\<not>occ (Var v) M" by (cases "occ (Var v) M", auto)
krauss@22999:   with prems have "\<sigma> = [(v, M)]" by simp
krauss@22999:   thus ?case by (induct t, auto)
krauss@22999: next
krauss@22999:   case (7 M N M' N' \<sigma>)
krauss@22999:   then obtain \<theta>1 \<theta>2 
krauss@22999:     where "unify M M' = Some \<theta>1"
krauss@22999:     and "unify (N \<triangleleft> \<theta>1) (N' \<triangleleft> \<theta>1) = Some \<theta>2"
krauss@22999:     and \<sigma>: "\<sigma> = \<theta>1 \<bullet> \<theta>2"
krauss@22999:     and ih1: "\<And>t. ?P M M' \<theta>1 t"
krauss@22999:     and ih2: "\<And>t. ?P (N\<triangleleft>\<theta>1) (N'\<triangleleft>\<theta>1) \<theta>2 t"
krauss@22999:     by (auto split:option.split_asm)
krauss@22999: 
krauss@22999:   show ?case
krauss@22999:   proof
krauss@22999:     fix v assume a: "v \<in> vars_of (t \<triangleleft> \<sigma>)"
krauss@22999:     
krauss@22999:     show "v \<in> vars_of (M \<cdot> N) \<union> vars_of (M' \<cdot> N') \<union> vars_of t"
krauss@22999:     proof (cases "v \<notin> vars_of M \<and> v \<notin> vars_of M'
krauss@22999: 	    \<and> v \<notin> vars_of N \<and> v \<notin> vars_of N'")
krauss@22999:       case True
krauss@22999:       with ih1 have l:"\<And>t. v \<in> vars_of (t \<triangleleft> \<theta>1) \<Longrightarrow> v \<in> vars_of t"
krauss@22999: 	    by auto
krauss@22999:       
krauss@22999:       from a and ih2[where t="t \<triangleleft> \<theta>1"]
krauss@22999:       have "v \<in> vars_of (N \<triangleleft> \<theta>1) \<union> vars_of (N' \<triangleleft> \<theta>1) 
krauss@22999:         \<or> v \<in> vars_of (t \<triangleleft> \<theta>1)" unfolding \<sigma>
krauss@22999: 	    by auto
krauss@22999:       hence "v \<in> vars_of t"
krauss@22999:       proof
krauss@22999: 	    assume "v \<in> vars_of (N \<triangleleft> \<theta>1) \<union> vars_of (N' \<triangleleft> \<theta>1)"
krauss@22999: 	    with True show ?thesis by (auto dest:l)
krauss@22999:       next
krauss@22999: 	    assume "v \<in> vars_of (t \<triangleleft> \<theta>1)" 
krauss@22999: 	    thus ?thesis by (rule l)
krauss@22999:       qed
krauss@22999:       
krauss@22999:       thus ?thesis by auto
krauss@22999:     qed auto
krauss@22999:   qed
krauss@22999: qed (auto split: split_if_asm)
krauss@22999: 
krauss@22999: 
krauss@22999: text {* The result of a unification is either the identity
krauss@22999: substitution or it eliminates a variable from one of the terms: *}
krauss@22999: 
krauss@22999: lemma unify_eliminates: 
krauss@22999:   assumes "unify_dom (M, N)"
krauss@22999:   assumes "unify M N = Some \<sigma>"
krauss@22999:   shows "(\<exists>v\<in>vars_of M \<union> vars_of N. elim \<sigma> v) \<or> \<sigma> =\<^sub>s []"
krauss@22999:   (is "?P M N \<sigma>")
krauss@22999: using prems
krauss@22999: proof (induct M N arbitrary:\<sigma>)
krauss@22999:   case 1 thus ?case by simp
krauss@22999: next
krauss@22999:   case 2 thus ?case by simp
krauss@22999: next
krauss@22999:   case (3 c v)
krauss@22999:   have no_occ: "\<not> occ (Var v) (Const c)" by simp
krauss@22999:   with prems have "\<sigma> = [(v, Const c)]" by simp
krauss@22999:   with occ_elim[OF no_occ]
krauss@22999:   show ?case by auto
krauss@22999: next
krauss@22999:   case (4 M N v)
krauss@22999:   hence no_occ: "\<not>occ (Var v) (M\<cdot>N)" by (cases "occ (Var v) (M\<cdot>N)", auto)
krauss@22999:   with prems have "\<sigma> = [(v, M\<cdot>N)]" by simp
krauss@22999:   with occ_elim[OF no_occ]
krauss@22999:   show ?case by auto 
krauss@22999: next
krauss@22999:   case (5 v M) 
krauss@22999:   hence no_occ: "\<not>occ (Var v) M" by (cases "occ (Var v) M", auto)
krauss@22999:   with prems have "\<sigma> = [(v, M)]" by simp
krauss@22999:   with occ_elim[OF no_occ]
krauss@22999:   show ?case by auto 
krauss@22999: next 
krauss@22999:   case (6 c d) thus ?case
krauss@22999:     by (cases "c = d") auto
krauss@22999: next
krauss@22999:   case (7 M N M' N' \<sigma>)
krauss@22999:   then obtain \<theta>1 \<theta>2 
krauss@22999:     where "unify M M' = Some \<theta>1"
krauss@22999:     and "unify (N \<triangleleft> \<theta>1) (N' \<triangleleft> \<theta>1) = Some \<theta>2"
krauss@22999:     and \<sigma>: "\<sigma> = \<theta>1 \<bullet> \<theta>2"
krauss@22999:     and ih1: "?P M M' \<theta>1"
krauss@22999:     and ih2: "?P (N\<triangleleft>\<theta>1) (N'\<triangleleft>\<theta>1) \<theta>2"
krauss@22999:     by (auto split:option.split_asm)
krauss@22999: 
krauss@22999:   from `unify_dom (M \<cdot> N, M' \<cdot> N')`
krauss@22999:   have "unify_dom (M, M')"
krauss@22999:     by (rule acc_downward) (rule unify_rel.intros)
krauss@22999:   hence no_new_vars: 
krauss@22999:     "\<And>t. vars_of (t \<triangleleft> \<theta>1) \<subseteq> vars_of M \<union> vars_of M' \<union> vars_of t"
krauss@22999:     by (rule unify_vars)
krauss@22999: 
krauss@22999:   from ih2 show ?case 
krauss@22999:   proof 
krauss@22999:     assume "\<exists>v\<in>vars_of (N \<triangleleft> \<theta>1) \<union> vars_of (N' \<triangleleft> \<theta>1). elim \<theta>2 v"
krauss@22999:     then obtain v 
krauss@22999:       where "v\<in>vars_of (N \<triangleleft> \<theta>1) \<union> vars_of (N' \<triangleleft> \<theta>1)"
krauss@22999:       and el: "elim \<theta>2 v" by auto
krauss@22999:     with no_new_vars show ?thesis unfolding \<sigma> 
krauss@22999:       by (auto simp:elim_def)
krauss@22999:   next
krauss@22999:     assume empty[simp]: "\<theta>2 =\<^sub>s []"
krauss@22999: 
krauss@22999:     have "\<sigma> =\<^sub>s (\<theta>1 \<bullet> [])" unfolding \<sigma>
krauss@22999:       by (rule compose_eqv) auto
krauss@22999:     also have "\<dots> =\<^sub>s \<theta>1" by auto
krauss@22999:     finally have "\<sigma> =\<^sub>s \<theta>1" .
krauss@22999: 
krauss@22999:     from ih1 show ?thesis
krauss@22999:     proof
krauss@22999:       assume "\<exists>v\<in>vars_of M \<union> vars_of M'. elim \<theta>1 v"
krauss@22999:       with elim_eqv[OF `\<sigma> =\<^sub>s \<theta>1`]
krauss@22999:       show ?thesis by auto
krauss@22999:     next
krauss@22999:       note `\<sigma> =\<^sub>s \<theta>1`
krauss@22999:       also assume "\<theta>1 =\<^sub>s []"
krauss@22999:       finally show ?thesis ..
krauss@22999:     qed
krauss@22999:   qed
krauss@22999: qed
krauss@22999: 
krauss@22999: 
krauss@22999: subsection {* Termination proof *}
krauss@22999: 
krauss@22999: 
krauss@22999: termination unify
krauss@22999: proof 
krauss@22999:   let ?R = "measures [\<lambda>(M,N). card (vars_of M \<union> vars_of N),
krauss@22999:                            \<lambda>(M, N). size M]"
krauss@22999:   show "wf ?R" by simp
krauss@22999: 
krauss@22999:   fix M N M' N' 
krauss@22999:   show "((M, M'), (M \<cdot> N, M' \<cdot> N')) \<in> ?R" -- "Inner call"
krauss@22999:     by (rule measures_lesseq) (auto intro: card_mono)
krauss@22999: 
krauss@22999:   fix \<theta>                                   -- "Outer call"
krauss@22999:   assume inner: "unify_dom (M, M')"
krauss@22999:     "unify M M' = Some \<theta>"
krauss@22999: 
krauss@22999:   from unify_eliminates[OF inner]
krauss@22999:   show "((N \<triangleleft> \<theta>, N' \<triangleleft> \<theta>), (M \<cdot> N, M' \<cdot> N')) \<in>?R"
krauss@22999:   proof
krauss@22999:     -- {* Either a variable is eliminated \ldots *}
krauss@22999:     assume "(\<exists>v\<in>vars_of M \<union> vars_of M'. elim \<theta> v)"
krauss@22999:     then obtain v 
krauss@22999: 	  where "elim \<theta> v" 
krauss@22999: 	  and "v\<in>vars_of M \<union> vars_of M'" by auto
krauss@22999:     with unify_vars[OF inner]
krauss@22999:     have "vars_of (N\<triangleleft>\<theta>) \<union> vars_of (N'\<triangleleft>\<theta>)
krauss@22999: 	  \<subset> vars_of (M\<cdot>N) \<union> vars_of (M'\<cdot>N')"
krauss@22999: 	  by auto
krauss@22999:     
krauss@22999:     thus ?thesis
krauss@22999:       by (auto intro!: measures_less intro: psubset_card_mono)
krauss@22999:   next
krauss@22999:     -- {* Or the substitution is empty *}
krauss@22999:     assume "\<theta> =\<^sub>s []"
krauss@22999:     hence "N \<triangleleft> \<theta> = N" 
krauss@22999: 	  and "N' \<triangleleft> \<theta> = N'" by auto
krauss@22999:     thus ?thesis 
krauss@22999:        by (auto intro!: measures_less intro: psubset_card_mono)
krauss@22999:   qed
krauss@22999: qed
krauss@22999: 
krauss@22999: 
krauss@22999: (*<*)end(*>*)
krauss@22999: 
krauss@22999: 
krauss@22999: 
krauss@22999: 
krauss@22999: 
krauss@22999: 
krauss@22999: 
krauss@22999: 
krauss@22999: 
krauss@22999: 
krauss@22999: 
krauss@22999: 
krauss@22999: 
krauss@22999: 
krauss@22999: 
krauss@22999: 
krauss@22999: 
krauss@22999: 




