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