src/HOL/Bali/AxExample.thy
author berghofe
Sat Jan 30 17:03:46 2010 +0100 (2010-01-30)
changeset 34990 81e8fdfeb849
parent 33965 f57c11db4ad4
child 35067 af4c18c30593
permissions -rw-r--r--
Adapted to changes in cases method.
wenzelm@12857
     1
(*  Title:      HOL/Bali/AxExample.thy
schirmer@12854
     2
    Author:     David von Oheimb
schirmer@12854
     3
*)
schirmer@12925
     4
schirmer@12854
     5
header {* Example of a proof based on the Bali axiomatic semantics *}
schirmer@12854
     6
haftmann@33965
     7
theory AxExample
haftmann@33965
     8
imports AxSem Example
haftmann@33965
     9
begin
schirmer@12854
    10
schirmer@12854
    11
constdefs
schirmer@12854
    12
  arr_inv :: "st \<Rightarrow> bool"
schirmer@12854
    13
 "arr_inv \<equiv> \<lambda>s. \<exists>obj a T el. globs s (Stat Base) = Some obj \<and>
schirmer@12854
    14
                              values obj (Inl (arr, Base)) = Some (Addr a) \<and>
schirmer@12854
    15
                              heap s a = Some \<lparr>tag=Arr T 2,values=el\<rparr>"
schirmer@12854
    16
schirmer@12854
    17
lemma arr_inv_new_obj: 
schirmer@12854
    18
"\<And>a. \<lbrakk>arr_inv s; new_Addr (heap s)=Some a\<rbrakk> \<Longrightarrow> arr_inv (gupd(Inl a\<mapsto>x) s)"
schirmer@12854
    19
apply (unfold arr_inv_def)
schirmer@12854
    20
apply (force dest!: new_AddrD2)
schirmer@12854
    21
done
schirmer@12854
    22
schirmer@12854
    23
lemma arr_inv_set_locals [simp]: "arr_inv (set_locals l s) = arr_inv s"
schirmer@12854
    24
apply (unfold arr_inv_def)
schirmer@12854
    25
apply (simp (no_asm))
schirmer@12854
    26
done
schirmer@12854
    27
schirmer@12854
    28
lemma arr_inv_gupd_Stat [simp]: 
schirmer@12854
    29
  "Base \<noteq> C \<Longrightarrow> arr_inv (gupd(Stat C\<mapsto>obj) s) = arr_inv s"
schirmer@12854
    30
apply (unfold arr_inv_def)
schirmer@12854
    31
apply (simp (no_asm_simp))
schirmer@12854
    32
done
schirmer@12854
    33
schirmer@12854
    34
lemma ax_inv_lupd [simp]: "arr_inv (lupd(x\<mapsto>y) s) = arr_inv s"
schirmer@12854
    35
apply (unfold arr_inv_def)
schirmer@12854
    36
apply (simp (no_asm))
schirmer@12854
    37
done
schirmer@12854
    38
schirmer@12854
    39
schirmer@12854
    40
declare split_if_asm [split del]
schirmer@12854
    41
declare lvar_def [simp]
schirmer@12854
    42
wenzelm@16121
    43
ML {*
wenzelm@27240
    44
fun inst1_tac ctxt s t st =
wenzelm@29258
    45
  case AList.lookup (op =) (rev (Term.add_var_names (Thm.prop_of st) [])) s of
wenzelm@27240
    46
  SOME i => instantiate_tac ctxt [((s, i), t)] st | NONE => Seq.empty;
wenzelm@20195
    47
wenzelm@20195
    48
val ax_tac =
wenzelm@20195
    49
  REPEAT o rtac allI THEN'
wenzelm@27240
    50
  resolve_tac (@{thm ax_Skip} :: @{thm ax_StatRef} :: @{thm ax_MethdN} :: @{thm ax_Alloc} ::
wenzelm@27240
    51
    @{thm ax_Alloc_Arr} :: @{thm ax_SXAlloc_Normal} :: @{thms ax_derivs.intros(8-)});
schirmer@12854
    52
*}
schirmer@12854
    53
