]> matita.cs.unibo.it Git - helm.git/blob - matita/library/Fsub/defn.ma
Some notation added
[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 (*** Various kinds of substitution, not all will be used probably ***)
57
58 (* substitutes i-th dangling index in type T with type U *)
59 let rec subst_type_nat T U i \def
60     match T with
61     [ (TVar n) \Rightarrow match (eqb n i) with
62       [ true \Rightarrow U
63       | false \Rightarrow T]
64     | (TFree X) \Rightarrow T
65     | Top \Rightarrow T
66     | (Arrow T1 T2) \Rightarrow (Arrow (subst_type_nat T1 U i) (subst_type_nat T2 U i))
67     | (Forall T1 T2) \Rightarrow (Forall (subst_type_nat T1 U i) (subst_type_nat T2 U (S i))) ].
68
69 (* substitutes 0-th dangling index in type T with type U *)
70 (*let rec subst_type_O T U  \def subst_type_nat T U O.*) 
71
72 (* substitutes 0-th dangling index in term t with term u *)
73 (*let rec subst_term_O t u  \def
74   let rec aux t0 i \def
75     match t0 with
76     [ (Var n) \Rightarrow match (eqb n i) with
77       [ true \Rightarrow u
78       | false \Rightarrow t0]
79     | (Free X) \Rightarrow t0
80     | (Abs T1 t1) \Rightarrow (Abs T1 (aux t1 (S i)))
81     | (App t1 t2) \Rightarrow (App (aux t1 i) (aux t2 i))
82     | (TAbs T1 t1) \Rightarrow (TAbs T1 (aux t1 (S i)))
83     | (TApp t1 T1) \Rightarrow (TApp (aux t1 i) T1) ]
84   in aux t O.
85
86 (* substitutes 0-th dangling index in term T, which shall be a TVar,
87    with type U *)
88 let rec subst_term_tO t T  \def
89   let rec aux t0 i \def
90     match t0 with
91     [ (Var n) \Rightarrow t0 
92     | (Free X) \Rightarrow t0
93     | (Abs T1 t1) \Rightarrow (Abs (subst_type_nat T1 T i) (aux t1 (S i)))
94     | (App t1 t2) \Rightarrow (App (aux t1 i) (aux t2 i))
95     | (TAbs T1 t1) \Rightarrow (TAbs (subst_type_nat T1 T i) (aux t1 (S i)))
96     | (TApp t1 T1) \Rightarrow (TApp (aux t1 i) (subst_type_nat T1 T i)) ]
97   in aux t O.
98
99 (* substitutes (TFree X) in type T with type U *)
100 let rec subst_type_tfree_type T X U on T \def
101   match T with
102     [ (TVar n) \Rightarrow T
103     | (TFree Y) \Rightarrow match (eqb X Y) with
104       [ true \Rightarrow U
105       | false \Rightarrow T ]
106     | Top \Rightarrow T
107     | (Arrow T1 T2) \Rightarrow (Arrow (subst_type_tfree_type T1 X U) 
108                                        (subst_type_tfree_type T2 X U))
109     | (Forall T1 T2) \Rightarrow (Forall (subst_type_tfree_type T1 X U) 
110                                        (subst_type_tfree_type T2 X U)) ].*)
111             
112 (*** height of T's syntactic tree ***)
113
114 let rec t_len T \def
115   match T with
116      [(TVar n) \Rightarrow (S O)
117      |(TFree X) \Rightarrow (S O)
118      |Top \Rightarrow (S O)
119      |(Arrow T1 T2) \Rightarrow (S (max (t_len T1) (t_len T2)))
120      |(Forall T1 T2) \Rightarrow (S (max (t_len T1) (t_len T2)))].
121
122 definition head \def
123   \lambda G:(list bound).match G with
124     [ nil \Rightarrow (mk_bound false O Top)
125     | (cons b H) \Rightarrow b].
126
127 definition head_nat \def
128   \lambda G:(list nat).match G with
129     [ nil \Rightarrow O
130     | (cons n H) \Rightarrow n].
131
132 (*** definitions about lists ***)
133
134 (*(* var binding is in env judgement *)                
135 definition var_bind_in_env : bound \to (list bound) \to Prop \def
136   \lambda b,G.(in_list bound b G).*)
137
138 definition fv_env : (list bound) \to (list nat) \def
139   \lambda G.(map ? ? (\lambda b.match b with
140       [(mk_bound B X T) \Rightarrow X]) G).
141
142 (*(* variable is in env judgement *)              
143 definition var_in_env : nat \to (list bound) \to Prop \def
144   \lambda x,G.(in_list nat x (fv_env G)).
145   
146 definition var_type_in_env : nat \to (list bound) \to Prop \def
147   \lambda x,G.\exists T.(var_bind_in_env (mk_bound true x T) G).*)
148
149 let rec fv_type T \def
150   match T with
151     [(TVar n) \Rightarrow []
152     |(TFree x) \Rightarrow [x]
153     |Top \Rightarrow []
154     |(Arrow U V) \Rightarrow ((fv_type U) @ (fv_type V))
155     |(Forall U V) \Rightarrow ((fv_type U) @ (fv_type V))].
156
157 (*** Type Well-Formedness judgement ***)
158
159 inductive WFType : (list bound) \to Typ \to Prop \def
160   | WFT_TFree : \forall X,G.(in_list ? X (fv_env G)) 
161                 \to (WFType G (TFree X))
162   | WFT_Top : \forall G.(WFType G Top)
163   | WFT_Arrow : \forall G,T,U.(WFType G T) \to (WFType G U) \to 
164                 (WFType G (Arrow T U))
165   | WFT_Forall : \forall G,T,U.(WFType G T) \to
166                  (\forall X:nat.
167                     (\lnot (in_list ? X (fv_env G))) \to
168                     (\lnot (in_list ? X (fv_type U))) \to
169                     (WFType ((mk_bound true X T) :: G) 
170                        (subst_type_nat U (TFree X) O))) \to 
171                  (WFType G (Forall T U)).
172
173 (*** Environment Well-Formedness judgement ***)
174
175 inductive WFEnv : (list bound) \to Prop \def
176   | WFE_Empty : (WFEnv (nil ?))
177   | WFE_cons : \forall B,X,T,G.(WFEnv G) \to 
178                \lnot (in_list ? X (fv_env G)) \to
179                   (WFType G T) \to (WFEnv ((mk_bound B X T) :: G)).
180             
181 (*** Subtyping judgement ***)              
182 inductive JSubtype : (list bound) \to Typ \to Typ \to Prop \def
183   | SA_Top : \forall G.\forall T:Typ.(WFEnv G) \to
184              (WFType G T) \to (JSubtype G T Top)
185   | SA_Refl_TVar : \forall G.\forall X:nat.(WFEnv G) 
186                    \to (in_list ? X (fv_env G)) 
187                    \to (JSubtype G (TFree X) (TFree X))
188   | SA_Trans_TVar : \forall G.\forall X:nat.\forall T:Typ.
189                     \forall U:Typ.
190                     (in_list ? (mk_bound true X U) G) \to
191                     (JSubtype G U T) \to (JSubtype G (TFree X) T)
192   | SA_Arrow : \forall G.\forall S1,S2,T1,T2:Typ.
193                (JSubtype G T1 S1) \to (JSubtype G S2 T2) \to
194                (JSubtype G (Arrow S1 S2) (Arrow T1 T2))
195   | SA_All : \forall G.\forall S1,S2,T1,T2:Typ.
196              (JSubtype G T1 S1) \to
197              (\forall X:nat.\lnot (in_list ? X (fv_env G)) \to
198                 (JSubtype ((mk_bound true X T1) :: G) 
199                    (subst_type_nat S2 (TFree X) O) (subst_type_nat T2 (TFree X) O))) \to
200              (JSubtype G (Forall S1 S2) (Forall T1 T2)).
201
202 notation "hvbox(e ⊢ break ta ⊴  break tb)" 
203   non associative with precedence 30 for @{ 'subjudg $e $ta $tb }.  
204 interpretation "Fsub subtype judgement" 'subjudg e ta tb =
205  (cic:/matita/Fsub/defn/JSubtype.ind#xpointer(1/1) e ta tb).
206
207 notation > "hvbox(\Forall S.T)" 
208   non associative with precedence 60 for @{ 'forall $S $T}.
209 notation < "hvbox('All' \sub S. break T)" 
210   non associative with precedence 60 for @{ 'forall $S $T}.
211 interpretation "universal type" 'forall S T = 
212   (cic:/matita/Fsub/defn/Typ.ind#xpointer(1/1/5) S T).
213   
214 notation "#x" with precedence 79 for @{'tvar $x}.
215 interpretation "bound tvar" 'tvar x = 
216   (cic:/matita/Fsub/defn/Typ.ind#xpointer(1/1/1) x).
217
218 notation "!x" with precedence 79 for @{'tname $x}.
219 interpretation "bound tname" 'tname x = 
220   (cic:/matita/Fsub/defn/Typ.ind#xpointer(1/1/2) x).
221   
222 notation "⊤" with precedence 90 for @{'toptype}.
223 interpretation "toptype" 'toptype = 
224   (cic:/matita/Fsub/defn/Typ.ind#xpointer(1/1/3)).
225
226 notation "hvbox(s break ⇛ t)"
227   right associative with precedence 55 for @{ 'arrow $s $t }.
228 interpretation "arrow type" 'arrow S T = 
229   (cic:/matita/Fsub/defn/Typ.ind#xpointer(1/1/4) S T).
230   
231 notation "hvbox(S [# n ↦ T])"
232   non associative with precedence 80 for @{ 'substvar $S $T $n }.
233 interpretation "subst bound var" 'substvar S T n =
234   (cic:/matita/Fsub/defn/subst_type_nat.con S T n).  
235
236 notation "hvbox(|T|)"
237   non associative with precedence 30 for @{ 'tlen $T }.
238 interpretation "type length" 'tlen T =
239   (cic:/matita/Fsub/defn/t_len.con T).  
240
241 notation > "hvbox(x ∈ l)"
242   non associative with precedence 30 for @{ 'inlist $x $l }.
243 notation < "hvbox(x \nbsp ∈ \nbsp l)"
244   non associative with precedence 30 for @{ 'inlist $x $l }.
245 interpretation "item in list" 'inlist x l =
246   (cic:/matita/Fsub/util/in_list.ind#xpointer(1/1) _ x l).
247   
248 notation "hvbox(!X ⊴ T)"
249   non associative with precedence 60 for @{ 'subtypebound $X $T }.
250 interpretation "subtyping bound" 'subtypebound X T =
251   (cic:/matita/Fsub/defn/bound.ind#xpointer(1/1/1) true X T).  
252
253 (*notation < "hvbox(e break ⊢ ta \nbsp 'V' \nbsp tb (= \above \alpha))" 
254   non associative with precedence 30 for @{ 'subjudg $e $ta $tb }.
255 notation > "hvbox(e break ⊢ ta 'Fall' break tb)" 
256   non associative with precedence 30 for @{ 'subjudg $e $ta $tb }.
257 notation "hvbox(e break ⊢ ta \lessdot break tb)" 
258   non associative with precedence 30 for @{ 'subjudg $e $ta $tb }.*)  
259
260 (****** PROOFS ********)
261
262 (*lemma subst_O_nat : \forall T,U.((subst_type_O T U) = T[#O↦U]).
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:(list bound).
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 l)));elim Hletin1;
512       cut ((\lnot (in_list ? a (fv_type t1))) \land
513            (\lnot (in_list ? a (fv_env l))))
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            |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 ⇒ (t_len T2)
565       | true  ⇒ (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.(G \vdash 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)=[])
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 l)
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 l)
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.