src/HOL/IMP/Live.thy
changeset 52046 bc01725d7918
parent 51975 7bab3fb52e3e
child 53015 a1119cf551e8
equal deleted inserted replaced
52045:90cd3c53a887 52046:bc01725d7918
     8 subsection "Liveness Analysis"
     8 subsection "Liveness Analysis"
     9 
     9 
    10 fun L :: "com \<Rightarrow> vname set \<Rightarrow> vname set" where
    10 fun L :: "com \<Rightarrow> vname set \<Rightarrow> vname set" where
    11 "L SKIP X = X" |
    11 "L SKIP X = X" |
    12 "L (x ::= a) X = vars a \<union> (X - {x})" |
    12 "L (x ::= a) X = vars a \<union> (X - {x})" |
    13 "L (c\<^isub>1; c\<^isub>2) X = L c\<^isub>1 (L c\<^isub>2 X)" |
    13 "L (c\<^isub>1;; c\<^isub>2) X = L c\<^isub>1 (L c\<^isub>2 X)" |
    14 "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" |
    14 "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" |
    15 "L (WHILE b DO c) X = vars b \<union> X \<union> L c X"
    15 "L (WHILE b DO c) X = vars b \<union> X \<union> L c X"
    16 
    16 
    17 value "show (L (''y'' ::= V ''z''; ''x'' ::= Plus (V ''y'') (V ''z'')) {''x''})"
    17 value "show (L (''y'' ::= V ''z'';; ''x'' ::= Plus (V ''y'') (V ''z'')) {''x''})"
    18 
    18 
    19 value "show (L (WHILE Less (V ''x'') (V ''x'') DO ''y'' ::= V ''z'') {''x''})"
    19 value "show (L (WHILE Less (V ''x'') (V ''x'') DO ''y'' ::= V ''z'') {''x''})"
    20 
    20 
    21 fun "kill" :: "com \<Rightarrow> vname set" where
    21 fun "kill" :: "com \<Rightarrow> vname set" where
    22 "kill SKIP = {}" |
    22 "kill SKIP = {}" |
    23 "kill (x ::= a) = {x}" |
    23 "kill (x ::= a) = {x}" |
    24 "kill (c\<^isub>1; c\<^isub>2) = kill c\<^isub>1 \<union> kill c\<^isub>2" |
    24 "kill (c\<^isub>1;; c\<^isub>2) = kill c\<^isub>1 \<union> kill c\<^isub>2" |
    25 "kill (IF b THEN c\<^isub>1 ELSE c\<^isub>2) = kill c\<^isub>1 \<inter> kill c\<^isub>2" |
    25 "kill (IF b THEN c\<^isub>1 ELSE c\<^isub>2) = kill c\<^isub>1 \<inter> kill c\<^isub>2" |
    26 "kill (WHILE b DO c) = {}"
    26 "kill (WHILE b DO c) = {}"
    27 
    27 
    28 fun gen :: "com \<Rightarrow> vname set" where
    28 fun gen :: "com \<Rightarrow> vname set" where
    29 "gen SKIP = {}" |
    29 "gen SKIP = {}" |
    30 "gen (x ::= a) = vars a" |
    30 "gen (x ::= a) = vars a" |
    31 "gen (c\<^isub>1; c\<^isub>2) = gen c\<^isub>1 \<union> (gen c\<^isub>2 - kill c\<^isub>1)" |
    31 "gen (c\<^isub>1;; c\<^isub>2) = gen c\<^isub>1 \<union> (gen c\<^isub>2 - kill c\<^isub>1)" |
    32 "gen (IF b THEN c\<^isub>1 ELSE c\<^isub>2) = vars b \<union> gen c\<^isub>1 \<union> gen c\<^isub>2" |
    32 "gen (IF b THEN c\<^isub>1 ELSE c\<^isub>2) = vars b \<union> gen c\<^isub>1 \<union> gen c\<^isub>2" |
    33 "gen (WHILE b DO c) = vars b \<union> gen c"
    33 "gen (WHILE b DO c) = vars b \<union> gen c"
    34 
    34 
    35 lemma L_gen_kill: "L c X = gen c \<union> (X - kill c)"
    35 lemma L_gen_kill: "L c X = gen c \<union> (X - kill c)"
    36 by(induct c arbitrary:X) auto
    36 by(induct c arbitrary:X) auto
   108 
   108 
   109 text{* Burying assignments to dead variables: *}
   109 text{* Burying assignments to dead variables: *}
   110 fun bury :: "com \<Rightarrow> vname set \<Rightarrow> com" where
   110 fun bury :: "com \<Rightarrow> vname set \<Rightarrow> com" where
   111 "bury SKIP X = SKIP" |
   111 "bury SKIP X = SKIP" |
   112 "bury (x ::= a) X = (if x \<in> X then x ::= a else SKIP)" |
   112 "bury (x ::= a) X = (if x \<in> X then x ::= a else SKIP)" |
   113 "bury (c\<^isub>1; c\<^isub>2) X = (bury c\<^isub>1 (L c\<^isub>2 X); bury c\<^isub>2 X)" |
   113 "bury (c\<^isub>1;; c\<^isub>2) X = (bury c\<^isub>1 (L c\<^isub>2 X);; bury c\<^isub>2 X)" |
   114 "bury (IF b THEN c\<^isub>1 ELSE c\<^isub>2) X = IF b THEN bury c\<^isub>1 X ELSE bury c\<^isub>2 X" |
   114 "bury (IF b THEN c\<^isub>1 ELSE c\<^isub>2) X = IF b THEN bury c\<^isub>1 X ELSE bury c\<^isub>2 X" |
   115 "bury (WHILE b DO c) X = WHILE b DO bury c (L (WHILE b DO c) X)"
   115 "bury (WHILE b DO c) X = WHILE b DO bury c (L (WHILE b DO c) X)"
   116 
   116 
   117 text{* We could prove the analogous lemma to @{thm[source]L_correct}, and the
   117 text{* We could prove the analogous lemma to @{thm[source]L_correct}, and the
   118 proof would be very similar. However, we phrase it as a semantics
   118 proof would be very similar. However, we phrase it as a semantics
   180 by (cases c) auto
   180 by (cases c) auto
   181 
   181 
   182 lemma Assign_bury[simp]: "x::=a = bury c X \<longleftrightarrow> c = x::=a & x : X"
   182 lemma Assign_bury[simp]: "x::=a = bury c X \<longleftrightarrow> c = x::=a & x : X"
   183 by (cases c) auto
   183 by (cases c) auto
   184 
   184 
   185 lemma Seq_bury[simp]: "bc\<^isub>1;bc\<^isub>2 = bury c X \<longleftrightarrow>
   185 lemma Seq_bury[simp]: "bc\<^isub>1;;bc\<^isub>2 = bury c X \<longleftrightarrow>
   186   (EX c\<^isub>1 c\<^isub>2. c = c\<^isub>1;c\<^isub>2 & bc\<^isub>2 = bury c\<^isub>2 X & bc\<^isub>1 = bury c\<^isub>1 (L c\<^isub>2 X))"
   186   (EX c\<^isub>1 c\<^isub>2. c = c\<^isub>1;;c\<^isub>2 & bc\<^isub>2 = bury c\<^isub>2 X & bc\<^isub>1 = bury c\<^isub>1 (L c\<^isub>2 X))"
   187 by (cases c) auto
   187 by (cases c) auto
   188 
   188 
   189 lemma If_bury[simp]: "IF b THEN bc1 ELSE bc2 = bury c X \<longleftrightarrow>
   189 lemma If_bury[simp]: "IF b THEN bc1 ELSE bc2 = bury c X \<longleftrightarrow>
   190   (EX c1 c2. c = IF b THEN c1 ELSE c2 &
   190   (EX c1 c2. c = IF b THEN c1 ELSE c2 &
   191      bc1 = bury c1 X & bc2 = bury c2 X)"
   191      bc1 = bury c1 X & bc2 = bury c2 X)"
   203 next
   203 next
   204   case Assign then show ?case
   204   case Assign then show ?case
   205     by (auto simp: ball_Un)
   205     by (auto simp: ball_Un)
   206 next
   206 next
   207   case (Seq bc1 s1 s2 bc2 s3 c X t1)
   207   case (Seq bc1 s1 s2 bc2 s3 c X t1)
   208   then obtain c1 c2 where c: "c = c1;c2"
   208   then obtain c1 c2 where c: "c = c1;;c2"
   209     and bc2: "bc2 = bury c2 X" and bc1: "bc1 = bury c1 (L c2 X)" by auto
   209     and bc2: "bc2 = bury c2 X" and bc1: "bc1 = bury c1 (L c2 X)" by auto
   210   note IH = Seq.hyps(2,4)
   210   note IH = Seq.hyps(2,4)
   211   from IH(1)[OF bc1, of t1] Seq.prems c obtain t2 where
   211   from IH(1)[OF bc1, of t1] Seq.prems c obtain t2 where
   212     t12: "(c1, t1) \<Rightarrow> t2" and s2t2: "s2 = t2 on L c2 X" by auto
   212     t12: "(c1, t1) \<Rightarrow> t2" and s2t2: "s2 = t2 on L c2 X" by auto
   213   from IH(2)[OF bc2 s2t2] obtain t3 where
   213   from IH(2)[OF bc2 s2t2] obtain t3 where