Tutorial/Tutorial4s.thy
author webertj
Tue, 26 Mar 2013 16:41:31 +0100
changeset 3213 a8724924a62e
parent 3132 87eca760dcba
permissions -rw-r--r--
Manual merge of d121bd2a5a47 from Isabelle/AFP.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3132
87eca760dcba updated tutorial to latest version and added it to the tests
Christian Urban <urbanc@in.tum.de>
parents: 2696
diff changeset
     1
theory Tutorial4s
87eca760dcba updated tutorial to latest version and added it to the tests
Christian Urban <urbanc@in.tum.de>
parents: 2696
diff changeset
     2
imports Tutorial1s
2696
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
     3
begin
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
     4
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
     5
section {* The CBV Reduction Relation (Small-Step Semantics) *}
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
     6
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
     7
text {*
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
     8
  In order to help establishing the property that the CK Machine
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
     9
  calculates a nomrmalform that corresponds to the evaluation 
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    10
  relation, we introduce the call-by-value small-step semantics.
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    11
*}
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    12
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    13
inductive
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    14
  cbv :: "lam \<Rightarrow> lam \<Rightarrow> bool" ("_ \<longrightarrow>cbv _" [60, 60] 60) 
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    15
where
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    16
  cbv1: "\<lbrakk>val v; atom x \<sharp> v\<rbrakk> \<Longrightarrow> App (Lam [x].t) v \<longrightarrow>cbv t[x ::= v]"
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    17
| cbv2[intro]: "t \<longrightarrow>cbv t' \<Longrightarrow> App t t2 \<longrightarrow>cbv App t' t2"
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    18
| cbv3[intro]: "t \<longrightarrow>cbv t' \<Longrightarrow> App t2 t \<longrightarrow>cbv App t2 t'"
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    19
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    20
equivariance val
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    21
equivariance cbv
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    22
nominal_inductive cbv
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    23
  avoids cbv1: "x"
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    24
  unfolding fresh_star_def
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    25
  by (simp_all add: lam.fresh Abs_fresh_iff fresh_Pair fresh_fact)
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    26
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    27
text {*
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    28
  In order to satisfy the vc-condition we have to formulate
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    29
  this relation with the additional freshness constraint
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    30
  atom x \<sharp> v. Although this makes the definition vc-ompatible, it
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    31
  makes the definition less useful. We can with a little bit of 
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    32
  pain show that the more restricted rule is equivalent to the
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    33
  usual rule. 
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    34
*}
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    35
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    36
lemma subst_rename: 
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    37
  assumes a: "atom y \<sharp> t"
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    38
  shows "t[x ::= s] = ((y \<leftrightarrow> x) \<bullet> t)[y ::= s]"
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    39
using a 
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    40
by (nominal_induct t avoiding: x y s rule: lam.strong_induct)
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    41
   (auto simp add: lam.fresh fresh_at_base)
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    42
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    43
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    44
lemma better_cbv1 [intro]: 
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    45
  assumes a: "val v" 
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    46
  shows "App (Lam [x].t) v \<longrightarrow>cbv t[x::=v]"
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    47
proof -
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    48
  obtain y::"name" where fs: "atom y \<sharp> (x, t, v)" by (rule obtain_fresh)
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    49
  have "App (Lam [x].t) v = App (Lam [y].((y \<leftrightarrow> x) \<bullet> t)) v" using fs
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    50
    by (auto simp add: lam.eq_iff Abs1_eq_iff' flip_def fresh_Pair fresh_at_base)
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    51
  also have "\<dots> \<longrightarrow>cbv ((y \<leftrightarrow> x) \<bullet> t)[y ::= v]" using fs a cbv1 by auto
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    52
  also have "\<dots> = t[x ::= v]" using fs subst_rename[symmetric] by simp
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    53
  finally show "App (Lam [x].t) v \<longrightarrow>cbv t[x ::= v]" by simp
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    54
qed
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    55
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    56
text {*
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    57
  The transitive closure of the cbv-reduction relation: 
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    58
*}
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    59
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    60
inductive 
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    61
  "cbvs" :: "lam \<Rightarrow> lam \<Rightarrow> bool" (" _ \<longrightarrow>cbv* _" [60, 60] 60)
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    62
where
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    63
  cbvs1[intro]: "e \<longrightarrow>cbv* e"
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    64
| cbvs2[intro]: "\<lbrakk>e1\<longrightarrow>cbv e2; e2 \<longrightarrow>cbv* e3\<rbrakk> \<Longrightarrow> e1 \<longrightarrow>cbv* e3"
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    65
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    66
lemma cbvs3 [intro]:
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    67
  assumes a: "e1 \<longrightarrow>cbv* e2" "e2 \<longrightarrow>cbv* e3"
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    68
  shows "e1 \<longrightarrow>cbv* e3"
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    69
using a by (induct) (auto) 
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    70
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    71
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    72
subsection {* EXERCISE 8 *}
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    73
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    74
text {*  
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    75
  If more simple exercises are needed, then complete the following proof. 
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    76
*}
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    77
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    78
lemma cbv_in_ctx:
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    79
  assumes a: "t \<longrightarrow>cbv t'"
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    80
  shows "E\<lbrakk>t\<rbrakk> \<longrightarrow>cbv E\<lbrakk>t'\<rbrakk>"
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    81
using a
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    82
proof (induct E)
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    83
  case Hole
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    84
  have "t \<longrightarrow>cbv t'" by fact
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    85
  then show "\<box>\<lbrakk>t\<rbrakk> \<longrightarrow>cbv \<box>\<lbrakk>t'\<rbrakk>" by simp
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    86
next
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    87
  case (CAppL E s)
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    88
  have ih: "t \<longrightarrow>cbv t' \<Longrightarrow> E\<lbrakk>t\<rbrakk> \<longrightarrow>cbv E\<lbrakk>t'\<rbrakk>" by fact
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    89
  moreover
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    90
  have "t \<longrightarrow>cbv t'" by fact
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    91
  ultimately 
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    92
  have "E\<lbrakk>t\<rbrakk> \<longrightarrow>cbv E\<lbrakk>t'\<rbrakk>" by simp
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    93
  then show "(CAppL E s)\<lbrakk>t\<rbrakk> \<longrightarrow>cbv (CAppL E s)\<lbrakk>t'\<rbrakk>" by auto
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    94
next
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    95
  case (CAppR s E)
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    96
  have ih: "t \<longrightarrow>cbv t' \<Longrightarrow> E\<lbrakk>t\<rbrakk> \<longrightarrow>cbv E\<lbrakk>t'\<rbrakk>" by fact
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    97
  moreover
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    98
  have a: "t \<longrightarrow>cbv t'" by fact
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    99
  ultimately 
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   100
  have "E\<lbrakk>t\<rbrakk> \<longrightarrow>cbv E\<lbrakk>t'\<rbrakk>" by simp
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   101
  then show "(CAppR s E)\<lbrakk>t\<rbrakk> \<longrightarrow>cbv (CAppR s E)\<lbrakk>t'\<rbrakk>" by auto
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   102
qed
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   103
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   104
section {* EXERCISE 9 *} 
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   105
 
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   106
text {*
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   107
  The point of the cbv-reduction was that we can easily relatively 
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   108
  establish the follwoing property:
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   109
*}
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   110
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   111
lemma machine_implies_cbvs_ctx:
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   112
  assumes a: "<e, Es> \<mapsto> <e', Es'>"
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   113
  shows "(Es\<down>)\<lbrakk>e\<rbrakk> \<longrightarrow>cbv* (Es'\<down>)\<lbrakk>e'\<rbrakk>"
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   114
using a 
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   115
proof (induct)
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   116
  case (m1 t1 t2 Es)
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   117
thm machine.intros thm cbv2
3132
87eca760dcba updated tutorial to latest version and added it to the tests
Christian Urban <urbanc@in.tum.de>
parents: 2696
diff changeset
   118
  have "Es\<down>\<lbrakk>App t1 t2\<rbrakk> = (Es\<down> \<odot> CAppL \<box> t2)\<lbrakk>t1\<rbrakk>" 
87eca760dcba updated tutorial to latest version and added it to the tests
Christian Urban <urbanc@in.tum.de>
parents: 2696
diff changeset
   119
    using ctx_compose ctx_composes.simps filling.simps by simp
2696
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   120
  then show "Es\<down>\<lbrakk>App t1 t2\<rbrakk> \<longrightarrow>cbv* ((CAppL \<box> t2) # Es)\<down>\<lbrakk>t1\<rbrakk>" using cbvs.intros by simp
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   121
next
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   122
  case (m2 v t2 Es)
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   123
  have "val v" by fact
3132
87eca760dcba updated tutorial to latest version and added it to the tests
Christian Urban <urbanc@in.tum.de>
parents: 2696
diff changeset
   124
  have "((CAppL \<box> t2) # Es)\<down>\<lbrakk>v\<rbrakk> = (CAppR v \<box> # Es)\<down>\<lbrakk>t2\<rbrakk>"  
87eca760dcba updated tutorial to latest version and added it to the tests
Christian Urban <urbanc@in.tum.de>
parents: 2696
diff changeset
   125
    using ctx_compose ctx_composes.simps filling.simps by simp
2696
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   126
  then show "((CAppL \<box> t2) # Es)\<down>\<lbrakk>v\<rbrakk> \<longrightarrow>cbv* (CAppR v \<box> # Es)\<down>\<lbrakk>t2\<rbrakk>" using cbvs.intros by simp
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   127
next
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   128
  case (m3 v x t Es)
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   129
  have aa: "val v" by fact
3132
87eca760dcba updated tutorial to latest version and added it to the tests
Christian Urban <urbanc@in.tum.de>
parents: 2696
diff changeset
   130
  have "(((CAppR (Lam [x].t) \<box>) # Es)\<down>)\<lbrakk>v\<rbrakk> = Es\<down>\<lbrakk>App (Lam [x]. t) v\<rbrakk>" 
87eca760dcba updated tutorial to latest version and added it to the tests
Christian Urban <urbanc@in.tum.de>
parents: 2696
diff changeset
   131
    using ctx_compose ctx_composes.simps filling.simps by simp
87eca760dcba updated tutorial to latest version and added it to the tests
Christian Urban <urbanc@in.tum.de>
parents: 2696
diff changeset
   132
  then have "(((CAppR (Lam [x].t) \<box>) # Es)\<down>)\<lbrakk>v\<rbrakk> \<longrightarrow>cbv (Es\<down>)\<lbrakk>(t[x ::= v])\<rbrakk>" 
87eca760dcba updated tutorial to latest version and added it to the tests
Christian Urban <urbanc@in.tum.de>
parents: 2696
diff changeset
   133
    using better_cbv1[OF aa] cbv_in_ctx by simp
2696
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   134
  then show "(((CAppR (Lam [x].t) \<box>) # Es)\<down>)\<lbrakk>v\<rbrakk> \<longrightarrow>cbv* (Es\<down>)\<lbrakk>(t[x ::= v])\<rbrakk>" using cbvs.intros by blast
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   135
qed
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   136
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   137
text {* 
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   138
  It is not difficult to extend the lemma above to
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   139
  arbitrary reductions sequences of the CK machine. *}
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   140
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   141
lemma machines_implies_cbvs_ctx:
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   142
  assumes a: "<e, Es> \<mapsto>* <e', Es'>"
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   143
  shows "(Es\<down>)\<lbrakk>e\<rbrakk> \<longrightarrow>cbv* (Es'\<down>)\<lbrakk>e'\<rbrakk>"
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   144
using a machine_implies_cbvs_ctx 
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   145
by (induct) (blast)+
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   146
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   147
text {* 
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   148
  So whenever we let the CL machine start in an initial
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   149
  state and it arrives at a final state, then there exists
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   150
  a corresponding cbv-reduction sequence. 
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   151
*}
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   152
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   153
corollary machines_implies_cbvs:
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   154
  assumes a: "<e, []> \<mapsto>* <e', []>"
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   155
  shows "e \<longrightarrow>cbv* e'"
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   156
proof - 
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   157
  have "[]\<down>\<lbrakk>e\<rbrakk> \<longrightarrow>cbv* []\<down>\<lbrakk>e'\<rbrakk>" 
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   158
     using a machines_implies_cbvs_ctx by blast
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   159
  then show "e \<longrightarrow>cbv* e'" by simp  
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   160
qed
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   161
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   162
text {*
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   163
  We now want to relate the cbv-reduction to the evaluation
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   164
  relation. For this we need two auxiliary lemmas. 
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   165
*}
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   166
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   167
lemma eval_val:
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   168
  assumes a: "val t"
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   169
  shows "t \<Down> t"
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   170
using a by (induct) (auto)
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   171
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   172
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   173
lemma e_App_elim:
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   174
  assumes a: "App t1 t2 \<Down> v"
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   175
  obtains x t v' where "t1 \<Down> Lam [x].t" "t2 \<Down> v'" "t[x::=v'] \<Down> v"
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   176
using a by (cases) (auto simp add: lam.eq_iff lam.distinct) 
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   177
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   178
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   179
subsection {* EXERCISE *}
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   180
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   181
text {*
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   182
  Complete the first and second case in the 
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   183
  proof below. 
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   184
*}
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   185
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   186
lemma cbv_eval:
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   187
  assumes a: "t1 \<longrightarrow>cbv t2" "t2 \<Down> t3"
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   188
  shows "t1 \<Down> t3"
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   189
using a
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   190
proof(induct arbitrary: t3)
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   191
  case (cbv1 v x t t3)
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   192
  have a1: "val v" by fact
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   193
  have a2: "t[x ::= v] \<Down> t3" by fact
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   194
  have a3: "Lam [x].t \<Down> Lam [x].t" by auto
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   195
  have a4: "v \<Down> v" using a1 eval_val by auto
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   196
  show "App (Lam [x].t) v \<Down> t3" using a3 a4 a2 by auto 
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   197
next
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   198
  case (cbv2 t t' t2 t3)
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   199
  have ih: "\<And>t3. t' \<Down> t3 \<Longrightarrow> t \<Down> t3" by fact
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   200
  have "App t' t2 \<Down> t3" by fact
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   201
  then obtain x t'' v' 
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   202
    where a1: "t' \<Down> Lam [x].t''" 
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   203
      and a2: "t2 \<Down> v'" 
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   204
      and a3: "t''[x ::= v'] \<Down> t3" by (rule e_App_elim) 
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   205
  have "t \<Down>  Lam [x].t''" using ih a1 by auto 
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   206
  then show "App t t2 \<Down> t3" using a2 a3 by auto
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   207
qed (auto elim!: e_App_elim)
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   208
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   209
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   210
text {* 
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   211
  Next we extend the lemma above to arbitray initial
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   212
  sequences of cbv-reductions. *}
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   213
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   214
lemma cbvs_eval:
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   215
  assumes a: "t1 \<longrightarrow>cbv* t2" "t2 \<Down> t3"
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   216
  shows "t1 \<Down> t3"
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   217
using a by (induct) (auto intro: cbv_eval)
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   218
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   219
text {* 
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   220
  Finally, we can show that if from a term t we reach a value 
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   221
  by a cbv-reduction sequence, then t evaluates to this value. 
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   222
*}
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   223
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   224
lemma cbvs_implies_eval:
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   225
  assumes a: "t \<longrightarrow>cbv* v" "val v"
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   226
  shows "t \<Down> v"
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   227
using a
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   228
by (induct) (auto intro: eval_val cbvs_eval)
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   229
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   230
text {* 
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   231
  All facts tied together give us the desired property about
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   232
  machines. 
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   233
*}
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   234
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   235
theorem machines_implies_eval:
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   236
  assumes a: "<t1, []> \<mapsto>* <t2, []>" 
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   237
  and     b: "val t2" 
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   238
  shows "t1 \<Down> t2"
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   239
proof -
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   240
  have "t1 \<longrightarrow>cbv* t2" using a machines_implies_cbvs by simp
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   241
  then show "t1 \<Down> t2" using b cbvs_implies_eval by simp
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   242
qed
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   243
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   244
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   245
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   246
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   247
end
af4fb03ecf32 Tutorial 4s
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   248