src/HOL/Library/While_Combinator.thy
author krauss
Fri, 09 Jul 2010 16:32:25 +0200
changeset 37757 dc78d2d9e90a
parent 30738 0842e906300c
child 37760 8380686be5cd
permissions -rw-r--r--
added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
22803
5129e02f4df2 slightly tuned
haftmann
parents: 21404
diff changeset
     1
(*  Title:      HOL/Library/While_Combinator.thy
10251
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
     2
    Author:     Tobias Nipkow
37757
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
     3
    Author:     Alexander Krauss
10251
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
     4
    Copyright   2000 TU Muenchen
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
     5
*)
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
     6
14706
71590b7733b7 tuned document;
wenzelm
parents: 14589
diff changeset
     7
header {* A general ``while'' combinator *}
10251
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
     8
15131
c69542757a4d New theory header syntax.
nipkow
parents: 14706
diff changeset
     9
theory While_Combinator
30738
0842e906300c normalized imports
haftmann
parents: 27487
diff changeset
    10
imports Main
15131
c69542757a4d New theory header syntax.
nipkow
parents: 14706
diff changeset
    11
begin
10251
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    12
37757
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    13
subsection {* Option result *}
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    14
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    15
definition while_option :: "('a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a option" where
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    16
"while_option b c s = (if (\<exists>k. ~ b ((c ^^ k) s))
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    17
   then Some ((c ^^ (LEAST k. ~ b ((c ^^ k) s))) s)
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    18
   else None)"
10251
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    19
37757
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    20
theorem while_option_unfold[code]:
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    21
"while_option b c s = (if b s then while_option b c (c s) else Some s)"
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    22
proof cases
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    23
  assume "b s"
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    24
  show ?thesis
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    25
  proof (cases "\<exists>k. ~ b ((c ^^ k) s)")
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    26
    case True
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    27
    then obtain k where 1: "~ b ((c ^^ k) s)" ..
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    28
    with `b s` obtain l where "k = Suc l" by (cases k) auto
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    29
    with 1 have "~ b ((c ^^ l) (c s))" by (auto simp: funpow_swap1)
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    30
    then have 2: "\<exists>l. ~ b ((c ^^ l) (c s))" ..
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    31
    from 1
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    32
    have "(LEAST k. ~ b ((c ^^ k) s)) = Suc (LEAST l. ~ b ((c ^^ Suc l) s))"
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    33
      by (rule Least_Suc) (simp add: `b s`)
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    34
    also have "... = Suc (LEAST l. ~ b ((c ^^ l) (c s)))"
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    35
      by (simp add: funpow_swap1)
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    36
    finally
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    37
    show ?thesis 
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    38
      using True 2 `b s` by (simp add: funpow_swap1 while_option_def)
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    39
  next
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    40
    case False
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    41
    then have "~ (\<exists>l. ~ b ((c ^^ Suc l) s))" by blast
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    42
    then have "~ (\<exists>l. ~ b ((c ^^ l) (c s)))"
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    43
      by (simp add: funpow_swap1)
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    44
    with False  `b s` show ?thesis by (simp add: while_option_def)
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    45
  qed
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    46
next
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    47
  assume [simp]: "~ b s"
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    48
  have least: "(LEAST k. ~ b ((c ^^ k) s)) = 0"
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    49
    by (rule Least_equality) auto
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    50
  moreover 
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    51
  have "\<exists>k. ~ b ((c ^^ k) s)" by (rule exI[of _ "0::nat"]) auto
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    52
  ultimately show ?thesis unfolding while_option_def by auto 
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    53
qed
10251
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    54
37757
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    55
lemma while_option_stop:
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    56
assumes "while_option b c s = Some t"
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    57
shows "~ b t"
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    58
proof -
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    59
  from assms have ex: "\<exists>k. ~ b ((c ^^ k) s)"
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    60
  and t: "t = (c ^^ (LEAST k. ~ b ((c ^^ k) s))) s"
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    61
    by (auto simp: while_option_def split: if_splits)
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    62
  from LeastI_ex[OF ex]
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    63
  show "~ b t" unfolding t .
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    64
qed
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    65
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    66
theorem while_option_rule:
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    67
assumes step: "!!s. P s ==> b s ==> P (c s)"
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    68
and result: "while_option b c s = Some t"
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    69
and init: "P s"
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    70
shows "P t"
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    71
proof -
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    72
  def k == "LEAST k. ~ b ((c ^^ k) s)"
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    73
  from assms have t: "t = (c ^^ k) s"
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    74
    by (simp add: while_option_def k_def split: if_splits)    
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    75
  have 1: "ALL i<k. b ((c ^^ i) s)"
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    76
    by (auto simp: k_def dest: not_less_Least)
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    77
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    78
  { fix i assume "i <= k" then have "P ((c ^^ i) s)"
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    79
      by (induct i) (auto simp: init step 1) }
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    80
  thus "P t" by (auto simp: t)
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    81
qed
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    82
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    83
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    84
subsection {* Totalized version *}
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    85
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    86
definition while :: "('a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a"
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    87
where "while b c s = the (while_option b c s)"
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    88
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    89
lemma while_unfold:
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    90
  "while b c s = (if b s then while b c (c s) else s)"
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    91
unfolding while_def by (subst while_option_unfold) simp
10984
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
    92