schirmer@12854
    54
schirmer@12854
    55
theorem ax_test: "tprg,({}::'a triple set)\<turnstile> 
schirmer@12854
    56
  {Normal (\<lambda>Y s Z::'a. heap_free four s \<and> \<not>initd Base s \<and> \<not> initd Ext s)} 
schirmer@13688
    57
  .test [Class Base]. 
schirmer@13688
    58
  {\<lambda>Y s Z. abrupt s = Some (Xcpt (Std IndOutBound))}"
schirmer@12854
    59
apply (unfold test_def arr_viewed_from_def)
schirmer@12854
    60
apply (tactic "ax_tac 1" (*;;*))
schirmer@13688
    61
defer (* We begin with the last assertion, to synthesise the intermediate
schirmer@13688
    62
         assertions, like in the fashion of the weakest
schirmer@13688
    63
         precondition. *)
schirmer@12854
    64
apply  (tactic "ax_tac 1" (* Try *))
schirmer@12854
    65
defer
wenzelm@27240
    66
apply    (tactic {* inst1_tac @{context} "Q" 
schirmer@12854
    67
                 "\<lambda>Y s Z. arr_inv (snd s) \<and> tprg,s\<turnstile>catch SXcpt NullPointer" *})
schirmer@12854
    68
prefer 2
schirmer@12854
    69
apply    simp
schirmer@12854
    70
apply   (rule_tac P' = "Normal (\<lambda>Y s Z. arr_inv (snd s))" in conseq1)
schirmer@12854
    71
prefer 2
schirmer@12854
    72
apply    clarsimp
schirmer@12854
    73
apply   (rule_tac Q' = "(\<lambda>Y s Z. ?Q Y s Z)\<leftarrow>=False\<down>=\<diamondsuit>" in conseq2)
schirmer@12854
    74
prefer 2
schirmer@12854
    75
apply    simp
schirmer@12854
    76
apply   (tactic "ax_tac 1" (* While *))
schirmer@12854
    77
prefer 2
schirmer@12854
    78
apply    (rule ax_impossible [THEN conseq1], clarsimp)
schirmer@12854
    79
apply   (rule_tac P' = "Normal ?P" in conseq1)
schirmer@12854
    80
prefer 2
schirmer@12854
    81
apply    clarsimp
schirmer@12854
    82
apply   (tactic "ax_tac 1")
schirmer@12854
    83
apply   (tactic "ax_tac 1" (* AVar *))
schirmer@12854
    84
prefer 2
schirmer@12854
    85
apply    (rule ax_subst_Val_allI)
wenzelm@27240
    86
apply    (tactic {* inst1_tac @{context} "P'" "\<lambda>u a. Normal (?PP a\<leftarrow>?x) u" *})
schirmer@12854
    87
apply    (simp del: avar_def2 peek_and_def2)
schirmer@12854
    88
apply    (tactic "ax_tac 1")
schirmer@12854
    89
apply   (tactic "ax_tac 1")
schirmer@12854
    90
      (* just for clarification: *)
schirmer@12854
    91
apply   (rule_tac Q' = "Normal (\<lambda>Var:(v, f) u ua. fst (snd (avar tprg (Intg 2) v u)) = Some (Xcpt (Std IndOutBound)))" in conseq2)
schirmer@12854
    92
prefer 2
schirmer@12854
    93
apply    (clarsimp simp add: split_beta)
schirmer@12854
    94
apply   (tactic "ax_tac 1" (* FVar *))
schirmer@12854
    95
apply    (tactic "ax_tac 2" (* StatRef *))
schirmer@12854
    96
apply   (rule ax_derivs.Done [THEN conseq1])
schirmer@12854
    97
apply   (clarsimp simp add: arr_inv_def inited_def in_bounds_def)
schirmer@12854
    98
defer
schirmer@12854
    99
apply  (rule ax_SXAlloc_catch_SXcpt)
schirmer@12854
   100
apply  (rule_tac Q' = "(\<lambda>Y (x, s) Z. x = Some (Xcpt (Std NullPointer)) \<and> arr_inv s) \<and>. heap_free two" in conseq2)
schirmer@12854
   101
prefer 2
schirmer@12854
   102
apply   (simp add: arr_inv_new_obj)
schirmer@12854
   103
apply  (tactic "ax_tac 1") 
schirmer@12854
   104
apply  (rule_tac C = "Ext" in ax_Call_known_DynT)
schirmer@12854
   105
apply     (unfold DynT_prop_def)
schirmer@12854
   106
apply     (simp (no_asm))
schirmer@12854
   107
apply    (intro strip)
schirmer@12854
   108
apply    (rule_tac P' = "Normal ?P" in conseq1)
schirmer@12854
   109
apply     (tactic "ax_tac 1" (* Methd *))
schirmer@12854
   110
apply     (rule ax_thin [OF _ empty_subsetI])
schirmer@12854
   111
apply     (simp (no_asm) add: body_def2)
schirmer@12854
   112
apply     (tactic "ax_tac 1" (* Body *))
schirmer@12854
   113
(* apply       (rule_tac [2] ax_derivs.Abrupt) *)
schirmer@12854
   114
defer
schirmer@12854
   115
apply      (simp (no_asm))
schirmer@13688
   116
apply      (tactic "ax_tac 1") (* Comp *)
schirmer@13688
   117
            (* The first statement in the  composition 
schirmer@13688
   118
                 ((Ext)z).vee = 1; Return Null 
schirmer@13688
   119
                will throw an exception (since z is null). So we can handle
schirmer@13688
   120
                Return Null with the Abrupt rule *)
schirmer@13688
   121
apply       (rule_tac [2] ax_derivs.Abrupt)
schirmer@13688
   122
             
schirmer@13688
   123
apply      (rule ax_derivs.Expr) (* Expr *)
schirmer@12854
   124
apply      (tactic "ax_tac 1") (* Ass *)
schirmer@12854
   125
prefer 2
schirmer@12854
   126
apply       (rule ax_subst_Var_allI)
wenzelm@27240
   127
apply       (tactic {* inst1_tac @{context} "P'" "\<lambda>a vs l vf. ?PP a vs l vf\<leftarrow>?x \<and>. ?p" *})
schirmer@12854
   128
apply       (rule allI)
berghofe@26810
   129
apply       (tactic {* simp_tac (@{simpset} delloop "split_all_tac" delsimps [@{thm peek_and_def2}, @{thm heap_def2}, @{thm subst_res_def2}, @{thm normal_def2}]) 1 *})
schirmer@12854
   130
apply       (rule ax_derivs.Abrupt)
schirmer@12854
   131
apply      (simp (no_asm))
schirmer@12854
   132
apply      (tactic "ax_tac 1" (* FVar *))
schirmer@12854
   133
apply       (tactic "ax_tac 2", tactic "ax_tac 2", tactic "ax_tac 2")
schirmer@12854
   134
apply      (tactic "ax_tac 1")
wenzelm@27240
   135
apply     (tactic {* inst1_tac @{context} "R" "\<lambda>a'. Normal ((\<lambda>Vals:vs (x, s) Z. arr_inv s \<and> inited Ext (globs s) \<and> a' \<noteq> Null \<and> vs = [Null]) \<and>. heap_free two)" *})
schirmer@13688
   136
apply     fastsimp
schirmer@13688
   137
prefer 4
schirmer@13688
   138
apply    (rule ax_derivs.Done [THEN conseq1],force)
schirmer@12854
   139
apply   (rule ax_subst_Val_allI)
wenzelm@27240
   140
apply   (tactic {* inst1_tac @{context} "P'" "\<lambda>u a. Normal (?PP a\<leftarrow>?x) u" *})
berghofe@26810
   141
apply   (simp (no_asm) del: peek_and_def2 heap_free_def2 normal_def2 o_apply)
schirmer@12854
   142
apply   (tactic "ax_tac 1")
schirmer@12854
   143
prefer 2
schirmer@12854
   144
apply   (rule ax_subst_Val_allI)
wenzelm@27240
   145
apply    (tactic {* inst1_tac @{context} "P'" "\<lambda>aa v. Normal (?QQ aa v\<leftarrow>?y)" *})
berghofe@26810
   146
apply    (simp del: peek_and_def2 heap_free_def2 normal_def2)
schirmer@12854
   147
apply    (tactic "ax_tac 1")
schirmer@12854
   148
apply   (tactic "ax_tac 1")
schirmer@12854
   149
apply  (tactic "ax_tac 1")
schirmer@12854
   150
apply  (tactic "ax_tac 1")
schirmer@12854
   151
(* end method call *)
schirmer@12854
   152
apply (simp (no_asm))
schirmer@12854
   153
    (* just for clarification: *)
schirmer@12854
   154
apply (rule_tac Q' = "Normal ((\<lambda>Y (x, s) Z. arr_inv s \<and> (\<exists>a. the (locals s (VName e)) = Addr a \<and> obj_class (the (globs s (Inl a))) = Ext \<and> 
schirmer@12854
   155
 invocation_declclass tprg IntVir s (the (locals s (VName e))) (ClassT Base)  
schirmer@12854
   156
     \<lparr>name = foo, parTs = [Class Base]\<rparr> = Ext)) \<and>. initd Ext \<and>. heap_free two)"
schirmer@12854
   157
  in conseq2)
schirmer@12854
   158
prefer 2
schirmer@12854
   159
apply  clarsimp
schirmer@12854
   160
apply (tactic "ax_tac 1")
schirmer@12854
   161
apply (tactic "ax_tac 1")
schirmer@12854
   162
defer
schirmer@12854
   163
apply  (rule ax_subst_Var_allI)
wenzelm@27240
   164
apply  (tactic {* inst1_tac @{context} "P'" "\<lambda>u vf. Normal (?PP vf \<and>. ?p) u" *})
berghofe@26810
   165
apply  (simp (no_asm) del: split_paired_All peek_and_def2 initd_def2 heap_free_def2 normal_def2)
schirmer@12854
   166
apply  (tactic "ax_tac 1" (* NewC *))
schirmer@12854
   167
apply  (tactic "ax_tac 1" (* ax_Alloc *))
schirmer@12854
   168
     (* just for clarification: *)
schirmer@12854
   169
apply  (rule_tac Q' = "Normal ((\<lambda>Y s Z. arr_inv (store s) \<and> vf=lvar (VName e) (store s)) \<and>. heap_free tree \<and>. initd Ext)" in conseq2)
schirmer@12854
   170
prefer 2
schirmer@12854
   171
apply   (simp add: invocation_declclass_def dynmethd_def)
schirmer@12854
   172
apply   (unfold dynlookup_def)
schirmer@12854
   173
apply   (simp add: dynmethd_Ext_foo)
schirmer@12854
   174
apply   (force elim!: arr_inv_new_obj atleast_free_SucD atleast_free_weaken)
schirmer@12854
   175
     (* begin init *)
schirmer@12854
   176
apply  (rule ax_InitS)
schirmer@12854
   177
apply     force
schirmer@12854
   178
apply    (simp (no_asm))
wenzelm@26342
   179
apply   (tactic {* simp_tac (@{simpset} delloop "split_all_tac") 1 *})
schirmer@12854
   180
apply   (rule ax_Init_Skip_lemma)
wenzelm@26342
   181
apply  (tactic {* simp_tac (@{simpset} delloop "split_all_tac") 1 *})
schirmer@12854
   182
apply  (rule ax_InitS [THEN conseq1] (* init Base *))
schirmer@12854
   183
apply      force
schirmer@12854
   184
apply     (simp (no_asm))
schirmer@12854
   185
apply    (unfold arr_viewed_from_def)
schirmer@12854
   186
apply    (rule allI)
schirmer@12854
   187
apply    (rule_tac P' = "Normal ?P" in conseq1)
wenzelm@26342
   188
apply     (tactic {* simp_tac (@{simpset} delloop "split_all_tac") 1 *})
schirmer@12854
   189
apply     (tactic "ax_tac 1")
schirmer@12854
   190
apply     (tactic "ax_tac 1")
schirmer@12854
   191
apply     (rule_tac [2] ax_subst_Var_allI)
wenzelm@27240
   192
apply      (tactic {* inst1_tac @{context} "P'" "\<lambda>vf l vfa. Normal (?P vf l vfa)" *})
berghofe@26810
   193
apply     (tactic {* simp_tac (@{simpset} delloop "split_all_tac" delsimps [split_paired_All, @{thm peek_and_def2}, @{thm heap_free_def2}, @{thm initd_def2}, @{thm normal_def2}, @{thm supd_lupd}]) 2 *})
schirmer@12854
   194
apply      (tactic "ax_tac 2" (* NewA *))
schirmer@12854
   195
apply       (tactic "ax_tac 3" (* ax_Alloc_Arr *))
schirmer@12854
   196
apply       (tactic "ax_tac 3")
wenzelm@27240
   197
apply      (tactic {* inst1_tac @{context} "P" "\<lambda>vf l vfa. Normal (?P vf l vfa\<leftarrow>\<diamondsuit>)" *})
wenzelm@26342
   198
apply      (tactic {* simp_tac (@{simpset} delloop "split_all_tac") 2 *})
schirmer@12854
   199
apply      (tactic "ax_tac 2")
schirmer@12854
   200
apply     (tactic "ax_tac 1" (* FVar *))
schirmer@12854
   201
apply      (tactic "ax_tac 2" (* StatRef *))
schirmer@12854
   202
apply     (rule ax_derivs.Done [THEN conseq1])
wenzelm@27240
   203
apply     (tactic {* inst1_tac @{context} "Q" "\<lambda>vf. Normal ((\<lambda>Y s Z. vf=lvar (VName e) (snd s)) \<and>. heap_free four \<and>. initd Base \<and>. initd Ext)" *})
schirmer@12854
   204
apply     (clarsimp split del: split_if)
schirmer@12854
   205
apply     (frule atleast_free_weaken [THEN atleast_free_weaken])
schirmer@12854
   206
apply     (drule initedD)
schirmer@12854
   207
apply     (clarsimp elim!: atleast_free_SucD simp add: arr_inv_def)
schirmer@12854
   208
apply    force
wenzelm@26342
   209
apply   (tactic {* simp_tac (@{simpset} delloop "split_all_tac") 1 *})
schirmer@12854
   210
apply   (rule ax_triv_Init_Object [THEN peek_and_forget2, THEN conseq1])
schirmer@12854
   211
apply     (rule wf_tprg)
schirmer@12854
   212
apply    clarsimp
wenzelm@27240
   213
apply   (tactic {* inst1_tac @{context} "P" "\<lambda>vf. Normal ((\<lambda>Y s Z. vf = lvar (VName e) (snd s)) \<and>. heap_free four \<and>. initd Ext)" *})
schirmer@12854
   214
apply   clarsimp
wenzelm@27240
   215
apply  (tactic {* inst1_tac @{context} "PP" "\<lambda>vf. Normal ((\<lambda>Y s Z. vf = lvar (VName e) (snd s)) \<and>. heap_free four \<and>. Not \<circ> initd Base)" *})
schirmer@12854
   216
apply  clarsimp
schirmer@12854
   217
     (* end init *)
schirmer@12854
   218
apply (rule conseq1)
schirmer@12854
   219
apply (tactic "ax_tac 1")
schirmer@12854
   220
apply clarsimp
schirmer@12854
   221
done
schirmer@12854
   222
schirmer@12854
   223
(*
schirmer@12854
   224
while (true) {
schirmer@12854
   225
  if (i) {throw xcpt;}
schirmer@12854
   226
  else i=j
schirmer@12854
   227
}
schirmer@12854
   228
*)
schirmer@12854
   229
lemma Loop_Xcpt_benchmark: 
schirmer@12854
   230
 "Q = (\<lambda>Y (x,s) Z. x \<noteq> None \<longrightarrow> the_Bool (the (locals s i))) \<Longrightarrow>  
schirmer@12854
   231
  G,({}::'a triple set)\<turnstile>{Normal (\<lambda>Y s Z::'a. True)}  
schirmer@12854
   232
  .lab1\<bullet> While(Lit (Bool True)) (If(Acc (LVar i)) (Throw (Acc (LVar xcpt))) Else
schirmer@12854
   233
        (Expr (Ass (LVar i) (Acc (LVar j))))). {Q}"
schirmer@12854
   234
apply (rule_tac P' = "Q" and Q' = "Q\<leftarrow>=False\<down>=\<diamondsuit>" in conseq12)
schirmer@12854
   235
apply  safe
schirmer@12854
   236
apply  (tactic "ax_tac 1" (* Loop *))
schirmer@12854
   237
apply   (rule ax_Normal_cases)
schirmer@12854
   238
prefer 2
schirmer@12854
   239
apply    (rule ax_derivs.Abrupt [THEN conseq1], clarsimp simp add: Let_def)
schirmer@12854
   240
apply   (rule conseq1)
schirmer@12854
   241
apply    (tactic "ax_tac 1")
schirmer@12854
   242
apply   clarsimp
schirmer@12854
   243
prefer 2
schirmer@12854
   244
apply  clarsimp
schirmer@12854
   245
apply (tactic "ax_tac 1" (* If *))
schirmer@12854
   246
apply  (tactic 
wenzelm@27240
   247
  {* inst1_tac @{context} "P'" "Normal (\<lambda>s.. (\<lambda>Y s Z. True)\<down>=Val (the (locals s i)))" *})
schirmer@12854
   248
apply  (tactic "ax_tac 1")
schirmer@12854
   249
apply  (rule conseq1)
schirmer@12854
   250
apply   (tactic "ax_tac 1")
schirmer@12854
   251
apply  clarsimp
schirmer@12854
   252
apply (rule allI)
schirmer@12854
   253
apply (rule ax_escape)
schirmer@12854
   254
apply auto
schirmer@12854
   255
apply  (rule conseq1)
schirmer@12854
   256
apply   (tactic "ax_tac 1" (* Throw *))
schirmer@12854
   257
apply   (tactic "ax_tac 1")
schirmer@12854
   258
apply   (tactic "ax_tac 1")
schirmer@12854
   259
apply  clarsimp
schirmer@12854
   260
apply (rule_tac Q' = "Normal (\<lambda>Y s Z. True)" in conseq2)
schirmer@12854
   261
prefer 2
schirmer@12854
   262
apply  clarsimp
schirmer@12854
   263
apply (rule conseq1)
schirmer@12854
   264
apply  (tactic "ax_tac 1")
schirmer@12854
   265
apply  (tactic "ax_tac 1")
schirmer@12854
   266
prefer 2
schirmer@12854
   267
apply   (rule ax_subst_Var_allI)
wenzelm@27240
   268
apply   (tactic {* inst1_tac @{context} "P'" "\<lambda>b Y ba Z vf. \<lambda>Y (x,s) Z. x=None \<and> snd vf = snd (lvar i s)" *})
schirmer@12854
   269
apply   (rule allI)
schirmer@12854
   270
apply   (rule_tac P' = "Normal ?P" in conseq1)
schirmer@12854
   271
prefer 2
schirmer@12854
   272
apply    clarsimp
schirmer@12854
   273
apply   (tactic "ax_tac 1")
schirmer@12854
   274
apply   (rule conseq1)
schirmer@12854
   275
apply    (tactic "ax_tac 1")
schirmer@12854
   276
apply   clarsimp
schirmer@12854
   277
apply  (tactic "ax_tac 1")
schirmer@12854
   278
apply clarsimp
schirmer@12854
   279
done
schirmer@12854
   280
schirmer@12854
   281
end
schirmer@12854
   282