1(* interactive use: 2 3quietdec := true; 4loadPath := (concat Globals.HOLDIR "/examples/dev/sw") :: !loadPath; 5 6app load ["numLib", "relationTheory", "arithmeticTheory", "preARMTheory", "pairTheory", 7 "pred_setSimps", "pred_setTheory", "listTheory", "rich_listTheory", "whileTheory", "ARMCompositionTheory", "ILTheory", "wordsTheory"]; 8 9quietdec := false; 10*) 11 12 13open HolKernel Parse boolLib bossLib numLib relationTheory arithmeticTheory preARMTheory pairTheory 14 pred_setSimps pred_setTheory listTheory rich_listTheory whileTheory ARMCompositionTheory ILTheory wordsTheory; 15 16 17val _ = new_theory "rules"; 18 19(*---------------------------------------------------------------------------------*) 20(* Simplifier on finite maps *) 21(*---------------------------------------------------------------------------------*) 22 23val set_ss = std_ss ++ SET_SPEC_ss ++ PRED_SET_ss; 24 25(*---------------------------------------------------------------------------------*) 26(* Inference based on Hoare Logic *) 27(*---------------------------------------------------------------------------------*) 28 29(*---------------------------------------------------------------------------------*) 30(* read from an data state *) 31(*---------------------------------------------------------------------------------*) 32val _ = Hol_datatype ` 33 REXP = RR of MREG 34 | RM of MMEM 35 | RC of DATA 36 | PR of REXP # REXP 37 `; 38 39 40val mread_def = Define ` 41 (mread st (RR r) = read st (toREG r)) /\ 42 (mread st (RM m) = read st (toMEM m)) /\ 43 (mread st (RC c) = c)`; 44 45val _ = add_rule {term_name = "mread", fixity = Suffix 60, 46 pp_elements = [TOK "<", TM, TOK ">"], 47 paren_style = OnlyIfNecessary, 48 block_style = (AroundSameName, (PP.INCONSISTENT, 0))} handle HOL_ERR e => print (#message e); 49 50 51(*---------------------------------------------------------------------------------*) 52(* The fp and sp point to the default positions *) 53(*---------------------------------------------------------------------------------*) 54 55val proper_def = Define ` 56 proper = (\(regs,mem):DSTATE. (regs ' 11w = 100w) /\ (regs ' 13w = 100w))`; 57 58 59(*---------------------------------------------------------------------------------*) 60(* Hoare Logic Style Specification *) 61(*---------------------------------------------------------------------------------*) 62 63val HSPEC_def = Define ` 64 HSPEC P ir Q = !st. P st ==> Q (run_ir ir st)`; 65 66val _ = type_abbrev("HSPEC_TYPE", type_of (Term `HSPEC`)); 67 68(* 69val _ = add_rule {term_name = "HSPEC", 70 fixity = Infix (HOLgrammars.RIGHT, 3), 71 pp_elements = [HardSpace 1, TOK "(", TM, TOK ")", HardSpace 1], 72 paren_style = OnlyIfNecessary, 73 block_style = (AroundEachPhrase, 74 (PP.INCONSISTENT, 0))}; 75*) 76 77(*---------------------------------------------------------------------------------*) 78(* Sequential Composition *) 79(*---------------------------------------------------------------------------------*) 80 81val SC_RULE = Q.store_thm ( 82 "SC_RULE", 83 `!P Q R ir1 ir2. WELL_FORMED ir1 /\ WELL_FORMED ir2 /\ 84 HSPEC P ir1 Q /\ HSPEC Q ir2 R ==> 85 HSPEC P (SC ir1 ir2) R`, 86 RW_TAC std_ss [HSPEC_def] THEN 87 METIS_TAC [IR_SEMANTICS_SC] 88 ); 89 90(*---------------------------------------------------------------------------------*) 91(* Block Rule *) 92(* Block of assigment *) 93(*---------------------------------------------------------------------------------*) 94 95val BLK_EQ_SC = Q.store_thm ( 96 "BLK_EQ_SC", 97 `!stm stmL st. (run_ir (BLK (stm::stmL)) st = run_ir (SC (BLK [stm]) (BLK stmL)) st) /\ 98 (run_ir (BLK (SNOC stm stmL)) st = run_ir (SC (BLK stmL) (BLK [stm])) st)`, 99 100 REPEAT GEN_TAC THEN 101 `WELL_FORMED (BLK [stm]) /\ WELL_FORMED (BLK stmL)` by 102 METIS_TAC [BLOCK_IS_WELL_FORMED] THEN 103 STRIP_TAC THENL [ 104 `run_ir (BLK [stm]) st = mdecode st stm` by ( 105 RW_TAC list_ss [run_ir_def, run_arm_def, translate_def, Once RUNTO_ADVANCE] THEN 106 RW_TAC list_ss [GSYM uploadCode_def, UPLOADCODE_LEM] THEN 107 RW_TAC list_ss [GSYM TRANSLATE_ASSIGMENT_CORRECT, ARMCompositionTheory.get_st_def, Once RUNTO_ADVANCE] 108 ) THEN 109 RW_TAC list_ss [IR_SEMANTICS_BLK, IR_SEMANTICS_SC], 110 111 RW_TAC list_ss [SNOC_APPEND, run_ir_def, translate_def] THEN 112 `mk_SC (translate (BLK stmL)) [translate_assignment stm] = translate (BLK (stmL ++ [stm]))` by ( 113 RW_TAC list_ss [ARMCompositionTheory.mk_SC_def] THEN 114 Induct_on `stmL` THENL [ 115 RW_TAC list_ss [translate_def], 116 RW_TAC list_ss [translate_def] THEN 117 METIS_TAC [BLOCK_IS_WELL_FORMED] 118 ]) THEN 119 METIS_TAC [] 120 ] 121 ); 122 123val EMPTY_BLK_AXIOM = Q.store_thm ( 124 "EMPTY_BLK_AXIOM", 125 `!P Q. (!st. P st ==> Q st) ==> 126 HSPEC P (BLK []) Q`, 127 RW_TAC std_ss [HSPEC_def, IR_SEMANTICS_BLK] 128 ); 129 130val BLK_RULE = Q.store_thm ( 131 "BLK_RULE", 132 `!P Q R stm stmL. HSPEC Q (BLK [stm]) R /\ 133 HSPEC P (BLK stmL) Q ==> 134 HSPEC P (BLK (SNOC stm stmL)) R`, 135 RW_TAC std_ss [HSPEC_def] THEN 136 RW_TAC std_ss [BLK_EQ_SC] THEN 137 METIS_TAC [HSPEC_def, SC_RULE, BLOCK_IS_WELL_FORMED] 138 ); 139 140 141(*---------------------------------------------------------------------------------*) 142(* Conditional Jumps *) 143(*---------------------------------------------------------------------------------*) 144 145val CJ_RULE = Q.store_thm ( 146 "CJ_RULE", 147 `!P Q cond ir1 ir2 st. WELL_FORMED ir1 /\ WELL_FORMED ir2 /\ 148 HSPEC (\st.eval_il_cond cond st /\ P st) ir1 Q /\ HSPEC (\st.~eval_il_cond cond st /\ P st) ir2 Q ==> 149 HSPEC P (CJ cond ir1 ir2) Q`, 150 RW_TAC std_ss [HSPEC_def] THEN 151 METIS_TAC [IR_SEMANTICS_CJ] 152 ); 153 154 155val CJ_RULE_2 = Q.store_thm ( 156 "CJ_RULE_2", 157 `!P Q cond ir1 ir2 st. WELL_FORMED ir1 /\ WELL_FORMED ir2 /\ 158 HSPEC P ir1 Q /\ HSPEC P ir2 Q ==> 159 HSPEC P (CJ cond ir1 ir2) Q`, 160 RW_TAC std_ss [HSPEC_def] THEN 161 METIS_TAC [IR_SEMANTICS_CJ] 162 ); 163 164(*---------------------------------------------------------------------------------*) 165(* Tail Recursion *) 166(*---------------------------------------------------------------------------------*) 167 168val TR_RULE = Q.store_thm ( 169 "TR_RULE", 170 `!cond ir P Q. 171 WELL_FORMED ir /\ WF_TR (translate_condition cond, translate ir) /\ 172 HSPEC P ir P ==> HSPEC P (TR cond ir) P`, 173 RW_TAC std_ss [HSPEC_def] THEN 174 METIS_TAC [HOARE_TR_IR] 175 ); 176 177(*---------------------------------------------------------------------------------*) 178(* Well-founded Tail Recursion *) 179(*---------------------------------------------------------------------------------*) 180 181val WF_DEF_2 = Q.store_thm ( 182 "WF_DEF_2", 183 `WF R = !P. (?w. P w) ==> ?min. P min /\ !b. R b min ==> ~P b`, 184 RW_TAC std_ss [relationTheory.WF_DEF] 185 ); 186 187val WF_TR_LEM_1 = Q.store_thm ( 188 "WF_TR_LEM_1", 189 `!cond ir st. WELL_FORMED ir /\ 190 WF (\st1 st0. ~eval_il_cond cond st0 /\ (st1 = run_ir ir st0)) ==> 191 WF_TR (translate_condition cond,translate ir)`, 192 193 RW_TAC std_ss [WELL_FORMED_SUB_thm, WF_TR_def, WF_Loop_def, run_ir_def, run_arm_def] THEN 194 POP_ASSUM MP_TAC THEN Q.ABBREV_TAC `arm = translate ir` THEN STRIP_TAC THEN 195 Q.EXISTS_TAC `\s1 s0. if eval_il_cond cond (get_st s0) then F else (get_st s1 = get_st (runTo (upload arm (\i. ARB) (FST (FST s0))) 196 (FST (FST s0) + LENGTH (translate ir)) s0))` THEN 197 STRIP_TAC THENL [ 198 FULL_SIMP_TAC std_ss [WF_DEF_2, GSYM RIGHT_FORALL_IMP_THM] THEN 199 STRIP_TAC THEN 200 POP_ASSUM (ASSUME_TAC o Q.SPEC `\st. ?pc cpsr pcS. (P:STATEPCS->bool) (((pc,cpsr,st),pcS):STATEPCS)`) THEN 201 STRIP_TAC THEN 202 FULL_SIMP_TAC std_ss [GSYM RIGHT_EXISTS_IMP_THM] THEN 203 `?st pc cpsr pcS. w = ((pc,cpsr,st),pcS)` by METIS_TAC [ABS_PAIR_THM] THEN 204 FULL_SIMP_TAC std_ss [] THEN RES_TAC THEN 205 Q.EXISTS_TAC `((pc',cpsr',st0),pcS')` THEN 206 RW_TAC std_ss [Once get_st_def] THEN RES_TAC THEN 207 `get_st (runTo (upload arm (\i. ARB) pc') (pc'+LENGTH arm) ((pc',cpsr',st0),pcS')) = 208 get_st (runTo (upload arm (\i. ARB) 0) (LENGTH arm) ((0,0w,st0),{}))` by 209 METIS_TAC [well_formed_def, get_st_def, DSTATE_IRRELEVANT_PCS, status_independent_def, FST, DECIDE (Term `!x.0 + x = x`)] THEN 210 METIS_TAC [FST,SND,get_st_def, ABS_PAIR_THM], 211 212 RW_TAC std_ss [get_st_def, eval_il_cond_def] THEN 213 METIS_TAC [WELL_FORMED_INSTB] 214 ] 215 ); 216 217val WF_TR_LEM_2 = Q.store_thm ( 218 "WF_TR_LEM_2", 219 `!cond ir prj_f f cond_f. 220 (!st. cond_f (prj_f st) = eval_il_cond cond st) /\ (!st. prj_f (run_ir ir st) = f (prj_f st)) /\ 221 WF (\t1 t0. ~cond_f t0 /\ (t1 = f t0)) ==> 222 WF (\st1 st0. ~eval_il_cond cond st0 /\ (st1 = run_ir ir st0))`, 223 224 RW_TAC std_ss [WF_DEF_2] THEN 225 Q.PAT_ASSUM `!P.p` (ASSUME_TAC o Q.SPEC `\t:'a. ?y:DSTATE. (prj_f y = t) /\ P y`) THEN 226 FULL_SIMP_TAC std_ss [GSYM RIGHT_EXISTS_IMP_THM] THEN 227 RES_TAC THEN 228 Q.EXISTS_TAC `y` THEN 229 RW_TAC std_ss [] THEN 230 `~cond_f (prj_f y)` by METIS_TAC [] THEN 231 RES_TAC THEN 232 Q.PAT_ASSUM `!t1.p` (ASSUME_TAC o Q.SPEC `prj_f (st1:DSTATE)`) THEN 233 METIS_TAC [] 234 ); 235 236val WF_TR_LEM_3 = Q.store_thm ( 237 "WF_TR_LEM_3", 238 `!cond_f f. (?R. WF R /\ !t0 t1. ~cond_f t0 ==> R (f t0) t0) ==> 239 WF (\t1 t0. ~cond_f t0 /\ (t1 = f t0))`, 240 RW_TAC std_ss [] THEN 241 MATCH_MP_TAC WF_SUBSET THEN 242 Q.EXISTS_TAC `R` THEN 243 RW_TAC std_ss [] 244 ); 245 246val WF_TR_THM_1 = Q.store_thm ( 247 "WF_TR_THM_1", 248 `!cond ir prj_f f cond_f pre_p. 249 (!st. cond_f (prj_f st) = eval_il_cond cond st) /\ 250 (!st. pre_p st ==> (prj_f (run_ir ir st) = f (prj_f st))) /\ 251 WF (\t1 t0. ~cond_f t0 /\ (t1 = f t0)) ==> 252 WF (\st1 st0. (pre_p st0) /\ ~(eval_il_cond cond st0) /\ (st1 = run_ir ir st0))`, 253 254 RW_TAC std_ss [WF_DEF_2] THEN 255 Q.PAT_ASSUM `!P.p` (ASSUME_TAC o Q.SPEC `\t:'a. ?y:DSTATE. (prj_f y = t) /\ P y`) THEN 256 FULL_SIMP_TAC std_ss [GSYM RIGHT_EXISTS_IMP_THM] THEN 257 RES_TAC THEN 258 Q.EXISTS_TAC `y` THEN 259 RW_TAC std_ss [] THEN 260 `~cond_f (prj_f y)` by METIS_TAC [] THEN 261 RES_TAC THEN 262 Q.PAT_ASSUM `!y1.p` (ASSUME_TAC o Q.SPEC `prj_f (run_ir ir y)`) THEN 263 METIS_TAC [] 264 ); 265 266(*---------------------------------------------------------------------------------*) 267(* Hoare Rules on Projection on Inputs and Ouputs (represented *) 268(* by projective functions *) 269(* The pre-conditions and post-conditions (on data other than inputs and *) 270(* outputs) are also specified *) 271(*---------------------------------------------------------------------------------*) 272 273val PSPEC_def = Define ` 274 PSPEC ir (pre_p,post_p) stk_f (in_f,f,out_f) = 275 !v x. HSPEC (\st. pre_p st /\ (stk_f st = x) /\ (in_f st = v)) 276 ir (\st. post_p st /\ (stk_f st = x) /\ (out_f st = f v))`; 277 278val _ = type_abbrev("PSPEC_TYPE", type_of (Term `PSPEC`)); 279 280val PSPEC_STACK = Q.store_thm ( 281 "PSPEC_STACK", 282 `!ir pre_p post_p stk_f in_f f out_f x. 283 PSPEC ir (pre_p,post_p) stk_f (in_f,f,out_f) 284 ==> 285 HSPEC (\st. pre_p st /\ (stk_f st = x)) ir (\st. post_p st /\ (stk_f st = x))`, 286 RW_TAC std_ss [PSPEC_def, HSPEC_def] 287 ); 288 289val PSPEC_CHARACTERISTIC = Q.store_thm ( 290 "PSPEC_CHARACTERISTIC", 291 `!ir pre_p post_p stk_f in_f f out_f. 292 PSPEC ir (pre_p,post_p) stk_f (in_f,f,out_f) 293 ==> 294 HSPEC (\st. pre_p st /\ (in_f st = v)) ir (\st. post_p st /\ (out_f st = f v))`, 295 RW_TAC std_ss [PSPEC_def, HSPEC_def] 296 ); 297 298val PRJ_SHUFFLE_RULE = Q.store_thm ( 299 "PRJ_SHUFFLE_RULE", 300 `!ir pre_p post_p stk_f in_f f out_f shuffle_f. 301 PSPEC ir (pre_p,post_p) stk_f (in_f,f,out_f) 302 ==> 303 PSPEC ir (pre_p, post_p) stk_f (in_f, shuffle_f o f, shuffle_f o out_f)`, 304 RW_TAC std_ss [PSPEC_def, HSPEC_def] 305 ); 306 307val PRJ_SHUFFLE_RULE2 = Q.store_thm ( 308 "PRJ_SHUFFLE_RULE2", 309 `!ir pre_p post_p stk_f in_f f out_f g in_f'. 310 PSPEC ir (pre_p, post_p) stk_f (in_f, f, out_f) /\ (g o in_f' = f o in_f) 311 ==> 312 PSPEC ir (pre_p,post_p) stk_f (in_f', g, out_f)`, 313 RW_TAC std_ss [PSPEC_def, HSPEC_def] THEN 314 METIS_TAC [FUN_EQ_THM, combinTheory.o_THM] 315 ); 316 317val PRJ_SC_RULE = Q.store_thm ( 318 "PRJ_SC_RULE", 319 `!ir1 ir2 pre_p1 post_p1 post_p2 stk_f in_f1 f1 f2 out_f1 out_f2. 320 WELL_FORMED ir1 /\ WELL_FORMED ir2 /\ 321 PSPEC ir1 (pre_p1,post_p1) stk_f (in_f1,f1,out_f1) /\ PSPEC ir2 (post_p1,post_p2) stk_f (out_f1,f2,out_f2) 322 ==> 323 PSPEC (SC ir1 ir2) (pre_p1,post_p2) stk_f (in_f1,f2 o f1,out_f2)`, 324 325 RW_TAC std_ss [PSPEC_def] THEN 326 METIS_TAC [SC_RULE] 327 ); 328 329val PRJ_CJ_RULE = Q.store_thm ( 330 "PRJ_CJ_RULE", 331 `!cond ir_t ir_f pre_p post_p stk_f cond_f in_f f1 f2 out_f. 332 WELL_FORMED ir_t /\ WELL_FORMED ir_f /\ 333 PSPEC ir_t (pre_p,post_p) stk_f (in_f,f1,out_f) /\ 334 PSPEC ir_f (pre_p, post_p) stk_f (in_f,f2,out_f) /\ (!st. cond_f (in_f st) = eval_il_cond cond st) 335 ==> 336 PSPEC (CJ cond ir_t ir_f) (pre_p,post_p) stk_f (in_f, (\v.if cond_f v then f1 v else f2 v), out_f)`, 337 338 RW_TAC std_ss [PSPEC_def, HSPEC_def] THEN 339 METIS_TAC [IR_SEMANTICS_CJ] 340 ); 341 342(* Need the theorems in ARMCompositionTheory to prove the PROJ_TR_RULE *) 343val PRJ_TR_RULE = Q.store_thm ( 344 "PRJ_TR_RULE", 345 `!cond ir pre_p stk_f cond_f prj_f f. 346 WELL_FORMED ir /\ WF (\st1 st0. ~eval_il_cond cond st0 /\ (st1 = run_ir ir st0)) /\ 347 (!st. cond_f (prj_f st) = eval_il_cond cond st) /\ PSPEC ir (pre_p,pre_p) stk_f (prj_f,f,prj_f) ==> 348 PSPEC (TR cond ir) (pre_p,pre_p) stk_f (prj_f, WHILE ($~ o cond_f) f, prj_f)`, 349 350 RW_TAC std_ss [PSPEC_def] THEN 351 RW_TAC std_ss [HSPEC_def] THENL [ 352 FULL_SIMP_TAC std_ss [HSPEC_def] THEN 353 METIS_TAC [SIMP_RULE std_ss [HSPEC_def] TR_RULE, WF_TR_LEM_1], 354 355 IMP_RES_TAC (SIMP_RULE std_ss [PSPEC_def] PSPEC_STACK) THEN 356 POP_ASSUM (ASSUME_TAC o Q.SPEC `(stk_f:DSTATE->'a) st`) THEN 357 IMP_RES_TAC WF_TR_LEM_1 THEN 358 IMP_RES_TAC (Q.SPECL [`cond`,`ir`,`\st1. pre_p st1 /\ ((stk_f:DSTATE->'a) 359 st1 = (stk_f:DSTATE->'a) st)`] TR_RULE) THEN 360 POP_ASSUM (ASSUME_TAC o Q.SPEC `st` o SIMP_RULE std_ss [HSPEC_def]) THEN 361 METIS_TAC [], 362 363 IMP_RES_TAC (SIMP_RULE std_ss [PSPEC_def] PSPEC_CHARACTERISTIC) THEN 364 Q.PAT_ASSUM `!v x.p` (K ALL_TAC) THEN 365 `WF_TR (translate_condition cond,translate ir)` by METIS_TAC [WF_TR_LEM_1] THEN 366 FULL_SIMP_TAC std_ss [WELL_FORMED_SUB_thm, HSPEC_def, run_ir_def, run_arm_def, translate_def, eval_il_cond_def] THEN 367 Q.ABBREV_TAC `arm = translate ir` THEN 368 IMP_RES_TAC (SIMP_RULE set_ss [] (Q.SPECL [`translate_condition cond`,`arm`,`(\i. ARB)`,`(0,0w,st):STATE`,`{}`] 369 ARMCompositionTheory.UNROLL_TR_LEM)) THEN 370 POP_ASSUM (ASSUME_TAC o Q.SPEC `st`) THEN 371 FULL_SIMP_TAC std_ss [FUNPOW, ARMCompositionTheory.get_st_def] THEN 372 NTAC 2 (POP_ASSUM (K ALL_TAC)) THEN 373 Induct_on `loopNum (translate_condition cond) arm (\i.ARB) ((0,0w,st),{})` THENL [ 374 REWRITE_TAC [Once EQ_SYM_EQ] THEN RW_TAC std_ss [FUNPOW,ARMCompositionTheory.get_st_def] THEN 375 IMP_RES_TAC ARMCompositionTheory.LOOPNUM_BASIC THEN 376 FULL_SIMP_TAC arith_ss [Once WHILE, ARMCompositionTheory.get_st_def], 377 378 REWRITE_TAC [Once EQ_SYM_EQ] THEN RW_TAC std_ss [FUNPOW] THEN 379 IMP_RES_TAC ARMCompositionTheory.LOOPNUM_INDUCTIVE THEN 380 `v = loopNum (translate_condition cond) arm (\i.ARB) ((0,0w,SND (SND (FST (runTo (upload arm (\i.ARB) 0) (LENGTH arm) 381 ((0,0w,st),{}))))),{})` by METIS_TAC [ABS_PAIR_THM,DECIDE (Term`!x.0+x=x`), 382 ARMCompositionTheory.LOOPNUM_INDEPENDENT_OF_CPSR_PCS, ARMCompositionTheory.get_st_def, 383 FST, SND, ARMCompositionTheory.DSTATE_IRRELEVANT_PCS,ARMCompositionTheory.well_formed_def] THEN 384 RES_TAC THEN Q.PAT_ASSUM `v = x` (ASSUME_TAC o GSYM) THEN 385 FULL_SIMP_TAC std_ss [] THEN POP_ASSUM (K ALL_TAC) THEN 386 Q.PAT_ASSUM `v = x` (ASSUME_TAC o GSYM) THEN FULL_SIMP_TAC std_ss [] THEN POP_ASSUM (K ALL_TAC) THEN 387 Q.PAT_ASSUM `~x` (ASSUME_TAC o SIMP_RULE std_ss [ARMCompositionTheory.get_st_def]) THEN 388 RW_TAC std_ss [Once WHILE] THEN 389 Q.UNABBREV_TAC `arm` THEN 390 `run_ir ir st = SND (SND (FST (runTo (upload (translate ir) (\i. ARB) 0) (LENGTH (translate ir)) 391 ((0,0w,st),{}))))` by RW_TAC arith_ss [ 392 ARMCompositionTheory.get_st_def, run_ir_def, run_arm_def] THEN 393 METIS_TAC [SND,FST,ARMCompositionTheory.get_st_def,ARMCompositionTheory.FUNPOW_DSTATE, ABS_PAIR_THM] 394 ] 395 ] 396 ); 397 398val PRJ_TR_RULE_2 = Q.store_thm ( 399 "PRJ_TR_RULE_2", 400 `!cond ir stk_f cond_f prj_f f. 401 WELL_FORMED ir /\ (!st. cond_f (prj_f st) = eval_il_cond cond st) /\ 402 (?R. WF R /\ !t0 t1. ~cond_f t0 ==> R (f t0) t0) /\ 403 PSPEC ir ((\st.T),(\st.T)) stk_f (prj_f,f,prj_f) ==> 404 PSPEC (TR cond ir) ((\st.T),(\st.T)) stk_f (prj_f, WHILE ($~ o cond_f) f, prj_f)`, 405 406 SIMP_TAC std_ss [PSPEC_def, HSPEC_def] THEN 407 REPEAT GEN_TAC THEN NTAC 2 STRIP_TAC THEN 408 `WF (\st1 st0. ~eval_il_cond cond st0 /\ (st1 = run_ir ir st0))` by METIS_TAC [WF_TR_LEM_2, WF_TR_LEM_3] THEN 409 METIS_TAC [SIMP_RULE std_ss [PSPEC_def, HSPEC_def] (Q.SPECL [`cond`,`ir`,`\st.T`] PRJ_TR_RULE)] 410 ); 411 412 413(*---------------------------------------------------------------------------------*) 414(* Rules for Conditions (projective function version) *) 415(*---------------------------------------------------------------------------------*) 416 417val PRJ_STRENGTHEN_RULE = Q.store_thm ( 418 "PRJ_STRENGTHEN_RULE", 419 `!ir pre_p pre_p' post_p stk_f in_f f out_f. 420 WELL_FORMED ir /\ 421 PSPEC ir (pre_p,post_p) stk_f (in_f,f,out_f) /\ (!st. pre_p' st ==> pre_p st) ==> 422 PSPEC ir (pre_p',post_p) stk_f (in_f,f,out_f)`, 423 RW_TAC std_ss [PSPEC_def, HSPEC_def] 424 ); 425 426val PRJ_WEAKEN_RULE = Q.store_thm ( 427 "PRJ_WEAKEN_RULE", 428 `!ir pre_p post_p post_p' stk_f in_f f out_f. 429 WELL_FORMED ir /\ 430 PSPEC ir (pre_p,post_p) stk_f (in_f,f,out_f) /\ (!st. post_p st ==> post_p' st) ==> 431 PSPEC ir (pre_p,post_p') stk_f (in_f,f,out_f)`, 432 RW_TAC std_ss [PSPEC_def, HSPEC_def] 433 ); 434 435(*---------------------------------------------------------------------------------*) 436(* Rules for Stack (projective function version) *) 437(*---------------------------------------------------------------------------------*) 438 439val valid_push_def = Define ` 440 valid_push (stk_f,in_f,f,out_f) (stk_f',in_f',g,out_f') = 441 !st st'. (stk_f st' = stk_f st) /\ (out_f st' = f (in_f st)) ==> 442 (stk_f' st' = stk_f' st) /\ (out_f' st' = g (in_f' st))`; 443 444val PRJ_POP_RULE = Q.store_thm ( 445 "PRJ_POP_RULE", 446 `!ir pre_p post_p stk_f in_f f out_f stk_f' in_f' g out_f'. 447 PSPEC ir (pre_p,post_p) stk_f (in_f,f,out_f) /\ 448 valid_push (stk_f,in_f,f,out_f) (stk_f',in_f',g,out_f') 449 ==> 450 PSPEC ir (pre_p,post_p) stk_f' (in_f', g, out_f')`, 451 RW_TAC list_ss [PSPEC_def, HSPEC_def, valid_push_def] 452 ); 453 454val P_intact_def = Define ` 455 P_intact (P,Q) (stk_f,stk_g) = 456 !st st'. (stk_f st' = stk_f st) /\ P st /\ Q st' 457 ==> (stk_g st' = stk_g st)`; 458 459val PRJ_PUSH_RULE = Q.store_thm ( 460 "PRJ_PUSH_RULE", 461 `!ir pre_p post_p stk_f in_f f out_f e_f stk_g. 462 PSPEC ir (pre_p,post_p) stk_f (in_f,f,out_f) /\ 463 P_intact (pre_p,post_p) (stk_f,stk_g) 464 ==> PSPEC ir (pre_p,post_p) stk_g (in_f, f, out_f)`, 465 RW_TAC list_ss [PSPEC_def, HSPEC_def, P_intact_def] 466 ); 467 468(*---------------------------------------------------------------------------------*) 469(* Hoare Rules on Projection on Inputs and Ouputs (represented by vectors) *) 470(* To overcome the type restriction on tuples in HOL definitions *) 471(*---------------------------------------------------------------------------------*) 472 473(* Vectors *) 474 475val _ = Hol_datatype ` 476 VEXP = SG of DATA (* registers *) 477 | VT of VEXP # VEXP (* pairs *) 478 `; 479 480val readv_def = Define ` 481 (readv st (PR (a,b)) = VT (readv st a, readv st b)) /\ 482 (readv st x = SG (mread st x))`; 483 484 485(* Vector Stack, modelled as a list of expression vectors *) 486 487val push_def = Define ` 488 push x stk = x :: stk`; 489 490val top_def = Define ` 491 top = HD`; 492 493val pop_def = Define ` 494 pop = TL`; 495 496(* Specification on vectors *) 497 498val VSPEC_def = Define ` 499 VSPEC ir (pre_p,post_p) stk (iv,f,ov) = 500 PSPEC ir (pre_p,post_p) (\st. MAP (readv st) stk) ((\st.readv st iv), f, (\st.readv st ov)) 501 `; 502 503val _ = type_abbrev("VSPEC_TYPE", type_of (Term `VSPEC`)); 504 505val V_SHUFFLE_RULE = Q.store_thm ( 506 "V_SHUFFLE_RULE", 507 `!ir stk iv f ov g iv'. 508 VSPEC ir (pre_p,post_p) stk (iv,f,ov) /\ (!st. g (readv st iv') = f (readv st iv)) 509 ==> 510 VSPEC ir (pre_p,post_p) stk (iv', g, ov)`, 511 RW_TAC std_ss [VSPEC_def, PSPEC_def, HSPEC_def] 512 ); 513 514val V_SC_RULE = Q.store_thm ( 515 "V_SC_RULE", 516 `!ir1 ir2 pre_p1 post_p1 post_p2 stk vi1 f1 vo1 f2 vo2. 517 WELL_FORMED ir1 /\ WELL_FORMED ir2 /\ 518 VSPEC ir1 (pre_p1,post_p1) stk (vi1,f1,vo1) /\ VSPEC ir2 (post_p1,post_p2) stk (vo1,f2,vo2) 519 ==> 520 VSPEC (SC ir1 ir2) (pre_p1,post_p2) stk (vi1,f2 o f1,vo2)`, 521 RW_TAC std_ss [VSPEC_def] THEN 522 METIS_TAC [PRJ_SC_RULE] 523 ); 524 525val V_CJ_RULE = Q.store_thm ( 526 "V_CJ_RULE", 527 `!cond ir_t ir_f pre_p post_p stk cond_f iv f1 f2 ov. 528 WELL_FORMED ir_t /\ WELL_FORMED ir_f /\ 529 VSPEC ir_t (pre_p,post_p) stk (iv,f1,ov) /\ 530 VSPEC ir_f (pre_p, post_p) stk (iv,f2,ov) /\ (!st. cond_f (readv st iv) = eval_il_cond cond st) 531 ==> 532 VSPEC (CJ cond ir_t ir_f) (pre_p,post_p) stk (iv, (\v.if cond_f v then f1 v else f2 v), ov)`, 533 RW_TAC std_ss [VSPEC_def] THEN 534 FULL_SIMP_TAC std_ss [PRJ_CJ_RULE] 535 ); 536 537(* Need the theorems in ARMCompositionTheory to prove the PROJ_TR_RULE *) 538 539val V_TR_RULE = Q.store_thm ( 540 "V_TR_RULE", 541 `!cond ir pre_p stk cond_f iv f. 542 WELL_FORMED ir /\ WF (\st1 st0. ~eval_il_cond cond st0 /\ (st1 = run_ir ir st0)) /\ 543 (!st. cond_f (readv st iv) = eval_il_cond cond st) /\ VSPEC ir (pre_p,pre_p) stk (iv,f,iv) ==> 544 VSPEC (TR cond ir) (pre_p,pre_p) stk (iv, WHILE ($~ o cond_f) f, iv)`, 545 546 RW_TAC std_ss [VSPEC_def] THEN 547 FULL_SIMP_TAC std_ss [PRJ_TR_RULE] 548 ); 549 550(*---------------------------------------------------------------------------------*) 551(* Rules for Conditions (vector version) *) 552(*---------------------------------------------------------------------------------*) 553 554val V_STRENGTHEN_RULE = Q.store_thm ( 555 "V_STRENGTHEN_RULE", 556 `!ir pre_p pre_p' post_p stk iv f ov. 557 WELL_FORMED ir /\ 558 VSPEC ir (pre_p,post_p) stk (iv,f,ov) /\ (!st. pre_p' st ==> pre_p st) ==> 559 VSPEC ir (pre_p',post_p) stk (iv,f,ov)`, 560 RW_TAC std_ss [VSPEC_def] THEN 561 METIS_TAC [PRJ_STRENGTHEN_RULE] 562 ); 563 564val V_WEAKEN_RULE = Q.store_thm ( 565 "V_WEAKEN_RULE", 566 `!ir pre_p post_p post_p' stk iv f ov. 567 WELL_FORMED ir /\ 568 PSPEC ir (pre_p,post_p) stk (iv,f,ov) /\ (!st. post_p st ==> post_p' st) ==> 569 PSPEC ir (pre_p,post_p') stk (iv,f,ov)`, 570 RW_TAC std_ss [VSPEC_def] THEN 571 METIS_TAC [PRJ_WEAKEN_RULE] 572 ); 573 574(*---------------------------------------------------------------------------------*) 575(* Rules for Stack (vector version) *) 576(*---------------------------------------------------------------------------------*) 577 578val V_POP_RULE = Q.store_thm ( 579 "V_POP_RULE", 580 `!ir pre_p post_p stk iv f ov e g. 581 VSPEC ir (pre_p,post_p) (e::stk) (iv,f,ov) /\ 582 (!st. g (readv st (PR(iv,e))) = VT (f (readv st iv), readv st e)) ==> 583 VSPEC ir (pre_p,post_p) stk (PR(iv,e), g, PR(ov,e))`, 584 RW_TAC list_ss [VSPEC_def, PSPEC_def, HSPEC_def, readv_def] 585 ); 586 587val V_intact_def = Define ` 588 V_intact (P,Q,e) = 589 ?x. (!st.P st ==> (readv st e = x)) /\ (!st.Q st ==> (readv st e = x))`; 590 591 592val V_PUSH_RULE = Q.store_thm ( 593 "V_PUSH_RULE", 594 `!ir pre_p post_p stk iv f ov e. 595 VSPEC ir (pre_p,post_p) stk (iv,f,ov) /\ V_intact(pre_p, post_p, e) 596 ==> 597 VSPEC ir (pre_p,post_p) (e::stk) (iv, f, ov)`, 598 RW_TAC list_ss [VSPEC_def, PSPEC_def, HSPEC_def, V_intact_def, readv_def] THEN 599 METIS_TAC [] 600 ); 601 602 603(*---------------------------------------------------------------------------------*) 604(* Rules for Well-formedness *) 605(*---------------------------------------------------------------------------------*) 606 607val WELL_FORMED_TR_RULE = Q.store_thm ( 608 "WELL_FORMED_TR_RULE", 609 `!cond ir context_f. 610 WELL_FORMED ir /\ WF (\st1 st0. ~eval_il_cond cond st0 /\ (st1 = run_ir ir st0)) ==> 611 WELL_FORMED (TR cond ir)`, 612 613 RW_TAC std_ss [] THEN 614 METIS_TAC [IR_TR_IS_WELL_FORMED, WF_TR_LEM_1] 615 ); 616 617 618 619val IR_CJ_UNCHANGED = store_thm ("IR_CJ_UNCHANGED", 620``!cond ir_t ir_f s. 621 (WELL_FORMED ir_t /\ WELL_FORMED ir_f /\ 622 UNCHANGED s ir_t /\ UNCHANGED s ir_f) ==> 623 UNCHANGED s (CJ cond ir_t ir_f)``, 624 625 626REWRITE_TAC[UNCHANGED_def] THEN 627REPEAT STRIP_TAC THEN 628ASM_SIMP_TAC std_ss [SEMANTICS_OF_IR] THEN 629PROVE_TAC[]); 630 631 632val IR_SC_UNCHANGED = store_thm ("IR_SC_UNCHANGED", 633``!ir1 ir2 s. 634 (WELL_FORMED ir1 /\ WELL_FORMED ir2 /\ 635 UNCHANGED s ir1 /\ UNCHANGED s ir2) ==> 636 UNCHANGED s (SC ir1 ir2)``, 637 638 639REWRITE_TAC[UNCHANGED_def] THEN 640REPEAT STRIP_TAC THEN 641ASM_SIMP_TAC std_ss [SEMANTICS_OF_IR] THEN 642PROVE_TAC[]) 643 644val UNCHANGED_TR_RULE = store_thm ("UNCHANGED_TR_RULE", 645``!c ir s. 646 (WELL_FORMED (TR c ir) /\ UNCHANGED s ir) ==> 647 UNCHANGED s (TR c ir)``, 648 649 REWRITE_TAC [UNCHANGED_def, WELL_FORMED_def] THEN 650 REPEAT STRIP_TAC THEN 651 ASM_SIMP_TAC std_ss [IR_SEMANTICS_TR___FUNPOW] THEN 652 Q.ABBREV_TAC `n = (shortest (eval_il_cond c) (run_ir ir) st)` THEN 653 POP_ASSUM (fn x => ALL_TAC) THEN 654 Induct_on `n` THENL [ 655 REWRITE_TAC[FUNPOW], 656 REWRITE_TAC[FUNPOW_SUC] THEN PROVE_TAC[] 657 ]); 658 659 660val _ = export_theory(); 661