| author | hoelzl | 
| Tue, 05 Jul 2016 20:29:58 +0200 | |
| changeset 63393 | c22928719e19 | 
| parent 53015 | a1119cf551e8 | 
| child 66453 | cc19f7ca2ed6 | 
| 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)" |
 | 
| 53015 
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
 wenzelm parents: 
52072diff
changeset | 12 | "L (c\<^sub>1;; c\<^sub>2) X = L c\<^sub>1 (L c\<^sub>2 X)" | | 
| 
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
 wenzelm parents: 
52072diff
changeset | 13 | "L (IF b THEN c\<^sub>1 ELSE c\<^sub>2) X = vars b \<union> L c\<^sub>1 X \<union> L c\<^sub>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 | |
| 52072 | 49 | text{* Disable @{text "L WHILE"} equation and reason only with @{text "L WHILE"} constraints: *}
 | 
| 51468 
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 | |
| 51975 | 53 | subsection "Correctness" | 
| 45812 | 54 | |
| 51975 | 55 | theorem L_correct: | 
| 45812 | 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 | ||
| 50009 | 105 | |
| 50007 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 106 | subsection "Executability" | 
| 45812 | 107 | |
| 52072 | 108 | lemma L_subset_vars: "L c X \<subseteq> rvars c \<union> X" | 
| 45812 | 109 | proof(induction c arbitrary: X) | 
| 110 | case (While b c) | |
| 52072 | 111 | have "lfp(\<lambda>Y. vars b \<union> X \<union> L c Y) \<subseteq> vars b \<union> rvars c \<union> X" | 
| 112 | using While.IH[of "vars b \<union> rvars c \<union> X"] | |
| 45812 | 113 | by (auto intro!: lfp_lowerbound) | 
| 51468 
0e8012983c4e
proofs depend only on constraints, not on def of L WHILE
 nipkow parents: 
51464diff
changeset | 114 | thus ?case by (simp add: L.simps(5)) | 
| 45812 | 115 | qed auto | 
| 116 | ||
| 50007 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 117 | 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 | 118 | while} combinator from theory @{theory While_Combinator}. The @{const while}
 | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 119 | combinator obeys the recursion equation | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 120 | @{thm[display] While_Combinator.while_unfold[no_vars]}
 | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 121 | and is thus executable. *} | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 122 | |
| 45812 | 123 | lemma L_While: fixes b c X | 
| 51464 | 124 | assumes "finite X" defines "f == \<lambda>Y. vars b \<union> X \<union> L c Y" | 
| 125 | shows "L (WHILE b DO c) X = while (\<lambda>Y. f Y \<noteq> Y) f {}" (is "_ = ?r")
 | |
| 45812 | 126 | proof - | 
| 52072 | 127 | let ?V = "vars b \<union> rvars c \<union> X" | 
| 45812 | 128 | have "lfp f = ?r" | 
| 50007 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 129 | proof(rule lfp_while[where C = "?V"]) | 
| 45812 | 130 | show "mono f" by(simp add: f_def mono_union_L) | 
| 131 | next | |
| 132 | fix Y show "Y \<subseteq> ?V \<Longrightarrow> f Y \<subseteq> ?V" | |
| 133 | unfolding f_def using L_subset_vars[of c] by blast | |
| 134 | next | |
| 135 | show "finite ?V" using `finite X` by simp | |
| 136 | qed | |
| 51468 
0e8012983c4e
proofs depend only on constraints, not on def of L WHILE
 nipkow parents: 
