7 theory Loop |
7 theory Loop |
8 imports HOLCF |
8 imports HOLCF |
9 begin |
9 begin |
10 |
10 |
11 definition |
11 definition |
12 step :: "('a -> tr)->('a -> 'a)->'a->'a" where |
12 step :: "('a \<rightarrow> tr) \<rightarrow> ('a \<rightarrow> 'a) \<rightarrow> 'a \<rightarrow> 'a" where |
13 "step = (LAM b g x. If b$x then g$x else x)" |
13 "step = (LAM b g x. If b\<cdot>x then g\<cdot>x else x)" |
14 |
14 |
15 definition |
15 definition |
16 while :: "('a -> tr)->('a -> 'a)->'a->'a" where |
16 while :: "('a \<rightarrow> tr) \<rightarrow> ('a \<rightarrow> 'a) \<rightarrow> 'a \<rightarrow> 'a" where |
17 "while = (LAM b g. fix$(LAM f x. If b$x then f$(g$x) else x))" |
17 "while = (LAM b g. fix\<cdot>(LAM f x. If b\<cdot>x then f\<cdot>(g\<cdot>x) else x))" |
18 |
18 |
19 (* ------------------------------------------------------------------------- *) |
19 (* ------------------------------------------------------------------------- *) |
20 (* access to definitions *) |
20 (* access to definitions *) |
21 (* ------------------------------------------------------------------------- *) |
21 (* ------------------------------------------------------------------------- *) |
22 |
22 |
23 |
23 |
24 lemma step_def2: "step$b$g$x = If b$x then g$x else x" |
24 lemma step_def2: "step\<cdot>b\<cdot>g\<cdot>x = If b\<cdot>x then g\<cdot>x else x" |
25 apply (unfold step_def) |
25 apply (unfold step_def) |
26 apply simp |
26 apply simp |
27 done |
27 done |
28 |
28 |
29 lemma while_def2: "while$b$g = fix$(LAM f x. If b$x then f$(g$x) else x)" |
29 lemma while_def2: "while\<cdot>b\<cdot>g = fix\<cdot>(LAM f x. If b\<cdot>x then f\<cdot>(g\<cdot>x) else x)" |
30 apply (unfold while_def) |
30 apply (unfold while_def) |
31 apply simp |
31 apply simp |
32 done |
32 done |
33 |
33 |
34 |
34 |
35 (* ------------------------------------------------------------------------- *) |
35 (* ------------------------------------------------------------------------- *) |
36 (* rekursive properties of while *) |
36 (* rekursive properties of while *) |
37 (* ------------------------------------------------------------------------- *) |
37 (* ------------------------------------------------------------------------- *) |
38 |
38 |
39 lemma while_unfold: "while$b$g$x = If b$x then while$b$g$(g$x) else x" |
39 lemma while_unfold: "while\<cdot>b\<cdot>g\<cdot>x = If b\<cdot>x then while\<cdot>b\<cdot>g\<cdot>(g\<cdot>x) else x" |
40 apply (rule trans) |
40 apply (rule trans) |
41 apply (rule while_def2 [THEN fix_eq5]) |
41 apply (rule while_def2 [THEN fix_eq5]) |
42 apply simp |
42 apply simp |
43 done |
43 done |
44 |
44 |
45 lemma while_unfold2: "ALL x. while$b$g$x = while$b$g$(iterate k$(step$b$g)$x)" |
45 lemma while_unfold2: "\<forall>x. while\<cdot>b\<cdot>g\<cdot>x = while\<cdot>b\<cdot>g\<cdot>(iterate k\<cdot>(step\<cdot>b\<cdot>g)\<cdot>x)" |
46 apply (induct_tac k) |
46 apply (induct_tac k) |
47 apply simp |
47 apply simp |
48 apply (rule allI) |
48 apply (rule allI) |
49 apply (rule trans) |
49 apply (rule trans) |
50 apply (rule while_unfold) |
50 apply (rule while_unfold) |
51 apply (subst iterate_Suc2) |
51 apply (subst iterate_Suc2) |
52 apply (rule trans) |
52 apply (rule trans) |
53 apply (erule_tac [2] spec) |
53 apply (erule_tac [2] spec) |
54 apply (subst step_def2) |
54 apply (subst step_def2) |
55 apply (rule_tac p = "b$x" in trE) |
55 apply (rule_tac p = "b\<cdot>x" in trE) |
56 apply simp |
56 apply simp |
57 apply (subst while_unfold) |
57 apply (subst while_unfold) |
58 apply (rule_tac s = "UU" and t = "b$UU" in ssubst) |
58 apply (rule_tac s = "UU" and t = "b\<cdot>UU" in ssubst) |
59 apply (erule strictI) |
59 apply (erule strictI) |
60 apply simp |
60 apply simp |
61 apply simp |
61 apply simp |
62 apply simp |
62 apply simp |
63 apply (subst while_unfold) |
63 apply (subst while_unfold) |
64 apply simp |
64 apply simp |
65 done |
65 done |
66 |
66 |
67 lemma while_unfold3: "while$b$g$x = while$b$g$(step$b$g$x)" |
67 lemma while_unfold3: "while\<cdot>b\<cdot>g\<cdot>x = while\<cdot>b\<cdot>g\<cdot>(step\<cdot>b\<cdot>g\<cdot>x)" |
68 apply (rule_tac s = "while$b$g$ (iterate (Suc 0) $ (step$b$g) $x) " in trans) |
68 apply (rule_tac s = "while\<cdot>b\<cdot>g\<cdot>(iterate (Suc 0)\<cdot>(step\<cdot>b\<cdot>g)\<cdot>x)" in trans) |
69 apply (rule while_unfold2 [THEN spec]) |
69 apply (rule while_unfold2 [THEN spec]) |
70 apply simp |
70 apply simp |
71 done |
71 done |
72 |
72 |
73 |
73 |
74 (* ------------------------------------------------------------------------- *) |
74 (* ------------------------------------------------------------------------- *) |
75 (* properties of while and iterations *) |
75 (* properties of while and iterations *) |
76 (* ------------------------------------------------------------------------- *) |
76 (* ------------------------------------------------------------------------- *) |
77 |
77 |
78 lemma loop_lemma1: "[| EX y. b$y=FF; iterate k$(step$b$g)$x = UU |] |
78 lemma loop_lemma1: "\<lbrakk>EX y. b\<cdot>y = FF; iterate k\<cdot>(step\<cdot>b\<cdot>g)\<cdot>x = UU\<rbrakk> |
79 ==>iterate(Suc k)$(step$b$g)$x=UU" |
79 \<Longrightarrow> iterate(Suc k)\<cdot>(step\<cdot>b\<cdot>g)\<cdot>x = UU" |
80 apply (simp (no_asm)) |
80 apply (simp (no_asm)) |
81 apply (rule trans) |
81 apply (rule trans) |
82 apply (rule step_def2) |
82 apply (rule step_def2) |
83 apply simp |
83 apply simp |
84 apply (erule exE) |
84 apply (erule exE) |
85 apply (erule flat_codom [THEN disjE]) |
85 apply (erule flat_codom [THEN disjE]) |
86 apply simp_all |
86 apply simp_all |
87 done |
87 done |
88 |
88 |
89 lemma loop_lemma2: "[|EX y. b$y=FF;iterate (Suc k)$(step$b$g)$x ~=UU |]==> |
89 lemma loop_lemma2: "\<lbrakk>\<exists>y. b\<cdot>y = FF; iterate (Suc k)\<cdot>(step\<cdot>b\<cdot>g)\<cdot>x \<noteq> UU\<rbrakk> \<Longrightarrow> |
90 iterate k$(step$b$g)$x ~=UU" |
90 iterate k\<cdot>(step\<cdot>b\<cdot>g)\<cdot>x \<noteq> UU" |
91 apply (blast intro: loop_lemma1) |
91 apply (blast intro: loop_lemma1) |
92 done |
92 done |
93 |
93 |
94 lemma loop_lemma3 [rule_format (no_asm)]: |
94 lemma loop_lemma3 [rule_format (no_asm)]: |
95 "[| ALL x. INV x & b$x=TT & g$x~=UU --> INV (g$x); |
95 "\<lbrakk>\<forall>x. INV x \<and> b\<cdot>x = TT \<and> g\<cdot>x \<noteq> UU \<longrightarrow> INV (g\<cdot>x); |
96 EX y. b$y=FF; INV x |] |
96 \<exists>y. b\<cdot>y = FF; INV x\<rbrakk> |
97 ==> iterate k$(step$b$g)$x ~=UU --> INV (iterate k$(step$b$g)$x)" |
97 \<Longrightarrow> iterate k\<cdot>(step\<cdot>b\<cdot>g)\<cdot>x \<noteq> UU \<longrightarrow> INV (iterate k\<cdot>(step\<cdot>b\<cdot>g)\<cdot>x)" |
98 apply (induct_tac "k") |
98 apply (induct_tac "k") |
99 apply (simp (no_asm_simp)) |
99 apply (simp (no_asm_simp)) |
100 apply (intro strip) |
100 apply (intro strip) |
101 apply (simp (no_asm) add: step_def2) |
101 apply (simp (no_asm) add: step_def2) |
102 apply (rule_tac p = "b$ (iterate n$ (step$b$g) $x) " in trE) |
102 apply (rule_tac p = "b\<cdot>(iterate n\<cdot>(step\<cdot>b\<cdot>g)\<cdot>x)" in trE) |
103 apply (erule notE) |
103 apply (erule notE) |
104 apply (simp add: step_def2) |
104 apply (simp add: step_def2) |
105 apply (simp (no_asm_simp)) |
105 apply (simp (no_asm_simp)) |
106 apply (rule mp) |
106 apply (rule mp) |
107 apply (erule spec) |
107 apply (erule spec) |
108 apply (simp (no_asm_simp) del: iterate_Suc add: loop_lemma2) |
108 apply (simp (no_asm_simp) del: iterate_Suc add: loop_lemma2) |
109 apply (rule_tac s = "iterate (Suc n) $ (step$b$g) $x" |
109 apply (rule_tac s = "iterate (Suc n)\<cdot>(step\<cdot>b\<cdot>g)\<cdot>x" |
110 and t = "g$ (iterate n$ (step$b$g) $x) " in ssubst) |
110 and t = "g\<cdot>(iterate n\<cdot>(step\<cdot>b\<cdot>g)\<cdot>x)" in ssubst) |
111 prefer 2 apply (assumption) |
111 prefer 2 apply (assumption) |
112 apply (simp add: step_def2) |
112 apply (simp add: step_def2) |
113 apply (drule (1) loop_lemma2, simp) |
113 apply (drule (1) loop_lemma2, simp) |
114 done |
114 done |
115 |
115 |
116 lemma loop_lemma4 [rule_format]: |
116 lemma loop_lemma4 [rule_format]: |
117 "ALL x. b$(iterate k$(step$b$g)$x)=FF --> while$b$g$x= iterate k$(step$b$g)$x" |
117 "\<forall>x. b\<cdot>(iterate k\<cdot>(step\<cdot>b\<cdot>g)\<cdot>x) = FF \<longrightarrow> while\<cdot>b\<cdot>g\<cdot>x = iterate k\<cdot>(step\<cdot>b\<cdot>g)\<cdot>x" |
118 apply (induct_tac k) |
118 apply (induct_tac k) |
119 apply (simp (no_asm)) |
119 apply (simp (no_asm)) |
120 apply (intro strip) |
120 apply (intro strip) |
121 apply (simplesubst while_unfold) |
121 apply (simplesubst while_unfold) |
122 apply simp |
122 apply simp |
127 apply (rule while_unfold3) |
127 apply (rule while_unfold3) |
128 apply simp |
128 apply simp |
129 done |
129 done |
130 |
130 |
131 lemma loop_lemma5 [rule_format (no_asm)]: |
131 lemma loop_lemma5 [rule_format (no_asm)]: |
132 "ALL k. b$(iterate k$(step$b$g)$x) ~= FF ==> |
132 "\<forall>k. b\<cdot>(iterate k\<cdot>(step\<cdot>b\<cdot>g)\<cdot>x) \<noteq> FF \<Longrightarrow> |
133 ALL m. while$b$g$(iterate m$(step$b$g)$x)=UU" |
133 \<forall>m. while\<cdot>b\<cdot>g\<cdot>(iterate m\<cdot>(step\<cdot>b\<cdot>g)\<cdot>x) = UU" |
134 apply (simplesubst while_def2) |
134 apply (simplesubst while_def2) |
135 apply (rule fix_ind) |
135 apply (rule fix_ind) |
136 apply simp |
136 apply simp |
137 apply simp |
137 apply simp |
138 apply (rule allI) |
138 apply (rule allI) |
139 apply (simp (no_asm)) |
139 apply (simp (no_asm)) |
140 apply (rule_tac p = "b$ (iterate m$ (step$b$g) $x) " in trE) |
140 apply (rule_tac p = "b\<cdot>(iterate m\<cdot>(step\<cdot>b\<cdot>g)\<cdot>x)" in trE) |
141 apply (simp (no_asm_simp)) |
141 apply (simp (no_asm_simp)) |
142 apply (simp (no_asm_simp)) |
142 apply (simp (no_asm_simp)) |
143 apply (rule_tac s = "xa$ (iterate (Suc m) $ (step$b$g) $x) " in trans) |
143 apply (rule_tac s = "xa\<cdot>(iterate (Suc m)\<cdot>(step\<cdot>b\<cdot>g)\<cdot>x)" in trans) |
144 apply (erule_tac [2] spec) |
144 apply (erule_tac [2] spec) |
145 apply (rule cfun_arg_cong) |
145 apply (rule cfun_arg_cong) |
146 apply (rule trans) |
146 apply (rule trans) |
147 apply (rule_tac [2] iterate_Suc [symmetric]) |
147 apply (rule_tac [2] iterate_Suc [symmetric]) |
148 apply (simp add: step_def2) |
148 apply (simp add: step_def2) |
149 apply blast |
149 apply blast |
150 done |
150 done |
151 |
151 |
152 lemma loop_lemma6: "ALL k. b$(iterate k$(step$b$g)$x) ~= FF ==> while$b$g$x=UU" |
152 lemma loop_lemma6: "\<forall>k. b\<cdot>(iterate k\<cdot>(step\<cdot>b\<cdot>g)\<cdot>x) \<noteq> FF \<Longrightarrow> while\<cdot>b\<cdot>g\<cdot>x = UU" |
153 apply (rule_tac t = "x" in iterate_0 [THEN subst]) |
153 apply (rule_tac t = "x" in iterate_0 [THEN subst]) |
154 apply (erule loop_lemma5) |
154 apply (erule loop_lemma5) |
155 done |
155 done |
156 |
156 |
157 lemma loop_lemma7: "while$b$g$x ~= UU ==> EX k. b$(iterate k$(step$b$g)$x) = FF" |
157 lemma loop_lemma7: "while\<cdot>b\<cdot>g\<cdot>x \<noteq> UU \<Longrightarrow> \<exists>k. b\<cdot>(iterate k\<cdot>(step\<cdot>b\<cdot>g)\<cdot>x) = FF" |
158 apply (blast intro: loop_lemma6) |
158 apply (blast intro: loop_lemma6) |
159 done |
159 done |
160 |
160 |
161 |
161 |
162 (* ------------------------------------------------------------------------- *) |
162 (* ------------------------------------------------------------------------- *) |
163 (* an invariant rule for loops *) |
163 (* an invariant rule for loops *) |
164 (* ------------------------------------------------------------------------- *) |
164 (* ------------------------------------------------------------------------- *) |
165 |
165 |
166 lemma loop_inv2: |
166 lemma loop_inv2: |
167 "[| (ALL y. INV y & b$y=TT & g$y ~= UU --> INV (g$y)); |
167 "\<lbrakk>(\<forall>y. INV y \<and> b\<cdot>y = TT \<and> g\<cdot>y \<noteq> UU \<longrightarrow> INV (g\<cdot>y)); |
168 (ALL y. INV y & b$y=FF --> Q y); |
168 (\<forall>y. INV y \<and> b\<cdot>y = FF \<longrightarrow> Q y); |
169 INV x; while$b$g$x~=UU |] ==> Q (while$b$g$x)" |
169 INV x; while\<cdot>b\<cdot>g\<cdot>x \<noteq> UU\<rbrakk> \<Longrightarrow> Q (while\<cdot>b\<cdot>g\<cdot>x)" |
170 apply (rule_tac P = "%k. b$ (iterate k$ (step$b$g) $x) =FF" in exE) |
170 apply (rule_tac P = "\<lambda>k. b\<cdot>(iterate k\<cdot>(step\<cdot>b\<cdot>g)\<cdot>x) = FF" in exE) |
171 apply (erule loop_lemma7) |
171 apply (erule loop_lemma7) |
172 apply (simplesubst loop_lemma4) |
172 apply (simplesubst loop_lemma4) |
173 apply assumption |
173 apply assumption |
174 apply (drule spec, erule mp) |
174 apply (drule spec, erule mp) |
175 apply (rule conjI) |
175 apply (rule conjI) |