src/HOL/ex/Unification.thy
author krauss
Sun Aug 21 22:13:04 2011 +0200 (2011-08-21)
changeset 44371 3a10392fb8c3
parent 44370 03d91bfad83b
child 44372 f9825056dbab
permissions -rw-r--r--
added proof of idempotence, roughly after HOL/Subst/Unify.thy
bulwahn@41460
     1
(*
krauss@22999
     2
    Author:     Alexander Krauss, Technische Universitaet Muenchen
krauss@22999
     3
*)
krauss@22999
     4
krauss@22999
     5
header {* Case study: Unification Algorithm *}
krauss@22999
     6
wenzelm@23219
     7
theory Unification
krauss@22999
     8
imports Main
wenzelm@23219
     9
begin
krauss@22999
    10
krauss@22999
    11
text {* 
krauss@22999
    12
  This is a formalization of a first-order unification
krauss@22999
    13
  algorithm. It uses the new "function" package to define recursive
krauss@22999
    14
  functions, which allows a better treatment of nested recursion. 
krauss@22999
    15
krauss@22999
    16
  This is basically a modernized version of a previous formalization
krauss@22999
    17
  by Konrad Slind (see: HOL/Subst/Unify.thy), which itself builds on
wenzelm@23219
    18
  previous work by Paulson and Manna \& Waldinger (for details, see
krauss@22999
    19
  there).
krauss@22999
    20
krauss@22999
    21
  Unlike that formalization, where the proofs of termination and
krauss@22999
    22
  some partial correctness properties are intertwined, we can prove
krauss@22999
    23
  partial correctness and termination separately.
krauss@22999
    24
*}
krauss@22999
    25
wenzelm@23219
    26
krauss@44368
    27
subsection {* Terms *}
krauss@44368
    28
krauss@44368
    29
text {* Binary trees with leaves that are constants or variables. *}
krauss@22999
    30
krauss@22999
    31
datatype 'a trm = 
krauss@22999
    32
  Var 'a 
krauss@22999
    33
  | Const 'a
krauss@44367
    34
  | Comb "'a trm" "'a trm" (infix "\<cdot>" 60)
krauss@22999
    35
krauss@44368
    36
primrec vars_of :: "'a trm \<Rightarrow> 'a set"
krauss@44368
    37
where
krauss@44368
    38
  "vars_of (Var v) = {v}"
krauss@44368
    39
| "vars_of (Const c) = {}"
krauss@44368
    40
| "vars_of (M \<cdot> N) = vars_of M \<union> vars_of N"
krauss@44368
    41
krauss@44368
    42
fun occs :: "'a trm \<Rightarrow> 'a trm \<Rightarrow> bool" (infixl "\<prec>" 54) 
krauss@44368
    43
where
krauss@44369
    44
  "u \<prec> Var v \<longleftrightarrow> False"
krauss@44369
    45
| "u \<prec> Const c \<longleftrightarrow> False"
krauss@44369
    46
| "u \<prec> M \<cdot> N \<longleftrightarrow> u = M \<or> u = N \<or> u \<prec> M \<or> u \<prec> N"
krauss@44368
    47
krauss@44368
    48
krauss@44368
    49
lemma finite_vars_of[intro]: "finite (vars_of t)"
krauss@44368
    50
  by (induct t) simp_all
krauss@44368
    51
krauss@44368
    52
lemma vars_var_iff: "v \<in> vars_of (Var w) \<longleftrightarrow> w = v"
krauss@44368
    53
  by auto
krauss@44368
    54
krauss@44368
    55
lemma vars_iff_occseq: "x \<in> vars_of t \<longleftrightarrow> Var x \<prec> t \<or> Var x = t"
krauss@44368
    56
  by (induct t) auto
krauss@44368
    57
krauss@44368
    58
lemma occs_vars_subset: "M \<prec> N \<Longrightarrow> vars_of M \<subseteq> vars_of N"
krauss@44368
    59
  by (induct N) auto
krauss@44368
    60
krauss@44368
    61
krauss@44368
    62
subsection {* Substitutions *}
krauss@44368
    63
wenzelm@42463
    64
type_synonym 'a subst = "('a \<times> 'a trm) list"
krauss@22999
    65
krauss@22999
    66
text {* Applying a substitution to a variable: *}
krauss@22999
    67
krauss@22999
    68
fun assoc :: "'a \<Rightarrow> 'b \<Rightarrow> ('a \<times> 'b) list \<Rightarrow> 'b"
krauss@22999
    69
where
krauss@22999
    70
  "assoc x d [] = d"
krauss@22999
    71
| "assoc x d ((p,q)#t) = (if x = p then q else assoc x d t)"
krauss@22999
    72
krauss@22999
    73
text {* Applying a substitution to a term: *}
krauss@22999
    74
krauss@44367
    75
primrec subst :: "'a trm \<Rightarrow> 'a subst \<Rightarrow> 'a trm" (infixl "\<lhd>" 55)
krauss@22999
    76
where
krauss@44367
    77
  "(Var v) \<lhd> s = assoc v (Var v) s"
krauss@44367
    78
