Cookies disclaimer

I agree Our site saves small pieces of text information (cookies) on your device in order to deliver better content and for statistical purposes. You can disable the usage of cookies by changing the settings of your browser. By browsing our website without changing the browser settings you grant us permission to store that information on your device.

Homework 06.2

This is the task corresponding to the second part of homework 6.

Resources

Download Files

Definitions File

theory Defs
  imports "HOL-IMP.BExp" "HOL-IMP.Star"
begin

datatype
  com = SKIP
      | Assign vname aexp       ("_ ::= _" [1000, 61] 61)
      | Seq    com  com         ("_;;/ _"  [60, 61] 60)
      | If     bexp com com     ("(IF _/ THEN _/ ELSE _)"  [0, 0, 61] 61)
      | While  bexp com         ("(WHILE _/ DO _)"  [0, 61] 61)
      | CONTINUE



section "Compiler for IMP"

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"

datatype instr = 
  LOADI int | LOAD vname | ADD | STORE vname |
  JMP int | JMPLESS int | JMPGE int

type_synonym stack = "val list"
type_synonym config = "int × state × stack"

abbreviation "hd2 xs == hd(tl xs)"
abbreviation "tl2 xs == tl(tl xs)"

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) |
  STORE x ⇒ (i+1,s(x := hd stk),tl stk) |
  JMP n ⇒  (i+1+n,s,stk) |
  JMPLESS n ⇒ (if hd2 stk < hd stk then i+1+n else i+1,s,tl2 stk) |
  JMPGE n ⇒ (if hd2 stk >= hd stk then i+1+n else i+1,s,tl2 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)"

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'"
by (auto simp: exec1_def)

lemma exec_appendR: "P ⊢ c →* c' ⟹ P@P' ⊢ c →* c'"
by (induction rule: star.induct) (fastforce intro: star.step 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: star.step exec1_appendL)+

text‹Now we specialise the above lemmas to enable automatic proofs of
\<^prop>‹P ⊢ c →* c'› where ‹P› is a mixture of concrete instructions and
pieces of code that we already know how they execute (by induction), combined
by ‹@› and ‹#›. 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 execution 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 ⇒ bool ⇒ int ⇒ instr list" where
"bcomp (Bc v) f n = (if v=f then [JMP n] else [])" |
"bcomp (Not b) f n = bcomp b (¬f) n" |
"bcomp (And b1 b2) f n =
 (let cb2 = bcomp b2 f n;
        m = if f then size cb2 else (size cb2::int)+n;
      cb1 = bcomp b1 False m
  in cb1 @ cb2)" |
"bcomp (Less a1 a2) f n =
 acomp a1 @ acomp a2 @ (if f then [JMPLESS n] else [JMPGE n])"

value
  "bcomp (And (Less (V ''x'') (V ''y'')) (Not(Less (V ''u'') (V ''v''))))
     False 3"

lemma bcomp_correct[intro]:
  fixes n :: int
  shows
  "0 ≤ n ⟹
  bcomp b f n ⊢
 (0,s,stk)  →*  (size(bcomp b f n) + (if f = bval b s then n else 0),s,stk)"
proof(induction b arbitrary: f n)
  case Not
  from Not(1)[where f="~f"] Not(2) show ?case by fastforce
next
  case (And b1 b2)
  from And(1)[of "if f then size(bcomp b2 f n) else size(bcomp b2 f n) + n" 
                 "False"] 
       And(2)[of n f] And(3) 
  show ?case by fastforce
qed fastforce+
(*>*)

end

Template File

theory Submission
  imports Defs
begin

inductive big_step :: "com × state ⇒ bool × state ⇒ bool" (infix "⇒" 55)
― ‹Your rules here.›


text‹Proof automation:›
declare big_step.intros [intro]

lemmas big_step_induct = big_step.induct[split_format(complete)]

text "Rule inversion"
inductive_cases SkipE[elim!]: "(SKIP,s) ⇒ t"
inductive_cases ContinueE[elim!]: "(CONTINUE,s) ⇒ t"
inductive_cases AssignE[elim!]: "(x ::= a,s) ⇒ t"
inductive_cases SeqE[elim!]: "(c1;;c2,s1) ⇒ s3"
inductive_cases IfE[elim!]: "(IF b THEN c1 ELSE c2,s) ⇒ t"
inductive_cases WhileE[elim]: "(WHILE b DO c,s) ⇒ t"
text‹Only [elim]: [elim!] would not terminate.›


text ‹Compiler›
fun ccomp :: "com ⇒ nat ⇒ instr list" where
  "ccomp _ _ = undefined"

definition
  "len_of c = length (ccomp c 0)"

theorem length_ccomp[simp]:
  "length (ccomp c i) = len_of c"
  sorry

theorem ccomp_bigstep1:
  "(c,s) ⇒ (f, t) ⟹ i ≤ length pre
  ⟹ pre @ ccomp c i ⊢
        (length pre,s,stk) →* (if f then length pre - i else size(pre @ ccomp c i),t,stk)"
  sorry

theorem ccomp_bigstep:
  "(c,s) ⇒ (False, t) ⟹ ccomp c 0 ⊢ (0,s,stk) →* (size(ccomp c 0),t,stk)"
  sorry

end

Check File

theory Check
  imports Submission
begin

theorem length_ccomp[simp]:
  "length (ccomp c i) = len_of c"
  by (rule Submission.length_ccomp)

theorem ccomp_bigstep1:
  "(c,s) ⇒ (f, t) ⟹ i ≤ length pre
  ⟹ pre @ ccomp c i ⊢
        (length pre,s,stk) →* (if f then length pre - i else size(pre @ ccomp c i),t,stk)"
  by (rule Submission.ccomp_bigstep1)

theorem ccomp_bigstep:
  "(c,s) ⇒ (False, t) ⟹ ccomp c 0 ⊢ (0,s,stk) →* (size(ccomp c 0),t,stk)"
  by (rule Submission.ccomp_bigstep)

end

Terms and Conditions