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