src/HOL/MicroJava/BV/Err.thy
author webertj
Mon Mar 07 19:30:53 2005 +0100 (2005-03-07)
changeset 15584 3478bb4f93ff
parent 13074 96bf406fd3e5
child 16417 9bc16273c2d4
permissions -rw-r--r--
refute_params: default value itself=1 added (for type classes)
     1 (*  Title:      HOL/MicroJava/BV/Err.thy
     2     ID:         $Id$
     3     Author:     Tobias Nipkow
     4     Copyright   2000 TUM
     5 
     6 The error type
     7 *)
     8 
     9 header {* \isaheader{The Error Type} *}
    10 
    11 theory Err = Semilat:
    12 
    13 datatype 'a err = Err | OK 'a
    14 
    15 types 'a ebinop = "'a \<Rightarrow> 'a \<Rightarrow> 'a err"
    16       'a esl =    "'a set * 'a ord * 'a ebinop"
    17 
    18 consts
    19   ok_val :: "'a err \<Rightarrow> 'a"
    20 primrec
    21   "ok_val (OK x) = x"
    22 
    23 constdefs
    24  lift :: "('a \<Rightarrow> 'b err) \<Rightarrow> ('a err \<Rightarrow> 'b err)"
    25 "lift f e == case e of Err \<Rightarrow> Err | OK x \<Rightarrow> f x"
    26 
    27  lift2 :: "('a \<Rightarrow> 'b \<Rightarrow> 'c err) \<Rightarrow> 'a err \<Rightarrow> 'b err \<Rightarrow> 'c err"
    28 "lift2 f e1 e2 ==
    29  case e1 of Err  \<Rightarrow> Err
    30           | OK x \<Rightarrow> (case e2 of Err \<Rightarrow> Err | OK y \<Rightarrow> f x y)"
    31 
    32  le :: "'a ord \<Rightarrow> 'a err ord"
    33 "le r e1 e2 ==
    34         case e2 of Err \<Rightarrow> True |
    35                    OK y \<Rightarrow> (case e1 of Err \<Rightarrow> False | OK x \<Rightarrow> x <=_r y)"
    36 
    37  sup :: "('a \<Rightarrow> 'b \<Rightarrow> 'c) \<Rightarrow> ('a err \<Rightarrow> 'b err \<Rightarrow> 'c err)"
    38 "sup f == lift2(%x y. OK(x +_f y))"
    39 
    40  err :: "'a set \<Rightarrow> 'a err set"
    41 "err A == insert Err {x . ? y:A. x = OK y}"
    42 
    43  esl :: "'a sl \<Rightarrow> 'a esl"
    44 "esl == %(A,r,f). (A,r, %x y. OK(f x y))"
    45 
    46  sl :: "'a esl \<Rightarrow> 'a err sl"
    47 "sl == %(A,r,f). (err A, le r, lift2 f)"
    48 
    49 syntax
    50  err_semilat :: "'a esl \<Rightarrow> bool"
    51 translations
    52 "err_semilat L" == "semilat(Err.sl L)"
    53 
    54 
    55 consts
    56   strict  :: "('a \<Rightarrow> 'b err) \<Rightarrow> ('a err \<Rightarrow> 'b err)"
    57 primrec
    58   "strict f Err    = Err"
    59   "strict f (OK x) = f x"
    60 
    61 lemma strict_Some [simp]: 
    62   "(strict f x = OK y) = (\<exists> z. x = OK z \<and> f z = OK y)"
    63   by (cases x, auto)
    64 
    65 lemma not_Err_eq:
    66   "(x \<noteq> Err) = (\<exists>a. x = OK a)" 
    67   by (cases x) auto
    68 
    69 lemma not_OK_eq:
    70   "(\<forall>y. x \<noteq> OK y) = (x = Err)"
    71   by (cases x) auto  
    72 
    73 lemma unfold_lesub_err:
    74   "e1 <=_(le r) e2 == le r e1 e2"
    75   by (simp add: lesub_def)
    76 
    77 lemma le_err_refl:
    78   "!x. x <=_r x \<Longrightarrow> e <=_(Err.le r) e"
    79 apply (unfold lesub_def Err.le_def)
    80 apply (simp split: err.split)
    81 done 
    82 
    83 lemma le_err_trans [rule_format]:
    84   "order r \<Longrightarrow> e1 <=_(le r) e2 \<longrightarrow> e2 <=_(le r) e3 \<longrightarrow> e1 <=_(le r) e3"
    85 apply (unfold unfold_lesub_err le_def)
    86 apply (simp split: err.split)
    87 apply (blast intro: order_trans)
    88 done
    89 
    90 lemma le_err_antisym [rule_format]:
    91   "order r \<Longrightarrow> e1 <=_(le r) e2 \<longrightarrow> e2 <=_(le r) e1 \<longrightarrow> e1=e2"
    92 apply (unfold unfold_lesub_err le_def)
    93 apply (simp split: err.split)
    94 apply (blast intro: order_antisym)
    95 done 
    96 
    97 lemma OK_le_err_OK:
    98   "(OK x <=_(le r) OK y) = (x <=_r y)"
    99   by (simp add: unfold_lesub_err le_def)
   100 
   101 lemma order_le_err [iff]:
   102   "order(le r) = order r"
   103 apply (rule iffI)
   104  apply (subst order_def)
   105  apply (blast dest: order_antisym OK_le_err_OK [THEN iffD2]
   106               intro: order_trans OK_le_err_OK [THEN iffD1])
   107 apply (subst order_def)
   108 apply (blast intro: le_err_refl le_err_trans le_err_antisym
   109              dest: order_refl)
   110 done 
   111 
   112 lemma le_Err [iff]:  "e <=_(le r) Err"
   113   by (simp add: unfold_lesub_err le_def)
   114 
   115 lemma Err_le_conv [iff]:
   116  "Err <=_(le r) e  = (e = Err)"
   117   by (simp add: unfold_lesub_err le_def  split: err.split)
   118 
   119 lemma le_OK_conv [iff]:
   120   "e <=_(le r) OK x  =  (? y. e = OK y & y <=_r x)"
   121   by (simp add: unfold_lesub_err le_def split: err.split)
   122 
   123 lemma OK_le_conv:
   124  "OK x <=_(le r) e  =  (e = Err | (? y. e = OK y & x <=_r y))"
   125   by (simp add: unfold_lesub_err le_def split: err.split)
   126 
   127 lemma top_Err [iff]: "top (le r) Err";
   128   by (simp add: top_def)
   129 
   130 lemma OK_less_conv [rule_format, iff]:
   131   "OK x <_(le r) e = (e=Err | (? y. e = OK y & x <_r y))"
   132   by (simp add: lesssub_def lesub_def le_def split: err.split)
   133 
   134 lemma not_Err_less [rule_format, iff]:
   135   "~(Err <_(le r) x)"
   136   by (simp add: lesssub_def lesub_def le_def split: err.split)
   137 
   138 lemma semilat_errI [intro]: includes semilat
   139 shows "semilat(err A, Err.le r, lift2(%x y. OK(f x y)))"
   140 apply(insert semilat)
   141 apply (unfold semilat_Def closed_def plussub_def lesub_def 
   142               lift2_def Err.le_def err_def)
   143 apply (simp split: err.split)
   144 done
   145 
   146 lemma err_semilat_eslI_aux:
   147 includes semilat shows "err_semilat(esl(A,r,f))"
   148 apply (unfold sl_def esl_def)
   149 apply (simp add: semilat_errI[OF semilat])
   150 done
   151 
   152 lemma err_semilat_eslI [intro, simp]:
   153  "\<And>L. semilat L \<Longrightarrow> err_semilat(esl L)"
   154 by(simp add: err_semilat_eslI_aux split_tupled_all)
   155 
   156 lemma acc_err [simp, intro!]:  "acc r \<Longrightarrow> acc(le r)"
   157 apply (unfold acc_def lesub_def le_def lesssub_def)
   158 apply (simp add: wf_eq_minimal split: err.split)
   159 apply clarify
   160 apply (case_tac "Err : Q")
   161  apply blast
   162 apply (erule_tac x = "{a . OK a : Q}" in allE)
   163 apply (case_tac "x")
   164  apply fast
   165 apply blast
   166 done 
   167 
   168 lemma Err_in_err [iff]: "Err : err A"
   169   by (simp add: err_def)
   170 
   171 lemma Ok_in_err [iff]: "(OK x : err A) = (x:A)"
   172   by (auto simp add: err_def)
   173 
   174 section {* lift *}
   175 
   176 lemma lift_in_errI:
   177   "\<lbrakk> e : err S; !x:S. e = OK x \<longrightarrow> f x : err S \<rbrakk> \<Longrightarrow> lift f e : err S"
   178 apply (unfold lift_def)
   179 apply (simp split: err.split)
   180 apply blast
   181 done 
   182 
   183 lemma Err_lift2 [simp]: 
   184   "Err +_(lift2 f) x = Err"
   185   by (simp add: lift2_def plussub_def)
   186 
   187 lemma lift2_Err [simp]: 
   188   "x +_(lift2 f) Err = Err"
   189   by (simp add: lift2_def plussub_def split: err.split)
   190 
   191 lemma OK_lift2_OK [simp]:
   192   "OK x +_(lift2 f) OK y = x +_f y"
   193   by (simp add: lift2_def plussub_def split: err.split)
   194 
   195 
   196 section {* sup *}
   197 
   198 lemma Err_sup_Err [simp]:
   199   "Err +_(Err.sup f) x = Err"
   200   by (simp add: plussub_def Err.sup_def Err.lift2_def)
   201 
   202 lemma Err_sup_Err2 [simp]:
   203   "x +_(Err.sup f) Err = Err"
   204   by (simp add: plussub_def Err.sup_def Err.lift2_def split: err.split)
   205 
   206 lemma Err_sup_OK [simp]:
   207   "OK x +_(Err.sup f) OK y = OK(x +_f y)"
   208   by (simp add: plussub_def Err.sup_def Err.lift2_def)
   209 
   210 lemma Err_sup_eq_OK_conv [iff]:
   211   "(Err.sup f ex ey = OK z) = (? x y. ex = OK x & ey = OK y & f x y = z)"
   212 apply (unfold Err.sup_def lift2_def plussub_def)
   213 apply (rule iffI)
   214  apply (simp split: err.split_asm)
   215 apply clarify
   216 apply simp
   217 done
   218 
   219 lemma Err_sup_eq_Err [iff]:
   220   "(Err.sup f ex ey = Err) = (ex=Err | ey=Err)"
   221 apply (unfold Err.sup_def lift2_def plussub_def)
   222 apply (simp split: err.split)
   223 done 
   224 
   225 section {* semilat (err A) (le r) f *}
   226 
   227 lemma semilat_le_err_Err_plus [simp]:
   228   "\<lbrakk> x: err A; semilat(err A, le r, f) \<rbrakk> \<Longrightarrow> Err +_f x = Err"
   229   by (blast intro: semilat.le_iff_plus_unchanged [THEN iffD1]
   230                    semilat.le_iff_plus_unchanged2 [THEN iffD1])
   231 
   232 lemma semilat_le_err_plus_Err [simp]:
   233   "\<lbrakk> x: err A; semilat(err A, le r, f) \<rbrakk> \<Longrightarrow> x +_f Err = Err"
   234   by (blast intro: semilat.le_iff_plus_unchanged [THEN iffD1]
   235                    semilat.le_iff_plus_unchanged2 [THEN iffD1])
   236 
   237 lemma semilat_le_err_OK1:
   238   "\<lbrakk> x:A; y:A; semilat(err A, le r, f); OK x +_f OK y = OK z \<rbrakk> 
   239   \<Longrightarrow> x <=_r z";
   240 apply (rule OK_le_err_OK [THEN iffD1])
   241 apply (erule subst)
   242 apply (simp add:semilat.ub1)
   243 done
   244 
   245 lemma semilat_le_err_OK2:
   246   "\<lbrakk> x:A; y:A; semilat(err A, le r, f); OK x +_f OK y = OK z \<rbrakk> 
   247   \<Longrightarrow> y <=_r z"
   248 apply (rule OK_le_err_OK [THEN iffD1])
   249 apply (erule subst)
   250 apply (simp add:semilat.ub2)
   251 done
   252 
   253 lemma eq_order_le:
   254   "\<lbrakk> x=y; order r \<rbrakk> \<Longrightarrow> x <=_r y"
   255 apply (unfold order_def)
   256 apply blast
   257 done
   258 
   259 lemma OK_plus_OK_eq_Err_conv [simp]:
   260   "\<lbrakk> x:A; y:A; semilat(err A, le r, fe) \<rbrakk> \<Longrightarrow> 
   261   ((OK x) +_fe (OK y) = Err) = (~(? z:A. x <=_r z & y <=_r z))"
   262 proof -
   263   have plus_le_conv3: "\<And>A x y z f r. 
   264     \<lbrakk> semilat (A,r,f); x +_f y <=_r z; x:A; y:A; z:A \<rbrakk> 
   265     \<Longrightarrow> x <=_r z \<and> y <=_r z"
   266     by (rule semilat.plus_le_conv [THEN iffD1])
   267   case rule_context
   268   thus ?thesis
   269   apply (rule_tac iffI)
   270    apply clarify
   271    apply (drule OK_le_err_OK [THEN iffD2])
   272    apply (drule OK_le_err_OK [THEN iffD2])
   273    apply (drule semilat.lub[of _ _ _ "OK x" _ "OK y"])
   274         apply assumption
   275        apply assumption
   276       apply simp
   277      apply simp
   278     apply simp
   279    apply simp
   280   apply (case_tac "(OK x) +_fe (OK y)")
   281    apply assumption
   282   apply (rename_tac z)
   283   apply (subgoal_tac "OK z: err A")
   284   apply (drule eq_order_le)
   285     apply (erule semilat.orderI)
   286    apply (blast dest: plus_le_conv3) 
   287   apply (erule subst)
   288   apply (blast intro: semilat.closedI closedD)
   289   done 
   290 qed
   291 
   292 section {* semilat (err(Union AS)) *}
   293 
   294 (* FIXME? *)
   295 lemma all_bex_swap_lemma [iff]:
   296   "(!x. (? y:A. x = f y) \<longrightarrow> P x) = (!y:A. P(f y))"
   297   by blast
   298 
   299 lemma closed_err_Union_lift2I: 
   300   "\<lbrakk> !A:AS. closed (err A) (lift2 f); AS ~= {}; 
   301       !A:AS.!B:AS. A~=B \<longrightarrow> (!a:A.!b:B. a +_f b = Err) \<rbrakk> 
   302   \<Longrightarrow> closed (err(Union AS)) (lift2 f)"
   303 apply (unfold closed_def err_def)
   304 apply simp
   305 apply clarify
   306 apply simp
   307 apply fast
   308 done 
   309 
   310 text {* 
   311   If @{term "AS = {}"} the thm collapses to
   312   @{prop "order r & closed {Err} f & Err +_f Err = Err"}
   313   which may not hold 
   314 *}
   315 lemma err_semilat_UnionI:
   316   "\<lbrakk> !A:AS. err_semilat(A, r, f); AS ~= {}; 
   317       !A:AS.!B:AS. A~=B \<longrightarrow> (!a:A.!b:B. ~ a <=_r b & a +_f b = Err) \<rbrakk> 
   318   \<Longrightarrow> err_semilat(Union AS, r, f)"
   319 apply (unfold semilat_def sl_def)
   320 apply (simp add: closed_err_Union_lift2I)
   321 apply (rule conjI)
   322  apply blast
   323 apply (simp add: err_def)
   324 apply (rule conjI)
   325  apply clarify
   326  apply (rename_tac A a u B b)
   327  apply (case_tac "A = B")
   328   apply simp
   329  apply simp
   330 apply (rule conjI)
   331  apply clarify
   332  apply (rename_tac A a u B b)
   333  apply (case_tac "A = B")
   334   apply simp
   335  apply simp
   336 apply clarify
   337 apply (rename_tac A ya yb B yd z C c a b)
   338 apply (case_tac "A = B")
   339  apply (case_tac "A = C")
   340   apply simp
   341  apply (rotate_tac -1)
   342  apply simp
   343 apply (rotate_tac -1)
   344 apply (case_tac "B = C")
   345  apply simp
   346 apply (rotate_tac -1)
   347 apply simp
   348 done 
   349 
   350 end