src/HOL/Wellfounded.thy
author krauss
Fri Apr 25 15:30:33 2008 +0200 (2008-04-25)
changeset 26748 4d51ddd6aa5c
child 26803 0af0f674845d
permissions -rw-r--r--
Merged theories about wellfoundedness into one: Wellfounded.thy
krauss@26748
     1
(*  ID:         $Id$
krauss@26748
     2
    Author:     Tobias Nipkow
krauss@26748
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
krauss@26748
     4
    Author:     Konrad Slind, Alexander Krauss
krauss@26748
     5
    Copyright   1992-2008  University of Cambridge and TU Muenchen
krauss@26748
     6
*)
krauss@26748
     7
krauss@26748
     8
header {*Well-founded Recursion*}
krauss@26748
     9
krauss@26748
    10
theory Wellfounded
krauss@26748
    11
imports Finite_Set Nat
krauss@26748
    12
uses ("Tools/function_package/size.ML")
krauss@26748
    13
begin
krauss@26748
    14
krauss@26748
    15
inductive
krauss@26748
    16
  wfrec_rel :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => 'a => 'b => bool"
krauss@26748
    17
  for R :: "('a * 'a) set"
krauss@26748
    18
  and F :: "('a => 'b) => 'a => 'b"
krauss@26748
    19
where
krauss@26748
    20
  wfrecI: "ALL z. (z, x) : R --> wfrec_rel R F z (g z) ==>
krauss@26748
    21
            wfrec_rel R F x (F g x)"
krauss@26748
    22
krauss@26748
    23
constdefs
krauss@26748
    24
  wf         :: "('a * 'a)set => bool"
krauss@26748
    25
  "wf(r) == (!P. (!x. (!y. (y,x):r --> P(y)) --> P(x)) --> (!x. P(x)))"
krauss@26748
    26
krauss@26748
    27
  wfP :: "('a => 'a => bool) => bool"
krauss@26748
    28
  "wfP r == wf {(x, y). r x y}"
krauss@26748
    29
krauss@26748
    30
  acyclic :: "('a*'a)set => bool"
krauss@26748
    31
  "acyclic r == !x. (x,x) ~: r^+"
krauss@26748
    32
krauss@26748
    33
  cut        :: "('a => 'b) => ('a * 'a)set => 'a => 'a => 'b"
krauss@26748
    34
  "cut f r x == (%y. if (y,x):r then f y else arbitrary)"
krauss@26748
    35
krauss@26748
    36
  adm_wf :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => bool"
krauss@26748
    37
  "adm_wf R F == ALL f g x.
krauss@26748
    38
     (ALL z. (z, x) : R --> f z = g z) --> F f x = F g x"
krauss@26748
    39
krauss@26748
    40
  wfrec :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => 'a => 'b"
krauss@26748
    41
  [code func del]: "wfrec R F == %x. THE y. wfrec_rel R (%f x. F (cut f R x) x) x y"
krauss@26748
    42
krauss@26748
    43
abbreviation acyclicP :: "('a => 'a => bool) => bool" where
krauss@26748
    44
  "acyclicP r == acyclic {(x, y). r x y}"
krauss@26748
    45
krauss@26748
    46
class wellorder = linorder +
krauss@26748
    47
  assumes wf: "wf {(x, y). x < y}"
krauss@26748
    48
krauss@26748
    49
krauss@26748
    50
lemma wfP_wf_eq [pred_set_conv]: "wfP (\<lambda>x y. (x, y) \<in> r) = wf r"
krauss@26748
    51
  by (simp add: wfP_def)
krauss@26748
    52
krauss@26748
    53
lemma wfUNIVI: 
krauss@26748
    54
   "(!!P x. (ALL x. (ALL y. (y,x) : r --> P(y)) --> P(x)) ==> P(x)) ==> wf(r)"
krauss@26748
    55
  unfolding wf_def by blast
krauss@26748
    56
krauss@26748
    57
lemmas wfPUNIVI = wfUNIVI [to_pred]
krauss@26748
    58
krauss@26748
    59
text{*Restriction to domain @{term A} and range @{term B}.  If @{term r} is
krauss@26748
    60
    well-founded over their intersection, then @{term "wf r"}*}
krauss@26748
    61
lemma wfI: 
krauss@26748
    62
 "[| r \<subseteq> A <*> B; 
krauss@26748
    63
     !!x P. [|\<forall>x. (\<forall>y. (y,x) : r --> P y) --> P x;  x : A; x : B |] ==> P x |]
krauss@26748
    64
  ==>  wf r"
krauss@26748
    65
  unfolding wf_def by blast
krauss@26748
    66
krauss@26748
    67
lemma wf_induct: 
krauss@26748
    68
    "[| wf(r);           
krauss@26748
    69
        !!x.[| ALL y. (y,x): r --> P(y) |] ==> P(x)  
krauss@26748
    70
     |]  ==>  P(a)"
krauss@26748
    71
  unfolding wf_def by blast
krauss@26748
    72
krauss@26748
    73
lemmas wfP_induct = wf_induct [to_pred]
krauss@26748
    74
krauss@26748
    75
lemmas wf_induct_rule = wf_induct [rule_format, consumes 1, case_names less, induct set: wf]
krauss@26748
    76
krauss@26748
    77
lemmas wfP_induct_rule = wf_induct_rule [to_pred, induct set: wfP]
krauss@26748
    78
krauss@26748
    79
lemma wf_not_sym: "wf r ==> (a, x) : r ==> (x, a) ~: r"
krauss@26748
    80
  by (induct a arbitrary: x set: wf) blast
krauss@26748
    81
krauss@26748
    82
(* [| wf r;  ~Z ==> (a,x) : r;  (x,a) ~: r ==> Z |] ==> Z *)
krauss@26748
    83
lemmas wf_asym = wf_not_sym [elim_format]
krauss@26748
    84
krauss@26748
    85
lemma wf_not_refl [simp]: "wf r ==> (a, a) ~: r"
krauss@26748
    86
  by (blast elim: wf_asym)
krauss@26748
    87
krauss@26748
    88
(* [| wf r;  (a,a) ~: r ==> PROP W |] ==> PROP W *)
krauss@26748
    89
lemmas wf_irrefl = wf_not_refl [elim_format]
krauss@26748
    90
krauss@26748
    91
text{*transitive closure of a well-founded relation is well-founded! *}
krauss@26748
    92
lemma wf_trancl:
krauss@26748
    93
  assumes "wf r"
krauss@26748
    94
  shows "wf (r^+)"
krauss@26748
    95
proof -
krauss@26748
    96
  {
krauss@26748
    97
    fix P and x
krauss@26748
    98
    assume induct_step: "!!x. (!!y. (y, x) : r^+ ==> P y) ==> P x"
krauss@26748
    99
    have "P x"
krauss@26748
   100
    proof (rule induct_step)
krauss@26748
   101
      fix y assume "(y, x) : r^+"
krauss@26748
   102
      with `wf r` show "P y"
krauss@26748
   103
      proof (induct x arbitrary: y)
krauss@26748
   104
	case (less x)
krauss@26748
   105
	note hyp = `\<And>x' y'. (x', x) : r ==> (y', x') : r^+ ==> P y'`
krauss@26748
   106
	from `(y, x) : r^+` show "P y"
krauss@26748
   107
	proof cases
krauss@26748
   108
	  case base
krauss@26748
   109
	  show "P y"
krauss@26748
   110
	  proof (rule induct_step)
krauss@26748
   111
	    fix y' assume "(y', y) : r^+"
