A general ``while'' combinator (from main HOL);
authorwenzelm
Wed Oct 18 23:29:49 2000 +0200 (2000-10-18)
changeset 102515cc44cae9590
parent 10250 ca93fe25a84b
child 10252 dd46544e259d
A general ``while'' combinator (from main HOL);
src/HOL/Library/While_Combinator.thy
src/HOL/Library/While_Combinator_Example.thy
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Library/While_Combinator.thy	Wed Oct 18 23:29:49 2000 +0200
     1.3 @@ -0,0 +1,121 @@
     1.4 +(*  Title:      HOL/Library/While.thy
     1.5 +    ID:         $Id$
     1.6 +    Author:     Tobias Nipkow
     1.7 +    Copyright   2000 TU Muenchen
     1.8 +*)
     1.9 +
    1.10 +header {*
    1.11 + \title{A general ``while'' combinator}
    1.12 + \author{Tobias Nipkow}
    1.13 +*}
    1.14 +
    1.15 +theory While_Combinator = Main:
    1.16 +
    1.17 +text {*
    1.18 + We define a while-combinator @{term while} and prove: (a) an
    1.19 + unrestricted unfolding law (even if while diverges!)  (I got this
    1.20 + idea from Wolfgang Goerigk), and (b) the invariant rule for reasoning
    1.21 + about @{term while}.
    1.22 +*}
    1.23 +
    1.24 +consts while_aux :: "('a => bool) \<times> ('a => 'a) \<times> 'a => 'a"
    1.25 +recdef while_aux
    1.26 +  "same_fst (\<lambda>b. True) (\<lambda>b. same_fst (\<lambda>c. True) (\<lambda>c.
    1.27 +      {(t, s).  b s \<and> c s = t \<and>
    1.28 +        \<not> (\<exists>f. f 0 = s \<and> (\<forall>i. b (f i) \<and> c (f i) = f (i + 1)))}))"
    1.29 +  "while_aux (b, c, s) =
    1.30 +    (if (\<exists>f. f 0 = s \<and> (\<forall>i. b (f i) \<and> c (f i) = f (i + 1)))
    1.31 +      then arbitrary
    1.32 +      else if b s then while_aux (b, c, c s)
    1.33 +      else s)"
    1.34 +
    1.35 +constdefs
    1.36 +  while :: "('a => bool) => ('a => 'a) => 'a => 'a"
    1.37 +  "while b c s == while_aux (b, c, s)"
    1.38 +
    1.39 +ML_setup {*
    1.40 +  goalw_cterm [] (cterm_of (sign_of (the_context ()))
    1.41 +    (HOLogic.mk_Trueprop (hd while_aux.tcs)));
    1.42 +  br wf_same_fstI 1;
    1.43 +  br wf_same_fstI 1;
    1.44 +  by (asm_full_simp_tac (simpset() addsimps [wf_iff_no_infinite_down_chain]) 1);
    1.45 +  by (Blast_tac 1);
    1.46 +  qed "while_aux_tc";
    1.47 +*} (* FIXME cannot prove recdef tcs in Isar yet! *)
    1.48 +
    1.49 +lemma while_aux_unfold:
    1.50 +  "while_aux (b, c, s) =
    1.51 +    (if \<exists>f. f 0 = s \<and> (\<forall>i. b (f i) \<and> c (f i) = f (i + 1))
    1.52 +      then arbitrary
    1.53 +      else if b s then while_aux (b, c, c s)
    1.54 +      else s)"
    1.55 +  apply (rule while_aux_tc [THEN while_aux.simps [THEN trans]])
    1.56 +   apply (simp add: same_fst_def)
    1.57 +  apply (rule refl)
    1.58 +  done
    1.59 +
    1.60 +text {*
    1.61 + The recursion equation for @{term while}: directly executable!
    1.62 +*}
    1.63 +
    1.64 +theorem while_unfold:
    1.65 +    "while b c s = (if b s then while b c (c s) else s)"
    1.66 +  apply (unfold while_def)
    1.67 +  apply (rule while_aux_unfold [THEN trans])
    1.68 +  apply auto
    1.69 +  apply (subst while_aux_unfold)
    1.70 +  apply simp
    1.71 +  apply clarify
    1.72 +  apply (erule_tac x = "\<lambda>i. f (Suc i)" in allE)
    1.73 +  apply blast
    1.74 +  done
    1.75 +
    1.76 +text {*
    1.77 + The proof rule for @{term while}, where @{term P} is the invariant.
    1.78 +*}
    1.79 +
    1.80 +theorem while_rule [rule_format]:
    1.81 +  "(!!s. P s ==> b s ==> P (c s)) ==>
    1.82 +    (!!s. P s ==> \<not> b s ==> Q s) ==>
    1.83 +    wf {(t, s). P s \<and> b s \<and> t = c s} ==>
    1.84 +    P s --> Q (while b c s)"
    1.85 +proof -
    1.86 +  case antecedent
    1.87 +  assume wf: "wf {(t, s). P s \<and> b s \<and> t = c s}"
    1.88 +  show ?thesis
    1.89 +    apply (induct s rule: wf [THEN wf_induct])
    1.90 +    apply simp
    1.91 +    apply clarify
    1.92 +    apply (subst while_unfold)
    1.93 +    apply (simp add: antecedent)
    1.94 +    done
    1.95 +qed
    1.96 +
    1.97 +hide const while_aux
    1.98 +
    1.99 +text {*
   1.100 + \medskip An application: computation of the @{term lfp} on finite
   1.101 + sets via iteration.
   1.102 +*}
   1.103 +
   1.104 +theorem lfp_conv_while:
   1.105 +  "mono f ==> finite U ==> f U = U ==>
   1.106 +    lfp f = fst (while (\<lambda>(A, fA). A \<noteq> fA) (\<lambda>(A, fA). (fA, f fA)) ({}, f {}))"
   1.107 +  apply (rule_tac P =
   1.108 +      "\<lambda>(A, B). (A \<subseteq> U \<and> B = f A \<and> A \<subseteq> B \<and> B \<subseteq> lfp f)" in while_rule)
   1.109 +     apply (subst lfp_unfold)
   1.110 +      apply assumption
   1.111 +     apply clarsimp
   1.112 +     apply (blast dest: monoD)
   1.113 +    apply (fastsimp intro!: lfp_lowerbound)
   1.114 +   apply (rule_tac r = "((Pow U <*> UNIV) <*> (Pow U <*> UNIV)) \<inter>
   1.115 +       inv_image finite_psubset (op - U o fst)" in wf_subset)
   1.116 +    apply (blast intro: wf_finite_psubset Int_lower2 [THEN [2] wf_subset])
   1.117 +   apply (clarsimp simp add: inv_image_def finite_psubset_def order_less_le)
   1.118 +   apply (blast intro!: finite_Diff dest: monoD)
   1.119 +  apply (subst lfp_unfold)
   1.120 +   apply assumption
   1.121 +  apply (simp add: monoD)
   1.122 +  done
   1.123 +
   1.124 +end
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/HOL/Library/While_Combinator_Example.thy	Wed Oct 18 23:29:49 2000 +0200
     2.3 @@ -0,0 +1,25 @@
     2.4 +
     2.5 +header {* \title{} *}
     2.6 +
     2.7 +theory While_Combinator_Example = While_Combinator:
     2.8 +
     2.9 +text {*
    2.10 + An example of using the @{term while} combinator.
    2.11 +*}
    2.12 +
    2.13 +lemma aux: "{f n| n. A n \<or> B n} = {f n| n. A n} \<union> {f n| n. B n}"
    2.14 +  apply blast
    2.15 +  done
    2.16 +
    2.17 +theorem "P (lfp (\<lambda>N::int set. {#0} \<union> {(n + #2) mod #6| n. n \<in> N})) =
    2.18 +    P {#0, #4, #2}"
    2.19 +  apply (subst lfp_conv_while [where ?U = "{#0, #1, #2, #3, #4, #5}"])
    2.20 +     apply (rule monoI)
    2.21 +    apply blast
    2.22 +   apply simp
    2.23 +  apply (simp add: aux set_eq_subset)
    2.24 +  txt {* The fixpoint computation is performed purely by rewriting: *}
    2.25 +  apply (simp add: while_unfold aux set_eq_subset del: subset_empty)
    2.26 +  done
    2.27 +
    2.28 +end