| "(Const c) \<lhd> s = (Const c)"
krauss@44367
    79
| "(M \<cdot> N) \<lhd> s = (M \<lhd> s) \<cdot> (N \<lhd> s)"
krauss@22999
    80
krauss@44368
    81
definition subst_eq (infixr "\<doteq>" 52)
krauss@44368
    82
where
krauss@44368
    83
  "s1 \<doteq> s2 \<longleftrightarrow> (\<forall>t. t \<lhd> s1 = t \<lhd> s2)" 
krauss@22999
    84
krauss@44368
    85
fun comp :: "'a subst \<Rightarrow> 'a subst \<Rightarrow> 'a subst" (infixl "\<lozenge>" 56)
krauss@22999
    86
where
krauss@44367
    87
  "[] \<lozenge> bl = bl"
krauss@44367
    88
| "((a,b) # al) \<lozenge> bl = (a, b \<lhd> bl) # (al \<lozenge> bl)"
krauss@22999
    89
krauss@44368
    90
krauss@44368
    91
subsection {* Basic Laws *}
krauss@44368
    92
krauss@44368
    93
lemma subst_Nil[simp]: "t \<lhd> [] = t"
krauss@44368
    94
by (induct t) auto
krauss@44368
    95
krauss@44368
    96
lemma subst_mono: "t \<prec> u \<Longrightarrow> t \<lhd> s \<prec> u \<lhd> s"
krauss@44368
    97
by (induct u) auto
krauss@44368
    98
krauss@44368
    99
lemma agreement: "(t \<lhd> r = t \<lhd> s) \<longleftrightarrow> (\<forall>v \<in> vars_of t. Var v \<lhd> r = Var v \<lhd> s)"
krauss@44368
   100
by (induct t) auto
krauss@44368
   101
krauss@44368
   102
lemma repl_invariance: "v \<notin> vars_of t \<Longrightarrow> t \<lhd> (v,u) # s = t \<lhd> s"
krauss@44368
   103
by (simp add: agreement)
krauss@22999
   104
krauss@44368
   105
lemma Var_in_subst:
krauss@44368
   106
  "v \<in> vars_of t \<Longrightarrow> w \<in> vars_of (t \<lhd> (v, Var(w)) # s)"
krauss@44368
   107
by (induct t) auto
krauss@44368
   108
krauss@44370
   109
lemma remove_var: "v \<notin> vars_of s \<Longrightarrow> v \<notin> vars_of (t \<lhd> [(v, s)])"
krauss@44370
   110
by (induct t) simp_all
krauss@44370
   111
krauss@44368
   112
lemma subst_refl[iff]: "s \<doteq> s"
krauss@44368
   113
  by (auto simp:subst_eq_def)
krauss@44368
   114
krauss@44368
   115
lemma subst_sym[sym]: "\<lbrakk>s1 \<doteq> s2\<rbrakk> \<Longrightarrow> s2 \<doteq> s1"
krauss@44368
   116
  by (auto simp:subst_eq_def)
krauss@44368
   117
krauss@44368
   118
lemma subst_trans[trans]: "\<lbrakk>s1 \<doteq> s2; s2 \<doteq> s3\<rbrakk> \<Longrightarrow> s1 \<doteq> s3"
krauss@44368
   119
  by (auto simp:subst_eq_def)
krauss@44368
   120
krauss@44371
   121
lemma subst_no_occs: "\<not> Var v \<prec> t \<Longrightarrow> Var v \<noteq> t
krauss@44371
   122
  \<Longrightarrow> t \<lhd> [(v,s)] = t"
krauss@44371
   123
by (induct t) auto
krauss@44370
   124
krauss@44368
   125
text {* Composition of Substitutions *}
krauss@22999
   126
krauss@44368
   127
lemma comp_Nil[simp]: "\<sigma> \<lozenge> [] = \<sigma>"
krauss@22999
   128
by (induct \<sigma>) auto
krauss@22999
   129
krauss@44368
   130
lemma subst_comp[simp]: "t \<lhd> (r \<lozenge> s) = t \<lhd> r \<lhd> s"
krauss@22999
   131
proof (induct t)
krauss@22999
   132
  case (Var v) thus ?case
krauss@44368
   133
    by (induct r) auto
krauss@44368
   134
qed auto
krauss@22999
   135
krauss@44368
   136
lemma subst_eq_intro[intro]: "(\<And>t. t \<lhd> \<sigma> = t \<lhd> \<theta>) \<Longrightarrow> \<sigma> \<doteq> \<theta>"
krauss@44367
   137
  by (auto simp:subst_eq_def)
krauss@22999
   138
krauss@44368
   139
lemma subst_eq_dest[dest]: "s1 \<doteq> s2 \<Longrightarrow> t \<lhd> s1 = t \<lhd> s2"
krauss@44367
   140
  by (auto simp:subst_eq_def)
krauss@22999
   141
krauss@44368
   142
lemma comp_assoc: "(a \<lozenge> b) \<lozenge> c \<doteq> a \<lozenge> (b \<lozenge> c)"
krauss@44368
   143
  by auto
krauss@22999
   144