krauss@26748
   112
	    with `(y, x) : r` show "P y'" by (rule hyp [of y y'])
krauss@26748
   113
	  qed
krauss@26748
   114
	next
krauss@26748
   115
	  case step
krauss@26748
   116
	  then obtain x' where "(x', x) : r" and "(y, x') : r^+" by simp
krauss@26748
   117
	  then show "P y" by (rule hyp [of x' y])
krauss@26748
   118
	qed
krauss@26748
   119
      qed
krauss@26748
   120
    qed
krauss@26748
   121
  } then show ?thesis unfolding wf_def by blast
krauss@26748
   122
qed
krauss@26748
   123
krauss@26748
   124
lemmas wfP_trancl = wf_trancl [to_pred]
krauss@26748
   125
krauss@26748
   126
lemma wf_converse_trancl: "wf (r^-1) ==> wf ((r^+)^-1)"
krauss@26748
   127
  apply (subst trancl_converse [symmetric])
krauss@26748
   128
  apply (erule wf_trancl)
krauss@26748
   129
  done
krauss@26748
   130
krauss@26748
   131
krauss@26748
   132
subsubsection {* Other simple well-foundedness results *}
krauss@26748
   133
krauss@26748
   134
text{*Minimal-element characterization of well-foundedness*}
krauss@26748
   135
lemma wf_eq_minimal: "wf r = (\<forall>Q x. x\<in>Q --> (\<exists>z\<in>Q. \<forall>y. (y,z)\<in>r --> y\<notin>Q))"
krauss@26748
   136
proof (intro iffI strip)
krauss@26748
   137
  fix Q :: "'a set" and x
krauss@26748
   138
  assume "wf r" and "x \<in> Q"
krauss@26748
   139
  then show "\<exists>z\<in>Q. \<forall>y. (y, z) \<in> r \<longrightarrow> y \<notin> Q"
krauss@26748
   140
    unfolding wf_def
krauss@26748
   141
    by (blast dest: spec [of _ "%x. x\<in>Q \<longrightarrow> (\<exists>z\<in>Q. \<forall>y. (y,z) \<in> r \<longrightarrow> y\<notin>Q)"]) 
krauss@26748
   142
next
krauss@26748
   143
  assume 1: "\<forall>Q x. x \<in> Q \<longrightarrow> (\<exists>z\<in>Q. \<forall>y. (y, z) \<in> r \<longrightarrow> y \<notin> Q)"
krauss@26748
   144
  show "wf r"
krauss@26748
   145
  proof (rule wfUNIVI)
krauss@26748
   146
    fix P :: "'a \<Rightarrow> bool" and x
krauss@26748
   147
    assume 2: "\<forall>x. (\<forall>y. (y, x) \<in> r \<longrightarrow> P y) \<longrightarrow> P x"
krauss@26748
   148
    let ?Q = "{x. \<not> P x}"
krauss@26748
   149
    have "x \<in> ?Q \<longrightarrow> (\<exists>z \<in> ?Q. \<forall>y. (y, z) \<in> r \<longrightarrow> y \<notin> ?Q)"
krauss@26748
   150
      by (rule 1 [THEN spec, THEN spec])
krauss@26748
   151
    then have "\<not> P x \<longrightarrow> (\<exists>z. \<not> P z \<and> (\<forall>y. (y, z) \<in> r \<longrightarrow> P y))" by simp
krauss@26748
   152
    with 2 have "\<not> P x \<longrightarrow> (\<exists>z. \<not> P z \<and> P z)" by fast
krauss@26748
   153
    then show "P x" by simp
krauss@26748
   154
  qed
krauss@26748
   155
qed
krauss@26748
   156
krauss@26748
   157
lemma wfE_min: 
krauss@26748
   158
  assumes "wf R" "x \<in> Q"
krauss@26748
   159
  obtains z where "z \<in> Q" "\<And>y. (y, z) \<in> R \<Longrightarrow> y \<notin> Q"
krauss@26748
   160
  using assms unfolding wf_eq_minimal by blast
krauss@26748
   161
krauss@26748
   162
lemma wfI_min:
krauss@26748
   163
  "(\<And>x Q. x \<in> Q \<Longrightarrow> \<exists>z\<in>Q. \<forall>y. (y, z) \<in> R \<longrightarrow> y \<notin> Q)
krauss@26748
   164
  \<Longrightarrow> wf R"
krauss@26748
   165
  unfolding wf_eq_minimal by blast
krauss@26748
   166
krauss@26748
   167
lemmas wfP_eq_minimal = wf_eq_minimal [to_pred]
krauss@26748
   168
krauss@26748
   169
text {* Well-foundedness of subsets *}
krauss@26748
   170
lemma wf_subset: "[| wf(r);  p<=r |] ==> wf(p)"
krauss@26748
   171
  apply (simp (no_asm_use) add: wf_eq_minimal)
krauss@26748
   172
  apply fast
krauss@26748
   173
  done
krauss@26748
   174
krauss@26748
   175
lemmas wfP_subset = wf_subset [to_pred]
krauss@26748
   176
krauss@26748
   177
text {* Well-foundedness of the empty relation *}
krauss@26748
   178
lemma wf_empty [iff]: "wf({})"
krauss@26748
   179
  by (simp add: wf_def)
krauss@26748
   180
krauss@26748
   181
lemmas wfP_empty [iff] =
krauss@26748
   182
  wf_empty [to_pred bot_empty_eq2, simplified bot_fun_eq bot_bool_eq]
krauss@26748
   183
krauss@26748
   184
lemma wf_Int1: "wf r ==> wf (r Int r')"
krauss@26748
   185
  apply (erule wf_subset)
krauss@26748
   186
  apply (rule Int_lower1)
krauss@26748
   187
  done
krauss@26748
   188
krauss@26748
   189
lemma wf_Int2: "wf r ==> wf (r' Int r)"
krauss@26748
   190
  apply (erule wf_subset)
krauss@26748
   191
  apply (rule Int_lower2)
krauss@26748
   192
  done  
krauss@26748
   193
krauss@26748
   194
text{*Well-foundedness of insert*}
krauss@26748
   195
lemma wf_insert [iff]: "wf(insert (y,x) r) = (wf(r) & (x,y) ~: r^*)"
krauss@26748
   196
apply (rule iffI)
krauss@26748
   197
 apply (blast elim: wf_trancl [THEN wf_irrefl]
krauss@26748
   198
              intro: rtrancl_into_trancl1 wf_subset 
krauss@26748
   199
                     rtrancl_mono [THEN [2] rev_subsetD])
krauss@26748
   200
apply (simp add: wf_eq_minimal, safe)
krauss@26748
   201
apply (rule allE, assumption, erule impE, blast) 
krauss@26748
   202
apply (erule bexE)
krauss@26748
   203
apply (rename_tac "a", case_tac "a = x")
krauss@26748
   204
 prefer 2
krauss@26748
   205
apply blast 
krauss@26748
   206
apply (case_tac "y:Q")
krauss@26748
   207
 prefer 2 apply blast
krauss@26748
   208
apply (rule_tac x = "{z. z:Q & (z,y) : r^*}" in allE)
krauss@26748
   209
 apply assumption
krauss@26748
   210
apply (erule_tac V = "ALL Q. (EX x. x : Q) --> ?P Q" in thin_rl) 
krauss@26748
   211
  --{*essential for speed*}
krauss@26748
   212
txt{*Blast with new substOccur fails*}
krauss@26748
   213
apply (fast intro: converse_rtrancl_into_rtrancl)
krauss@26748
   214
done
krauss@26748
   215
krauss@26748
   216
text{*Well-foundedness of image*}
krauss@26748
   217
lemma wf_prod_fun_image: "[| wf r; inj f |] ==> wf(prod_fun f f ` r)"
krauss@26748
   218
apply (simp only: wf_eq_minimal, clarify)
krauss@26748
   219
apply (case_tac "EX p. f p : Q")
krauss@26748
   220
apply (erule_tac x = "{p. f p : Q}" in allE)
krauss@26748
   221
apply (fast dest: inj_onD, blast)
krauss@26748
   222
done
krauss@26748
   223
krauss@26748
   224
krauss@26748
   225
subsubsection {* Well-Foundedness Results for Unions *}
krauss@26748
   226
krauss@26748
   227
lemma wf_union_compatible:
krauss@26748
   228
  assumes "wf R" "wf S"
krauss@26748
   229
  assumes "S O R \<subseteq> R"
krauss@26748
   230
  shows "wf (R \<union> S)"
krauss@26748
   231
proof (rule wfI_min)
krauss@26748
   232
  fix x :: 'a and Q 
krauss@26748
   233
  let ?Q' = "{x \<in> Q. \<forall>y. (y, x) \<in> R \<longrightarrow> y \<notin> Q}"
krauss@26748
   234
  assume "x \<in> Q"
krauss@26748
   235
  obtain a where "a \<in> ?Q'"
krauss@26748
   236
    by (rule wfE_min [OF `wf R` `x \<in> Q`]) blast
krauss@26748
   237
  with `wf S`
krauss@26748
   238
  obtain z where "z \<in> ?Q'" and zmin: "\<And>y. (y, z) \<in> S \<Longrightarrow> y \<notin> ?Q'" by (erule wfE_min)
krauss@26748
   239
  { 
krauss@26748
   240
    fix y assume "(y, z) \<in> S"
krauss@26748
   241
    then have "y \<notin> ?Q'" by (rule zmin)
krauss@26748
   242
krauss@26748
   243
    have "y \<notin> Q"
krauss@26748
   244
    proof 
krauss@26748
   245
      assume "y \<in> Q"
krauss@26748
   246
      with `y \<notin> ?Q'` 
krauss@26748
   247
      obtain w where "(w, y) \<in> R" and "w \<in> Q" by auto
krauss@26748
   248
      from `(w, y) \<in> R` `(y, z) \<in> S` have "(w, z) \<in> S O R" by (rule rel_compI)
krauss@26748
   249
      with `S O R \<subseteq> R` have "(w, z) \<in> R" ..
krauss@26748
   250
      with `z \<in> ?Q'` have "w \<notin> Q" by blast 
krauss@26748
   251
      with `w \<in> Q` show False by contradiction
krauss@26748
   252
    qed
krauss@26748
   253
  }
krauss@26748
   254
  with `z \<in> ?Q'` show "\<exists>z\<in>Q. \<forall>y. (y, z) \<in> R \<union> S \<longrightarrow> y \<notin> Q" by blast
krauss@26748
   255
qed
krauss@26748
   256
krauss@26748
   257
krauss@26748
   258
text {* Well-foundedness of indexed union with disjoint domains and ranges *}
krauss@26748
   259
krauss@26748
   260
lemma wf_UN: "[| ALL i:I. wf(r i);  
krauss@26748
   261
         ALL i:I. ALL j:I. r i ~= r j --> Domain(r i) Int Range(r j) = {}  
krauss@26748
   262
      |] ==> wf(UN i:I. r i)"
krauss@26748
   263
apply (simp only: wf_eq_minimal, clarify)
krauss@26748
   264
apply (rename_tac A a, case_tac "EX i:I. EX a:A. EX b:A. (b,a) : r i")
krauss@26748
   265
 prefer 2
krauss@26748
   266
 apply force 
krauss@26748
   267
apply clarify
krauss@26748
   268
apply (drule bspec, assumption)  
krauss@26748
   269
apply (erule_tac x="{a. a:A & (EX b:A. (b,a) : r i) }" in allE)
krauss@26748
   270
apply (blast elim!: allE)  
krauss@26748
   271
done
krauss@26748
   272
krauss@26748
   273
lemmas wfP_SUP = wf_UN [where I=UNIV and r="\<lambda>i. {(x, y). r i x y}",
krauss@26748
   274
  to_pred SUP_UN_eq2 bot_empty_eq, simplified, standard]
krauss@26748
   275
krauss@26748
   276
lemma wf_Union: 
krauss@26748
   277
 "[| ALL r:R. wf r;  
krauss@26748
   278
     ALL r:R. ALL s:R. r ~= s --> Domain r Int Range s = {}  
krauss@26748
   279
  |] ==> wf(Union R)"
krauss@26748
   280
apply (simp add: Union_def)
krauss@26748
   281
apply (blast intro: wf_UN)
krauss@26748
   282
done
krauss@26748
   283
krauss@26748
   284
(*Intuition: we find an (R u S)-min element of a nonempty subset A
krauss@26748
   285
             by case distinction.
krauss@26748
   286
  1. There is a step a -R-> b with a,b : A.
krauss@26748
   287
     Pick an R-min element z of the (nonempty) set {a:A | EX b:A. a -R-> b}.
krauss@26748
   288
     By definition, there is z':A s.t. z -R-> z'. Because z is R-min in the
krauss@26748
   289
     subset, z' must be R-min in A. Because z' has an R-predecessor, it cannot
krauss@26748
   290
     have an S-successor and is thus S-min in A as well.
krauss@26748
   291
  2. There is no such step.
krauss@26748
   292
     Pick an S-min element of A. In this case it must be an R-min
krauss@26748
   293
     element of A as well.
krauss@26748
   294
krauss@26748
   295
*)
krauss@26748
   296
lemma wf_Un:
krauss@26748
   297
     "[| wf r; wf s; Domain r Int Range s = {} |] ==> wf(r Un s)"
krauss@26748
   298
  using wf_union_compatible[of s r] 
krauss@26748
   299
  by (auto simp: Un_ac)
krauss@26748
   300
krauss@26748
   301
lemma wf_union_merge: 
krauss@26748
   302
  "wf (R \<union> S) = wf (R O R \<union> R O S \<union> S)" (is "wf ?A = wf ?B")
krauss@26748
   303
proof
krauss@26748
   304
  assume "wf ?A"
krauss@26748
   305
  with wf_trancl have wfT: "wf (?A^+)" .
krauss@26748
   306
  moreover have "?B \<subseteq> ?A^+"
krauss@26748
   307
    by (subst trancl_unfold, subst trancl_unfold) blast
krauss@26748
   308
  ultimately show "wf ?B" by (rule wf_subset)
krauss@26748
   309
next
krauss@26748
   310
  assume "wf ?B"
krauss@26748
   311
krauss@26748
   312
  show "wf ?A"
krauss@26748
   313
  proof (rule wfI_min)
krauss@26748
   314
    fix Q :: "'a set" and x 
krauss@26748
   315
    assume "x \<in> Q"
krauss@26748
   316
krauss@26748
   317
    with `wf ?B`
krauss@26748
   318
    obtain z where "z \<in> Q" and "\<And>y. (y, z) \<in> ?B \<Longrightarrow> y \<notin> Q" 
krauss@26748
   319
      by (erule wfE_min)
krauss@26748
   320
    then have A1: "\<And>y. (y, z) \<in> R O R \<Longrightarrow> y \<notin> Q"
krauss@26748
   321
      and A2: "\<And>y. (y, z) \<in> R O S \<Longrightarrow> y \<notin> Q"
krauss@26748
   322
      and A3: "\<And>y. (y, z) \<in> S \<Longrightarrow> y \<notin> Q"
krauss@26748
   323
      by auto
krauss@26748
   324
    
krauss@26748
   325
    show "\<exists>z\<in>Q. \<forall>y. (y, z) \<in> ?A \<longrightarrow> y \<notin> Q"
krauss@26748
   326
    proof (cases "\<forall>y. (y, z) \<in> R \<longrightarrow> y \<notin> Q")
krauss@26748
   327
      case True
krauss@26748
   328
      with `z \<in> Q` A3 show ?thesis by blast
krauss@26748
   329
    next
krauss@26748
   330
      case False 
krauss@26748
   331
      then obtain z' where "z'\<in>Q" "(z', z) \<in> R" by blast
krauss@26748
   332
krauss@26748
   333
      have "\<forall>y. (y, z') \<in> ?A \<longrightarrow> y \<notin> Q"
krauss@26748
   334
      proof (intro allI impI)
krauss@26748
   335
        fix y assume "(y, z') \<in> ?A"
krauss@26748
   336
        then show "y \<notin> Q"
krauss@26748
   337
        proof
krauss@26748
   338
          assume "(y, z') \<in> R" 
krauss@26748
   339
          then have "(y, z) \<in> R O R" using `(z', z) \<in> R` ..
krauss@26748
   340
          with A1 show "y \<notin> Q" .
krauss@26748
   341
        next
krauss@26748
   342
          assume "(y, z') \<in> S" 
krauss@26748
   343
          then have "(y, z) \<in> R O S" using  `(z', z) \<in> R` ..
krauss@26748
   344
          with A2 show "y \<notin> Q" .
krauss@26748
   345
        qed
krauss@26748
   346
      qed
krauss@26748
   347
      with `z' \<in> Q` show ?thesis ..
krauss@26748
   348
    qed
krauss@26748
   349
  qed
krauss@26748
   350
qed
krauss@26748
   351
krauss@26748
   352
lemma wf_comp_self: "wf R = wf (R O R)"  -- {* special case *}
krauss@26748
   353
  by (rule wf_union_merge [where S = "{}", simplified])
krauss@26748
   354
krauss@26748
   355
krauss@26748
   356
subsubsection {* acyclic *}
krauss@26748
   357
krauss@26748
   358
lemma acyclicI: "ALL x. (x, x) ~: r^+ ==> acyclic r"
krauss@26748
   359
  by (simp add: acyclic_def)
krauss@26748
   360
krauss@26748
   361
lemma wf_acyclic: "wf r ==> acyclic r"
krauss@26748
   362
apply (simp add: acyclic_def)
krauss@26748
   363
apply (blast elim: wf_trancl [THEN wf_irrefl])
krauss@26748
   364
done
krauss@26748
   365
krauss@26748
   366
lemmas wfP_acyclicP = wf_acyclic [to_pred]
krauss@26748
   367
krauss@26748
   368
lemma acyclic_insert [iff]:
krauss@26748
   369
     "acyclic(insert (y,x) r) = (acyclic r & (x,y) ~: r^*)"
krauss@26748
   370
apply (simp add: acyclic_def trancl_insert)
krauss@26748
   371
apply (blast intro: rtrancl_trans)
krauss@26748
   372
done
krauss@26748
   373
krauss@26748
   374
lemma acyclic_converse [iff]: "acyclic(r^-1) = acyclic r"
krauss@26748
   375
by (simp add: acyclic_def trancl_converse)
krauss@26748
   376
krauss@26748
   377
lemmas acyclicP_converse [iff] = acyclic_converse [to_pred]
krauss@26748
   378
krauss@26748
   379
lemma acyclic_impl_antisym_rtrancl: "acyclic r ==> antisym(r^*)"
krauss@26748
   380
apply (simp add: acyclic_def antisym_def)
krauss@26748
   381
apply (blast elim: rtranclE intro: rtrancl_into_trancl1 rtrancl_trancl_trancl)
krauss@26748
   382
done
krauss@26748
   383
krauss@26748
   384
(* Other direction:
krauss@26748
   385
acyclic = no loops
krauss@26748
   386
antisym = only self loops
krauss@26748
   387
Goalw [acyclic_def,antisym_def] "antisym( r^* ) ==> acyclic(r - Id)
krauss@26748
   388
==> antisym( r^* ) = acyclic(r - Id)";
krauss@26748
   389
*)
krauss@26748
   390
krauss@26748
   391
lemma acyclic_subset: "[| acyclic s; r <= s |] ==> acyclic r"
krauss@26748
   392
apply (simp add: acyclic_def)
krauss@26748
   393
apply (blast intro: trancl_mono)
krauss@26748
   394
done
krauss@26748
   395
krauss@26748
   396
text{* Wellfoundedness of finite acyclic relations*}
krauss@26748
   397
krauss@26748
   398
lemma finite_acyclic_wf [rule_format]: "finite r ==> acyclic r --> wf r"
krauss@26748
   399
apply (erule finite_induct, blast)
krauss@26748
   400
apply (simp (no_asm_simp) only: split_tupled_all)
krauss@26748
   401
apply simp
krauss@26748
   402
done
krauss@26748
   403
krauss@26748
   404
lemma finite_acyclic_wf_converse: "[|finite r; acyclic r|] ==> wf (r^-1)"
krauss@26748
   405
apply (erule finite_converse [THEN iffD2, THEN finite_acyclic_wf])
krauss@26748
   406
apply (erule acyclic_converse [THEN iffD2])
krauss@26748
   407
done
krauss@26748
   408
krauss@26748
   409
lemma wf_iff_acyclic_if_finite: "finite r ==> wf r = acyclic r"
krauss@26748
   410
by (blast intro: finite_acyclic_wf wf_acyclic)
krauss@26748
   411
krauss@26748
   412
krauss@26748
   413
subsection{*Well-Founded Recursion*}
krauss@26748
   414
krauss@26748
   415
text{*cut*}
krauss@26748
   416
krauss@26748
   417
lemma cuts_eq: "(cut f r x = cut g r x) = (ALL y. (y,x):r --> f(y)=g(y))"
krauss@26748
   418
by (simp add: expand_fun_eq cut_def)
krauss@26748
   419
krauss@26748
   420
lemma cut_apply: "(x,a):r ==> (cut f r a)(x) = f(x)"
krauss@26748
   421
by (simp add: cut_def)
krauss@26748
   422
krauss@26748
   423
text{*Inductive characterization of wfrec combinator; for details see:  
krauss@26748
   424
John Harrison, "Inductive definitions: automation and application"*}
krauss@26748
   425
krauss@26748
   426
lemma wfrec_unique: "[| adm_wf R F; wf R |] ==> EX! y. wfrec_rel R F x y"
krauss@26748
   427
apply (simp add: adm_wf_def)
krauss@26748
   428
apply (erule_tac a=x in wf_induct) 
krauss@26748
   429
apply (rule ex1I)
krauss@26748
   430
apply (rule_tac g = "%x. THE y. wfrec_rel R F x y" in wfrec_rel.wfrecI)
krauss@26748
   431
apply (fast dest!: theI')
krauss@26748
   432
apply (erule wfrec_rel.cases, simp)
krauss@26748
   433
apply (erule allE, erule allE, erule allE, erule mp)
krauss@26748
   434
apply (fast intro: the_equality [symmetric])
krauss@26748
   435
done
krauss@26748
   436
krauss@26748
   437
lemma adm_lemma: "adm_wf R (%f x. F (cut f R x) x)"
krauss@26748
   438
apply (simp add: adm_wf_def)
krauss@26748
   439
apply (intro strip)
krauss@26748
   440
apply (rule cuts_eq [THEN iffD2, THEN subst], assumption)
krauss@26748
   441
apply (rule refl)
krauss@26748
   442
done
krauss@26748
   443
krauss@26748
   444
lemma wfrec: "wf(r) ==> wfrec r H a = H (cut (wfrec r H) r a) a"
krauss@26748
   445
apply (simp add: wfrec_def)
krauss@26748
   446
apply (rule adm_lemma [THEN wfrec_unique, THEN the1_equality], assumption)
krauss@26748
   447
apply (rule wfrec_rel.wfrecI)
krauss@26748
   448
apply (intro strip)
krauss@26748
   449
apply (erule adm_lemma [THEN wfrec_unique, THEN theI'])
krauss@26748
   450
done
krauss@26748
   451
krauss@26748
   452
subsection {* Code generator setup *}
krauss@26748
   453
krauss@26748
   454
consts_code
krauss@26748
   455
  "wfrec"   ("\<module>wfrec?")
krauss@26748
   456
attach {*
krauss@26748
   457
fun wfrec f x = f (wfrec f) x;
krauss@26748
   458
*}
krauss@26748
   459
krauss@26748
   460
krauss@26748
   461
subsection {*LEAST and wellorderings*}
krauss@26748
   462
krauss@26748
   463
text{* See also @{text wf_linord_ex_has_least} and its consequences in
krauss@26748
   464
 @{text Wellfounded_Relations.ML}*}
krauss@26748
   465
krauss@26748
   466
lemma wellorder_Least_lemma [rule_format]:
krauss@26748
   467
     "P (k::'a::wellorder) --> P (LEAST x. P(x)) & (LEAST x. P(x)) <= k"
krauss@26748
   468
apply (rule_tac a = k in wf [THEN wf_induct])
krauss@26748
   469
apply (rule impI)
krauss@26748
   470
apply (rule classical)
krauss@26748
   471
apply (rule_tac s = x in Least_equality [THEN ssubst], auto)
krauss@26748
   472
apply (auto simp add: linorder_not_less [symmetric])
krauss@26748
   473
done
krauss@26748
   474
krauss@26748
   475
lemmas LeastI   = wellorder_Least_lemma [THEN conjunct1, standard]
krauss@26748
   476
lemmas Least_le = wellorder_Least_lemma [THEN conjunct2, standard]
krauss@26748
   477
krauss@26748
   478
-- "The following 3 lemmas are due to Brian Huffman"
krauss@26748
   479
lemma LeastI_ex: "EX x::'a::wellorder. P x ==> P (Least P)"
krauss@26748
   480
apply (erule exE)
krauss@26748
   481
apply (erule LeastI)
krauss@26748
   482
done
krauss@26748
   483
krauss@26748
   484
lemma LeastI2:
krauss@26748
   485
  "[| P (a::'a::wellorder); !!x. P x ==> Q x |] ==> Q (Least P)"
krauss@26748
   486
by (blast intro: LeastI)
krauss@26748
   487
krauss@26748
   488
lemma LeastI2_ex:
krauss@26748
   489
  "[| EX a::'a::wellorder. P a; !!x. P x ==> Q x |] ==> Q (Least P)"
krauss@26748
   490
by (blast intro: LeastI_ex)
krauss@26748
   491
krauss@26748
   492
lemma not_less_Least: "[| k < (LEAST x. P x) |] ==> ~P (k::'a::wellorder)"
krauss@26748
   493
apply (simp (no_asm_use) add: linorder_not_le [symmetric])
krauss@26748
   494
apply (erule contrapos_nn)
krauss@26748
   495
apply (erule Least_le)
krauss@26748
   496
done
krauss@26748
   497
krauss@26748
   498
subsection {* @{typ nat} is well-founded *}
krauss@26748
   499
krauss@26748
   500
lemma less_nat_rel: "op < = (\<lambda>m n. n = Suc m)^++"
krauss@26748
   501
proof (rule ext, rule ext, rule iffI)
krauss@26748
   502
  fix n m :: nat
krauss@26748
   503
  assume "m < n"
krauss@26748
   504
  then show "(\<lambda>m n. n = Suc m)^++ m n"
krauss@26748
   505
  proof (induct n)
krauss@26748
   506
    case 0 then show ?case by auto
krauss@26748
   507
  next
krauss@26748
   508
    case (Suc n) then show ?case
krauss@26748
   509
      by (auto simp add: less_Suc_eq_le le_less intro: tranclp.trancl_into_trancl)
krauss@26748
   510
  qed
krauss@26748
   511
next
krauss@26748
   512
  fix n m :: nat
krauss@26748
   513
  assume "(\<lambda>m n. n = Suc m)^++ m n"
krauss@26748
   514
  then show "m < n"
krauss@26748
   515
    by (induct n)
krauss@26748
   516
      (simp_all add: less_Suc_eq_le reflexive le_less)
krauss@26748
   517
qed
krauss@26748
   518
krauss@26748
   519
definition
krauss@26748
   520
  pred_nat :: "(nat * nat) set" where
krauss@26748
   521
  "pred_nat = {(m, n). n = Suc m}"
krauss@26748
   522
krauss@26748
   523
definition
krauss@26748
   524
  less_than :: "(nat * nat) set" where
krauss@26748
   525
  "less_than = pred_nat^+"
krauss@26748
   526
krauss@26748
   527
lemma less_eq: "(m, n) \<in> pred_nat^+ \<longleftrightarrow> m < n"
krauss@26748
   528
  unfolding less_nat_rel pred_nat_def trancl_def by simp
krauss@26748
   529
krauss@26748
   530
lemma pred_nat_trancl_eq_le:
krauss@26748
   531
  "(m, n) \<in> pred_nat^* \<longleftrightarrow> m \<le> n"
krauss@26748
   532
  unfolding less_eq rtrancl_eq_or_trancl by auto
krauss@26748
   533
krauss@26748
   534
lemma wf_pred_nat: "wf pred_nat"
krauss@26748
   535
  apply (unfold wf_def pred_nat_def, clarify)
krauss@26748
   536
  apply (induct_tac x, blast+)
krauss@26748
   537
  done
krauss@26748
   538
krauss@26748
   539
lemma wf_less_than [iff]: "wf less_than"
krauss@26748
   540
  by (simp add: less_than_def wf_pred_nat [THEN wf_trancl])
krauss@26748
   541
krauss@26748
   542
lemma trans_less_than [iff]: "trans less_than"
krauss@26748
   543
  by (simp add: less_than_def trans_trancl)
krauss@26748
   544
krauss@26748
   545
lemma less_than_iff [iff]: "((x,y): less_than) = (x<y)"
krauss@26748
   546
  by (simp add: less_than_def less_eq)
krauss@26748
   547
krauss@26748
   548
lemma wf_less: "wf {(x, y::nat). x < y}"
krauss@26748
   549
  using wf_less_than by (simp add: less_than_def less_eq [symmetric])
krauss@26748
   550
krauss@26748
   551
text {* Type @{typ nat} is a wellfounded order *}
krauss@26748
   552
krauss@26748
   553
instance nat :: wellorder
krauss@26748
   554
  by intro_classes
krauss@26748
   555
    (assumption |
krauss@26748
   556
      rule le_refl le_trans le_anti_sym nat_less_le nat_le_linear wf_less)+
krauss@26748
   557
krauss@26748
   558
text {* @{text LEAST} theorems for type @{typ nat}*}
krauss@26748
   559
krauss@26748
   560
lemma Least_Suc:
krauss@26748
   561
     "[| P n; ~ P 0 |] ==> (LEAST n. P n) = Suc (LEAST m. P(Suc m))"
krauss@26748
   562
  apply (case_tac "n", auto)
krauss@26748
   563
  apply (frule LeastI)
krauss@26748
   564
  apply (drule_tac P = "%x. P (Suc x) " in LeastI)
krauss@26748
   565
  apply (subgoal_tac " (LEAST x. P x) \<le> Suc (LEAST x. P (Suc x))")
krauss@26748
   566
  apply (erule_tac [2] Least_le)
krauss@26748
   567
  apply (case_tac "LEAST x. P x", auto)
krauss@26748
   568
  apply (drule_tac P = "%x. P (Suc x) " in Least_le)
krauss@26748
   569
  apply (blast intro: order_antisym)
krauss@26748
   570
  done
krauss@26748
   571
krauss@26748
   572
lemma Least_Suc2:
krauss@26748
   573
   "[|P n; Q m; ~P 0; !k. P (Suc k) = Q k|] ==> Least P = Suc (Least Q)"
krauss@26748
   574
  apply (erule (1) Least_Suc [THEN ssubst])
krauss@26748
   575
  apply simp
krauss@26748
   576
  done
krauss@26748
   577
krauss@26748
   578
lemma ex_least_nat_le: "\<not>P(0) \<Longrightarrow> P(n::nat) \<Longrightarrow> \<exists>k\<le>n. (\<forall>i<k. \<not>P i) & P(k)"
krauss@26748
   579
  apply (cases n)
krauss@26748
   580
   apply blast
krauss@26748
   581
  apply (rule_tac x="LEAST k. P(k)" in exI)
krauss@26748
   582
  apply (blast intro: Least_le dest: not_less_Least intro: LeastI_ex)
krauss@26748
   583
  done
krauss@26748
   584
krauss@26748
   585
lemma ex_least_nat_less: "\<not>P(0) \<Longrightarrow> P(n::nat) \<Longrightarrow> \<exists>k<n. (\<forall>i\<le>k. \<not>P i) & P(k+1)"
krauss@26748
   586
  apply (cases n)
krauss@26748
   587
   apply blast
krauss@26748
   588
  apply (frule (1) ex_least_nat_le)
krauss@26748
   589
  apply (erule exE)
krauss@26748
   590
  apply (case_tac k)
krauss@26748
   591
   apply simp
krauss@26748
   592
  apply (rename_tac k1)
krauss@26748
   593
  apply (rule_tac x=k1 in exI)
krauss@26748
   594
  apply fastsimp
krauss@26748
   595
  done
krauss@26748
   596
krauss@26748
   597
krauss@26748
   598
subsection {* Accessible Part *}
krauss@26748
   599
krauss@26748
   600
text {*
krauss@26748
   601
 Inductive definition of the accessible part @{term "acc r"} of a
krauss@26748
   602
 relation; see also \cite{paulin-tlca}.
krauss@26748
   603
*}
krauss@26748
   604
krauss@26748
   605
inductive_set
krauss@26748
   606
  acc :: "('a * 'a) set => 'a set"
krauss@26748
   607
  for r :: "('a * 'a) set"
krauss@26748
   608
  where
krauss@26748
   609
    accI: "(!!y. (y, x) : r ==> y : acc r) ==> x : acc r"
krauss@26748
   610
krauss@26748
   611
abbreviation
krauss@26748
   612
  termip :: "('a => 'a => bool) => 'a => bool" where
krauss@26748
   613
  "termip r == accp (r\<inverse>\<inverse>)"
krauss@26748
   614
krauss@26748
   615
abbreviation
krauss@26748
   616
  termi :: "('a * 'a) set => 'a set" where
krauss@26748
   617
  "termi r == acc (r\<inverse>)"
krauss@26748
   618
krauss@26748
   619
lemmas accpI = accp.accI
krauss@26748
   620
krauss@26748
   621
text {* Induction rules *}
krauss@26748
   622
krauss@26748
   623
theorem accp_induct:
krauss@26748
   624
  assumes major: "accp r a"
krauss@26748
   625
  assumes hyp: "!!x. accp r x ==> \<forall>y. r y x --> P y ==> P x"
krauss@26748
   626
  shows "P a"
krauss@26748
   627
  apply (rule major [THEN accp.induct])
krauss@26748
   628
  apply (rule hyp)
krauss@26748
   629
   apply (rule accp.accI)
krauss@26748
   630
   apply fast
krauss@26748
   631
  apply fast
krauss@26748
   632
  done
krauss@26748
   633
krauss@26748
   634
theorems accp_induct_rule = accp_induct [rule_format, induct set: accp]
krauss@26748
   635
krauss@26748
   636
theorem accp_downward: "accp r b ==> r a b ==> accp r a"
krauss@26748
   637
  apply (erule accp.cases)
krauss@26748
   638
  apply fast
krauss@26748
   639
  done
krauss@26748
   640
krauss@26748
   641
lemma not_accp_down:
krauss@26748
   642
  assumes na: "\<not> accp R x"
krauss@26748
   643
  obtains z where "R z x" and "\<not> accp R z"
krauss@26748
   644
proof -
krauss@26748
   645
  assume a: "\<And>z. \<lbrakk>R z x; \<not> accp R z\<rbrakk> \<Longrightarrow> thesis"
krauss@26748
   646
krauss@26748
   647
  show thesis
krauss@26748
   648
  proof (cases "\<forall>z. R z x \<longrightarrow> accp R z")
krauss@26748
   649
    case True
krauss@26748
   650
    hence "\<And>z. R z x \<Longrightarrow> accp R z" by auto
krauss@26748
   651
    hence "accp R x"
krauss@26748
   652
      by (rule accp.accI)
krauss@26748
   653
    with na show thesis ..
krauss@26748
   654
  next
krauss@26748
   655
    case False then obtain z where "R z x" and "\<not> accp R z"
krauss@26748
   656
      by auto
krauss@26748
   657
    with a show thesis .
krauss@26748
   658
  qed
krauss@26748
   659
qed
krauss@26748
   660
krauss@26748
   661
lemma accp_downwards_aux: "r\<^sup>*\<^sup>* b a ==> accp r a --> accp r b"
krauss@26748
   662
  apply (erule rtranclp_induct)
krauss@26748
   663
   apply blast
krauss@26748
   664
  apply (blast dest: accp_downward)
krauss@26748
   665
  done
krauss@26748
   666
krauss@26748
   667
theorem accp_downwards: "accp r a ==> r\<^sup>*\<^sup>* b a ==> accp r b"
krauss@26748
   668
  apply (blast dest: accp_downwards_aux)
krauss@26748
   669
  done
krauss@26748
   670
krauss@26748
   671
theorem accp_wfPI: "\<forall>x. accp r x ==> wfP r"
krauss@26748
   672
  apply (rule wfPUNIVI)
krauss@26748
   673
  apply (induct_tac P x rule: accp_induct)
krauss@26748
   674
   apply blast
krauss@26748
   675
  apply blast
krauss@26748
   676
  done
krauss@26748
   677
krauss@26748
   678
theorem accp_wfPD: "wfP r ==> accp r x"
krauss@26748
   679
  apply (erule wfP_induct_rule)
krauss@26748
   680
  apply (rule accp.accI)
krauss@26748
   681
  apply blast
krauss@26748
   682
  done
krauss@26748
   683
krauss@26748
   684
theorem wfP_accp_iff: "wfP r = (\<forall>x. accp r x)"
krauss@26748
   685
  apply (blast intro: accp_wfPI dest: accp_wfPD)
krauss@26748
   686
  done
krauss@26748
   687
krauss@26748
   688
krauss@26748
   689
text {* Smaller relations have bigger accessible parts: *}
krauss@26748
   690
krauss@26748
   691
lemma accp_subset:
krauss@26748
   692
  assumes sub: "R1 \<le> R2"
krauss@26748
   693
  shows "accp R2 \<le> accp R1"
krauss@26748
   694
proof
krauss@26748
   695
  fix x assume "accp R2 x"
krauss@26748
   696
  then show "accp R1 x"
krauss@26748
   697
  proof (induct x)
krauss@26748
   698
    fix x
krauss@26748
   699
    assume ih: "\<And>y. R2 y x \<Longrightarrow> accp R1 y"
krauss@26748
   700
    with sub show "accp R1 x"
krauss@26748
   701
      by (blast intro: accp.accI)
krauss@26748
   702
  qed
krauss@26748
   703
qed
krauss@26748
   704
krauss@26748
   705
krauss@26748
   706
text {* This is a generalized induction theorem that works on
krauss@26748
   707
  subsets of the accessible part. *}
krauss@26748
   708
krauss@26748
   709
lemma accp_subset_induct:
krauss@26748
   710
  assumes subset: "D \<le> accp R"
krauss@26748
   711
    and dcl: "\<And>x z. \<lbrakk>D x; R z x\<rbrakk> \<Longrightarrow> D z"
krauss@26748
   712
    and "D x"
krauss@26748
   713
    and istep: "\<And>x. \<lbrakk>D x; (\<And>z. R z x \<Longrightarrow> P z)\<rbrakk> \<Longrightarrow> P x"
krauss@26748
   714
  shows "P x"
krauss@26748
   715
proof -
krauss@26748
   716
  from subset and `D x`
krauss@26748
   717
  have "accp R x" ..
krauss@26748
   718
  then show "P x" using `D x`
krauss@26748
   719
  proof (induct x)
krauss@26748
   720
    fix x
krauss@26748
   721
    assume "D x"
krauss@26748
   722
      and "\<And>y. R y x \<Longrightarrow> D y \<Longrightarrow> P y"
krauss@26748
   723
    with dcl and istep show "P x" by blast
krauss@26748
   724
  qed
krauss@26748
   725
qed
krauss@26748
   726
krauss@26748
   727
krauss@26748
   728
text {* Set versions of the above theorems *}
krauss@26748
   729
krauss@26748
   730
lemmas acc_induct = accp_induct [to_set]
krauss@26748
   731
krauss@26748
   732
lemmas acc_induct_rule = acc_induct [rule_format, induct set: acc]
krauss@26748
   733
krauss@26748
   734
lemmas acc_downward = accp_downward [to_set]
krauss@26748
   735
krauss@26748
   736
lemmas not_acc_down = not_accp_down [to_set]
krauss@26748
   737
krauss@26748
   738
lemmas acc_downwards_aux = accp_downwards_aux [to_set]
krauss@26748
   739
krauss@26748
   740
lemmas acc_downwards = accp_downwards [to_set]
krauss@26748
   741
krauss@26748
   742
lemmas acc_wfI = accp_wfPI [to_set]
krauss@26748
   743
krauss@26748
   744
lemmas acc_wfD = accp_wfPD [to_set]
krauss@26748
   745
krauss@26748
   746
lemmas wf_acc_iff = wfP_accp_iff [to_set]
krauss@26748
   747
krauss@26748
   748
lemmas acc_subset = accp_subset [to_set]
krauss@26748
   749
krauss@26748
   750
lemmas acc_subset_induct = accp_subset_induct [to_set]
krauss@26748
   751
krauss@26748
   752
krauss@26748
   753
subsection {* Tools for building wellfounded relations *}
krauss@26748
   754
krauss@26748
   755
text {* Inverse Image *}
krauss@26748
   756
krauss@26748
   757
lemma wf_inv_image [simp,intro!]: "wf(r) ==> wf(inv_image r (f::'a=>'b))"
krauss@26748
   758
apply (simp (no_asm_use) add: inv_image_def wf_eq_minimal)
krauss@26748
   759
apply clarify
krauss@26748
   760
apply (subgoal_tac "EX (w::'b) . w : {w. EX (x::'a) . x: Q & (f x = w) }")
krauss@26748
   761
prefer 2 apply (blast del: allE)
krauss@26748
   762
apply (erule allE)
krauss@26748
   763
apply (erule (1) notE impE)
krauss@26748
   764
apply blast
krauss@26748
   765
done
krauss@26748
   766
krauss@26748
   767
lemma in_inv_image[simp]: "((x,y) : inv_image r f) = ((f x, f y) : r)"
krauss@26748
   768
  by (auto simp:inv_image_def)
krauss@26748
   769
krauss@26748
   770
text {* Measure functions into @{typ nat} *}
krauss@26748
   771
krauss@26748
   772
definition measure :: "('a => nat) => ('a * 'a)set"
krauss@26748
   773
where "measure == inv_image less_than"
krauss@26748
   774
krauss@26748
   775
lemma in_measure[simp]: "((x,y) : measure f) = (f x < f y)"
krauss@26748
   776
  by (simp add:measure_def)
krauss@26748
   777
krauss@26748
   778
lemma wf_measure [iff]: "wf (measure f)"
krauss@26748
   779
apply (unfold measure_def)
krauss@26748
   780
apply (rule wf_less_than [THEN wf_inv_image])
krauss@26748
   781
done
krauss@26748
   782
krauss@26748
   783
text{* Lexicographic combinations *}
krauss@26748
   784
krauss@26748
   785
definition
krauss@26748
   786
 lex_prod  :: "[('a*'a)set, ('b*'b)set] => (('a*'b)*('a*'b))set"
krauss@26748
   787
               (infixr "<*lex*>" 80)
krauss@26748
   788
where
krauss@26748
   789
    "ra <*lex*> rb == {((a,b),(a',b')). (a,a') : ra | a=a' & (b,b') : rb}"
krauss@26748
   790
krauss@26748
   791
lemma wf_lex_prod [intro!]: "[| wf(ra); wf(rb) |] ==> wf(ra <*lex*> rb)"
krauss@26748
   792
apply (unfold wf_def lex_prod_def) 
krauss@26748
   793
apply (rule allI, rule impI)
krauss@26748
   794
apply (simp (no_asm_use) only: split_paired_All)
krauss@26748
   795
apply (drule spec, erule mp) 
krauss@26748
   796
apply (rule allI, rule impI)
krauss@26748
   797
apply (drule spec, erule mp, blast) 
krauss@26748
   798
done
krauss@26748
   799
krauss@26748
   800
lemma in_lex_prod[simp]: 
krauss@26748
   801
  "(((a,b),(a',b')): r <*lex*> s) = ((a,a'): r \<or> (a = a' \<and> (b, b') : s))"
krauss@26748
   802
  by (auto simp:lex_prod_def)
krauss@26748
   803
krauss@26748
   804
text{* @{term "op <*lex*>"} preserves transitivity *}
krauss@26748
   805
krauss@26748
   806
lemma trans_lex_prod [intro!]: 
krauss@26748
   807
    "[| trans R1; trans R2 |] ==> trans (R1 <*lex*> R2)"
krauss@26748
   808
by (unfold trans_def lex_prod_def, blast) 
krauss@26748
   809
krauss@26748
   810
text {* lexicographic combinations with measure functions *}
krauss@26748
   811
krauss@26748
   812
definition 
krauss@26748
   813
  mlex_prod :: "('a \<Rightarrow> nat) \<Rightarrow> ('a \<times> 'a) set \<Rightarrow> ('a \<times> 'a) set" (infixr "<*mlex*>" 80)
krauss@26748
   814
where
krauss@26748
   815
  "f <*mlex*> R = inv_image (less_than <*lex*> R) (%x. (f x, x))"
krauss@26748
   816
krauss@26748
   817
lemma wf_mlex: "wf R \<Longrightarrow> wf (f <*mlex*> R)"
krauss@26748
   818
unfolding mlex_prod_def
krauss@26748
   819
by auto
krauss@26748
   820
krauss@26748
   821
lemma mlex_less: "f x < f y \<Longrightarrow> (x, y) \<in> f <*mlex*> R"
krauss@26748
   822
unfolding mlex_prod_def by simp
krauss@26748
   823
krauss@26748
   824
lemma mlex_leq: "f x \<le> f y \<Longrightarrow> (x, y) \<in> R \<Longrightarrow> (x, y) \<in> f <*mlex*> R"
krauss@26748
   825
unfolding mlex_prod_def by auto
krauss@26748
   826
krauss@26748
   827
text {* proper subset relation on finite sets *}
krauss@26748
   828
krauss@26748
   829
definition finite_psubset  :: "('a set * 'a set) set"
krauss@26748
   830
where "finite_psubset == {(A,B). A < B & finite B}"
krauss@26748
   831
krauss@26748
   832
lemma wf_finite_psubset: "wf(finite_psubset)"
krauss@26748
   833
apply (unfold finite_psubset_def)
krauss@26748
   834
apply (rule wf_measure [THEN wf_subset])
krauss@26748
   835
apply (simp add: measure_def inv_image_def less_than_def less_eq)
krauss@26748
   836
apply (fast elim!: psubset_card_mono)
krauss@26748
   837
done
krauss@26748
   838
krauss@26748
   839
lemma trans_finite_psubset: "trans finite_psubset"
krauss@26748
   840
by (simp add: finite_psubset_def psubset_def trans_def, blast)
krauss@26748
   841
krauss@26748
   842
krauss@26748
   843
krauss@26748
   844
krauss@26748
   845
text {*Wellfoundedness of @{text same_fst}*}
krauss@26748
   846
krauss@26748
   847
definition
krauss@26748
   848
 same_fst :: "('a => bool) => ('a => ('b * 'b)set) => (('a*'b)*('a*'b))set"
krauss@26748
   849
where
krauss@26748
   850
    "same_fst P R == {((x',y'),(x,y)) . x'=x & P x & (y',y) : R x}"
krauss@26748
   851
   --{*For @{text rec_def} declarations where the first n parameters
krauss@26748
   852
       stay unchanged in the recursive call. 
krauss@26748
   853
       See @{text "Library/While_Combinator.thy"} for an application.*}
krauss@26748
   854
krauss@26748
   855
lemma same_fstI [intro!]:
krauss@26748
   856
     "[| P x; (y',y) : R x |] ==> ((x,y'),(x,y)) : same_fst P R"
krauss@26748
   857
by (simp add: same_fst_def)
krauss@26748
   858
krauss@26748
   859
lemma wf_same_fst:
krauss@26748
   860
  assumes prem: "(!!x. P x ==> wf(R x))"
krauss@26748
   861
  shows "wf(same_fst P R)"
krauss@26748
   862
apply (simp cong del: imp_cong add: wf_def same_fst_def)
krauss@26748
   863
apply (intro strip)
krauss@26748
   864
apply (rename_tac a b)
krauss@26748
   865
apply (case_tac "wf (R a)")
krauss@26748
   866
 apply (erule_tac a = b in wf_induct, blast)
krauss@26748
   867
apply (blast intro: prem)
krauss@26748
   868
done
krauss@26748
   869
krauss@26748
   870
krauss@26748
   871
subsection{*Weakly decreasing sequences (w.r.t. some well-founded order) 
krauss@26748
   872
   stabilize.*}
krauss@26748
   873
krauss@26748
   874
text{*This material does not appear to be used any longer.*}
krauss@26748
   875
krauss@26748
   876
lemma lemma1: "[| ALL i. (f (Suc i), f i) : r^* |] ==> (f (i+k), f i) : r^*"
krauss@26748
   877
apply (induct_tac "k", simp_all)
krauss@26748
   878
apply (blast intro: rtrancl_trans)
krauss@26748
   879
done
krauss@26748
   880
krauss@26748
   881
lemma lemma2: "[| ALL i. (f (Suc i), f i) : r^*; wf (r^+) |]  
krauss@26748
   882
      ==> ALL m. f m = x --> (EX i. ALL k. f (m+i+k) = f (m+i))"
krauss@26748
   883
apply (erule wf_induct, clarify)
krauss@26748
   884
apply (case_tac "EX j. (f (m+j), f m) : r^+")
krauss@26748
   885
 apply clarify
krauss@26748
   886
 apply (subgoal_tac "EX i. ALL k. f ((m+j) +i+k) = f ( (m+j) +i) ")
krauss@26748
   887
  apply clarify
krauss@26748
   888
  apply (rule_tac x = "j+i" in exI)
krauss@26748
   889
  apply (simp add: add_ac, blast)
krauss@26748
   890
apply (rule_tac x = 0 in exI, clarsimp)
krauss@26748
   891
apply (drule_tac i = m and k = k in lemma1)
krauss@26748
   892
apply (blast elim: rtranclE dest: rtrancl_into_trancl1)
krauss@26748
   893
done
krauss@26748
   894
krauss@26748
   895
lemma wf_weak_decr_stable: "[| ALL i. (f (Suc i), f i) : r^*; wf (r^+) |]  
krauss@26748
   896
      ==> EX i. ALL k. f (i+k) = f i"
krauss@26748
   897
apply (drule_tac x = 0 in lemma2 [THEN spec], auto)
krauss@26748
   898
done
krauss@26748
   899
krauss@26748
   900
(* special case of the theorem above: <= *)
krauss@26748
   901
lemma weak_decr_stable:
krauss@26748
   902
     "ALL i. f (Suc i) <= ((f i)::nat) ==> EX i. ALL k. f (i+k) = f i"
krauss@26748
   903
apply (rule_tac r = pred_nat in wf_weak_decr_stable)
krauss@26748
   904
apply (simp add: pred_nat_trancl_eq_le)
krauss@26748
   905
apply (intro wf_trancl wf_pred_nat)
krauss@26748
   906
done
krauss@26748
   907
krauss@26748
   908
krauss@26748
   909
subsection {* size of a datatype value *}
krauss@26748
   910
krauss@26748
   911
use "Tools/function_package/size.ML"
krauss@26748
   912
krauss@26748
   913
setup Size.setup
krauss@26748
   914
krauss@26748
   915
lemma nat_size [simp, code func]: "size (n\<Colon>nat) = n"
krauss@26748
   916
  by (induct n) simp_all
krauss@26748
   917
krauss@26748
   918
krauss@26748
   919
end