Tutorial/Tutorial4.thy
changeset 2701 7b2691911fbc
parent 2693 2abc8cb46a5c
child 2706 8ae1c2e6369e
equal deleted inserted replaced
2700:e0391947b7da 2701:7b2691911fbc
     1 
     1 
     2 theory Tutorial4
     2 theory Tutorial4
     3 imports Tutorial1 Tutorial2 Tutorial3
     3 imports Tutorial1 Tutorial2
     4 begin
     4 begin
     5 
     5 
     6 section {* The CBV Reduction Relation (Small-Step Semantics) *}
     6 section {* The CBV Reduction Relation (Small-Step Semantics) *}
     7 
     7 
     8 
     8 
    19   cbvs1: "e \<longrightarrow>cbv* e"
    19   cbvs1: "e \<longrightarrow>cbv* e"
    20 | cbvs2: "\<lbrakk>e1\<longrightarrow>cbv e2; e2 \<longrightarrow>cbv* e3\<rbrakk> \<Longrightarrow> e1 \<longrightarrow>cbv* e3"
    20 | cbvs2: "\<lbrakk>e1\<longrightarrow>cbv e2; e2 \<longrightarrow>cbv* e3\<rbrakk> \<Longrightarrow> e1 \<longrightarrow>cbv* e3"
    21 
    21 
    22 declare cbv.intros[intro] cbv_star.intros[intro]
    22 declare cbv.intros[intro] cbv_star.intros[intro]
    23 
    23 
    24 subsection {* EXERCISE 3 *}
    24 subsection {* EXERCISE 11 *}
    25 
    25 
    26 text {*
    26 text {*
    27   Show that cbv* is transitive, by filling the gaps in the 
    27   Show that cbv* is transitive, by filling the gaps in the 
    28   proof below.
    28   proof below.
    29 *}
    29 *}
    30 
    30 
    31 lemma 
    31 lemma cbvs3 [intro]:
    32   assumes a: "e1 \<longrightarrow>cbv* e2" "e2 \<longrightarrow>cbv* e3"
    32   assumes a: "e1 \<longrightarrow>cbv* e2" "e2 \<longrightarrow>cbv* e3"
    33   shows "e1 \<longrightarrow>cbv* e3"
    33   shows "e1 \<longrightarrow>cbv* e3"
    34 using a 
    34 using a 
    35 proof (induct) 
    35 proof (induct) 
    36   case (cbvs1 e1)
    36   case (cbvs1 e1)
    44   have "e3' \<longrightarrow>cbv* e3" by fact
    44   have "e3' \<longrightarrow>cbv* e3" by fact
    45 
    45 
    46   show "e1 \<longrightarrow>cbv* e3" sorry
    46   show "e1 \<longrightarrow>cbv* e3" sorry
    47 qed 
    47 qed 
    48 
    48 
    49 lemma cbvs3 [intro]:
    49 
    50   assumes a: "e1 \<longrightarrow>cbv* e2" "e2 \<longrightarrow>cbv* e3"
    50 text {*
    51   shows "e1 \<longrightarrow>cbv* e3"
    51   In order to help establishing the property that the machine
    52 using a by (induct) (auto) 
       
    53 
       
    54 
       
    55 
       
    56 
       
    57 
       
    58 text {*
       
    59   In order to help establishing the property that the CK Machine
       
    60   calculates a nomrmalform that corresponds to the evaluation 
    52   calculates a nomrmalform that corresponds to the evaluation 
    61   relation, we introduce the call-by-value small-step semantics.
    53   relation, we introduce the call-by-value small-step semantics.
    62 *}
    54 *}
    63 
    55 
    64 
    56 
    96   also have "\<dots> \<longrightarrow>cbv ((y \<leftrightarrow> x) \<bullet> t)[y ::= v]" using fs a cbv1 by auto
    88   also have "\<dots> \<longrightarrow>cbv ((y \<leftrightarrow> x) \<bullet> t)[y ::= v]" using fs a cbv1 by auto
    97   also have "\<dots> = t[x ::= v]" using fs subst_rename[symmetric] by simp
    89   also have "\<dots> = t[x ::= v]" using fs subst_rename[symmetric] by simp
    98   finally show "App (Lam [x].t) v \<longrightarrow>cbv t[x ::= v]" by simp
    90   finally show "App (Lam [x].t) v \<longrightarrow>cbv t[x ::= v]" by simp
    99 qed
    91 qed
   100 
    92 
   101 text {*
    93 
   102   The transitive closure of the cbv-reduction relation: 
    94 
   103 *}
    95 subsection {* EXERCISE 12 *}
   104 
       
   105 
       
   106 
       
   107 
       
   108 
       
   109 subsection {* EXERCISE 8 *}
       
   110 
    96 
   111 text {*  
    97 text {*  
   112   If more simple exercises are needed, then complete the following proof. 
    98   If more simple exercises are needed, then complete the following proof. 
   113 *}
    99 *}
   114 
   100 
   117   shows "E\<lbrakk>t\<rbrakk> \<longrightarrow>cbv E\<lbrakk>t'\<rbrakk>"
   103   shows "E\<lbrakk>t\<rbrakk> \<longrightarrow>cbv E\<lbrakk>t'\<rbrakk>"
   118 using a
   104 using a
   119 proof (induct E)
   105 proof (induct E)
   120   case Hole
   106   case Hole
   121   have "t \<longrightarrow>cbv t'" by fact
   107   have "t \<longrightarrow>cbv t'" by fact
   122   then show "\<box>\<lbrakk>t\<rbrakk> \<longrightarrow>cbv \<box>\<lbrakk>t'\<rbrakk>" by simp
   108   show "\<box>\<lbrakk>t\<rbrakk> \<longrightarrow>cbv \<box>\<lbrakk>t'\<rbrakk>" sorry
   123 next
   109 next
   124   case (CAppL E s)
   110   case (CAppL E s)
   125   have ih: "t \<longrightarrow>cbv t' \<Longrightarrow> E\<lbrakk>t\<rbrakk> \<longrightarrow>cbv E\<lbrakk>t'\<rbrakk>" by fact
   111   have ih: "t \<longrightarrow>cbv t' \<Longrightarrow> E\<lbrakk>t\<rbrakk> \<longrightarrow>cbv E\<lbrakk>t'\<rbrakk>" by fact
   126   moreover
       
   127   have "t \<longrightarrow>cbv t'" by fact
   112   have "t \<longrightarrow>cbv t'" by fact
   128   ultimately 
   113 
   129   have "E\<lbrakk>t\<rbrakk> \<longrightarrow>cbv E\<lbrakk>t'\<rbrakk>" by simp
   114   show "(CAppL E s)\<lbrakk>t\<rbrakk> \<longrightarrow>cbv (CAppL E s)\<lbrakk>t'\<rbrakk>" sorry
   130   then show "(CAppL E s)\<lbrakk>t\<rbrakk> \<longrightarrow>cbv (CAppL E s)\<lbrakk>t'\<rbrakk>" by auto
       
   131 next
   115 next
   132   case (CAppR s E)
   116   case (CAppR s E)
   133   have ih: "t \<longrightarrow>cbv t' \<Longrightarrow> E\<lbrakk>t\<rbrakk> \<longrightarrow>cbv E\<lbrakk>t'\<rbrakk>" by fact
   117   have ih: "t \<longrightarrow>cbv t' \<Longrightarrow> E\<lbrakk>t\<rbrakk> \<longrightarrow>cbv E\<lbrakk>t'\<rbrakk>" by fact
   134   moreover
       
   135   have a: "t \<longrightarrow>cbv t'" by fact
   118   have a: "t \<longrightarrow>cbv t'" by fact
   136   ultimately 
   119   
   137   have "E\<lbrakk>t\<rbrakk> \<longrightarrow>cbv E\<lbrakk>t'\<rbrakk>" by simp
   120   show "(CAppR s E)\<lbrakk>t\<rbrakk> \<longrightarrow>cbv (CAppR s E)\<lbrakk>t'\<rbrakk>" sorry
   138   then show "(CAppR s E)\<lbrakk>t\<rbrakk> \<longrightarrow>cbv (CAppR s E)\<lbrakk>t'\<rbrakk>" by auto
   121 qed
   139 qed
   122 
   140 
   123 section {* EXERCISE 13 *} 
   141 section {* EXERCISE 9 *} 
       
   142  
   124  
   143 text {*
   125 text {*
   144   The point of the cbv-reduction was that we can easily relatively 
   126   The point of the cbv-reduction was that we can easily relatively 
   145   establish the follwoing property:
   127   establish the follwoing property:
   146 *}
   128 *}
   165   show "(((CAppR (Lam [x].t) \<box>) # Es)\<down>)\<lbrakk>v\<rbrakk> \<longrightarrow>cbv* (Es\<down>)\<lbrakk>(t[x ::= v])\<rbrakk>" sorry
   147   show "(((CAppR (Lam [x].t) \<box>) # Es)\<down>)\<lbrakk>v\<rbrakk> \<longrightarrow>cbv* (Es\<down>)\<lbrakk>(t[x ::= v])\<rbrakk>" sorry
   166 qed
   148 qed
   167 
   149 
   168 text {* 
   150 text {* 
   169   It is not difficult to extend the lemma above to
   151   It is not difficult to extend the lemma above to
   170   arbitrary reductions sequences of the CK machine. *}
   152   arbitrary reductions sequences of the machine. 
       
   153 *}
   171 
   154 
   172 lemma machines_implies_cbvs_ctx:
   155 lemma machines_implies_cbvs_ctx:
   173   assumes a: "<e, Es> \<mapsto>* <e', Es'>"
   156   assumes a: "<e, Es> \<mapsto>* <e', Es'>"
   174   shows "(Es\<down>)\<lbrakk>e\<rbrakk> \<longrightarrow>cbv* (Es'\<down>)\<lbrakk>e'\<rbrakk>"
   157   shows "(Es\<down>)\<lbrakk>e\<rbrakk> \<longrightarrow>cbv* (Es'\<down>)\<lbrakk>e'\<rbrakk>"
   175 using a machine_implies_cbvs_ctx 
   158 using a machine_implies_cbvs_ctx 
   176 by (induct) (blast)+
   159 by (induct) (blast)+
   177 
   160 
   178 text {* 
   161 text {* 
   179   So whenever we let the CL machine start in an initial
   162   So whenever we let the machine start in an initial
   180   state and it arrives at a final state, then there exists
   163   state and it arrives at a final state, then there exists
   181   a corresponding cbv-reduction sequence. 
   164   a corresponding cbv-reduction sequence. 
   182 *}
   165 *}
   183 
   166 
   184 corollary machines_implies_cbvs:
   167 corollary machines_implies_cbvs:
   190   then show "e \<longrightarrow>cbv* e'" by simp  
   173   then show "e \<longrightarrow>cbv* e'" by simp  
   191 qed
   174 qed
   192 
   175 
   193 text {*
   176 text {*
   194   We now want to relate the cbv-reduction to the evaluation
   177   We now want to relate the cbv-reduction to the evaluation
   195   relation. For this we need two auxiliary lemmas. 
   178   relation. For this we need one auxiliary lemma about
   196 *}
   179   inverting the e_App rule. 
   197 
   180 *}
   198 lemma eval_val:
       
   199   assumes a: "val t"
       
   200   shows "t \<Down> t"
       
   201 using a by (induct) (auto)
       
   202 
   181 
   203 
   182 
   204 lemma e_App_elim:
   183 lemma e_App_elim:
   205   assumes a: "App t1 t2 \<Down> v"
   184   assumes a: "App t1 t2 \<Down> v"
   206   obtains x t v' where "t1 \<Down> Lam [x].t" "t2 \<Down> v'" "t[x::=v'] \<Down> v"
   185   obtains x t v' where "t1 \<Down> Lam [x].t" "t2 \<Down> v'" "t[x::=v'] \<Down> v"
   207 using a by (cases) (auto simp add: lam.eq_iff lam.distinct) 
   186 using a by (cases) (auto simp add: lam.eq_iff lam.distinct) 
   208 
   187 
   209 
   188 
   210 subsection {* EXERCISE *}
   189 subsection {* EXERCISE 13 *}
   211 
   190 
   212 text {*
   191 text {*
   213   Complete the first and second case in the 
   192   Complete the first and second case in the 
   214   proof below. 
   193   proof below. 
   215 *}
   194 *}
   220 using a
   199 using a
   221 proof(induct arbitrary: t3)
   200 proof(induct arbitrary: t3)
   222   case (cbv1 v x t t3)
   201   case (cbv1 v x t t3)
   223   have a1: "val v" by fact
   202   have a1: "val v" by fact
   224   have a2: "t[x ::= v] \<Down> t3" by fact
   203   have a2: "t[x ::= v] \<Down> t3" by fact
   225   have a3: "Lam [x].t \<Down> Lam [x].t" by auto
   204 
   226   have a4: "v \<Down> v" using a1 eval_val by auto
   205   show "App (Lam [x].t) v \<Down> t3" sorry
   227   show "App (Lam [x].t) v \<Down> t3" using a3 a4 a2 by auto 
       
   228 next
   206 next
   229   case (cbv2 t t' t2 t3)
   207   case (cbv2 t t' t2 t3)
   230   have ih: "\<And>t3. t' \<Down> t3 \<Longrightarrow> t \<Down> t3" by fact
   208   have ih: "\<And>t3. t' \<Down> t3 \<Longrightarrow> t \<Down> t3" by fact
   231   have "App t' t2 \<Down> t3" by fact
   209   have "App t' t2 \<Down> t3" by fact
   232   then obtain x t'' v' 
   210   then obtain x t'' v' 
   233     where a1: "t' \<Down> Lam [x].t''" 
   211     where a1: "t' \<Down> Lam [x].t''" 
   234       and a2: "t2 \<Down> v'" 
   212       and a2: "t2 \<Down> v'" 
   235       and a3: "t''[x ::= v'] \<Down> t3" by (rule e_App_elim) 
   213       and a3: "t''[x ::= v'] \<Down> t3" by (rule e_App_elim) 
   236   have "t \<Down>  Lam [x].t''" using ih a1 by auto 
   214   
   237   then show "App t t2 \<Down> t3" using a2 a3 by auto
   215   show "App t t2 \<Down> t3" sorry
   238 qed (auto elim!: e_App_elim)
   216 qed (auto elim!: e_App_elim)
   239 
   217 
   240 
   218 
   241 text {* 
   219 text {* 
   242   Next we extend the lemma above to arbitray initial
   220   Next we extend the lemma above to arbitray initial
   243   sequences of cbv-reductions. *}
   221   sequences of cbv-reductions. 
       
   222 *}
   244 
   223 
   245 lemma cbvs_eval:
   224 lemma cbvs_eval:
   246   assumes a: "t1 \<longrightarrow>cbv* t2" "t2 \<Down> t3"
   225   assumes a: "t1 \<longrightarrow>cbv* t2" "t2 \<Down> t3"
   247   shows "t1 \<Down> t3"
   226   shows "t1 \<Down> t3"
   248 using a by (induct) (auto intro: cbv_eval)
   227 using a by (induct) (auto intro: cbv_eval)
   249 
   228 
   250 text {* 
   229 text {* 
   251   Finally, we can show that if from a term t we reach a value 
   230   Finally, we can show that if from a term t we reach a value 
   252   by a cbv-reduction sequence, then t evaluates to this value. 
   231   by a cbv-reduction sequence, then t evaluates to this value. 
       
   232 
       
   233   This proof is not by induction. So we have to set up the proof
       
   234   with
       
   235 
       
   236     proof -
       
   237     
       
   238   in order to prevent Isabelle from applying a default introduction   
       
   239   rule.
   253 *}
   240 *}
   254 
   241 
   255 lemma cbvs_implies_eval:
   242 lemma cbvs_implies_eval:
   256   assumes a: "t \<longrightarrow>cbv* v" "val v"
   243   assumes a: "t \<longrightarrow>cbv* v" 
       
   244   and     b: "val v"
   257   shows "t \<Down> v"
   245   shows "t \<Down> v"
   258 using a
   246 proof - 
   259 by (induct) (auto intro: eval_val cbvs_eval)
   247   have "v \<Down> v" using b eval_val by simp
   260 
   248   then show "t \<Down> v" using a cbvs_eval by auto
   261 text {* 
   249 qed
   262   All facts tied together give us the desired property about
   250 
   263   machines. 
   251 section {* EXERCISE 15 *}
       
   252 
       
   253 text {* 
       
   254   All facts tied together give us the desired property 
       
   255   about machines: we know that a machines transitions
       
   256   correspond to cbvs transitions, and with the lemma
       
   257   above they correspond to an eval judgement.
   264 *}
   258 *}
   265 
   259 
   266 theorem machines_implies_eval:
   260 theorem machines_implies_eval:
   267   assumes a: "<t1, []> \<mapsto>* <t2, []>" 
   261   assumes a: "<t1, []> \<mapsto>* <t2, []>" 
   268   and     b: "val t2" 
   262   and     b: "val t2" 
   269   shows "t1 \<Down> t2"
   263   shows "t1 \<Down> t2"
   270 proof -
   264 proof - 
   271   have "t1 \<longrightarrow>cbv* t2" using a machines_implies_cbvs by simp
   265   
   272   then show "t1 \<Down> t2" using b cbvs_implies_eval by simp
   266   show "t1 \<Down> t2" sorry
   273 qed
   267 qed
   274 
       
   275 
       
   276 
       
   277 
   268 
   278 end
   269 end
   279 
   270