krauss@44368
   145
lemma subst_cong: "\<lbrakk>\<sigma> \<doteq> \<sigma>'; \<theta> \<doteq> \<theta>'\<rbrakk> \<Longrightarrow> (\<sigma> \<lozenge> \<theta>) \<doteq> (\<sigma>' \<lozenge> \<theta>')"
krauss@44368
   146
  by (auto simp: subst_eq_def)
krauss@22999
   147
krauss@44370
   148
lemma var_self: "[(v, Var v)] \<doteq> []"
krauss@44370
   149
proof
krauss@44370
   150
  fix t show "t \<lhd> [(v, Var v)] = t \<lhd> []"
krauss@44370
   151
    by (induct t) simp_all
krauss@44370
   152
qed
krauss@44370
   153
krauss@44370
   154
lemma var_same[simp]: "[(v, t)] \<doteq> [] \<longleftrightarrow> t = Var v"
krauss@44370
   155
by (metis assoc.simps(2) subst.simps(1) subst_eq_def var_self)
krauss@22999
   156
wenzelm@23219
   157
krauss@22999
   158
subsection {* Specification: Most general unifiers *}
krauss@22999
   159
krauss@44368
   160
definition Unifier :: "'a subst \<Rightarrow> 'a trm \<Rightarrow> 'a trm \<Rightarrow> bool"
krauss@44368
   161
where "Unifier \<sigma> t u \<longleftrightarrow> (t \<lhd> \<sigma> = u \<lhd> \<sigma>)"
krauss@22999
   162
krauss@44368
   163
definition MGU :: "'a subst \<Rightarrow> 'a trm \<Rightarrow> 'a trm \<Rightarrow> bool" where
krauss@44368
   164
  "MGU \<sigma> t u \<longleftrightarrow> 
krauss@44368
   165
   Unifier \<sigma> t u \<and> (\<forall>\<theta>. Unifier \<theta> t u \<longrightarrow> (\<exists>\<gamma>. \<theta> \<doteq> \<sigma> \<lozenge> \<gamma>))"
krauss@22999
   166
krauss@22999
   167
lemma MGUI[intro]:
krauss@44367
   168
  "\<lbrakk>t \<lhd> \<sigma> = u \<lhd> \<sigma>; \<And>\<theta>. t \<lhd> \<theta> = u \<lhd> \<theta> \<Longrightarrow> \<exists>\<gamma>. \<theta> \<doteq> \<sigma> \<lozenge> \<gamma>\<rbrakk>
krauss@22999
   169
  \<Longrightarrow> MGU \<sigma> t u"
krauss@22999
   170
  by (simp only:Unifier_def MGU_def, auto)
krauss@22999
   171
krauss@22999
   172
lemma MGU_sym[sym]:
krauss@22999
   173
  "MGU \<sigma> s t \<Longrightarrow> MGU \<sigma> t s"
krauss@22999
   174
  by (auto simp:MGU_def Unifier_def)
krauss@22999
   175
krauss@44371
   176
lemma MGU_is_Unifier: "MGU \<sigma> t u \<Longrightarrow> Unifier \<sigma> t u"
krauss@44371
   177
unfolding MGU_def by (rule conjunct1)
krauss@44371
   178
krauss@44370
   179
lemma MGU_Var: 
krauss@44370
   180
  assumes "\<not> Var v \<prec> t"
krauss@44370
   181
  shows "MGU [(v,t)] (Var v) t"
krauss@44370
   182
proof (intro MGUI exI)
krauss@44370
   183
  show "Var v \<lhd> [(v,t)] = t \<lhd> [(v,t)]" using assms
krauss@44370
   184
    by (metis assoc.simps(2) repl_invariance subst.simps(1) subst_Nil vars_iff_occseq)
krauss@44370
   185
next
krauss@44370
   186
  fix \<theta> assume th: "Var v \<lhd> \<theta> = t \<lhd> \<theta>" 
krauss@44370
   187
  show "\<theta> \<doteq> [(v,t)] \<lozenge> \<theta>" 
krauss@44370
   188
  proof
krauss@44370
   189
    fix s show "s \<lhd> \<theta> = s \<lhd> [(v,t)] \<lozenge> \<theta>" using th 
krauss@44370
   190
      by (induct s) auto
krauss@44370
   191
  qed
krauss@44370
   192
qed
krauss@44370
   193
krauss@44370
   194
lemma MGU_Const: "MGU [] (Const c) (Const d) \<longleftrightarrow> c = d"
krauss@44370
   195
  by (auto simp: MGU_def Unifier_def)
krauss@44370
   196
  
krauss@22999
   197
krauss@44368
   198
definition Idem :: "'a subst \<Rightarrow> bool"
krauss@44368
   199
where "Idem s \<longleftrightarrow> (s \<lozenge> s) \<doteq> s"
krauss@44368
   200
krauss@44368
   201
krauss@44368
   202
krauss@22999
   203
subsection {* The unification algorithm *}
krauss@22999
   204
krauss@22999
   205
krauss@22999
   206
text {* The unification algorithm: *}
krauss@22999
   207
krauss@22999
   208
