Theory While_Combinator

Up to index of Isabelle/HOL/MicroJava

theory While_Combinator
imports Main
begin

(*  Title:      HOL/Library/While_Combinator.thy
    ID:         $Id: While_Combinator.thy,v 1.28 2007/07/16 19:39:56 krauss Exp $
    Author:     Tobias Nipkow
    Copyright   2000 TU Muenchen
*)

header {* A general ``while'' combinator *}

theory While_Combinator
imports Main
begin

text {* 
  We define the while combinator as the "mother of all tail recursive functions".
*}

function (tailrec) while :: "('a => bool) => ('a => 'a) => 'a => 'a"
where
  while_unfold[simp del]: "while b c s = (if b s then while b c (c s) else s)"
by auto

declare while_unfold[code]

lemma def_while_unfold:
  assumes fdef: "f == while test do"
  shows "f x = (if test x then f(do x) else x)"
proof -
  have "f x = while test do x" using fdef by simp
  also have "… = (if test x then while test do (do x) else x)"
    by(rule while_unfold)
  also have "… = (if test x then f(do x) else x)" by(simp add:fdef[symmetric])
  finally show ?thesis .
qed


text {*
 The proof rule for @{term while}, where @{term P} is the invariant.
*}

theorem while_rule_lemma:
  assumes invariant: "!!s. P s ==> b s ==> P (c s)"
    and terminate: "!!s. P s ==> ¬ b s ==> Q s"
    and wf: "wf {(t, s). P s ∧ b s ∧ t = c s}"
  shows "P s ==> Q (while b c s)"
  using wf
  apply (induct s)
  apply simp
  apply (subst while_unfold)
  apply (simp add: invariant terminate)
  done

theorem while_rule:
  "[| P s;
      !!s. [| P s; b s  |] ==> P (c s);
      !!s. [| P s; ¬ b s  |] ==> Q s;
      wf r;
      !!s. [| P s; b s  |] ==> (c s, s) ∈ r |] ==>
   Q (while b c s)"
  apply (rule while_rule_lemma)
     prefer 4 apply assumption
    apply blast
   apply blast
  apply (erule wf_subset)
  apply blast
  done

text {*
 \medskip An application: computation of the @{term lfp} on finite
 sets via iteration.
*}

theorem lfp_conv_while:
  "[| mono f; finite U; f U = U |] ==>
    lfp f = fst (while (λ(A, fA). A ≠ fA) (λ(A, fA). (fA, f fA)) ({}, f {}))"
apply (rule_tac P = "λ(A, B). (A ⊆ U ∧ B = f A ∧ A ⊆ B ∧ B ⊆ lfp f)" and
                r = "((Pow U × UNIV) × (Pow U × UNIV)) ∩
                     inv_image finite_psubset (op - U o fst)" in while_rule)
   apply (subst lfp_unfold)
    apply assumption
   apply (simp add: monoD)
  apply (subst lfp_unfold)
   apply assumption
  apply clarsimp
  apply (blast dest: monoD)
 apply (fastsimp intro!: lfp_lowerbound)
 apply (blast intro: wf_finite_psubset Int_lower2 [THEN [2] wf_subset])
apply (clarsimp simp add: finite_psubset_def order_less_le)
apply (blast intro!: finite_Diff dest: monoD)
done


text {*
 An example of using the @{term while} combinator.
*}

text{* Cannot use @{thm[source]set_eq_subset} because it leads to
looping because the antisymmetry simproc turns the subset relationship
back into equality. *}

theorem "P (lfp (λN::int set. {0} ∪ {(n + 2) mod 6 | n. n ∈ N})) =
  P {0, 4, 2}"
proof -
  have seteq: "!!A B. (A = B) = ((!a : A. a:B) & (!b:B. b:A))"
    by blast
  have aux: "!!f A B. {f n | n. A n ∨ B n} = {f n | n. A n} ∪ {f n | n. B n}"
    apply blast
    done
  show ?thesis
    apply (subst lfp_conv_while [where ?U = "{0, 1, 2, 3, 4, 5}"])
       apply (rule monoI)
      apply blast
     apply simp
    apply (simp add: aux set_eq_subset)
    txt {* The fixpoint computation is performed purely by rewriting: *}
    apply (simp add: while_unfold aux seteq del: subset_empty)
    done
qed

end

lemma def_while_unfold:

  f == while test do ==> f x = (if test x then f (do x) else x)

theorem while_rule_lemma:

  [| !!s. [| P s; b s |] ==> P (c s); !!s. [| P s; ¬ b s |] ==> Q s;
     wf {(t, s). P sb st = c s}; P s |]
  ==> Q (while b c s)

theorem while_rule:

  [| P s; !!s. [| P s; b s |] ==> P (c s); !!s. [| P s; ¬ b s |] ==> Q s; wf r;
     !!s. [| P s; b s |] ==> (c s, s) ∈ r |]
  ==> Q (while b c s)

theorem lfp_conv_while:

  [| mono f; finite U; f U = U |]
  ==> lfp f = fst (while (λ(A, fA). A  fA) (λ(A, fA). (fA, f fA)) ({}, f {}))

theorem

  P (lfpN. {0} ∪ {(n + 2) mod 6 |n. nN})) = P {0, 4, 2}