1 (* Title: HOL/IMP/Examples.thy
3 Author: David von Oheimb, TUM
9 theory Examples imports Natural begin
12 factorial :: "loc => loc => com" where
13 "factorial a b = (b :== (%s. 1);
14 \<WHILE> (%s. s a ~= 0) \<DO>
15 (b :== (%s. s b * s a); a :== (%s. s a - 1)))"
17 declare update_def [simp]
19 subsection "An example due to Tony Hoare"
22 assumes 1: "!x. P x \<longrightarrow> Q x"
23 and 2: "\<langle>w,s\<rangle> \<longrightarrow>\<^sub>c t"
24 shows "w = While P c \<Longrightarrow> \<langle>While Q c,t\<rangle> \<longrightarrow>\<^sub>c u \<Longrightarrow> \<langle>While Q c,s\<rangle> \<longrightarrow>\<^sub>c u"
29 lemma lemma2 [rule_format (no_asm)]:
30 "[| !x. P x \<longrightarrow> Q x; \<langle>w,s\<rangle> \<longrightarrow>\<^sub>c u |] ==>
31 !c. w = While Q c \<longrightarrow> \<langle>While P c; While Q c,s\<rangle> \<longrightarrow>\<^sub>c u"
32 apply (erule evalc.induct)
33 apply (simp_all (no_asm_simp))
35 apply (case_tac "P s")
39 lemma Hoare_example: "!x. P x \<longrightarrow> Q x ==>
40 (\<langle>While P c; While Q c, s\<rangle> \<longrightarrow>\<^sub>c t) = (\<langle>While Q c, s\<rangle> \<longrightarrow>\<^sub>c t)"
41 by (blast intro: lemma1 lemma2 dest: semi [THEN iffD1])
44 subsection "Factorial"
46 lemma factorial_3: "a~=b ==>
47 \<langle>factorial a b, Mem(a:=3)\<rangle> \<longrightarrow>\<^sub>c Mem(b:=6, a:=0)"
48 by (simp add: factorial_def)
50 text {* the same in single step mode: *}
51 lemmas [simp del] = evalc_cases
52 lemma "a~=b \<Longrightarrow> \<langle>factorial a b, Mem(a:=3)\<rangle> \<longrightarrow>\<^sub>c Mem(b:=6, a:=0)"
53 apply (unfold factorial_def)
55 apply (rule evalc.intros)
56 apply (rule evalc.intros)
58 apply (rule evalc.intros)
60 apply (rule evalc.intros)
61 apply (rule evalc.intros)
63 apply (rule evalc.intros)
65 apply (rule evalc.intros)
67 apply (rule evalc.intros)
68 apply (rule evalc.intros)
70 apply (rule evalc.intros)
72 apply (rule evalc.intros)
74 apply (rule evalc.intros)
75 apply (rule evalc.intros)
77 apply (rule evalc.intros)
79 apply (rule evalc.intros)