author | Christian Urban <urbanc@in.tum.de> |
Sat, 22 Jan 2011 16:04:40 -0600 | |
changeset 2695 | e8736c1cdd7f |
parent 2692 | da9bed7baf23 |
child 3132 | 87eca760dcba |
permissions | -rw-r--r-- |
2695
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
1 |
|
2689 | 2 |
theory Tutorial2 |
2695
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
3 |
imports Lambda |
2689 | 4 |
begin |
5 |
||
2695
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
6 |
section {* Height of a Lambda-Term *} |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
7 |
|
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
8 |
text {* |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
9 |
The theory Lambda defines the notions of capture-avoiding |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
10 |
substitution and the height of lambda terms. |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
11 |
*} |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
12 |
|
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
13 |
thm height.simps |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
14 |
thm subst.simps |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
15 |
|
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
16 |
subsection {* EXERCISE 7 *} |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
17 |
|
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
18 |
text {* |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
19 |
Lets prove a property about the height of substitutions. |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
20 |
|
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
21 |
Assume that the height of a lambda-term is always greater |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
22 |
or equal to 1. |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
23 |
*} |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
24 |
|
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
25 |
lemma height_ge_one: |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
26 |
shows "1 \<le> height t" |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
27 |
by (induct t rule: lam.induct) (auto) |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
28 |
|
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
29 |
text {* Remove the sorries *} |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
30 |
|
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
31 |
theorem height_subst: |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
32 |
shows "height (t[x ::= t']) \<le> height t - 1 + height t'" |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
33 |
proof (induct t rule: lam.induct) |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
34 |
case (Var y) |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
35 |
show "height (Var y[x ::= t']) \<le> height (Var y) - 1 + height t'" sorry |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
36 |
next |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
37 |
case (App t1 t2) |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
38 |
have ih1: "height (t1[x::=t']) \<le> (height t1) - 1 + height t'" by fact |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
39 |
have ih2: "height (t2[x::=t']) \<le> (height t2) - 1 + height t'" by fact |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
40 |
|
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
41 |
show "height ((App t1 t2)[x ::= t']) \<le> height (App t1 t2) - 1 + height t'" sorry |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
42 |
next |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
43 |
case (Lam y t1) |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
44 |
have ih: "height (t1[x::=t']) \<le> height t1 - 1 + height t'" by fact |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
45 |
-- {* the definition of capture-avoiding substitution *} |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
46 |
thm subst.simps |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
47 |
|
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
48 |
show "height ((Lam [y].t1)[x ::= t']) \<le> height (Lam [y].t1) - 1 + height t'" sorry |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
49 |
qed |
2689 | 50 |
|
51 |
||
2695
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
52 |
text {* |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
53 |
The point is that substitutions can only be moved under a binder |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
54 |
provided certain freshness conditions are satisfied. The structural |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
55 |
induction above does not say anything about such freshness conditions. |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
56 |
|
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
57 |
Fortunately, Nominal derives automatically some stronger induction |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
58 |
principle for lambda terms which has the usual variable convention |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
59 |
build in. |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
60 |
|
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
61 |
In the proof below, we use this stronger induction principle. The |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
62 |
variable and application case are as before. |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
63 |
*} |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
64 |
|
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
65 |
|
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
66 |
theorem |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
67 |
shows "height (t[x ::= t']) \<le> height t - 1 + height t'" |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
68 |
proof (nominal_induct t avoiding: x t' rule: lam.strong_induct) |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
69 |
case (Var y) |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
70 |
have "1 \<le> height t'" using height_ge_one by simp |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
71 |
then show "height (Var y[x ::= t']) \<le> height (Var y) - 1 + height t'" by simp |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
72 |
next |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
73 |
case (App t1 t2) |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
74 |
have ih1: "height (t1[x::=t']) \<le> (height t1) - 1 + height t'" |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
75 |
and ih2: "height (t2[x::=t']) \<le> (height t2) - 1 + height t'" by fact+ |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
76 |
then show "height ((App t1 t2)[x ::= t']) \<le> height (App t1 t2) - 1 + height t'" by simp |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
77 |
next |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
78 |
case (Lam y t1) |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
79 |
have ih: "height (t1[x::=t']) \<le> height t1 - 1 + height t'" by fact |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
80 |
have vc: "atom y \<sharp> x" "atom y \<sharp> t'" by fact+ -- {* usual variable convention *} |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
81 |
|
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
82 |
show "height ((Lam [y].t1)[x ::= t']) \<le> height (Lam [y].t1) - 1 + height t'" sorry |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
83 |
qed |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
84 |
|
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
85 |
|
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
86 |
section {* Types and the Weakening Lemma *} |
2689 | 87 |
|
88 |
nominal_datatype ty = |
|
89 |
tVar "string" |
|
2695
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
90 |
| tArr "ty" "ty" (infixr "\<rightarrow>" 100) |
2689 | 91 |
|
92 |
||
93 |
text {* |
|
94 |
Having defined them as nominal datatypes gives us additional |
|
95 |
definitions and theorems that come with types (see below). |
|
96 |
||
97 |
We next define the type of typing contexts, a predicate that |
|
98 |
defines valid contexts (i.e. lists that contain only unique |
|
99 |
variables) and the typing judgement. |
|
100 |
*} |
|
101 |
||
102 |
type_synonym ty_ctx = "(name \<times> ty) list" |
|
103 |
||
104 |
inductive |
|
105 |
valid :: "ty_ctx \<Rightarrow> bool" |
|
106 |
where |
|
107 |
v1[intro]: "valid []" |
|
108 |
| v2[intro]: "\<lbrakk>valid \<Gamma>; atom x \<sharp> \<Gamma>\<rbrakk>\<Longrightarrow> valid ((x, T) # \<Gamma>)" |
|
109 |
||
110 |
||
111 |
inductive |
|
112 |
typing :: "ty_ctx \<Rightarrow> lam \<Rightarrow> ty \<Rightarrow> bool" ("_ \<turnstile> _ : _" [60, 60, 60] 60) |
|
113 |
where |
|
114 |
t_Var[intro]: "\<lbrakk>valid \<Gamma>; (x, T) \<in> set \<Gamma>\<rbrakk> \<Longrightarrow> \<Gamma> \<turnstile> Var x : T" |
|
115 |
| t_App[intro]: "\<lbrakk>\<Gamma> \<turnstile> t1 : T1 \<rightarrow> T2; \<Gamma> \<turnstile> t2 : T1\<rbrakk> \<Longrightarrow> \<Gamma> \<turnstile> App t1 t2 : T2" |
|
116 |
| t_Lam[intro]: "\<lbrakk>atom x \<sharp> \<Gamma>; (x, T1) # \<Gamma> \<turnstile> t : T2\<rbrakk> \<Longrightarrow> \<Gamma> \<turnstile> Lam [x].t : T1 \<rightarrow> T2" |
|
117 |
||
118 |
||
119 |
text {* |
|
120 |
The predicate atom x \<sharp> \<Gamma>, read as x fresh for \<Gamma>, is defined by |
|
121 |
Nominal Isabelle. It is defined for lambda-terms, products, lists etc. |
|
122 |
For example we have: |
|
123 |
*} |
|
124 |
||
125 |
lemma |
|
126 |
fixes x::"name" |
|
127 |
shows "atom x \<sharp> Lam [x].t" |
|
128 |
and "atom x \<sharp> (t1, t2) \<Longrightarrow> atom x \<sharp> App t1 t2" |
|
129 |
and "atom x \<sharp> Var y \<Longrightarrow> atom x \<sharp> y" |
|
130 |
and "\<lbrakk>atom x \<sharp> t1; atom x \<sharp> t2\<rbrakk> \<Longrightarrow> atom x \<sharp> (t1, t2)" |
|
131 |
and "\<lbrakk>atom x \<sharp> l1; atom x \<sharp> l2\<rbrakk> \<Longrightarrow> atom x \<sharp> (l1 @ l2)" |
|
132 |
and "atom x \<sharp> y \<Longrightarrow> x \<noteq> y" |
|
133 |
by (simp_all add: lam.fresh fresh_append fresh_at_base) |
|
134 |
||
135 |
text {* |
|
136 |
We can also prove that every variable is fresh for a type. |
|
137 |
*} |
|
138 |
||
139 |
lemma ty_fresh: |
|
140 |
fixes x::"name" |
|
141 |
and T::"ty" |
|
142 |
shows "atom x \<sharp> T" |
|
143 |
by (induct T rule: ty.induct) |
|
144 |
(simp_all add: ty.fresh pure_fresh) |
|
145 |
||
146 |
text {* |
|
147 |
In order to state the weakening lemma in a convenient form, we |
|
148 |
using the following abbreviation. Abbreviations behave like |
|
149 |
definitions, except that they are always automatically folded |
|
150 |
and unfolded. |
|
151 |
*} |
|
152 |
||
153 |
abbreviation |
|
154 |
"sub_ty_ctx" :: "ty_ctx \<Rightarrow> ty_ctx \<Rightarrow> bool" ("_ \<sqsubseteq> _" [60, 60] 60) |
|
155 |
where |
|
156 |
"\<Gamma>1 \<sqsubseteq> \<Gamma>2 \<equiv> \<forall>x. x \<in> set \<Gamma>1 \<longrightarrow> x \<in> set \<Gamma>2" |
|
157 |
||
158 |
||
2695
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
159 |
subsection {* EXERCISE 8 *} |
2689 | 160 |
|
161 |
text {* |
|
162 |
Fill in the details and give a proof of the weakening lemma. |
|
163 |
*} |
|
164 |
||
165 |
lemma |
|
166 |
assumes a: "\<Gamma>1 \<turnstile> t : T" |
|
167 |
and b: "valid \<Gamma>2" |
|
168 |
and c: "\<Gamma>1 \<sqsubseteq> \<Gamma>2" |
|
169 |
shows "\<Gamma>2 \<turnstile> t : T" |
|
170 |
using a b c |
|
171 |
proof (induct arbitrary: \<Gamma>2) |
|
172 |
case (t_Var \<Gamma>1 x T) |
|
173 |
have a1: "valid \<Gamma>1" by fact |
|
174 |
have a2: "(x, T) \<in> set \<Gamma>1" by fact |
|
175 |
have a3: "valid \<Gamma>2" by fact |
|
176 |
have a4: "\<Gamma>1 \<sqsubseteq> \<Gamma>2" by fact |
|
177 |
||
178 |
show "\<Gamma>2 \<turnstile> Var x : T" sorry |
|
179 |
next |
|
180 |
case (t_Lam x \<Gamma>1 T1 t T2) |
|
181 |
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 |
|
182 |
have a0: "atom x \<sharp> \<Gamma>1" by fact |
|
183 |
have a1: "valid \<Gamma>2" by fact |
|
184 |
have a2: "\<Gamma>1 \<sqsubseteq> \<Gamma>2" by fact |
|
185 |
||
186 |
show "\<Gamma>2 \<turnstile> Lam [x].t : T1 \<rightarrow> T2" sorry |
|
2695
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
187 |
qed (auto) -- {* the application case is automatic*} |
2689 | 188 |
|
189 |
||
190 |
text {* |
|
191 |
Despite the frequent claim that the weakening lemma is trivial, |
|
192 |
routine or obvious, the proof in the lambda-case does not go |
|
193 |
through smoothly. Painful variable renamings seem to be necessary. |
|
194 |
But the proof using renamings is horribly complicated (see below). |
|
195 |
*} |
|
196 |
||
197 |
equivariance valid |
|
198 |
equivariance typing |
|
199 |
||
200 |
lemma weakening_NOT_TO_BE_TRIED_AT_HOME: |
|
201 |
assumes a: "\<Gamma>1 \<turnstile> t : T" |
|
202 |
and b: "valid \<Gamma>2" |
|
203 |
and c: "\<Gamma>1 \<sqsubseteq> \<Gamma>2" |
|
204 |
shows "\<Gamma>2 \<turnstile> t : T" |
|
205 |
using a b c |
|
206 |
proof (induct arbitrary: \<Gamma>2) |
|
207 |
-- {* lambda case *} |
|
208 |
case (t_Lam x \<Gamma>1 T1 t T2) |
|
209 |
have fc0: "atom x \<sharp> \<Gamma>1" by fact |
|
210 |
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 |
|
211 |
-- {* we choose a fresh variable *} |
|
212 |
obtain c::"name" where fc1: "atom c \<sharp> (x, t, \<Gamma>1, \<Gamma>2)" by (rule obtain_fresh) |
|
213 |
-- {* we alpha-rename the abstraction *} |
|
214 |
have "Lam [c].((c \<leftrightarrow> x) \<bullet> t) = Lam [x].t" using fc1 |
|
215 |
by (auto simp add: lam.eq_iff Abs1_eq_iff flip_def) |
|
216 |
moreover |
|
217 |
-- {* we can then alpha rename the goal *} |
|
218 |
have "\<Gamma>2 \<turnstile> Lam [c].((c \<leftrightarrow> x) \<bullet> t) : T1 \<rightarrow> T2" |
|
219 |
proof - |
|
220 |
-- {* we need to establish *} |
|
221 |
-- {* (1) (x, T1) # \<Gamma>1 \<sqsubseteq> (x, T1) # ((c \<leftrightarrow> x) \<bullet> \<Gamma>2) *} |
|
222 |
-- {* (2) valid ((x, T1) # ((c \<leftrightarrow> x) \<bullet> \<Gamma>2)) *} |
|
223 |
have "(1)": "(x, T1) # \<Gamma>1 \<sqsubseteq> (x, T1) # ((c \<leftrightarrow> x) \<bullet> \<Gamma>2)" |
|
224 |
proof - |
|
225 |
have "\<Gamma>1 \<sqsubseteq> \<Gamma>2" by fact |
|
226 |
then have "(c \<leftrightarrow> x) \<bullet> ((x, T1) # \<Gamma>1 \<sqsubseteq> (x, T1) # ((c \<leftrightarrow> x) \<bullet> \<Gamma>2))" using fc0 fc1 |
|
227 |
by (perm_simp) (simp add: flip_fresh_fresh) |
|
228 |
then show "(x, T1) # \<Gamma>1 \<sqsubseteq> (x, T1) # ((c \<leftrightarrow> x) \<bullet> \<Gamma>2)" by (rule permute_boolE) |
|
229 |
qed |
|
230 |
moreover |
|
231 |
have "(2)": "valid ((x, T1) # ((c \<leftrightarrow> x) \<bullet> \<Gamma>2))" |
|
232 |
proof - |
|
233 |
have "valid \<Gamma>2" by fact |
|
234 |
then show "valid ((x, T1) # ((c \<leftrightarrow> x) \<bullet> \<Gamma>2))" using fc1 |
|
235 |
by (auto simp add: fresh_permute_left atom_eqvt valid.eqvt) |
|
236 |
qed |
|
237 |
-- {* these two facts give us by induction hypothesis the following *} |
|
238 |
ultimately have "(x, T1) # ((c \<leftrightarrow> x) \<bullet> \<Gamma>2) \<turnstile> t : T2" using ih by simp |
|
239 |
-- {* we now apply renamings to get to our goal *} |
|
240 |
then have "(c \<leftrightarrow> x) \<bullet> ((x, T1) # ((c \<leftrightarrow> x) \<bullet> \<Gamma>2) \<turnstile> t : T2)" by (rule permute_boolI) |
|
241 |
then have "(c, T1) # \<Gamma>2 \<turnstile> ((c \<leftrightarrow> x) \<bullet> t) : T2" using fc1 |
|
242 |
by (perm_simp) (simp add: flip_fresh_fresh ty_fresh) |
|
243 |
then show "\<Gamma>2 \<turnstile> Lam [c].((c \<leftrightarrow> x) \<bullet> t) : T1 \<rightarrow> T2" using fc1 by auto |
|
244 |
qed |
|
245 |
ultimately show "\<Gamma>2 \<turnstile> Lam [x].t : T1 \<rightarrow> T2" by simp |
|
246 |
qed (auto) -- {* var and app cases, luckily, are automatic *} |
|
247 |
||
248 |
||
249 |
text {* |
|
2695
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
250 |
The remedy is to use again a stronger induction principle that |
2689 | 251 |
has the usual "variable convention" already build in. The |
2695
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
252 |
following command derives this induction principle for the typing |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
253 |
relation. (We shall explain what happens here later.) |
2689 | 254 |
*} |
255 |
||
256 |
nominal_inductive typing |
|
257 |
avoids t_Lam: "x" |
|
258 |
unfolding fresh_star_def |
|
259 |
by (simp_all add: fresh_Pair lam.fresh ty_fresh) |
|
260 |
||
261 |
text {* Compare the two induction principles *} |
|
262 |
||
263 |
thm typing.induct |
|
2695
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
264 |
thm typing.strong_induct -- {* note the additional assumption {atom x} \<sharp> c *} |
2689 | 265 |
|
266 |
text {* |
|
267 |
We can use the stronger induction principle by replacing |
|
268 |
the line |
|
269 |
||
270 |
proof (induct arbitrary: \<Gamma>2) |
|
271 |
||
272 |
with |
|
273 |
||
274 |
proof (nominal_induct avoiding: \<Gamma>2 rule: typing.strong_induct) |
|
275 |
||
276 |
Try now the proof. |
|
277 |
*} |
|
2695
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
278 |
|
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
279 |
subsection {* EXERCISE 9 *} |
2689 | 280 |
|
281 |
lemma weakening: |
|
282 |
assumes a: "\<Gamma>1 \<turnstile> t : T" |
|
283 |
and b: "valid \<Gamma>2" |
|
284 |
and c: "\<Gamma>1 \<sqsubseteq> \<Gamma>2" |
|
285 |
shows "\<Gamma>2 \<turnstile> t : T" |
|
286 |
using a b c |
|
287 |
proof (nominal_induct avoiding: \<Gamma>2 rule: typing.strong_induct) |
|
2695
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
288 |
case (t_Var \<Gamma>1 x T) -- {* again the variable case is as before *} |
2689 | 289 |
have "\<Gamma>1 \<sqsubseteq> \<Gamma>2" by fact |
290 |
moreover |
|
291 |
have "valid \<Gamma>2" by fact |
|
292 |
moreover |
|
293 |
have "(x, T)\<in> set \<Gamma>1" by fact |
|
294 |
ultimately show "\<Gamma>2 \<turnstile> Var x : T" by auto |
|
295 |
next |
|
296 |
case (t_Lam x \<Gamma>1 T1 t T2) |
|
297 |
have vc: "atom x \<sharp> \<Gamma>2" by fact -- {* additional fact afforded by the strong induction *} |
|
298 |
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 |
|
299 |
have a0: "atom x \<sharp> \<Gamma>1" by fact |
|
300 |
have a1: "valid \<Gamma>2" by fact |
|
301 |
have a2: "\<Gamma>1 \<sqsubseteq> \<Gamma>2" by fact |
|
2695
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
302 |
|
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
303 |
show "\<Gamma>2 \<turnstile> Lam [x].t : T1 \<rightarrow> T2" sorry |
2689 | 304 |
qed (auto) -- {* app case *} |
305 |
||
306 |
||
2695
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
307 |
|
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
308 |
section {* Unbind and an Inconsistency Example *} |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
309 |
|
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
310 |
text {* |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
311 |
You might wonder why we had to discharge some proof |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
312 |
obligations in order to obtain the stronger induction |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
313 |
principle for the typing relation. (Remember for |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
314 |
lambda terms this stronger induction principle is |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
315 |
derived automatically.) |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
316 |
|
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
317 |
This proof obligation is really needed, because if we |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
318 |
assume universally a stronger induction principle, then |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
319 |
in some cases we can derive false. This is "shown" below. |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
320 |
*} |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
321 |
|
2689 | 322 |
|
323 |
inductive |
|
2695
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
324 |
unbind :: "lam \<Rightarrow> lam \<Rightarrow> bool" (infixr "\<mapsto>" 60) |
2689 | 325 |
where |
326 |
u_Var[intro]: "Var x \<mapsto> Var x" |
|
327 |
| u_App[intro]: "App t1 t2 \<mapsto> App t1 t2" |
|
328 |
| u_Lam[intro]: "t \<mapsto> t' \<Longrightarrow> Lam [x].t \<mapsto> t'" |
|
329 |
||
2695
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
330 |
text {* It is clear that the following judgement holds. *} |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
331 |
|
2689 | 332 |
lemma unbind_simple: |
333 |
shows "Lam [x].Var x \<mapsto> Var x" |
|
334 |
by auto |
|
335 |
||
2695
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
336 |
text {* |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
337 |
Now lets derive the strong induction principle for unbind. |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
338 |
The point is that we cannot establish the proof obligations, |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
339 |
therefore we force Isabelle to accept them by using "sorry". |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
340 |
*} |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
341 |
|
2689 | 342 |
equivariance unbind |
343 |
nominal_inductive unbind |
|
344 |
avoids u_Lam: "x" |
|
345 |
sorry |
|
346 |
||
2695
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
347 |
text {* |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
348 |
Using the stronger induction principle, we can establish |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
349 |
th follwoing bogus property. |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
350 |
*} |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
351 |
|
2689 | 352 |
lemma unbind_fake: |
353 |
fixes y::"name" |
|
354 |
assumes a: "t \<mapsto> t'" |
|
355 |
and b: "atom y \<sharp> t" |
|
356 |
shows "atom y \<sharp> t'" |
|
357 |
using a b |
|
358 |
proof (nominal_induct avoiding: y rule: unbind.strong_induct) |
|
359 |
case (u_Lam t t' x y) |
|
360 |
have ih: "atom y \<sharp> t \<Longrightarrow> atom y \<sharp> t'" by fact |
|
361 |
have asm: "atom y \<sharp> Lam [x]. t" by fact |
|
362 |
have fc: "atom x \<sharp> y" by fact |
|
363 |
then have in_eq: "x \<noteq> y" by (simp add: fresh_at_base) |
|
364 |
then have "atom y \<sharp> t" using asm by (simp add: lam.fresh) |
|
365 |
then show "atom y \<sharp> t'" using ih by simp |
|
366 |
qed |
|
367 |
||
2695
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
368 |
text {* |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
369 |
And from this follows the inconsitency: |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
370 |
*} |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
371 |
|
2689 | 372 |
lemma |
373 |
shows "False" |
|
374 |
proof - |
|
375 |
have "atom x \<sharp> Lam [x]. Var x" by (simp add: lam.fresh) |
|
2695
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
376 |
moreover |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
377 |
have "Lam [x]. Var x \<mapsto> Var x" using unbind_simple by auto |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
378 |
ultimately |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
379 |
have "atom x \<sharp> Var x" using unbind_fake by auto |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
380 |
then have "x \<noteq> x" by (simp add: lam.fresh fresh_at_base) |
e8736c1cdd7f
cleaned up and solution section
Christian Urban <urbanc@in.tum.de>
parents:
2692
diff
changeset
|
381 |
then show "False" by simp |
2689 | 382 |
qed |
383 |
||
384 |
end |