author | urbanc |
Fri, 02 May 2008 02:16:10 +0200 | |
changeset 26763 | fba4995cb0f9 |
parent 25867 | c24395ea4e71 |
child 26926 | 19d8783a30de |
permissions | -rw-r--r-- |
25722 | 1 |
(* $Id$ *) |
25499
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
2 |
|
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
3 |
theory Contexts |
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
4 |
imports "../Nominal" |
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
5 |
begin |
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
6 |
|
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
7 |
text {* |
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
8 |
|
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
9 |
We show here that the Plotkin-style of defining |
26763 | 10 |
beta-reduction (based on congruence rules) is |
25499
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
11 |
equivalent to the Felleisen-Hieb-style representation |
26763 | 12 |
(based on contexts). We also define cbv-evaluation |
13 |
via a CK-machine described by Roshan James. |
|
25499
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
14 |
|
26763 | 15 |
The interesting point in this theory is that contexts |
16 |
do not contain any binders. On the other hand the operation |
|
17 |
of filling a term into a context produces an alpha-equivalent |
|
18 |
term. |
|
25499
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
19 |
|
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
20 |
*} |
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
21 |
|
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
22 |
atom_decl name |
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
23 |
|
25867 | 24 |
text {* |
25 |
Lambda-Terms - the Lam-term constructor binds a name |
|
26 |
*} |
|
25499
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
27 |
nominal_datatype lam = |
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
28 |
Var "name" |
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
29 |
| App "lam" "lam" |
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
30 |
| Lam "\<guillemotleft>name\<guillemotright>lam" ("Lam [_]._" [100,100] 100) |
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
31 |
|
25751 | 32 |
text {* |
26763 | 33 |
Contexts - the lambda case in contexts does not bind |
34 |
a name, even if we introduce the notation [_]._ for CLam. |
|
25751 | 35 |
*} |
26763 | 36 |
|
25499
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
37 |
nominal_datatype ctx = |
25858 | 38 |
Hole ("\<box>" 1000) |
25499
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
39 |
| CAppL "ctx" "lam" |
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
40 |
| CAppR "lam" "ctx" |
25722 | 41 |
| CLam "name" "ctx" ("CLam [_]._" [100,100] 100) |
25499
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
42 |
|
25867 | 43 |
text {* Capture-avoiding substitution *} |
25499
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
44 |
|
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
45 |
consts subst :: "lam \<Rightarrow> name \<Rightarrow> lam \<Rightarrow> lam" ("_[_::=_]" [100,100,100] 100) |
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
46 |
|
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
47 |
nominal_primrec |
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
48 |
"(Var x)[y::=s] = (if x=y then s else (Var x))" |
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
49 |
"(App t\<^isub>1 t\<^isub>2)[y::=s] = App (t\<^isub>1[y::=s]) (t\<^isub>2[y::=s])" |
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
50 |
"x\<sharp>(y,s) \<Longrightarrow> (Lam [x].t)[y::=s] = Lam [x].(t[y::=s])" |
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
51 |
apply(finite_guess)+ |
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
52 |
apply(rule TrueI)+ |
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
53 |
apply(simp add: abs_fresh) |
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
54 |
apply(fresh_guess)+ |
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
55 |
done |
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
56 |
|
25867 | 57 |
text {* |
58 |
Filling is the operation that fills a term into a hole. |
|
59 |
This operation is possibly capturing. |
|
60 |
*} |
|
25499
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
61 |
|
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
62 |
consts |
26763 | 63 |
filling :: "ctx \<Rightarrow> lam \<Rightarrow> lam" ("_\<lbrakk>_\<rbrakk>" [100,100] 100) |
25499
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
64 |
|
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
65 |
nominal_primrec |
26763 | 66 |
"\<box>\<lbrakk>t\<rbrakk> = t" |
67 |
"(CAppL E t')\<lbrakk>t\<rbrakk> = App (E\<lbrakk>t\<rbrakk>) t'" |
|
68 |
"(CAppR t' E)\<lbrakk>t\<rbrakk> = App t' (E\<lbrakk>t\<rbrakk>)" |
|
69 |
"(CLam [x].E)\<lbrakk>t\<rbrakk> = Lam [x].(E\<lbrakk>t\<rbrakk>)" |
|
25499
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
70 |
by (rule TrueI)+ |
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
71 |
|
25858 | 72 |
text {* |
73 |
While contexts themselves are not alpha-equivalence classes, the |
|
74 |
filling operation produces an alpha-equivalent lambda-term. |
|
75 |
*} |
|
26763 | 76 |
|
25499
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
77 |
lemma alpha_test: |
25858 | 78 |
shows "x\<noteq>y \<Longrightarrow> (CLam [x].\<box>) \<noteq> (CLam [y].\<box>)" |
26763 | 79 |
and "(CLam [x].\<box>)\<lbrakk>Var x\<rbrakk> = (CLam [y].\<box>)\<lbrakk>Var y\<rbrakk>" |
25858 | 80 |
by (auto simp add: alpha ctx.inject lam.inject calc_atm fresh_atm) |
25722 | 81 |
|
25867 | 82 |
text {* The composition of two contexts. *} |
25499
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
83 |
|
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
84 |
consts |
25867 | 85 |
ctx_compose :: "ctx \<Rightarrow> ctx \<Rightarrow> ctx" ("_ \<circ> _" [100,100] 100) |
25499
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
86 |
|
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
87 |
nominal_primrec |
25858 | 88 |
"\<box> \<circ> E' = E'" |
25499
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
89 |
"(CAppL E t') \<circ> E' = CAppL (E \<circ> E') t'" |
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
90 |
"(CAppR t' E) \<circ> E' = CAppR t' (E \<circ> E')" |
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
91 |
"(CLam [x].E) \<circ> E' = CLam [x].(E \<circ> E')" |
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
92 |
by (rule TrueI)+ |
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
93 |
|
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
94 |
lemma ctx_compose: |
26763 | 95 |
shows "(E1 \<circ> E2)\<lbrakk>t\<rbrakk> = E1\<lbrakk>E2\<lbrakk>t\<rbrakk>\<rbrakk>" |
25499
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
96 |
by (induct E1 rule: ctx.weak_induct) (auto) |
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
97 |
|
25751 | 98 |
text {* Beta-reduction via contexts in the Felleisen-Hieb style. *} |
25499
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
99 |
|
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
100 |
inductive |
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
101 |
ctx_red :: "lam\<Rightarrow>lam\<Rightarrow>bool" ("_ \<longrightarrow>x _" [80,80] 80) |
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
102 |
where |
26763 | 103 |
xbeta[intro]: "E\<lbrakk>App (Lam [x].t) t'\<rbrakk> \<longrightarrow>x E\<lbrakk>t[x::=t']\<rbrakk>" |
25499
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
104 |
|
25751 | 105 |
text {* Beta-reduction via congruence rules in the Plotkin style. *} |
25499
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
106 |
|
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
107 |
inductive |
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
108 |
cong_red :: "lam\<Rightarrow>lam\<Rightarrow>bool" ("_ \<longrightarrow>o _" [80,80] 80) |
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
109 |
where |
25858 | 110 |
obeta[intro]: "App (Lam [x].t) t' \<longrightarrow>o t[x::=t']" |
25499
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
111 |
| oapp1[intro]: "t \<longrightarrow>o t' \<Longrightarrow> App t t2 \<longrightarrow>o App t' t2" |
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
112 |
| oapp2[intro]: "t \<longrightarrow>o t' \<Longrightarrow> App t2 t \<longrightarrow>o App t2 t'" |
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
113 |
| olam[intro]: "t \<longrightarrow>o t' \<Longrightarrow> Lam [x].t \<longrightarrow>o Lam [x].t'" |
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
114 |
|
25751 | 115 |
text {* The proof that shows both relations are equal. *} |
25499
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
116 |
|
25858 | 117 |
lemma cong_red_in_ctx: |
25499
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
118 |
assumes a: "t \<longrightarrow>o t'" |
26763 | 119 |
shows "E\<lbrakk>t\<rbrakk> \<longrightarrow>o E\<lbrakk>t'\<rbrakk>" |
25499
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
120 |
using a |
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
121 |
by (induct E rule: ctx.weak_induct) (auto) |
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
122 |
|
25858 | 123 |
lemma ctx_red_in_ctx: |
25499
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
124 |
assumes a: "t \<longrightarrow>x t'" |
26763 | 125 |
shows "E\<lbrakk>t\<rbrakk> \<longrightarrow>x E\<lbrakk>t'\<rbrakk>" |
126 |
using a |
|
127 |
by (induct) (auto simp add: ctx_compose[symmetric]) |
|
25499
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
128 |
|
25858 | 129 |
theorem ctx_red_implies_cong_red: |
25499
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
130 |
assumes a: "t \<longrightarrow>x t'" |
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
131 |
shows "t \<longrightarrow>o t'" |
26763 | 132 |
using a by (induct) (auto intro: cong_red_in_ctx) |
25499
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
133 |
|
25858 | 134 |
theorem cong_red_implies_ctx_red: |
25499
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
135 |
assumes a: "t \<longrightarrow>o t'" |
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
136 |
shows "t \<longrightarrow>x t'" |
25858 | 137 |
using a |
138 |
proof (induct) |
|
139 |
case (obeta x t' t) |
|
26763 | 140 |
have "\<box>\<lbrakk>App (Lam [x].t) t'\<rbrakk> \<longrightarrow>x \<box>\<lbrakk>t[x::=t']\<rbrakk>" by (rule xbeta) |
25858 | 141 |
then show "App (Lam [x].t) t' \<longrightarrow>x t[x::=t']" by simp |
142 |
qed (metis ctx_red_in_ctx filling.simps)+ (* found by SledgeHammer *) |
|
143 |
||
144 |
||
26763 | 145 |
section {* We now use context in a CBV list machine. *} |
146 |
||
147 |
text {* First, a function that composes a list of contexts. *} |
|
25858 | 148 |
|
149 |
primrec |
|
25867 | 150 |
ctx_composes :: "ctx list \<Rightarrow> ctx" ("_\<down>" [80] 80) |
151 |
where |
|
152 |
"[]\<down> = \<box>" |
|
153 |
| "(E#Es)\<down> = (Es\<down>) \<circ> E" |
|
25858 | 154 |
|
155 |
text {* Values *} |
|
26763 | 156 |
|
25858 | 157 |
inductive |
158 |
val :: "lam\<Rightarrow>bool" |
|
159 |
where |
|
160 |
v_Lam[intro]: "val (Lam [x].e)" |
|
161 |
| v_Var[intro]: "val (Var x)" |
|
162 |
||
25867 | 163 |
text {* CBV-reduction using contexts *} |
26763 | 164 |
|
25858 | 165 |
inductive |
166 |
cbv :: "lam\<Rightarrow>lam\<Rightarrow>bool" ("_ \<longrightarrow>cbv _" [80,80] 80) |
|
167 |
where |
|
26763 | 168 |
cbv_beta[intro]: "val v \<Longrightarrow> E\<lbrakk>App (Lam [x].e) v\<rbrakk> \<longrightarrow>cbv E\<lbrakk>e[x::=v]\<rbrakk>" |
25858 | 169 |
|
170 |
text {* reflexive, transitive closure of CBV reduction *} |
|
171 |
inductive |
|
172 |
"cbv_star" :: "lam\<Rightarrow>lam\<Rightarrow>bool" (" _ \<longrightarrow>cbv* _" [80,80] 80) |
|
173 |
where |
|
174 |
cbvs1[intro]: "e \<longrightarrow>cbv* e" |
|
175 |
| cbvs2[intro]: "e \<longrightarrow>cbv e' \<Longrightarrow> e \<longrightarrow>cbv* e'" |
|
176 |
| cbvs3[intro,trans]: "\<lbrakk>e1\<longrightarrow>cbv* e2; e2 \<longrightarrow>cbv* e3\<rbrakk> \<Longrightarrow> e1 \<longrightarrow>cbv* e3" |
|
177 |
||
25867 | 178 |
text {* A little CK-machine, which builds up a list of evaluation contexts. *} |
26763 | 179 |
|
25858 | 180 |
inductive |
181 |
machine :: "lam\<Rightarrow>ctx list\<Rightarrow>lam\<Rightarrow>ctx list\<Rightarrow>bool" ("<_,_> \<mapsto> <_,_>" [60,60,60,60] 60) |
|
182 |
where |
|
183 |
m1[intro]: "<App e1 e2, Es> \<mapsto> <e1,(CAppL \<box> e2)#Es>" |
|
184 |
| m2[intro]: "val v \<Longrightarrow> <v,(CAppL \<box> e2)#Es> \<mapsto> <e2,(CAppR v \<box>)#Es>" |
|
185 |
| m3[intro]: "val v \<Longrightarrow> <v,(CAppR (Lam [x].e) \<box>)#Es> \<mapsto> <e[x::=v],Es>" |
|
186 |
||
187 |
lemma machine_red_implies_cbv_reds_aux: |
|
188 |
assumes a: "<e,Es> \<mapsto> <e',Es'>" |
|
26763 | 189 |
shows "(Es\<down>)\<lbrakk>e\<rbrakk> \<longrightarrow>cbv* (Es'\<down>)\<lbrakk>e'\<rbrakk>" |
190 |
using a by (induct) (auto simp add: ctx_compose) |
|
25858 | 191 |
|
192 |
lemma machine_execution_implies_cbv_reds: |
|
193 |
assumes a: "<e,[]> \<mapsto> <e',[]>" |
|
194 |
shows "e \<longrightarrow>cbv* e'" |
|
26763 | 195 |
using a by (auto dest: machine_red_implies_cbv_reds_aux) |
25499
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
196 |
|
25867 | 197 |
text {* |
198 |
One would now like to show something about the opposite |
|
199 |
direction, by according to Amr Sabry this amounts to |
|
200 |
showing a standardisation lemma, which is hard. |
|
201 |
*} |
|
202 |
||
25499
7e0ad4838ce9
an example file for how to treat Felleisen-Hieb-style contexts
urbanc
parents:
diff
changeset
|
203 |
end |