Theory Loop

(*  Title:      HOL/HOLCF/ex/Loop.thy
    Author:     Franz Regensburger
*)

section ‹Theory for a loop primitive like while›

theory Loop
imports HOLCF
begin

definition
  step  :: "('a  tr)  ('a  'a)  'a  'a" where
  "step = (LAM b g x. If bx then gx else x)"

definition
  while :: "('a  tr)  ('a  'a)  'a  'a" where
  "while = (LAM b g. fix(LAM f x. If bx then f(gx) else x))"

(* ------------------------------------------------------------------------- *)
(* access to definitions                                                     *)
(* ------------------------------------------------------------------------- *)


lemma step_def2: "stepbgx = If bx then gx else x"
apply (unfold step_def)
apply simp
done

lemma while_def2: "whilebg = fix(LAM f x. If bx then f(gx) else x)"
apply (unfold while_def)
apply simp
done


(* ------------------------------------------------------------------------- *)
(* rekursive properties of while                                             *)
(* ------------------------------------------------------------------------- *)

lemma while_unfold: "whilebgx = If bx then whilebg(gx) else x"
apply (rule trans)
apply (rule while_def2 [THEN fix_eq5])
apply simp
done

lemma while_unfold2: "x. whilebgx = whilebg(iterate k(stepbg)x)"
apply (induct_tac k)
apply simp
apply (rule allI)
apply (rule trans)
apply (rule while_unfold)
apply (subst iterate_Suc2)
apply (rule trans)
apply (erule_tac [2] spec)
apply (subst step_def2)
apply (rule_tac p = "bx" in trE)
apply simp
apply (subst while_unfold)
apply (rule_tac s = "UU" and t = "bUU" in ssubst)
apply (erule strictI)
apply simp
apply simp
apply simp
apply (subst while_unfold)
apply simp
done

lemma while_unfold3: "whilebgx = whilebg(stepbgx)"
apply (rule_tac s = "whilebg(iterate (Suc 0)(stepbg)x)" in trans)
apply (rule while_unfold2 [THEN spec])
apply simp
done


(* ------------------------------------------------------------------------- *)
(* properties of while and iterations                                        *)
(* ------------------------------------------------------------------------- *)

lemma loop_lemma1: "y. by = FF; iterate k(stepbg)x = UU
      iterate(Suc k)(stepbg)x = UU"
apply (simp (no_asm))
apply (rule trans)
apply (rule step_def2)
apply simp
apply (erule exE)
apply (erule flat_codom [THEN disjE])
apply simp_all
done

lemma loop_lemma2: "y. by = FF; iterate (Suc k)(stepbg)x  UU 
      iterate k(stepbg)x  UU"
apply (blast intro: loop_lemma1)
done

lemma loop_lemma3 [rule_format (no_asm)]:
  "x. INV x  bx = TT  gx  UU  INV (gx);
         y. by = FF; INV x
       iterate k(stepbg)x  UU  INV (iterate k(stepbg)x)"
apply (induct_tac "k")
apply (simp (no_asm_simp))
apply (intro strip)
apply (simp (no_asm) add: step_def2)
apply (rule_tac p = "b(iterate n(stepbg)x)" in trE)
apply (erule notE)
apply (simp add: step_def2)
apply (simp (no_asm_simp))
apply (rule mp)
apply (erule spec)
apply (simp (no_asm_simp) del: iterate_Suc add: loop_lemma2)
apply (rule_tac s = "iterate (Suc n)(stepbg)x"
  and t = "g(iterate n(stepbg)x)" in ssubst)
prefer 2 apply (assumption)
apply (simp add: step_def2)
apply (drule (1) loop_lemma2, simp)
done

lemma loop_lemma4 [rule_format]:
  "x. b(iterate k(stepbg)x) = FF  whilebgx = iterate k(stepbg)x"
apply (induct_tac k)
apply (simp (no_asm))
apply (intro strip)
apply (simplesubst while_unfold)
apply simp
apply (rule allI)
apply (simplesubst iterate_Suc2)
apply (intro strip)
apply (rule trans)
apply (rule while_unfold3)
apply simp
done

lemma loop_lemma5 [rule_format (no_asm)]:
  "k. b(iterate k(stepbg)x)  FF 
    m. whilebg(iterate m(stepbg)x) = UU"
apply (simplesubst while_def2)
apply (rule fix_ind)
apply simp
apply simp
apply (rule allI)
apply (simp (no_asm))
apply (rule_tac p = "b(iterate m(stepbg)x)" in trE)
apply (simp (no_asm_simp))
apply (simp (no_asm_simp))
apply (rule_tac s = "xa(iterate (Suc m)(stepbg)x)" in trans)
apply (erule_tac [2] spec)
apply (rule cfun_arg_cong)
apply (rule trans)
apply (rule_tac [2] iterate_Suc [symmetric])
apply (simp add: step_def2)
apply blast
done

lemma loop_lemma6: "k. b(iterate k(stepbg)x)  FF  whilebgx = UU"
apply (rule_tac t = "x" in iterate_0 [THEN subst])
apply (erule loop_lemma5)
done

lemma loop_lemma7: "whilebgx  UU  k. b(iterate k(stepbg)x) = FF"
apply (blast intro: loop_lemma6)
done


(* ------------------------------------------------------------------------- *)
(* an invariant rule for loops                                               *)
(* ------------------------------------------------------------------------- *)

lemma loop_inv2:
"(y. INV y  by = TT  gy  UU  INV (gy));
    (y. INV y  by = FF  Q y);
    INV x; whilebgx  UU  Q (whilebgx)"
apply (rule_tac P = "λk. b(iterate k(stepbg)x) = FF" in exE)
apply (erule loop_lemma7)
apply (simplesubst loop_lemma4)
apply assumption
apply (drule spec, erule mp)
apply (rule conjI)
prefer 2 apply (assumption)
apply (rule loop_lemma3)
apply assumption
apply (blast intro: loop_lemma6)
apply assumption
apply (rotate_tac -1)
apply (simp add: loop_lemma4)
done

lemma loop_inv:
  assumes premP: "P(x)"
    and premI: "y. P y  INV y"
    and premTT: "y. INV y; by = TT; gy  UU  INV (gy)"
    and premFF: "y. INV y; by = FF  Q y"
    and premW: "whilebgx  UU"
  shows "Q (whilebgx)"
apply (rule loop_inv2)
apply (rule_tac [3] premP [THEN premI])
apply (rule_tac [3] premW)
apply (blast intro: premTT)
apply (blast intro: premFF)
done

end