header {* Hand-in on 21 May at noon. Please rename the file and send your solution to me as Hw08_\your name\ *} theory Hw08 imports Nominal begin section {* Task 1 (easy) *} text {* Below I defined substitution, beta-reduction and the transitive closure of beta-reduction. *} atom_decl name nominal_datatype lam = Var "name" | App "lam" "lam" | Lam "\name\lam" ("Lam [_]._" [100,100] 100) consts subst :: "lam \ name \ lam \ lam" ("_[_::=_]" [100,100,100] 100) nominal_primrec "(Var x)[y::=s] = (if x=y then s else (Var x))" "(App t\<^isub>1 t\<^isub>2)[y::=s] = App (t\<^isub>1[y::=s]) (t\<^isub>2[y::=s])" "x\(y,s) \ (Lam [x].t)[y::=s] = Lam [x].(t[y::=s])" apply(finite_guess)+ apply(rule TrueI)+ apply(simp add: abs_fresh) apply(fresh_guess)+ done inductive "beta" :: "lam\lam\bool" (" _ \\<^isub>\ _") where b1[intro]: "t1 \\<^isub>\ t2 \ App t1 s \\<^isub>\ App t2 s" | b2[intro]: "s1 \\<^isub>\ s2 \ App t s1 \\<^isub>\ App t s2" | b3[intro]: "t1 \\<^isub>\ t2 \ Lam [x].t1 \\<^isub>\ Lam [x].t2" | b4[intro]: "App (Lam [x].t) s \\<^isub>\ t[x::=s]" inductive "beta_star" :: "lam\lam\bool" (" _ \\<^isub>\\<^sup>* _") where bs1[intro]: "t \\<^isub>\\<^sup>* t" | bs2[intro]: "t \\<^isub>\ s \ t \\<^isub>\\<^sup>* s" | bs3[intro]: "\t1\\<^isub>\\<^sup>* t2; t2 \\<^isub>\\<^sup>* t3\ \ t1 \\<^isub>\\<^sup>* t3" text {* Show the two lemmas establishing the fact that the beta-star-reduction is preserved under the term constructors. Hint: since the introduction rules of the inductive definitions are labelled with "intro", Isabelle is already aware of them (you do no need to say intro: beta.intros). Hint: The second lemma might have to be established using "two" side-lemmas. *} lemma Beta_Lam_cong: assumes a: "t1 \\<^isub>\\<^sup>* t2" shows "Lam [x].t1 \\<^isub>\\<^sup>* Lam [x].t2" using a by (induct) (blast)+ lemma Beta_App_cong_aux: assumes a: "t1 \\<^isub>\\<^sup>* t2" shows "App t1 s\\<^isub>\\<^sup>* App t2 s" and "App s t1 \\<^isub>\\<^sup>* App s t2" using a by (induct) (blast)+ lemma Beta_App_cong: assumes a: "t1 \\<^isub>\\<^sup>* t2" "s1 \\<^isub>\\<^sup>* s2" shows "App t1 s1 \\<^isub>\\<^sup>* App t2 s2" using a by (blast intro: Beta_App_cong_aux) section {* Task 2 (easy) *} text {* Give a detailed Isar proof for the formula (\x. P x) \ (\x. P x). Hint: The rules needed for dealing with the quantifiers are thm allE thm exI Hint: Use the notes http://www.cl.cam.ac.uk/research/hvg/Isabelle/dist/Isabelle/doc/isar-overview.pdf *} lemma "(\x. P x) \ (\x. P x)" proof - { assume "\x. P x" then have "P t" by (rule allE) then have "\x. P x" by (rule exI) } then show "(\x. P x) \ (\x. P x)" by (rule impI) qed text {* Task 3 (hard) Permutations are lists of pairs of names. Below I defined how such a permutation operates on names (remember Lecture 7). *} fun perm :: "(name\name) list \ name \ name" ("_ \ _") where "[] \ x = x" | "((z,y)#pi) \ x = (if pi \ x = z then y else (if pi \ x = y then z else pi \ x))" text {* Since permutations are represented as list, the inverse of a permutation is the reversal of the list. For example permutation its inverse [(a,c)(a,d)] [(a,d),(a,c)] Lemmas l1 and l2 show how both permutations operate on the names a, b, c and d. You can notice that in the argument and result in the first lemma are reversed in the second. (The reason is that permutations are bijections.) *} lemma l1: assumes a: "distinct [a,b,c,d]" shows "[(a,c),(a,d)] \ a = d" and "[(a,c),(a,d)] \ b = b" and "[(a,c),(a,d)] \ c = a" and "[(a,c),(a,d)] \ d = c" using a by (auto) lemma l2: assumes a: "distinct [a,b,c,d]" shows "[(a,d),(a,c)] \ a = c" and "[(a,d),(a,c)] \ b = b" and "[(a,d),(a,c)] \ c = d" and "[(a,d),(a,c)] \ d = a" using a by (auto) text {* The task is to show the general fact that applying a permutation and its inverse to a name is the identity. Hint: you might need a lemma about appended permutations. *} lemma pi_append: shows "(pi1@pi2) \ x = pi1 \ (pi2 \ x)" by (induct pi1) (auto) lemma shows "(rev pi) \ (pi \ x) = x" by (induct pi) (auto simp add: pi_append) text {* Task 4: (hard) Below I define contexts having a single hole and the operation of filling a lambda-term into that hole. Prove the lemma showing that for every reduction, there must be a context which uniquely identifies where the reduction occurred. *} datatype ctx = Hole ("\" 1000) | CAppL "ctx" "lam" | CAppR "lam" "ctx" | CLam "name" "ctx" ("CLam [_]._" [100,100] 100) fun filling :: "ctx \ lam \ lam" ("_\_\") where "\\t\ = t" | "(CAppL E t')\t\ = App (E\t\) t'" | "(CAppR t' E)\t\ = App t' (E\t\)" | "(CLam [x].E)\t\ = Lam [x].(E\t\)" lemma assumes a: "t \\<^isub>\ t'" shows "\C x s1 s2. t = C\App (Lam [x].s1) s2\ \ t' = C\s1[x::=s2]\" using a by (induct) (metis b1 b2 b3 b4 filling.simps)+ lemma assumes a: "t \\<^isub>\ t'" shows "\C x s1 s2. t = C\App (Lam [x].s1) s2\ \ t' = C\s1[x::=s2]\" using a proof (induct) case (b1 t1 t2 s) have "\C x s1 s2. t1 = C\App (Lam [x].s1) s2\ \ t2 = C\s1[x::=s2]\" by fact then obtain C x s1 s2 where "t1 = C\App (Lam [x].s1) s2\ \ t2 = C\s1[x::=s2]\" by blast then have "App t1 s = (CAppL C s)\App (Lam [x].s1) s2\ \ App t2 s = (CAppL C s)\s1[x::=s2]\" by simp then show "\C x s1 s2. App t1 s = C\App (Lam [x].s1) s2\ \ App t2 s = C\s1[x::=s2]\" by blast next case (b2 t1 t2 s) have "\C x s1 s2. t1 = C\App (Lam [x].s1) s2\ \ t2 = C\s1[x::=s2]\" by fact then obtain C x s1 s2 where "t1 = C\App (Lam [x].s1) s2\ \ t2 = C\s1[x::=s2]\" by blast then have "App s t1 = (CAppR s C)\App (Lam [x].s1) s2\ \ App s t2 = (CAppR s C)\s1[x::=s2]\" by simp then show "\C x s1 s2. App s t1 = C\App (Lam [x].s1) s2\ \ App s t2 = C\s1[x::=s2]\" by blast next case (b3 t1 t2 y) have "\C x s1 s2. t1 = C\App (Lam [x].s1) s2\ \ t2 = C\s1[x::=s2]\" by fact then obtain C x s1 s2 where "t1 = C\App (Lam [x].s1) s2\ \ t2 = C\s1[x::=s2]\" by blast then have "Lam [y].t1 = (CLam [y].C)\App (Lam [x].s1) s2\ \ Lam [y].t2 = (CLam [y].C)\s1[x::=s2]\" by simp then show "\C x s1 s2. Lam [y].t1 = C\App (Lam [x].s1) s2\ \ Lam [y].t2 = C\s1[x::=s2]\" by blast next case (b4 y t s) then have "App (Lam [y].t) s = \\App (Lam [y].t) s\ \ t[y::=s] = \\t[y::=s]\" by simp then show "\C x s1 s2. App (Lam [y].t) s = C\App (Lam [x].s1) s2\ \ t[y::=s] = C\s1[x::=s2]\" by blast qed text {* Task 5: (?) Prove the follwoing lemma. Hint: Think carefully about the induction. *} lemma assumes a: "t \\<^isub>\ t'" shows "C\t\ \\<^isub>\ C\t'\" using a by (induct C) (auto) end