src/HOL/Library/While_Combinator.thy
 author haftmann Thu Jun 26 10:07:01 2008 +0200 (2008-06-26 ago) changeset 27368 9f90ac19e32b parent 23821 2acd9d79d855 child 27487 c8a6ce181805 permissions -rw-r--r--
established Plain theory and image
```     1 (*  Title:      HOL/Library/While_Combinator.thy
```
```     2     ID:         \$Id\$
```
```     3     Author:     Tobias Nipkow
```
```     4     Copyright   2000 TU Muenchen
```
```     5 *)
```
```     6
```
```     7 header {* A general ``while'' combinator *}
```
```     8
```
```     9 theory While_Combinator
```
```    10 imports Plain Presburger
```
```    11 begin
```
```    12
```
```    13 text {*
```
```    14   We define the while combinator as the "mother of all tail recursive functions".
```
```    15 *}
```
```    16
```
```    17 function (tailrec) while :: "('a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a"
```
```    18 where
```
```    19   while_unfold[simp del]: "while b c s = (if b s then while b c (c s) else s)"
```
```    20 by auto
```
```    21
```
```    22 declare while_unfold[code]
```
```    23
```
```    24 lemma def_while_unfold:
```
```    25   assumes fdef: "f == while test do"
```
```    26   shows "f x = (if test x then f(do x) else x)"
```
```    27 proof -
```
```    28   have "f x = while test do x" using fdef by simp
```
```    29   also have "\<dots> = (if test x then while test do (do x) else x)"
```
```    30     by(rule while_unfold)
```
```    31   also have "\<dots> = (if test x then f(do x) else x)" by(simp add:fdef[symmetric])
```
```    32   finally show ?thesis .
```
```    33 qed
```
```    34
```
```    35
```
```    36 text {*
```
```    37  The proof rule for @{term while}, where @{term P} is the invariant.
```
```    38 *}
```
```    39
```
```    40 theorem while_rule_lemma:
```
```    41   assumes invariant: "!!s. P s ==> b s ==> P (c s)"
```
```    42     and terminate: "!!s. P s ==> \<not> b s ==> Q s"
```
```    43     and wf: "wf {(t, s). P s \<and> b s \<and> t = c s}"
```
```    44   shows "P s \<Longrightarrow> Q (while b c s)"
```
```    45   using wf
```
```    46   apply (induct s)
```
```    47   apply simp
```
```    48   apply (subst while_unfold)
```
```    49   apply (simp add: invariant terminate)
```
```    50   done
```
```    51
```
```    52 theorem while_rule:
```
```    53   "[| P s;
```
```    54       !!s. [| P s; b s  |] ==> P (c s);
```
```    55       !!s. [| P s; \<not> b s  |] ==> Q s;
```
```    56       wf r;
```
```    57       !!s. [| P s; b s  |] ==> (c s, s) \<in> r |] ==>
```
```    58    Q (while b c s)"
```
```    59   apply (rule while_rule_lemma)
```
```    60      prefer 4 apply assumption
```
```    61     apply blast
```
```    62    apply blast
```
```    63   apply (erule wf_subset)
```
```    64   apply blast
```
```    65   done
```
```    66
```
```    67 text {*
```
```    68  \medskip An application: computation of the @{term lfp} on finite
```
```    69  sets via iteration.
```
```    70 *}
```
```    71
```
```    72 theorem lfp_conv_while:
```
```    73   "[| mono f; finite U; f U = U |] ==>
```
```    74     lfp f = fst (while (\<lambda>(A, fA). A \<noteq> fA) (\<lambda>(A, fA). (fA, f fA)) ({}, f {}))"
```
```    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
```
```    76                 r = "((Pow U \<times> UNIV) \<times> (Pow U \<times> UNIV)) \<inter>
```
```    77                      inv_image finite_psubset (op - U o fst)" in while_rule)
```
```    78    apply (subst lfp_unfold)
```
```    79     apply assumption
```
```    80    apply (simp add: monoD)
```
```    81   apply (subst lfp_unfold)
```
```    82    apply assumption
```
```    83   apply clarsimp
```
```    84   apply (blast dest: monoD)
```
```    85  apply (fastsimp intro!: lfp_lowerbound)
```
```    86  apply (blast intro: wf_finite_psubset Int_lower2 [THEN  wf_subset])
```
```    87 apply (clarsimp simp add: finite_psubset_def order_less_le)
```
```    88 apply (blast intro!: finite_Diff dest: monoD)
```
```    89 done
```
```    90
```
```    91
```
```    92 text {*
```
```    93  An example of using the @{term while} combinator.
```
```    94 *}
```
```    95
```
```    96 text{* Cannot use @{thm[source]set_eq_subset} because it leads to
```
```    97 looping because the antisymmetry simproc turns the subset relationship
```
```    98 back into equality. *}
```
```    99
```
```   100 theorem "P (lfp (\<lambda>N::int set. {0} \<union> {(n + 2) mod 6 | n. n \<in> N})) =
```
```   101   P {0, 4, 2}"
```
```   102 proof -
```
```   103   have seteq: "!!A B. (A = B) = ((!a : A. a:B) & (!b:B. b:A))"
```
```   104     by blast
```
```   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}"
```
```   106     apply blast
```
```   107     done
```
```   108   show ?thesis
```
```   109     apply (subst lfp_conv_while [where ?U = "{0, 1, 2, 3, 4, 5}"])
```
```   110        apply (rule monoI)
```
```   111       apply blast
```
```   112      apply simp
```
```   113     apply (simp add: aux set_eq_subset)
```
```   114     txt {* The fixpoint computation is performed purely by rewriting: *}
```
```   115     apply (simp add: while_unfold aux seteq del: subset_empty)
```
```   116     done
```
```   117 qed
```
```   118
```
```   119 end
```