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