author | wenzelm |
Wed, 29 Aug 2007 11:10:28 +0200 | |
changeset 24470 | 41c81e23c08d |
parent 23880 | 64b9806e160b |
child 24742 | 73b8b42a36b6 |
permissions | -rw-r--r-- |
12944 | 1 |
(* Title: HOL/W0/W0.thy |
2 |
ID: $Id$ |
|
12945 | 3 |
Author: Dieter Nazareth, Tobias Nipkow, Thomas Stauner, Markus Wenzel |
12944 | 4 |
*) |
5 |
||
15140 | 6 |
theory W0 |
7 |
imports Main |
|
8 |
begin |
|
12944 | 9 |
|
10 |
section {* Universal error monad *} |
|
11 |
||
12 |
datatype 'a maybe = Ok 'a | Fail |
|
13 |
||
19736 | 14 |
definition |
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
20523
diff
changeset
|
15 |
bind :: "'a maybe \<Rightarrow> ('a \<Rightarrow> 'b maybe) \<Rightarrow> 'b maybe" (infixl "\<bind>" 60) where |
19736 | 16 |
"m \<bind> f = (case m of Ok r \<Rightarrow> f r | Fail \<Rightarrow> Fail)" |
12944 | 17 |
|
18 |
syntax |
|
19 |
"_bind" :: "patterns \<Rightarrow> 'a maybe \<Rightarrow> 'b \<Rightarrow> 'c" ("(_ := _;//_)" 0) |
|
20 |
translations |
|
21 |
"P := E; F" == "E \<bind> (\<lambda>P. F)" |
|
22 |
||
23 |
lemma bind_Ok [simp]: "(Ok s) \<bind> f = (f s)" |
|
24 |
by (simp add: bind_def) |
|
25 |
||
26 |
lemma bind_Fail [simp]: "Fail \<bind> f = Fail" |
|
27 |
by (simp add: bind_def) |
|
28 |
||
29 |
lemma split_bind: |
|
30 |
"P (res \<bind> f) = ((res = Fail \<longrightarrow> P Fail) \<and> (\<forall>s. res = Ok s \<longrightarrow> P (f s)))" |
|
31 |
by (induct res) simp_all |
|
32 |
||
33 |
lemma split_bind_asm: |
|
34 |
"P (res \<bind> f) = (\<not> (res = Fail \<and> \<not> P Fail \<or> (\<exists>s. res = Ok s \<and> \<not> P (f s))))" |
|
35 |
by (simp split: split_bind) |
|
36 |
||
37 |
lemmas bind_splits = split_bind split_bind_asm |
|
38 |
||
39 |
lemma bind_eq_Fail [simp]: |
|
40 |
"((m \<bind> f) = Fail) = ((m = Fail) \<or> (\<exists>p. m = Ok p \<and> f p = Fail))" |
|
41 |
by (simp split: split_bind) |
|
42 |
||
43 |
lemma rotate_Ok: "(y = Ok x) = (Ok x = y)" |
|
44 |
by (rule eq_sym_conv) |
|
45 |
||
46 |
||
47 |
section {* MiniML-types and type substitutions *} |
|
48 |
||
49 |
axclass type_struct \<subseteq> type |
|
50 |
-- {* new class for structures containing type variables *} |
|
51 |
||
52 |
datatype "typ" = TVar nat | TFun "typ" "typ" (infixr "->" 70) |
|
53 |
-- {* type expressions *} |
|
54 |
||
55 |
types subst = "nat => typ" |
|
56 |
-- {* type variable substitution *} |
|
57 |
||
58 |
instance "typ" :: type_struct .. |
|
59 |
instance list :: (type_struct) type_struct .. |
|
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20503
diff
changeset
|
60 |
instance "fun" :: (type, type_struct) type_struct .. |
12944 | 61 |
|
62 |
||
63 |
subsection {* Substitutions *} |
|
64 |
||
65 |
consts |
|
66 |
app_subst :: "subst \<Rightarrow> 'a::type_struct \<Rightarrow> 'a::type_struct" ("$") |
|
67 |
-- {* extension of substitution to type structures *} |
|
13537 | 68 |
primrec (app_subst_typ) |
12944 | 69 |
app_subst_TVar: "$s (TVar n) = s n" |
70 |
app_subst_Fun: "$s (t1 -> t2) = $s t1 -> $s t2" |
|
71 |
||
72 |
defs (overloaded) |
|
73 |
app_subst_list: "$s \<equiv> map ($s)" |
|
74 |
||
75 |
consts |
|
76 |
free_tv :: "'a::type_struct \<Rightarrow> nat set" |
|
77 |
-- {* @{text "free_tv s"}: the type variables occuring freely in the type structure @{text s} *} |
|
78 |
||
13537 | 79 |
primrec (free_tv_typ) |
12944 | 80 |
"free_tv (TVar m) = {m}" |
81 |
"free_tv (t1 -> t2) = free_tv t1 \<union> free_tv t2" |
|
82 |
||
13537 | 83 |
primrec (free_tv_list) |
12944 | 84 |
"free_tv [] = {}" |
85 |
"free_tv (x # xs) = free_tv x \<union> free_tv xs" |
|
86 |
||
19736 | 87 |
definition |
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
20523
diff
changeset
|
88 |
dom :: "subst \<Rightarrow> nat set" where |
19736 | 89 |
"dom s = {n. s n \<noteq> TVar n}" |
12944 | 90 |
-- {* domain of a substitution *} |
91 |
||
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
20523
diff
changeset
|
92 |
definition |
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
20523
diff
changeset
|
93 |
cod :: "subst \<Rightarrow> nat set" where |
19736 | 94 |
"cod s = (\<Union>m \<in> dom s. free_tv (s m))" |
12944 | 95 |
-- {* codomain of a substitutions: the introduced variables *} |
96 |
||
19736 | 97 |
defs (overloaded) |
12944 | 98 |
free_tv_subst: "free_tv s \<equiv> dom s \<union> cod s" |
99 |
||
100 |
text {* |
|
101 |
@{text "new_tv s n"} checks whether @{text n} is a new type variable |
|
102 |
wrt.\ a type structure @{text s}, i.e.\ whether @{text n} is greater |
|
103 |
than any type variable occuring in the type structure. |
|
104 |
*} |
|
105 |
||
19736 | 106 |
definition |
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
20523
diff
changeset
|
107 |
new_tv :: "nat \<Rightarrow> 'a::type_struct \<Rightarrow> bool" where |
19736 | 108 |
"new_tv n ts = (\<forall>m. m \<in> free_tv ts \<longrightarrow> m < n)" |
12944 | 109 |
|
110 |
||
111 |
subsubsection {* Identity substitution *} |
|
112 |
||
19736 | 113 |
definition |
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
20523
diff
changeset
|
114 |
id_subst :: subst where |
19736 | 115 |
"id_subst = (\<lambda>n. TVar n)" |
12944 | 116 |
|
117 |
lemma app_subst_id_te [simp]: |
|
118 |
"$id_subst = (\<lambda>t::typ. t)" |
|
119 |
-- {* application of @{text id_subst} does not change type expression *} |
|
120 |
proof |
|
121 |
fix t :: "typ" |
|
122 |
show "$id_subst t = t" |
|
123 |
by (induct t) (simp_all add: id_subst_def) |
|
124 |
qed |
|
125 |
||
126 |
lemma app_subst_id_tel [simp]: "$id_subst = (\<lambda>ts::typ list. ts)" |
|
127 |
-- {* application of @{text id_subst} does not change list of type expressions *} |
|
128 |
proof |
|
129 |
fix ts :: "typ list" |
|
130 |
show "$id_subst ts = ts" |
|
131 |
by (induct ts) (simp_all add: app_subst_list) |
|
132 |
qed |
|
133 |
||
134 |
lemma o_id_subst [simp]: "$s o id_subst = s" |
|
135 |
by (rule ext) (simp add: id_subst_def) |
|
136 |
||
137 |
lemma dom_id_subst [simp]: "dom id_subst = {}" |
|
13890 | 138 |
by (simp add: dom_def id_subst_def) |
12944 | 139 |
|
140 |
lemma cod_id_subst [simp]: "cod id_subst = {}" |
|
141 |
by (simp add: cod_def) |
|
142 |
||
143 |
lemma free_tv_id_subst [simp]: "free_tv id_subst = {}" |
|
144 |
by (simp add: free_tv_subst) |
|
145 |
||
146 |
||
147 |
lemma cod_app_subst [simp]: |
|
148 |
assumes free: "v \<in> free_tv (s n)" |
|
149 |
and neq: "v \<noteq> n" |
|
150 |
shows "v \<in> cod s" |
|
151 |
proof - |
|
152 |
have "s n \<noteq> TVar n" |
|
153 |
proof |
|
154 |
assume "s n = TVar n" |
|
155 |
with free have "v = n" by simp |
|
156 |
with neq show False .. |
|
157 |
qed |
|
158 |
with free show ?thesis |
|
159 |
by (auto simp add: dom_def cod_def) |
|
160 |
qed |
|
161 |
||
162 |
lemma subst_comp_te: "$g ($f t :: typ) = $(\<lambda>x. $g (f x)) t" |
|
163 |
-- {* composition of substitutions *} |
|
164 |
by (induct t) simp_all |
|
165 |
||
166 |
lemma subst_comp_tel: "$g ($f ts :: typ list) = $(\<lambda>x. $g (f x)) ts" |
|
167 |
by (induct ts) (simp_all add: app_subst_list subst_comp_te) |
|
168 |
||
169 |
||
170 |
lemma app_subst_Nil [simp]: "$s [] = []" |
|
171 |
by (simp add: app_subst_list) |
|
172 |
||
173 |
lemma app_subst_Cons [simp]: "$s (t # ts) = ($s t) # ($s ts)" |
|
174 |
by (simp add: app_subst_list) |
|
175 |
||
176 |
lemma new_tv_TVar [simp]: "new_tv n (TVar m) = (m < n)" |
|
177 |
by (simp add: new_tv_def) |
|
178 |
||
179 |
lemma new_tv_Fun [simp]: |
|
180 |
"new_tv n (t1 -> t2) = (new_tv n t1 \<and> new_tv n t2)" |
|
181 |
by (auto simp add: new_tv_def) |
|
182 |
||
183 |
lemma new_tv_Nil [simp]: "new_tv n []" |
|
184 |
by (simp add: new_tv_def) |
|
185 |
||
186 |
lemma new_tv_Cons [simp]: "new_tv n (t # ts) = (new_tv n t \<and> new_tv n ts)" |
|
187 |
by (auto simp add: new_tv_def) |
|
188 |
||
189 |
lemma new_tv_id_subst [simp]: "new_tv n id_subst" |
|
190 |
by (simp add: id_subst_def new_tv_def free_tv_subst dom_def cod_def) |
|
191 |
||
192 |
lemma new_tv_subst: |
|
193 |
"new_tv n s = |
|
194 |
((\<forall>m. n \<le> m \<longrightarrow> s m = TVar m) \<and> |
|
195 |
(\<forall>l. l < n \<longrightarrow> new_tv n (s l)))" |
|
196 |
apply (unfold new_tv_def) |
|
197 |
apply (tactic "safe_tac HOL_cs") |
|
198 |
-- {* @{text \<Longrightarrow>} *} |
|
22548 | 199 |
apply (tactic {* fast_tac (HOL_cs addDs [@{thm leD}] addss (simpset() |
12944 | 200 |
addsimps [thm "free_tv_subst", thm "dom_def"])) 1 *}) |
201 |
apply (subgoal_tac "m \<in> cod s \<or> s l = TVar l") |
|
202 |
apply (tactic "safe_tac HOL_cs") |
|
203 |
apply (tactic {* fast_tac (HOL_cs addDs [UnI2] addss (simpset() |
|
204 |
addsimps [thm "free_tv_subst"])) 1 *}) |
|
205 |
apply (drule_tac P = "\<lambda>x. m \<in> free_tv x" in subst, assumption) |
|
206 |
apply simp |
|
207 |
apply (tactic {* fast_tac (set_cs addss (simpset() |
|
208 |
addsimps [thm "free_tv_subst", thm "cod_def", thm "dom_def"])) 1 *}) |
|
209 |
-- {* @{text \<Longleftarrow>} *} |
|
210 |
apply (unfold free_tv_subst cod_def dom_def) |
|
211 |
apply (tactic "safe_tac set_cs") |
|
212 |
apply (cut_tac m = m and n = n in less_linear) |
|
23880 | 213 |
apply (tactic "fast_tac (HOL_cs addSIs [@{thm less_or_eq_imp_le}]) 1") |
12944 | 214 |
apply (cut_tac m = ma and n = n in less_linear) |
215 |
apply (fast intro!: less_or_eq_imp_le) |
|
216 |
done |
|
217 |
||
218 |
lemma new_tv_list: "new_tv n x = (\<forall>y \<in> set x. new_tv n y)" |
|
219 |
by (induct x) simp_all |
|
220 |
||
221 |
lemma subst_te_new_tv [simp]: |
|
19736 | 222 |
"new_tv n (t::typ) \<Longrightarrow> $(\<lambda>x. if x = n then t' else s x) t = $s t" |
12944 | 223 |
-- {* substitution affects only variables occurring freely *} |
224 |
by (induct t) simp_all |
|
225 |
||
226 |
lemma subst_tel_new_tv [simp]: |
|
19736 | 227 |
"new_tv n (ts::typ list) \<Longrightarrow> $(\<lambda>x. if x = n then t else s x) ts = $s ts" |
12944 | 228 |
by (induct ts) simp_all |
229 |
||
230 |
lemma new_tv_le: "n \<le> m \<Longrightarrow> new_tv n (t::typ) \<Longrightarrow> new_tv m t" |
|
231 |
-- {* all greater variables are also new *} |
|
232 |
proof (induct t) |
|
233 |
case (TVar n) |
|
19736 | 234 |
then show ?case by (auto intro: less_le_trans) |
12944 | 235 |
next |
236 |
case TFun |
|
19736 | 237 |
then show ?case by simp |
12944 | 238 |
qed |
239 |
||
240 |
lemma [simp]: "new_tv n t \<Longrightarrow> new_tv (Suc n) (t::typ)" |
|
241 |
by (rule lessI [THEN less_imp_le [THEN new_tv_le]]) |
|
242 |
||
243 |
lemma new_tv_list_le: |
|
19736 | 244 |
assumes "n \<le> m" |
245 |
shows "new_tv n (ts::typ list) \<Longrightarrow> new_tv m ts" |
|
12944 | 246 |
proof (induct ts) |
247 |
case Nil |
|
19736 | 248 |
then show ?case by simp |
12944 | 249 |
next |
250 |
case Cons |
|
19736 | 251 |
with `n \<le> m` show ?case by (auto intro: new_tv_le) |
12944 | 252 |
qed |
253 |
||
254 |
lemma [simp]: "new_tv n ts \<Longrightarrow> new_tv (Suc n) (ts::typ list)" |
|
255 |
by (rule lessI [THEN less_imp_le [THEN new_tv_list_le]]) |
|
256 |
||
257 |
lemma new_tv_subst_le: "n \<le> m \<Longrightarrow> new_tv n (s::subst) \<Longrightarrow> new_tv m s" |
|
258 |
apply (simp add: new_tv_subst) |
|
259 |
apply clarify |
|
260 |
apply (rule_tac P = "l < n" and Q = "n <= l" in disjE) |
|
261 |
apply clarify |
|
262 |
apply (simp_all add: new_tv_le) |
|
263 |
done |
|
264 |
||
265 |
lemma [simp]: "new_tv n s \<Longrightarrow> new_tv (Suc n) (s::subst)" |
|
266 |
by (rule lessI [THEN less_imp_le [THEN new_tv_subst_le]]) |
|
267 |
||
268 |
lemma new_tv_subst_var: |
|
269 |
"n < m \<Longrightarrow> new_tv m (s::subst) \<Longrightarrow> new_tv m (s n)" |
|
270 |
-- {* @{text new_tv} property remains if a substitution is applied *} |
|
271 |
by (simp add: new_tv_subst) |
|
272 |
||
273 |
lemma new_tv_subst_te [simp]: |
|
274 |
"new_tv n s \<Longrightarrow> new_tv n (t::typ) \<Longrightarrow> new_tv n ($s t)" |
|
275 |
by (induct t) (auto simp add: new_tv_subst) |
|
276 |
||
277 |
lemma new_tv_subst_tel [simp]: |
|
278 |
"new_tv n s \<Longrightarrow> new_tv n (ts::typ list) \<Longrightarrow> new_tv n ($s ts)" |
|
279 |
by (induct ts) (fastsimp simp add: new_tv_subst)+ |
|
280 |
||
281 |
lemma new_tv_Suc_list: "new_tv n ts --> new_tv (Suc n) (TVar n # ts)" |
|
282 |
-- {* auxilliary lemma *} |
|
283 |
by (simp add: new_tv_list) |
|
284 |
||
285 |
lemma new_tv_subst_comp_1 [simp]: |
|
286 |
"new_tv n (s::subst) \<Longrightarrow> new_tv n r \<Longrightarrow> new_tv n ($r o s)" |
|
287 |
-- {* composition of substitutions preserves @{text new_tv} proposition *} |
|
288 |
by (simp add: new_tv_subst) |
|
289 |
||
290 |
lemma new_tv_subst_comp_2 [simp]: |
|
291 |
"new_tv n (s::subst) \<Longrightarrow> new_tv n r \<Longrightarrow> new_tv n (\<lambda>v. $r (s v))" |
|
292 |
by (simp add: new_tv_subst) |
|
293 |
||
294 |
lemma new_tv_not_free_tv [simp]: "new_tv n ts \<Longrightarrow> n \<notin> free_tv ts" |
|
295 |
-- {* new type variables do not occur freely in a type structure *} |
|
296 |
by (auto simp add: new_tv_def) |
|
297 |
||
298 |
lemma ftv_mem_sub_ftv_list [simp]: |
|
299 |
"(t::typ) \<in> set ts \<Longrightarrow> free_tv t \<subseteq> free_tv ts" |
|
300 |
by (induct ts) auto |
|
301 |
||
302 |
text {* |
|
303 |
If two substitutions yield the same result if applied to a type |
|
304 |
structure the substitutions coincide on the free type variables |
|
305 |
occurring in the type structure. |
|
306 |
*} |
|
307 |
||
308 |
lemma eq_subst_te_eq_free: |
|
309 |
"$s1 (t::typ) = $s2 t \<Longrightarrow> n \<in> free_tv t \<Longrightarrow> s1 n = s2 n" |
|
310 |
by (induct t) auto |
|
311 |
||
312 |
lemma eq_free_eq_subst_te: |
|
313 |
"(\<forall>n. n \<in> free_tv t --> s1 n = s2 n) \<Longrightarrow> $s1 (t::typ) = $s2 t" |
|
314 |
by (induct t) auto |
|
315 |
||
316 |
lemma eq_subst_tel_eq_free: |
|
317 |
"$s1 (ts::typ list) = $s2 ts \<Longrightarrow> n \<in> free_tv ts \<Longrightarrow> s1 n = s2 n" |
|
318 |
by (induct ts) (auto intro: eq_subst_te_eq_free) |
|
319 |
||
320 |
lemma eq_free_eq_subst_tel: |
|
321 |
"(\<forall>n. n \<in> free_tv ts --> s1 n = s2 n) \<Longrightarrow> $s1 (ts::typ list) = $s2 ts" |
|
322 |
by (induct ts) (auto intro: eq_free_eq_subst_te) |
|
323 |
||
324 |
text {* |
|
325 |
\medskip Some useful lemmas. |
|
326 |
*} |
|
327 |
||
328 |
lemma codD: "v \<in> cod s \<Longrightarrow> v \<in> free_tv s" |
|
329 |
by (simp add: free_tv_subst) |
|
330 |
||
331 |
lemma not_free_impl_id: "x \<notin> free_tv s \<Longrightarrow> s x = TVar x" |
|
332 |
by (simp add: free_tv_subst dom_def) |
|
333 |
||
334 |
lemma free_tv_le_new_tv: "new_tv n t \<Longrightarrow> m \<in> free_tv t \<Longrightarrow> m < n" |
|
335 |
by (unfold new_tv_def) fast |
|
336 |
||
337 |
lemma free_tv_subst_var: "free_tv (s (v::nat)) \<le> insert v (cod s)" |
|
338 |
by (cases "v \<in> dom s") (auto simp add: cod_def dom_def) |
|
339 |
||
340 |
lemma free_tv_app_subst_te: "free_tv ($s (t::typ)) \<subseteq> cod s \<union> free_tv t" |
|
341 |
by (induct t) (auto simp add: free_tv_subst_var) |
|
342 |
||
343 |
lemma free_tv_app_subst_tel: "free_tv ($s (ts::typ list)) \<subseteq> cod s \<union> free_tv ts" |
|
344 |
apply (induct ts) |
|
345 |
apply simp |
|
346 |
apply (cut_tac free_tv_app_subst_te) |
|
347 |
apply fastsimp |
|
348 |
done |
|
349 |
||
350 |
lemma free_tv_comp_subst: |
|
351 |
"free_tv (\<lambda>u::nat. $s1 (s2 u) :: typ) \<subseteq> free_tv s1 \<union> free_tv s2" |
|
352 |
apply (unfold free_tv_subst dom_def) |
|
353 |
apply (tactic {* |
|
354 |
fast_tac (set_cs addSDs [thm "free_tv_app_subst_te" RS subsetD, |
|
355 |
thm "free_tv_subst_var" RS subsetD] |
|
21669 | 356 |
addss (simpset() delsimps (thms "bex_simps") |
12944 | 357 |
addsimps [thm "cod_def", thm "dom_def"])) 1 *}) |
358 |
done |
|
359 |
||
360 |
||
361 |
subsection {* Most general unifiers *} |
|
362 |
||
363 |
consts |
|
364 |
mgu :: "typ \<Rightarrow> typ \<Rightarrow> subst maybe" |
|
365 |
axioms |
|
366 |
mgu_eq [simp]: "mgu t1 t2 = Ok u \<Longrightarrow> $u t1 = $u t2" |
|
367 |
mgu_mg [simp]: "mgu t1 t2 = Ok u \<Longrightarrow> $s t1 = $s t2 \<Longrightarrow> \<exists>r. s = $r o u" |
|
368 |
mgu_Ok: "$s t1 = $s t2 \<Longrightarrow> \<exists>u. mgu t1 t2 = Ok u" |
|
369 |
mgu_free [simp]: "mgu t1 t2 = Ok u \<Longrightarrow> free_tv u \<subseteq> free_tv t1 \<union> free_tv t2" |
|
370 |
||
371 |
lemma mgu_new: "mgu t1 t2 = Ok u \<Longrightarrow> new_tv n t1 \<Longrightarrow> new_tv n t2 \<Longrightarrow> new_tv n u" |
|
372 |
-- {* @{text mgu} does not introduce new type variables *} |
|
373 |
by (unfold new_tv_def) (blast dest: mgu_free) |
|
374 |
||
375 |
||
376 |
section {* Mini-ML with type inference rules *} |
|
377 |
||
378 |
datatype |
|
379 |
expr = Var nat | Abs expr | App expr expr |
|
380 |
||
381 |
||
382 |
text {* Type inference rules. *} |
|
383 |
||
23767 | 384 |
inductive |
385 |
has_type :: "typ list \<Rightarrow> expr \<Rightarrow> typ \<Rightarrow> bool" ("((_) |-/ (_) :: (_))" [60, 0, 60] 60) |
|
386 |
where |
|
12944 | 387 |
Var: "n < length a \<Longrightarrow> a |- Var n :: a ! n" |
23767 | 388 |
| Abs: "t1#a |- e :: t2 \<Longrightarrow> a |- Abs e :: t1 -> t2" |
389 |
| App: "a |- e1 :: t2 -> t1 \<Longrightarrow> a |- e2 :: t2 |
|
12944 | 390 |
\<Longrightarrow> a |- App e1 e2 :: t1" |
391 |
||
392 |
||
393 |
text {* Type assigment is closed wrt.\ substitution. *} |
|
394 |
||
395 |
lemma has_type_subst_closed: "a |- e :: t ==> $s a |- e :: $s t" |
|
19736 | 396 |
proof (induct set: has_type) |
23767 | 397 |
case (Var n a) |
19736 | 398 |
then have "n < length (map ($ s) a)" by simp |
399 |
then have "map ($ s) a |- Var n :: map ($ s) a ! n" |
|
400 |
by (rule has_type.Var) |
|
401 |
also have "map ($ s) a ! n = $ s (a ! n)" |
|
23373 | 402 |
by (rule nth_map) (rule Var) |
19736 | 403 |
also have "map ($ s) a = $ s a" |
404 |
by (simp only: app_subst_list) |
|
405 |
finally show ?case . |
|
406 |
next |
|
23767 | 407 |
case (Abs t1 a e t2) |
19736 | 408 |
then have "$ s t1 # map ($ s) a |- e :: $ s t2" |
409 |
by (simp add: app_subst_list) |
|
410 |
then have "map ($ s) a |- Abs e :: $ s t1 -> $ s t2" |
|
411 |
by (rule has_type.Abs) |
|
412 |
then show ?case |
|
413 |
by (simp add: app_subst_list) |
|
414 |
next |
|
415 |
case App |
|
416 |
then show ?case by (simp add: has_type.App) |
|
12944 | 417 |
qed |
418 |
||
419 |
||
12961 | 420 |
section {* Correctness and completeness of the type inference algorithm W *} |
12944 | 421 |
|
422 |
consts |
|
19747 | 423 |
"\<W>" :: "expr \<Rightarrow> typ list \<Rightarrow> nat \<Rightarrow> (subst \<times> typ \<times> nat) maybe" |
12944 | 424 |
primrec |
425 |
"\<W> (Var i) a n = |
|
426 |
(if i < length a then Ok (id_subst, a ! i, n) else Fail)" |
|
427 |
"\<W> (Abs e) a n = |
|
428 |
((s, t, m) := \<W> e (TVar n # a) (Suc n); |
|
429 |
Ok (s, (s n) -> t, m))" |
|
430 |
"\<W> (App e1 e2) a n = |
|
431 |
((s1, t1, m1) := \<W> e1 a n; |
|
432 |
(s2, t2, m2) := \<W> e2 ($s1 a) m1; |
|
433 |
u := mgu ($ s2 t1) (t2 -> TVar m2); |
|
434 |
Ok ($u o $s2 o s1, $u (TVar m2), Suc m2))" |
|
435 |
||
19736 | 436 |
theorem W_correct: "Ok (s, t, m) = \<W> e a n ==> $s a |- e :: t" |
20503 | 437 |
proof (induct e arbitrary: a s t m n) |
19736 | 438 |
case (Var i) |
439 |
from `Ok (s, t, m) = \<W> (Var i) a n` |
|
440 |
show "$s a |- Var i :: t" by (simp add: has_type.Var split: if_splits) |
|
441 |
next |
|
442 |
case (Abs e) |
|
443 |
from `Ok (s, t, m) = \<W> (Abs e) a n` |
|
444 |
obtain t' where "t = s n -> t'" |
|
445 |
and "Ok (s, t', m) = \<W> e (TVar n # a) (Suc n)" |
|
446 |
by (auto split: bind_splits) |
|
447 |
with Abs.hyps show "$s a |- Abs e :: t" |
|
448 |
by (force intro: has_type.Abs) |
|
449 |
next |
|
450 |
case (App e1 e2) |
|
451 |
from `Ok (s, t, m) = \<W> (App e1 e2) a n` |
|
452 |
obtain s1 t1 n1 s2 t2 n2 u where |
|
12944 | 453 |
s: "s = $u o $s2 o s1" |
19736 | 454 |
and t: "t = u n2" |
455 |
and mgu_ok: "mgu ($s2 t1) (t2 -> TVar n2) = Ok u" |
|
456 |
and W1_ok: "Ok (s1, t1, n1) = \<W> e1 a n" |
|
457 |
and W2_ok: "Ok (s2, t2, n2) = \<W> e2 ($s1 a) n1" |
|
458 |
by (auto split: bind_splits simp: that) |
|
459 |
show "$s a |- App e1 e2 :: t" |
|
460 |
proof (rule has_type.App) |
|
461 |
from s have s': "$u ($s2 ($s1 a)) = $s a" |
|
462 |
by (simp add: subst_comp_tel o_def) |
|
463 |
show "$s a |- e1 :: $u t2 -> t" |
|
464 |
proof - |
|
19747 | 465 |
from W1_ok have "$s1 a |- e1 :: t1" by (rule App.hyps(1)) |
19736 | 466 |
then have "$u ($s2 ($s1 a)) |- e1 :: $u ($s2 t1)" |
467 |
by (intro has_type_subst_closed) |
|
468 |
with s' t mgu_ok show ?thesis by simp |
|
12944 | 469 |
qed |
19736 | 470 |
show "$s a |- e2 :: $u t2" |
471 |
proof - |
|
19747 | 472 |
from W2_ok have "$s2 ($s1 a) |- e2 :: t2" by (rule App.hyps(2)) |
19736 | 473 |
then have "$u ($s2 ($s1 a)) |- e2 :: $u t2" |
474 |
by (rule has_type_subst_closed) |
|
475 |
with s' show ?thesis by simp |
|
476 |
qed |
|
477 |
qed |
|
12944 | 478 |
qed |
479 |
||
480 |
||
481 |
inductive_cases has_type_casesE: |
|
482 |
"s |- Var n :: t" |
|
483 |
"s |- Abs e :: t" |
|
484 |
"s |- App e1 e2 ::t" |
|
485 |
||
486 |
||
487 |
lemmas [simp] = Suc_le_lessD |
|
488 |
and [simp del] = less_imp_le ex_simps all_simps |
|
489 |
||
490 |
lemma W_var_ge [simp]: "!!a n s t m. \<W> e a n = Ok (s, t, m) \<Longrightarrow> n \<le> m" |
|
491 |
-- {* the resulting type variable is always greater or equal than the given one *} |
|
492 |
apply (atomize (full)) |
|
493 |
apply (induct e) |
|
494 |
txt {* case @{text "Var n"} *} |
|
495 |
apply clarsimp |
|
496 |
txt {* case @{text "Abs e"} *} |
|
497 |
apply (simp split add: split_bind) |
|
498 |
apply (fast dest: Suc_leD) |
|
499 |
txt {* case @{text "App e1 e2"} *} |
|
500 |
apply (simp (no_asm) split add: split_bind) |
|
501 |
apply (intro strip) |
|
502 |
apply (rename_tac s t na sa ta nb sb) |
|
503 |
apply (erule_tac x = a in allE) |
|
504 |
apply (erule_tac x = n in allE) |
|
505 |
apply (erule_tac x = "$s a" in allE) |
|
506 |
apply (erule_tac x = s in allE) |
|
507 |
apply (erule_tac x = t in allE) |
|
508 |
apply (erule_tac x = na in allE) |
|
509 |
apply (erule_tac x = na in allE) |
|
510 |
apply (simp add: eq_sym_conv) |
|
511 |
done |
|
512 |
||
513 |
lemma W_var_geD: "Ok (s, t, m) = \<W> e a n \<Longrightarrow> n \<le> m" |
|
514 |
by (simp add: eq_sym_conv) |
|
515 |
||
516 |
lemma new_tv_W: "!!n a s t m. |
|
517 |
new_tv n a \<Longrightarrow> \<W> e a n = Ok (s, t, m) \<Longrightarrow> new_tv m s & new_tv m t" |
|
518 |
-- {* resulting type variable is new *} |
|
519 |
apply (atomize (full)) |
|
520 |
apply (induct e) |
|
521 |
txt {* case @{text "Var n"} *} |
|
522 |
apply clarsimp |
|
523 |
apply (force elim: list_ball_nth simp add: id_subst_def new_tv_list new_tv_subst) |
|
524 |
txt {* case @{text "Abs e"} *} |
|
525 |
apply (simp (no_asm) add: new_tv_subst new_tv_Suc_list split add: split_bind) |
|
526 |
apply (intro strip) |
|
527 |
apply (erule_tac x = "Suc n" in allE) |
|
528 |
apply (erule_tac x = "TVar n # a" in allE) |
|
529 |
apply (fastsimp simp add: new_tv_subst new_tv_Suc_list) |
|
530 |
txt {* case @{text "App e1 e2"} *} |
|
531 |
apply (simp (no_asm) split add: split_bind) |
|
532 |
apply (intro strip) |
|
533 |
apply (rename_tac s t na sa ta nb sb) |
|
534 |
apply (erule_tac x = n in allE) |
|
535 |
apply (erule_tac x = a in allE) |
|
536 |
apply (erule_tac x = s in allE) |
|
537 |
apply (erule_tac x = t in allE) |
|
538 |
apply (erule_tac x = na in allE) |
|
539 |
apply (erule_tac x = na in allE) |
|
540 |
apply (simp add: eq_sym_conv) |
|
541 |
apply (erule_tac x = "$s a" in allE) |
|
542 |
apply (erule_tac x = sa in allE) |
|
543 |
apply (erule_tac x = ta in allE) |
|
544 |
apply (erule_tac x = nb in allE) |
|
545 |
apply (simp add: o_def rotate_Ok) |
|
546 |
apply (rule conjI) |
|
547 |
apply (rule new_tv_subst_comp_2) |
|
548 |
apply (rule new_tv_subst_comp_2) |
|
549 |
apply (rule lessI [THEN less_imp_le, THEN new_tv_subst_le]) |
|
550 |
apply (rule_tac n = na in new_tv_subst_le) |
|
551 |
apply (simp add: rotate_Ok) |
|
552 |
apply (simp (no_asm_simp)) |
|
553 |
apply (fast dest: W_var_geD intro: new_tv_list_le new_tv_subst_tel |
|
554 |
lessI [THEN less_imp_le, THEN new_tv_subst_le]) |
|
555 |
apply (erule sym [THEN mgu_new]) |
|
556 |
apply (best dest: W_var_geD intro: new_tv_subst_te new_tv_list_le new_tv_subst_tel |
|
557 |
lessI [THEN less_imp_le, THEN new_tv_le] lessI [THEN less_imp_le, THEN new_tv_subst_le] |
|
558 |
new_tv_le) |
|
559 |
apply (tactic {* fast_tac (HOL_cs addDs [thm "W_var_geD"] |
|
560 |
addIs [thm "new_tv_list_le", thm "new_tv_subst_tel", thm "new_tv_le"] |
|
561 |
addss (simpset())) 1 *}) |
|
562 |
apply (rule lessI [THEN new_tv_subst_var]) |
|
563 |
apply (erule sym [THEN mgu_new]) |
|
564 |
apply (bestsimp intro!: lessI [THEN less_imp_le, THEN new_tv_le] new_tv_subst_te |
|
565 |
dest!: W_var_geD intro: new_tv_list_le new_tv_subst_tel |
|
566 |
lessI [THEN less_imp_le, THEN new_tv_subst_le] new_tv_le) |
|
567 |
apply (tactic {* fast_tac (HOL_cs addDs [thm "W_var_geD"] |
|
568 |
addIs [thm "new_tv_list_le", thm "new_tv_subst_tel", thm "new_tv_le"] |
|
569 |
addss (simpset())) 1 *}) |
|
570 |
done |
|
571 |
||
572 |
lemma free_tv_W: "!!n a s t m v. \<W> e a n = Ok (s, t, m) \<Longrightarrow> |
|
573 |
(v \<in> free_tv s \<or> v \<in> free_tv t) \<Longrightarrow> v < n \<Longrightarrow> v \<in> free_tv a" |
|
574 |
apply (atomize (full)) |
|
575 |
apply (induct e) |
|
576 |
txt {* case @{text "Var n"} *} |
|
577 |
apply clarsimp |
|
21669 | 578 |
apply (tactic {* fast_tac (HOL_cs addIs [thm "nth_mem", subsetD, thm "ftv_mem_sub_ftv_list"]) 1 *}) |
12944 | 579 |
txt {* case @{text "Abs e"} *} |
580 |
apply (simp add: free_tv_subst split add: split_bind) |
|
581 |
apply (intro strip) |
|
582 |
apply (rename_tac s t n1 v) |
|
583 |
apply (erule_tac x = "Suc n" in allE) |
|
584 |
apply (erule_tac x = "TVar n # a" in allE) |
|
585 |
apply (erule_tac x = s in allE) |
|
586 |
apply (erule_tac x = t in allE) |
|
587 |
apply (erule_tac x = n1 in allE) |
|
588 |
apply (erule_tac x = v in allE) |
|
589 |
apply (force elim!: allE intro: cod_app_subst) |
|
590 |
txt {* case @{text "App e1 e2"} *} |
|
591 |
apply (simp (no_asm) split add: split_bind) |
|
592 |
apply (intro strip) |
|
593 |
apply (rename_tac s t n1 s1 t1 n2 s3 v) |
|
594 |
apply (erule_tac x = n in allE) |
|
595 |
apply (erule_tac x = a in allE) |
|
596 |
apply (erule_tac x = s in allE) |
|
597 |
apply (erule_tac x = t in allE) |
|
598 |
apply (erule_tac x = n1 in allE) |
|
599 |
apply (erule_tac x = n1 in allE) |
|
600 |
apply (erule_tac x = v in allE) |
|
601 |
txt {* second case *} |
|
602 |
apply (erule_tac x = "$ s a" in allE) |
|
603 |
apply (erule_tac x = s1 in allE) |
|
604 |
apply (erule_tac x = t1 in allE) |
|
605 |
apply (erule_tac x = n2 in allE) |
|
606 |
apply (erule_tac x = v in allE) |
|
607 |
apply (tactic "safe_tac (empty_cs addSIs [conjI, impI] addSEs [conjE])") |
|
608 |
apply (simp add: rotate_Ok o_def) |
|
609 |
apply (drule W_var_geD) |
|
610 |
apply (drule W_var_geD) |
|
611 |
apply (frule less_le_trans, assumption) |
|
612 |
apply (fastsimp dest: free_tv_comp_subst [THEN subsetD] sym [THEN mgu_free] codD |
|
613 |
free_tv_app_subst_te [THEN subsetD] free_tv_app_subst_tel [THEN subsetD] subsetD elim: UnE) |
|
614 |
apply simp |
|
615 |
apply (drule sym [THEN W_var_geD]) |
|
616 |
apply (drule sym [THEN W_var_geD]) |
|
617 |
apply (frule less_le_trans, assumption) |
|
618 |
apply (tactic {* fast_tac (HOL_cs addDs [thm "mgu_free", thm "codD", |
|
619 |
thm "free_tv_subst_var" RS subsetD, |
|
620 |
thm "free_tv_app_subst_te" RS subsetD, |
|
23880 | 621 |
thm "free_tv_app_subst_tel" RS subsetD, @{thm less_le_trans}, subsetD] |
12944 | 622 |
addSEs [UnE] addss (simpset() setSolver unsafe_solver)) 1 *}) |
623 |
-- {* builtin arithmetic in simpset messes things up *} |
|
624 |
done |
|
625 |
||
626 |
text {* |
|
627 |
\medskip Completeness of @{text \<W>} wrt.\ @{text has_type}. |
|
628 |
*} |
|
629 |
||
630 |
lemma W_complete_aux: "!!s' a t' n. $s' a |- e :: t' \<Longrightarrow> new_tv n a \<Longrightarrow> |
|
631 |
(\<exists>s t. (\<exists>m. \<W> e a n = Ok (s, t, m)) \<and> (\<exists>r. $s' a = $r ($s a) \<and> t' = $r t))" |
|
632 |
apply (atomize (full)) |
|
633 |
apply (induct e) |
|
634 |
txt {* case @{text "Var n"} *} |
|
635 |
apply (intro strip) |
|
636 |
apply (simp (no_asm) cong add: conj_cong) |
|
637 |
apply (erule has_type_casesE) |
|
638 |
apply (simp add: eq_sym_conv app_subst_list) |
|
639 |
apply (rule_tac x = s' in exI) |
|
640 |
apply simp |
|
641 |
txt {* case @{text "Abs e"} *} |
|
642 |
apply (intro strip) |
|
643 |
apply (erule has_type_casesE) |
|
644 |
apply (erule_tac x = "\<lambda>x. if x = n then t1 else (s' x)" in allE) |
|
645 |
apply (erule_tac x = "TVar n # a" in allE) |
|
646 |
apply (erule_tac x = t2 in allE) |
|
647 |
apply (erule_tac x = "Suc n" in allE) |
|
648 |
apply (fastsimp cong add: conj_cong split add: split_bind) |
|
649 |
txt {* case @{text "App e1 e2"} *} |
|
650 |
apply (intro strip) |
|
651 |
apply (erule has_type_casesE) |
|
652 |
apply (erule_tac x = s' in allE) |
|
653 |
apply (erule_tac x = a in allE) |
|
654 |
apply (erule_tac x = "t2 -> t'" in allE) |
|
655 |
apply (erule_tac x = n in allE) |
|
656 |
apply (tactic "safe_tac HOL_cs") |
|
657 |
apply (erule_tac x = r in allE) |
|
658 |
apply (erule_tac x = "$s a" in allE) |
|
659 |
apply (erule_tac x = t2 in allE) |
|
660 |
apply (erule_tac x = m in allE) |
|
661 |
apply simp |
|
662 |
apply (tactic "safe_tac HOL_cs") |
|
663 |
apply (tactic {* fast_tac (HOL_cs addIs [sym RS thm "W_var_geD", |
|
664 |
thm "new_tv_W" RS conjunct1, thm "new_tv_list_le", thm "new_tv_subst_tel"]) 1 *}) |
|
665 |
apply (subgoal_tac |
|
666 |
"$(\<lambda>x. if x = ma then t' else (if x \<in> free_tv t - free_tv sa then r x |
|
667 |
else ra x)) ($ sa t) = |
|
668 |
$(\<lambda>x. if x = ma then t' else (if x \<in> free_tv t - free_tv sa then r x |
|
669 |
else ra x)) (ta -> (TVar ma))") |
|
670 |
apply (rule_tac [2] t = "$(\<lambda>x. if x = ma then t' |
|
671 |
else (if x \<in> (free_tv t - free_tv sa) then r x else ra x)) ($sa t)" and |
|
672 |
s = "($ ra ta) -> t'" in ssubst) |
|
673 |
prefer 2 |
|
674 |
apply (simp add: subst_comp_te) |
|
675 |
apply (rule eq_free_eq_subst_te) |
|
676 |
apply (intro strip) |
|
677 |
apply (subgoal_tac "na \<noteq> ma") |
|
678 |
prefer 2 |
|
679 |
apply (fast dest: new_tv_W sym [THEN W_var_geD] new_tv_not_free_tv new_tv_le) |
|
680 |
apply (case_tac "na \<in> free_tv sa") |
|
681 |
txt {* @{text "na \<notin> free_tv sa"} *} |
|
682 |
prefer 2 |
|
683 |
apply (frule not_free_impl_id) |
|
684 |
apply simp |
|
685 |
txt {* @{text "na \<in> free_tv sa"} *} |
|
13601 | 686 |
apply (drule_tac ts1 = "$s a" and r = "$ r ($ s a)" in subst_comp_tel [THEN [2] trans]) |
12944 | 687 |
apply (drule_tac eq_subst_tel_eq_free) |
688 |
apply (fast intro: free_tv_W free_tv_le_new_tv dest: new_tv_W) |
|
689 |
apply simp |
|
690 |
apply (case_tac "na \<in> dom sa") |
|
691 |
prefer 2 |
|
692 |
txt {* @{text "na \<noteq> dom sa"} *} |
|
693 |
apply (simp add: dom_def) |
|
694 |
txt {* @{text "na \<in> dom sa"} *} |
|
695 |
apply (rule eq_free_eq_subst_te) |
|
696 |
apply (intro strip) |
|
697 |
apply (subgoal_tac "nb \<noteq> ma") |
|
698 |
prefer 2 |
|
699 |
apply (frule new_tv_W, assumption) |
|
700 |
apply (erule conjE) |
|
701 |
apply (drule new_tv_subst_tel) |
|
702 |
apply (fast intro: new_tv_list_le dest: sym [THEN W_var_geD]) |
|
703 |
apply (fastsimp dest: new_tv_W new_tv_not_free_tv simp add: cod_def free_tv_subst) |
|
704 |
apply (fastsimp simp add: cod_def free_tv_subst) |
|
705 |
prefer 2 |
|
706 |
apply (simp (no_asm)) |
|
707 |
apply (rule eq_free_eq_subst_te) |
|
708 |
apply (intro strip) |
|
709 |
apply (subgoal_tac "na \<noteq> ma") |
|
710 |
prefer 2 |
|
711 |
apply (frule new_tv_W, assumption) |
|
712 |
apply (erule conjE) |
|
713 |
apply (drule sym [THEN W_var_geD]) |
|
714 |
apply (fast dest: new_tv_list_le new_tv_subst_tel new_tv_W new_tv_not_free_tv) |
|
715 |
apply (case_tac "na \<in> free_tv t - free_tv sa") |
|
716 |
prefer 2 |
|
717 |
txt {* case @{text "na \<notin> free_tv t - free_tv sa"} *} |
|
718 |
apply simp |
|
719 |
defer |
|
720 |
txt {* case @{text "na \<in> free_tv t - free_tv sa"} *} |
|
721 |
apply simp |
|
13601 | 722 |
apply (drule_tac ts1 = "$s a" and r = "$ r ($ s a)" in subst_comp_tel [THEN [2] trans]) |
12944 | 723 |
apply (drule eq_subst_tel_eq_free) |
724 |
apply (fast intro: free_tv_W free_tv_le_new_tv dest: new_tv_W) |
|
725 |
apply (simp add: free_tv_subst dom_def) |
|
726 |
prefer 2 apply fast |
|
727 |
apply (simp (no_asm_simp) split add: split_bind) |
|
728 |
apply (tactic "safe_tac HOL_cs") |
|
729 |
apply (drule mgu_Ok) |
|
730 |
apply fastsimp |
|
731 |
apply (drule mgu_mg, assumption) |
|
732 |
apply (erule exE) |
|
733 |
apply (rule_tac x = rb in exI) |
|
734 |
apply (rule conjI) |
|
735 |
prefer 2 |
|
736 |
apply (drule_tac x = ma in fun_cong) |
|
737 |
apply (simp add: eq_sym_conv) |
|
738 |
apply (simp (no_asm) add: o_def subst_comp_tel [symmetric]) |
|
739 |
apply (rule subst_comp_tel [symmetric, THEN [2] trans]) |
|
740 |
apply (simp add: o_def eq_sym_conv) |
|
741 |
apply (rule eq_free_eq_subst_tel) |
|
742 |
apply (tactic "safe_tac HOL_cs") |
|
743 |
apply (subgoal_tac "ma \<noteq> na") |
|
744 |
prefer 2 |
|
745 |
apply (frule new_tv_W, assumption) |
|
746 |
apply (erule conjE) |
|
747 |
apply (drule new_tv_subst_tel) |
|
748 |
apply (fast intro: new_tv_list_le dest: sym [THEN W_var_geD]) |
|
749 |
apply (frule_tac n = m in new_tv_W, assumption) |
|
750 |
apply (erule conjE) |
|
751 |
apply (drule free_tv_app_subst_tel [THEN subsetD]) |
|
752 |
apply (tactic {* fast_tac (set_cs addDs [sym RS thm "W_var_geD", thm "new_tv_list_le", |
|
753 |
thm "codD", thm "new_tv_not_free_tv"]) 1 *}) |
|
754 |
apply (case_tac "na \<in> free_tv t - free_tv sa") |
|
755 |
prefer 2 |
|
756 |
txt {* case @{text "na \<notin> free_tv t - free_tv sa"} *} |
|
757 |
apply simp |
|
758 |
defer |
|
759 |
txt {* case @{text "na \<in> free_tv t - free_tv sa"} *} |
|
760 |
apply simp |
|
761 |
apply (drule free_tv_app_subst_tel [THEN subsetD]) |
|
762 |
apply (fastsimp dest: codD subst_comp_tel [THEN [2] trans] |
|
763 |
eq_subst_tel_eq_free simp add: free_tv_subst dom_def) |
|
764 |
apply fast |
|
765 |
done |
|
766 |
||
767 |
lemma W_complete: "[] |- e :: t' ==> |
|
768 |
\<exists>s t. (\<exists>m. \<W> e [] n = Ok (s, t, m)) \<and> (\<exists>r. t' = $r t)" |
|
769 |
apply (cut_tac a = "[]" and s' = id_subst and e = e and t' = t' in W_complete_aux) |
|
770 |
apply simp_all |
|
771 |
done |
|
772 |
||
773 |
||
12961 | 774 |
section {* Equivalence of W and I *} |
12944 | 775 |
|
776 |
text {* |
|
777 |
Recursive definition of type inference algorithm @{text \<I>} for |
|
778 |
Mini-ML. |
|
779 |
*} |
|
780 |
||
781 |
consts |
|
19747 | 782 |
"\<I>" :: "expr \<Rightarrow> typ list \<Rightarrow> nat \<Rightarrow> subst \<Rightarrow> (subst \<times> typ \<times> nat) maybe" |
12944 | 783 |
primrec |
784 |
"\<I> (Var i) a n s = (if i < length a then Ok (s, a ! i, n) else Fail)" |
|
785 |
"\<I> (Abs e) a n s = ((s, t, m) := \<I> e (TVar n # a) (Suc n) s; |
|
786 |
Ok (s, TVar n -> t, m))" |
|
787 |
"\<I> (App e1 e2) a n s = |
|
788 |
((s1, t1, m1) := \<I> e1 a n s; |
|
789 |
(s2, t2, m2) := \<I> e2 a m1 s1; |
|
790 |
u := mgu ($s2 t1) ($s2 t2 -> TVar m2); |
|
791 |
Ok($u o s2, TVar m2, Suc m2))" |
|
792 |
||
793 |
text {* \medskip Correctness. *} |
|
794 |
||
795 |
lemma I_correct_wrt_W: "!!a m s s' t n. |
|
796 |
new_tv m a \<and> new_tv m s \<Longrightarrow> \<I> e a m s = Ok (s', t, n) \<Longrightarrow> |
|
797 |
\<exists>r. \<W> e ($s a) m = Ok (r, $s' t, n) \<and> s' = ($r o s)" |
|
798 |
apply (atomize (full)) |
|
799 |
apply (induct e) |
|
800 |
txt {* case @{text "Var n"} *} |
|
801 |
apply (simp add: app_subst_list split: split_if) |
|
802 |
txt {* case @{text "Abs e"} *} |
|
803 |
apply (tactic {* asm_full_simp_tac |
|
804 |
(simpset() setloop (split_inside_tac [thm "split_bind"])) 1 *}) |
|
805 |
apply (intro strip) |
|
806 |
apply (rule conjI) |
|
807 |
apply (intro strip) |
|
808 |
apply (erule allE)+ |
|
809 |
apply (erule impE) |
|
810 |
prefer 2 apply (fastsimp simp add: new_tv_subst) |
|
811 |
apply (tactic {* fast_tac (HOL_cs addIs [thm "new_tv_Suc_list" RS mp, |
|
23880 | 812 |
thm "new_tv_subst_le", @{thm less_imp_le}, @{thm lessI}]) 1 *}) |
12944 | 813 |
apply (intro strip) |
814 |
apply (erule allE)+ |
|
815 |
apply (erule impE) |
|
816 |
prefer 2 apply (fastsimp simp add: new_tv_subst) |
|
817 |
apply (tactic {* fast_tac (HOL_cs addIs [thm "new_tv_Suc_list" RS mp, |
|
23880 | 818 |
thm "new_tv_subst_le", @{thm less_imp_le}, @{thm lessI}]) 1 *}) |
12944 | 819 |
txt {* case @{text "App e1 e2"} *} |
820 |
apply (tactic {* simp_tac (simpset () setloop (split_inside_tac [thm "split_bind"])) 1 *}) |
|
821 |
apply (intro strip) |
|
822 |
apply (rename_tac s1' t1 n1 s2' t2 n2 sa) |
|
823 |
apply (rule conjI) |
|
824 |
apply fastsimp |
|
825 |
apply (intro strip) |
|
826 |
apply (rename_tac s1 t1' n1') |
|
827 |
apply (erule_tac x = a in allE) |
|
828 |
apply (erule_tac x = m in allE) |
|
829 |
apply (erule_tac x = s in allE) |
|
830 |
apply (erule_tac x = s1' in allE) |
|
831 |
apply (erule_tac x = t1 in allE) |
|
832 |
apply (erule_tac x = n1 in allE) |
|
833 |
apply (erule_tac x = a in allE) |
|
834 |
apply (erule_tac x = n1 in allE) |
|
835 |
apply (erule_tac x = s1' in allE) |
|
836 |
apply (erule_tac x = s2' in allE) |
|
837 |
apply (erule_tac x = t2 in allE) |
|
838 |
apply (erule_tac x = n2 in allE) |
|
839 |
apply (rule conjI) |
|
840 |
apply (intro strip) |
|
841 |
apply (rule notI) |
|
842 |
apply simp |
|
843 |
apply (erule impE) |
|
844 |
apply (frule new_tv_subst_tel, assumption) |
|
845 |
apply (drule_tac a = "$s a" in new_tv_W, assumption) |
|
846 |
apply (fastsimp dest: sym [THEN W_var_geD] new_tv_subst_le new_tv_list_le) |
|
847 |
apply (fastsimp simp add: subst_comp_tel) |
|
848 |
apply (intro strip) |
|
849 |
apply (rename_tac s2 t2' n2') |
|
850 |
apply (rule conjI) |
|
851 |
apply (intro strip) |
|
852 |
apply (rule notI) |
|
853 |
apply simp |
|
854 |
apply (erule impE) |
|
855 |
apply (frule new_tv_subst_tel, assumption) |
|
856 |
apply (drule_tac a = "$s a" in new_tv_W, assumption) |
|
857 |
apply (fastsimp dest: sym [THEN W_var_geD] new_tv_subst_le new_tv_list_le) |
|
858 |
apply (fastsimp simp add: subst_comp_tel subst_comp_te) |
|
859 |
apply (intro strip) |
|
860 |
apply (erule (1) notE impE) |
|
861 |
apply (erule (1) notE impE) |
|
862 |
apply (erule exE) |
|
863 |
apply (erule conjE) |
|
864 |
apply (erule impE) |
|
865 |
apply (frule new_tv_subst_tel, assumption) |
|
866 |
apply (drule_tac a = "$s a" in new_tv_W, assumption) |
|
867 |
apply (fastsimp dest: sym [THEN W_var_geD] new_tv_subst_le new_tv_list_le) |
|
868 |
apply (erule (1) notE impE) |
|
869 |
apply (erule exE conjE)+ |
|
13601 | 870 |
apply (simp (asm_lr) add: subst_comp_tel subst_comp_te o_def, (erule conjE)+, hypsubst)+ |
12944 | 871 |
apply (subgoal_tac "new_tv n2 s \<and> new_tv n2 r \<and> new_tv n2 ra") |
872 |
apply (simp add: new_tv_subst) |
|
873 |
apply (frule new_tv_subst_tel, assumption) |
|
874 |
apply (drule_tac a = "$s a" in new_tv_W, assumption) |
|
875 |
apply (tactic "safe_tac HOL_cs") |
|
876 |
apply (bestsimp dest: sym [THEN W_var_geD] new_tv_subst_le new_tv_list_le) |
|
877 |
apply (fastsimp dest: sym [THEN W_var_geD] new_tv_subst_le new_tv_list_le) |
|
15236
f289e8ba2bb3
Proofs needed to be updated because induction now preserves name of
nipkow
parents:
15140
diff
changeset
|
878 |
apply (drule_tac e = e1 in sym [THEN W_var_geD]) |
12944 | 879 |
apply (drule new_tv_subst_tel, assumption) |
880 |
apply (drule_tac ts = "$s a" in new_tv_list_le, assumption) |
|
881 |
apply (drule new_tv_subst_tel, assumption) |
|
882 |
apply (bestsimp dest: new_tv_W simp add: subst_comp_tel) |
|
883 |
done |
|
884 |
||
885 |
lemma I_complete_wrt_W: "!!a m s. |
|
886 |
new_tv m a \<and> new_tv m s \<Longrightarrow> \<I> e a m s = Fail \<Longrightarrow> \<W> e ($s a) m = Fail" |
|
887 |
apply (atomize (full)) |
|
888 |
apply (induct e) |
|
889 |
apply (simp add: app_subst_list) |
|
890 |
apply (simp (no_asm)) |
|
891 |
apply (intro strip) |
|
892 |
apply (subgoal_tac "TVar m # $s a = $s (TVar m # a)") |
|
893 |
apply (tactic {* asm_simp_tac (HOL_ss addsimps |
|
23880 | 894 |
[thm "new_tv_Suc_list", @{thm lessI} RS @{thm less_imp_le} RS thm "new_tv_subst_le"]) 1 *}) |
12944 | 895 |
apply (erule conjE) |
896 |
apply (drule new_tv_not_free_tv [THEN not_free_impl_id]) |
|
897 |
apply (simp (no_asm_simp)) |
|
898 |
apply (simp (no_asm_simp)) |
|
899 |
apply (intro strip) |
|
900 |
apply (erule exE)+ |
|
901 |
apply (erule conjE)+ |
|
902 |
apply (drule I_correct_wrt_W [COMP swap_prems_rl]) |
|
903 |
apply fast |
|
904 |
apply (erule exE) |
|
905 |
apply (erule conjE) |
|
906 |
apply hypsubst |
|
907 |
apply (simp (no_asm_simp)) |
|
908 |
apply (erule disjE) |
|
909 |
apply (rule disjI1) |
|
910 |
apply (simp (no_asm_use) add: o_def subst_comp_tel) |
|
911 |
apply (erule allE, erule allE, erule allE, erule impE, erule_tac [2] impE, |
|
912 |
erule_tac [2] asm_rl, erule_tac [2] asm_rl) |
|
913 |
apply (rule conjI) |
|
914 |
apply (fast intro: W_var_ge [THEN new_tv_list_le]) |
|
915 |
apply (rule new_tv_subst_comp_2) |
|
916 |
apply (fast intro: W_var_ge [THEN new_tv_subst_le]) |
|
917 |
apply (fast intro!: new_tv_subst_tel intro: new_tv_W [THEN conjunct1]) |
|
918 |
apply (rule disjI2) |
|
919 |
apply (erule exE)+ |
|
920 |
apply (erule conjE) |
|
921 |
apply (drule I_correct_wrt_W [COMP swap_prems_rl]) |
|
922 |
apply (rule conjI) |
|
923 |
apply (fast intro: W_var_ge [THEN new_tv_list_le]) |
|
924 |
apply (rule new_tv_subst_comp_1) |
|
925 |
apply (fast intro: W_var_ge [THEN new_tv_subst_le]) |
|
926 |
apply (fast intro!: new_tv_subst_tel intro: new_tv_W [THEN conjunct1]) |
|
927 |
apply (erule exE) |
|
928 |
apply (erule conjE) |
|
929 |
apply hypsubst |
|
930 |
apply (simp add: o_def subst_comp_te [symmetric] subst_comp_tel [symmetric]) |
|
931 |
done |
|
932 |
||
933 |
end |