| author | nipkow | 
| Sat, 20 Apr 2013 20:57:49 +0200 | |
| changeset 51722 | 3da99469cc1b | 
| parent 51698 | c0af8bbc5825 | 
| child 51975 | 7bab3fb52e3e | 
| permissions | -rw-r--r-- | 
| 45812 | 1 | (* Author: Tobias Nipkow *) | 
| 2 | ||
| 3 | theory Live_True | |
| 4 | imports "~~/src/HOL/Library/While_Combinator" Vars Big_Step | |
| 5 | begin | |
| 6 | ||
| 7 | subsection "True Liveness Analysis" | |
| 8 | ||
| 9 | fun L :: "com \<Rightarrow> vname set \<Rightarrow> vname set" where | |
| 10 | "L SKIP X = X" | | |
| 51436 | 11 | "L (x ::= a) X = (if x \<in> X then vars a \<union> (X - {x}) else X)" |
 | 
| 51460 | 12 | "L (c\<^isub>1; c\<^isub>2) X = L c\<^isub>1 (L c\<^isub>2 X)" | | 
| 45812 | 13 | "L (IF b THEN c\<^isub>1 ELSE c\<^isub>2) X = vars b \<union> L c\<^isub>1 X \<union> L c\<^isub>2 X" | | 
| 50009 | 14 | "L (WHILE b DO c) X = lfp(\<lambda>Y. vars b \<union> X \<union> L c Y)" | 
| 45812 | 15 | |
| 16 | lemma L_mono: "mono (L c)" | |
| 17 | proof- | |
| 18 |   { fix X Y have "X \<subseteq> Y \<Longrightarrow> L c X \<subseteq> L c Y"
 | |
| 19 | proof(induction c arbitrary: X Y) | |
| 20 | case (While b c) | |
| 21 | show ?case | |
| 22 | proof(simp, rule lfp_mono) | |
| 23 | fix Z show "vars b \<union> X \<union> L c Z \<subseteq> vars b \<union> Y \<union> L c Z" | |
| 24 | using While by auto | |
| 25 | qed | |
| 26 | next | |
| 27 | case If thus ?case by(auto simp: subset_iff) | |
| 28 | qed auto | |
| 29 | } thus ?thesis by(rule monoI) | |
| 30 | qed | |
| 31 | ||
| 32 | lemma mono_union_L: | |
| 50009 | 33 | "mono (\<lambda>Y. X \<union> L c Y)" | 
| 45812 | 34 | by (metis (no_types) L_mono mono_def order_eq_iff set_eq_subset sup_mono) | 
| 35 | ||
| 36 | lemma L_While_unfold: | |
| 37 | "L (WHILE b DO c) X = vars b \<union> X \<union> L c (L (WHILE b DO c) X)" | |
| 38 | by(metis lfp_unfold[OF mono_union_L] L.simps(5)) | |
| 39 | ||
| 51468 
0e8012983c4e
proofs depend only on constraints, not on def of L WHILE
 nipkow parents: 
51464diff
changeset | 40 | lemma L_While_pfp: "L c (L (WHILE b DO c) X) \<subseteq> L (WHILE b DO c) X" | 
| 
0e8012983c4e
proofs depend only on constraints, not on def of L WHILE
 nipkow parents: 
51464diff
changeset | 41 | using L_While_unfold by blast | 
| 
0e8012983c4e
proofs depend only on constraints, not on def of L WHILE
 nipkow parents: 
51464diff
changeset | 42 | |
| 
0e8012983c4e
proofs depend only on constraints, not on def of L WHILE
 nipkow parents: 
51464diff
changeset | 43 | lemma L_While_vars: "vars b \<subseteq> L (WHILE b DO c) X" | 
| 
0e8012983c4e
proofs depend only on constraints, not on def of L WHILE
 nipkow parents: 
51464diff
changeset | 44 | using L_While_unfold by blast | 
| 
0e8012983c4e
proofs depend only on constraints, not on def of L WHILE
 nipkow parents: 
51464diff
changeset | 45 | |
| 
0e8012983c4e
proofs depend only on constraints, not on def of L WHILE
 nipkow parents: 
51464diff
changeset | 46 | lemma L_While_X: "X \<subseteq> L (WHILE b DO c) X" | 
| 
0e8012983c4e
proofs depend only on constraints, not on def of L WHILE
 nipkow parents: 
