src/HOL/Library/While_Combinator.thy
author wenzelm
Fri, 06 Mar 2009 22:32:27 +0100
changeset 30318 3d03190d2864
parent 27487 c8a6ce181805
child 30738 0842e906300c
permissions -rw-r--r--
replaced archaic use of rep_ss by Simplifier.mksimps;
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
    ID:         $Id$
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
     3
    Author:     Tobias Nipkow
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
27487
c8a6ce181805 absolute imports of HOL/*.thy theories
haftmann
parents: 27368
diff changeset
    10
imports Plain "~~/src/HOL/Presburger"
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
23821
2acd9d79d855 use function package
krauss
parents: 22803
diff changeset
    13
text {* 
2acd9d79d855 use function package
krauss
parents: 22803
diff changeset
    14
  We define the while combinator as the "mother of all tail recursive functions".
10251
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    15
*}
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    16
23821
2acd9d79d855 use function package
krauss
parents: 22803
diff changeset
    17
function (tailrec) while :: "('a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a"
2acd9d79d855 use function package
krauss
parents: 22803
diff changeset
    18
where
2acd9d79d855 use function package
krauss
parents: 22803
diff changeset
    19
  while_unfold[simp del]: "while b c s = (if b s then while b c (c s) else s)"
2acd9d79d855 use function package
krauss
parents: 22803
diff changeset
    20
by auto
10251
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    21
23821
2acd9d79d855 use function package
krauss
parents: 22803
diff changeset
    22
declare while_unfold[code]
10984
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
    23
18372
2bffdf62fe7f tuned proofs;
wenzelm
parents: 15197
diff changeset
    24
lemma def_while_unfold:
2bffdf62fe7f tuned proofs;
wenzelm
parents: 15197
diff changeset
    25
  assumes fdef: "f == while test do"
2bffdf62fe7f tuned proofs;
wenzelm
parents: 15197
diff changeset
    26
  shows "f x = (if test x then f(do x) else x)"
14300
bf8b8c9425c3 *** empty log message ***
nipkow
parents: 12791
diff changeset
    27
proof -
bf8b8c9425c3 *** empty log message ***
nipkow
parents: 12791
diff changeset
    28
  have "f x = while test do x" using fdef by simp
bf8b8c9425c3 *** empty log message ***
nipkow
parents: 12791
diff changeset
    29
  also have "\<dots> = (if test x then while test do (do x) else x)"
bf8b8c9425c3 *** empty log message ***
nipkow
parents: 12791
diff changeset
    30
    by(rule while_unfold)
bf8b8c9425c3 *** empty log message ***
nipkow
parents: 12791
diff changeset
    31
  also have "\<dots> = (if test x then f(do x) else x)" by(simp add:fdef[symmetric])
bf8b8c9425c3 *** empty log message ***
nipkow
parents: 12791
diff changeset
    32
  finally show ?thesis .
bf8b8c9425c3 *** empty log message ***
nipkow
parents: 12791
diff changeset
    33
qed
bf8b8c9425c3 *** empty log message ***
nipkow
parents: 12791
diff changeset
    34
bf8b8c9425c3 *** empty log message ***
nipkow
parents: 12791
diff changeset
    35
10251
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    36
text {*
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    37
 The proof rule for @{term while}, where @{term P} is the invariant.
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    38
*}
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    39
18372
2bffdf62fe7f tuned proofs;
wenzelm
parents: 15197
diff changeset
    40
theorem while_rule_lemma:
2bffdf62fe7f tuned proofs;
wenzelm
parents: 15197
diff changeset
    41
  assumes invariant: "!!s. P s ==> b s ==> P (c s)"
2bffdf62fe7f tuned proofs;
wenzelm
parents: 15197
diff changeset
    42
    and terminate: "!!s. P s ==> \<not> b s ==> Q s"
2bffdf62fe7f tuned proofs;
wenzelm
parents: 15197
diff changeset
    43
    and wf: "wf {(t, s). P s \<and> b s \<and> t = c s}"
2bffdf62fe7f tuned proofs;
wenzelm
parents: 15197
diff changeset
    44
  shows "P s \<Longrightarrow> Q (while b c s)"
19736
wenzelm
parents: 18372
diff changeset
    45
  using wf
wenzelm
parents: 18372
diff changeset
    46
  apply (induct s)
18372
2bffdf62fe7f tuned proofs;
wenzelm
parents: 15197
diff changeset
    47
  apply simp
2bffdf62fe7f tuned proofs;
wenzelm
parents: 15197
diff changeset
    48
  apply (subst while_unfold)
2bffdf62fe7f tuned proofs;
wenzelm
parents: 15197
diff changeset
    49
  apply (simp add: invariant terminate)
2bffdf62fe7f tuned proofs;
wenzelm
parents: 15197
diff changeset
    50
  done
10251
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
    51
10653
55f33da63366 small mods.
nipkow
parents: 10269
diff changeset
    52
theorem while_rule:
10984
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
    53
  "[| P s;
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
    54
      !!s. [| P s; b s  |] ==> P (c s);
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
    55
      !!s. [| P s; \<not> b s  |] ==> Q s;
10997
e14029f92770 avoid dead code;
wenzelm
parents: 10984
diff changeset
    56
      wf r;
10984
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
    57
      !!s. [| P s; b s  |] ==> (c s, s) \<in> r |] ==>
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
    58
   Q (while b c s)"
19736
wenzelm
parents: 18372
diff changeset
    59
  apply (rule while_rule_lemma)
wenzelm
parents: 18372
diff changeset
    60
     prefer 4 apply assumption
wenzelm
parents: 18372
diff changeset
    61
    apply blast
wenzelm
parents: 18372
diff changeset
    62
   apply blast
wenzelm
parents: 18372
diff changeset
    63
  apply (erule wf_subset)
wenzelm
parents: 18372
diff changeset
    64
  apply blast
wenzelm
parents: 18372
diff changeset
    65
  done
10653
55f33da63366 small mods.
nipkow
parents: 10269
diff changeset
    66
10984
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
    67
text {*
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
    68
 \medskip An application: computation of the @{term lfp} on finite
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
    69
 sets via iteration.
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
    70
*}
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
    71
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
    72
theorem lfp_conv_while:
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
    73
  "[| mono f; finite U; f U = U |] ==>
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
    74
    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
    75
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
    76
                r = "((Pow U \<times> UNIV) \<times> (Pow U \<times> UNIV)) \<inter>
10984
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
    77
                     inv_image finite_psubset (op - U o fst)" in while_rule)
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
    78
   apply (subst lfp_unfold)
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
    79
    apply assumption
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
    80
   apply (simp add: monoD)
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
    81
  apply (subst lfp_unfold)
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
    82
   apply assumption
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
    83
  apply clarsimp
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
    84
  apply (blast dest: monoD)
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
    85
 apply (fastsimp intro!: lfp_lowerbound)
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
    86
 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
    87
apply (clarsimp simp add: finite_psubset_def order_less_le)
10984
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
    88
apply (blast intro!: finite_Diff dest: monoD)
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
    89
done
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
    90
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
    91
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
    92
text {*
14589
feae7b5fd425 tuned document;
wenzelm
parents: 14300
diff changeset
    93
 An example of using the @{term while} combinator.
10984
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
    94
*}
8f49dcbec859 Merged Example into While_Combi
nipkow
parents: 10774
diff changeset
    95
15197
19e735596e51 Added antisymmetry simproc
nipkow
parents: 15140
diff changeset
    96
text{* Cannot use @{thm[source]set_eq_subset} because it leads to
19e735596e51 Added antisymmetry simproc
nipkow
parents: 15140
diff changeset
    97
looping because the antisymmetry simproc turns the subset relationship
19e735596e51 Added antisymmetry simproc
nipkow
parents: 15140
diff changeset
    98
back into equality. *}
19e735596e51 Added antisymmetry simproc
nipkow
parents: 15140
diff changeset
    99
14589
feae7b5fd425 tuned document;
wenzelm
parents: 14300
diff changeset
   100
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
   101
  P {0, 4, 2}"
10997
e14029f92770 avoid dead code;
wenzelm
parents: 10984
diff changeset
   102
proof -
19736
wenzelm
parents: 18372
diff changeset
   103
  have seteq: "!!A B. (A = B) = ((!a : A. a:B) & (!b:B. b:A))"
wenzelm
parents: 18372
diff changeset
   104
    by blast
10997
e14029f92770 avoid dead code;
wenzelm
parents: 10984
diff changeset
   105
  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
   106
    apply blast
10997
e14029f92770 avoid dead code;
wenzelm
parents: 10984
diff changeset
   107
    done
e14029f92770 avoid dead code;
wenzelm
parents: 10984
diff changeset
   108
  show ?thesis
11914
bca734def300 eliminated old numerals;
wenzelm
parents: 11704
diff changeset
   109
    apply (subst lfp_conv_while [where ?U = "{0, 1, 2, 3, 4, 5}"])
10997
e14029f92770 avoid dead code;
wenzelm
parents: 10984
diff changeset
   110
       apply (rule monoI)
e14029f92770 avoid dead code;
wenzelm
parents: 10984
diff changeset
   111
      apply blast
e14029f92770 avoid dead code;
wenzelm
parents: 10984
diff changeset
   112
     apply simp
e14029f92770 avoid dead code;
wenzelm
parents: 10984
diff changeset
   113
    apply (simp add: aux set_eq_subset)
e14029f92770 avoid dead code;
wenzelm
parents: 10984
diff changeset
   114
    txt {* The fixpoint computation is performed purely by rewriting: *}
15197
19e735596e51 Added antisymmetry simproc
nipkow
parents: 15140
diff changeset
   115
    apply (simp add: while_unfold aux seteq del: subset_empty)
10997
e14029f92770 avoid dead code;
wenzelm
parents: 10984
diff changeset
   116
    done
e14029f92770 avoid dead code;
wenzelm
parents: 10984
diff changeset
   117
qed
10251
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
   118
5cc44cae9590 A general ``while'' combinator (from main HOL);
wenzelm
parents:
diff changeset
   119
end