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