51464diff
changeset | 137 | thus ?thesis by (simp add: f_def L.simps(5)) | 
| 45812 | 138 | qed | 
| 139 | ||
| 51464 | 140 | lemma L_While_let: "finite X \<Longrightarrow> L (WHILE b DO c) X = | 
| 141 | (let f = (\<lambda>Y. vars b \<union> X \<union> L c Y) | |
| 142 |    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 | 143 | by(simp add: L_While) | 
| 51464 | 144 | |
| 50007 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 145 | lemma L_While_set: "L (WHILE b DO c) (set xs) = | 
| 51464 | 146 | (let f = (\<lambda>Y. vars b \<union> set xs \<union> L c Y) | 
| 147 |    in while (\<lambda>Y. f Y \<noteq> Y) f {})"
 | |
| 148 | by(rule L_While_let, simp) | |
| 50007 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 149 | |
| 50180 | 150 | 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 | 151 | lemmas [code] = L.simps(1-4) L_While_set | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 152 | text{* Sorry, this syntax is odd. *}
 | 
| 45812 | 153 | |
| 50180 | 154 | text{* A test: *}
 | 
| 52046 
bc01725d7918
replaced `;' by `;;' to disambiguate syntax; unexpected slight increase in build time
 nipkow parents: 
51975diff
changeset | 155 | lemma "(let b = Less (N 0) (V ''y''); c = ''y'' ::= V ''x'';; ''x'' ::= V ''z'' | 
| 50007 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 156 |   in L (WHILE b DO c) {''y''}) = {''x'', ''y'', ''z''}"
 | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 157 | by eval | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 158 | |
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 159 | |
| 50009 | 160 | subsection "Limiting the number of iterations" | 
| 50007 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 161 | |
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 162 | text{* The final parameter is the default value: *}
 | 
| 
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 | fun iter :: "('a \<Rightarrow> 'a) \<Rightarrow> nat \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'a" where
 | 
| 45812 | 165 | "iter f 0 p d = d" | | 
| 166 | "iter f (Suc n) p d = (if f p = p then p else iter f n (f p) d)" | |
| 167 | ||
| 50007 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 168 | 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 | 169 | in the WHILE case: *} | 
| 45812 | 170 | |
| 50007 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 171 | fun Lb :: "com \<Rightarrow> vname set \<Rightarrow> vname set" where | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 172 | "Lb SKIP X = X" | | 
| 50009 | 173 | "Lb (x ::= a) X = (if x \<in> X then X - {x} \<union> vars a else X)" |
 | 
| 53015 
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
 wenzelm parents: 
52072diff
changeset | 174 | "Lb (c\<^sub>1;; c\<^sub>2) X = (Lb c\<^sub>1 \<circ> Lb c\<^sub>2) X" | | 
| 
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
 wenzelm parents: 
52072diff
changeset | 175 | "Lb (IF b THEN c\<^sub>1 ELSE c\<^sub>2) X = vars b \<union> Lb c\<^sub>1 X \<union> Lb c\<^sub>2 X" | | 
| 52072 | 176 | "Lb (WHILE b DO c) X = iter (\<lambda>A. vars b \<union> X \<union> Lb c A) 2 {} (vars b \<union> rvars c \<union> X)"
 | 
| 50007 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 177 | |
| 50009 | 178 | text{* @{const Lb} (and @{const iter}) is not monotone! *}
 | 
| 52046 
bc01725d7918
replaced `;' by `;;' to disambiguate syntax; unexpected slight increase in build time
 nipkow parents: 
51975diff
changeset | 179 | lemma "let w = WHILE Bc False DO (''x'' ::= V ''y'';; ''z'' ::= V ''x'')
 | 
| 50009 | 180 |   in \<not> (Lb w {''z''} \<subseteq> Lb w {''y'',''z''})"
 | 
| 181 | by eval | |
| 182 | ||
| 50007 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 183 | lemma lfp_subset_iter: | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 184 | "\<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 | 185 | proof(induction n arbitrary: A) | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 186 | case 0 thus ?case by simp | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 187 | next | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 188 | case Suc thus ?case by simp (metis lfp_lowerbound) | 
| 45812 | 189 | qed | 
| 190 | ||
| 50007 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 191 | lemma "L c X \<subseteq> Lb c X" | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 192 | proof(induction c arbitrary: X) | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 193 | case (While b c) | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 194 | 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 | 195 | 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 | 196 | show ?case | 
| 51468 
0e8012983c4e
proofs depend only on constraints, not on def of L WHILE
 nipkow parents: 
51464diff
changeset | 197 | 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 | 198 | show "!!X. ?f X \<subseteq> ?fb X" using While.IH by blast | 
| 52072 | 199 | show "lfp ?f \<subseteq> vars b \<union> rvars c \<union> X" | 
| 200 | by (metis (full_types) L.simps(5) L_subset_vars rvars.simps(5)) | |
| 50007 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 201 | qed | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 202 | next | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 203 | 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 | 204 | qed auto | 
| 
56f269baae76
executable true liveness analysis incl an approximating version
 nipkow parents: 
48256diff
changeset | 205 | |
| 45812 | 206 | end |