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