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