function unify :: "'a trm \<Rightarrow> 'a trm \<Rightarrow> 'a subst option"
krauss@22999
   209
where
krauss@22999
   210
  "unify (Const c) (M \<cdot> N)   = None"
krauss@22999
   211
| "unify (M \<cdot> N)   (Const c) = None"
krauss@22999
   212
| "unify (Const c) (Var v)   = Some [(v, Const c)]"
krauss@44369
   213
| "unify (M \<cdot> N)   (Var v)   = (if Var v \<prec> M \<cdot> N 
krauss@22999
   214
                                        then None
krauss@22999
   215
                                        else Some [(v, M \<cdot> N)])"
krauss@44369
   216
| "unify (Var v)   M         = (if Var v \<prec> M
krauss@22999
   217
                                        then None
krauss@22999
   218
                                        else Some [(v, M)])"
krauss@22999
   219
| "unify (Const c) (Const d) = (if c=d then Some [] else None)"
krauss@22999
   220
| "unify (M \<cdot> N) (M' \<cdot> N') = (case unify M M' of
krauss@22999
   221
                                    None \<Rightarrow> None |
krauss@44367
   222
                                    Some \<theta> \<Rightarrow> (case unify (N \<lhd> \<theta>) (N' \<lhd> \<theta>)
krauss@22999
   223
                                      of None \<Rightarrow> None |
krauss@44367
   224
                                         Some \<sigma> \<Rightarrow> Some (\<theta> \<lozenge> \<sigma>)))"
krauss@22999
   225
  by pat_completeness auto
krauss@22999
   226
krauss@22999
   227
subsection {* Properties used in termination proof *}
krauss@22999
   228
krauss@22999
   229
krauss@22999
   230
text {* Elimination of variables by a substitution: *}
krauss@22999
   231
krauss@22999
   232
definition
krauss@44367
   233
  "elim \<sigma> v \<equiv> \<forall>t. v \<notin> vars_of (t \<lhd> \<sigma>)"
krauss@22999
   234
krauss@44367
   235
lemma elim_intro[intro]: "(\<And>t. v \<notin> vars_of (t \<lhd> \<sigma>)) \<Longrightarrow> elim \<sigma> v"
krauss@22999
   236
  by (auto simp:elim_def)
krauss@22999
   237
krauss@44367
   238
lemma elim_dest[dest]: "elim \<sigma> v \<Longrightarrow> v \<notin> vars_of (t \<lhd> \<sigma>)"
krauss@22999
   239
  by (auto simp:elim_def)
krauss@22999
   240
krauss@44370
   241
lemma elim_eq: "\<sigma> \<doteq> \<theta> \<Longrightarrow> elim \<sigma> x = elim \<theta> x"
krauss@44367
   242
  by (auto simp:elim_def subst_eq_def)
krauss@22999
   243
krauss@44369
   244
lemma occs_elim: "\<not> Var v \<prec> t 
krauss@44367
   245
  \<Longrightarrow> elim [(v,t)] v \<or> [(v,t)] \<doteq> []"
krauss@44370
   246
by (metis elim_intro remove_var var_same vars_iff_occseq)
krauss@22999
   247
krauss@22999
   248
text {* The result of a unification never introduces new variables: *}
krauss@22999
   249
krauss@44370
   250
declare unify.psimps[simp]
krauss@44370
   251
krauss@22999
   252
lemma unify_vars: 
krauss@22999
   253
  assumes "unify_dom (M, N)"
krauss@22999
   254
  assumes "unify M N = Some \<sigma>"
krauss@44367
   255
  shows "vars_of (t \<lhd> \<sigma>) \<subseteq> vars_of M \<union> vars_of N \<union> vars_of t"
krauss@22999
   256
  (is "?P M N \<sigma> t")
wenzelm@24444
   257
using assms
krauss@22999
   258
proof (induct M N arbitrary:\<sigma> t)
krauss@22999
   259
  case (3 c v) 
krauss@22999
   260
  hence "\<sigma> = [(v, Const c)]" by simp
wenzelm@24444
   261
  thus ?case by (induct t) auto
krauss@22999
   262
next
krauss@22999
   263
  case (4 M N v) 
krauss@44369
   264
  hence "\<not> Var v \<prec> M \<cdot> N" by auto
wenzelm@24444
   265
  with 4 have "\<sigma> = [(v, M\<cdot>N)]" by simp
wenzelm@24444
   266
  thus ?case by (induct t) auto
krauss@22999
   267
next
krauss@22999
   268
  case (5 v M)
krauss@44369
   269
  hence "\<not> Var v \<prec> M" by auto
wenzelm@24444
   270
  with 5 have "\<sigma> = [(v, M)]" by simp
wenzelm@24444
   271
  thus ?case by (induct t) auto
krauss@22999
   272
next
krauss@22999
   273
  case (7 M N M' N' \<sigma>)
krauss@22999
   274
  then obtain \<theta>1 \<theta>2 
krauss@22999
   275
    where "unify M M' = Some \<theta>1"
krauss@44367
   276
    and "unify (N \<lhd> \<theta>1) (N' \<lhd> \<theta>1) = Some \<theta>2"
