src/ZF/WF.thy
changeset 76213 e44d86131648
parent 71085 950e1cfe0fe4
child 76215 a642599ffdea
equal deleted inserted replaced
76212:f2094906e491 76213:e44d86131648
    19 theory WF imports Trancl begin
    19 theory WF imports Trancl begin
    20 
    20 
    21 definition
    21 definition
    22   wf           :: "i=>o"  where
    22   wf           :: "i=>o"  where
    23     (*r is a well-founded relation*)
    23     (*r is a well-founded relation*)
    24     "wf(r) == \<forall>Z. Z=0 | (\<exists>x\<in>Z. \<forall>y. <y,x>:r \<longrightarrow> ~ y \<in> Z)"
    24     "wf(r) \<equiv> \<forall>Z. Z=0 | (\<exists>x\<in>Z. \<forall>y. <y,x>:r \<longrightarrow> \<not> y \<in> Z)"
    25 
    25 
    26 definition
    26 definition
    27   wf_on        :: "[i,i]=>o"                      (\<open>wf[_]'(_')\<close>)  where
    27   wf_on        :: "[i,i]=>o"                      (\<open>wf[_]'(_')\<close>)  where
    28     (*r is well-founded on A*)
    28     (*r is well-founded on A*)
    29     "wf_on(A,r) == wf(r \<inter> A*A)"
    29     "wf_on(A,r) \<equiv> wf(r \<inter> A*A)"
    30 
    30 
    31 definition
    31 definition
    32   is_recfun    :: "[i, i, [i,i]=>i, i] =>o"  where
    32   is_recfun    :: "[i, i, [i,i]=>i, i] =>o"  where
    33     "is_recfun(r,a,H,f) == (f = (\<lambda>x\<in>r-``{a}. H(x, restrict(f, r-``{x}))))"
    33     "is_recfun(r,a,H,f) \<equiv> (f = (\<lambda>x\<in>r-``{a}. H(x, restrict(f, r-``{x}))))"
    34 
    34 
    35 definition
    35 definition
    36   the_recfun   :: "[i, i, [i,i]=>i] =>i"  where
    36   the_recfun   :: "[i, i, [i,i]=>i] =>i"  where
    37     "the_recfun(r,a,H) == (THE f. is_recfun(r,a,H,f))"
    37     "the_recfun(r,a,H) \<equiv> (THE f. is_recfun(r,a,H,f))"
    38 
    38 
    39 definition
    39 definition
    40   wftrec :: "[i, i, [i,i]=>i] =>i"  where
    40   wftrec :: "[i, i, [i,i]=>i] =>i"  where
    41     "wftrec(r,a,H) == H(a, the_recfun(r,a,H))"
    41     "wftrec(r,a,H) \<equiv> H(a, the_recfun(r,a,H))"
    42 
    42 
    43 definition
    43 definition
    44   wfrec :: "[i, i, [i,i]=>i] =>i"  where
    44   wfrec :: "[i, i, [i,i]=>i] =>i"  where
    45     (*public version.  Does not require r to be transitive*)
    45     (*public version.  Does not require r to be transitive*)
    46     "wfrec(r,a,H) == wftrec(r^+, a, %x f. H(x, restrict(f,r-``{x})))"
    46     "wfrec(r,a,H) \<equiv> wftrec(r^+, a, %x f. H(x, restrict(f,r-``{x})))"
    47 
    47 
    48 definition
    48 definition
    49   wfrec_on     :: "[i, i, i, [i,i]=>i] =>i"       (\<open>wfrec[_]'(_,_,_')\<close>)  where
    49   wfrec_on     :: "[i, i, i, [i,i]=>i] =>i"       (\<open>wfrec[_]'(_,_,_')\<close>)  where
    50     "wfrec[A](r,a,H) == wfrec(r \<inter> A*A, a, H)"
    50     "wfrec[A](r,a,H) \<equiv> wfrec(r \<inter> A*A, a, H)"
    51 
    51 
    52 
    52 
    53 subsection\<open>Well-Founded Relations\<close>
    53 subsection\<open>Well-Founded Relations\<close>
    54 
    54 
    55 subsubsection\<open>Equivalences between \<^term>\<open>wf\<close> and \<^term>\<open>wf_on\<close>\<close>
    55 subsubsection\<open>Equivalences between \<^term>\<open>wf\<close> and \<^term>\<open>wf_on\<close>\<close>
    56 
    56 
    57 lemma wf_imp_wf_on: "wf(r) ==> wf[A](r)"
    57 lemma wf_imp_wf_on: "wf(r) \<Longrightarrow> wf[A](r)"
    58 by (unfold wf_def wf_on_def, force)
    58 by (unfold wf_def wf_on_def, force)
    59 
    59 
    60 lemma wf_on_imp_wf: "[|wf[A](r); r \<subseteq> A*A|] ==> wf(r)"
    60 lemma wf_on_imp_wf: "\<lbrakk>wf[A](r); r \<subseteq> A*A\<rbrakk> \<Longrightarrow> wf(r)"
    61 by (simp add: wf_on_def subset_Int_iff)
    61 by (simp add: wf_on_def subset_Int_iff)
    62 
    62 
    63 lemma wf_on_field_imp_wf: "wf[field(r)](r) ==> wf(r)"
    63 lemma wf_on_field_imp_wf: "wf[field(r)](r) \<Longrightarrow> wf(r)"
    64 by (unfold wf_def wf_on_def, fast)
    64 by (unfold wf_def wf_on_def, fast)
    65 
    65 
    66 lemma wf_iff_wf_on_field: "wf(r) \<longleftrightarrow> wf[field(r)](r)"
    66 lemma wf_iff_wf_on_field: "wf(r) \<longleftrightarrow> wf[field(r)](r)"
    67 by (blast intro: wf_imp_wf_on wf_on_field_imp_wf)
    67 by (blast intro: wf_imp_wf_on wf_on_field_imp_wf)
    68 
    68 
    69 lemma wf_on_subset_A: "[| wf[A](r);  B<=A |] ==> wf[B](r)"
    69 lemma wf_on_subset_A: "\<lbrakk>wf[A](r);  B<=A\<rbrakk> \<Longrightarrow> wf[B](r)"
    70 by (unfold wf_on_def wf_def, fast)
    70 by (unfold wf_on_def wf_def, fast)
    71 
    71 
    72 lemma wf_on_subset_r: "[| wf[A](r); s<=r |] ==> wf[A](s)"
    72 lemma wf_on_subset_r: "\<lbrakk>wf[A](r); s<=r\<rbrakk> \<Longrightarrow> wf[A](s)"
    73 by (unfold wf_on_def wf_def, fast)
    73 by (unfold wf_on_def wf_def, fast)
    74 
    74 
    75 lemma wf_subset: "[|wf(s); r<=s|] ==> wf(r)"
    75 lemma wf_subset: "\<lbrakk>wf(s); r<=s\<rbrakk> \<Longrightarrow> wf(r)"
    76 by (simp add: wf_def, fast)
    76 by (simp add: wf_def, fast)
    77 
    77 
    78 subsubsection\<open>Introduction Rules for \<^term>\<open>wf_on\<close>\<close>
    78 subsubsection\<open>Introduction Rules for \<^term>\<open>wf_on\<close>\<close>
    79 
    79 
    80 text\<open>If every non-empty subset of \<^term>\<open>A\<close> has an \<^term>\<open>r\<close>-minimal element
    80 text\<open>If every non-empty subset of \<^term>\<open>A\<close> has an \<^term>\<open>r\<close>-minimal element
    81    then we have \<^term>\<open>wf[A](r)\<close>.\<close>
    81    then we have \<^term>\<open>wf[A](r)\<close>.\<close>
    82 lemma wf_onI:
    82 lemma wf_onI:
    83  assumes prem: "!!Z u. [| Z<=A;  u \<in> Z;  \<forall>x\<in>Z. \<exists>y\<in>Z. <y,x>:r |] ==> False"
    83  assumes prem: "\<And>Z u. \<lbrakk>Z<=A;  u \<in> Z;  \<forall>x\<in>Z. \<exists>y\<in>Z. <y,x>:r\<rbrakk> \<Longrightarrow> False"
    84  shows         "wf[A](r)"
    84  shows         "wf[A](r)"
    85 apply (unfold wf_on_def wf_def)
    85 apply (unfold wf_on_def wf_def)
    86 apply (rule equals0I [THEN disjCI, THEN allI])
    86 apply (rule equals0I [THEN disjCI, THEN allI])
    87 apply (rule_tac Z = Z in prem, blast+)
    87 apply (rule_tac Z = Z in prem, blast+)
    88 done
    88 done
    89 
    89 
    90 text\<open>If \<^term>\<open>r\<close> allows well-founded induction over \<^term>\<open>A\<close>
    90 text\<open>If \<^term>\<open>r\<close> allows well-founded induction over \<^term>\<open>A\<close>
    91    then we have \<^term>\<open>wf[A](r)\<close>.   Premise is equivalent to
    91    then we have \<^term>\<open>wf[A](r)\<close>.   Premise is equivalent to
    92   \<^prop>\<open>!!B. \<forall>x\<in>A. (\<forall>y. <y,x>: r \<longrightarrow> y \<in> B) \<longrightarrow> x \<in> B ==> A<=B\<close>\<close>
    92   \<^prop>\<open>\<And>B. \<forall>x\<in>A. (\<forall>y. <y,x>: r \<longrightarrow> y \<in> B) \<longrightarrow> x \<in> B \<Longrightarrow> A<=B\<close>\<close>
    93 lemma wf_onI2:
    93 lemma wf_onI2:
    94  assumes prem: "!!y B. [| \<forall>x\<in>A. (\<forall>y\<in>A. <y,x>:r \<longrightarrow> y \<in> B) \<longrightarrow> x \<in> B;   y \<in> A |]
    94  assumes prem: "\<And>y B. \<lbrakk>\<forall>x\<in>A. (\<forall>y\<in>A. <y,x>:r \<longrightarrow> y \<in> B) \<longrightarrow> x \<in> B;   y \<in> A\<rbrakk>
    95                        ==> y \<in> B"
    95                        \<Longrightarrow> y \<in> B"
    96  shows         "wf[A](r)"
    96  shows         "wf[A](r)"
    97 apply (rule wf_onI)
    97 apply (rule wf_onI)
    98 apply (rule_tac c=u in prem [THEN DiffE])
    98 apply (rule_tac c=u in prem [THEN DiffE])
    99   prefer 3 apply blast
    99   prefer 3 apply blast
   100  apply fast+
   100  apply fast+
   104 subsubsection\<open>Well-founded Induction\<close>
   104 subsubsection\<open>Well-founded Induction\<close>
   105 
   105 
   106 text\<open>Consider the least \<^term>\<open>z\<close> in \<^term>\<open>domain(r)\<close> such that
   106 text\<open>Consider the least \<^term>\<open>z\<close> in \<^term>\<open>domain(r)\<close> such that
   107   \<^term>\<open>P(z)\<close> does not hold...\<close>
   107   \<^term>\<open>P(z)\<close> does not hold...\<close>
   108 lemma wf_induct_raw:
   108 lemma wf_induct_raw:
   109     "[| wf(r);
   109     "\<lbrakk>wf(r);
   110         !!x.[| \<forall>y. <y,x>: r \<longrightarrow> P(y) |] ==> P(x) |]
   110         \<And>x.\<lbrakk>\<forall>y. <y,x>: r \<longrightarrow> P(y)\<rbrakk> \<Longrightarrow> P(x)\<rbrakk>
   111      ==> P(a)"
   111      \<Longrightarrow> P(a)"
   112 apply (unfold wf_def)
   112 apply (unfold wf_def)
   113 apply (erule_tac x = "{z \<in> domain(r). ~ P(z)}" in allE)
   113 apply (erule_tac x = "{z \<in> domain(r). \<not> P(z)}" in allE)
   114 apply blast
   114 apply blast
   115 done
   115 done
   116 
   116 
   117 lemmas wf_induct = wf_induct_raw [rule_format, consumes 1, case_names step, induct set: wf]
   117 lemmas wf_induct = wf_induct_raw [rule_format, consumes 1, case_names step, induct set: wf]
   118 
   118 
   119 text\<open>The form of this rule is designed to match \<open>wfI\<close>\<close>
   119 text\<open>The form of this rule is designed to match \<open>wfI\<close>\<close>
   120 lemma wf_induct2:
   120 lemma wf_induct2:
   121     "[| wf(r);  a \<in> A;  field(r)<=A;
   121     "\<lbrakk>wf(r);  a \<in> A;  field(r)<=A;
   122         !!x.[| x \<in> A;  \<forall>y. <y,x>: r \<longrightarrow> P(y) |] ==> P(x) |]
   122         \<And>x.\<lbrakk>x \<in> A;  \<forall>y. <y,x>: r \<longrightarrow> P(y)\<rbrakk> \<Longrightarrow> P(x)\<rbrakk>
   123      ==>  P(a)"
   123      \<Longrightarrow>  P(a)"
   124 apply (erule_tac P="a \<in> A" in rev_mp)
   124 apply (erule_tac P="a \<in> A" in rev_mp)
   125 apply (erule_tac a=a in wf_induct, blast)
   125 apply (erule_tac a=a in wf_induct, blast)
   126 done
   126 done
   127 
   127 
   128 lemma field_Int_square: "field(r \<inter> A*A) \<subseteq> A"
   128 lemma field_Int_square: "field(r \<inter> A*A) \<subseteq> A"
   129 by blast
   129 by blast
   130 
   130 
   131 lemma wf_on_induct_raw [consumes 2, induct set: wf_on]:
   131 lemma wf_on_induct_raw [consumes 2, induct set: wf_on]:
   132     "[| wf[A](r);  a \<in> A;
   132     "\<lbrakk>wf[A](r);  a \<in> A;
   133         !!x.[| x \<in> A;  \<forall>y\<in>A. <y,x>: r \<longrightarrow> P(y) |] ==> P(x)
   133         \<And>x.\<lbrakk>x \<in> A;  \<forall>y\<in>A. <y,x>: r \<longrightarrow> P(y)\<rbrakk> \<Longrightarrow> P(x)
   134      |]  ==>  P(a)"
   134 \<rbrakk>  \<Longrightarrow>  P(a)"
   135 apply (unfold wf_on_def)
   135 apply (unfold wf_on_def)
   136 apply (erule wf_induct2, assumption)
   136 apply (erule wf_induct2, assumption)
   137 apply (rule field_Int_square, blast)
   137 apply (rule field_Int_square, blast)
   138 done
   138 done
   139 
   139 
   142   using wf_on_induct_raw [of A r a P] by simp
   142   using wf_on_induct_raw [of A r a P] by simp
   143 
   143 
   144 text\<open>If \<^term>\<open>r\<close> allows well-founded induction
   144 text\<open>If \<^term>\<open>r\<close> allows well-founded induction
   145    then we have \<^term>\<open>wf(r)\<close>.\<close>
   145    then we have \<^term>\<open>wf(r)\<close>.\<close>
   146 lemma wfI:
   146 lemma wfI:
   147     "[| field(r)<=A;
   147     "\<lbrakk>field(r)<=A;
   148         !!y B. [| \<forall>x\<in>A. (\<forall>y\<in>A. <y,x>:r \<longrightarrow> y \<in> B) \<longrightarrow> x \<in> B;  y \<in> A|]
   148         \<And>y B. \<lbrakk>\<forall>x\<in>A. (\<forall>y\<in>A. <y,x>:r \<longrightarrow> y \<in> B) \<longrightarrow> x \<in> B;  y \<in> A\<rbrakk>
   149                ==> y \<in> B |]
   149                \<Longrightarrow> y \<in> B\<rbrakk>
   150      ==>  wf(r)"
   150      \<Longrightarrow>  wf(r)"
   151 apply (rule wf_on_subset_A [THEN wf_on_field_imp_wf])
   151 apply (rule wf_on_subset_A [THEN wf_on_field_imp_wf])
   152 apply (rule wf_onI2)
   152 apply (rule wf_onI2)
   153  prefer 2 apply blast
   153  prefer 2 apply blast
   154 apply blast
   154 apply blast
   155 done
   155 done
   156 
   156 
   157 
   157 
   158 subsection\<open>Basic Properties of Well-Founded Relations\<close>
   158 subsection\<open>Basic Properties of Well-Founded Relations\<close>
   159 
   159 
   160 lemma wf_not_refl: "wf(r) ==> <a,a> \<notin> r"
   160 lemma wf_not_refl: "wf(r) \<Longrightarrow> <a,a> \<notin> r"
   161 by (erule_tac a=a in wf_induct, blast)
   161 by (erule_tac a=a in wf_induct, blast)
   162 
   162 
   163 lemma wf_not_sym [rule_format]: "wf(r) ==> \<forall>x. <a,x>:r \<longrightarrow> <x,a> \<notin> r"
   163 lemma wf_not_sym [rule_format]: "wf(r) \<Longrightarrow> \<forall>x. <a,x>:r \<longrightarrow> <x,a> \<notin> r"
   164 by (erule_tac a=a in wf_induct, blast)
   164 by (erule_tac a=a in wf_induct, blast)
   165 
   165 
   166 (* @{term"[| wf(r);  <a,x> \<in> r;  ~P ==> <x,a> \<in> r |] ==> P"} *)
   166 (* @{term"\<lbrakk>wf(r);  <a,x> \<in> r;  \<not>P \<Longrightarrow> <x,a> \<in> r\<rbrakk> \<Longrightarrow> P"} *)
   167 lemmas wf_asym = wf_not_sym [THEN swap]
   167 lemmas wf_asym = wf_not_sym [THEN swap]
   168 
   168 
   169 lemma wf_on_not_refl: "[| wf[A](r); a \<in> A |] ==> <a,a> \<notin> r"
   169 lemma wf_on_not_refl: "\<lbrakk>wf[A](r); a \<in> A\<rbrakk> \<Longrightarrow> <a,a> \<notin> r"
   170 by (erule_tac a=a in wf_on_induct, assumption, blast)
   170 by (erule_tac a=a in wf_on_induct, assumption, blast)
   171 
   171 
   172 lemma wf_on_not_sym:
   172 lemma wf_on_not_sym:
   173      "[| wf[A](r);  a \<in> A |] ==> (\<And>b. b\<in>A \<Longrightarrow> <a,b>:r \<Longrightarrow> <b,a>\<notin>r)"
   173      "\<lbrakk>wf[A](r);  a \<in> A\<rbrakk> \<Longrightarrow> (\<And>b. b\<in>A \<Longrightarrow> <a,b>:r \<Longrightarrow> <b,a>\<notin>r)"
   174 apply (atomize (full), intro impI)
   174 apply (atomize (full), intro impI)
   175 apply (erule_tac a=a in wf_on_induct, assumption, blast)
   175 apply (erule_tac a=a in wf_on_induct, assumption, blast)
   176 done
   176 done
   177 
   177 
   178 lemma wf_on_asym:
   178 lemma wf_on_asym:
   179      "[| wf[A](r);  ~Z ==> <a,b> \<in> r;
   179      "\<lbrakk>wf[A](r);  \<not>Z \<Longrightarrow> <a,b> \<in> r;
   180          <b,a> \<notin> r ==> Z; ~Z ==> a \<in> A; ~Z ==> b \<in> A |] ==> Z"
   180          <b,a> \<notin> r \<Longrightarrow> Z; \<not>Z \<Longrightarrow> a \<in> A; \<not>Z \<Longrightarrow> b \<in> A\<rbrakk> \<Longrightarrow> Z"
   181 by (blast dest: wf_on_not_sym)
   181 by (blast dest: wf_on_not_sym)
   182 
   182 
   183 
   183 
   184 (*Needed to prove well_ordI.  Could also reason that wf[A](r) means
   184 (*Needed to prove well_ordI.  Could also reason that wf[A](r) means
   185   wf(r \<inter> A*A);  thus wf( (r \<inter> A*A)^+ ) and use wf_not_refl *)
   185   wf(r \<inter> A*A);  thus wf( (r \<inter> A*A)^+ ) and use wf_not_refl *)
   186 lemma wf_on_chain3:
   186 lemma wf_on_chain3:
   187      "[| wf[A](r); <a,b>:r; <b,c>:r; <c,a>:r; a \<in> A; b \<in> A; c \<in> A |] ==> P"
   187      "\<lbrakk>wf[A](r); <a,b>:r; <b,c>:r; <c,a>:r; a \<in> A; b \<in> A; c \<in> A\<rbrakk> \<Longrightarrow> P"
   188 apply (subgoal_tac "\<forall>y\<in>A. \<forall>z\<in>A. <a,y>:r \<longrightarrow> <y,z>:r \<longrightarrow> <z,a>:r \<longrightarrow> P",
   188 apply (subgoal_tac "\<forall>y\<in>A. \<forall>z\<in>A. <a,y>:r \<longrightarrow> <y,z>:r \<longrightarrow> <z,a>:r \<longrightarrow> P",
   189        blast)
   189        blast)
   190 apply (erule_tac a=a in wf_on_induct, assumption, blast)
   190 apply (erule_tac a=a in wf_on_induct, assumption, blast)
   191 done
   191 done
   192 
   192 
   193 
   193 
   194 
   194 
   195 text\<open>transitive closure of a WF relation is WF provided
   195 text\<open>transitive closure of a WF relation is WF provided
   196   \<^term>\<open>A\<close> is downward closed\<close>
   196   \<^term>\<open>A\<close> is downward closed\<close>
   197 lemma wf_on_trancl:
   197 lemma wf_on_trancl:
   198     "[| wf[A](r);  r-``A \<subseteq> A |] ==> wf[A](r^+)"
   198     "\<lbrakk>wf[A](r);  r-``A \<subseteq> A\<rbrakk> \<Longrightarrow> wf[A](r^+)"
   199 apply (rule wf_onI2)
   199 apply (rule wf_onI2)
   200 apply (frule bspec [THEN mp], assumption+)
   200 apply (frule bspec [THEN mp], assumption+)
   201 apply (erule_tac a = y in wf_on_induct, assumption)
   201 apply (erule_tac a = y in wf_on_induct, assumption)
   202 apply (blast elim: tranclE, blast)
   202 apply (blast elim: tranclE, blast)
   203 done
   203 done
   204 
   204 
   205 lemma wf_trancl: "wf(r) ==> wf(r^+)"
   205 lemma wf_trancl: "wf(r) \<Longrightarrow> wf(r^+)"
   206 apply (simp add: wf_iff_wf_on_field)
   206 apply (simp add: wf_iff_wf_on_field)
   207 apply (rule wf_on_subset_A)
   207 apply (rule wf_on_subset_A)
   208  apply (erule wf_on_trancl)
   208  apply (erule wf_on_trancl)
   209  apply blast
   209  apply blast
   210 apply (rule trancl_type [THEN field_rel_subset])
   210 apply (rule trancl_type [THEN field_rel_subset])
   217 lemmas underD = vimage_singleton_iff [THEN iffD1]
   217 lemmas underD = vimage_singleton_iff [THEN iffD1]
   218 
   218 
   219 
   219 
   220 subsection\<open>The Predicate \<^term>\<open>is_recfun\<close>\<close>
   220 subsection\<open>The Predicate \<^term>\<open>is_recfun\<close>\<close>
   221 
   221 
   222 lemma is_recfun_type: "is_recfun(r,a,H,f) ==> f \<in> r-``{a} -> range(f)"
   222 lemma is_recfun_type: "is_recfun(r,a,H,f) \<Longrightarrow> f \<in> r-``{a} -> range(f)"
   223 apply (unfold is_recfun_def)
   223 apply (unfold is_recfun_def)
   224 apply (erule ssubst)
   224 apply (erule ssubst)
   225 apply (rule lamI [THEN rangeI, THEN lam_type], assumption)
   225 apply (rule lamI [THEN rangeI, THEN lam_type], assumption)
   226 done
   226 done
   227 
   227 
   228 lemmas is_recfun_imp_function = is_recfun_type [THEN fun_is_function]
   228 lemmas is_recfun_imp_function = is_recfun_type [THEN fun_is_function]
   229 
   229 
   230 lemma apply_recfun:
   230 lemma apply_recfun:
   231     "[| is_recfun(r,a,H,f); <x,a>:r |] ==> f`x = H(x, restrict(f,r-``{x}))"
   231     "\<lbrakk>is_recfun(r,a,H,f); <x,a>:r\<rbrakk> \<Longrightarrow> f`x = H(x, restrict(f,r-``{x}))"
   232 apply (unfold is_recfun_def)
   232 apply (unfold is_recfun_def)
   233   txt\<open>replace f only on the left-hand side\<close>
   233   txt\<open>replace f only on the left-hand side\<close>
   234 apply (erule_tac P = "%x. t(x) = u" for t u in ssubst)
   234 apply (erule_tac P = "%x. t(x) = u" for t u in ssubst)
   235 apply (simp add: underI)
   235 apply (simp add: underI)
   236 done
   236 done
   237 
   237 
   238 lemma is_recfun_equal [rule_format]:
   238 lemma is_recfun_equal [rule_format]:
   239      "[| wf(r);  trans(r);  is_recfun(r,a,H,f);  is_recfun(r,b,H,g) |]
   239      "\<lbrakk>wf(r);  trans(r);  is_recfun(r,a,H,f);  is_recfun(r,b,H,g)\<rbrakk>
   240       ==> <x,a>:r \<longrightarrow> <x,b>:r \<longrightarrow> f`x=g`x"
   240       \<Longrightarrow> <x,a>:r \<longrightarrow> <x,b>:r \<longrightarrow> f`x=g`x"
   241 apply (frule_tac f = f in is_recfun_type)
   241 apply (frule_tac f = f in is_recfun_type)
   242 apply (frule_tac f = g in is_recfun_type)
   242 apply (frule_tac f = g in is_recfun_type)
   243 apply (simp add: is_recfun_def)
   243 apply (simp add: is_recfun_def)
   244 apply (erule_tac a=x in wf_induct)
   244 apply (erule_tac a=x in wf_induct)
   245 apply (intro impI)
   245 apply (intro impI)
   251 apply (simp add: apply_iff)
   251 apply (simp add: apply_iff)
   252 apply (blast dest: transD intro: sym)
   252 apply (blast dest: transD intro: sym)
   253 done
   253 done
   254 
   254 
   255 lemma is_recfun_cut:
   255 lemma is_recfun_cut:
   256      "[| wf(r);  trans(r);
   256      "\<lbrakk>wf(r);  trans(r);
   257          is_recfun(r,a,H,f);  is_recfun(r,b,H,g);  <b,a>:r |]
   257          is_recfun(r,a,H,f);  is_recfun(r,b,H,g);  <b,a>:r\<rbrakk>
   258       ==> restrict(f, r-``{b}) = g"
   258       \<Longrightarrow> restrict(f, r-``{b}) = g"
   259 apply (frule_tac f = f in is_recfun_type)
   259 apply (frule_tac f = f in is_recfun_type)
   260 apply (rule fun_extension)
   260 apply (rule fun_extension)
   261   apply (blast dest: transD intro: restrict_type2)
   261   apply (blast dest: transD intro: restrict_type2)
   262  apply (erule is_recfun_type, simp)
   262  apply (erule is_recfun_type, simp)
   263 apply (blast dest: transD intro: is_recfun_equal)
   263 apply (blast dest: transD intro: is_recfun_equal)
   264 done
   264 done
   265 
   265 
   266 subsection\<open>Recursion: Main Existence Lemma\<close>
   266 subsection\<open>Recursion: Main Existence Lemma\<close>
   267 
   267 
   268 lemma is_recfun_functional:
   268 lemma is_recfun_functional:
   269      "[| wf(r); trans(r); is_recfun(r,a,H,f); is_recfun(r,a,H,g) |]  ==>  f=g"
   269      "\<lbrakk>wf(r); trans(r); is_recfun(r,a,H,f); is_recfun(r,a,H,g)\<rbrakk>  \<Longrightarrow>  f=g"
   270 by (blast intro: fun_extension is_recfun_type is_recfun_equal)
   270 by (blast intro: fun_extension is_recfun_type is_recfun_equal)
   271 
   271 
   272 lemma the_recfun_eq:
   272 lemma the_recfun_eq:
   273     "[| is_recfun(r,a,H,f);  wf(r);  trans(r) |] ==> the_recfun(r,a,H) = f"
   273     "\<lbrakk>is_recfun(r,a,H,f);  wf(r);  trans(r)\<rbrakk> \<Longrightarrow> the_recfun(r,a,H) = f"
   274 apply (unfold the_recfun_def)
   274 apply (unfold the_recfun_def)
   275 apply (blast intro: is_recfun_functional)
   275 apply (blast intro: is_recfun_functional)
   276 done
   276 done
   277 
   277 
   278 (*If some f satisfies is_recfun(r,a,H,-) then so does the_recfun(r,a,H) *)
   278 (*If some f satisfies is_recfun(r,a,H,-) then so does the_recfun(r,a,H) *)
   279 lemma is_the_recfun:
   279 lemma is_the_recfun:
   280     "[| is_recfun(r,a,H,f);  wf(r);  trans(r) |]
   280     "\<lbrakk>is_recfun(r,a,H,f);  wf(r);  trans(r)\<rbrakk>
   281      ==> is_recfun(r, a, H, the_recfun(r,a,H))"
   281      \<Longrightarrow> is_recfun(r, a, H, the_recfun(r,a,H))"
   282 by (simp add: the_recfun_eq)
   282 by (simp add: the_recfun_eq)
   283 
   283 
   284 lemma unfold_the_recfun:
   284 lemma unfold_the_recfun:
   285      "[| wf(r);  trans(r) |] ==> is_recfun(r, a, H, the_recfun(r,a,H))"
   285      "\<lbrakk>wf(r);  trans(r)\<rbrakk> \<Longrightarrow> is_recfun(r, a, H, the_recfun(r,a,H))"
   286 apply (rule_tac a=a in wf_induct, assumption)
   286 apply (rule_tac a=a in wf_induct, assumption)
   287 apply (rename_tac a1)
   287 apply (rename_tac a1)
   288 apply (rule_tac f = "\<lambda>y\<in>r-``{a1}. wftrec (r,y,H)" in is_the_recfun)
   288 apply (rule_tac f = "\<lambda>y\<in>r-``{a1}. wftrec (r,y,H)" in is_the_recfun)
   289   apply typecheck
   289   apply typecheck
   290 apply (unfold is_recfun_def wftrec_def)
   290 apply (unfold is_recfun_def wftrec_def)
   309 
   309 
   310 
   310 
   311 subsection\<open>Unfolding \<^term>\<open>wftrec(r,a,H)\<close>\<close>
   311 subsection\<open>Unfolding \<^term>\<open>wftrec(r,a,H)\<close>\<close>
   312 
   312 
   313 lemma the_recfun_cut:
   313 lemma the_recfun_cut:
   314      "[| wf(r);  trans(r);  <b,a>:r |]
   314      "\<lbrakk>wf(r);  trans(r);  <b,a>:r\<rbrakk>
   315       ==> restrict(the_recfun(r,a,H), r-``{b}) = the_recfun(r,b,H)"
   315       \<Longrightarrow> restrict(the_recfun(r,a,H), r-``{b}) = the_recfun(r,b,H)"
   316 by (blast intro: is_recfun_cut unfold_the_recfun)
   316 by (blast intro: is_recfun_cut unfold_the_recfun)
   317 
   317 
   318 (*NOT SUITABLE FOR REWRITING: it is recursive!*)
   318 (*NOT SUITABLE FOR REWRITING: it is recursive!*)
   319 lemma wftrec:
   319 lemma wftrec:
   320     "[| wf(r);  trans(r) |] ==>
   320     "\<lbrakk>wf(r);  trans(r)\<rbrakk> \<Longrightarrow>
   321           wftrec(r,a,H) = H(a, \<lambda>x\<in>r-``{a}. wftrec(r,x,H))"
   321           wftrec(r,a,H) = H(a, \<lambda>x\<in>r-``{a}. wftrec(r,x,H))"
   322 apply (unfold wftrec_def)
   322 apply (unfold wftrec_def)
   323 apply (subst unfold_the_recfun [unfolded is_recfun_def])
   323 apply (subst unfold_the_recfun [unfolded is_recfun_def])
   324 apply (simp_all add: vimage_singleton_iff [THEN iff_sym] the_recfun_cut)
   324 apply (simp_all add: vimage_singleton_iff [THEN iff_sym] the_recfun_cut)
   325 done
   325 done
   327 
   327 
   328 subsubsection\<open>Removal of the Premise \<^term>\<open>trans(r)\<close>\<close>
   328 subsubsection\<open>Removal of the Premise \<^term>\<open>trans(r)\<close>\<close>
   329 
   329 
   330 (*NOT SUITABLE FOR REWRITING: it is recursive!*)
   330 (*NOT SUITABLE FOR REWRITING: it is recursive!*)
   331 lemma wfrec:
   331 lemma wfrec:
   332     "wf(r) ==> wfrec(r,a,H) = H(a, \<lambda>x\<in>r-``{a}. wfrec(r,x,H))"
   332     "wf(r) \<Longrightarrow> wfrec(r,a,H) = H(a, \<lambda>x\<in>r-``{a}. wfrec(r,x,H))"
   333 apply (unfold wfrec_def)
   333 apply (unfold wfrec_def)
   334 apply (erule wf_trancl [THEN wftrec, THEN ssubst])
   334 apply (erule wf_trancl [THEN wftrec, THEN ssubst])
   335  apply (rule trans_trancl)
   335  apply (rule trans_trancl)
   336 apply (rule vimage_pair_mono [THEN restrict_lam_eq, THEN subst_context])
   336 apply (rule vimage_pair_mono [THEN restrict_lam_eq, THEN subst_context])
   337  apply (erule r_into_trancl)
   337  apply (erule r_into_trancl)
   338 apply (rule subset_refl)
   338 apply (rule subset_refl)
   339 done
   339 done
   340 
   340 
   341 (*This form avoids giant explosions in proofs.  NOTE USE OF == *)
   341 (*This form avoids giant explosions in proofs.  NOTE USE OF \<equiv> *)
   342 lemma def_wfrec:
   342 lemma def_wfrec:
   343     "[| !!x. h(x)==wfrec(r,x,H);  wf(r) |] ==>
   343     "\<lbrakk>\<And>x. h(x)\<equiv>wfrec(r,x,H);  wf(r)\<rbrakk> \<Longrightarrow>
   344      h(a) = H(a, \<lambda>x\<in>r-``{a}. h(x))"
   344      h(a) = H(a, \<lambda>x\<in>r-``{a}. h(x))"
   345 apply simp
   345 apply simp
   346 apply (elim wfrec)
   346 apply (elim wfrec)
   347 done
   347 done
   348 
   348 
   349 lemma wfrec_type:
   349 lemma wfrec_type:
   350     "[| wf(r);  a \<in> A;  field(r)<=A;
   350     "\<lbrakk>wf(r);  a \<in> A;  field(r)<=A;
   351         !!x u. [| x \<in> A;  u \<in> Pi(r-``{x}, B) |] ==> H(x,u) \<in> B(x)
   351         \<And>x u. \<lbrakk>x \<in> A;  u \<in> Pi(r-``{x}, B)\<rbrakk> \<Longrightarrow> H(x,u) \<in> B(x)
   352      |] ==> wfrec(r,a,H) \<in> B(a)"
   352 \<rbrakk> \<Longrightarrow> wfrec(r,a,H) \<in> B(a)"
   353 apply (rule_tac a = a in wf_induct2, assumption+)
   353 apply (rule_tac a = a in wf_induct2, assumption+)
   354 apply (subst wfrec, assumption)
   354 apply (subst wfrec, assumption)
   355 apply (simp add: lam_type underD)
   355 apply (simp add: lam_type underD)
   356 done
   356 done
   357 
   357 
   358 
   358 
   359 lemma wfrec_on:
   359 lemma wfrec_on:
   360  "[| wf[A](r);  a \<in> A |] ==>
   360  "\<lbrakk>wf[A](r);  a \<in> A\<rbrakk> \<Longrightarrow>
   361          wfrec[A](r,a,H) = H(a, \<lambda>x\<in>(r-``{a}) \<inter> A. wfrec[A](r,x,H))"
   361          wfrec[A](r,a,H) = H(a, \<lambda>x\<in>(r-``{a}) \<inter> A. wfrec[A](r,x,H))"
   362 apply (unfold wf_on_def wfrec_on_def)
   362 apply (unfold wf_on_def wfrec_on_def)
   363 apply (erule wfrec [THEN trans])
   363 apply (erule wfrec [THEN trans])
   364 apply (simp add: vimage_Int_square cons_subset_iff)
   364 apply (simp add: vimage_Int_square cons_subset_iff)
   365 done
   365 done