18372
2bffdf62fe7f tuned proofs;
wenzelm
parents: 15197
diff changeset
    93
lemma def_while_unfold:
2bffdf62fe7f tuned proofs;
wenzelm
parents: 15197
diff changeset
    94
  assumes fdef: "f == while test do"
2bffdf62fe7f tuned proofs;
wenzelm
parents: 15197
diff changeset
    95
  shows "f x = (if test x then f(do x) else x)"
37757
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
    96
unfolding fdef by (fact while_unfold)
14300
bf8b8c9425c3 *** empty log message ***
nipkow
parents: 12791
diff changeset
    97
bf8b8c9425c3 *** empty log message ***
nipkow
parents: 12791
diff changeset
    98
10251
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    99
text {*
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
   100
 The proof rule for @{term while}, where @{term P} is the invariant.
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
   101
*}
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
   102
18372
2bffdf62fe7f tuned proofs;
wenzelm
parents: 15197
diff changeset
   103
theorem while_rule_lemma:
2bffdf62fe7f tuned proofs;
wenzelm
parents: 15197
diff changeset
   104
  assumes invariant: "!!s. P s ==> b s ==> P (c s)"
2bffdf62fe7f tuned proofs;
wenzelm
parents: 15197
diff changeset
   105
    and terminate: "!!s. P s ==> \<not> b s ==> Q s"
2bffdf62fe7f tuned proofs;
wenzelm
parents: 15197
diff changeset
   106
    and wf: "wf {(t, s). P s \<and> b s \<and> t = c s}"
2bffdf62fe7f tuned proofs;
wenzelm
parents: 15197
diff changeset
   107
  shows "P s \<Longrightarrow> Q (while b c s)"
19736
wenzelm
parents: 18372
diff changeset
   108
  using wf
wenzelm
parents: 18372
diff changeset
   109
  apply (induct s)
18372
2bffdf62fe7f tuned proofs;
wenzelm
parents: 15197
diff changeset
   110
  apply simp
2bffdf62fe7f tuned proofs;
wenzelm
parents: 15197
diff changeset
   111
  apply (subst while_unfold)
2bffdf62fe7f tuned proofs;
wenzelm
parents: 15197
diff changeset
   112
  apply (simp add: invariant terminate)
2bffdf62fe7f tuned proofs;
wenzelm
parents: 15197
diff changeset
   113
  done
10251
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
   114
10653
55f33da63366 small mods.
nipkow
parents: 10269
diff changeset
   115
theorem while_rule:
10984
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
   116
  "[| P s;
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
   117
      !!s. [| P s; b s  |] ==> P (c s);
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
   118
      !!s. [| P s; \<not> b s  |] ==> Q s;
10997
e14029f92770 avoid dead code;
wenzelm
parents: 10984
diff changeset
   119
      wf r;
10984
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
   120
      !!s. [| P s; b s  |] ==> (c s, s) \<in> r |] ==>
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
   121
   Q (while b c s)"
19736
wenzelm
parents: 18372
diff changeset
   122
  apply (rule while_rule_lemma)
wenzelm
parents: 18372
diff changeset
   123
     prefer 4 apply assumption
wenzelm
parents: 18372
diff changeset
   124
    apply blast
wenzelm
parents: 18372
diff changeset
   125
   apply blast
wenzelm
parents: 18372
diff changeset
   126
  apply (erule wf_subset)
wenzelm
parents: 18372
diff changeset
   127
  apply blast
wenzelm
parents: 18372
diff changeset
   128
  done
10653
55f33da63366 small mods.
nipkow
parents: 10269
diff changeset
   129
10984
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
   130
text {*
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
   131
 \medskip An application: computation of the @{term lfp} on finite
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
   132
 sets via iteration.
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
   133
*}
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
   134
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
   135
theorem lfp_conv_while:
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
   136
  "[| mono f; finite U; f U = U |] ==>
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
   137
    lfp f = fst (while (\<lambda>(A, fA). A \<noteq> fA) (\<lambda>(A, fA). (fA, f fA)) ({}, f {}))"
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
   138