krauss@44367
   277
    and \<sigma>: "\<sigma> = \<theta>1 \<lozenge> \<theta>2"
krauss@22999
   278
    and ih1: "\<And>t. ?P M M' \<theta>1 t"
krauss@44367
   279
    and ih2: "\<And>t. ?P (N\<lhd>\<theta>1) (N'\<lhd>\<theta>1) \<theta>2 t"
krauss@22999
   280
    by (auto split:option.split_asm)
krauss@22999
   281
krauss@22999
   282
  show ?case
krauss@22999
   283
  proof
krauss@44367
   284
    fix v assume a: "v \<in> vars_of (t \<lhd> \<sigma>)"
krauss@22999
   285
    
krauss@22999
   286
    show "v \<in> vars_of (M \<cdot> N) \<union> vars_of (M' \<cdot> N') \<union> vars_of t"
krauss@22999
   287
    proof (cases "v \<notin> vars_of M \<and> v \<notin> vars_of M'
wenzelm@32960
   288
        \<and> v \<notin> vars_of N \<and> v \<notin> vars_of N'")
krauss@22999
   289
      case True
krauss@44367
   290
      with ih1 have l:"\<And>t. v \<in> vars_of (t \<lhd> \<theta>1) \<Longrightarrow> v \<in> vars_of t"
wenzelm@32960
   291
        by auto
krauss@22999
   292
      
krauss@44367
   293
      from a and ih2[where t="t \<lhd> \<theta>1"]
krauss@44367
   294
      have "v \<in> vars_of (N \<lhd> \<theta>1) \<union> vars_of (N' \<lhd> \<theta>1) 
krauss@44367
   295
        \<or> v \<in> vars_of (t \<lhd> \<theta>1)" unfolding \<sigma>
wenzelm@32960
   296
        by auto
krauss@22999
   297
      hence "v \<in> vars_of t"
krauss@22999
   298
      proof
krauss@44367
   299
        assume "v \<in> vars_of (N \<lhd> \<theta>1) \<union> vars_of (N' \<lhd> \<theta>1)"
wenzelm@32960
   300
        with True show ?thesis by (auto dest:l)
krauss@22999
   301
      next
krauss@44367
   302
        assume "v \<in> vars_of (t \<lhd> \<theta>1)" 
wenzelm@32960
   303
        thus ?thesis by (rule l)
krauss@22999
   304
      qed
krauss@22999
   305
      
krauss@22999
   306
      thus ?thesis by auto
krauss@22999
   307
    qed auto
krauss@22999
   308
  qed
krauss@22999
   309
qed (auto split: split_if_asm)
krauss@22999
   310
krauss@22999
   311
krauss@22999
   312
text {* The result of a unification is either the identity
krauss@22999
   313
substitution or it eliminates a variable from one of the terms: *}
krauss@22999
   314
krauss@22999
   315
lemma unify_eliminates: 
krauss@22999
   316
  assumes "unify_dom (M, N)"
krauss@22999
   317
  assumes "unify M N = Some \<sigma>"
krauss@44367
   318
  shows "(\<exists>v\<in>vars_of M \<union> vars_of N. elim \<sigma> v) \<or> \<sigma> \<doteq> []"
krauss@22999
   319
  (is "?P M N \<sigma>")
wenzelm@24444
   320
using assms
krauss@22999
   321
proof (induct M N arbitrary:\<sigma>)
krauss@22999
   322
  case 1 thus ?case by simp
krauss@22999
   323
next
krauss@22999
   324
  case 2 thus ?case by simp
krauss@22999
   325
next
krauss@22999
   326
  case (3 c v)
krauss@44369
   327
  have no_occs: "\<not> Var v \<prec> Const c" by simp
wenzelm@24444
   328
  with 3 have "\<sigma> = [(v, Const c)]" by simp
krauss@44367
   329
  with occs_elim[OF no_occs]
krauss@22999
   330
  show ?case by auto
krauss@22999
   331
next
krauss@22999
   332
  case (4 M N v)
krauss@44369
   333
  hence no_occs: "\<not> Var v \<prec> M \<cdot> N" by auto
wenzelm@24444
   334
  with 4 have "\<sigma> = [(v, M\<cdot>N)]" by simp
krauss@44367
   335
  with occs_elim[OF no_occs]
krauss@22999
   336
  show ?case by auto 
krauss@22999
   337
next
krauss@22999
   338
  case (5 v M) 
krauss@44369
   339
  hence no_occs: "\<not> Var v \<prec> M" by auto
wenzelm@24444
   340
  with 5 have "\<sigma> = [(v, M)]" by simp
krauss@44367
   341
  with occs_elim[OF no_occs]
krauss@22999
   342
  show ?case by auto 
krauss@22999
   343
next 
krauss@22999
   344
  case (6 c d) thus ?case
krauss@22999
   345
    by (cases "c = d") auto
krauss@22999
   346
next
krauss@22999
   347
  case (7 M N M' N' \<sigma>)
krauss@22999
   348
  then obtain \<theta>1 \<theta>2 
krauss@22999
   349
    where "unify M M' = Some \<theta>1"
krauss@44367
   350
    and "unify (N \<lhd> \<theta>1) (N' \<lhd> \<theta>1) = Some \<theta>2"
krauss@44367
   351
    and \<sigma>: "\<sigma> = \<theta>1 \<lozenge> \<theta>2"
krauss@22999
   352
    and ih1: "?P M M' \<theta>1"
krauss@44367
   353
    and ih2: "?P (N\<lhd>\<theta>1) (N'\<lhd>\<theta>1) \<theta>2"
krauss@22999
   354
    by (auto split:option.split_asm)
krauss@22999
   355
krauss@22999
   356
  from `unify_dom (M \<cdot> N, M' \<cdot> N')`
krauss@22999
   357
  have "unify_dom (M, M')"
berghofe@23777
   358
    by (rule accp_downward) (rule unify_rel.intros)
krauss@22999
   359
  hence no_new_vars: 
krauss@44367
   360
    "\<And>t. vars_of (t \<lhd> \<theta>1) \<subseteq> vars_of M \<union> vars_of M' \<union> vars_of t"
wenzelm@23373
   361
    by (rule unify_vars) (rule `unify M M' = Some \<theta>1`)
krauss@22999
   362
krauss@22999
   363
  from ih2 show ?case 
krauss@22999
   364
  proof 
krauss@44367
   365
    assume "\<exists>v\<in>vars_of (N \<lhd> \<theta>1) \<union> vars_of (N' \<lhd> \<theta>1). elim \<theta>2 v"
krauss@22999
   366
    then obtain v 
krauss@44367
   367
      where "v\<in>vars_of (N \<lhd> \<theta>1) \<union> vars_of (N' \<lhd> \<theta>1)"
krauss@22999
   368
      and el: "elim \<theta>2 v" by auto
krauss@22999
   369
    with no_new_vars show ?thesis unfolding \<sigma> 
krauss@22999
   370
      by (auto simp:elim_def)
krauss@22999
   371
  next
krauss@44367
   372
    assume empty[simp]: "\<theta>2 \<doteq> []"
krauss@22999
   373
krauss@44367
   374
    have "\<sigma> \<doteq> (\<theta>1 \<lozenge> [])" unfolding \<sigma>
krauss@44368
   375
      by (rule subst_cong) auto
krauss@44367
   376
    also have "\<dots> \<doteq> \<theta>1" by auto
krauss@44367
   377
    finally have "\<sigma> \<doteq> \<theta>1" .
krauss@22999
   378
krauss@22999
   379
    from ih1 show ?thesis
krauss@22999
   380
    proof
krauss@22999
   381
      assume "\<exists>v\<in>vars_of M \<union> vars_of M'. elim \<theta>1 v"
krauss@44370
   382
      with elim_eq[OF `\<sigma> \<doteq> \<theta>1`]
krauss@22999
   383
      show ?thesis by auto
krauss@22999
   384
    next
krauss@44367
   385
      note `\<sigma> \<doteq> \<theta>1`
krauss@44367
   386
      also assume "\<theta>1 \<doteq> []"
krauss@22999
   387
      finally show ?thesis ..
krauss@22999
   388
    qed
krauss@22999
   389
  qed
krauss@22999
   390
qed
krauss@22999
   391
krauss@44370
   392
declare unify.psimps[simp del]
krauss@22999
   393
krauss@22999
   394
subsection {* Termination proof *}
krauss@22999
   395
krauss@22999
   396
termination unify
krauss@22999
   397
proof 
krauss@22999
   398
  let ?R = "measures [\<lambda>(M,N). card (vars_of M \<union> vars_of N),
krauss@22999
   399
                           \<lambda>(M, N). size M]"
