src/HOL/Library/While_Combinator.thy
author nipkow
Thu Dec 18 08:20:36 2003 +0100 (2003-12-18)
changeset 14300 bf8b8c9425c3
parent 12791 ccc0f45ad2c4
child 14589 feae7b5fd425
permissions -rw-r--r--
*** empty log message ***
     1 (*  Title:      HOL/Library/While.thy
     2     ID:         $Id$
     3     Author:     Tobias Nipkow
     4     Copyright   2000 TU Muenchen
     5 *)
     6 
     7 header {*
     8  \title{A general ``while'' combinator}
     9  \author{Tobias Nipkow}
    10 *}
    11 
    12 theory While_Combinator = Main:
    13 
    14 text {*
    15  We define a while-combinator @{term while} and prove: (a) an
    16  unrestricted unfolding law (even if while diverges!)  (I got this
    17  idea from Wolfgang Goerigk), and (b) the invariant rule for reasoning
    18  about @{term while}.
    19 *}
    20 
    21 consts while_aux :: "('a => bool) \<times> ('a => 'a) \<times> 'a => 'a"
    22 recdef (permissive) while_aux
    23   "same_fst (\<lambda>b. True) (\<lambda>b. same_fst (\<lambda>c. True) (\<lambda>c.
    24       {(t, s).  b s \<and> c s = t \<and>
    25         \<not> (\<exists>f. f (0::nat) = s \<and> (\<forall>i. b (f i) \<and> c (f i) = f (i + 1)))}))"
    26   "while_aux (b, c, s) =
    27     (if (\<exists>f. f (0::nat) = s \<and> (\<forall>i. b (f i) \<and> c (f i) = f (i + 1)))
    28       then arbitrary
    29       else if b s then while_aux (b, c, c s)
    30       else s)"
    31 
    32 recdef_tc while_aux_tc: while_aux
    33   apply (rule wf_same_fst)
    34   apply (rule wf_same_fst)
    35   apply (simp add: wf_iff_no_infinite_down_chain)
    36   apply blast
    37   done
    38 
    39 constdefs
    40   while :: "('a => bool) => ('a => 'a) => 'a => 'a"
    41   "while b c s == while_aux (b, c, s)"
    42 
    43 lemma while_aux_unfold:
    44   "while_aux (b, c, s) =
    45     (if \<exists>f. f (0::nat) = s \<and> (\<forall>i. b (f i) \<and> c (f i) = f (i + 1))
    46       then arbitrary
    47       else if b s then while_aux (b, c, c s)
    48       else s)"
    49   apply (rule while_aux_tc [THEN while_aux.simps [THEN trans]])
    50   apply (rule refl)
    51   done
    52 
    53 text {*
    54  The recursion equation for @{term while}: directly executable!
    55 *}
    56 
    57 theorem while_unfold [code]:
    58     "while b c s = (if b s then while b c (c s) else s)"
    59   apply (unfold while_def)
    60   apply (rule while_aux_unfold [THEN trans])
    61   apply auto
    62   apply (subst while_aux_unfold)
    63   apply simp
    64   apply clarify
    65   apply (erule_tac x = "\<lambda>i. f (Suc i)" in allE)
    66   apply blast
    67   done
    68 
    69 hide const while_aux
    70 
    71 lemma def_while_unfold: assumes fdef: "f == while test do"
    72       shows "f x = (if test x then f(do x) else x)"
    73 proof -
    74   have "f x = while test do x" using fdef by simp
    75   also have "\<dots> = (if test x then while test do (do x) else x)"
    76     by(rule while_unfold)
    77   also have "\<dots> = (if test x then f(do x) else x)" by(simp add:fdef[symmetric])
    78   finally show ?thesis .
    79 qed
    80 
    81 
    82 text {*
    83  The proof rule for @{term while}, where @{term P} is the invariant.
    84 *}
    85 
    86 theorem while_rule_lemma[rule_format]:
    87   "[| !!s. P s ==> b s ==> P (c s);
    88       !!s. P s ==> \<not> b s ==> Q s;
    89       wf {(t, s). P s \<and> b s \<and> t = c s} |] ==>
    90     P s --> Q (while b c s)"
    91 proof -
    92   case rule_context
    93   assume wf: "wf {(t, s). P s \<and> b s \<and> t = c s}"
    94   show ?thesis
    95     apply (induct s rule: wf [THEN wf_induct])
    96     apply simp
    97     apply clarify
    98     apply (subst while_unfold)
    99     apply (simp add: rule_context)
   100     done
   101 qed
   102 
   103 theorem while_rule:
   104   "[| P s;
   105       !!s. [| P s; b s  |] ==> P (c s);
   106       !!s. [| P s; \<not> b s  |] ==> Q s;
   107       wf r;
   108       !!s. [| P s; b s  |] ==> (c s, s) \<in> r |] ==>
   109    Q (while b c s)"
   110 apply (rule while_rule_lemma)
   111 prefer 4 apply assumption
   112 apply blast
   113 apply blast
   114 apply(erule wf_subset)
   115 apply blast
   116 done
   117 
   118 text {*
   119  \medskip An application: computation of the @{term lfp} on finite
   120  sets via iteration.
   121 *}
   122 
   123 theorem lfp_conv_while:
   124   "[| mono f; finite U; f U = U |] ==>
   125     lfp f = fst (while (\<lambda>(A, fA). A \<noteq> fA) (\<lambda>(A, fA). (fA, f fA)) ({}, f {}))"
   126 apply (rule_tac P = "\<lambda>(A, B). (A \<subseteq> U \<and> B = f A \<and> A \<subseteq> B \<and> B \<subseteq> lfp f)" and
   127                 r = "((Pow U \<times> UNIV) \<times> (Pow U \<times> UNIV)) \<inter>
   128                      inv_image finite_psubset (op - U o fst)" in while_rule)
   129    apply (subst lfp_unfold)
   130     apply assumption
   131    apply (simp add: monoD)
   132   apply (subst lfp_unfold)
   133    apply assumption
   134   apply clarsimp
   135   apply (blast dest: monoD)
   136  apply (fastsimp intro!: lfp_lowerbound)
   137  apply (blast intro: wf_finite_psubset Int_lower2 [THEN [2] wf_subset])
   138 apply (clarsimp simp add: inv_image_def finite_psubset_def order_less_le)
   139 apply (blast intro!: finite_Diff dest: monoD)
   140 done
   141 
   142 
   143 text {*
   144  An example of using the @{term while} combinator.\footnote{It is safe
   145  to keep the example here, since there is no effect on the current
   146  theory.}
   147 *}
   148 
   149 theorem "P (lfp (\<lambda>N::int set. {0} \<union> {(n + 2) mod 6 | n. n \<in> N})) = P {0, 4, 2}"
   150 proof -
   151   have aux: "!!f A B. {f n | n. A n \<or> B n} = {f n | n. A n} \<union> {f n | n. B n}"
   152     apply blast
   153     done
   154   show ?thesis
   155     apply (subst lfp_conv_while [where ?U = "{0, 1, 2, 3, 4, 5}"])
   156        apply (rule monoI)
   157       apply blast
   158      apply simp
   159     apply (simp add: aux set_eq_subset)
   160     txt {* The fixpoint computation is performed purely by rewriting: *}
   161     apply (simp add: while_unfold aux set_eq_subset del: subset_empty)
   162     done
   163 qed
   164 
   165 end