]> matita.cs.unibo.it Git - helm.git/blob - matita/library/Fsub/defn.ma
PoplMark challenge part 1a: new, shorter version w/o equivariance proofs.
[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 : Set \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 : Set \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 : Set \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 (****** PROOFS ********)
261
262 lemma subst_O_nat : \forall T,U.((subst_type_O T U) = (subst_type_nat T U O)).
263 intros;elim T;simplify;reflexivity;
264 qed.
265
266 (*** theorems about lists ***)
267
268 (* FIXME: these definitions shouldn't be part of the poplmark challenge
269    - use destruct instead, when hopefully it will get fixed... *) 
270
271 lemma inj_head : \forall h1,h2:bound.\forall t1,t2:Env.
272                  ((h1::t1) = (h2::t2)) \to (h1 = h2).
273 intros.
274 lapply (eq_f ? ? head ? ? H).simplify in Hletin.assumption.
275 qed.
276
277 lemma inj_head_nat : \forall h1,h2:nat.\forall t1,t2:(list nat).
278                  ((h1::t1) = (h2::t2)) \to (h1 = h2).
279 intros.
280 lapply (eq_f ? ? head_nat ? ? H).simplify in Hletin.assumption.
281 qed.
282
283 lemma inj_tail : \forall A.\forall h1,h2:A.\forall t1,t2:(list A).
284                  ((h1::t1) = (h2::t2)) \to (t1 = t2).
285 intros.lapply (eq_f ? ? (tail ?) ? ? H).simplify in Hletin.assumption.
286 qed.
287
288 (* end of fixme *) 
289
290 lemma boundinenv_natinfv : \forall x,G.
291                               (\exists B,T.(in_list ? (mk_bound B x T) G)) \to
292                               (in_list ? x (fv_env G)).
293 intros 2;elim G
294   [elim H;elim H1;lapply (in_list_nil ? ? H2);elim Hletin
295   |elim H1;elim H2;inversion H3
296      [intros;rewrite < H4;simplify;apply in_Base
297      |intros;elim a3;simplify;apply in_Skip;
298       lapply (inj_tail ? ? ? ? ? H7);rewrite > Hletin in H;apply H;
299       apply ex_intro
300         [apply a
301         |apply ex_intro
302            [apply a1
303            |rewrite > H6;assumption]]]]
304 qed.
305
306 lemma nat_in_list_case : \forall G,H,n.(in_list nat n (H @ G)) \to 
307                                (in_list nat n G) \lor (in_list nat n H).
308 intros 3.elim H
309   [simplify in H1;left;assumption
310   |simplify in H2;inversion H2
311     [intros;lapply (inj_head_nat ? ? ? ? H4);rewrite > Hletin;
312      right;apply in_Base
313     |intros;lapply (inj_tail ? ? ? ? ? H6);rewrite < Hletin in H3;
314      rewrite > H5 in H1;lapply (H1 H3);elim Hletin1
315        [left;assumption|right;apply in_Skip;assumption]]]
316 qed.
317
318 lemma natinG_or_inH_to_natinGH : \forall G,H,n.
319                       (in_list nat n G) \lor (in_list nat n H) \to
320                       (in_list nat n (H @ G)).
321 intros.elim H1
322   [elim H
323      [simplify;assumption
324      |simplify;apply in_Skip;assumption]
325   |generalize in match H2;elim H2
326      [simplify;apply in_Base
327      |lapply (H4 H3);simplify;apply in_Skip;assumption]]
328 qed.
329
330 lemma natinfv_boundinenv : \forall x,G.(in_list ? x (fv_env G)) \to
331                               \exists B,T.(in_list ? (mk_bound B x T) G).
332 intros 2;elim G 0
333   [simplify;intro;lapply (in_list_nil ? ? H);elim Hletin
334   |intros 3;elim t;simplify in H1;inversion H1
335      [intros;rewrite < H2;simplify;apply ex_intro
336         [apply b
337         |apply ex_intro
338            [apply t1
339            |lapply (inj_head_nat ? ? ? ? H3);rewrite > H2;rewrite < Hletin;
340             apply in_Base]]
341      |intros;lapply (inj_tail ? ? ? ? ? H5);rewrite < Hletin in H2;
342       rewrite < H4 in H2;lapply (H H2);elim Hletin1;elim H6;apply ex_intro
343         [apply a2
344         |apply ex_intro
345            [apply a3
346            |apply in_Skip;rewrite < H4;assumption]]]]
347 qed.
348
349 theorem varinT_varinT_subst : \forall X,Y,T.
350         (in_list ? X (fv_type T)) \to \forall n.
351         (in_list ? X (fv_type (subst_type_nat T (TFree Y) n))).
352 intros 3;elim T
353   [simplify in H;elim (in_list_nil ? ? H)
354   |simplify in H;simplify;assumption
355   |simplify in H;elim (in_list_nil ? ? H)
356   |simplify in H2;simplify;elim (nat_in_list_case ? ? ? H2);
357    apply natinG_or_inH_to_natinGH;
358      [left;apply (H1 H3)
359      |right;apply (H H3)]
360   |simplify in H2;simplify;elim (nat_in_list_case ? ? ? H2);
361    apply natinG_or_inH_to_natinGH;
362      [left;apply (H1 H3);
363      |right;apply (H H3)]]
364 qed.
365
366 lemma incl_bound_fv : \forall l1,l2.(incl ? l1 l2) \to 
367                          (incl ? (fv_env l1) (fv_env l2)).
368 intros.unfold in H.unfold.intros.apply boundinenv_natinfv.
369 lapply (natinfv_boundinenv ? ? H1).elim Hletin.elim H2.apply ex_intro
370   [apply a
371   |apply ex_intro
372      [apply a1
373      |apply (H ? H3)]]
374 qed.
375
376 lemma incl_nat_cons : \forall x,l1,l2.
377                   (incl nat l1 l2) \to (incl nat (x :: l1) (x :: l2)).
378 intros.unfold in H.unfold.intros.inversion H1
379   [intros;lapply (inj_head_nat ? ? ? ? H3);rewrite > Hletin;apply in_Base
380   |intros;apply in_Skip;apply H;lapply (inj_tail ? ? ? ? ? H5);rewrite > Hletin;
381    assumption]
382 qed.
383
384 lemma WFT_env_incl : \forall G,T.(WFType G T) \to
385                      \forall H.(incl ? (fv_env G) (fv_env H)) \to (WFType H T).
386 intros 3.elim H
387   [apply WFT_TFree;unfold in H3;apply (H3 ? H1)
388   |apply WFT_Top
389   |apply WFT_Arrow [apply (H2 ? H6)|apply (H4 ? H6)]
390   |apply WFT_Forall 
391      [apply (H2 ? H6)
392      |intros;apply H4
393         [unfold;intro;apply H7;apply(H6 ? H9)
394         |assumption
395         |simplify;apply (incl_nat_cons ? ? ? H6)]]]
396 qed.
397
398 lemma fv_env_extends : \forall H,x,B,C,T,U,G.
399                           (fv_env (H @ ((mk_bound B x T) :: G))) = 
400                           (fv_env (H @ ((mk_bound C x U) :: G))).
401 intros;elim H
402   [simplify;reflexivity
403   |elim t;simplify;rewrite > H1;reflexivity]
404 qed.
405
406 lemma lookup_env_extends : \forall G,H,B,C,D,T,U,V,x,y.
407             (in_list ? (mk_bound D y V) (H @ ((mk_bound C x U) :: G))) \to
408             (y \neq x) \to
409             (in_list ? (mk_bound D y V) (H @ ((mk_bound B x T) :: G))).
410 intros 10;elim H
411   [simplify in H1;(*FIXME*)generalize in match H1;intro;inversion H1
412      [intros;lapply (inj_head ? ? ? ? H5);rewrite < H4 in Hletin;
413       destruct Hletin;absurd (y = x) [symmetry;assumption|assumption]
414      |intros;simplify;lapply (inj_tail ? ? ? ? ? H7);rewrite > Hletin;
415       apply in_Skip;assumption]
416   |(*FIXME*)generalize in match H2;intro;inversion H2
417      [intros;simplify in H6;lapply (inj_head ? ? ? ? H6);rewrite > Hletin;
418       simplify;apply in_Base
419      |simplify;intros;lapply (inj_tail ? ? ? ? ? H8);rewrite > Hletin in H1;
420       rewrite > H7 in H1;apply in_Skip;apply (H1 H5 H3)]]
421 qed.
422
423 lemma in_FV_subst : \forall x,T,U,n.(in_list ? x (fv_type T)) \to
424                                 (in_list ? x (fv_type (subst_type_nat T U n))).
425 intros 3;elim T
426   [simplify in H;inversion H
427      [intros;lapply (sym_eq ? ? ? H2);absurd (a::l = [])
428         [assumption|apply nil_cons]
429      |intros;lapply (sym_eq ? ? ? H4);absurd (a1::l = [])
430         [assumption|apply nil_cons]]
431   |2,3:simplify;simplify in H;assumption
432   |*:simplify in H2;simplify;apply natinG_or_inH_to_natinGH;
433    lapply (nat_in_list_case ? ? ? H2);elim Hletin
434      [1,3:left;apply (H1 ? H3)
435      |*:right;apply (H ? H3)]]
436 qed.
437
438 (*** lemma on fresh names ***)
439
440 lemma fresh_name : \forall l:(list nat).\exists n.\lnot (in_list ? n l).
441 cut (\forall l:(list nat).\exists n.\forall m.
442         (n \leq m) \to \lnot (in_list ? m l))
443   [intros;lapply (Hcut l);elim Hletin;apply ex_intro
444      [apply a
445      |apply H;constructor 1]
446   |intros;elim l
447     [apply ex_intro 
448        [apply O
449        |intros;unfold;intro;inversion H1
450           [intros;lapply (sym_eq ? ? ? H3);absurd (a::l1 = [])
451              [assumption|apply nil_cons]
452           |intros;lapply (sym_eq ? ? ? H5);absurd (a1::l1 = [])
453              [assumption|apply nil_cons]]]
454     |elim H;lapply (decidable_eq_nat a t);elim Hletin
455        [apply ex_intro
456           [apply (S a)
457           |intros;unfold;intro;inversion H4
458              [intros;lapply (inj_head_nat ? ? ? ? H6);rewrite < Hletin1 in H5;
459               rewrite < H2 in H5;rewrite > H5 in H3;
460               apply (not_le_Sn_n ? H3)
461              |intros;lapply (inj_tail ? ? ? ? ? H8);rewrite < Hletin1 in H5;
462               rewrite < H7 in H5;
463               apply (H1 m ? H5);lapply (le_S ? ? H3);
464               apply (le_S_S_to_le ? ? Hletin2)]]
465        |cut ((leb a t) = true \lor (leb a t) = false)
466           [elim Hcut
467              [apply ex_intro
468                 [apply (S t)
469                 |intros;unfold;intro;inversion H5
470                    [intros;lapply (inj_head_nat ? ? ? ? H7);rewrite > H6 in H4;
471                     rewrite < Hletin1 in H4;apply (not_le_Sn_n ? H4)
472                    |intros;lapply (inj_tail ? ? ? ? ? H9);
473                     rewrite < Hletin1 in H6;lapply (H1 a1)
474                       [apply (Hletin2 H6)
475                       |lapply (leb_to_Prop a t);rewrite > H3 in Hletin2;
476                        simplify in Hletin2;rewrite < H8;
477                        apply (trans_le ? ? ? Hletin2);
478                        apply (trans_le ? ? ? ? H4);constructor 2;constructor 1]]]
479              |apply ex_intro
480                 [apply a
481                 |intros;lapply (leb_to_Prop a t);rewrite > H3 in Hletin1;
482                  simplify in Hletin1;lapply (not_le_to_lt ? ? Hletin1);
483                  unfold in Hletin2;unfold;intro;inversion H5
484                    [intros;lapply (inj_head_nat ? ? ? ? H7);
485                     rewrite < Hletin3 in H6;rewrite > H6 in H4;
486                     apply (Hletin1 H4)
487                    |intros;lapply (inj_tail ? ? ? ? ? H9);
488                     rewrite < Hletin3 in H6;rewrite < H8 in H6;
489                     apply (H1 ? H4 H6)]]]
490           |elim (leb a t);autobatch]]]]
491 qed.
492
493 (*** lemmata on well-formedness ***)
494
495 lemma fv_WFT : \forall T,x,G.(WFType G T) \to (in_list ? x (fv_type T)) \to
496                   (in_list ? x (fv_env G)).
497 intros 4.elim H
498   [simplify in H2;inversion H2
499      [intros;lapply (inj_head_nat ? ? ? ? H4);rewrite < Hletin;assumption
500      |intros;lapply (inj_tail ? ? ? ? ? H6);rewrite < Hletin in H3;
501       inversion H3
502         [intros;lapply (sym_eq ? ? ? H8);absurd (a2 :: l2 = []) 
503            [assumption|apply nil_cons]
504         |intros;lapply (sym_eq ? ? ? H10);
505             absurd (a3 :: l2 = []) [assumption|apply nil_cons]]]
506   |simplify in H1;lapply (in_list_nil ? x H1);elim Hletin
507   |simplify in H5;lapply (nat_in_list_case ? ? ? H5);elim Hletin
508      [apply (H4 H6)
509      |apply (H2 H6)]
510   |simplify in H5;lapply (nat_in_list_case ? ? ? H5);elim Hletin
511      [lapply (fresh_name ((fv_type t1) @ (fv_env e)));elim Hletin1;
512       cut ((\lnot (in_list ? a (fv_type t1))) \land
513            (\lnot (in_list ? a (fv_env e))))
514         [elim Hcut;lapply (H4 ? H9 H8)
515            [cut (x \neq a)
516               [simplify in Hletin2;
517                (* FIXME trick *);generalize in match Hletin2;intro;
518                inversion Hletin2
519                  [intros;lapply (inj_head_nat ? ? ? ? H12);
520                   rewrite < Hletin3 in H11;lapply (Hcut1 H11);elim Hletin4
521                  |intros;lapply (inj_tail ? ? ? ? ? H14);rewrite > Hletin3;
522                   assumption]
523               |unfold;intro;apply H8;rewrite < H10;assumption]
524            |rewrite > subst_O_nat;apply in_FV_subst;assumption]
525         |split
526            [unfold;intro;apply H7;apply natinG_or_inH_to_natinGH;right;
527             assumption
528            |unfold;intro;apply H7;apply natinG_or_inH_to_natinGH;left;
529             assumption]]
530      |apply (H2 H6)]]
531 qed.
532            
533 (*** some exotic inductions and related lemmas ***) 
534
535 lemma not_t_len_lt_SO : \forall T.\lnot (t_len T) < (S O).
536 intros;elim T
537   [1,2,3:simplify;unfold;intro;unfold in H;elim (not_le_Sn_n ? H)
538   |*:simplify;unfold;rewrite > max_case;elim (leb (t_len t) (t_len t1))
539      [1,3:simplify in H2;apply H1;apply (trans_lt ? ? ? ? H2);unfold;constructor 1
540      |*:simplify in H2;apply H;apply (trans_lt ? ? ? ? H2);unfold;constructor 1]]
541 qed.
542
543 lemma Typ_len_ind : \forall P:Typ \to Prop.
544                        (\forall U.(\forall V.((t_len V) < (t_len U)) \to (P V))
545                            \to (P U))
546                        \to \forall T.(P T).
547 cut (\forall P:Typ \to Prop.
548         (\forall U.(\forall V.((t_len V) < (t_len U)) \to (P V))
549             \to (P U))
550         \to \forall T,n.(n = (t_len T)) \to (P T))                      
551   [intros;apply (Hcut ? H ? (t_len T));reflexivity
552   |intros 4;generalize in match T;apply (nat_elim1 n);intros;
553    generalize in match H2;elim t 
554      [1,2,3:apply H;intros;simplify in H4;elim (not_t_len_lt_SO ? H4)
555      |*:apply H;intros;apply (H1 (t_len V))
556         [1,3:rewrite > H5;assumption
557         |*:reflexivity]]]
558 qed.
559
560 lemma t_len_arrow1 : \forall T1,T2.(t_len T1) < (t_len (Arrow T1 T2)).
561 intros.simplify.
562 (* FIXME!!! BUG?!?! *)
563 cut ((max (t_len T1) (t_len T2)) = match (leb (t_len T1) (t_len T2)) with
564       [ false \Rightarrow (t_len T2)
565       | true \Rightarrow (t_len T1) ])
566   [rewrite > Hcut;cut ((leb (t_len T1) (t_len T2)) = false \lor
567                        (leb (t_len T1) (t_len T2)) = true)
568      [lapply (leb_to_Prop (t_len T1) (t_len T2));elim Hcut1
569         [rewrite > H;rewrite > H in Hletin;simplify;constructor 1
570         |rewrite > H;rewrite > H in Hletin;simplify;simplify in Hletin;
571          unfold;apply le_S_S;assumption]
572      |elim (leb (t_len T1) (t_len T2));autobatch]
573   |elim T1;simplify;reflexivity]
574 qed.
575
576 lemma t_len_arrow2 : \forall T1,T2.(t_len T2) < (t_len (Arrow T1 T2)).
577 intros.simplify.
578 (* FIXME!!! BUG?!?! *)
579 cut ((max (t_len T1) (t_len T2)) = match (leb (t_len T1) (t_len T2)) with
580       [ false \Rightarrow (t_len T2)
581       | true \Rightarrow (t_len T1) ])
582   [rewrite > Hcut;cut ((leb (t_len T1) (t_len T2)) = false \lor
583                        (leb (t_len T1) (t_len T2)) = true)
584      [lapply (leb_to_Prop (t_len T1) (t_len T2));elim Hcut1
585         [rewrite > H;rewrite > H in Hletin;simplify;simplify in Hletin;
586          lapply (not_le_to_lt ? ? Hletin);unfold in Hletin1;unfold;
587          constructor 2;assumption
588         |rewrite > H;simplify;unfold;constructor 1]
589      |elim (leb (t_len T1) (t_len T2));autobatch]
590   |elim T1;simplify;reflexivity]
591 qed.
592
593 lemma t_len_forall1 : \forall T1,T2.(t_len T1) < (t_len (Forall T1 T2)).
594 intros.simplify.
595 (* FIXME!!! BUG?!?! *)
596 cut ((max (t_len T1) (t_len T2)) = match (leb (t_len T1) (t_len T2)) with
597       [ false \Rightarrow (t_len T2)
598       | true \Rightarrow (t_len T1) ])
599   [rewrite > Hcut;cut ((leb (t_len T1) (t_len T2)) = false \lor
600                        (leb (t_len T1) (t_len T2)) = true)
601      [lapply (leb_to_Prop (t_len T1) (t_len T2));elim Hcut1
602         [rewrite > H;rewrite > H in Hletin;simplify;constructor 1
603         |rewrite > H;rewrite > H in Hletin;simplify;simplify in Hletin;
604          unfold;apply le_S_S;assumption]
605      |elim (leb (t_len T1) (t_len T2));autobatch]
606   |elim T1;simplify;reflexivity]
607 qed.
608
609 lemma t_len_forall2 : \forall T1,T2.(t_len T2) < (t_len (Forall T1 T2)).
610 intros.simplify.
611 (* FIXME!!! BUG?!?! *)
612 cut ((max (t_len T1) (t_len T2)) = match (leb (t_len T1) (t_len T2)) with
613       [ false \Rightarrow (t_len T2)
614       | true \Rightarrow (t_len T1) ])
615   [rewrite > Hcut;cut ((leb (t_len T1) (t_len T2)) = false \lor
616                        (leb (t_len T1) (t_len T2)) = true)
617      [lapply (leb_to_Prop (t_len T1) (t_len T2));elim Hcut1
618         [rewrite > H;rewrite > H in Hletin;simplify;simplify in Hletin;
619          lapply (not_le_to_lt ? ? Hletin);unfold in Hletin1;unfold;
620          constructor 2;assumption
621         |rewrite > H;simplify;unfold;constructor 1]
622      |elim (leb (t_len T1) (t_len T2));autobatch]
623   |elim T1;simplify;reflexivity]
624 qed.
625
626 lemma eq_t_len_TFree_subst : \forall T,n,X.(t_len T) = 
627                                          (t_len (subst_type_nat T (TFree X) n)).
628 intro.elim T
629   [simplify;elim (eqb n n1);simplify;reflexivity
630   |2,3:simplify;reflexivity
631   |simplify;lapply (H n X);lapply (H1 n X);rewrite < Hletin;rewrite < Hletin1;
632    reflexivity
633   |simplify;lapply (H n X);lapply (H1 (S n) X);rewrite < Hletin;
634    rewrite < Hletin1;reflexivity]
635 qed.
636
637 (*** lemmata relating subtyping and well-formedness ***)
638
639 lemma JS_to_WFE : \forall G,T,U.(JSubtype G T U) \to (WFEnv G).
640 intros;elim H;assumption.
641 qed.
642
643 lemma JS_to_WFT : \forall G,T,U.(JSubtype G T U) \to ((WFType G T) \land 
644                                                       (WFType G U)).
645 intros;elim H
646   [split [assumption|apply WFT_Top]
647   |split;apply WFT_TFree;assumption
648   |split 
649      [apply WFT_TFree;apply boundinenv_natinfv;apply ex_intro
650         [apply true | apply ex_intro [apply t1 |assumption]]
651      |elim H3;assumption]
652   |elim H2;elim H4;split;apply WFT_Arrow;assumption
653   |elim H2;split
654      [apply (WFT_Forall ? ? ? H6);intros;elim (H4 X H7);
655       apply (WFT_env_incl ? ? H9);simplify;unfold;intros;assumption
656      |apply (WFT_Forall ? ? ? H5);intros;elim (H4 X H7);
657       apply (WFT_env_incl ? ? H10);simplify;unfold;intros;assumption]]
658 qed.
659
660 lemma JS_to_WFT1 : \forall G,T,U.(JSubtype G T U) \to (WFType G T).
661 intros;lapply (JS_to_WFT ? ? ? H);elim Hletin;assumption.
662 qed.
663
664 lemma JS_to_WFT2 : \forall G,T,U.(JSubtype G T U) \to (WFType G U).
665 intros;lapply (JS_to_WFT ? ? ? H);elim Hletin;assumption.
666 qed.
667
668 lemma WFE_Typ_subst : \forall H,x,B,C,T,U,G.
669                       (WFEnv (H @ ((mk_bound B x T) :: G))) \to (WFType G U) \to
670                       (WFEnv (H @ ((mk_bound C x U) :: G))).
671 intros 7;elim H 0
672   [simplify;intros;(*FIXME*)generalize in match H1;intro;inversion H1
673      [intros;lapply (nil_cons ? G (mk_bound B x T));lapply (Hletin H4);
674       elim Hletin1
675      |intros;lapply (inj_tail ? ? ? ? ? H8);lapply (inj_head ? ? ? ? H8);
676       destruct Hletin1;rewrite < Hletin in H6;rewrite < Hletin in H4;
677       rewrite < Hcut1 in H6;apply (WFE_cons ? ? ? ? H4 H6 H2)]
678   |intros;simplify;generalize in match H2;elim t;simplify in H4;
679    inversion H4
680      [intros;absurd (mk_bound b n t1::l@(mk_bound B x T::G)=Empty)
681         [assumption
682         |apply nil_cons]
683      |intros;lapply (inj_tail ? ? ? ? ? H9);lapply (inj_head ? ? ? ? H9);
684       destruct Hletin1;apply WFE_cons
685         [apply H1
686            [rewrite > Hletin;assumption
687            |assumption]
688         |rewrite > Hcut1;generalize in match H7;rewrite < Hletin;
689          rewrite > (fv_env_extends ? x B C T U);intro;assumption
690         |rewrite < Hletin in H8;rewrite > Hcut2;
691          apply (WFT_env_incl ? ? H8);rewrite > (fv_env_extends ? x B C T U);
692          unfold;intros;assumption]]]
693 qed.
694
695 lemma WFE_bound_bound : \forall B,x,T,U,G. (WFEnv G) \to
696                                   (in_list ? (mk_bound B x T) G) \to
697                                   (in_list ? (mk_bound B x U) G) \to T = U.
698 intros 6;elim H
699   [lapply (in_list_nil ? ? H1);elim Hletin
700   |inversion H6
701      [intros;rewrite < H7 in H8;lapply (inj_head ? ? ? ? H8);
702       rewrite > Hletin in H5;inversion H5
703         [intros;rewrite < H9 in H10;lapply (inj_head ? ? ? ? H10);
704          destruct Hletin1;symmetry;assumption
705         |intros;lapply (inj_tail ? ? ? ? ? H12);rewrite < Hletin1 in H9;
706          rewrite < H11 in H9;lapply (boundinenv_natinfv x e)
707            [destruct Hletin;rewrite < Hcut1 in Hletin2;lapply (H3 Hletin2);
708             elim Hletin3
709            |apply ex_intro
710               [apply B|apply ex_intro [apply T|assumption]]]]
711      |intros;lapply (inj_tail ? ? ? ? ? H10);rewrite < H9 in H7;
712       rewrite < Hletin in H7;(*FIXME*)generalize in match H5;intro;inversion H5
713         [intros;rewrite < H12 in H13;lapply (inj_head ? ? ? ? H13);
714          destruct Hletin1;rewrite < Hcut1 in H7;
715          lapply (boundinenv_natinfv n e)
716            [lapply (H3 Hletin2);elim Hletin3
717            |apply ex_intro
718               [apply B|apply ex_intro [apply U|assumption]]]
719         |intros;apply (H2 ? H7);rewrite > H14;lapply (inj_tail ? ? ? ? ? H15);
720          rewrite > Hletin1;assumption]]]
721 qed.