diff -r da9bed7baf23 -r af4fb03ecf32 Tutorial/Tutorial4s.thy --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Tutorial/Tutorial4s.thy Sun Jan 23 07:15:59 2011 +0900 @@ -0,0 +1,244 @@ +theory Tutorial4 +imports Tutorial1 +begin + +section {* The CBV Reduction Relation (Small-Step Semantics) *} + +text {* + In order to help establishing the property that the CK Machine + calculates a nomrmalform that corresponds to the evaluation + relation, we introduce the call-by-value small-step semantics. +*} + +inductive + cbv :: "lam \ lam \ bool" ("_ \cbv _" [60, 60] 60) +where + cbv1: "\val v; atom x \ v\ \ App (Lam [x].t) v \cbv t[x ::= v]" +| cbv2[intro]: "t \cbv t' \ App t t2 \cbv App t' t2" +| cbv3[intro]: "t \cbv t' \ App t2 t \cbv App t2 t'" + +equivariance val +equivariance cbv +nominal_inductive cbv + avoids cbv1: "x" + unfolding fresh_star_def + by (simp_all add: lam.fresh Abs_fresh_iff fresh_Pair fresh_fact) + +text {* + In order to satisfy the vc-condition we have to formulate + this relation with the additional freshness constraint + atom x \ v. Although this makes the definition vc-ompatible, it + makes the definition less useful. We can with a little bit of + pain show that the more restricted rule is equivalent to the + usual rule. +*} + +lemma subst_rename: + assumes a: "atom y \ t" + shows "t[x ::= s] = ((y \ x) \ t)[y ::= s]" +using a +by (nominal_induct t avoiding: x y s rule: lam.strong_induct) + (auto simp add: lam.fresh fresh_at_base) + + +lemma better_cbv1 [intro]: + assumes a: "val v" + shows "App (Lam [x].t) v \cbv t[x::=v]" +proof - + obtain y::"name" where fs: "atom y \ (x, t, v)" by (rule obtain_fresh) + have "App (Lam [x].t) v = App (Lam [y].((y \ x) \ t)) v" using fs + by (auto simp add: lam.eq_iff Abs1_eq_iff' flip_def fresh_Pair fresh_at_base) + also have "\ \cbv ((y \ x) \ t)[y ::= v]" using fs a cbv1 by auto + also have "\ = t[x ::= v]" using fs subst_rename[symmetric] by simp + finally show "App (Lam [x].t) v \cbv t[x ::= v]" by simp +qed + +text {* + The transitive closure of the cbv-reduction relation: +*} + +inductive + "cbvs" :: "lam \ lam \ bool" (" _ \cbv* _" [60, 60] 60) +where + cbvs1[intro]: "e \cbv* e" +| cbvs2[intro]: "\e1\cbv e2; e2 \cbv* e3\ \ e1 \cbv* e3" + +lemma cbvs3 [intro]: + assumes a: "e1 \cbv* e2" "e2 \cbv* e3" + shows "e1 \cbv* e3" +using a by (induct) (auto) + + +subsection {* EXERCISE 8 *} + +text {* + If more simple exercises are needed, then complete the following proof. +*} + +lemma cbv_in_ctx: + assumes a: "t \cbv t'" + shows "E\t\ \cbv E\t'\" +using a +proof (induct E) + case Hole + have "t \cbv t'" by fact + then show "\\t\ \cbv \\t'\" by simp +next + case (CAppL E s) + have ih: "t \cbv t' \ E\t\ \cbv E\t'\" by fact + moreover + have "t \cbv t'" by fact + ultimately + have "E\t\ \cbv E\t'\" by simp + then show "(CAppL E s)\t\ \cbv (CAppL E s)\t'\" by auto +next + case (CAppR s E) + have ih: "t \cbv t' \ E\t\ \cbv E\t'\" by fact + moreover + have a: "t \cbv t'" by fact + ultimately + have "E\t\ \cbv E\t'\" by simp + then show "(CAppR s E)\t\ \cbv (CAppR s E)\t'\" by auto +qed + +section {* EXERCISE 9 *} + +text {* + The point of the cbv-reduction was that we can easily relatively + establish the follwoing property: +*} + +lemma machine_implies_cbvs_ctx: + assumes a: " \ " + shows "(Es\)\e\ \cbv* (Es'\)\e'\" +using a +proof (induct) + case (m1 t1 t2 Es) +thm machine.intros thm cbv2 + have "Es\\App t1 t2\ = (Es\ \ CAppL \ t2)\t1\" using ctx_compose ctx_composes.simps filling.simps by simp + then show "Es\\App t1 t2\ \cbv* ((CAppL \ t2) # Es)\\t1\" using cbvs.intros by simp +next + case (m2 v t2 Es) + have "val v" by fact + have "((CAppL \ t2) # Es)\\v\ = (CAppR v \ # Es)\\t2\" using ctx_compose ctx_composes.simps filling.simps by simp + then show "((CAppL \ t2) # Es)\\v\ \cbv* (CAppR v \ # Es)\\t2\" using cbvs.intros by simp +next + case (m3 v x t Es) + have aa: "val v" by fact + have "(((CAppR (Lam [x].t) \) # Es)\)\v\ = Es\\App (Lam [x]. t) v\" using ctx_compose ctx_composes.simps filling.simps by simp + then have "(((CAppR (Lam [x].t) \) # Es)\)\v\ \cbv (Es\)\(t[x ::= v])\" using better_cbv1[OF aa] cbv_in_ctx by simp + then show "(((CAppR (Lam [x].t) \) # Es)\)\v\ \cbv* (Es\)\(t[x ::= v])\" using cbvs.intros by blast +qed + +text {* + It is not difficult to extend the lemma above to + arbitrary reductions sequences of the CK machine. *} + +lemma machines_implies_cbvs_ctx: + assumes a: " \* " + shows "(Es\)\e\ \cbv* (Es'\)\e'\" +using a machine_implies_cbvs_ctx +by (induct) (blast)+ + +text {* + So whenever we let the CL machine start in an initial + state and it arrives at a final state, then there exists + a corresponding cbv-reduction sequence. +*} + +corollary machines_implies_cbvs: + assumes a: " \* " + shows "e \cbv* e'" +proof - + have "[]\\e\ \cbv* []\\e'\" + using a machines_implies_cbvs_ctx by blast + then show "e \cbv* e'" by simp +qed + +text {* + We now want to relate the cbv-reduction to the evaluation + relation. For this we need two auxiliary lemmas. +*} + +lemma eval_val: + assumes a: "val t" + shows "t \ t" +using a by (induct) (auto) + + +lemma e_App_elim: + assumes a: "App t1 t2 \ v" + obtains x t v' where "t1 \ Lam [x].t" "t2 \ v'" "t[x::=v'] \ v" +using a by (cases) (auto simp add: lam.eq_iff lam.distinct) + + +subsection {* EXERCISE *} + +text {* + Complete the first and second case in the + proof below. +*} + +lemma cbv_eval: + assumes a: "t1 \cbv t2" "t2 \ t3" + shows "t1 \ t3" +using a +proof(induct arbitrary: t3) + case (cbv1 v x t t3) + have a1: "val v" by fact + have a2: "t[x ::= v] \ t3" by fact + have a3: "Lam [x].t \ Lam [x].t" by auto + have a4: "v \ v" using a1 eval_val by auto + show "App (Lam [x].t) v \ t3" using a3 a4 a2 by auto +next + case (cbv2 t t' t2 t3) + have ih: "\t3. t' \ t3 \ t \ t3" by fact + have "App t' t2 \ t3" by fact + then obtain x t'' v' + where a1: "t' \ Lam [x].t''" + and a2: "t2 \ v'" + and a3: "t''[x ::= v'] \ t3" by (rule e_App_elim) + have "t \ Lam [x].t''" using ih a1 by auto + then show "App t t2 \ t3" using a2 a3 by auto +qed (auto elim!: e_App_elim) + + +text {* + Next we extend the lemma above to arbitray initial + sequences of cbv-reductions. *} + +lemma cbvs_eval: + assumes a: "t1 \cbv* t2" "t2 \ t3" + shows "t1 \ t3" +using a by (induct) (auto intro: cbv_eval) + +text {* + Finally, we can show that if from a term t we reach a value + by a cbv-reduction sequence, then t evaluates to this value. +*} + +lemma cbvs_implies_eval: + assumes a: "t \cbv* v" "val v" + shows "t \ v" +using a +by (induct) (auto intro: eval_val cbvs_eval) + +text {* + All facts tied together give us the desired property about + machines. +*} + +theorem machines_implies_eval: + assumes a: " \* " + and b: "val t2" + shows "t1 \ t2" +proof - + have "t1 \cbv* t2" using a machines_implies_cbvs by simp + then show "t1 \ t2" using b cbvs_implies_eval by simp +qed + + + + +end +