apply (rule_tac P = "\<lambda>(A, B). (A \<subseteq> U \<and> B = f A \<and> A \<subseteq> B \<and> B \<subseteq> lfp f)" and
11047
wenzelm
parents: 10997
diff changeset
   139
                r = "((Pow U \<times> UNIV) \<times> (Pow U \<times> UNIV)) \<inter>
10984
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
   140
                     inv_image finite_psubset (op - U o fst)" in while_rule)
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
   141
   apply (subst lfp_unfold)
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
   142
    apply assumption
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
   143
   apply (simp add: monoD)
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
   144
  apply (subst lfp_unfold)
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
   145
   apply assumption
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
   146
  apply clarsimp
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
   147
  apply (blast dest: monoD)
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
   148
 apply (fastsimp intro!: lfp_lowerbound)
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
   149
 apply (blast intro: wf_finite_psubset Int_lower2 [THEN [2] wf_subset])
19769
c40ce2de2020 Added [simp]-lemmas "in_inv_image" and "in_lex_prod" in the spirit of "in_measure".
krauss
parents: 19736
diff changeset
   150
apply (clarsimp simp add: finite_psubset_def order_less_le)
10984
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
   151
apply (blast intro!: finite_Diff dest: monoD)
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
   152
done
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
   153
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
   154
37757
dc78d2d9e90a added "while_option", which needs no well-foundedness; defined "while" in terms of "while_option"
krauss
parents: 30738
diff changeset
   155
subsection {* Example *}
10984
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
   156
15197
19e735596e51 Added antisymmetry simproc
nipkow
parents: 15140
diff changeset
   157
text{* Cannot use @{thm[source]set_eq_subset} because it leads to
19e735596e51 Added antisymmetry simproc
nipkow
parents: 15140
diff changeset
   158
looping because the antisymmetry simproc turns the subset relationship
19e735596e51 Added antisymmetry simproc
nipkow
parents: 15140
diff changeset
   159
back into equality. *}
19e735596e51 Added antisymmetry simproc
nipkow
parents: 15140
diff changeset
   160
14589
feae7b5fd425 tuned document;
wenzelm
parents: 14300
diff changeset
   161
theorem "P (lfp (\<lambda>N::int set. {0} \<union> {(n + 2) mod 6 | n. n \<in> N})) =
feae7b5fd425 tuned document;
wenzelm
parents: 14300
diff changeset
   162
  P {0, 4, 2}"
10997
e14029f92770 avoid dead code;
wenzelm
parents: 10984
diff changeset
   163
proof -
19736
wenzelm
parents: 18372
diff changeset
   164
  have seteq: "!!A B. (A = B) = ((!a : A. a:B) & (!b:B. b:A))"
wenzelm
parents: 18372
diff changeset
   165
    by blast
10997
e14029f92770 avoid dead code;
wenzelm
parents: 10984
diff changeset
   166
  have aux: "!!f A B. {f n | n. A n \<or> B n} = {f n | n. A n} \<union> {f n | n. B n}"
10984
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
   167
    apply blast
10997
e14029f92770 avoid dead code;
wenzelm
parents: 10984
diff changeset
   168
    done
e14029f92770 avoid dead code;
wenzelm
parents: 10984
diff changeset
   169
  show ?thesis
11914
bca734def300 eliminated old numerals;
wenzelm
parents: 11704
diff changeset
   170
    apply (subst lfp_conv_while [where ?U = "{0, 1, 2, 3, 4, 5}"])
10997
e14029f92770 avoid dead code;
wenzelm
parents: 10984
diff changeset
   171
       apply (rule monoI)
e14029f92770 avoid dead code;
wenzelm
parents: 10984
diff changeset
   172
      apply blast
e14029f92770 avoid dead code;
wenzelm
parents: 10984
diff changeset
   173
     apply simp
e14029f92770 avoid dead code;
wenzelm
parents: 10984
diff changeset
   174
    apply (simp add: aux set_eq_subset)
e14029f92770 avoid dead code;
wenzelm
parents: 10984
diff changeset
   175
    txt {* The fixpoint computation is performed purely by rewriting: *}
15197
19e735596e51 Added antisymmetry simproc
nipkow
parents: 15140
diff changeset
   176
    apply (simp add: while_unfold aux seteq del: subset_empty)
10997
e14029f92770 avoid dead code;
wenzelm
parents: 10984
diff changeset
   177
    done
e14029f92770 avoid dead code;
wenzelm
parents: 10984
diff changeset
   178
qed
10251
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
   179
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
   180
end