51464diff
changeset | 47 | using L_While_unfold by blast | 
| 
0e8012983c4e
proofs depend only on constraints, not on def of L WHILE
 nipkow parents: 
51464diff
changeset | 48 | |
| 
0e8012983c4e
proofs depend only on constraints, not on def of L WHILE
 nipkow parents: 
51464diff
changeset | 49 | text{* Disable L WHILE equation and reason only with L WHILE constraints *}
 | 
| 
0e8012983c4e
proofs depend only on constraints, not on def of L WHILE
 nipkow parents: 
51464diff
changeset | 50 | declare L.simps(5)[simp del] | 
| 
0e8012983c4e
proofs depend only on constraints, not on def of L WHILE
 nipkow parents: 
51464diff
changeset | 51 | |
| 45812 | 52 | |
| 53 | subsection "Soundness" | |
| 54 | ||
| 55 | theorem L_sound: | |
| 56 | "(c,s) \<Rightarrow> s' \<Longrightarrow> s = t on L c X \<Longrightarrow> | |
| 57 | \<exists> t'. (c,t) \<Rightarrow> t' & s' = t' on X" | |
| 58 | proof (induction arbitrary: X t rule: big_step_induct) | |
| 59 | case Skip then show ?case by auto | |
| 60 | next | |
| 61 | case Assign then show ?case | |
| 62 | by (auto simp: ball_Un) | |
| 63 | next | |
| 47818 | 64 | case (Seq c1 s1 s2 c2 s3 X t1) | 
| 65 | from Seq.IH(1) Seq.prems obtain t2 where | |
| 45812 | 66 | t12: "(c1, t1) \<Rightarrow> t2" and s2t2: "s2 = t2 on L c2 X" | 
| 67 | by simp blast | |
| 47818 | 68 | from Seq.IH(2)[OF s2t2] obtain t3 where | 
| 45812 | 69 | t23: "(c2, t2) \<Rightarrow> t3" and s3t3: "s3 = t3 on X" | 
| 70 | by auto | |
| 71 | show ?case using t12 t23 s3t3 by auto | |
| 72 | next | |
| 73 | case (IfTrue b s c1 s' c2) | |
| 50009 | 74 | hence "s = t on vars b" and "s = t on L c1 X" by auto | 
| 45812 | 75 | from bval_eq_if_eq_on_vars[OF this(1)] IfTrue(1) have "bval b t" by simp | 
| 50009 | 76 | from IfTrue.IH[OF `s = t on L c1 X`] obtain t' where | 
| 45812 | 77 | "(c1, t) \<Rightarrow> t'" "s' = t' on X" by auto | 
| 78 | thus ?case using `bval b t` by auto | |
| 79 | next | |
| 80 | case (IfFalse b s c2 s' c1) | |
| 81 | hence "s = t on vars b" "s = t on L c2 X" by auto | |
| 82 | from bval_eq_if_eq_on_vars[OF this(1)] IfFalse(1) have "~bval b t" by simp | |
| 50009 | 83 | from IfFalse.IH[OF `s = t on L c2 X`] obtain t' where | 
| 45812 | 84 | "(c2, t) \<Rightarrow> t'" "s' = t' on X" by auto | 
| 85 | thus ?case using `~bval b t` by auto | |
| 86 | next | |
| 87 | case (WhileFalse b s c) | |
| 88 | hence "~ bval b t" | |
| 51468 
0e8012983c4e
proofs depend only on constraints, not on def of L WHILE
 nipkow parents: 
51464diff
changeset | 89 | by (metis L_While_vars bval_eq_if_eq_on_vars set_mp) | 
| 
0e8012983c4e
proofs depend only on constraints, not on def of L WHILE
 nipkow parents: 
51464diff
changeset | 90 | thus ?case using WhileFalse.prems L_While_X[of X b c] by auto | 
| 45812 | 91 | next | 
| 92 | case (WhileTrue b s1 c s2 s3 X t1) | |
| 93 | let ?w = "WHILE b DO c" | |
| 94 | from `bval b s1` WhileTrue.prems have "bval b t1" | |
| 51468 
0e8012983c4e
proofs depend only on constraints, not on def of L WHILE
 nipkow parents: 
51464diff
changeset | 95 | by (metis L_While_vars bval_eq_if_eq_on_vars set_mp) | 
| 
0e8012983c4e
proofs depend only on constraints, not on def of L WHILE
 nipkow parents: 
51464diff
changeset | 96 | have "s1 = t1 on L c (L ?w X)" using L_While_pfp WhileTrue.prems | 
| 45812 | 97 | by (blast) | 
| 98 | from WhileTrue.IH(1)[OF this] obtain t2 where | |
| 99 | "(c, t1) \<Rightarrow> t2" "s2 = t2 on L ?w X" by auto | |
| 100 | from WhileTrue.IH(2)[OF this(2)] obtain t3 where "(?w,t2) \<Rightarrow> t3" "s3 = t3 on X" | |
| 101 | by auto | |
| 102 | with `bval b t1` `(c, t1) \<Rightarrow> t2` show ?case by auto | |
| 103 | qed | |
| 104 | ||
| 51468 
0e8012983c4e
proofs depend only on constraints, not on def of L WHILE
 nipkow parents: 
51464diff
changeset | 105 | (*declare L.simps(5)[simp]*) | 
| 
0e8012983c4e
proofs depend only on constraints, not on def of L WHILE
 nipkow parents: 
51464diff
changeset | 106 | |
| 50009 | 107 | |
| 50007 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 108 | subsection "Executability" | 
| 45812 | 109 | |
| 110 | lemma L_subset_vars: "L c X \<subseteq> vars c \<union> X" | |
| 111 | proof(induction c arbitrary: X) | |
| 112 | case (While b c) | |
| 50009 | 113 | have "lfp(\<lambda>Y. vars b \<union> X \<union> L c Y) \<subseteq> vars b \<union> vars c \<union> X" | 
| 45812 | 114 | using While.IH[of "vars b \<union> vars c \<union> X"] | 
| 115 | by (auto intro!: lfp_lowerbound) | |
| 51468 
0e8012983c4e
proofs depend only on constraints, not on def of L WHILE
 nipkow parents: 
51464diff
changeset | 116 | thus ?case by (simp add: L.simps(5)) | 
| 45812 | 117 | qed auto | 
| 118 | ||
| 50007 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 119 | text{* Make @{const L} executable by replacing @{const lfp} with the @{const
 | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 120 | while} combinator from theory @{theory While_Combinator}. The @{const while}
 | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 121 | combinator obeys the recursion equation | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 122 | @{thm[display] While_Combinator.while_unfold[no_vars]}
 | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 123 | and is thus executable. *} | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 124 | |
| 45812 | 125 | lemma L_While: fixes b c X | 
| 51464 | 126 | assumes "finite X" defines "f == \<lambda>Y. vars b \<union> X \<union> L c Y" | 
| 127 | shows "L (WHILE b DO c) X = while (\<lambda>Y. f Y \<noteq> Y) f {}" (is "_ = ?r")
 | |
| 45812 | 128 | proof - | 
| 129 | let ?V = "vars b \<union> vars c \<union> X" | |
| 130 | have "lfp f = ?r" | |
| 50007 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 131 | proof(rule lfp_while[where C = "?V"]) | 
| 45812 | 132 | show "mono f" by(simp add: f_def mono_union_L) | 
| 133 | next | |
| 134 | fix Y show "Y \<subseteq> ?V \<Longrightarrow> f Y \<subseteq> ?V" | |
| 135 | unfolding f_def using L_subset_vars[of c] by blast | |
| 136 | next | |
| 137 | show "finite ?V" using `finite X` by simp | |
| 138 | qed | |
| 51468 
0e8012983c4e
proofs depend only on constraints, not on def of L WHILE
 nipkow parents: 
51464diff
changeset | 139 | thus ?thesis by (simp add: f_def L.simps(5)) | 
| 45812 | 140 | qed | 
| 141 | ||
| 51464 | 142 | lemma L_While_let: "finite X \<Longrightarrow> L (WHILE b DO c) X = | 
| 143 | (let f = (\<lambda>Y. vars b \<union> X \<union> L c Y) | |
| 144 |    in while (\<lambda>Y. f Y \<noteq> Y) f {})"
 | |
| 51468 
0e8012983c4e
proofs depend only on constraints, not on def of L WHILE
 nipkow parents: 
51464diff
changeset | 145 | by(simp add: L_While) | 
| 51464 | 146 | |
| 50007 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 147 | lemma L_While_set: "L (WHILE b DO c) (set xs) = | 
| 51464 | 148 | (let f = (\<lambda>Y. vars b \<union> set xs \<union> L c Y) | 
| 149 |    in while (\<lambda>Y. f Y \<noteq> Y) f {})"
 | |
| 150 | by(rule L_While_let, simp) | |
| 50007 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 151 | |
| 50180 | 152 | text{* Replace the equation for @{text "L (WHILE \<dots>)"} by the executable @{thm[source] L_While_set}: *}
 | 
| 50007 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 153 | lemmas [code] = L.simps(1-4) L_While_set | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 154 | text{* Sorry, this syntax is odd. *}
 | 
| 45812 | 155 | |
| 50180 | 156 | text{* A test: *}
 | 
| 50007 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 157 | lemma "(let b = Less (N 0) (V ''y''); c = ''y'' ::= V ''x''; ''x'' ::= V ''z'' | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 158 |   in L (WHILE b DO c) {''y''}) = {''x'', ''y'', ''z''}"
 | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 159 | by eval | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 160 | |
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 161 | |
| 50009 | 162 | subsection "Limiting the number of iterations" | 
| 50007 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 163 | |
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 164 | text{* The final parameter is the default value: *}
 | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 165 | |
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 166 | fun iter :: "('a \<Rightarrow> 'a) \<Rightarrow> nat \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'a" where
 | 
| 45812 | 167 | "iter f 0 p d = d" | | 
| 168 | "iter f (Suc n) p d = (if f p = p then p else iter f n (f p) d)" | |
| 169 | ||
| 50007 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 170 | text{* A version of @{const L} with a bounded number of iterations (here: 2)
 | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 171 | in the WHILE case: *} | 
| 45812 | 172 | |
| 50007 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 173 | fun Lb :: "com \<Rightarrow> vname set \<Rightarrow> vname set" where | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 174 | "Lb SKIP X = X" | | 
| 50009 | 175 | "Lb (x ::= a) X = (if x \<in> X then X - {x} \<union> vars a else X)" |
 | 
| 50007 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 176 | "Lb (c\<^isub>1; c\<^isub>2) X = (Lb c\<^isub>1 \<circ> Lb c\<^isub>2) X" | | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 177 | "Lb (IF b THEN c\<^isub>1 ELSE c\<^isub>2) X = vars b \<union> Lb c\<^isub>1 X \<union> Lb c\<^isub>2 X" | | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 178 | "Lb (WHILE b DO c) X = iter (\<lambda>A. vars b \<union> X \<union> Lb c A) 2 {} (vars b \<union> vars c \<union> X)"
 | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 179 | |
| 50009 | 180 | text{* @{const Lb} (and @{const iter}) is not monotone! *}
 | 
| 181 | lemma "let w = WHILE Bc False DO (''x'' ::= V ''y''; ''z'' ::= V ''x'')
 | |
| 182 |   in \<not> (Lb w {''z''} \<subseteq> Lb w {''y'',''z''})"
 | |
| 183 | by eval | |
| 184 | ||
| 50007 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 185 | lemma lfp_subset_iter: | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 186 | "\<lbrakk> mono f; !!X. f X \<subseteq> f' X; lfp f \<subseteq> D \<rbrakk> \<Longrightarrow> lfp f \<subseteq> iter f' n A D" | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 187 | proof(induction n arbitrary: A) | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 188 | case 0 thus ?case by simp | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 189 | next | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 190 | case Suc thus ?case by simp (metis lfp_lowerbound) | 
| 45812 | 191 | qed | 
| 192 | ||
| 50007 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 193 | lemma "L c X \<subseteq> Lb c X" | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 194 | proof(induction c arbitrary: X) | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 195 | case (While b c) | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 196 | let ?f = "\<lambda>A. vars b \<union> X \<union> L c A" | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 197 | let ?fb = "\<lambda>A. vars b \<union> X \<union> Lb c A" | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 198 | show ?case | 
| 51468 
0e8012983c4e
proofs depend only on constraints, not on def of L WHILE
 nipkow parents: 
51464diff
changeset | 199 | proof (simp add: L.simps(5), rule lfp_subset_iter[OF mono_union_L]) | 
| 50007 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 200 | show "!!X. ?f X \<subseteq> ?fb X" using While.IH by blast | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 201 | show "lfp ?f \<subseteq> vars b \<union> vars c \<union> X" | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 202 | by (metis (full_types) L.simps(5) L_subset_vars vars_com.simps(5)) | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 203 | qed | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 204 | next | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 205 | case Seq thus ?case by simp (metis (full_types) L_mono monoD subset_trans) | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 206 | qed auto | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 207 | |
| 45812 | 208 | end |