src/HOL/Library/While_Combinator.thy
author paulson
Tue Jun 28 15:27:45 2005 +0200 (2005-06-28)
changeset 16587 b34c8aa657a5
parent 15197 19e735596e51
child 18372 2bffdf62fe7f
permissions -rw-r--r--
Constant "If" is now local
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@10251
    38
constdefs
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
nipkow@14300
    70
lemma def_while_unfold: assumes fdef: "f == while test do"
nipkow@14300
    71
      shows "f x = (if test x then f(do x) else x)"
nipkow@14300
    72
proof -
nipkow@14300
    73
  have "f x = while test do x" using fdef by simp
nipkow@14300
    74
  also have "\<dots> = (if test x then while test do (do x) else x)"
nipkow@14300
    75
    by(rule while_unfold)
nipkow@14300
    76
  also have "\<dots> = (if test x then f(do x) else x)" by(simp add:fdef[symmetric])
nipkow@14300
    77
  finally show ?thesis .
nipkow@14300
    78
qed
nipkow@14300
    79
nipkow@14300
    80
wenzelm@10251
    81
text {*
wenzelm@10251
    82
 The proof rule for @{term while}, where @{term P} is the invariant.
wenzelm@10251
    83
*}
wenzelm@10251
    84
nipkow@10653
    85
theorem while_rule_lemma[rule_format]:
nipkow@10984
    86
  "[| !!s. P s ==> b s ==> P (c s);
nipkow@10984
    87
      !!s. P s ==> \<not> b s ==> Q s;
nipkow@10984
    88
      wf {(t, s). P s \<and> b s \<and> t = c s} |] ==>
wenzelm@10251
    89
    P s --> Q (while b c s)"
wenzelm@10251
    90
proof -
wenzelm@11549
    91
  case rule_context
wenzelm@10251
    92
  assume wf: "wf {(t, s). P s \<and> b s \<and> t = c s}"
wenzelm@10251
    93
  show ?thesis
wenzelm@10251
    94
    apply (induct s rule: wf [THEN wf_induct])
wenzelm@10251
    95
    apply simp
wenzelm@10251
    96
    apply clarify
wenzelm@10251
    97
    apply (subst while_unfold)
wenzelm@11549
    98
    apply (simp add: rule_context)
wenzelm@10251
    99
    done
wenzelm@10251
   100
qed
wenzelm@10251
   101
nipkow@10653
   102
theorem while_rule:
nipkow@10984
   103
  "[| P s;
nipkow@10984
   104
      !!s. [| P s; b s  |] ==> P (c s);
nipkow@10984
   105
      !!s. [| P s; \<not> b s  |] ==> Q s;
wenzelm@10997
   106
      wf r;
nipkow@10984
   107
      !!s. [| P s; b s  |] ==> (c s, s) \<in> r |] ==>
nipkow@10984
   108
   Q (while b c s)"
nipkow@10653
   109
apply (rule while_rule_lemma)
nipkow@10653
   110
prefer 4 apply assumption
nipkow@10653
   111
apply blast
nipkow@10653
   112
apply blast
nipkow@10653
   113
apply(erule wf_subset)
nipkow@10653
   114
apply blast
nipkow@10653
   115
done
nipkow@10653
   116
nipkow@10984
   117
text {*
nipkow@10984
   118
 \medskip An application: computation of the @{term lfp} on finite
nipkow@10984
   119
 sets via iteration.
nipkow@10984
   120
*}
nipkow@10984
   121
nipkow@10984
   122
theorem lfp_conv_while:
nipkow@10984
   123
  "[| mono f; finite U; f U = U |] ==>
nipkow@10984
   124
    lfp f = fst (while (\<lambda>(A, fA). A \<noteq> fA) (\<lambda>(A, fA). (fA, f fA)) ({}, f {}))"
nipkow@10984
   125
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
   126
                r = "((Pow U \<times> UNIV) \<times> (Pow U \<times> UNIV)) \<inter>
nipkow@10984
   127
                     inv_image finite_psubset (op - U o fst)" in while_rule)
nipkow@10984
   128
   apply (subst lfp_unfold)
nipkow@10984
   129
    apply assumption
nipkow@10984
   130
   apply (simp add: monoD)
nipkow@10984
   131
  apply (subst lfp_unfold)
nipkow@10984
   132
   apply assumption
nipkow@10984
   133
  apply clarsimp
nipkow@10984
   134
  apply (blast dest: monoD)
nipkow@10984
   135
 apply (fastsimp intro!: lfp_lowerbound)
nipkow@10984
   136
 apply (blast intro: wf_finite_psubset Int_lower2 [THEN [2] wf_subset])
nipkow@10984
   137
apply (clarsimp simp add: inv_image_def finite_psubset_def order_less_le)
nipkow@10984
   138
apply (blast intro!: finite_Diff dest: monoD)
nipkow@10984
   139
done
nipkow@10984
   140
nipkow@10984
   141
nipkow@10984
   142
text {*
wenzelm@14589
   143
 An example of using the @{term while} combinator.
nipkow@10984
   144
*}
nipkow@10984
   145
nipkow@15197
   146
text{* Cannot use @{thm[source]set_eq_subset} because it leads to
nipkow@15197
   147
looping because the antisymmetry simproc turns the subset relationship
nipkow@15197
   148
back into equality. *}
nipkow@15197
   149
nipkow@15197
   150
lemma seteq: "(A = B) = ((!a : A. a:B) & (!b:B. b:A))"
nipkow@15197
   151
by blast
nipkow@15197
   152
wenzelm@14589
   153
theorem "P (lfp (\<lambda>N::int set. {0} \<union> {(n + 2) mod 6 | n. n \<in> N})) =
wenzelm@14589
   154
  P {0, 4, 2}"
wenzelm@10997
   155
proof -
wenzelm@10997
   156
  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
   157
    apply blast
wenzelm@10997
   158
    done
wenzelm@10997
   159
  show ?thesis
wenzelm@11914
   160
    apply (subst lfp_conv_while [where ?U = "{0, 1, 2, 3, 4, 5}"])
wenzelm@10997
   161
       apply (rule monoI)
wenzelm@10997
   162
      apply blast
wenzelm@10997
   163
     apply simp
wenzelm@10997
   164
    apply (simp add: aux set_eq_subset)
wenzelm@10997
   165
    txt {* The fixpoint computation is performed purely by rewriting: *}
nipkow@15197
   166
    apply (simp add: while_unfold aux seteq del: subset_empty)
wenzelm@10997
   167
    done
wenzelm@10997
   168
qed
wenzelm@10251
   169
wenzelm@10251
   170
end