theory tut06 imports "~~/src/HOL/IMP/Big_Step" "~~/src/HOL/IMP/Star" begin subsection "List setup" text {* In the following, we use the length of lists as integers instead of natural numbers. Instead of converting @{typ nat} to @{typ int} explicitly, we tell Isabelle to coerce @{typ nat} automatically when necessary. *} declare [[coercion_enabled]] declare [[coercion "int :: nat \ int"]] text {* Similarly, we will want to access the ith element of a list, where @{term i} is an @{typ int}. *} fun inth :: "'a list \ int \ 'a" (infixl "!!" 100) where "(x # xs) !! i = (if i = 0 then x else xs !! (i - 1))" text {* The only additional lemma we need about this function is indexing over append: *} lemma inth_append [simp]: "0 \ i \ (xs @ ys) !! i = (if i < size xs then xs !! i else ys !! (i - size xs))" by (induction xs arbitrary: i) (auto simp: algebra_simps) text{* We hide coercion @{const int} applied to @{const length}: *} abbreviation (output) "isize xs == int (length xs)" notation isize ("size") subsection "Instructions and Stack Machine" text_raw{*\snip{instrdef}{0}{1}{% *} datatype instr = LOADI int | LOAD vname | ADD | STORE vname | NOT | AND | LESS | JMP int | JMPFALSE int text_raw{*}%endsnip*} type_synonym stack = "val list" type_synonym config = "int \ state \ stack" abbreviation "hd2 xs == hd(tl xs)" abbreviation "tl2 xs == tl(tl xs)" abbreviation i2b :: "int \ bool" where "i2b i \ if i=0 then False else True" abbreviation b2i :: "bool \ int" where "b2i b \ if b then 1 else 0" fun iexec :: "instr \ config \ config" where "iexec instr (i,s,stk) = (case instr of LOADI n \ (i+1,s, n#stk) | LOAD x \ (i+1,s, s x # stk) | ADD \ (i+1,s, (hd2 stk + hd stk) # tl2 stk) | NOT \ (i+1,s,(b2i(\i2b (hd stk)))#tl stk) | AND \ (i+1,s, b2i( i2b (hd2 stk) \ i2b (hd stk)) # tl2 stk) | LESS \ (i+1,s, b2i( hd2 stk < hd stk) # tl2 stk) | STORE x \ (i+1,s(x := hd stk),tl stk) | JMP n \ (i+1+n,s,stk) | JMPFALSE n \ (if i2b (hd stk) then i+1 else i+1+n, s, tl stk) )" definition exec1 :: "instr list \ config \ config \ bool" ("(_/ \ (_ \/ _))" [59,0,59] 60) where "P \ c \ c' = (\i s stk. c = (i,s,stk) \ c' = iexec(P!!i) (i,s,stk) \ 0 \ i \ i < size P)" lemma exec1I [intro, code_pred_intro]: "c' = iexec (P!!i) (i,s,stk) \ 0 \ i \ i < size P \ P \ (i,s,stk) \ c'" by (simp add: exec1_def) abbreviation exec :: "instr list \ config \ config \ bool" ("(_/ \ (_ \*/ _))" 50) where "exec P \ star (exec1 P)" declare star.step[intro] lemmas exec_induct = star.induct [of "exec1 P", split_format(complete)] code_pred exec1 by (metis exec1_def) values "{(i,map t [''x'',''y''],stk) | i t stk. [LOAD ''y'', STORE ''x''] \ (0, <''x'' := 3, ''y'' := 4>, []) \* (i,t,stk)}" subsection{* Verification infrastructure *} text{* Below we need to argue about the execution of code that is embedded in larger programs. For this purpose we show that execution is preserved by appending code to the left or right of a program. *} lemma iexec_shift [simp]: "((n+i',s',stk') = iexec x (n+i,s,stk)) = ((i',s',stk') = iexec x (i,s,stk))" by(auto split:instr.split) lemma exec1_appendR: "P \ c \ c' \ P@P' \ c \ c'" apply (fastforce simp: exec1_def) done lemma exec_appendR: "P \ c \* c' \ P@P' \ c \* c'" by (induction rule: star.induct) (fastforce intro: exec1_appendR)+ lemma exec1_appendL: fixes i i' :: int shows "P \ (i,s,stk) \ (i',s',stk') \ P' @ P \ (size(P')+i,s,stk) \ (size(P')+i',s',stk')" unfolding exec1_def by (auto simp del: iexec.simps) lemma exec_appendL: fixes i i' :: int shows "P \ (i,s,stk) \* (i',s',stk') \ P' @ P \ (size(P')+i,s,stk) \* (size(P')+i',s',stk')" by (induction rule: exec_induct) (blast intro!: exec1_appendL)+ text{* Now we specialise the above lemmas to enable automatic proofs of @{prop "P \ c \* c'"} where @{text P} is a mixture of concrete instructions and pieces of code that we already know how they execute (by induction), combined by @{text "@"} and @{text "#"}. Backward jumps are not supported. The details should be skipped on a first reading. If we have just executed the first instruction of the program, drop it: *} lemma exec_Cons_1 [intro]: "P \ (0,s,stk) \* (j,t,stk') \ instr#P \ (1,s,stk) \* (1+j,t,stk')" by (drule exec_appendL[where P'="[instr]"]) simp lemma exec_appendL_if[intro]: fixes i i' j :: int shows "size P' <= i \ P \ (i - size P',s,stk) \* (j,s',stk') \ i' = size P' + j \ P' @ P \ (i,s,stk) \* (i',s',stk')" by (drule exec_appendL[where P'=P']) simp text{* Split the execution of a compound program up into the excution of its parts: *} lemma exec_append_trans[intro]: fixes i' i'' j'' :: int shows "P \ (0,s,stk) \* (i',s',stk') \ size P \ i' \ P' \ (i' - size P,s',stk') \* (i'',s'',stk'') \ j'' = size P + i'' \ P @ P' \ (0,s,stk) \* (j'',s'',stk'')" by(metis star_trans[OF exec_appendR exec_appendL_if]) declare Let_def[simp] subsection "Compilation" fun acomp :: "aexp \ instr list" where "acomp (N n) = [LOADI n]" | "acomp (V x) = [LOAD x]" | "acomp (Plus a1 a2) = acomp a1 @ acomp a2 @ [ADD]" lemma acomp_correct[intro]: "acomp a \ (0,s,stk) \* (size(acomp a),s,aval a s#stk)" by (induction a arbitrary: stk) fastforce+ fun bcomp :: "bexp \ instr list" where "bcomp (Bc v) = [LOADI (b2i v)]" | "bcomp (Not b) = bcomp b @ [NOT]" | "bcomp (And b1 b2) = bcomp b1 @ bcomp b2 @ [AND]" | "bcomp (Less a1 a2) = acomp a1 @ acomp a2 @ [LESS]" value "bcomp (And (Less (V ''x'') (V ''y'')) (Not(Less (V ''u'') (V ''v''))))" lemma bcomp_correct[intro]: shows "bcomp b \ (0,s,stk) \* (size(bcomp b),s,(b2i (bval b s))#stk)" apply (induction b arbitrary: stk) apply (fastforce intro!: exec_append_trans)+ done fun ccomp :: "com \ instr list" where "ccomp SKIP = []" | "ccomp (x ::= a) = acomp a @ [STORE x]" | "ccomp (c\<^sub>1;;c\<^sub>2) = ccomp c\<^sub>1 @ ccomp c\<^sub>2" | "ccomp (IF b THEN c\<^sub>1 ELSE c\<^sub>2) = (let cc\<^sub>1 = ccomp c\<^sub>1; cc\<^sub>2 = ccomp c\<^sub>2; cb = bcomp b in cb @ [JMPFALSE (size cc\<^sub>1 + 1)] @ cc\<^sub>1 @ JMP (size cc\<^sub>2) # cc\<^sub>2)" | "ccomp (WHILE b DO c) = (let cc = ccomp c; cb = bcomp b in cb @ [JMPFALSE (size cc + 1)] @ cc @ [JMP (-(size cb + size cc + 2))])" value "ccomp (IF Less (V ''u'') (N 1) THEN ''u'' ::= Plus (V ''u'') (N 1) ELSE ''v'' ::= V ''u'')" value "ccomp (WHILE Less (V ''u'') (N 1) DO (''u'' ::= Plus (V ''u'') (N 1)))" subsection "Preservation of semantics" lemma ccomp_bigstep: "(c,s) \ t \ ccomp c \ (0,s,stk) \* (size(ccomp c),t,stk)" (*apply (induction arbitrary: stk rule: big_step_induct) apply fastforce defer defer apply fastforce defer apply fastforce oops*) proof(induction arbitrary: stk rule: big_step_induct) case (Assign x a s) show ?case by (fastforce simp:fun_upd_def cong: if_cong) next case (Seq c1 s1 s2 c2 s3) let ?cc1 = "ccomp c1" let ?cc2 = "ccomp c2" have "?cc1 @ ?cc2 \ (0,s1,stk) \* (size ?cc1,s2,stk)" using Seq.IH(1) by fastforce moreover have "?cc1 @ ?cc2 \ (size ?cc1,s2,stk) \* (size(?cc1 @ ?cc2),s3,stk)" using Seq.IH(2) by fastforce ultimately show ?case by simp (blast intro: star_trans) next case (WhileTrue b s1 c s2 s3) let ?cc = "ccomp c" let ?cb = "bcomp b@[JMPFALSE (size ?cc + 1)]" let ?cw = "ccomp(WHILE b DO c)" have "?cw \ (0,s1,stk) \* (size ?cb,s1,stk)" using `bval b s1` by fastforce moreover have "?cw \ (size ?cb,s1,stk) \* (size ?cb + size ?cc,s2,stk)" using WhileTrue.IH(1) by fastforce moreover have "?cw \ (size ?cb + size ?cc,s2,stk) \* (0,s2,stk)" by fastforce moreover have "?cw \ (0,s2,stk) \* (size ?cw,s3,stk)" by(rule WhileTrue.IH(2)) ultimately show ?case by(blast intro: star_trans) next case (IfFalse b s c2 t c1 stk) let ?cc1 = "ccomp c1" let ?cc2 = "ccomp c2" let ?cb = "bcomp b" let ?ci = "ccomp (IF b THEN c1 ELSE c2)" let ?P = "?cb@[JMPFALSE (size ?cc1 + 1)]@?cc1@[JMP (size ?cc2)]" note [trans] = star_trans have "?ci \ (0,s,stk) \* (size ?cb + size ?cc1 + 2,s,stk)" using `\bval b s` by fastforce also from exec_appendL[OF IfFalse.IH[of stk], where P'="?P"] have "?ci \ \ \* (size ?ci,t ,stk)" by (simp add: algebra_simps) finally show ?case . qed fastforce+ notepad begin assume "0 < (5::nat)" also assume "(5::nat) \ 7" also assume "\ = a" also assume "\ < 20" finally have "0 < (20::nat)" . end end