src/HOL/MicroJava/DFA/Err.thy
changeset 33954 1bc3b688548c
child 35102 cc7a0b9f938c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/MicroJava/DFA/Err.thy	Tue Nov 24 14:37:23 2009 +0100
     1.3 @@ -0,0 +1,350 @@
     1.4 +(*  Title:      HOL/MicroJava/BV/Err.thy
     1.5 +    Author:     Tobias Nipkow
     1.6 +    Copyright   2000 TUM
     1.7 +*)
     1.8 +
     1.9 +header {* \isaheader{The Error Type} *}
    1.10 +
    1.11 +theory Err
    1.12 +imports Semilat
    1.13 +begin
    1.14 +
    1.15 +datatype 'a err = Err | OK 'a
    1.16 +
    1.17 +types 'a ebinop = "'a \<Rightarrow> 'a \<Rightarrow> 'a err"
    1.18 +      'a esl =    "'a set * 'a ord * 'a ebinop"
    1.19 +
    1.20 +consts
    1.21 +  ok_val :: "'a err \<Rightarrow> 'a"
    1.22 +primrec
    1.23 +  "ok_val (OK x) = x"
    1.24 +
    1.25 +constdefs
    1.26 + lift :: "('a \<Rightarrow> 'b err) \<Rightarrow> ('a err \<Rightarrow> 'b err)"
    1.27 +"lift f e == case e of Err \<Rightarrow> Err | OK x \<Rightarrow> f x"
    1.28 +
    1.29 + lift2 :: "('a \<Rightarrow> 'b \<Rightarrow> 'c err) \<Rightarrow> 'a err \<Rightarrow> 'b err \<Rightarrow> 'c err"
    1.30 +"lift2 f e1 e2 ==
    1.31 + case e1 of Err  \<Rightarrow> Err
    1.32 +          | OK x \<Rightarrow> (case e2 of Err \<Rightarrow> Err | OK y \<Rightarrow> f x y)"
    1.33 +
    1.34 + le :: "'a ord \<Rightarrow> 'a err ord"
    1.35 +"le r e1 e2 ==
    1.36 +        case e2 of Err \<Rightarrow> True |
    1.37 +                   OK y \<Rightarrow> (case e1 of Err \<Rightarrow> False | OK x \<Rightarrow> x <=_r y)"
    1.38 +
    1.39 + sup :: "('a \<Rightarrow> 'b \<Rightarrow> 'c) \<Rightarrow> ('a err \<Rightarrow> 'b err \<Rightarrow> 'c err)"
    1.40 +"sup f == lift2(%x y. OK(x +_f y))"
    1.41 +
    1.42 + err :: "'a set \<Rightarrow> 'a err set"
    1.43 +"err A == insert Err {x . ? y:A. x = OK y}"
    1.44 +
    1.45 + esl :: "'a sl \<Rightarrow> 'a esl"
    1.46 +"esl == %(A,r,f). (A,r, %x y. OK(f x y))"
    1.47 +
    1.48 + sl :: "'a esl \<Rightarrow> 'a err sl"
    1.49 +"sl == %(A,r,f). (err A, le r, lift2 f)"
    1.50 +
    1.51 +syntax
    1.52 + err_semilat :: "'a esl \<Rightarrow> bool"
    1.53 +translations
    1.54 +"err_semilat L" == "semilat(Err.sl L)"
    1.55 +
    1.56 +
    1.57 +consts
    1.58 +  strict  :: "('a \<Rightarrow> 'b err) \<Rightarrow> ('a err \<Rightarrow> 'b err)"
    1.59 +primrec
    1.60 +  "strict f Err    = Err"
    1.61 +  "strict f (OK x) = f x"
    1.62 +
    1.63 +lemma strict_Some [simp]: 
    1.64 +  "(strict f x = OK y) = (\<exists> z. x = OK z \<and> f z = OK y)"
    1.65 +  by (cases x, auto)
    1.66 +
    1.67 +lemma not_Err_eq:
    1.68 +  "(x \<noteq> Err) = (\<exists>a. x = OK a)" 
    1.69 +  by (cases x) auto
    1.70 +
    1.71 +lemma not_OK_eq:
    1.72 +  "(\<forall>y. x \<noteq> OK y) = (x = Err)"
    1.73 +  by (cases x) auto  
    1.74 +
    1.75 +lemma unfold_lesub_err:
    1.76 +  "e1 <=_(le r) e2 == le r e1 e2"
    1.77 +  by (simp add: lesub_def)
    1.78 +
    1.79 +lemma le_err_refl:
    1.80 +  "!x. x <=_r x \<Longrightarrow> e <=_(Err.le r) e"
    1.81 +apply (unfold lesub_def Err.le_def)
    1.82 +apply (simp split: err.split)
    1.83 +done 
    1.84 +
    1.85 +lemma le_err_trans [rule_format]:
    1.86 +  "order r \<Longrightarrow> e1 <=_(le r) e2 \<longrightarrow> e2 <=_(le r) e3 \<longrightarrow> e1 <=_(le r) e3"
    1.87 +apply (unfold unfold_lesub_err le_def)
    1.88 +apply (simp split: err.split)
    1.89 +apply (blast intro: order_trans)
    1.90 +done
    1.91 +
    1.92 +lemma le_err_antisym [rule_format]:
    1.93 +  "order r \<Longrightarrow> e1 <=_(le r) e2 \<longrightarrow> e2 <=_(le r) e1 \<longrightarrow> e1=e2"
    1.94 +apply (unfold unfold_lesub_err le_def)
    1.95 +apply (simp split: err.split)
    1.96 +apply (blast intro: order_antisym)
    1.97 +done 
    1.98 +
    1.99 +lemma OK_le_err_OK:
   1.100 +  "(OK x <=_(le r) OK y) = (x <=_r y)"
   1.101 +  by (simp add: unfold_lesub_err le_def)
   1.102 +
   1.103 +lemma order_le_err [iff]:
   1.104 +  "order(le r) = order r"
   1.105 +apply (rule iffI)
   1.106 + apply (subst Semilat.order_def)
   1.107 + apply (blast dest: order_antisym OK_le_err_OK [THEN iffD2]
   1.108 +              intro: order_trans OK_le_err_OK [THEN iffD1])
   1.109 +apply (subst Semilat.order_def)
   1.110 +apply (blast intro: le_err_refl le_err_trans le_err_antisym
   1.111 +             dest: order_refl)
   1.112 +done 
   1.113 +
   1.114 +lemma le_Err [iff]:  "e <=_(le r) Err"
   1.115 +  by (simp add: unfold_lesub_err le_def)
   1.116 +
   1.117 +lemma Err_le_conv [iff]:
   1.118 + "Err <=_(le r) e  = (e = Err)"
   1.119 +  by (simp add: unfold_lesub_err le_def  split: err.split)
   1.120 +
   1.121 +lemma le_OK_conv [iff]:
   1.122 +  "e <=_(le r) OK x  =  (? y. e = OK y & y <=_r x)"
   1.123 +  by (simp add: unfold_lesub_err le_def split: err.split)
   1.124 +
   1.125 +lemma OK_le_conv:
   1.126 + "OK x <=_(le r) e  =  (e = Err | (? y. e = OK y & x <=_r y))"
   1.127 +  by (simp add: unfold_lesub_err le_def split: err.split)
   1.128 +
   1.129 +lemma top_Err [iff]: "top (le r) Err";
   1.130 +  by (simp add: top_def)
   1.131 +
   1.132 +lemma OK_less_conv [rule_format, iff]:
   1.133 +  "OK x <_(le r) e = (e=Err | (? y. e = OK y & x <_r y))"
   1.134 +  by (simp add: lesssub_def lesub_def le_def split: err.split)
   1.135 +
   1.136 +lemma not_Err_less [rule_format, iff]:
   1.137 +  "~(Err <_(le r) x)"
   1.138 +  by (simp add: lesssub_def lesub_def le_def split: err.split)
   1.139 +
   1.140 +lemma semilat_errI [intro]:
   1.141 +  assumes semilat: "semilat (A, r, f)"
   1.142 +  shows "semilat(err A, Err.le r, lift2(%x y. OK(f x y)))"
   1.143 +  apply(insert semilat)
   1.144 +  apply (unfold semilat_Def closed_def plussub_def lesub_def 
   1.145 +    lift2_def Err.le_def err_def)
   1.146 +  apply (simp split: err.split)
   1.147 +  done
   1.148 +
   1.149 +lemma err_semilat_eslI_aux:
   1.150 +  assumes semilat: "semilat (A, r, f)"
   1.151 +  shows "err_semilat(esl(A,r,f))"
   1.152 +  apply (unfold sl_def esl_def)
   1.153 +  apply (simp add: semilat_errI[OF semilat])
   1.154 +  done
   1.155 +
   1.156 +lemma err_semilat_eslI [intro, simp]:
   1.157 + "\<And>L. semilat L \<Longrightarrow> err_semilat(esl L)"
   1.158 +by(simp add: err_semilat_eslI_aux split_tupled_all)
   1.159 +
   1.160 +lemma acc_err [simp, intro!]:  "acc r \<Longrightarrow> acc(le r)"
   1.161 +apply (unfold acc_def lesub_def le_def lesssub_def)
   1.162 +apply (simp add: wf_eq_minimal split: err.split)
   1.163 +apply clarify
   1.164 +apply (case_tac "Err : Q")
   1.165 + apply blast
   1.166 +apply (erule_tac x = "{a . OK a : Q}" in allE)
   1.167 +apply (case_tac "x")
   1.168 + apply fast
   1.169 +apply blast
   1.170 +done 
   1.171 +
   1.172 +lemma Err_in_err [iff]: "Err : err A"
   1.173 +  by (simp add: err_def)
   1.174 +
   1.175 +lemma Ok_in_err [iff]: "(OK x : err A) = (x:A)"
   1.176 +  by (auto simp add: err_def)
   1.177 +
   1.178 +section {* lift *}
   1.179 +
   1.180 +lemma lift_in_errI:
   1.181 +  "\<lbrakk> e : err S; !x:S. e = OK x \<longrightarrow> f x : err S \<rbrakk> \<Longrightarrow> lift f e : err S"
   1.182 +apply (unfold lift_def)
   1.183 +apply (simp split: err.split)
   1.184 +apply blast
   1.185 +done 
   1.186 +
   1.187 +lemma Err_lift2 [simp]: 
   1.188 +  "Err +_(lift2 f) x = Err"
   1.189 +  by (simp add: lift2_def plussub_def)
   1.190 +
   1.191 +lemma lift2_Err [simp]: 
   1.192 +  "x +_(lift2 f) Err = Err"
   1.193 +  by (simp add: lift2_def plussub_def split: err.split)
   1.194 +
   1.195 +lemma OK_lift2_OK [simp]:
   1.196 +  "OK x +_(lift2 f) OK y = x +_f y"
   1.197 +  by (simp add: lift2_def plussub_def split: err.split)
   1.198 +
   1.199 +
   1.200 +section {* sup *}
   1.201 +
   1.202 +lemma Err_sup_Err [simp]:
   1.203 +  "Err +_(Err.sup f) x = Err"
   1.204 +  by (simp add: plussub_def Err.sup_def Err.lift2_def)
   1.205 +
   1.206 +lemma Err_sup_Err2 [simp]:
   1.207 +  "x +_(Err.sup f) Err = Err"
   1.208 +  by (simp add: plussub_def Err.sup_def Err.lift2_def split: err.split)
   1.209 +
   1.210 +lemma Err_sup_OK [simp]:
   1.211 +  "OK x +_(Err.sup f) OK y = OK(x +_f y)"
   1.212 +  by (simp add: plussub_def Err.sup_def Err.lift2_def)
   1.213 +
   1.214 +lemma Err_sup_eq_OK_conv [iff]:
   1.215 +  "(Err.sup f ex ey = OK z) = (? x y. ex = OK x & ey = OK y & f x y = z)"
   1.216 +apply (unfold Err.sup_def lift2_def plussub_def)
   1.217 +apply (rule iffI)
   1.218 + apply (simp split: err.split_asm)
   1.219 +apply clarify
   1.220 +apply simp
   1.221 +done
   1.222 +
   1.223 +lemma Err_sup_eq_Err [iff]:
   1.224 +  "(Err.sup f ex ey = Err) = (ex=Err | ey=Err)"
   1.225 +apply (unfold Err.sup_def lift2_def plussub_def)
   1.226 +apply (simp split: err.split)
   1.227 +done 
   1.228 +
   1.229 +section {* semilat (err A) (le r) f *}
   1.230 +
   1.231 +lemma semilat_le_err_Err_plus [simp]:
   1.232 +  "\<lbrakk> x: err A; semilat(err A, le r, f) \<rbrakk> \<Longrightarrow> Err +_f x = Err"
   1.233 +  by (blast intro: Semilat.le_iff_plus_unchanged [OF Semilat.intro, THEN iffD1]
   1.234 +                   Semilat.le_iff_plus_unchanged2 [OF Semilat.intro, THEN iffD1])
   1.235 +
   1.236 +lemma semilat_le_err_plus_Err [simp]:
   1.237 +  "\<lbrakk> x: err A; semilat(err A, le r, f) \<rbrakk> \<Longrightarrow> x +_f Err = Err"
   1.238 +  by (blast intro: Semilat.le_iff_plus_unchanged [OF Semilat.intro, THEN iffD1]
   1.239 +                   Semilat.le_iff_plus_unchanged2 [OF Semilat.intro, THEN iffD1])
   1.240 +
   1.241 +lemma semilat_le_err_OK1:
   1.242 +  "\<lbrakk> x:A; y:A; semilat(err A, le r, f); OK x +_f OK y = OK z \<rbrakk> 
   1.243 +  \<Longrightarrow> x <=_r z";
   1.244 +apply (rule OK_le_err_OK [THEN iffD1])
   1.245 +apply (erule subst)
   1.246 +apply (simp add: Semilat.ub1 [OF Semilat.intro])
   1.247 +done
   1.248 +
   1.249 +lemma semilat_le_err_OK2:
   1.250 +  "\<lbrakk> x:A; y:A; semilat(err A, le r, f); OK x +_f OK y = OK z \<rbrakk> 
   1.251 +  \<Longrightarrow> y <=_r z"
   1.252 +apply (rule OK_le_err_OK [THEN iffD1])
   1.253 +apply (erule subst)
   1.254 +apply (simp add: Semilat.ub2 [OF Semilat.intro])
   1.255 +done
   1.256 +
   1.257 +lemma eq_order_le:
   1.258 +  "\<lbrakk> x=y; order r \<rbrakk> \<Longrightarrow> x <=_r y"
   1.259 +apply (unfold Semilat.order_def)
   1.260 +apply blast
   1.261 +done
   1.262 +
   1.263 +lemma OK_plus_OK_eq_Err_conv [simp]:
   1.264 +  assumes "x:A" and "y:A" and "semilat(err A, le r, fe)"
   1.265 +  shows "((OK x) +_fe (OK y) = Err) = (~(? z:A. x <=_r z & y <=_r z))"
   1.266 +proof -
   1.267 +  have plus_le_conv3: "\<And>A x y z f r. 
   1.268 +    \<lbrakk> semilat (A,r,f); x +_f y <=_r z; x:A; y:A; z:A \<rbrakk> 
   1.269 +    \<Longrightarrow> x <=_r z \<and> y <=_r z"
   1.270 +    by (rule Semilat.plus_le_conv [OF Semilat.intro, THEN iffD1])
   1.271 +  from prems show ?thesis
   1.272 +  apply (rule_tac iffI)
   1.273 +   apply clarify
   1.274 +   apply (drule OK_le_err_OK [THEN iffD2])
   1.275 +   apply (drule OK_le_err_OK [THEN iffD2])
   1.276 +   apply (drule Semilat.lub [OF Semilat.intro, of _ _ _ "OK x" _ "OK y"])
   1.277 +        apply assumption
   1.278 +       apply assumption
   1.279 +      apply simp
   1.280 +     apply simp
   1.281 +    apply simp
   1.282 +   apply simp
   1.283 +  apply (case_tac "(OK x) +_fe (OK y)")
   1.284 +   apply assumption
   1.285 +  apply (rename_tac z)
   1.286 +  apply (subgoal_tac "OK z: err A")
   1.287 +  apply (drule eq_order_le)
   1.288 +    apply (erule Semilat.orderI [OF Semilat.intro])
   1.289 +   apply (blast dest: plus_le_conv3) 
   1.290 +  apply (erule subst)
   1.291 +  apply (blast intro: Semilat.closedI [OF Semilat.intro] closedD)
   1.292 +  done 
   1.293 +qed
   1.294 +
   1.295 +section {* semilat (err(Union AS)) *}
   1.296 +
   1.297 +(* FIXME? *)
   1.298 +lemma all_bex_swap_lemma [iff]:
   1.299 +  "(!x. (? y:A. x = f y) \<longrightarrow> P x) = (!y:A. P(f y))"
   1.300 +  by blast
   1.301 +
   1.302 +lemma closed_err_Union_lift2I: 
   1.303 +  "\<lbrakk> !A:AS. closed (err A) (lift2 f); AS ~= {}; 
   1.304 +      !A:AS.!B:AS. A~=B \<longrightarrow> (!a:A.!b:B. a +_f b = Err) \<rbrakk> 
   1.305 +  \<Longrightarrow> closed (err(Union AS)) (lift2 f)"
   1.306 +apply (unfold closed_def err_def)
   1.307 +apply simp
   1.308 +apply clarify
   1.309 +apply simp
   1.310 +apply fast
   1.311 +done 
   1.312 +
   1.313 +text {* 
   1.314 +  If @{term "AS = {}"} the thm collapses to
   1.315 +  @{prop "order r & closed {Err} f & Err +_f Err = Err"}
   1.316 +  which may not hold 
   1.317 +*}
   1.318 +lemma err_semilat_UnionI:
   1.319 +  "\<lbrakk> !A:AS. err_semilat(A, r, f); AS ~= {}; 
   1.320 +      !A:AS.!B:AS. A~=B \<longrightarrow> (!a:A.!b:B. ~ a <=_r b & a +_f b = Err) \<rbrakk> 
   1.321 +  \<Longrightarrow> err_semilat(Union AS, r, f)"
   1.322 +apply (unfold semilat_def sl_def)
   1.323 +apply (simp add: closed_err_Union_lift2I)
   1.324 +apply (rule conjI)
   1.325 + apply blast
   1.326 +apply (simp add: err_def)
   1.327 +apply (rule conjI)
   1.328 + apply clarify
   1.329 + apply (rename_tac A a u B b)
   1.330 + apply (case_tac "A = B")
   1.331 +  apply simp
   1.332 + apply simp
   1.333 +apply (rule conjI)
   1.334 + apply clarify
   1.335 + apply (rename_tac A a u B b)
   1.336 + apply (case_tac "A = B")
   1.337 +  apply simp
   1.338 + apply simp
   1.339 +apply clarify
   1.340 +apply (rename_tac A ya yb B yd z C c a b)
   1.341 +apply (case_tac "A = B")
   1.342 + apply (case_tac "A = C")
   1.343 +  apply simp
   1.344 + apply (rotate_tac -1)
   1.345 + apply simp
   1.346 +apply (rotate_tac -1)
   1.347 +apply (case_tac "B = C")
   1.348 + apply simp
   1.349 +apply (rotate_tac -1)
   1.350 +apply simp
   1.351 +done 
   1.352 +
   1.353 +end