krauss@22999
   400
  show "wf ?R" by simp
krauss@22999
   401
krauss@44370
   402
  fix M N M' N' :: "'a trm"
krauss@22999
   403
  show "((M, M'), (M \<cdot> N, M' \<cdot> N')) \<in> ?R" -- "Inner call"
krauss@22999
   404
    by (rule measures_lesseq) (auto intro: card_mono)
krauss@22999
   405
krauss@22999
   406
  fix \<theta>                                   -- "Outer call"
krauss@22999
   407
  assume inner: "unify_dom (M, M')"
krauss@22999
   408
    "unify M M' = Some \<theta>"
krauss@22999
   409
krauss@22999
   410
  from unify_eliminates[OF inner]
krauss@44367
   411
  show "((N \<lhd> \<theta>, N' \<lhd> \<theta>), (M \<cdot> N, M' \<cdot> N')) \<in>?R"
krauss@22999
   412
  proof
krauss@22999
   413
    -- {* Either a variable is eliminated \ldots *}
krauss@22999
   414
    assume "(\<exists>v\<in>vars_of M \<union> vars_of M'. elim \<theta> v)"
krauss@22999
   415
    then obtain v 
wenzelm@32960
   416
      where "elim \<theta> v" 
wenzelm@32960
   417
      and "v\<in>vars_of M \<union> vars_of M'" by auto
krauss@22999
   418
    with unify_vars[OF inner]
