| {"text": "theory Chapter4\n imports \"~~/src/HOL/IMP/ASM\"\nbegin\n\n(*\nExercise 4.1. Start from the data type of binary trees defined earlier:\ndatatype 'a tree = Tip | Node \"'a tree\" 'a \"'a tree\"\n0Define a function set :: 'a tree \\<Rightarrow> 'a set that returns the elements in a tree\nand a function ord :: int tree \\<Rightarrow> bool that tests if an int tree is ordered.\nDefine a function ins that inserts an element into an ordered int tree\nwhile maintaining the order of the tree. If the element is already in the tree,\nthe same tree should be returned. Prove correctness of ins: set (ins x t) =\n{x} \\<union> set t and ord t \\<Longrightarrow> ord (ins i t).\n*)\n\ndatatype 'a tree = Tip | Node \"'a tree\" 'a \"'a tree\"\n\nfun set :: \"'a tree \\<Rightarrow> 'a set\" where\n\"set (Tip) = {}\" |\n\"set (Node l a r) = (set l) \\<union> {a} \\<union> (set r)\"\n\nfun ord :: \"int tree \\<Rightarrow> bool\" where\n\"ord (Tip) = True\" |\n\"ord (Node l a r) = (ord l \\<and> (\\<forall>x\\<in>(set l). x < a) \\<and> ord r \\<and> (\\<forall>x\\<in>(set r). x > a))\"\n\nfun ins :: \"int \\<Rightarrow> int tree \\<Rightarrow> int tree\" where\n\"ins x Tip = Node Tip x Tip\" |\n\"ins x (Node l a r) = (if x = a then (Node l a r)\n else if x < a then (Node (ins x l) a r)\n else (Node l a (ins x r)))\"\n\n\nlemma set_ins: \"set (ins x t) = {x} \\<union> set t\"\n apply(induction t arbitrary: x)\n apply(auto)\n done\n\nlemma ord_ins: \"ord t \\<Longrightarrow> ord (ins x t)\"\n apply(induction t arbitrary: x)\n apply(auto simp add: set_ins)\n done\n\n(*\nExercise 4.2. Formalize the following definition of palindromes\n\u000f The empty list and a singleton list are palindromes.\n\u000f If xs is a palindrome, so is a # xs @ [a].\nas an inductive predicate palindrome :: 'a list \\<Rightarrow> bool and prove that rev xs\n= xs if xs is a palindrome. \n*)\n\ninductive palindrome :: \"'a list \\<Rightarrow> bool\" where\npaE: \"palindrome []\" |\npaS: \"palindrome [x]\" |\npaX: \"palindrome xs \\<Longrightarrow> palindrome (a # xs @ [a])\"\n\nlemma \"palindrome xs \\<Longrightarrow> rev xs = xs\"\n apply(induction rule: palindrome.induct)\n apply(auto)\n done\n\n(*\nExercise 4.3. We could also have defined star as follows:\ninductive star' :: \"('a \\<Rightarrow> 'a \\<Rightarrow> bool) \\<Rightarrow> 'a \\<Rightarrow> 'a \\<Rightarrow> bool\" for r where\nrefl' : \"star' r x x\" |\nstep' : \"star' r x y =\\<Rightarrow> r y z =\\<Rightarrow> star' r x z\"\nThe single r step is performed after rather than before the star steps. Prove\nstar' r x y =\\<Rightarrow> star r x y and star r x y =\\<Rightarrow> star' r x y. You may need\nlemmas. Note that rule induction fails if the assumption about the inductive\npredicate is not the first assumption.\n*)\n\ninductive star :: \"('a \\<Rightarrow> 'a \\<Rightarrow> bool) \\<Rightarrow> 'a \\<Rightarrow> 'a \\<Rightarrow> bool\" for r where\nrefl : \"star r x x\" |\nstep : \"r x y \\<Longrightarrow> star r y z \\<Longrightarrow> star r x z\"\n\ninductive star' :: \"('a \\<Rightarrow> 'a \\<Rightarrow> bool) \\<Rightarrow> 'a \\<Rightarrow> 'a \\<Rightarrow> bool\" for r where\nrefl' : \"star' r x x\" |\nstep' : \"star' r x y \\<Longrightarrow> r y z \\<Longrightarrow> star' r x z\"\n\n\nlemma star_trans: \"star r x y \\<Longrightarrow> star r y z \\<Longrightarrow> star r x z\"\n apply(induction rule: star.induct)\n apply(assumption)\n apply(metis step)\n done\n\nlemma star'_trans : \"star' r y z \\<Longrightarrow> star' r x y \\<Longrightarrow> star' r x z\"\n apply(induction rule: star'.induct)\n apply(assumption)\n apply(metis step')\n done\n\nlemma star'_implies_star: \"star' r x y \\<Longrightarrow> star r x y\"\n apply(induction rule: star'.induct)\n apply(metis refl)\n apply (meson refl step star_trans)\n done\n\nlemma star_implies_star': \"star r x y \\<Longrightarrow> star' r x y\"\n apply(induction rule: star.induct)\n apply(metis refl')\n apply(meson refl' step' star'_trans)\n done\n\n(*\nExercise 4.4. Analogous to star, give an inductive definition of the n-fold\niteration of a relation r : iter r n x y should hold if there are x0, . . . , xn such\nthat x = x0 , xn = y and r xi xi+1 for all i < n. Correct and prove the\nfollowing claim: star r x y \\<Longrightarrow> iter r n x y.\n*)\n\ninductive iter :: \"('a \\<Rightarrow> 'a \\<Rightarrow> bool) \\<Rightarrow> nat \\<Rightarrow> 'a \\<Rightarrow> 'a \\<Rightarrow> bool\" for r where\nit0 : \"iter r 0 x x\" |\nitSS : \"r x y \\<Longrightarrow> iter r n y z \\<Longrightarrow> iter r (Suc n) x z\"\n\nlemma \"star r x y \\<Longrightarrow> \\<exists>n. iter r n x y\"\n apply(induction rule: star.induct)\n apply(metis it0)\n apply(metis itSS)\n done\n\n(*\nExercise 4.5. A context-free grammar can be seen as an inductive definition\nwhere each nonterminal A is an inductively defined predicate on lists of ter-\nminal symbols: A(w) means that w is in the language generated by A. For\nexample, the production S \\<rightarrow> aSb can be viewed as the implication S w =\\<Rightarrow>\nS (a # w @ [b]) where a and b are terminal symbols, i.e., elements of some\nalphabet. The alphabet can be defined like this: datatype alpha = a | b | . . .\nDefine the two grammars (where \\<epsilon> is the empty word)\nS \\<rightarrow> \\<epsilon> | aSb | SS\nT \\<rightarrow> \\<epsilon> | TaTb\nas two inductive predicates. If you think of a and b as “(” and “)”, the\ngrammar defines strings of balanced parentheses. Prove T w =\\<Rightarrow> S w and\nS w =\\<Rightarrow> T w separately and conclude S w = T w.\n*)\n\ndatatype alpha = ay | be | ce | de | ee | ef | ge \n\ninductive S :: \"alpha list \\<Rightarrow> bool\" where\nS0 : \"S []\" |\nSw : \"S w \\<Longrightarrow> S (a # w @ [b])\" |\nSc : \"S w1 \\<Longrightarrow> S w2 \\<Longrightarrow> S (w1 @ w2)\"\n\ninductive T :: \"alpha list \\<Rightarrow> bool\" where\nT0 : \"T []\" |\nTw : \"T w \\<Longrightarrow> T x \\<Longrightarrow> T (w @ [a] @ x @ [b])\"\n\nlemma T_comm: \"T w \\<Longrightarrow> T x \\<Longrightarrow> T (x @ w)\"\n apply(induction rule: T.induct)\n apply(simp)\n apply(metis Tw append_assoc)\n done\n\nlemma T_implies_S: \"T w \\<Longrightarrow> S w\"\n apply(induction rule: T.induct)\n apply(metis S0)\n apply(simp add: Sw Sc)\n done\n\nlemma S_implies_T: \"S w \\<Longrightarrow> T w\"\n apply(induction rule: S.induct)\n apply(metis T0)\n apply (metis Cons_eq_appendI T.simps self_append_conv2)\n apply(simp add: T_comm)\n done\n\ntheorem S_eq_T: \"S w = T w\"\n apply(auto simp add: T_implies_S S_implies_T)\n done\n\n(*\nExercise 4.6. In Section 3.1 we defined a recursive evaluation function aval\n:: aexp \\<Rightarrow> state \\<Rightarrow> val. Define an inductive evaluation predicate aval_rel\n:: aexp \\<Rightarrow> state \\<Rightarrow> val \\<Rightarrow> bool and prove that it agrees with the recursive\nfunction: aval_rel a s v =\\<Rightarrow> aval a s = v, aval a s = v =\\<Rightarrow> aval_rel a s v\nand thus aval_rel a s v \\<leftarrow>\\<rightarrow> aval a s = v .\n*)\n\ninductive avalrel :: \"aexp \\<Rightarrow> state \\<Rightarrow> val \\<Rightarrow> bool\" where\narN : \"avalrel (N a) s a\" |\narV : \"avalrel (V x) s (s x)\" |\narP : \"avalrel p1 s v1 \\<Longrightarrow> avalrel p2 s v2 \\<Longrightarrow> avalrel (Plus p1 p2) s (v1 + v2)\"\n\nlemma avalrel_implies_aval: \"avalrel a s v \\<Longrightarrow> aval a s = v\"\n apply(induction rule: avalrel.induct)\n apply(simp_all add: arN arV arP)\n done\n\nlemma aval_implies_avalrel: \"aval a s = v \\<Longrightarrow> avalrel a s v\"\n apply(induction a arbitrary: v)\n apply(auto simp add: arN arV arP)\n done\n\ntheorem avalrel_iff_aval: \"avalrel a s v \\<longleftrightarrow> aval a s = v\"\n using aval_implies_avalrel avalrel_implies_aval by blast\n\n(*\nExercise 4.7. Consider the stack machine from Chapter 3 and recall the\nconcept of stack underflow from Exercise 3.10. Define an inductive predicate\nok :: nat \\<Rightarrow> instr list \\<Rightarrow> nat \\<Rightarrow> bool such that ok n is n' means that with\nany initial stack of length n the instructions is can be executed without stack\nunderflow and that the final stack has length n'. Prove that ok correctly\ncomputes the final stack size\n\nok n is' n' \\<Longrightarrow> length stk = n \\<Longrightarrow> length (exec is' s stk) = n'\n\nand that instruction sequences generated by comp cannot cause stack under-\nflow: ok n (comp a) ? for some suitable value of ?.\n*)\n\ninductive ok :: \"nat \\<Rightarrow> instr list \\<Rightarrow> nat \\<Rightarrow> bool\" where\nok0 : \"ok n [] n\" |\nokLI : \"ok (Suc n) is n' \\<Longrightarrow> ok n (LOADI _ # is) n'\" |\nokL : \"ok (Suc n) is n' \\<Longrightarrow> ok n (LOAD _ # is) n'\" |\nokA : \"ok (Suc n) is n' \\<Longrightarrow> ok (Suc (Suc n)) (ADD # is) n'\"\n\nlemma okLI2: \"ok n ((LOADI x) # is) n' \\<Longrightarrow> ok (Suc n) is n'\"\n using ok.cases by force\n\nlemma okL2: \"ok n ((LOAD x) # is) n' \\<Longrightarrow> ok (Suc n) is n'\"\n using ok.cases by force\n\nlemma okA2: \"ok (Suc (Suc n)) (ADD # is) n' \\<Longrightarrow> ok (Suc n) is n'\"\n using ok.cases by force\n\n(* hacky function that allows us to induct on three different sizes of stack, allowing us to\nmore easily prove the 'hack' lemma with the ADD instruction *)\nfun dummy :: \"stack \\<Rightarrow> nat\" where\n\"dummy [] = 0\" |\n\"dummy [x] = 0\" |\n\"dummy (x # y # ys) = 0\"\n\nlemma hack: \"\\<lbrakk>\\<And>n stk.\n \\<lbrakk>ok n is' n'; length stk = n\\<rbrakk>\n \\<Longrightarrow> length (exec is' s stk) = n';\n ok (length stk) (a # is') n'\\<rbrakk>\n \\<Longrightarrow> length (exec is' s (exec1 a s stk)) =\n n'\"\n apply (induction a)\n apply(auto)\n apply (meson length_Cons okLI2)\n apply(meson length_Cons okL2)\n apply(induction stk rule: dummy.induct)\n apply(auto)\n using ok.cases apply fastforce\n using ok.cases apply fastforce\n using okA2 by fastforce\n\nlemma check_ok_forall_n_stk: \"\\<And> n stk . ok n is' n' \\<Longrightarrow> length stk = n \\<Longrightarrow> length (exec is' s stk) = n'\"\n apply (induction is')\n apply auto\n using ok.cases apply force\n by (simp add: hack)\n\ntheorem \"ok n is' n' \\<Longrightarrow> length stk = n \\<Longrightarrow> length (exec is' s stk) = n'\"\n by (rule check_ok_forall_n_stk)\n\nlemma comp_append: \"ok n is1 n' \\<Longrightarrow> ok n' is2 n'' \n \\<Longrightarrow> ok n (is1 @ is2) n''\"\n apply(induction rule: ok.induct)\n apply simp\n apply (simp add: okLI)\n apply (simp add: okL)\n by (simp add: okA)\n\ntheorem \"ok n (comp a) (Suc n)\"\n apply (induction a arbitrary: n rule: comp.induct)\n apply(simp add: ok0 okLI)\n apply(simp add: ok0 okL)\n by (metis comp.simps(3) comp_append ok0 okA) \n\nend\n", "meta": {"author": "rasheedja", "repo": "concrete-semantics", "sha": "65997b65adccf690f076a79291aa643e2d1a9d43", "save_path": "github-repos/isabelle/rasheedja-concrete-semantics", "path": "github-repos/isabelle/rasheedja-concrete-semantics/concrete-semantics-65997b65adccf690f076a79291aa643e2d1a9d43/Chapter4.thy", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES\n\n", "lm_q1_score": 0.942506716354847, "lm_q2_score": 0.9664104918104167, "lm_q1q2_score": 0.9108483792871086}} | |