]> matita.cs.unibo.it Git - helm.git/blob - matita/library/Fsub/defn.ma
set -> type
[helm.git] / matita / library / Fsub / defn.ma
1 (**************************************************************************)
2 (*       ___                                                              *)
3 (*      ||M||                                                             *)
4 (*      ||A||       A project by Andrea Asperti                           *)
5 (*      ||T||                                                             *)
6 (*      ||I||       Developers:                                           *)
7 (*      ||T||         The HELM team.                                      *)
8 (*      ||A||         http://helm.cs.unibo.it                             *)
9 (*      \   /                                                             *)
10 (*       \ /        This file is distributed under the terms of the       *)
11 (*        v         GNU General Public License Version 2                  *)
12 (*                                                                        *)
13 (**************************************************************************)
14
15 set "baseuri" "cic:/matita/Fsub/defn".
16 include "logic/equality.ma".
17 include "nat/nat.ma".
18 include "datatypes/bool.ma".
19 include "nat/compare.ma".
20 include "list/list.ma".
21 include "Fsub/util.ma".
22
23 (*** representation of Fsub types ***)  
24 inductive Typ : Type \def
25   | TVar : nat \to Typ            (* type var *)
26   | TFree: nat \to Typ            (* free type name *)
27   | Top : Typ                     (* maximum type *)
28   | Arrow : Typ \to Typ \to Typ   (* functions *) 
29   | Forall : Typ \to Typ \to Typ. (* universal type *)
30   
31 (*** representation of Fsub terms ***)
32 inductive Term : Type \def
33   | Var : nat \to Term            (* variable *)
34   | Free : nat \to Term          (* free name *)
35   | Abs : Typ \to Term \to Term   (* abstraction *)
36   | App : Term \to Term \to Term  (* function application *)
37   | TAbs : Typ \to Term \to Term  (* type abstraction *)
38   | TApp : Term \to Typ \to Term. (* type application *)
39   
40 (* representation of bounds *)
41
42 record bound : Type \def { 
43                           istype : bool;    (* is subtyping bound? *)
44                           name   : nat ;    (* name *)
45                           btype  : Typ      (* type to which the name is bound *)
46                         }.
47                
48 (* representation of Fsub typing environments *)
49 definition Env \def (list bound).
50 definition Empty \def (nil bound).
51 definition Cons \def \lambda G,X,T.((mk_bound false X T) :: G).
52 definition TCons \def \lambda G,X,T.((mk_bound true X T) :: G).
53
54 definition env_append : Env \to Env \to Env \def \lambda G,H.(H @ G).
55   
56 (* notation "hvbox(\Forall S. break T)" 
57   non associative with precedence 90
58 for @{ 'forall $S $T}.
59
60 notation "hvbox(#x)"
61   with precedence 60
62   for @{'var $x}.
63   
64 notation "hvbox(##x)"
65   with precedence 61
66   for @{'tvar $x}.
67
68 notation "hvbox(!x)"
69   with precedence 60
70   for @{'name $x}.
71   
72 notation "hvbox(!!x)"
73   with precedence 61
74   for @{'tname $x}.
75
76 notation "hvbox(s break \mapsto t)"
77   right associative with precedence 55
78   for @{ 'arrow $s $t }.
79
80 interpretation "universal type" 'forall S T = (cic:/matita/Fsub/defn/Typ.ind#xpointer(1/1/5) S T).
81
82 interpretation "bound var" 'var x = (cic:/matita/Fsub/defn/Typ.ind#xpointer(1/1/1) x).
83
84 interpretation "bound tvar" 'tvar x = (cic:/matita/Fsub/defn/Typ.ind#xpointer(1/1/3) x).
85
86 interpretation "bound tname" 'tname x = (cic:/matita/Fsub/defn/Typ.ind#xpointer(1/1/2) x).
87
88 interpretation "arrow type" 'arrow S T = (cic:/matita/Fsub/defn/Typ.ind#xpointer(1/1/4) S T). *) 
89
90 (*** Various kinds of substitution, not all will be used probably ***)
91
92 (* substitutes i-th dangling index in type T with type U *)
93 let rec subst_type_nat T U i \def
94     match T with
95     [ (TVar n) \Rightarrow match (eqb n i) with
96       [ true \Rightarrow U
97       | false \Rightarrow T]
98     | (TFree X) \Rightarrow T
99     | Top \Rightarrow T
100     | (Arrow T1 T2) \Rightarrow (Arrow (subst_type_nat T1 U i) (subst_type_nat T2 U i))
101     | (Forall T1 T2) \Rightarrow (Forall (subst_type_nat T1 U i) (subst_type_nat T2 U (S i))) ].
102
103 (* substitutes 0-th dangling index in type T with type U *)
104 let rec subst_type_O T U  \def subst_type_nat T U O. 
105
106 (* substitutes 0-th dangling index in term t with term u *)
107 let rec subst_term_O t u  \def
108   let rec aux t0 i \def
109     match t0 with
110     [ (Var n) \Rightarrow match (eqb n i) with
111       [ true \Rightarrow u
112       | false \Rightarrow t0]
113     | (Free X) \Rightarrow t0
114     | (Abs T1 t1) \Rightarrow (Abs T1 (aux t1 (S i)))
115     | (App t1 t2) \Rightarrow (App (aux t1 i) (aux t2 i))
116     | (TAbs T1 t1) \Rightarrow (TAbs T1 (aux t1 (S i)))
117     | (TApp t1 T1) \Rightarrow (TApp (aux t1 i) T1) ]
118   in aux t O.
119
120 (* substitutes 0-th dangling index in term T, which shall be a TVar,
121    with type U *)
122 let rec subst_term_tO t T  \def
123   let rec aux t0 i \def
124     match t0 with
125     [ (Var n) \Rightarrow t0 
126     | (Free X) \Rightarrow t0
127     | (Abs T1 t1) \Rightarrow (Abs (subst_type_nat T1 T i) (aux t1 (S i)))
128     | (App t1 t2) \Rightarrow (App (aux t1 i) (aux t2 i))
129     | (TAbs T1 t1) \Rightarrow (TAbs (subst_type_nat T1 T i) (aux t1 (S i)))
130     | (TApp t1 T1) \Rightarrow (TApp (aux t1 i) (subst_type_nat T1 T i)) ]
131   in aux t O.
132
133 (* substitutes (TFree X) in type T with type U *)
134 let rec subst_type_tfree_type T X U on T \def
135   match T with
136     [ (TVar n) \Rightarrow T
137     | (TFree Y) \Rightarrow match (eqb X Y) with
138       [ true \Rightarrow U
139       | false \Rightarrow T ]
140     | Top \Rightarrow T
141     | (Arrow T1 T2) \Rightarrow (Arrow (subst_type_tfree_type T1 X U) 
142                                        (subst_type_tfree_type T2 X U))
143     | (Forall T1 T2) \Rightarrow (Forall (subst_type_tfree_type T1 X U) 
144                                        (subst_type_tfree_type T2 X U)) ].
145             
146 (*** height of T's syntactic tree ***)
147
148 let rec t_len T \def
149   match T with
150      [(TVar n) \Rightarrow (S O)
151      |(TFree X) \Rightarrow (S O)
152      |Top \Rightarrow (S O)
153      |(Arrow T1 T2) \Rightarrow (S (max (t_len T1) (t_len T2)))
154      |(Forall T1 T2) \Rightarrow (S (max (t_len T1) (t_len T2)))].
155
156 definition head \def
157   \lambda G:(list bound).match G with
158     [ nil \Rightarrow (mk_bound false O Top)
159     | (cons b H) \Rightarrow b].
160
161 definition head_nat \def
162   \lambda G:(list nat).match G with
163     [ nil \Rightarrow O
164     | (cons n H) \Rightarrow n].
165
166 (*** definitions about lists ***)
167
168 (* var binding is in env judgement *)                
169 definition var_bind_in_env : bound \to Env \to Prop \def
170   \lambda b,G.(in_list bound b G).
171
172 definition fv_env : (list bound) \to (list nat) \def
173   \lambda G.(map ? ? (\lambda b.match b with
174       [(mk_bound B X T) \Rightarrow X]) G).
175
176 (* variable is in env judgement *)              
177 definition var_in_env : nat \to Env \to Prop \def
178   \lambda x,G.(in_list nat x (fv_env G)).
179   
180 definition var_type_in_env : nat \to Env \to Prop \def
181   \lambda x,G.\exists T.(var_bind_in_env (mk_bound true x T) G).
182
183 let rec fv_type T \def
184   match T with
185     [(TVar n) \Rightarrow []
186     |(TFree x) \Rightarrow [x]
187     |Top \Rightarrow []
188     |(Arrow U V) \Rightarrow ((fv_type U) @ (fv_type V))
189     |(Forall U V) \Rightarrow ((fv_type U) @ (fv_type V))].
190
191 (*** Type Well-Formedness judgement ***)
192
193 inductive WFType : Env \to Typ \to Prop \def
194   | WFT_TFree : \forall X,G.(in_list ? X (fv_env G)) 
195                 \to (WFType G (TFree X))
196   | WFT_Top : \forall G.(WFType G Top)
197   | WFT_Arrow : \forall G,T,U.(WFType G T) \to (WFType G U) \to 
198                 (WFType G (Arrow T U))
199   | WFT_Forall : \forall G,T,U.(WFType G T) \to
200                  (\forall X:nat.
201                     (\lnot (in_list ? X (fv_env G))) \to
202                     (\lnot (in_list ? X (fv_type U))) \to
203                     (WFType ((mk_bound true X T) :: G) 
204                        (subst_type_O U (TFree X)))) \to 
205                  (WFType G (Forall T U)).
206
207 (*** Environment Well-Formedness judgement ***)
208
209 inductive WFEnv : Env \to Prop \def
210   | WFE_Empty : (WFEnv Empty)
211   | WFE_cons : \forall B,X,T,G.(WFEnv G) \to 
212                \lnot (in_list ? X (fv_env G)) \to
213                   (WFType G T) \to (WFEnv ((mk_bound B X T) :: G)).
214             
215 (*** Subtyping judgement ***)              
216 inductive JSubtype : Env \to Typ \to Typ \to Prop \def
217   | SA_Top : \forall G:Env.\forall T:Typ.(WFEnv G) \to
218              (WFType G T) \to (JSubtype G T Top)
219   | SA_Refl_TVar : \forall G:Env.\forall X:nat.(WFEnv G) \to (var_in_env X G) 
220                    \to (JSubtype G (TFree X) (TFree X))
221   | SA_Trans_TVar : \forall G:Env.\forall X:nat.\forall T:Typ.
222                     \forall U:Typ.
223                     (var_bind_in_env (mk_bound true X U) G) \to
224                     (JSubtype G U T) \to (JSubtype G (TFree X) T)
225   | SA_Arrow : \forall G:Env.\forall S1,S2,T1,T2:Typ.
226                (JSubtype G T1 S1) \to (JSubtype G S2 T2) \to
227                (JSubtype G (Arrow S1 S2) (Arrow T1 T2))
228   | SA_All : \forall G:Env.\forall S1,S2,T1,T2:Typ.
229              (JSubtype G T1 S1) \to
230              (\forall X:nat.\lnot (var_in_env X G) \to
231                 (JSubtype ((mk_bound true X T1) :: G) 
232                    (subst_type_O S2 (TFree X)) (subst_type_O T2 (TFree X)))) \to
233              (JSubtype G (Forall S1 S2) (Forall T1 T2)).
234
235 (*** Typing judgement ***)
236 inductive JType : Env \to Term \to Typ \to Prop \def
237   | T_Var : \forall G:Env.\forall x:nat.\forall T:Typ.
238             (WFEnv G) \to (var_bind_in_env (mk_bound false x T) G) \to
239             (JType G (Free x) T)
240   | T_Abs : \forall G.\forall T1,T2:Typ.\forall t2:Term.
241             \forall x:nat.
242             (JType ((mk_bound false x T1)::G) (subst_term_O t2 (Free x)) T2) \to
243             (JType G (Abs T1 t2) (Arrow T1 T2))
244   | T_App : \forall G.\forall t1,t2:Term.\forall T2:Typ.
245             \forall T1:Typ.(JType G t1 (Arrow T1 T2)) \to (JType G t2 T1) \to
246             (JType G (App t1 t2) T2)
247   | T_TAbs : \forall G:Env.\forall T1,T2:Typ.\forall t2:Term.
248              \forall X:nat.
249              (JType ((mk_bound true X T1)::G) 
250              (subst_term_tO t2 (TFree X)) (subst_type_O T2 (TFree X)))
251              \to (JType G (TAbs T1 t2) (Forall T1 T2))
252   | T_TApp : \forall G:Env.\forall t1:Term.\forall T2,T12:Typ.
253              \forall X:nat.\forall T11:Typ.
254              (JType G t1 (Forall T11 (subst_type_tfree_type T12 X (TVar O)))) \to 
255              (JSubtype G T2 T11)
256              \to (JType G (TApp t1 T2) (subst_type_tfree_type T12 X T2))
257   | T_Sub : \forall G:Env.\forall t:Term.\forall T:Typ.
258             \forall S:Typ.(JType G t S) \to (JSubtype G S T) \to (JType G t T).
259
260 (*** definitions about swaps ***)
261
262 let rec swap_Typ u v T on T \def
263   match T with
264      [(TVar n) \Rightarrow (TVar n)
265      |(TFree X) \Rightarrow (TFree (swap u v X))
266      |Top \Rightarrow Top
267      |(Arrow T1 T2) \Rightarrow (Arrow (swap_Typ u v T1) (swap_Typ u v T2))
268      |(Forall T1 T2) \Rightarrow (Forall (swap_Typ u v T1) (swap_Typ u v T2))].
269      
270 definition swap_bound : nat \to nat \to bound \to bound \def
271   \lambda u,v,b.match b with
272      [(mk_bound B X T) \Rightarrow (mk_bound B (swap u v X) (swap_Typ u v T))].
273
274 definition swap_Env : nat \to nat \to Env \to Env \def
275   \lambda u,v,G.(map ? ? (\lambda b.(swap_bound u v b)) G). 
276
277 (****** PROOFS ********)
278
279 lemma subst_O_nat : \forall T,U.((subst_type_O T U) = (subst_type_nat T U O)).
280 intros;elim T;simplify;reflexivity;
281 qed.
282
283 (*** theorems about lists ***)
284
285 (* FIXME: these definitions shouldn't be part of the poplmark challenge
286    - use destruct instead, when hopefully it will get fixed... *) 
287
288 lemma inj_head : \forall h1,h2:bound.\forall t1,t2:Env.
289                  ((h1::t1) = (h2::t2)) \to (h1 = h2).
290 intros.
291 lapply (eq_f ? ? head ? ? H).simplify in Hletin.assumption.
292 qed.
293
294 lemma inj_head_nat : \forall h1,h2:nat.\forall t1,t2:(list nat).
295                  ((h1::t1) = (h2::t2)) \to (h1 = h2).
296 intros.
297 lapply (eq_f ? ? head_nat ? ? H).simplify in Hletin.assumption.
298 qed.
299
300 lemma inj_tail : \forall A.\forall h1,h2:A.\forall t1,t2:(list A).
301                  ((h1::t1) = (h2::t2)) \to (t1 = t2).
302 intros.lapply (eq_f ? ? (tail ?) ? ? H).simplify in Hletin.assumption.
303 qed.
304
305 (* end of fixme *) 
306
307 lemma var_notinbG_notinG : \forall G,x,b.
308                            (\lnot (var_in_env x (b::G))) 
309                            \to \lnot (var_in_env x G).
310 intros 3.elim b.unfold.intro.elim H.unfold.simplify.constructor 2.exact H1.
311 qed.
312
313 lemma boundinenv_natinfv : \forall x,G.
314                               (\exists B,T.(in_list ? (mk_bound B x T) G)) \to
315                               (in_list ? x (fv_env G)).
316 intros 2;elim G
317   [elim H;elim H1;lapply (in_list_nil ? ? H2);elim Hletin
318   |elim H1;elim H2;inversion H3
319      [intros;rewrite < H4;simplify;apply in_Base
320      |intros;elim a3;simplify;apply in_Skip;
321       lapply (inj_tail ? ? ? ? ? H7);rewrite > Hletin in H;apply H;
322       apply ex_intro
323         [apply a
324         |apply ex_intro
325            [apply a1
326            |rewrite > H6;assumption]]]]
327 qed.
328
329 lemma nat_in_list_case : \forall G,H,n.(in_list nat n (H @ G)) \to 
330                                (in_list nat n G) \lor (in_list nat n H).
331 intros 3.elim H
332   [simplify in H1;left;assumption
333   |simplify in H2;inversion H2
334     [intros;lapply (inj_head_nat ? ? ? ? H4);rewrite > Hletin;
335      right;apply in_Base
336     |intros;lapply (inj_tail ? ? ? ? ? H6);rewrite < Hletin in H3;
337      rewrite > H5 in H1;lapply (H1 H3);elim Hletin1
338        [left;assumption|right;apply in_Skip;assumption]]]
339 qed.
340
341 lemma natinG_or_inH_to_natinGH : \forall G,H,n.
342                       (in_list nat n G) \lor (in_list nat n H) \to
343                       (in_list nat n (H @ G)).
344 intros.elim H1
345   [elim H
346      [simplify;assumption
347      |simplify;apply in_Skip;assumption]
348   |generalize in match H2;elim H2
349      [simplify;apply in_Base
350      |lapply (H4 H3);simplify;apply in_Skip;assumption]]
351 qed.
352
353 lemma natinfv_boundinenv : \forall x,G.(in_list ? x (fv_env G)) \to
354                               \exists B,T.(in_list ? (mk_bound B x T) G).
355 intros 2;elim G 0
356   [simplify;intro;lapply (in_list_nil ? ? H);elim Hletin
357   |intros 3;elim t;simplify in H1;inversion H1
358      [intros;rewrite < H2;simplify;apply ex_intro
359         [apply b
360         |apply ex_intro
361            [apply t1
362            |lapply (inj_head_nat ? ? ? ? H3);rewrite > H2;rewrite < Hletin;
363             apply in_Base]]
364      |intros;lapply (inj_tail ? ? ? ? ? H5);rewrite < Hletin in H2;
365       rewrite < H4 in H2;lapply (H H2);elim Hletin1;elim H6;apply ex_intro
366         [apply a2
367         |apply ex_intro
368            [apply a3
369            |apply in_Skip;rewrite < H4;assumption]]]]
370 qed.
371            
372 lemma incl_bound_fv : \forall l1,l2.(incl ? l1 l2) \to 
373                          (incl ? (fv_env l1) (fv_env l2)).
374 intros.unfold in H.unfold.intros.apply boundinenv_natinfv.
375 lapply (natinfv_boundinenv ? ? H1).elim Hletin.elim H2.apply ex_intro
376   [apply a
377   |apply ex_intro
378      [apply a1
379      |apply (H ? H3)]]
380 qed.
381
382 (* lemma incl_cons : \forall x,l1,l2.
383                   (incl bound l1 l2) \to (incl bound (x :: l1) (x :: l2)).
384 intros.unfold in H.unfold.intros.inversion H1
385   [intros;lapply (inj_head ? ? ? ? H3);rewrite > Hletin;apply in_Base
386   |intros;apply in_Skip;apply H;lapply (inj_tail ? ? ? ? ? H5);rewrite > Hletin;
387    assumption]
388 qed. *)
389
390 lemma incl_nat_cons : \forall x,l1,l2.
391                   (incl nat l1 l2) \to (incl nat (x :: l1) (x :: l2)).
392 intros.unfold in H.unfold.intros.inversion H1
393   [intros;lapply (inj_head_nat ? ? ? ? H3);rewrite > Hletin;apply in_Base
394   |intros;apply in_Skip;apply H;lapply (inj_tail ? ? ? ? ? H5);rewrite > Hletin;
395    assumption]
396 qed.
397
398 lemma boundin_envappend_case : \forall G,H,b.(var_bind_in_env b (H @ G)) \to 
399                                (var_bind_in_env b G) \lor (var_bind_in_env b H).
400 intros 3.elim H
401   [simplify in H1;left;assumption
402   |unfold in H2;inversion H2
403     [intros;simplify in H4;lapply (inj_head ? ? ? ? H4);rewrite > Hletin;
404      right;apply in_Base
405     |intros;simplify in H6;lapply (inj_tail ? ? ? ? ? H6);rewrite < Hletin in H3;
406      rewrite > H5 in H1;lapply (H1 H3);elim Hletin1
407        [left;assumption|right;apply in_Skip;assumption]]]
408 qed.
409
410 lemma varin_envappend_case: \forall G,H,x.(var_in_env x (H @ G)) \to
411                             (var_in_env x G) \lor (var_in_env x H).
412 intros 3.elim H 0
413   [simplify;intro;left;assumption
414   |intros 2;elim t;simplify in H2;inversion H2
415      [intros;lapply (inj_head_nat ? ? ? ? H4);rewrite > Hletin;right;
416       simplify;constructor 1
417      |intros;lapply (inj_tail ? ? ? ? ? H6);
418       lapply H1
419         [rewrite < H5;elim Hletin1
420            [left;assumption|right;simplify;constructor 2;assumption]
421         |unfold var_in_env;unfold fv_env;rewrite > Hletin;rewrite > H5;
422          assumption]]]
423 qed.
424
425 lemma boundinG_or_boundinH_to_boundinGH : \forall G,H,b.
426                       (var_bind_in_env b G) \lor (var_bind_in_env b H) \to
427                       (var_bind_in_env b (H @ G)).
428 intros.elim H1
429   [elim H
430      [simplify;assumption
431      |simplify;apply in_Skip;assumption]
432   |generalize in match H2;elim H2
433      [simplify;apply in_Base
434      |lapply (H4 H3);simplify;apply in_Skip;assumption]]
435 qed. 
436
437
438 lemma varinG_or_varinH_to_varinGH : \forall G,H,x.
439                           (var_in_env x G) \lor (var_in_env x H) \to
440                           (var_in_env x (H @ G)).
441 intros.elim H1 0
442   [elim H
443      [simplify;assumption
444      |elim t;simplify;constructor 2;apply (H2 H3)]
445   |elim H 0
446      [simplify;intro;lapply (in_list_nil nat x H2);elim Hletin
447      |intros 2;elim t;simplify in H3;inversion H3
448         [intros;lapply (inj_head_nat ? ? ? ? H5);rewrite > Hletin;simplify;
449          constructor 1
450         |intros;simplify;constructor 2;rewrite < H6;apply H2;
451          lapply (inj_tail ? ? ? ? ? H7);rewrite > H6;unfold;unfold fv_env;
452          rewrite > Hletin;assumption]]]
453 qed.
454
455 lemma varbind_to_append : \forall G,b.(var_bind_in_env b G) \to
456                           \exists G1,G2.(G = (G2 @ (b :: G1))).
457 intros.generalize in match H.elim H
458   [apply ex_intro [apply l|apply ex_intro [apply Empty|reflexivity]]
459   |lapply (H2 H1);elim Hletin;elim H4;rewrite > H5;
460    apply ex_intro 
461      [apply a2|apply ex_intro [apply (a1 :: a3)|simplify;reflexivity]]]
462 qed.
463   
464
465 lemma WFT_env_incl : \forall G,T.(WFType G T) \to
466                      \forall H.(incl ? (fv_env G) (fv_env H)) \to (WFType H T).
467 intros 4.generalize in match H1.elim H
468   [apply WFT_TFree;unfold in H3;apply (H3 ? H2)
469   |apply WFT_Top
470   |apply WFT_Arrow [apply (H3 ? H6)|apply (H5 ? H6)]
471   |apply WFT_Forall 
472      [apply (H3 ? H6)
473      |intros;apply H5
474         [unfold;intro;unfold in H7;apply H7;unfold in H6;apply(H6 ? H9)
475         |assumption
476         |simplify;apply (incl_nat_cons ? ? ? H6)]]]
477 qed.
478
479 lemma fv_env_extends : \forall H,x,B,C,T,U,G.
480                           (fv_env (H @ ((mk_bound B x T) :: G))) = 
481                           (fv_env (H @ ((mk_bound C x U) :: G))).
482 intros;elim H
483   [simplify;reflexivity
484   |elim t;simplify;rewrite > H1;reflexivity]
485 qed.
486
487 lemma lookup_env_extends : \forall G,H,B,C,D,T,U,V,x,y.
488             (in_list ? (mk_bound D y V) (H @ ((mk_bound C x U) :: G))) \to
489             (y \neq x) \to
490             (in_list ? (mk_bound D y V) (H @ ((mk_bound B x T) :: G))).
491 intros 10;elim H
492   [simplify in H1;(*FIXME*)generalize in match H1;intro;inversion H1
493      [intros;lapply (inj_head ? ? ? ? H5);rewrite < H4 in Hletin;
494       destruct Hletin;absurd (y = x) [symmetry;assumption|assumption]
495      |intros;simplify;lapply (inj_tail ? ? ? ? ? H7);rewrite > Hletin;
496       apply in_Skip;assumption]
497   |(*FIXME*)generalize in match H2;intro;inversion H2
498      [intros;simplify in H6;lapply (inj_head ? ? ? ? H6);rewrite > Hletin;
499       simplify;apply in_Base
500      |simplify;intros;lapply (inj_tail ? ? ? ? ? H8);rewrite > Hletin in H1;
501       rewrite > H7 in H1;apply in_Skip;apply (H1 H5 H3)]]
502 qed.
503
504
505 (*** theorems about swaps ***)
506        
507 lemma fv_subst_type_nat : \forall x,T,y,n.(in_list ? x (fv_type T)) \to
508                          (in_list ? x (fv_type (subst_type_nat T (TFree y) n))).
509 intros 3;elim T 0
510   [intros;simplify in H;elim (in_list_nil ? ? H)
511   |2,3:simplify;intros;assumption
512   |*:intros;simplify in H2;elim (nat_in_list_case ? ? ? H2)
513      [1,3:simplify;apply natinG_or_inH_to_natinGH;left;apply (H1 ? H3)
514      |*:simplify;apply natinG_or_inH_to_natinGH;right;apply (H ? H3)]]
515 qed.
516
517 lemma fv_subst_type_O : \forall x,T,y.(in_list ? x (fv_type T)) \to
518                          (in_list ? x (fv_type (subst_type_O T (TFree y)))).
519 intros;rewrite > subst_O_nat;apply (fv_subst_type_nat ? ? ? ? H);
520 qed.
521
522 lemma swap_Typ_inv : \forall u,v,T.(swap_Typ u v (swap_Typ u v T)) = T.
523 intros;elim T
524   [1,3:simplify;reflexivity
525   |simplify;rewrite > swap_inv;reflexivity
526   |*:simplify;rewrite > H;rewrite > H1;reflexivity]
527 qed.
528
529 lemma swap_Typ_not_free : \forall u,v,T.\lnot (in_list ? u (fv_type T)) \to
530                       \lnot (in_list ? v (fv_type T)) \to (swap_Typ u v T) = T.
531 intros 3;elim T 0
532   [1,3:intros;simplify;reflexivity
533   |simplify;intros;cut (n \neq u \land n \neq v)
534      [elim Hcut;rewrite > (swap_other ? ? ? H2 H3);reflexivity
535      |split
536         [unfold;intro;apply H;rewrite > H2;apply in_Base
537         |unfold;intro;apply H1;rewrite > H2;apply in_Base]]
538   |*:simplify;intros;cut ((\lnot (in_list ? u (fv_type t)) \land
539                          \lnot (in_list ? u (fv_type t1))) \land
540                         (\lnot (in_list ? v (fv_type t)) \land
541                          \lnot (in_list ? v (fv_type t1))))
542      [1,3:elim Hcut;elim H4;elim H5;clear Hcut H4 H5;rewrite > (H H6 H8);
543       rewrite > (H1 H7 H9);reflexivity
544      |*:split
545         [1,3:split;unfold;intro;apply H2;apply natinG_or_inH_to_natinGH;auto
546         |*:split;unfold;intro;apply H3;apply natinG_or_inH_to_natinGH;auto]]]
547 qed.
548         
549 lemma subst_type_nat_swap : \forall u,v,T,X,m.
550          (swap_Typ u v (subst_type_nat T (TFree X) m)) =
551          (subst_type_nat (swap_Typ u v T) (TFree (swap u v X)) m).
552 intros 4;elim T
553   [simplify;elim (eqb_case n m);rewrite > H;simplify;reflexivity
554   |2,3:simplify;reflexivity
555   |*:simplify;rewrite > H;rewrite > H1;reflexivity]
556 qed.
557
558 lemma subst_type_O_swap : \forall u,v,T,X.
559          (swap_Typ u v (subst_type_O T (TFree X))) =
560          (subst_type_O (swap_Typ u v T) (TFree (swap u v X))).
561 intros 4;rewrite > (subst_O_nat (swap_Typ u v T));rewrite > (subst_O_nat T);
562 apply subst_type_nat_swap;
563 qed.
564
565 lemma in_fv_type_swap : \forall u,v,x,T.((in_list ? x (fv_type T)) \to
566               (in_list ? (swap u v x) (fv_type (swap_Typ u v T)))) \land
567              ((in_list ? (swap u v x) (fv_type (swap_Typ u v T))) \to
568               (in_list ? x (fv_type T))).
569 intros;split
570   [elim T 0
571      [1,3:simplify;intros;elim (in_list_nil ? ? H)
572      |simplify;intros;cut (x = n)
573         [rewrite > Hcut;apply in_Base
574         |inversion H
575            [intros;lapply (inj_head_nat ? ? ? ? H2);rewrite > Hletin;
576             reflexivity
577            |intros;lapply (inj_tail ? ? ? ? ? H4);rewrite < Hletin in H1;
578             elim (in_list_nil ? ? H1)]]
579      |*:simplify;intros;elim (nat_in_list_case ? ? ? H2)
580         [1,3:apply natinG_or_inH_to_natinGH;left;apply (H1 H3)
581         |*:apply natinG_or_inH_to_natinGH;right;apply (H H3)]]
582   |elim T 0
583      [1,3:simplify;intros;elim (in_list_nil ? ? H)
584      |simplify;intros;cut ((swap u v x) = (swap u v n))
585         [lapply (swap_inj ? ? ? ? Hcut);rewrite > Hletin;apply in_Base
586         |inversion H
587            [intros;lapply (inj_head_nat ? ? ? ? H2);rewrite > Hletin;
588             reflexivity
589            |intros;lapply (inj_tail ? ? ? ? ? H4);rewrite < Hletin in H1;
590             elim (in_list_nil ? ? H1)]]
591      |*:simplify;intros;elim (nat_in_list_case ? ? ? H2)
592         [1,3:apply natinG_or_inH_to_natinGH;left;apply (H1 H3)
593         |*:apply natinG_or_inH_to_natinGH;right;apply (H H3)]]]
594 qed.
595         
596 lemma lookup_swap : \forall x,u,v,T,B,G.(in_list ? (mk_bound B x T) G) \to
597     (in_list ? (mk_bound B (swap u v x) (swap_Typ u v T)) (swap_Env u v G)).
598 intros 6;elim G 0
599   [intros;elim (in_list_nil ? ? H)
600   |intro;elim t;simplify;inversion H1
601      [intros;lapply (inj_head ? ? ? ? H3);rewrite < H2 in Hletin;
602       destruct Hletin;rewrite > Hcut;rewrite > Hcut1;rewrite > Hcut2;
603       apply in_Base
604      |intros;lapply (inj_tail ? ? ? ? ? H5);rewrite < Hletin in H2;
605       rewrite < H4 in H2;apply in_Skip;apply (H H2)]]
606 qed.
607
608 lemma in_FV_subst : \forall x,T,U,n.(in_list ? x (fv_type T)) \to
609                                 (in_list ? x (fv_type (subst_type_nat T U n))).
610 intros 3;elim T
611   [simplify in H;inversion H
612      [intros;lapply (sym_eq ? ? ? H2);absurd (a::l = [])
613         [assumption|apply nil_cons]
614      |intros;lapply (sym_eq ? ? ? H4);absurd (a1::l = [])
615         [assumption|apply nil_cons]]
616   |2,3:simplify;simplify in H;assumption
617   |*:simplify in H2;simplify;apply natinG_or_inH_to_natinGH;
618    lapply (nat_in_list_case ? ? ? H2);elim Hletin
619      [1,3:left;apply (H1 ? H3)
620      |*:right;apply (H ? H3)]]
621 qed.
622
623 lemma in_dom_swap : \forall u,v,x,G.
624                        ((in_list ? x (fv_env G)) \to 
625                        (in_list ? (swap u v x) (fv_env (swap_Env u v G)))) \land
626                        ((in_list ? (swap u v x) (fv_env (swap_Env u v G))) \to
627                        (in_list ? x (fv_env G))).
628 intros;split
629   [elim G 0
630      [simplify;intro;elim (in_list_nil ? ? H)
631      |intro;elim t 0;simplify;intros;inversion H1
632         [intros;lapply (inj_head_nat ? ? ? ? H3);rewrite > Hletin;apply in_Base
633         |intros;lapply (inj_tail ? ? ? ? ? H5);rewrite < Hletin in H2;
634          rewrite > H4 in H;apply in_Skip;apply (H H2)]]
635   |elim G 0
636      [simplify;intro;elim (in_list_nil ? ? H)
637      |intro;elim t 0;simplify;intros;inversion H1
638         [intros;lapply (inj_head_nat ? ? ? ? H3);rewrite < H2 in Hletin;
639          lapply (swap_inj ? ? ? ? Hletin);rewrite > Hletin1;apply in_Base
640         |intros;lapply (inj_tail ? ? ? ? ? H5);rewrite < Hletin in H2;
641          rewrite > H4 in H;apply in_Skip;apply (H H2)]]]
642 qed.
643
644 (*** lemma on fresh names ***)
645
646 lemma fresh_name : \forall l:(list nat).\exists n.\lnot (in_list ? n l).
647 cut (\forall l:(list nat).\exists n.\forall m.
648         (n \leq m) \to \lnot (in_list ? m l))
649   [intros;lapply (Hcut l);elim Hletin;apply ex_intro
650      [apply a
651      |apply H;constructor 1]
652   |intros;elim l
653     [apply ex_intro 
654        [apply O
655        |intros;unfold;intro;inversion H1
656           [intros;lapply (sym_eq ? ? ? H3);absurd (a::l1 = [])
657              [assumption|apply nil_cons]
658           |intros;lapply (sym_eq ? ? ? H5);absurd (a1::l1 = [])
659              [assumption|apply nil_cons]]]
660     |elim H;lapply (decidable_eq_nat a t);elim Hletin
661        [apply ex_intro
662           [apply (S a)
663           |intros;unfold;intro;inversion H4
664              [intros;lapply (inj_head_nat ? ? ? ? H6);rewrite < Hletin1 in H5;
665               rewrite < H2 in H5;rewrite > H5 in H3;
666               apply (not_le_Sn_n ? H3)
667              |intros;lapply (inj_tail ? ? ? ? ? H8);rewrite < Hletin1 in H5;
668               rewrite < H7 in H5;
669               apply (H1 m ? H5);lapply (le_S ? ? H3);
670               apply (le_S_S_to_le ? ? Hletin2)]]
671        |cut ((leb a t) = true \lor (leb a t) = false)
672           [elim Hcut
673              [apply ex_intro
674                 [apply (S t)
675                 |intros;unfold;intro;inversion H5
676                    [intros;lapply (inj_head_nat ? ? ? ? H7);rewrite > H6 in H4;
677                     rewrite < Hletin1 in H4;apply (not_le_Sn_n ? H4)
678                    |intros;lapply (inj_tail ? ? ? ? ? H9);
679                     rewrite < Hletin1 in H6;lapply (H1 a1)
680                       [apply (Hletin2 H6)
681                       |lapply (leb_to_Prop a t);rewrite > H3 in Hletin2;
682                        simplify in Hletin2;rewrite < H8;
683                        apply (trans_le ? ? ? Hletin2);
684                        apply (trans_le ? ? ? ? H4);constructor 2;constructor 1]]]
685              |apply ex_intro
686                 [apply a
687                 |intros;lapply (leb_to_Prop a t);rewrite > H3 in Hletin1;
688                  simplify in Hletin1;lapply (not_le_to_lt ? ? Hletin1);
689                  unfold in Hletin2;unfold;intro;inversion H5
690                    [intros;lapply (inj_head_nat ? ? ? ? H7);
691                     rewrite < Hletin3 in H6;rewrite > H6 in H4;
692                     apply (Hletin1 H4)
693                    |intros;lapply (inj_tail ? ? ? ? ? H9);
694                     rewrite < Hletin3 in H6;rewrite < H8 in H6;
695                     apply (H1 ? H4 H6)]]]
696           |elim (leb a t);auto]]]]
697 qed.
698
699 (*** lemmas on well-formedness ***)
700
701 lemma fv_WFT : \forall T,x,G.(WFType G T) \to (in_list ? x (fv_type T)) \to
702                   (in_list ? x (fv_env G)).
703 intros 4.elim H
704   [simplify in H2;inversion H2
705      [intros;lapply (inj_head_nat ? ? ? ? H4);rewrite < Hletin;assumption
706      |intros;lapply (inj_tail ? ? ? ? ? H6);rewrite < Hletin in H3;
707       inversion H3
708         [intros;lapply (sym_eq ? ? ? H8);absurd (a2 :: l2 = []) 
709            [assumption|apply nil_cons]
710         |intros;lapply (sym_eq ? ? ? H10);
711             absurd (a3 :: l2 = []) [assumption|apply nil_cons]]]
712   |simplify in H1;lapply (in_list_nil ? x H1);elim Hletin
713   |simplify in H5;lapply (nat_in_list_case ? ? ? H5);elim Hletin
714      [apply (H4 H6)
715      |apply (H2 H6)]
716   |simplify in H5;lapply (nat_in_list_case ? ? ? H5);elim Hletin
717      [lapply (fresh_name ((fv_type t1) @ (fv_env e)));elim Hletin1;
718       cut ((\lnot (in_list ? a (fv_type t1))) \land
719            (\lnot (in_list ? a (fv_env e))))
720         [elim Hcut;lapply (H4 ? H9 H8)
721            [cut (x \neq a)
722               [simplify in Hletin2;
723                (* FIXME trick *);generalize in match Hletin2;intro;
724                inversion Hletin2
725                  [intros;lapply (inj_head_nat ? ? ? ? H12);
726                   rewrite < Hletin3 in H11;lapply (Hcut1 H11);elim Hletin4
727                  |intros;lapply (inj_tail ? ? ? ? ? H14);rewrite > Hletin3;
728                   assumption]
729               |unfold;intro;apply H8;rewrite < H10;assumption]
730            |rewrite > subst_O_nat;apply in_FV_subst;assumption]
731         |split
732            [unfold;intro;apply H7;apply natinG_or_inH_to_natinGH;right;
733             assumption
734            |unfold;intro;apply H7;apply natinG_or_inH_to_natinGH;left;
735             assumption]]
736      |apply (H2 H6)]]
737 qed.
738            
739 lemma WFE_consG_to_WFT : \forall G.\forall b,X,T.
740                          (WFEnv ((mk_bound b X T)::G)) \to (WFType G T).
741 intros.
742 inversion H
743   [intro;reduce in H1;destruct H1
744   |intros;lapply (inj_head ? ? ? ? H5);lapply (inj_tail ? ? ? ? ? H5);
745    destruct Hletin;rewrite > Hletin1;rewrite > Hcut2;assumption]
746 qed.
747          
748 lemma WFE_consG_WFE_G : \forall G.\forall b.
749                          (WFEnv (b::G)) \to (WFEnv G).
750 intros.
751 inversion H
752   [intro;reduce in H1;destruct H1
753   |intros;lapply (inj_tail ? ? ? ? ? H5);rewrite > Hletin;assumption]
754 qed.
755
756 (* silly, but later useful *)
757
758 lemma env_append_weaken : \forall G,H.(WFEnv (H @ G)) \to
759                              (incl ? G (H @ G)).
760 intros 2;elim H
761   [simplify;unfold;intros;assumption
762   |simplify in H2;simplify;unfold;intros;apply in_Skip;apply H1
763      [apply (WFE_consG_WFE_G ? ? H2)
764      |assumption]]
765 qed.
766
767 lemma WFT_swap : \forall u,v,G,T.(WFType G T) \to
768                     (WFType (swap_Env u v G) (swap_Typ u v T)).
769 intros.elim H
770   [simplify;apply WFT_TFree;lapply (natinfv_boundinenv ? ? H1);elim Hletin;
771    elim H2;apply boundinenv_natinfv;apply ex_intro
772      [apply a
773      |apply ex_intro 
774         [apply (swap_Typ u v a1)
775         |apply lookup_swap;assumption]]
776   |simplify;apply WFT_Top
777   |simplify;apply WFT_Arrow
778      [assumption|assumption]
779   |simplify;apply WFT_Forall
780      [assumption
781      |intros;rewrite < (swap_inv u v);
782       cut (\lnot (in_list ? (swap u v X) (fv_type t1)))
783         [cut (\lnot (in_list ? (swap u v X) (fv_env e)))
784            [generalize in match (H4 ? Hcut1 Hcut);simplify;
785             rewrite > subst_type_O_swap;intro;assumption
786            |lapply (in_dom_swap u v (swap u v X) e);elim Hletin;unfold;
787             intros;lapply (H7 H9);rewrite > (swap_inv u v) in Hletin1;
788             apply (H5 Hletin1)] 
789         |generalize in match (in_fv_type_swap u v (swap u v X) t1);intros;
790          elim H7;unfold;intro;lapply (H8 H10);
791          rewrite > (swap_inv u v) in Hletin;apply (H6 Hletin)]]]
792 qed.
793
794 lemma WFE_swap : \forall u,v,G.(WFEnv G) \to (WFEnv (swap_Env u v G)).
795 intros 3.elim G 0
796   [intro;simplify;assumption
797   |intros 2;elim t;simplify;constructor 2
798      [apply H;apply (WFE_consG_WFE_G ? ? H1)
799      |unfold;intro;lapply (in_dom_swap u v n l);elim Hletin;lapply (H4 H2);
800       (* FIXME trick *)generalize in match H1;intro;inversion H1
801         [intros;absurd ((mk_bound b n t1)::l = [])
802            [assumption|apply nil_cons]
803         |intros;lapply (inj_head ? ? ? ? H10);lapply (inj_tail ? ? ? ? ? H10);
804          destruct Hletin2;rewrite < Hcut1 in H8;rewrite < Hletin3 in H8;
805          apply (H8 Hletin1)]
806      |apply (WFT_swap u v l t1);inversion H1
807         [intro;absurd ((mk_bound b n t1)::l = [])
808            [assumption|apply nil_cons]
809         |intros;lapply (inj_head ? ? ? ? H6);lapply (inj_tail ? ? ? ? ? H6);
810          destruct Hletin;rewrite > Hletin1;rewrite > Hcut2;assumption]]]
811 qed.
812
813 (*** some exotic inductions and related lemmas ***) 
814
815 lemma not_t_len_lt_SO : \forall T.\lnot (t_len T) < (S O).
816 intros;elim T
817   [1,2,3:simplify;unfold;intro;unfold in H;elim (not_le_Sn_n ? H)
818   |*:simplify;unfold;rewrite > max_case;elim (leb (t_len t) (t_len t1))
819      [1,3:simplify in H2;apply H1;apply (trans_lt ? ? ? ? H2);unfold;constructor 1
820      |*:simplify in H2;apply H;apply (trans_lt ? ? ? ? H2);unfold;constructor 1]]
821 qed.
822
823 lemma t_len_gt_O : \forall T.(t_len T) > O.
824 intro;elim T
825   [1,2,3:simplify;unfold;unfold;constructor 1
826   |*:simplify;lapply (max_case (t_len t) (t_len t1));rewrite > Hletin;
827    elim (leb (t_len t) (t_len t1))
828      [1,3:simplify;unfold;unfold;constructor 2;unfold in H1;unfold in H1;assumption
829      |*:simplify;unfold;unfold;constructor 2;unfold in H;unfold in H;assumption]]
830 qed.
831
832 lemma Typ_len_ind : \forall P:Typ \to Prop.
833                        (\forall U.(\forall V.((t_len V) < (t_len U)) \to (P V))
834                            \to (P U))
835                        \to \forall T.(P T).
836 cut (\forall P:Typ \to Prop.
837         (\forall U.(\forall V.((t_len V) < (t_len U)) \to (P V))
838             \to (P U))
839         \to \forall T,n.(n = (t_len T)) \to (P T))                      
840   [intros;apply (Hcut ? H ? (t_len T));reflexivity
841   |intros 4;generalize in match T;apply (nat_elim1 n);intros;
842    generalize in match H2;elim t 
843      [1,2,3:apply H;intros;simplify in H4;elim (not_t_len_lt_SO ? H4)
844      |*:apply H;intros;apply (H1 (t_len V))
845         [1,3:rewrite > H5;assumption
846         |*:reflexivity]]]
847 qed.
848
849 lemma t_len_arrow1 : \forall T1,T2.(t_len T1) < (t_len (Arrow T1 T2)).
850 intros.simplify.
851 (* FIXME!!! BUG?!?! *)
852 cut ((max (t_len T1) (t_len T2)) = match (leb (t_len T1) (t_len T2)) with
853       [ false \Rightarrow (t_len T2)
854       | true \Rightarrow (t_len T1) ])
855   [rewrite > Hcut;cut ((leb (t_len T1) (t_len T2)) = false \lor
856                        (leb (t_len T1) (t_len T2)) = true)
857      [lapply (leb_to_Prop (t_len T1) (t_len T2));elim Hcut1
858         [rewrite > H;rewrite > H in Hletin;simplify;constructor 1
859         |rewrite > H;rewrite > H in Hletin;simplify;simplify in Hletin;
860          unfold;apply le_S_S;assumption]
861      |elim (leb (t_len T1) (t_len T2));auto]
862   |elim T1;simplify;reflexivity]
863 qed.
864
865 lemma t_len_arrow2 : \forall T1,T2.(t_len T2) < (t_len (Arrow T1 T2)).
866 intros.simplify.
867 (* FIXME!!! BUG?!?! *)
868 cut ((max (t_len T1) (t_len T2)) = match (leb (t_len T1) (t_len T2)) with
869       [ false \Rightarrow (t_len T2)
870       | true \Rightarrow (t_len T1) ])
871   [rewrite > Hcut;cut ((leb (t_len T1) (t_len T2)) = false \lor
872                        (leb (t_len T1) (t_len T2)) = true)
873      [lapply (leb_to_Prop (t_len T1) (t_len T2));elim Hcut1
874         [rewrite > H;rewrite > H in Hletin;simplify;simplify in Hletin;
875          lapply (not_le_to_lt ? ? Hletin);unfold in Hletin1;unfold;
876          constructor 2;assumption
877         |rewrite > H;simplify;unfold;constructor 1]
878      |elim (leb (t_len T1) (t_len T2));auto]
879   |elim T1;simplify;reflexivity]
880 qed.
881
882 lemma t_len_forall1 : \forall T1,T2.(t_len T1) < (t_len (Forall T1 T2)).
883 intros.simplify.
884 (* FIXME!!! BUG?!?! *)
885 cut ((max (t_len T1) (t_len T2)) = match (leb (t_len T1) (t_len T2)) with
886       [ false \Rightarrow (t_len T2)
887       | true \Rightarrow (t_len T1) ])
888   [rewrite > Hcut;cut ((leb (t_len T1) (t_len T2)) = false \lor
889                        (leb (t_len T1) (t_len T2)) = true)
890      [lapply (leb_to_Prop (t_len T1) (t_len T2));elim Hcut1
891         [rewrite > H;rewrite > H in Hletin;simplify;constructor 1
892         |rewrite > H;rewrite > H in Hletin;simplify;simplify in Hletin;
893          unfold;apply le_S_S;assumption]
894      |elim (leb (t_len T1) (t_len T2));auto]
895   |elim T1;simplify;reflexivity]
896 qed.
897
898 lemma t_len_forall2 : \forall T1,T2.(t_len T2) < (t_len (Forall T1 T2)).
899 intros.simplify.
900 (* FIXME!!! BUG?!?! *)
901 cut ((max (t_len T1) (t_len T2)) = match (leb (t_len T1) (t_len T2)) with
902       [ false \Rightarrow (t_len T2)
903       | true \Rightarrow (t_len T1) ])
904   [rewrite > Hcut;cut ((leb (t_len T1) (t_len T2)) = false \lor
905                        (leb (t_len T1) (t_len T2)) = true)
906      [lapply (leb_to_Prop (t_len T1) (t_len T2));elim Hcut1
907         [rewrite > H;rewrite > H in Hletin;simplify;simplify in Hletin;
908          lapply (not_le_to_lt ? ? Hletin);unfold in Hletin1;unfold;
909          constructor 2;assumption
910         |rewrite > H;simplify;unfold;constructor 1]
911      |elim (leb (t_len T1) (t_len T2));auto]
912   |elim T1;simplify;reflexivity]
913 qed.
914
915 lemma eq_t_len_TFree_subst : \forall T,n,X.(t_len T) = 
916                                          (t_len (subst_type_nat T (TFree X) n)).
917 intro.elim T
918   [simplify;elim (eqb n n1);simplify;reflexivity
919   |2,3:simplify;reflexivity
920   |simplify;lapply (H n X);lapply (H1 n X);rewrite < Hletin;rewrite < Hletin1;
921    reflexivity
922   |simplify;lapply (H n X);lapply (H1 (S n) X);rewrite < Hletin;
923    rewrite < Hletin1;reflexivity]
924 qed.
925
926 lemma swap_env_not_free : \forall u,v,G.(WFEnv G) \to 
927                                         \lnot (in_list ? u (fv_env G)) \to
928                                         \lnot (in_list ? v (fv_env G)) \to
929                                         (swap_Env u v G) = G.
930 intros 3.elim G 0
931   [simplify;intros;reflexivity
932   |intros 2;elim t 0;simplify;intros;lapply (notin_cons ? ? ? ? H2);
933    lapply (notin_cons ? ? ? ? H3);elim Hletin;elim Hletin1;
934    lapply (swap_other ? ? ? H4 H6);lapply (WFE_consG_to_WFT ? ? ? ? H1);
935    cut (\lnot (in_list ? u (fv_type t1)))
936      [cut (\lnot (in_list ? v (fv_type t1)))
937         [lapply (swap_Typ_not_free ? ? ? Hcut Hcut1);
938          lapply (WFE_consG_WFE_G ? ? H1);
939          lapply (H Hletin5 H5 H7);
940          rewrite > Hletin2;rewrite > Hletin4;rewrite > Hletin6;reflexivity
941         |unfold;intro;apply H7;
942          apply (fv_WFT ? ? ? Hletin3 H8)] 
943      |unfold;intro;apply H5;apply (fv_WFT ? ? ? Hletin3 H8)]]
944 qed.
945
946 (*** alternate "constructor" for universal types' well-formedness ***)
947
948 lemma WFT_Forall2 : \forall G,X,T,T1,T2.
949                        (WFEnv G) \to
950                        (WFType G T1) \to
951                        \lnot (in_list ? X (fv_type T2)) \to
952                        \lnot (in_list ? X (fv_env G)) \to
953                        (WFType ((mk_bound true X T)::G) 
954                           (subst_type_O T2 (TFree X))) \to
955                     (WFType G (Forall T1 T2)).
956 intros.apply WFT_Forall
957   [assumption
958   |intros;generalize in match (WFT_swap X X1 ? ? H4);simplify;
959    rewrite > swap_left;
960    rewrite > (swap_env_not_free X X1 G H H3 H5);
961    rewrite > subst_type_O_swap;rewrite > swap_left;
962    rewrite > (swap_Typ_not_free ? ? T2 H2 H6);
963    intro;apply (WFT_env_incl ? ? H7);unfold;simplify;intros;assumption]
964 qed.
965
966 (*** lemmas relating subtyping and well-formedness ***)
967
968 lemma JS_to_WFE : \forall G,T,U.(JSubtype G T U) \to (WFEnv G).
969 intros;elim H;assumption.
970 qed.
971
972 lemma JS_to_WFT : \forall G,T,U.(JSubtype G T U) \to ((WFType G T) \land 
973                                                       (WFType G U)).
974 intros;elim H
975   [split [assumption|apply WFT_Top]
976   |split;apply WFT_TFree;assumption
977   |split 
978      [apply WFT_TFree;apply boundinenv_natinfv;apply ex_intro
979         [apply true | apply ex_intro [apply t1 |assumption]]
980      |elim H3;assumption]
981   |elim H2;elim H4;split;apply WFT_Arrow;assumption
982   |elim H2;split
983      [lapply (fresh_name ((fv_env e) @ (fv_type t1)));
984       elim Hletin;cut ((\lnot (in_list ? a (fv_env e))) \land
985                        (\lnot (in_list ? a (fv_type t1))))
986         [elim Hcut;apply (WFT_Forall2 ? a t2 ? ? (JS_to_WFE ? ? ? H1) H6 H9 H8);
987          lapply (H4 ? H8);elim Hletin1;assumption
988         |split;unfold;intro;apply H7;apply natinG_or_inH_to_natinGH
989            [right;assumption
990            |left;assumption]]
991      |lapply (fresh_name ((fv_env e) @ (fv_type t3)));
992       elim Hletin;cut ((\lnot (in_list ? a (fv_env e))) \land
993                        (\lnot (in_list ? a (fv_type t3))))
994         [elim Hcut;apply (WFT_Forall2 ? a t2 ? ? (JS_to_WFE ? ? ? H1) H5 H9 H8);
995          lapply (H4 ? H8);elim Hletin1;assumption
996         |split;unfold;intro;apply H7;apply natinG_or_inH_to_natinGH
997            [right;assumption
998            |left;assumption]]]]
999 qed.
1000
1001 lemma JS_to_WFT1 : \forall G,T,U.(JSubtype G T U) \to (WFType G T).
1002 intros;lapply (JS_to_WFT ? ? ? H);elim Hletin;assumption.
1003 qed.
1004
1005 lemma JS_to_WFT2 : \forall G,T,U.(JSubtype G T U) \to (WFType G U).
1006 intros;lapply (JS_to_WFT ? ? ? H);elim Hletin;assumption.
1007 qed.
1008
1009 (*** lemma relating subtyping and swaps ***)
1010
1011 lemma JS_swap : \forall u,v,G,T,U.(JSubtype G T U) \to
1012                    (JSubtype (swap_Env u v G) (swap_Typ u v T) (swap_Typ u v U)).
1013 intros 6.elim H
1014   [simplify;apply SA_Top
1015      [apply WFE_swap;assumption
1016      |apply WFT_swap;assumption]
1017   |simplify;apply SA_Refl_TVar
1018      [apply WFE_swap;assumption
1019      |unfold in H2;unfold;lapply (in_dom_swap u v n e);elim Hletin;
1020       apply (H3 H2)]
1021   |simplify;apply SA_Trans_TVar
1022      [apply (swap_Typ u v t1)
1023      |apply lookup_swap;assumption
1024      |assumption]
1025   |simplify;apply SA_Arrow;assumption
1026   |simplify;apply SA_All
1027      [assumption
1028      |intros;lapply (H4 (swap u v X))
1029         [simplify in Hletin;rewrite > subst_type_O_swap in Hletin;
1030          rewrite > subst_type_O_swap in Hletin;rewrite > swap_inv in Hletin;
1031          assumption
1032         |unfold;intro;apply H5;unfold;
1033          lapply (in_dom_swap u v (swap u v X) e);
1034          elim Hletin;rewrite > swap_inv in H7;apply H7;assumption]]]
1035 qed.
1036
1037 lemma fresh_WFT : \forall x,G,T.(WFType G T) \to \lnot (in_list ? x (fv_env G))
1038                      \to \lnot (in_list ? x (fv_type T)).
1039 intros;unfold;intro;apply H1;apply (fv_WFT ? ? ? H H2);
1040 qed.
1041
1042 lemma fresh_subst_type_O : \forall x,T1,B,G,T,y.
1043                   (WFType ((mk_bound B x T1)::G) (subst_type_O T (TFree x))) \to
1044                   \lnot (in_list ? y (fv_env G)) \to (x \neq y) \to
1045                   \lnot (in_list ? y (fv_type T)).
1046 intros;unfold;intro;
1047 cut (in_list ? y (fv_env ((mk_bound B x T1) :: G)))
1048   [simplify in Hcut;inversion Hcut
1049      [intros;apply H2;lapply (inj_head_nat ? ? ? ? H5);rewrite < H4 in Hletin;
1050       assumption
1051      |intros;apply H1;rewrite > H6;lapply (inj_tail ? ? ? ? ? H7);
1052       rewrite > Hletin;assumption]
1053   |apply (fv_WFT (subst_type_O T (TFree x)) ? ? H);
1054    apply fv_subst_type_O;assumption]
1055 qed.
1056
1057 (*** alternate "constructor" for subtyping between universal types ***)
1058
1059 lemma SA_All2 : \forall G,S1,S2,T1,T2,X.(JSubtype G T1 S1) \to
1060                    \lnot (in_list ? X (fv_env G)) \to
1061                    \lnot (in_list ? X (fv_type S2)) \to
1062                    \lnot (in_list ? X (fv_type T2)) \to
1063                    (JSubtype ((mk_bound true X T1) :: G)
1064                       (subst_type_O S2 (TFree X))
1065                       (subst_type_O T2 (TFree X))) \to
1066                    (JSubtype G (Forall S1 S2) (Forall T1 T2)).
1067 intros;apply (SA_All ? ? ? ? ? H);intros;
1068 lapply (decidable_eq_nat X X1);elim Hletin
1069   [rewrite < H6;assumption
1070   |elim (JS_to_WFT ? ? ? H);elim (JS_to_WFT ? ? ? H4);
1071    cut (\lnot (in_list ? X1 (fv_type S2)))
1072      [cut (\lnot (in_list ? X1 (fv_type T2)))
1073         [cut (((mk_bound true X1 T1)::G) =
1074               (swap_Env X X1 ((mk_bound true X T1)::G)))
1075            [rewrite > Hcut2;
1076             cut (((subst_type_O S2 (TFree X1)) =
1077                    (swap_Typ X X1 (subst_type_O S2 (TFree X)))) \land
1078                  ((subst_type_O T2 (TFree X1)) =
1079                    (swap_Typ X X1 (subst_type_O T2 (TFree X)))))
1080               [elim Hcut3;rewrite > H11;rewrite > H12;apply JS_swap;
1081                assumption
1082               |split
1083                  [rewrite > (subst_type_O_swap X X1 S2 X);
1084                   rewrite > (swap_Typ_not_free X X1 S2 H2 Hcut); 
1085                   rewrite > swap_left;reflexivity
1086                  |rewrite > (subst_type_O_swap X X1 T2 X);
1087                   rewrite > (swap_Typ_not_free X X1 T2 H3 Hcut1); 
1088                   rewrite > swap_left;reflexivity]]
1089            |simplify;lapply (JS_to_WFE ? ? ? H);
1090             rewrite > (swap_env_not_free X X1 G Hletin1 H1 H5);
1091             cut ((\lnot (in_list ? X (fv_type T1))) \land
1092                  (\lnot (in_list ? X1 (fv_type T1))))
1093               [elim Hcut2;rewrite > (swap_Typ_not_free X X1 T1 H11 H12);
1094                rewrite > swap_left;reflexivity
1095               |split
1096                  [unfold;intro;apply H1;apply (fv_WFT T1 X G H7 H11)
1097                  |unfold;intro;apply H5;apply (fv_WFT T1 X1 G H7 H11)]]]
1098         |unfold;intro;apply H5;lapply (fv_WFT ? X1 ? H10)
1099            [inversion Hletin1
1100               [intros;simplify in H13;lapply (inj_head_nat ? ? ? ? H13);
1101                rewrite < H12 in Hletin2;lapply (H6 Hletin2);elim Hletin3
1102               |intros;simplify in H15;lapply (inj_tail ? ? ? ? ? H15);
1103                rewrite < Hletin2 in H12;rewrite < H14 in H12;lapply (H5 H12);
1104                elim Hletin3]
1105            |rewrite > subst_O_nat;apply in_FV_subst;assumption]]
1106      |unfold;intro;apply H5;lapply (fv_WFT ? X1 ? H9)
1107         [inversion Hletin1
1108            [intros;simplify in H13;lapply (inj_head_nat ? ? ? ? H13);
1109             rewrite < H12 in Hletin2;lapply (H6 Hletin2);elim Hletin3
1110            |intros;simplify in H15;lapply (inj_tail ? ? ? ? ? H15);
1111             rewrite < Hletin2 in H12;rewrite < H14 in H12;lapply (H5 H12);
1112             elim Hletin3]
1113         |rewrite > subst_O_nat;apply in_FV_subst;assumption]]]
1114 qed.
1115
1116 lemma WFE_Typ_subst : \forall H,x,B,C,T,U,G.
1117                       (WFEnv (H @ ((mk_bound B x T) :: G))) \to (WFType G U) \to
1118                       (WFEnv (H @ ((mk_bound C x U) :: G))).
1119 intros 7;elim H 0
1120   [simplify;intros;(*FIXME*)generalize in match H1;intro;inversion H1
1121      [intros;lapply (nil_cons ? G (mk_bound B x T));lapply (Hletin H4);
1122       elim Hletin1
1123      |intros;lapply (inj_tail ? ? ? ? ? H8);lapply (inj_head ? ? ? ? H8);
1124       destruct Hletin1;rewrite < Hletin in H6;rewrite < Hletin in H4;
1125       rewrite < Hcut1 in H6;apply (WFE_cons ? ? ? ? H4 H6 H2)]
1126   |intros;simplify;generalize in match H2;elim t;simplify in H4;
1127    inversion H4
1128      [intros;absurd (mk_bound b n t1::l@(mk_bound B x T::G)=Empty)
1129         [assumption
1130         |apply nil_cons]
1131      |intros;lapply (inj_tail ? ? ? ? ? H9);lapply (inj_head ? ? ? ? H9);
1132       destruct Hletin1;apply WFE_cons
1133         [apply H1
1134            [rewrite > Hletin;assumption
1135            |assumption]
1136         |rewrite > Hcut1;generalize in match H7;rewrite < Hletin;
1137          rewrite > (fv_env_extends ? x B C T U);intro;assumption
1138         |rewrite < Hletin in H8;rewrite > Hcut2;
1139          apply (WFT_env_incl ? ? H8);rewrite > (fv_env_extends ? x B C T U);
1140          unfold;intros;assumption]]]
1141 qed.
1142
1143 lemma t_len_pred: \forall T,m.(S (t_len T)) \leq m \to (t_len T) \leq (pred m).
1144 intros 2;elim m
1145   [elim (not_le_Sn_O ? H)
1146   |simplify;apply (le_S_S_to_le ? ? H1)]
1147 qed.
1148
1149 lemma pred_m_lt_m : \forall m,T.(t_len T) \leq m \to (pred m) < m.
1150 intros 2;elim m 0
1151   [elim T
1152      [4,5:simplify in H2;elim (not_le_Sn_O ? H2)
1153      |*:simplify in H;elim (not_le_Sn_n ? H)]
1154   |intros;simplify;unfold;constructor 1]
1155 qed.
1156
1157 lemma WFE_bound_bound : \forall B,x,T,U,G. (WFEnv G) \to
1158                                   (in_list ? (mk_bound B x T) G) \to
1159                                   (in_list ? (mk_bound B x U) G) \to T = U.
1160 intros 6;elim H
1161   [lapply (in_list_nil ? ? H1);elim Hletin
1162   |inversion H6
1163      [intros;rewrite < H7 in H8;lapply (inj_head ? ? ? ? H8);
1164       rewrite > Hletin in H5;inversion H5
1165         [intros;rewrite < H9 in H10;lapply (inj_head ? ? ? ? H10);
1166          destruct Hletin1;symmetry;assumption
1167         |intros;lapply (inj_tail ? ? ? ? ? H12);rewrite < Hletin1 in H9;
1168          rewrite < H11 in H9;lapply (boundinenv_natinfv x e)
1169            [destruct Hletin;rewrite < Hcut1 in Hletin2;lapply (H3 Hletin2);
1170             elim Hletin3
1171            |apply ex_intro
1172               [apply B|apply ex_intro [apply T|assumption]]]]
1173      |intros;lapply (inj_tail ? ? ? ? ? H10);rewrite < H9 in H7;
1174       rewrite < Hletin in H7;(*FIXME*)generalize in match H5;intro;inversion H5
1175         [intros;rewrite < H12 in H13;lapply (inj_head ? ? ? ? H13);
1176          destruct Hletin1;rewrite < Hcut1 in H7;
1177          lapply (boundinenv_natinfv n e)
1178            [lapply (H3 Hletin2);elim Hletin3
1179            |apply ex_intro
1180               [apply B|apply ex_intro [apply U|assumption]]]
1181         |intros;apply (H2 ? H7);rewrite > H14;lapply (inj_tail ? ? ? ? ? H15);
1182          rewrite > Hletin1;assumption]]]
1183 qed.         
1184