101 apply(induct stp, simp add: steps.simps) |
101 apply(induct stp, simp add: steps.simps) |
102 apply(auto simp: step_red) |
102 apply(auto simp: step_red) |
103 apply(rule_tac inv_begin_step, simp_all) |
103 apply(rule_tac inv_begin_step, simp_all) |
104 done |
104 done |
105 |
105 |
106 fun init_state :: "config \<Rightarrow> nat" |
106 fun begin_state :: "config \<Rightarrow> nat" |
107 where |
107 where |
108 "init_state (s, l, r) = (if s = 0 then 0 else 5 - s)" |
108 "begin_state (s, l, r) = (if s = 0 then 0 else 5 - s)" |
109 |
109 |
110 fun init_step :: "config \<Rightarrow> nat" |
110 fun begin_step :: "config \<Rightarrow> nat" |
111 where |
111 where |
112 "init_step (s, l, r) = |
112 "begin_step (s, l, r) = |
113 (if s = 2 then length r else |
113 (if s = 2 then length r else |
114 if s = 3 then (if r = [] \<or> r = [Bk] then 1 else 0) else |
114 if s = 3 then (if r = [] \<or> r = [Bk] then 1 else 0) else |
115 if s = 4 then length l |
115 if s = 4 then length l |
116 else 0)" |
116 else 0)" |
117 |
117 |
118 fun init_measure :: "config \<Rightarrow> nat \<times> nat" |
118 fun begin_measure :: "config \<Rightarrow> nat \<times> nat" |
119 where |
119 where |
120 "init_measure c = (init_state c, init_step c)" |
120 "begin_measure c = (begin_state c, begin_step c)" |
121 |
121 |
122 |
122 |
123 definition lex_pair :: "((nat \<times> nat) \<times> nat \<times> nat) set" |
123 definition lex_pair :: "((nat \<times> nat) \<times> nat \<times> nat) set" |
124 where |
124 where |
125 "lex_pair \<equiv> less_than <*lex*> less_than" |
125 "lex_pair \<equiv> less_than <*lex*> less_than" |
126 |
126 |
127 definition init_LE :: "(config \<times> config) set" |
127 definition begin_LE :: "(config \<times> config) set" |
128 where |
128 where |
129 "init_LE \<equiv> (inv_image lex_pair init_measure)" |
129 "begin_LE \<equiv> (inv_image lex_pair begin_measure)" |
130 |
130 |
131 lemma [simp]: "\<lbrakk>tl r = []; r \<noteq> []; r \<noteq> [Bk]\<rbrakk> \<Longrightarrow> r = [Oc]" |
131 lemma [simp]: "\<lbrakk>tl r = []; r \<noteq> []; r \<noteq> [Bk]\<rbrakk> \<Longrightarrow> r = [Oc]" |
132 by (case_tac r, auto, case_tac a, auto) |
132 by (case_tac r, auto, case_tac a, auto) |
133 |
133 |
134 |
134 |
135 lemma wf_begin_le: "wf init_LE" |
135 lemma wf_begin_le: "wf begin_LE" |
136 by(auto intro:wf_inv_image simp:init_LE_def lex_pair_def) |
136 by(auto intro:wf_inv_image simp:begin_LE_def lex_pair_def) |
137 |
137 |
138 lemma init_halt: |
138 lemma begin_halt: |
139 "x > 0 \<Longrightarrow> \<exists> stp. is_final (steps0 (Suc 0, [], Oc\<up>x) tcopy_begin stp)" |
139 "x > 0 \<Longrightarrow> \<exists> stp. is_final (steps0 (Suc 0, [], Oc\<up>x) tcopy_begin stp)" |
140 proof(rule_tac LE = init_LE in halt_lemma) |
140 proof(rule_tac LE = begin_LE in halt_lemma) |
141 show "wf init_LE" by(simp add: wf_begin_le) |
141 show "wf begin_LE" by(simp add: wf_begin_le) |
142 next |
142 next |
143 assume h: "0 < x" |
143 assume h: "0 < x" |
144 show "\<forall>n. \<not> is_final (steps (Suc 0, [], Oc \<up> x) (tcopy_begin, 0) n) \<longrightarrow> |
144 show "\<forall>n. \<not> is_final (steps (Suc 0, [], Oc \<up> x) (tcopy_begin, 0) n) \<longrightarrow> |
145 (steps (Suc 0, [], Oc \<up> x) (tcopy_begin, 0) (Suc n), |
145 (steps (Suc 0, [], Oc \<up> x) (tcopy_begin, 0) (Suc n), |
146 steps (Suc 0, [], Oc \<up> x) (tcopy_begin, 0) n) \<in> init_LE" |
146 steps (Suc 0, [], Oc \<up> x) (tcopy_begin, 0) n) \<in> begin_LE" |
147 proof(rule_tac allI, rule_tac impI) |
147 proof(rule_tac allI, rule_tac impI) |
148 fix n |
148 fix n |
149 assume a: "\<not> is_final (steps (Suc 0, [], Oc \<up> x) (tcopy_begin, 0) n)" |
149 assume a: "\<not> is_final (steps (Suc 0, [], Oc \<up> x) (tcopy_begin, 0) n)" |
150 have b: "inv_begin x (steps (Suc 0, [], Oc \<up> x) (tcopy_begin, 0) n)" |
150 have b: "inv_begin x (steps (Suc 0, [], Oc \<up> x) (tcopy_begin, 0) n)" |
151 apply(rule_tac inv_begin_steps) |
151 apply(rule_tac inv_begin_steps) |
155 apply(case_tac "steps (Suc 0, [], Oc \<up> x) (tcopy_begin, 0) n", auto) |
155 apply(case_tac "steps (Suc 0, [], Oc \<up> x) (tcopy_begin, 0) n", auto) |
156 done |
156 done |
157 moreover hence "inv_begin x (s, l, r)" |
157 moreover hence "inv_begin x (s, l, r)" |
158 using c b by simp |
158 using c b by simp |
159 ultimately show "(steps (Suc 0, [], Oc \<up> x) (tcopy_begin, 0) (Suc n), |
159 ultimately show "(steps (Suc 0, [], Oc \<up> x) (tcopy_begin, 0) (Suc n), |
160 steps (Suc 0, [], Oc \<up> x) (tcopy_begin, 0) n) \<in> init_LE" |
160 steps (Suc 0, [], Oc \<up> x) (tcopy_begin, 0) n) \<in> begin_LE" |
161 using a |
161 using a |
162 proof(simp del: inv_begin.simps) |
162 proof(simp del: inv_begin.simps) |
163 assume "inv_begin x (s, l, r)" "0 < s" |
163 assume "inv_begin x (s, l, r)" "0 < s" |
164 thus "(step (s, l, r) (tcopy_begin, 0), s, l, r) \<in> init_LE" |
164 thus "(step (s, l, r) (tcopy_begin, 0), s, l, r) \<in> begin_LE" |
165 apply(auto simp: init_LE_def lex_pair_def step.simps tcopy_begin_def numeral split: if_splits) |
165 apply(auto simp: begin_LE_def lex_pair_def step.simps tcopy_begin_def numeral split: if_splits) |
166 apply(case_tac r, auto, case_tac a, auto) |
166 apply(case_tac r, auto, case_tac a, auto) |
167 done |
167 done |
168 qed |
168 qed |
169 qed |
169 qed |
170 qed |
170 qed |
171 |
171 |
172 lemma init_correct: |
172 lemma begin_correct: |
173 "0 < x \<Longrightarrow> {inv_begin1 x} tcopy_begin {inv_begin0 x}" |
173 "0 < x \<Longrightarrow> {inv_begin1 x} tcopy_begin {inv_begin0 x}" |
174 proof(rule_tac Hoare_haltI) |
174 proof(rule_tac Hoare_haltI) |
175 fix l r |
175 fix l r |
176 assume h: "0 < x" "inv_begin1 x (l, r)" |
176 assume h: "0 < x" "inv_begin1 x (l, r)" |
177 hence "\<exists> stp. is_final (steps0 (Suc 0, [], Oc \<up> x) tcopy_begin stp)" |
177 hence "\<exists> stp. is_final (steps0 (Suc 0, [], Oc \<up> x) tcopy_begin stp)" |
178 by (rule_tac init_halt) |
178 by (rule_tac begin_halt) |
179 then obtain stp where "is_final (steps (Suc 0, [], Oc\<up>x) (tcopy_begin, 0) stp)" .. |
179 then obtain stp where "is_final (steps (Suc 0, [], Oc\<up>x) (tcopy_begin, 0) stp)" .. |
180 moreover have "inv_begin x (steps (Suc 0, [], Oc\<up>x) (tcopy_begin, 0) stp)" |
180 moreover have "inv_begin x (steps (Suc 0, [], Oc\<up>x) (tcopy_begin, 0) stp)" |
181 apply(rule_tac inv_begin_steps) |
181 apply(rule_tac inv_begin_steps) |
182 using h by(simp_all add: inv_begin.simps) |
182 using h by(simp_all add: inv_begin.simps) |
183 ultimately show |
183 ultimately show |
968 definition dither :: "instr list" |
968 definition dither :: "instr list" |
969 where |
969 where |
970 "dither \<equiv> [(W0, 1), (R, 2), (L, 1), (L, 0)] " |
970 "dither \<equiv> [(W0, 1), (R, 2), (L, 1), (L, 0)] " |
971 |
971 |
972 (* invariants of dither *) |
972 (* invariants of dither *) |
973 abbreviation |
973 abbreviation (input) |
974 "dither_halt_inv \<equiv> \<lambda>tp. (\<exists>n. tp = (Bk \<up> n, <1::nat>))" |
974 "dither_halt_inv \<equiv> \<lambda>tp. (\<exists>n. tp = (Bk \<up> n, <1::nat>))" |
975 |
975 |
976 abbreviation |
976 abbreviation (input) |
977 "dither_unhalt_inv \<equiv> \<lambda>tp. (\<exists>n. tp = (Bk \<up> n, <0::nat>))" |
977 "dither_unhalt_inv \<equiv> \<lambda>tp. (\<exists>n. tp = (Bk \<up> n, <0::nat>))" |
978 |
978 |
979 lemma dither_loops_aux: |
979 lemma dither_loops_aux: |
980 "(steps0 (1, Bk \<up> m, [Oc]) dither stp = (1, Bk \<up> m, [Oc])) \<or> |
980 "(steps0 (1, Bk \<up> m, [Oc]) dither stp = (1, Bk \<up> m, [Oc])) \<or> |
981 (steps0 (1, Bk \<up> m, [Oc]) dither stp = (2, Oc # Bk \<up> m, []))" |
981 (steps0 (1, Bk \<up> m, [Oc]) dither stp = (2, Oc # Bk \<up> m, []))" |