# HG changeset patch # User krauss # Date 1313957584 -7200 # Node ID 91e8062605d575b864299309cd2c9e90b1650c6c # Parent 74c08021ab2e741643cec4e07cf69ccc6953187f ported some lemmas from HOL/Subst/*; tuned order diff -r 74c08021ab2e -r 91e8062605d5 src/HOL/ex/Unification.thy --- a/src/HOL/ex/Unification.thy Sun Aug 21 22:13:04 2011 +0200 +++ b/src/HOL/ex/Unification.thy Sun Aug 21 22:13:04 2011 +0200 @@ -24,13 +24,43 @@ *} -subsection {* Basic definitions *} +subsection {* Terms *} + +text {* Binary trees with leaves that are constants or variables. *} datatype 'a trm = Var 'a | Const 'a | Comb "'a trm" "'a trm" (infix "\" 60) +primrec vars_of :: "'a trm \ 'a set" +where + "vars_of (Var v) = {v}" +| "vars_of (Const c) = {}" +| "vars_of (M \ N) = vars_of M \ vars_of N" + +fun occs :: "'a trm \ 'a trm \ bool" (infixl "\" 54) +where + "occs u (Var v) = False" +| "occs u (Const c) = False" +| "occs u (M \ N) = (u = M \ u = N \ occs u M \ occs u N)" + + +lemma finite_vars_of[intro]: "finite (vars_of t)" + by (induct t) simp_all + +lemma vars_var_iff: "v \ vars_of (Var w) \ w = v" + by auto + +lemma vars_iff_occseq: "x \ vars_of t \ Var x \ t \ Var x = t" + by (induct t) auto + +lemma occs_vars_subset: "M \ N \ vars_of M \ vars_of N" + by (induct N) auto + + +subsection {* Substitutions *} + type_synonym 'a subst = "('a \ 'a trm) list" text {* Applying a substitution to a variable: *} @@ -48,73 +78,77 @@ | "(Const c) \ s = (Const c)" | "(M \ N) \ s = (M \ s) \ (N \ s)" -text {* Composition of substitutions: *} +definition subst_eq (infixr "\" 52) +where + "s1 \ s2 \ (\t. t \ s1 = t \ s2)" -fun - comp :: "'a subst \ 'a subst \ 'a subst" (infixl "\" 56) +fun comp :: "'a subst \ 'a subst \ 'a subst" (infixl "\" 56) where "[] \ bl = bl" | "((a,b) # al) \ bl = (a, b \ bl) # (al \ bl)" -text {* Equivalence of substitutions: *} + +subsection {* Basic Laws *} + +lemma subst_Nil[simp]: "t \ [] = t" +by (induct t) auto + +lemma subst_mono: "t \ u \ t \ s \ u \ s" +by (induct u) auto + +lemma agreement: "(t \ r = t \ s) \ (\v \ vars_of t. Var v \ r = Var v \ s)" +by (induct t) auto + +lemma repl_invariance: "v \ vars_of t \ t \ (v,u) # s = t \ s" +by (simp add: agreement) -definition subst_eq (infixr "\" 52) -where - "s1 \ s2 \ \t. t \ s1 = t \ s2" +lemma Var_in_subst: + "v \ vars_of t \ w \ vars_of (t \ (v, Var(w)) # s)" +by (induct t) auto + +lemma subst_refl[iff]: "s \ s" + by (auto simp:subst_eq_def) + +lemma subst_sym[sym]: "\s1 \ s2\ \ s2 \ s1" + by (auto simp:subst_eq_def) + +lemma subst_trans[trans]: "\s1 \ s2; s2 \ s3\ \ s1 \ s3" + by (auto simp:subst_eq_def) + +text {* Composition of Substitutions *} -subsection {* Basic lemmas *} - -lemma apply_empty[simp]: "t \ [] = t" -by (induct t) auto - -lemma compose_empty[simp]: "\ \ [] = \" +lemma comp_Nil[simp]: "\ \ [] = \" by (induct \) auto -lemma apply_compose[simp]: "t \ (s1 \ s2) = t \ s1 \ s2" +lemma subst_comp[simp]: "t \ (r \ s) = t \ r \ s" proof (induct t) - case Comb thus ?case by simp -next - case Const thus ?case by simp -next case (Var v) thus ?case - proof (induct s1) - case Nil show ?case by simp - next - case (Cons p s1s) thus ?case by (cases p, simp) - qed -qed + by (induct r) auto +qed auto -lemma eqv_refl[intro]: "s \ s" +lemma subst_eq_intro[intro]: "(\t. t \ \ = t \ \) \ \ \ \" by (auto simp:subst_eq_def) -lemma eqv_trans[trans]: "\s1 \ s2; s2 \ s3\ \ s1 \ s3" - by (auto simp:subst_eq_def) - -lemma eqv_sym[sym]: "\s1 \ s2\ \ s2 \ s1" - by (auto simp:subst_eq_def) - -lemma eqv_intro[intro]: "(\t. t \ \ = t \ \) \ \ \ \" +lemma subst_eq_dest[dest]: "s1 \ s2 \ t \ s1 = t \ s2" by (auto simp:subst_eq_def) -lemma eqv_dest[dest]: "s1 \ s2 \ t \ s1 = t \ s2" - by (auto simp:subst_eq_def) +lemma comp_assoc: "(a \ b) \ c \ a \ (b \ c)" + by auto -lemma compose_eqv: "\\ \ \'; \ \ \'\ \ (\ \ \) \ (\' \ \')" - by (auto simp:subst_eq_def) +lemma subst_cong: "\\ \ \'; \ \ \'\ \ (\ \ \) \ (\' \ \')" + by (auto simp: subst_eq_def) -lemma compose_assoc: "(a \ b) \ c \ a \ (b \ c)" - by auto subsection {* Specification: Most general unifiers *} -definition - "Unifier \ t u \ (t\\ = u\\)" +definition Unifier :: "'a subst \ 'a trm \ 'a trm \ bool" +where "Unifier \ t u \ (t \ \ = u \ \)" -definition - "MGU \ t u \ Unifier \ t u \ (\\. Unifier \ t u - \ (\\. \ \ \ \ \))" +definition MGU :: "'a subst \ 'a trm \ 'a trm \ bool" where + "MGU \ t u \ + Unifier \ t u \ (\\. Unifier \ t u \ (\\. \ \ \ \ \))" lemma MGUI[intro]: "\t \ \ = u \ \; \\. t \ \ = u \ \ \ \\. \ \ \ \ \\ @@ -126,15 +160,13 @@ by (auto simp:MGU_def Unifier_def) +definition Idem :: "'a subst \ bool" +where "Idem s \ (s \ s) \ s" + + + subsection {* The unification algorithm *} -text {* Occurs check: Proper subterm relation *} - -fun occs :: "'a trm \ 'a trm \ bool" (infixl "<:" 54) -where - "occs u (Var v) = False" -| "occs u (Const c) = False" -| "occs u (M \ N) = (u = M \ u = N \ occs u M \ occs u N)" text {* The unification algorithm: *} @@ -226,7 +258,7 @@ by auto from Ns have "N \ \1 \ \ = N' \ \1 \ \" - by (simp add:eqv_dest[OF eqv]) + by (simp add:subst_eq_dest[OF eqv]) with MGU_outer obtain \ where eqv2: "\ \ \2 \ \" @@ -234,7 +266,7 @@ by auto have "\' \ \ \ \" unfolding \ - by (rule eqv_intro, auto simp:eqv_dest[OF eqv] eqv_dest[OF eqv2]) + by (rule subst_eq_intro, auto simp:subst_eq_dest[OF eqv] subst_eq_dest[OF eqv2]) thus "\\. \' \ \ \ \" .. qed qed (auto split:split_if_asm) -- "Solve the remaining cases automatically" @@ -242,16 +274,6 @@ subsection {* Properties used in termination proof *} -text {* The variables of a term: *} - -fun vars_of:: "'a trm \ 'a set" -where - "vars_of (Var v) = { v }" -| "vars_of (Const c) = {}" -| "vars_of (M \ N) = vars_of M \ vars_of N" - -lemma vars_of_finite[intro]: "finite (vars_of t)" - by (induct t) simp_all text {* Elimination of variables by a substitution: *} @@ -474,7 +496,7 @@ assume empty[simp]: "\2 \ []" have "\ \ (\1 \ [])" unfolding \ - by (rule compose_eqv) auto + by (rule subst_cong) auto also have "\ \ \1" by auto finally have "\ \ \1" .