src/ZF/OrderArith.thy
author wenzelm
Sun Nov 02 16:39:54 2014 +0100 (2014-11-02)
changeset 58871 c399ae4b836f
parent 46953 2b6e55924af3
child 60770 240563fbf41d
permissions -rw-r--r--
modernized header;
     1 (*  Title:      ZF/OrderArith.thy
     2     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     3     Copyright   1994  University of Cambridge
     4 *)
     5 
     6 section{*Combining Orderings: Foundations of Ordinal Arithmetic*}
     7 
     8 theory OrderArith imports Order Sum Ordinal begin
     9 
    10 definition
    11   (*disjoint sum of two relations; underlies ordinal addition*)
    12   radd    :: "[i,i,i,i]=>i"  where
    13     "radd(A,r,B,s) ==
    14                 {z: (A+B) * (A+B).
    15                     (\<exists>x y. z = <Inl(x), Inr(y)>)   |
    16                     (\<exists>x' x. z = <Inl(x'), Inl(x)> & <x',x>:r)   |
    17                     (\<exists>y' y. z = <Inr(y'), Inr(y)> & <y',y>:s)}"
    18 
    19 definition
    20   (*lexicographic product of two relations; underlies ordinal multiplication*)
    21   rmult   :: "[i,i,i,i]=>i"  where
    22     "rmult(A,r,B,s) ==
    23                 {z: (A*B) * (A*B).
    24                     \<exists>x' y' x y. z = <<x',y'>, <x,y>> &
    25                        (<x',x>: r | (x'=x & <y',y>: s))}"
    26 
    27 definition
    28   (*inverse image of a relation*)
    29   rvimage :: "[i,i,i]=>i"  where
    30     "rvimage(A,f,r) == {z \<in> A*A. \<exists>x y. z = <x,y> & <f`x,f`y>: r}"
    31 
    32 definition
    33   measure :: "[i, i\<Rightarrow>i] \<Rightarrow> i"  where
    34     "measure(A,f) == {<x,y>: A*A. f(x) < f(y)}"
    35 
    36 
    37 subsection{*Addition of Relations -- Disjoint Sum*}
    38 
    39 subsubsection{*Rewrite rules.  Can be used to obtain introduction rules*}
    40 
    41 lemma radd_Inl_Inr_iff [iff]:
    42     "<Inl(a), Inr(b)> \<in> radd(A,r,B,s)  \<longleftrightarrow>  a \<in> A & b \<in> B"
    43 by (unfold radd_def, blast)
    44 
    45 lemma radd_Inl_iff [iff]:
    46     "<Inl(a'), Inl(a)> \<in> radd(A,r,B,s)  \<longleftrightarrow>  a':A & a \<in> A & <a',a>:r"
    47 by (unfold radd_def, blast)
    48 
    49 lemma radd_Inr_iff [iff]:
    50     "<Inr(b'), Inr(b)> \<in> radd(A,r,B,s) \<longleftrightarrow>  b':B & b \<in> B & <b',b>:s"
    51 by (unfold radd_def, blast)
    52 
    53 lemma radd_Inr_Inl_iff [simp]:
    54     "<Inr(b), Inl(a)> \<in> radd(A,r,B,s) \<longleftrightarrow> False"
    55 by (unfold radd_def, blast)
    56 
    57 declare radd_Inr_Inl_iff [THEN iffD1, dest!]
    58 
    59 subsubsection{*Elimination Rule*}
    60 
    61 lemma raddE:
    62     "[| <p',p> \<in> radd(A,r,B,s);
    63         !!x y. [| p'=Inl(x); x \<in> A; p=Inr(y); y \<in> B |] ==> Q;
    64         !!x' x. [| p'=Inl(x'); p=Inl(x); <x',x>: r; x':A; x \<in> A |] ==> Q;
    65         !!y' y. [| p'=Inr(y'); p=Inr(y); <y',y>: s; y':B; y \<in> B |] ==> Q
    66      |] ==> Q"
    67 by (unfold radd_def, blast)
    68 
    69 subsubsection{*Type checking*}
    70 
    71 lemma radd_type: "radd(A,r,B,s) \<subseteq> (A+B) * (A+B)"
    72 apply (unfold radd_def)
    73 apply (rule Collect_subset)
    74 done
    75 
    76 lemmas field_radd = radd_type [THEN field_rel_subset]
    77 
    78 subsubsection{*Linearity*}
    79 
    80 lemma linear_radd:
    81     "[| linear(A,r);  linear(B,s) |] ==> linear(A+B,radd(A,r,B,s))"
    82 by (unfold linear_def, blast)
    83 
    84 
    85 subsubsection{*Well-foundedness*}
    86 
    87 lemma wf_on_radd: "[| wf[A](r);  wf[B](s) |] ==> wf[A+B](radd(A,r,B,s))"
    88 apply (rule wf_onI2)
    89 apply (subgoal_tac "\<forall>x\<in>A. Inl (x) \<in> Ba")
    90  --{*Proving the lemma, which is needed twice!*}
    91  prefer 2
    92  apply (erule_tac V = "y \<in> A + B" in thin_rl)
    93  apply (rule_tac ballI)
    94  apply (erule_tac r = r and a = x in wf_on_induct, assumption)
    95  apply blast
    96 txt{*Returning to main part of proof*}
    97 apply safe
    98 apply blast
    99 apply (erule_tac r = s and a = ya in wf_on_induct, assumption, blast)
   100 done
   101 
   102 lemma wf_radd: "[| wf(r);  wf(s) |] ==> wf(radd(field(r),r,field(s),s))"
   103 apply (simp add: wf_iff_wf_on_field)
   104 apply (rule wf_on_subset_A [OF _ field_radd])
   105 apply (blast intro: wf_on_radd)
   106 done
   107 
   108 lemma well_ord_radd:
   109      "[| well_ord(A,r);  well_ord(B,s) |] ==> well_ord(A+B, radd(A,r,B,s))"
   110 apply (rule well_ordI)
   111 apply (simp add: well_ord_def wf_on_radd)
   112 apply (simp add: well_ord_def tot_ord_def linear_radd)
   113 done
   114 
   115 subsubsection{*An @{term ord_iso} congruence law*}
   116 
   117 lemma sum_bij:
   118      "[| f \<in> bij(A,C);  g \<in> bij(B,D) |]
   119       ==> (\<lambda>z\<in>A+B. case(%x. Inl(f`x), %y. Inr(g`y), z)) \<in> bij(A+B, C+D)"
   120 apply (rule_tac d = "case (%x. Inl (converse(f)`x), %y. Inr(converse(g)`y))"
   121        in lam_bijective)
   122 apply (typecheck add: bij_is_inj inj_is_fun)
   123 apply (auto simp add: left_inverse_bij right_inverse_bij)
   124 done
   125 
   126 lemma sum_ord_iso_cong:
   127     "[| f \<in> ord_iso(A,r,A',r');  g \<in> ord_iso(B,s,B',s') |] ==>
   128             (\<lambda>z\<in>A+B. case(%x. Inl(f`x), %y. Inr(g`y), z))
   129             \<in> ord_iso(A+B, radd(A,r,B,s), A'+B', radd(A',r',B',s'))"
   130 apply (unfold ord_iso_def)
   131 apply (safe intro!: sum_bij)
   132 (*Do the beta-reductions now*)
   133 apply (auto cong add: conj_cong simp add: bij_is_fun [THEN apply_type])
   134 done
   135 
   136 (*Could we prove an ord_iso result?  Perhaps
   137      ord_iso(A+B, radd(A,r,B,s), A \<union> B, r \<union> s) *)
   138 lemma sum_disjoint_bij: "A \<inter> B = 0 ==>
   139             (\<lambda>z\<in>A+B. case(%x. x, %y. y, z)) \<in> bij(A+B, A \<union> B)"
   140 apply (rule_tac d = "%z. if z \<in> A then Inl (z) else Inr (z) " in lam_bijective)
   141 apply auto
   142 done
   143 
   144 subsubsection{*Associativity*}
   145 
   146 lemma sum_assoc_bij:
   147      "(\<lambda>z\<in>(A+B)+C. case(case(Inl, %y. Inr(Inl(y))), %y. Inr(Inr(y)), z))
   148       \<in> bij((A+B)+C, A+(B+C))"
   149 apply (rule_tac d = "case (%x. Inl (Inl (x)), case (%x. Inl (Inr (x)), Inr))"
   150        in lam_bijective)
   151 apply auto
   152 done
   153 
   154 lemma sum_assoc_ord_iso:
   155      "(\<lambda>z\<in>(A+B)+C. case(case(Inl, %y. Inr(Inl(y))), %y. Inr(Inr(y)), z))
   156       \<in> ord_iso((A+B)+C, radd(A+B, radd(A,r,B,s), C, t),
   157                 A+(B+C), radd(A, r, B+C, radd(B,s,C,t)))"
   158 by (rule sum_assoc_bij [THEN ord_isoI], auto)
   159 
   160 
   161 subsection{*Multiplication of Relations -- Lexicographic Product*}
   162 
   163 subsubsection{*Rewrite rule.  Can be used to obtain introduction rules*}
   164 
   165 lemma  rmult_iff [iff]:
   166     "<<a',b'>, <a,b>> \<in> rmult(A,r,B,s) \<longleftrightarrow>
   167             (<a',a>: r  & a':A & a \<in> A & b': B & b \<in> B) |
   168             (<b',b>: s  & a'=a & a \<in> A & b': B & b \<in> B)"
   169 
   170 by (unfold rmult_def, blast)
   171 
   172 lemma rmultE:
   173     "[| <<a',b'>, <a,b>> \<in> rmult(A,r,B,s);
   174         [| <a',a>: r;  a':A;  a \<in> A;  b':B;  b \<in> B |] ==> Q;
   175         [| <b',b>: s;  a \<in> A;  a'=a;  b':B;  b \<in> B |] ==> Q
   176      |] ==> Q"
   177 by blast
   178 
   179 subsubsection{*Type checking*}
   180 
   181 lemma rmult_type: "rmult(A,r,B,s) \<subseteq> (A*B) * (A*B)"
   182 by (unfold rmult_def, rule Collect_subset)
   183 
   184 lemmas field_rmult = rmult_type [THEN field_rel_subset]
   185 
   186 subsubsection{*Linearity*}
   187 
   188 lemma linear_rmult:
   189     "[| linear(A,r);  linear(B,s) |] ==> linear(A*B,rmult(A,r,B,s))"
   190 by (simp add: linear_def, blast)
   191 
   192 subsubsection{*Well-foundedness*}
   193 
   194 lemma wf_on_rmult: "[| wf[A](r);  wf[B](s) |] ==> wf[A*B](rmult(A,r,B,s))"
   195 apply (rule wf_onI2)
   196 apply (erule SigmaE)
   197 apply (erule ssubst)
   198 apply (subgoal_tac "\<forall>b\<in>B. <x,b>: Ba", blast)
   199 apply (erule_tac a = x in wf_on_induct, assumption)
   200 apply (rule ballI)
   201 apply (erule_tac a = b in wf_on_induct, assumption)
   202 apply (best elim!: rmultE bspec [THEN mp])
   203 done
   204 
   205 
   206 lemma wf_rmult: "[| wf(r);  wf(s) |] ==> wf(rmult(field(r),r,field(s),s))"
   207 apply (simp add: wf_iff_wf_on_field)
   208 apply (rule wf_on_subset_A [OF _ field_rmult])
   209 apply (blast intro: wf_on_rmult)
   210 done
   211 
   212 lemma well_ord_rmult:
   213      "[| well_ord(A,r);  well_ord(B,s) |] ==> well_ord(A*B, rmult(A,r,B,s))"
   214 apply (rule well_ordI)
   215 apply (simp add: well_ord_def wf_on_rmult)
   216 apply (simp add: well_ord_def tot_ord_def linear_rmult)
   217 done
   218 
   219 
   220 subsubsection{*An @{term ord_iso} congruence law*}
   221 
   222 lemma prod_bij:
   223      "[| f \<in> bij(A,C);  g \<in> bij(B,D) |]
   224       ==> (lam <x,y>:A*B. <f`x, g`y>) \<in> bij(A*B, C*D)"
   225 apply (rule_tac d = "%<x,y>. <converse (f) `x, converse (g) `y>"
   226        in lam_bijective)
   227 apply (typecheck add: bij_is_inj inj_is_fun)
   228 apply (auto simp add: left_inverse_bij right_inverse_bij)
   229 done
   230 
   231 lemma prod_ord_iso_cong:
   232     "[| f \<in> ord_iso(A,r,A',r');  g \<in> ord_iso(B,s,B',s') |]
   233      ==> (lam <x,y>:A*B. <f`x, g`y>)
   234          \<in> ord_iso(A*B, rmult(A,r,B,s), A'*B', rmult(A',r',B',s'))"
   235 apply (unfold ord_iso_def)
   236 apply (safe intro!: prod_bij)
   237 apply (simp_all add: bij_is_fun [THEN apply_type])
   238 apply (blast intro: bij_is_inj [THEN inj_apply_equality])
   239 done
   240 
   241 lemma singleton_prod_bij: "(\<lambda>z\<in>A. <x,z>) \<in> bij(A, {x}*A)"
   242 by (rule_tac d = snd in lam_bijective, auto)
   243 
   244 (*Used??*)
   245 lemma singleton_prod_ord_iso:
   246      "well_ord({x},xr) ==>
   247           (\<lambda>z\<in>A. <x,z>) \<in> ord_iso(A, r, {x}*A, rmult({x}, xr, A, r))"
   248 apply (rule singleton_prod_bij [THEN ord_isoI])
   249 apply (simp (no_asm_simp))
   250 apply (blast dest: well_ord_is_wf [THEN wf_on_not_refl])
   251 done
   252 
   253 (*Here we build a complicated function term, then simplify it using
   254   case_cong, id_conv, comp_lam, case_case.*)
   255 lemma prod_sum_singleton_bij:
   256      "a\<notin>C ==>
   257        (\<lambda>x\<in>C*B + D. case(%x. x, %y.<a,y>, x))
   258        \<in> bij(C*B + D, C*B \<union> {a}*D)"
   259 apply (rule subst_elem)
   260 apply (rule id_bij [THEN sum_bij, THEN comp_bij])
   261 apply (rule singleton_prod_bij)
   262 apply (rule sum_disjoint_bij, blast)
   263 apply (simp (no_asm_simp) cong add: case_cong)
   264 apply (rule comp_lam [THEN trans, symmetric])
   265 apply (fast elim!: case_type)
   266 apply (simp (no_asm_simp) add: case_case)
   267 done
   268 
   269 lemma prod_sum_singleton_ord_iso:
   270  "[| a \<in> A;  well_ord(A,r) |] ==>
   271     (\<lambda>x\<in>pred(A,a,r)*B + pred(B,b,s). case(%x. x, %y.<a,y>, x))
   272     \<in> ord_iso(pred(A,a,r)*B + pred(B,b,s),
   273                   radd(A*B, rmult(A,r,B,s), B, s),
   274               pred(A,a,r)*B \<union> {a}*pred(B,b,s), rmult(A,r,B,s))"
   275 apply (rule prod_sum_singleton_bij [THEN ord_isoI])
   276 apply (simp (no_asm_simp) add: pred_iff well_ord_is_wf [THEN wf_on_not_refl])
   277 apply (auto elim!: well_ord_is_wf [THEN wf_on_asym] predE)
   278 done
   279 
   280 subsubsection{*Distributive law*}
   281 
   282 lemma sum_prod_distrib_bij:
   283      "(lam <x,z>:(A+B)*C. case(%y. Inl(<y,z>), %y. Inr(<y,z>), x))
   284       \<in> bij((A+B)*C, (A*C)+(B*C))"
   285 by (rule_tac d = "case (%<x,y>.<Inl (x),y>, %<x,y>.<Inr (x),y>) "
   286     in lam_bijective, auto)
   287 
   288 lemma sum_prod_distrib_ord_iso:
   289  "(lam <x,z>:(A+B)*C. case(%y. Inl(<y,z>), %y. Inr(<y,z>), x))
   290   \<in> ord_iso((A+B)*C, rmult(A+B, radd(A,r,B,s), C, t),
   291             (A*C)+(B*C), radd(A*C, rmult(A,r,C,t), B*C, rmult(B,s,C,t)))"
   292 by (rule sum_prod_distrib_bij [THEN ord_isoI], auto)
   293 
   294 subsubsection{*Associativity*}
   295 
   296 lemma prod_assoc_bij:
   297      "(lam <<x,y>, z>:(A*B)*C. <x,<y,z>>) \<in> bij((A*B)*C, A*(B*C))"
   298 by (rule_tac d = "%<x, <y,z>>. <<x,y>, z>" in lam_bijective, auto)
   299 
   300 lemma prod_assoc_ord_iso:
   301  "(lam <<x,y>, z>:(A*B)*C. <x,<y,z>>)
   302   \<in> ord_iso((A*B)*C, rmult(A*B, rmult(A,r,B,s), C, t),
   303             A*(B*C), rmult(A, r, B*C, rmult(B,s,C,t)))"
   304 by (rule prod_assoc_bij [THEN ord_isoI], auto)
   305 
   306 subsection{*Inverse Image of a Relation*}
   307 
   308 subsubsection{*Rewrite rule*}
   309 
   310 lemma rvimage_iff: "<a,b> \<in> rvimage(A,f,r)  \<longleftrightarrow>  <f`a,f`b>: r & a \<in> A & b \<in> A"
   311 by (unfold rvimage_def, blast)
   312 
   313 subsubsection{*Type checking*}
   314 
   315 lemma rvimage_type: "rvimage(A,f,r) \<subseteq> A*A"
   316 by (unfold rvimage_def, rule Collect_subset)
   317 
   318 lemmas field_rvimage = rvimage_type [THEN field_rel_subset]
   319 
   320 lemma rvimage_converse: "rvimage(A,f, converse(r)) = converse(rvimage(A,f,r))"
   321 by (unfold rvimage_def, blast)
   322 
   323 
   324 subsubsection{*Partial Ordering Properties*}
   325 
   326 lemma irrefl_rvimage:
   327     "[| f \<in> inj(A,B);  irrefl(B,r) |] ==> irrefl(A, rvimage(A,f,r))"
   328 apply (unfold irrefl_def rvimage_def)
   329 apply (blast intro: inj_is_fun [THEN apply_type])
   330 done
   331 
   332 lemma trans_on_rvimage:
   333     "[| f \<in> inj(A,B);  trans[B](r) |] ==> trans[A](rvimage(A,f,r))"
   334 apply (unfold trans_on_def rvimage_def)
   335 apply (blast intro: inj_is_fun [THEN apply_type])
   336 done
   337 
   338 lemma part_ord_rvimage:
   339     "[| f \<in> inj(A,B);  part_ord(B,r) |] ==> part_ord(A, rvimage(A,f,r))"
   340 apply (unfold part_ord_def)
   341 apply (blast intro!: irrefl_rvimage trans_on_rvimage)
   342 done
   343 
   344 subsubsection{*Linearity*}
   345 
   346 lemma linear_rvimage:
   347     "[| f \<in> inj(A,B);  linear(B,r) |] ==> linear(A,rvimage(A,f,r))"
   348 apply (simp add: inj_def linear_def rvimage_iff)
   349 apply (blast intro: apply_funtype)
   350 done
   351 
   352 lemma tot_ord_rvimage:
   353     "[| f \<in> inj(A,B);  tot_ord(B,r) |] ==> tot_ord(A, rvimage(A,f,r))"
   354 apply (unfold tot_ord_def)
   355 apply (blast intro!: part_ord_rvimage linear_rvimage)
   356 done
   357 
   358 
   359 subsubsection{*Well-foundedness*}
   360 
   361 lemma wf_rvimage [intro!]: "wf(r) ==> wf(rvimage(A,f,r))"
   362 apply (simp (no_asm_use) add: rvimage_def wf_eq_minimal)
   363 apply clarify
   364 apply (subgoal_tac "\<exists>w. w \<in> {w: {f`x. x \<in> Q}. \<exists>x. x \<in> Q & (f`x = w) }")
   365  apply (erule allE)
   366  apply (erule impE)
   367  apply assumption
   368  apply blast
   369 apply blast
   370 done
   371 
   372 text{*But note that the combination of @{text wf_imp_wf_on} and
   373  @{text wf_rvimage} gives @{prop "wf(r) ==> wf[C](rvimage(A,f,r))"}*}
   374 lemma wf_on_rvimage: "[| f \<in> A->B;  wf[B](r) |] ==> wf[A](rvimage(A,f,r))"
   375 apply (rule wf_onI2)
   376 apply (subgoal_tac "\<forall>z\<in>A. f`z=f`y \<longrightarrow> z \<in> Ba")
   377  apply blast
   378 apply (erule_tac a = "f`y" in wf_on_induct)
   379  apply (blast intro!: apply_funtype)
   380 apply (blast intro!: apply_funtype dest!: rvimage_iff [THEN iffD1])
   381 done
   382 
   383 (*Note that we need only wf[A](...) and linear(A,...) to get the result!*)
   384 lemma well_ord_rvimage:
   385      "[| f \<in> inj(A,B);  well_ord(B,r) |] ==> well_ord(A, rvimage(A,f,r))"
   386 apply (rule well_ordI)
   387 apply (unfold well_ord_def tot_ord_def)
   388 apply (blast intro!: wf_on_rvimage inj_is_fun)
   389 apply (blast intro!: linear_rvimage)
   390 done
   391 
   392 lemma ord_iso_rvimage:
   393     "f \<in> bij(A,B) ==> f \<in> ord_iso(A, rvimage(A,f,s), B, s)"
   394 apply (unfold ord_iso_def)
   395 apply (simp add: rvimage_iff)
   396 done
   397 
   398 lemma ord_iso_rvimage_eq:
   399     "f \<in> ord_iso(A,r, B,s) ==> rvimage(A,f,s) = r \<inter> A*A"
   400 by (unfold ord_iso_def rvimage_def, blast)
   401 
   402 
   403 subsection{*Every well-founded relation is a subset of some inverse image of
   404       an ordinal*}
   405 
   406 lemma wf_rvimage_Ord: "Ord(i) \<Longrightarrow> wf(rvimage(A, f, Memrel(i)))"
   407 by (blast intro: wf_rvimage wf_Memrel)
   408 
   409 
   410 definition
   411   wfrank :: "[i,i]=>i"  where
   412     "wfrank(r,a) == wfrec(r, a, %x f. \<Union>y \<in> r-``{x}. succ(f`y))"
   413 
   414 definition
   415   wftype :: "i=>i"  where
   416     "wftype(r) == \<Union>y \<in> range(r). succ(wfrank(r,y))"
   417 
   418 lemma wfrank: "wf(r) ==> wfrank(r,a) = (\<Union>y \<in> r-``{a}. succ(wfrank(r,y)))"
   419 by (subst wfrank_def [THEN def_wfrec], simp_all)
   420 
   421 lemma Ord_wfrank: "wf(r) ==> Ord(wfrank(r,a))"
   422 apply (rule_tac a=a in wf_induct, assumption)
   423 apply (subst wfrank, assumption)
   424 apply (rule Ord_succ [THEN Ord_UN], blast)
   425 done
   426 
   427 lemma wfrank_lt: "[|wf(r); <a,b> \<in> r|] ==> wfrank(r,a) < wfrank(r,b)"
   428 apply (rule_tac a1 = b in wfrank [THEN ssubst], assumption)
   429 apply (rule UN_I [THEN ltI])
   430 apply (simp add: Ord_wfrank vimage_iff)+
   431 done
   432 
   433 lemma Ord_wftype: "wf(r) ==> Ord(wftype(r))"
   434 by (simp add: wftype_def Ord_wfrank)
   435 
   436 lemma wftypeI: "\<lbrakk>wf(r);  x \<in> field(r)\<rbrakk> \<Longrightarrow> wfrank(r,x) \<in> wftype(r)"
   437 apply (simp add: wftype_def)
   438 apply (blast intro: wfrank_lt [THEN ltD])
   439 done
   440 
   441 
   442 lemma wf_imp_subset_rvimage:
   443      "[|wf(r); r \<subseteq> A*A|] ==> \<exists>i f. Ord(i) & r \<subseteq> rvimage(A, f, Memrel(i))"
   444 apply (rule_tac x="wftype(r)" in exI)
   445 apply (rule_tac x="\<lambda>x\<in>A. wfrank(r,x)" in exI)
   446 apply (simp add: Ord_wftype, clarify)
   447 apply (frule subsetD, assumption, clarify)
   448 apply (simp add: rvimage_iff wfrank_lt [THEN ltD])
   449 apply (blast intro: wftypeI)
   450 done
   451 
   452 theorem wf_iff_subset_rvimage:
   453   "relation(r) ==> wf(r) \<longleftrightarrow> (\<exists>i f A. Ord(i) & r \<subseteq> rvimage(A, f, Memrel(i)))"
   454 by (blast dest!: relation_field_times_field wf_imp_subset_rvimage
   455           intro: wf_rvimage_Ord [THEN wf_subset])
   456 
   457 
   458 subsection{*Other Results*}
   459 
   460 lemma wf_times: "A \<inter> B = 0 ==> wf(A*B)"
   461 by (simp add: wf_def, blast)
   462 
   463 text{*Could also be used to prove @{text wf_radd}*}
   464 lemma wf_Un:
   465      "[| range(r) \<inter> domain(s) = 0; wf(r);  wf(s) |] ==> wf(r \<union> s)"
   466 apply (simp add: wf_def, clarify)
   467 apply (rule equalityI)
   468  prefer 2 apply blast
   469 apply clarify
   470 apply (drule_tac x=Z in spec)
   471 apply (drule_tac x="Z \<inter> domain(s)" in spec)
   472 apply simp
   473 apply (blast intro: elim: equalityE)
   474 done
   475 
   476 subsubsection{*The Empty Relation*}
   477 
   478 lemma wf0: "wf(0)"
   479 by (simp add: wf_def, blast)
   480 
   481 lemma linear0: "linear(0,0)"
   482 by (simp add: linear_def)
   483 
   484 lemma well_ord0: "well_ord(0,0)"
   485 by (blast intro: wf_imp_wf_on well_ordI wf0 linear0)
   486 
   487 subsubsection{*The "measure" relation is useful with wfrec*}
   488 
   489 lemma measure_eq_rvimage_Memrel:
   490      "measure(A,f) = rvimage(A,Lambda(A,f),Memrel(Collect(RepFun(A,f),Ord)))"
   491 apply (simp (no_asm) add: measure_def rvimage_def Memrel_iff)
   492 apply (rule equalityI, auto)
   493 apply (auto intro: Ord_in_Ord simp add: lt_def)
   494 done
   495 
   496 lemma wf_measure [iff]: "wf(measure(A,f))"
   497 by (simp (no_asm) add: measure_eq_rvimage_Memrel wf_Memrel wf_rvimage)
   498 
   499 lemma measure_iff [iff]: "<x,y> \<in> measure(A,f) \<longleftrightarrow> x \<in> A & y \<in> A & f(x)<f(y)"
   500 by (simp (no_asm) add: measure_def)
   501 
   502 lemma linear_measure:
   503  assumes Ordf: "!!x. x \<in> A ==> Ord(f(x))"
   504      and inj:  "!!x y. [|x \<in> A; y \<in> A; f(x) = f(y) |] ==> x=y"
   505  shows "linear(A, measure(A,f))"
   506 apply (auto simp add: linear_def)
   507 apply (rule_tac i="f(x)" and j="f(y)" in Ord_linear_lt)
   508     apply (simp_all add: Ordf)
   509 apply (blast intro: inj)
   510 done
   511 
   512 lemma wf_on_measure: "wf[B](measure(A,f))"
   513 by (rule wf_imp_wf_on [OF wf_measure])
   514 
   515 lemma well_ord_measure:
   516  assumes Ordf: "!!x. x \<in> A ==> Ord(f(x))"
   517      and inj:  "!!x y. [|x \<in> A; y \<in> A; f(x) = f(y) |] ==> x=y"
   518  shows "well_ord(A, measure(A,f))"
   519 apply (rule well_ordI)
   520 apply (rule wf_on_measure)
   521 apply (blast intro: linear_measure Ordf inj)
   522 done
   523 
   524 lemma measure_type: "measure(A,f) \<subseteq> A*A"
   525 by (auto simp add: measure_def)
   526 
   527 subsubsection{*Well-foundedness of Unions*}
   528 
   529 lemma wf_on_Union:
   530  assumes wfA: "wf[A](r)"
   531      and wfB: "!!a. a\<in>A ==> wf[B(a)](s)"
   532      and ok: "!!a u v. [|<u,v> \<in> s; v \<in> B(a); a \<in> A|]
   533                        ==> (\<exists>a'\<in>A. <a',a> \<in> r & u \<in> B(a')) | u \<in> B(a)"
   534  shows "wf[\<Union>a\<in>A. B(a)](s)"
   535 apply (rule wf_onI2)
   536 apply (erule UN_E)
   537 apply (subgoal_tac "\<forall>z \<in> B(a). z \<in> Ba", blast)
   538 apply (rule_tac a = a in wf_on_induct [OF wfA], assumption)
   539 apply (rule ballI)
   540 apply (rule_tac a = z in wf_on_induct [OF wfB], assumption, assumption)
   541 apply (rename_tac u)
   542 apply (drule_tac x=u in bspec, blast)
   543 apply (erule mp, clarify)
   544 apply (frule ok, assumption+, blast)
   545 done
   546 
   547 subsubsection{*Bijections involving Powersets*}
   548 
   549 lemma Pow_sum_bij:
   550     "(\<lambda>Z \<in> Pow(A+B). <{x \<in> A. Inl(x) \<in> Z}, {y \<in> B. Inr(y) \<in> Z}>)
   551      \<in> bij(Pow(A+B), Pow(A)*Pow(B))"
   552 apply (rule_tac d = "%<X,Y>. {Inl (x). x \<in> X} \<union> {Inr (y). y \<in> Y}"
   553        in lam_bijective)
   554 apply force+
   555 done
   556 
   557 text{*As a special case, we have @{term "bij(Pow(A*B), A -> Pow(B))"} *}
   558 lemma Pow_Sigma_bij:
   559     "(\<lambda>r \<in> Pow(Sigma(A,B)). \<lambda>x \<in> A. r``{x})
   560      \<in> bij(Pow(Sigma(A,B)), \<Pi> x \<in> A. Pow(B(x)))"
   561 apply (rule_tac d = "%f. \<Union>x \<in> A. \<Union>y \<in> f`x. {<x,y>}" in lam_bijective)
   562 apply (blast intro: lam_type)
   563 apply (blast dest: apply_type, simp_all)
   564 apply fast (*strange, but blast can't do it*)
   565 apply (rule fun_extension, auto)
   566 by blast
   567 
   568 end