src/HOL/Library/While_Combinator.thy
 author wenzelm Wed Oct 18 23:29:49 2000 +0200 (2000-10-18) changeset 10251 5cc44cae9590 child 10269 cc20c9d7e682 permissions -rw-r--r--
A general ``while'' combinator (from main HOL);
```     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 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 = 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 = 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 constdefs
```
```    33   while :: "('a => bool) => ('a => 'a) => 'a => 'a"
```
```    34   "while b c s == while_aux (b, c, s)"
```
```    35
```
```    36 ML_setup {*
```
```    37   goalw_cterm [] (cterm_of (sign_of (the_context ()))
```
```    38     (HOLogic.mk_Trueprop (hd while_aux.tcs)));
```
```    39   br wf_same_fstI 1;
```
```    40   br wf_same_fstI 1;
```
```    41   by (asm_full_simp_tac (simpset() addsimps [wf_iff_no_infinite_down_chain]) 1);
```
```    42   by (Blast_tac 1);
```
```    43   qed "while_aux_tc";
```
```    44 *} (* FIXME cannot prove recdef tcs in Isar yet! *)
```
```    45
```
```    46 lemma while_aux_unfold:
```
```    47   "while_aux (b, c, s) =
```
```    48     (if \<exists>f. f 0 = s \<and> (\<forall>i. b (f i) \<and> c (f i) = f (i + 1))
```
```    49       then arbitrary
```
```    50       else if b s then while_aux (b, c, c s)
```
```    51       else s)"
```
```    52   apply (rule while_aux_tc [THEN while_aux.simps [THEN trans]])
```
```    53    apply (simp add: same_fst_def)
```
```    54   apply (rule refl)
```
```    55   done
```
```    56
```
```    57 text {*
```
```    58  The recursion equation for @{term while}: directly executable!
```
```    59 *}
```
```    60
```
```    61 theorem while_unfold:
```
```    62     "while b c s = (if b s then while b c (c s) else s)"
```
```    63   apply (unfold while_def)
```
```    64   apply (rule while_aux_unfold [THEN trans])
```
```    65   apply auto
```
```    66   apply (subst while_aux_unfold)
```
```    67   apply simp
```
```    68   apply clarify
```
```    69   apply (erule_tac x = "\<lambda>i. f (Suc i)" in allE)
```
```    70   apply blast
```
```    71   done
```
```    72
```
```    73 text {*
```
```    74  The proof rule for @{term while}, where @{term P} is the invariant.
```
```    75 *}
```
```    76
```
```    77 theorem while_rule [rule_format]:
```
```    78   "(!!s. P s ==> b s ==> P (c s)) ==>
```
```    79     (!!s. P s ==> \<not> b s ==> Q s) ==>
```
```    80     wf {(t, s). P s \<and> b s \<and> t = c s} ==>
```
```    81     P s --> Q (while b c s)"
```
```    82 proof -
```
```    83   case antecedent
```
```    84   assume wf: "wf {(t, s). P s \<and> b s \<and> t = c s}"
```
```    85   show ?thesis
```
```    86     apply (induct s rule: wf [THEN wf_induct])
```
```    87     apply simp
```
```    88     apply clarify
```
```    89     apply (subst while_unfold)
```
```    90     apply (simp add: antecedent)
```
```    91     done
```
```    92 qed
```
```    93
```
```    94 hide const while_aux
```
```    95
```
```    96 text {*
```
```    97  \medskip An application: computation of the @{term lfp} on finite
```
```    98  sets via iteration.
```
```    99 *}
```
```   100
```
```   101 theorem lfp_conv_while:
```
```   102   "mono f ==> finite U ==> f U = U ==>
```
```   103     lfp f = fst (while (\<lambda>(A, fA). A \<noteq> fA) (\<lambda>(A, fA). (fA, f fA)) ({}, f {}))"
```
```   104   apply (rule_tac P =
```
```   105       "\<lambda>(A, B). (A \<subseteq> U \<and> B = f A \<and> A \<subseteq> B \<and> B \<subseteq> lfp f)" in while_rule)
```
```   106      apply (subst lfp_unfold)
```
```   107       apply assumption
```
```   108      apply clarsimp
```
```   109      apply (blast dest: monoD)
```
```   110     apply (fastsimp intro!: lfp_lowerbound)
```
```   111    apply (rule_tac r = "((Pow U <*> UNIV) <*> (Pow U <*> UNIV)) \<inter>
```
```   112        inv_image finite_psubset (op - U o fst)" in wf_subset)
```
```   113     apply (blast intro: wf_finite_psubset Int_lower2 [THEN [2] wf_subset])
```
```   114    apply (clarsimp simp add: inv_image_def finite_psubset_def order_less_le)
```
```   115    apply (blast intro!: finite_Diff dest: monoD)
```
```   116   apply (subst lfp_unfold)
```
```   117    apply assumption
```
```   118   apply (simp add: monoD)
```
```   119   done
```
```   120
```
```   121 end
```