2689
|
1 |
theory Tutorial2
|
|
2 |
imports Tutorial1
|
|
3 |
begin
|
|
4 |
|
|
5 |
(* fixme: put height example here *)
|
|
6 |
|
|
7 |
|
|
8 |
section {* Types *}
|
|
9 |
|
|
10 |
nominal_datatype ty =
|
|
11 |
tVar "string"
|
|
12 |
| tArr "ty" "ty" ("_ \<rightarrow> _" [100, 100] 100)
|
|
13 |
|
|
14 |
|
|
15 |
text {*
|
|
16 |
Having defined them as nominal datatypes gives us additional
|
|
17 |
definitions and theorems that come with types (see below).
|
|
18 |
|
|
19 |
We next define the type of typing contexts, a predicate that
|
|
20 |
defines valid contexts (i.e. lists that contain only unique
|
|
21 |
variables) and the typing judgement.
|
|
22 |
|
|
23 |
*}
|
|
24 |
|
|
25 |
type_synonym ty_ctx = "(name \<times> ty) list"
|
|
26 |
|
|
27 |
inductive
|
|
28 |
valid :: "ty_ctx \<Rightarrow> bool"
|
|
29 |
where
|
|
30 |
v1[intro]: "valid []"
|
|
31 |
| v2[intro]: "\<lbrakk>valid \<Gamma>; atom x \<sharp> \<Gamma>\<rbrakk>\<Longrightarrow> valid ((x, T) # \<Gamma>)"
|
|
32 |
|
|
33 |
|
|
34 |
inductive
|
|
35 |
typing :: "ty_ctx \<Rightarrow> lam \<Rightarrow> ty \<Rightarrow> bool" ("_ \<turnstile> _ : _" [60, 60, 60] 60)
|
|
36 |
where
|
|
37 |
t_Var[intro]: "\<lbrakk>valid \<Gamma>; (x, T) \<in> set \<Gamma>\<rbrakk> \<Longrightarrow> \<Gamma> \<turnstile> Var x : T"
|
|
38 |
| t_App[intro]: "\<lbrakk>\<Gamma> \<turnstile> t1 : T1 \<rightarrow> T2; \<Gamma> \<turnstile> t2 : T1\<rbrakk> \<Longrightarrow> \<Gamma> \<turnstile> App t1 t2 : T2"
|
|
39 |
| t_Lam[intro]: "\<lbrakk>atom x \<sharp> \<Gamma>; (x, T1) # \<Gamma> \<turnstile> t : T2\<rbrakk> \<Longrightarrow> \<Gamma> \<turnstile> Lam [x].t : T1 \<rightarrow> T2"
|
|
40 |
|
|
41 |
|
|
42 |
text {*
|
|
43 |
The predicate atom x \<sharp> \<Gamma>, read as x fresh for \<Gamma>, is defined by
|
|
44 |
Nominal Isabelle. It is defined for lambda-terms, products, lists etc.
|
|
45 |
For example we have:
|
|
46 |
*}
|
|
47 |
|
|
48 |
lemma
|
|
49 |
fixes x::"name"
|
|
50 |
shows "atom x \<sharp> Lam [x].t"
|
|
51 |
and "atom x \<sharp> (t1, t2) \<Longrightarrow> atom x \<sharp> App t1 t2"
|
|
52 |
and "atom x \<sharp> Var y \<Longrightarrow> atom x \<sharp> y"
|
|
53 |
and "\<lbrakk>atom x \<sharp> t1; atom x \<sharp> t2\<rbrakk> \<Longrightarrow> atom x \<sharp> (t1, t2)"
|
|
54 |
and "\<lbrakk>atom x \<sharp> l1; atom x \<sharp> l2\<rbrakk> \<Longrightarrow> atom x \<sharp> (l1 @ l2)"
|
|
55 |
and "atom x \<sharp> y \<Longrightarrow> x \<noteq> y"
|
|
56 |
by (simp_all add: lam.fresh fresh_append fresh_at_base)
|
|
57 |
|
|
58 |
text {*
|
|
59 |
We can also prove that every variable is fresh for a type.
|
|
60 |
*}
|
|
61 |
|
|
62 |
lemma ty_fresh:
|
|
63 |
fixes x::"name"
|
|
64 |
and T::"ty"
|
|
65 |
shows "atom x \<sharp> T"
|
|
66 |
by (induct T rule: ty.induct)
|
|
67 |
(simp_all add: ty.fresh pure_fresh)
|
|
68 |
|
|
69 |
text {*
|
|
70 |
In order to state the weakening lemma in a convenient form, we
|
|
71 |
using the following abbreviation. Abbreviations behave like
|
|
72 |
definitions, except that they are always automatically folded
|
|
73 |
and unfolded.
|
|
74 |
*}
|
|
75 |
|
|
76 |
abbreviation
|
|
77 |
"sub_ty_ctx" :: "ty_ctx \<Rightarrow> ty_ctx \<Rightarrow> bool" ("_ \<sqsubseteq> _" [60, 60] 60)
|
|
78 |
where
|
|
79 |
"\<Gamma>1 \<sqsubseteq> \<Gamma>2 \<equiv> \<forall>x. x \<in> set \<Gamma>1 \<longrightarrow> x \<in> set \<Gamma>2"
|
|
80 |
|
|
81 |
|
|
82 |
subsection {* EXERCISE 4 *}
|
|
83 |
|
|
84 |
text {*
|
|
85 |
Fill in the details and give a proof of the weakening lemma.
|
|
86 |
*}
|
|
87 |
|
|
88 |
lemma
|
|
89 |
assumes a: "\<Gamma>1 \<turnstile> t : T"
|
|
90 |
and b: "valid \<Gamma>2"
|
|
91 |
and c: "\<Gamma>1 \<sqsubseteq> \<Gamma>2"
|
|
92 |
shows "\<Gamma>2 \<turnstile> t : T"
|
|
93 |
using a b c
|
|
94 |
proof (induct arbitrary: \<Gamma>2)
|
|
95 |
case (t_Var \<Gamma>1 x T)
|
|
96 |
have a1: "valid \<Gamma>1" by fact
|
|
97 |
have a2: "(x, T) \<in> set \<Gamma>1" by fact
|
|
98 |
have a3: "valid \<Gamma>2" by fact
|
|
99 |
have a4: "\<Gamma>1 \<sqsubseteq> \<Gamma>2" by fact
|
|
100 |
|
|
101 |
show "\<Gamma>2 \<turnstile> Var x : T" sorry
|
|
102 |
next
|
|
103 |
case (t_Lam x \<Gamma>1 T1 t T2)
|
|
104 |
have ih: "\<And>\<Gamma>3. \<lbrakk>valid \<Gamma>3; (x, T1) # \<Gamma>1 \<sqsubseteq> \<Gamma>3\<rbrakk> \<Longrightarrow> \<Gamma>3 \<turnstile> t : T2" by fact
|
|
105 |
have a0: "atom x \<sharp> \<Gamma>1" by fact
|
|
106 |
have a1: "valid \<Gamma>2" by fact
|
|
107 |
have a2: "\<Gamma>1 \<sqsubseteq> \<Gamma>2" by fact
|
|
108 |
|
|
109 |
show "\<Gamma>2 \<turnstile> Lam [x].t : T1 \<rightarrow> T2" sorry
|
|
110 |
qed (auto) -- {* the application case *}
|
|
111 |
|
|
112 |
|
|
113 |
text {*
|
|
114 |
Despite the frequent claim that the weakening lemma is trivial,
|
|
115 |
routine or obvious, the proof in the lambda-case does not go
|
|
116 |
through smoothly. Painful variable renamings seem to be necessary.
|
|
117 |
But the proof using renamings is horribly complicated (see below).
|
|
118 |
*}
|
|
119 |
|
|
120 |
equivariance valid
|
|
121 |
equivariance typing
|
|
122 |
|
|
123 |
lemma weakening_NOT_TO_BE_TRIED_AT_HOME:
|
|
124 |
assumes a: "\<Gamma>1 \<turnstile> t : T"
|
|
125 |
and b: "valid \<Gamma>2"
|
|
126 |
and c: "\<Gamma>1 \<sqsubseteq> \<Gamma>2"
|
|
127 |
shows "\<Gamma>2 \<turnstile> t : T"
|
|
128 |
using a b c
|
|
129 |
proof (induct arbitrary: \<Gamma>2)
|
|
130 |
-- {* lambda case *}
|
|
131 |
case (t_Lam x \<Gamma>1 T1 t T2)
|
|
132 |
have fc0: "atom x \<sharp> \<Gamma>1" by fact
|
|
133 |
have ih: "\<And>\<Gamma>3. \<lbrakk>valid \<Gamma>3; (x, T1) # \<Gamma>1 \<sqsubseteq> \<Gamma>3\<rbrakk> \<Longrightarrow> \<Gamma>3 \<turnstile> t : T2" by fact
|
|
134 |
-- {* we choose a fresh variable *}
|
|
135 |
obtain c::"name" where fc1: "atom c \<sharp> (x, t, \<Gamma>1, \<Gamma>2)" by (rule obtain_fresh)
|
|
136 |
-- {* we alpha-rename the abstraction *}
|
|
137 |
have "Lam [c].((c \<leftrightarrow> x) \<bullet> t) = Lam [x].t" using fc1
|
|
138 |
by (auto simp add: lam.eq_iff Abs1_eq_iff flip_def)
|
|
139 |
moreover
|
|
140 |
-- {* we can then alpha rename the goal *}
|
|
141 |
have "\<Gamma>2 \<turnstile> Lam [c].((c \<leftrightarrow> x) \<bullet> t) : T1 \<rightarrow> T2"
|
|
142 |
proof -
|
|
143 |
-- {* we need to establish *}
|
|
144 |
-- {* (1) (x, T1) # \<Gamma>1 \<sqsubseteq> (x, T1) # ((c \<leftrightarrow> x) \<bullet> \<Gamma>2) *}
|
|
145 |
-- {* (2) valid ((x, T1) # ((c \<leftrightarrow> x) \<bullet> \<Gamma>2)) *}
|
|
146 |
have "(1)": "(x, T1) # \<Gamma>1 \<sqsubseteq> (x, T1) # ((c \<leftrightarrow> x) \<bullet> \<Gamma>2)"
|
|
147 |
proof -
|
|
148 |
have "\<Gamma>1 \<sqsubseteq> \<Gamma>2" by fact
|
|
149 |
then have "(c \<leftrightarrow> x) \<bullet> ((x, T1) # \<Gamma>1 \<sqsubseteq> (x, T1) # ((c \<leftrightarrow> x) \<bullet> \<Gamma>2))" using fc0 fc1
|
|
150 |
by (perm_simp) (simp add: flip_fresh_fresh)
|
|
151 |
then show "(x, T1) # \<Gamma>1 \<sqsubseteq> (x, T1) # ((c \<leftrightarrow> x) \<bullet> \<Gamma>2)" by (rule permute_boolE)
|
|
152 |
qed
|
|
153 |
moreover
|
|
154 |
have "(2)": "valid ((x, T1) # ((c \<leftrightarrow> x) \<bullet> \<Gamma>2))"
|
|
155 |
proof -
|
|
156 |
have "valid \<Gamma>2" by fact
|
|
157 |
then show "valid ((x, T1) # ((c \<leftrightarrow> x) \<bullet> \<Gamma>2))" using fc1
|
|
158 |
by (auto simp add: fresh_permute_left atom_eqvt valid.eqvt)
|
|
159 |
qed
|
|
160 |
-- {* these two facts give us by induction hypothesis the following *}
|
|
161 |
ultimately have "(x, T1) # ((c \<leftrightarrow> x) \<bullet> \<Gamma>2) \<turnstile> t : T2" using ih by simp
|
|
162 |
-- {* we now apply renamings to get to our goal *}
|
|
163 |
then have "(c \<leftrightarrow> x) \<bullet> ((x, T1) # ((c \<leftrightarrow> x) \<bullet> \<Gamma>2) \<turnstile> t : T2)" by (rule permute_boolI)
|
|
164 |
then have "(c, T1) # \<Gamma>2 \<turnstile> ((c \<leftrightarrow> x) \<bullet> t) : T2" using fc1
|
|
165 |
by (perm_simp) (simp add: flip_fresh_fresh ty_fresh)
|
|
166 |
then show "\<Gamma>2 \<turnstile> Lam [c].((c \<leftrightarrow> x) \<bullet> t) : T1 \<rightarrow> T2" using fc1 by auto
|
|
167 |
qed
|
|
168 |
ultimately show "\<Gamma>2 \<turnstile> Lam [x].t : T1 \<rightarrow> T2" by simp
|
|
169 |
qed (auto) -- {* var and app cases, luckily, are automatic *}
|
|
170 |
|
|
171 |
|
|
172 |
text {*
|
|
173 |
The remedy is to use a stronger induction principle that
|
|
174 |
has the usual "variable convention" already build in. The
|
|
175 |
following command derives this induction principle for us.
|
|
176 |
(We shall explain what happens here later.)
|
|
177 |
*}
|
|
178 |
|
|
179 |
nominal_inductive typing
|
|
180 |
avoids t_Lam: "x"
|
|
181 |
unfolding fresh_star_def
|
|
182 |
by (simp_all add: fresh_Pair lam.fresh ty_fresh)
|
|
183 |
|
|
184 |
text {* Compare the two induction principles *}
|
|
185 |
|
|
186 |
thm typing.induct
|
|
187 |
thm typing.strong_induct -- {* has the additional assumption {atom x} \<sharp> c *}
|
|
188 |
|
|
189 |
text {*
|
|
190 |
We can use the stronger induction principle by replacing
|
|
191 |
the line
|
|
192 |
|
|
193 |
proof (induct arbitrary: \<Gamma>2)
|
|
194 |
|
|
195 |
with
|
|
196 |
|
|
197 |
proof (nominal_induct avoiding: \<Gamma>2 rule: typing.strong_induct)
|
|
198 |
|
|
199 |
Try now the proof.
|
|
200 |
*}
|
|
201 |
|
|
202 |
|
|
203 |
lemma weakening:
|
|
204 |
assumes a: "\<Gamma>1 \<turnstile> t : T"
|
|
205 |
and b: "valid \<Gamma>2"
|
|
206 |
and c: "\<Gamma>1 \<sqsubseteq> \<Gamma>2"
|
|
207 |
shows "\<Gamma>2 \<turnstile> t : T"
|
|
208 |
using a b c
|
|
209 |
proof (nominal_induct avoiding: \<Gamma>2 rule: typing.strong_induct)
|
|
210 |
case (t_Var \<Gamma>1 x T) -- {* variable case is as before *}
|
|
211 |
have "\<Gamma>1 \<sqsubseteq> \<Gamma>2" by fact
|
|
212 |
moreover
|
|
213 |
have "valid \<Gamma>2" by fact
|
|
214 |
moreover
|
|
215 |
have "(x, T)\<in> set \<Gamma>1" by fact
|
|
216 |
ultimately show "\<Gamma>2 \<turnstile> Var x : T" by auto
|
|
217 |
next
|
|
218 |
case (t_Lam x \<Gamma>1 T1 t T2)
|
|
219 |
have vc: "atom x \<sharp> \<Gamma>2" by fact -- {* additional fact afforded by the strong induction *}
|
|
220 |
have ih: "\<And>\<Gamma>3. \<lbrakk>valid \<Gamma>3; (x, T1) # \<Gamma>1 \<sqsubseteq> \<Gamma>3\<rbrakk> \<Longrightarrow> \<Gamma>3 \<turnstile> t : T2" by fact
|
|
221 |
have a0: "atom x \<sharp> \<Gamma>1" by fact
|
|
222 |
have a1: "valid \<Gamma>2" by fact
|
|
223 |
have a2: "\<Gamma>1 \<sqsubseteq> \<Gamma>2" by fact
|
|
224 |
have "valid ((x, T1) # \<Gamma>2)" using a1 vc by auto
|
|
225 |
moreover
|
|
226 |
have "(x, T1) # \<Gamma>1 \<sqsubseteq> (x, T1) # \<Gamma>2" using a2 by auto
|
|
227 |
ultimately
|
|
228 |
have "(x, T1) # \<Gamma>2 \<turnstile> t : T2" using ih by simp
|
|
229 |
then show "\<Gamma>2 \<turnstile> Lam [x].t : T1 \<rightarrow> T2" using vc by auto
|
|
230 |
qed (auto) -- {* app case *}
|
|
231 |
|
|
232 |
|
|
233 |
text {* unbind / inconsistency example *}
|
|
234 |
|
|
235 |
inductive
|
|
236 |
unbind :: "lam \<Rightarrow> lam \<Rightarrow> bool" ("_ \<mapsto> _" [60, 60] 60)
|
|
237 |
where
|
|
238 |
u_Var[intro]: "Var x \<mapsto> Var x"
|
|
239 |
| u_App[intro]: "App t1 t2 \<mapsto> App t1 t2"
|
|
240 |
| u_Lam[intro]: "t \<mapsto> t' \<Longrightarrow> Lam [x].t \<mapsto> t'"
|
|
241 |
|
|
242 |
lemma unbind_simple:
|
|
243 |
shows "Lam [x].Var x \<mapsto> Var x"
|
|
244 |
by auto
|
|
245 |
|
|
246 |
equivariance unbind
|
|
247 |
|
|
248 |
nominal_inductive unbind
|
|
249 |
avoids u_Lam: "x"
|
|
250 |
sorry
|
|
251 |
|
|
252 |
lemma unbind_fake:
|
|
253 |
fixes y::"name"
|
|
254 |
assumes a: "t \<mapsto> t'"
|
|
255 |
and b: "atom y \<sharp> t"
|
|
256 |
shows "atom y \<sharp> t'"
|
|
257 |
using a b
|
|
258 |
proof (nominal_induct avoiding: y rule: unbind.strong_induct)
|
|
259 |
case (u_Lam t t' x y)
|
|
260 |
have ih: "atom y \<sharp> t \<Longrightarrow> atom y \<sharp> t'" by fact
|
|
261 |
have asm: "atom y \<sharp> Lam [x]. t" by fact
|
|
262 |
have fc: "atom x \<sharp> y" by fact
|
|
263 |
then have in_eq: "x \<noteq> y" by (simp add: fresh_at_base)
|
|
264 |
then have "atom y \<sharp> t" using asm by (simp add: lam.fresh)
|
|
265 |
then show "atom y \<sharp> t'" using ih by simp
|
|
266 |
qed
|
|
267 |
|
|
268 |
lemma
|
|
269 |
shows "False"
|
|
270 |
proof -
|
|
271 |
have "atom x \<sharp> Lam [x]. Var x" by (simp add: lam.fresh)
|
|
272 |
then have "atom x \<sharp> Var x" using unbind_fake unbind_simple by auto
|
|
273 |
then show "False" by (simp add: lam.fresh fresh_at_base)
|
|
274 |
qed
|
|
275 |
|
|
276 |
end
|