# Theory While_Combinator_Example

theory While_Combinator_Example
imports While_Combinator
```(*  Title:      HOL/ex/While_Combinator_Example.thy
Author:     Tobias Nipkow
*)

section ‹An application of the While combinator›

theory While_Combinator_Example
imports "HOL-Library.While_Combinator"
begin

text ‹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 ((-) U o fst)" in while_rule)
apply (subst lfp_unfold)
apply assumption
apply (subst lfp_unfold)
apply assumption
apply clarsimp
apply (blast dest: monoD)
apply (fastforce 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 dest: monoD)
done

subsection ‹Example›

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