krauss@44367
   419
    have "vars_of (N\<lhd>\<theta>) \<union> vars_of (N'\<lhd>\<theta>)
wenzelm@32960
   420
      \<subset> vars_of (M\<cdot>N) \<union> vars_of (M'\<cdot>N')"
wenzelm@32960
   421
      by auto
krauss@22999
   422
    
krauss@22999
   423
    thus ?thesis
krauss@22999
   424
      by (auto intro!: measures_less intro: psubset_card_mono)
krauss@22999
   425
  next
krauss@22999
   426
    -- {* Or the substitution is empty *}
krauss@44367
   427
    assume "\<theta> \<doteq> []"
krauss@44367
   428
    hence "N \<lhd> \<theta> = N" 
krauss@44367
   429
      and "N' \<lhd> \<theta> = N'" by auto
krauss@22999
   430
    thus ?thesis 
krauss@22999
   431
       by (auto intro!: measures_less intro: psubset_card_mono)
krauss@22999
   432
  qed
krauss@22999
   433
qed
krauss@22999
   434
krauss@44370
   435
krauss@44370
   436
subsection {* Other Properties *}
krauss@44370
   437
krauss@44370
   438
lemma unify_computes_MGU:
krauss@44370
   439
  "unify M N = Some \<sigma> \<Longrightarrow> MGU \<sigma> M N"
krauss@44370
   440
proof (induct M N arbitrary: \<sigma> rule: unify.induct)
krauss@44370
   441
  case (7 M N M' N' \<sigma>) -- "The interesting case"
krauss@44370
   442
krauss@44370
   443
  then obtain \<theta>1 \<theta>2 
krauss@44370
   444
    where "unify M M' = Some \<theta>1"
krauss@44370
   445
    and "unify (N \<lhd> \<theta>1) (N' \<lhd> \<theta>1) = Some \<theta>2"
krauss@44370
   446
    and \<sigma>: "\<sigma> = \<theta>1 \<lozenge> \<theta>2"
krauss@44370
   447
    and MGU_inner: "MGU \<theta>1 M M'" 
krauss@44370
   448
    and MGU_outer: "MGU \<theta>2 (N \<lhd> \<theta>1) (N' \<lhd> \<theta>1)"
krauss@44370
   449
    by (auto split:option.split_asm)
krauss@44370
   450
krauss@44370
   451
  show ?case
krauss@44370
   452
  proof
krauss@44370
   453
    from MGU_inner and MGU_outer
krauss@44370
   454
    have "M \<lhd> \<theta>1 = M' \<lhd> \<theta>1" 
krauss@44370
   455
      and "N \<lhd> \<theta>1 \<lhd> \<theta>2 = N' \<lhd> \<theta>1 \<lhd> \<theta>2"
krauss@44370
   456
      unfolding MGU_def Unifier_def
krauss@44370
   457
      by auto
krauss@44370
   458
    thus "M \<cdot> N \<lhd> \<sigma> = M' \<cdot> N' \<lhd> \<sigma>" unfolding \<sigma>
krauss@44370
   459
      by simp
krauss@44370
   460
  next
krauss@44370
   461
    fix \<sigma>' assume "M \<cdot> N \<lhd> \<sigma>' = M' \<cdot> N' \<lhd> \<sigma>'"
krauss@44370
   462
    hence "M \<lhd> \<sigma>' = M' \<lhd> \<sigma>'"
krauss@44370
   463
      and Ns: "N \<lhd> \<sigma>' = N' \<lhd> \<sigma>'" by auto
krauss@44370
   464
krauss@44370
   465
    with MGU_inner obtain \<delta>
krauss@44370
   466
      where eqv: "\<sigma>' \<doteq> \<theta>1 \<lozenge> \<delta>"
krauss@44370
   467
      unfolding MGU_def Unifier_def
krauss@44370
   468
      by auto
krauss@44370
   469
krauss@44370
   470
    from Ns have "N \<lhd> \<theta>1 \<lhd> \<delta> = N' \<lhd> \<theta>1 \<lhd> \<delta>"
krauss@44370
   471
      by (simp add:subst_eq_dest[OF eqv])
krauss@44370
   472
krauss@44370
   473
    with MGU_outer obtain \<rho>
krauss@44370
   474
      where eqv2: "\<delta> \<doteq> \<theta>2 \<lozenge> \<rho>"
krauss@44370
   475
      unfolding MGU_def Unifier_def
krauss@44370
   476
      by auto
krauss@44370
   477
    
krauss@44370
   478
    have "\<sigma>' \<doteq> \<sigma> \<lozenge> \<rho>" unfolding \<sigma>
krauss@44370
   479
      by (rule subst_eq_intro, auto simp:subst_eq_dest[OF eqv] subst_eq_dest[OF eqv2])
krauss@44370
   480
    thus "\<exists>\<gamma>. \<sigma>' \<doteq> \<sigma> \<lozenge> \<gamma>" ..
krauss@44370
   481
  qed
krauss@44370
   482
qed (auto simp: MGU_Const intro: MGU_Var MGU_Var[symmetric] split: split_if_asm)
krauss@44370
   483
