src/HOL/Library/While_Combinator.thy
author wenzelm
Wed Jan 03 21:24:29 2001 +0100 (2001-01-03)
changeset 10774 4de3a0d3ae28
parent 10673 337c00fd385b
child 10984 8f49dcbec859
permissions -rw-r--r--
recdef_tc;
     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 recdef_tc while_aux_tc: while_aux
    33   apply (rule wf_same_fst)
    34   apply (rule wf_same_fst)
    35   apply (simp add: wf_iff_no_infinite_down_chain)
    36   apply blast
    37   done
    38 
    39 constdefs
    40   while :: "('a => bool) => ('a => 'a) => 'a => 'a"
    41   "while b c s == while_aux (b, c, s)"
    42 
    43 lemma while_aux_unfold:
    44   "while_aux (b, c, s) =
    45     (if \<exists>f. f 0 = s \<and> (\<forall>i. b (f i) \<and> c (f i) = f (i + 1))
    46       then arbitrary
    47       else if b s then while_aux (b, c, c s)
    48       else s)"
    49   apply (rule while_aux_tc [THEN while_aux.simps [THEN trans]])
    50    apply (simp add: same_fst_def)
    51   apply (rule refl)
    52   done
    53 
    54 text {*
    55  The recursion equation for @{term while}: directly executable!
    56 *}
    57 
    58 theorem while_unfold:
    59     "while b c s = (if b s then while b c (c s) else s)"
    60   apply (unfold while_def)
    61   apply (rule while_aux_unfold [THEN trans])
    62   apply auto
    63   apply (subst while_aux_unfold)
    64   apply simp
    65   apply clarify
    66   apply (erule_tac x = "\<lambda>i. f (Suc i)" in allE)
    67   apply blast
    68   done
    69 
    70 text {*
    71  The proof rule for @{term while}, where @{term P} is the invariant.
    72 *}
    73 
    74 theorem while_rule_lemma[rule_format]:
    75   "(!!s. P s ==> b s ==> P (c s)) ==>
    76     (!!s. P s ==> \<not> b s ==> Q s) ==>
    77     wf {(t, s). P s \<and> b s \<and> t = c s} ==>
    78     P s --> Q (while b c s)"
    79 proof -
    80   case antecedent
    81   assume wf: "wf {(t, s). P s \<and> b s \<and> t = c s}"
    82   show ?thesis
    83     apply (induct s rule: wf [THEN wf_induct])
    84     apply simp
    85     apply clarify
    86     apply (subst while_unfold)
    87     apply (simp add: antecedent)
    88     done
    89 qed
    90 
    91 theorem while_rule:
    92   "[| P s; !!s. [| P s; b s  |] ==> P (c s);
    93     !!s. [| P s; \<not> b s  |] ==> Q s;
    94     wf r;  !!s. [| P s; b s  |] ==> (c s, s) \<in> r |] ==>
    95     Q (while b c s)"
    96 apply (rule while_rule_lemma)
    97 prefer 4 apply assumption
    98 apply blast
    99 apply blast
   100 apply(erule wf_subset)
   101 apply blast
   102 done
   103 
   104 hide const while_aux
   105 
   106 end