krauss@44371
   484
lemma Idem_Nil [iff]: "Idem []"
krauss@44371
   485
  by (simp add: Idem_def)
krauss@44370
   486
krauss@44371
   487
lemma Var_Idem: 
krauss@44371
   488
  assumes "~ (Var v \<prec> t)" shows "Idem [(v,t)]"
krauss@44371
   489
  unfolding Idem_def
krauss@44371
   490
proof
krauss@44371
   491
  from assms have [simp]: "t \<lhd> [(v, t)] = t"
krauss@44371
   492
    by (metis assoc.simps(2) subst.simps(1) subst_no_occs)
krauss@44371
   493
krauss@44371
   494
  fix s show "s \<lhd> [(v, t)] \<lozenge> [(v, t)] = s \<lhd> [(v, t)]"
krauss@44371
   495
    by (induct s) auto
krauss@44371
   496
qed
krauss@44371
   497
krauss@44371
   498
lemma Unifier_Idem_subst: 
krauss@44371
   499
  "Idem(r) \<Longrightarrow> Unifier s (t \<lhd> r) (u \<lhd> r) \<Longrightarrow>
krauss@44371
   500
    Unifier (r \<lozenge> s) (t \<lhd> r) (u \<lhd> r)"
krauss@44371
   501
by (simp add: Idem_def Unifier_def subst_eq_def)
krauss@44371
   502
krauss@44371
   503
lemma Idem_comp:
krauss@44371
   504
  "Idem r \<Longrightarrow> Unifier s (t \<lhd> r) (u \<lhd> r) \<Longrightarrow>
krauss@44371
   505
      (!!q. Unifier q (t \<lhd> r) (u \<lhd> r) \<Longrightarrow> s \<lozenge> q \<doteq> q) \<Longrightarrow>
krauss@44371
   506
    Idem (r \<lozenge> s)"
krauss@44371
   507
  apply (frule Unifier_Idem_subst, blast) 
krauss@44371
   508
  apply (force simp add: Idem_def subst_eq_def)
krauss@44371
   509
  done
krauss@44371
   510
krauss@44371
   511
theorem unify_gives_Idem:
krauss@44371
   512
  "unify M N  = Some \<sigma> \<Longrightarrow> Idem \<sigma>"
krauss@44371
   513
proof (induct M N arbitrary: \<sigma> rule: unify.induct)
krauss@44371
   514
  case (7 M M' N N' \<sigma>)
krauss@44371
   515
krauss@44371
   516
  then obtain \<theta>1 \<theta>2 
krauss@44371
   517
    where "unify M N = Some \<theta>1"
krauss@44371
   518
    and \<theta>2: "unify (M' \<lhd> \<theta>1) (N' \<lhd> \<theta>1) = Some \<theta>2"
krauss@44371
   519
    and \<sigma>: "\<sigma> = \<theta>1 \<lozenge> \<theta>2"
krauss@44371
   520
    and "Idem \<theta>1" 
krauss@44371
   521
    and "Idem \<theta>2"
krauss@44371
   522
    by (auto split: option.split_asm)
krauss@44371
   523
krauss@44371
   524
  from \<theta>2 have "Unifier \<theta>2 (M' \<lhd> \<theta>1) (N' \<lhd> \<theta>1)"
krauss@44371
   525
    by (rule unify_computes_MGU[THEN MGU_is_Unifier])
krauss@44371
   526
krauss@44371
   527
  with `Idem \<theta>1`
krauss@44371
   528
  show "Idem \<sigma>" unfolding \<sigma>
krauss@44371
   529
  proof (rule Idem_comp)
krauss@44371
   530
    fix \<sigma> assume "Unifier \<sigma> (M' \<lhd> \<theta>1) (N' \<lhd> \<theta>1)"
krauss@44371
   531
    with \<theta>2 obtain \<gamma> where \<sigma>: "\<sigma> \<doteq> \<theta>2 \<lozenge> \<gamma>"
krauss@44371
   532
      using unify_computes_MGU MGU_def by blast
krauss@44371
   533
krauss@44371
   534
    have "\<theta>2 \<lozenge> \<sigma> \<doteq> \<theta>2 \<lozenge> (\<theta>2 \<lozenge> \<gamma>)" by (rule subst_cong) (auto simp: \<sigma>)
krauss@44371
   535
    also have "... \<doteq> (\<theta>2 \<lozenge> \<theta>2) \<lozenge> \<gamma>" by (rule comp_assoc[symmetric])
krauss@44371
   536
    also have "... \<doteq> \<theta>2 \<lozenge> \<gamma>" by (rule subst_cong) (auto simp: `Idem \<theta>2`[unfolded Idem_def])
krauss@44371
   537
    also have "... \<doteq> \<sigma>" by (rule \<sigma>[symmetric])
krauss@44371
   538
    finally show "\<theta>2 \<lozenge> \<sigma> \<doteq> \<sigma>" .
krauss@44371
   539
  qed
krauss@44371
   540
qed (auto intro!: Var_Idem split: option.splits if_splits)
krauss@39754
   541
wenzelm@23219
   542
end