]> matita.cs.unibo.it Git - helm.git/blob - matita/library/Fsub/defn.ma
Qualche semplificazione.
[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 "Fsub/util.ma".
17
18 (*** representation of Fsub types ***)  
19 inductive Typ : Set \def
20   | TVar : nat \to Typ            (* type var *)
21   | TFree: nat \to Typ            (* free type name *)
22   | Top : Typ                     (* maximum type *)
23   | Arrow : Typ \to Typ \to Typ   (* functions *) 
24   | Forall : Typ \to Typ \to Typ. (* universal type *)
25
26 (* representation of bounds *)
27
28 record bound : Set \def { 
29                           istype : bool;    (* is subtyping bound? *)
30                           name   : nat ;    (* name *)
31                           btype  : Typ      (* type to which the name is bound *)
32                         }.
33                
34 (*** Various kinds of substitution, not all will be used probably ***)
35
36 (* substitutes i-th dangling index in type T with type U *)
37 let rec subst_type_nat T U i \def
38     match T with
39     [ (TVar n) \Rightarrow match (eqb n i) with
40       [ true \Rightarrow U
41       | false \Rightarrow T]
42     | (TFree X) \Rightarrow T
43     | Top \Rightarrow T
44     | (Arrow T1 T2) \Rightarrow (Arrow (subst_type_nat T1 U i) (subst_type_nat T2 U i))
45     | (Forall T1 T2) \Rightarrow (Forall (subst_type_nat T1 U i) (subst_type_nat T2 U (S i))) ].
46
47 (*** height of T's syntactic tree ***)
48
49 let rec t_len T \def
50   match T with
51      [(TVar n) \Rightarrow (S O)
52      |(TFree X) \Rightarrow (S O)
53      |Top \Rightarrow (S O)
54      |(Arrow T1 T2) \Rightarrow (S (max (t_len T1) (t_len T2)))
55      |(Forall T1 T2) \Rightarrow (S (max (t_len T1) (t_len T2)))].
56
57 (*** definitions about lists ***)
58
59 definition fv_env : (list bound) \to (list nat) \def
60   \lambda G.(map ? ? (\lambda b.match b with
61       [(mk_bound B X T) \Rightarrow X]) G).
62
63 let rec fv_type T \def
64   match T with
65     [(TVar n) \Rightarrow []
66     |(TFree x) \Rightarrow [x]
67     |Top \Rightarrow []
68     |(Arrow U V) \Rightarrow ((fv_type U) @ (fv_type V))
69     |(Forall U V) \Rightarrow ((fv_type U) @ (fv_type V))].
70
71 (*** Type Well-Formedness judgement ***)
72
73 inductive WFType : (list bound) \to Typ \to Prop \def
74   | WFT_TFree : \forall X,G.(in_list ? X (fv_env G)) 
75                 \to (WFType G (TFree X))
76   | WFT_Top : \forall G.(WFType G Top)
77   | WFT_Arrow : \forall G,T,U.(WFType G T) \to (WFType G U) \to 
78                 (WFType G (Arrow T U))
79   | WFT_Forall : \forall G,T,U.(WFType G T) \to
80                  (\forall X:nat.
81                     (\lnot (in_list ? X (fv_env G))) \to
82                     (\lnot (in_list ? X (fv_type U))) \to
83                     (WFType ((mk_bound true X T) :: G) 
84                        (subst_type_nat U (TFree X) O))) \to 
85                  (WFType G (Forall T U)).
86
87 (*** Environment Well-Formedness judgement ***)
88
89 inductive WFEnv : (list bound) \to Prop \def
90   | WFE_Empty : (WFEnv (nil ?))
91   | WFE_cons : \forall B,X,T,G.(WFEnv G) \to 
92                \lnot (in_list ? X (fv_env G)) \to
93                   (WFType G T) \to (WFEnv ((mk_bound B X T) :: G)).
94             
95 (*** Subtyping judgement ***)              
96 inductive JSubtype : (list bound) \to Typ \to Typ \to Prop \def
97   | SA_Top : \forall G.\forall T:Typ.(WFEnv G) \to
98              (WFType G T) \to (JSubtype G T Top)
99   | SA_Refl_TVar : \forall G.\forall X:nat.(WFEnv G) 
100                    \to (in_list ? X (fv_env G)) 
101                    \to (JSubtype G (TFree X) (TFree X))
102   | SA_Trans_TVar : \forall G.\forall X:nat.\forall T:Typ.
103                     \forall U:Typ.
104                     (in_list ? (mk_bound true X U) G) \to
105                     (JSubtype G U T) \to (JSubtype G (TFree X) T)
106   | SA_Arrow : \forall G.\forall S1,S2,T1,T2:Typ.
107                (JSubtype G T1 S1) \to (JSubtype G S2 T2) \to
108                (JSubtype G (Arrow S1 S2) (Arrow T1 T2))
109   | SA_All : \forall G.\forall S1,S2,T1,T2:Typ.
110              (JSubtype G T1 S1) \to
111              (\forall X:nat.\lnot (in_list ? X (fv_env G)) \to
112                 (JSubtype ((mk_bound true X T1) :: G) 
113                    (subst_type_nat S2 (TFree X) O) (subst_type_nat T2 (TFree X) O))) \to
114              (JSubtype G (Forall S1 S2) (Forall T1 T2)).
115
116 notation "hvbox(e ⊢ break ta ⊴  break tb)" 
117   non associative with precedence 30 for @{ 'subjudg $e $ta $tb }.  
118 interpretation "Fsub subtype judgement" 'subjudg e ta tb =
119  (cic:/matita/Fsub/defn/JSubtype.ind#xpointer(1/1) e ta tb).
120
121 notation > "hvbox(\Forall S.T)" 
122   non associative with precedence 60 for @{ 'forall $S $T}.
123 notation < "hvbox('All' \sub S. break T)" 
124   non associative with precedence 60 for @{ 'forall $S $T}.
125 interpretation "universal type" 'forall S T = 
126   (cic:/matita/Fsub/defn/Typ.ind#xpointer(1/1/5) S T).
127   
128 notation "#x" with precedence 79 for @{'tvar $x}.
129 interpretation "bound tvar" 'tvar x = 
130   (cic:/matita/Fsub/defn/Typ.ind#xpointer(1/1/1) x).
131
132 notation "!x" with precedence 79 for @{'tname $x}.
133 interpretation "bound tname" 'tname x = 
134   (cic:/matita/Fsub/defn/Typ.ind#xpointer(1/1/2) x).
135   
136 notation "⊤" with precedence 90 for @{'toptype}.
137 interpretation "toptype" 'toptype = 
138   (cic:/matita/Fsub/defn/Typ.ind#xpointer(1/1/3)).
139
140 notation "hvbox(s break ⇛ t)"
141   right associative with precedence 55 for @{ 'arrow $s $t }.
142 interpretation "arrow type" 'arrow S T = 
143   (cic:/matita/Fsub/defn/Typ.ind#xpointer(1/1/4) S T).
144   
145 notation "hvbox(S [# n ↦ T])"
146   non associative with precedence 80 for @{ 'substvar $S $T $n }.
147 interpretation "subst bound var" 'substvar S T n =
148   (cic:/matita/Fsub/defn/subst_type_nat.con S T n).  
149
150 notation "hvbox(|T|)"
151   non associative with precedence 30 for @{ 'tlen $T }.
152 interpretation "type length" 'tlen T =
153   (cic:/matita/Fsub/defn/t_len.con T).  
154
155 notation "hvbox(!X ⊴ T)"
156   non associative with precedence 60 for @{ 'subtypebound $X $T }.
157 interpretation "subtyping bound" 'subtypebound X T =
158   (cic:/matita/Fsub/defn/bound.ind#xpointer(1/1/1) true X T).  
159
160 (****** PROOFS ********)
161
162 (*** theorems about lists ***)
163
164 lemma boundinenv_natinfv : \forall x,G.
165                               (\exists B,T.(in_list ? (mk_bound B x T) G)) \to
166                               (in_list ? x (fv_env G)).
167 intros 2;elim G
168   [elim H;elim H1;lapply (in_list_nil ? ? H2);elim Hletin
169   |elim H1;elim H2;elim (in_cons_case ? ? ? ? H3)
170      [rewrite < H4;simplify;apply in_Base
171      |elim H4;elim t;simplify;apply in_Skip2;apply H;apply (ex_intro ? ? a);
172       apply (ex_intro ? ? a1);assumption]]
173 qed.
174
175 lemma nat_in_list_case : \forall G,H,n.(in_list nat n (H @ G)) \to 
176                                (in_list nat n G) \lor (in_list nat n H).
177 intros 3.elim H
178   [simplify in H1;left;assumption
179   |simplify in H2;elim (in_cons_case ? ? ? ? H2)
180     [right;rewrite > H3;apply in_Base
181     |elim H3;elim (H1 H5) [left;assumption|right;apply in_Skip2;assumption]]]
182 qed.
183
184 lemma natinG_or_inH_to_natinGH : \forall G,H,n.
185                       (in_list nat n G) \lor (in_list nat n H) \to
186                       (in_list nat n (H @ G)).
187 intros.elim H1
188   [elim H
189      [simplify;assumption
190      |simplify;apply in_Skip2;assumption]
191   |generalize in match H2;elim H2
192      [simplify;apply in_Base
193      |lapply (H4 H3);simplify;apply in_Skip;assumption]]
194 qed.
195
196 lemma natinfv_boundinenv : \forall x,G.(in_list ? x (fv_env G)) \to
197                               \exists B,T.(in_list ? (mk_bound B x T) G).
198 intros 2;elim G 0
199   [simplify;intro;lapply (in_list_nil ? ? H);elim Hletin
200   |intros 3;elim t;simplify in H1;elim (in_cons_case ? ? ? ? H1)
201      [rewrite < H2;apply (ex_intro ? ? b);apply (ex_intro ? ? t1);apply in_Base
202      |elim H2;elim (H H4);elim H5;apply (ex_intro ? ? a);
203       apply (ex_intro ? ? a1);apply in_Skip
204         [assumption
205         |intro;destruct H7;elim (H3 Hcut1)]]]
206 qed.
207
208 theorem varinT_varinT_subst : \forall X,Y,T.
209         (in_list ? X (fv_type T)) \to \forall n.
210         (in_list ? X (fv_type (subst_type_nat T (TFree Y) n))).
211 intros 3;elim T
212   [simplify in H;elim (in_list_nil ? ? H)
213   |simplify in H;simplify;assumption
214   |simplify in H;elim (in_list_nil ? ? H)
215   |simplify in H2;simplify;elim (nat_in_list_case ? ? ? H2);
216    apply natinG_or_inH_to_natinGH;
217      [left;apply (H1 H3)
218      |right;apply (H H3)]
219   |simplify in H2;simplify;elim (nat_in_list_case ? ? ? H2);
220    apply natinG_or_inH_to_natinGH;
221      [left;apply (H1 H3);
222      |right;apply (H H3)]]
223 qed.
224
225 lemma incl_bound_fv : \forall l1,l2.(incl ? l1 l2) \to 
226                          (incl ? (fv_env l1) (fv_env l2)).
227 intros.unfold in H.unfold.intros.apply boundinenv_natinfv.
228 lapply (natinfv_boundinenv ? ? H1).elim Hletin.elim H2.apply ex_intro
229   [apply a
230   |apply ex_intro
231      [apply a1
232      |apply (H ? H3)]]
233 qed.
234
235 lemma incl_nat_cons : \forall x,l1,l2.
236                   (incl nat l1 l2) \to (incl nat (x :: l1) (x :: l2)).
237 intros.unfold in H.unfold.intros.elim (in_cons_case ? ? ? ? H1)
238   [rewrite > H2;apply in_Base|elim H2;apply in_Skip2;apply (H ? H4)]
239 qed.
240
241 lemma WFT_env_incl : \forall G,T.(WFType G T) \to
242                      \forall H.(incl ? (fv_env G) (fv_env H)) \to (WFType H T).
243 intros 3.elim H
244   [apply WFT_TFree;unfold in H3;apply (H3 ? H1)
245   |apply WFT_Top
246   |apply WFT_Arrow [apply (H2 ? H6)|apply (H4 ? H6)]
247   |apply WFT_Forall 
248      [apply (H2 ? H6)
249      |intros;apply (H4 ? ? H8)
250         [unfold;intro;apply H7;apply(H6 ? H9)
251         |simplify;apply (incl_nat_cons ? ? ? H6)]]]
252 qed.
253
254 lemma fv_env_extends : \forall H,x,B,C,T,U,G.
255                           (fv_env (H @ ((mk_bound B x T) :: G))) = 
256                           (fv_env (H @ ((mk_bound C x U) :: G))).
257 intros;elim H
258   [simplify;reflexivity|elim t;simplify;rewrite > H1;reflexivity]
259 qed.
260
261 lemma lookup_env_extends : \forall G,H,B,C,D,T,U,V,x,y.
262             (in_list ? (mk_bound D y V) (H @ ((mk_bound C x U) :: G))) \to
263             (y \neq x) \to
264             (in_list ? (mk_bound D y V) (H @ ((mk_bound B x T) :: G))).
265 intros 10;elim H
266   [simplify in H1;elim (in_cons_case ? ? ? ? H1)
267      [destruct H3;elim (H2 Hcut1)
268      |simplify;elim H3;apply (in_Skip ? ? ? ? H5);intro;destruct H6;
269       apply (H2 Hcut1)] 
270   |simplify in H2;simplify;elim (in_cons_case ? ? ? ? H2)
271      [rewrite > H4;apply in_Base
272      |elim H4;apply (in_Skip ? ? ? ? (H1 H6 H3) H5)]]
273 qed.
274
275 lemma in_FV_subst : \forall x,T,U,n.(in_list ? x (fv_type T)) \to
276                                 (in_list ? x (fv_type (subst_type_nat T U n))).
277 intros 3;elim T
278   [simplify in H;elim (in_list_nil ? ? H)
279   |2,3:simplify;simplify in H;assumption
280   |*:simplify in H2;simplify;apply natinG_or_inH_to_natinGH;
281    lapply (nat_in_list_case ? ? ? H2);elim Hletin
282      [1,3:left;apply (H1 ? H3)
283      |*:right;apply (H ? H3)]]
284 qed.
285
286 (*** lemma on fresh names ***)
287
288 lemma fresh_name : \forall l:(list nat).\exists n.\lnot (in_list ? n l).
289 cut (\forall l:(list nat).\exists n.\forall m.
290         (n \leq m) \to \lnot (in_list ? m l))
291   [intros;lapply (Hcut l);elim Hletin;apply ex_intro
292      [apply a
293      |apply H;constructor 1]
294   |intros;elim l
295     [apply (ex_intro ? ? O);intros;unfold;intro;elim (in_list_nil ? ? H1)
296     |elim H;
297      apply (ex_intro ? ? (S (max a t))).
298      intros.unfold. intro.
299      elim (in_cons_case ? ? ? ? H3)
300       [rewrite > H4 in H2.autobatch
301       |elim H4.apply (H1 m ? H6).
302        apply (trans_le ? (max a t));autobatch]]]
303 qed.
304
305 (*** lemmata on well-formedness ***)
306
307 lemma fv_WFT : \forall T,x,G.(WFType G T) \to (in_list ? x (fv_type T)) \to
308                   (in_list ? x (fv_env G)).
309 intros 4.elim H
310   [simplify in H2;elim (in_cons_case ? ? ? ? H2)
311      [rewrite > H3;assumption|elim H3;elim (in_list_nil ? ? H5)]
312   |simplify in H1;elim (in_list_nil ? x H1)
313   |simplify in H5;elim (nat_in_list_case ? ? ? H5);autobatch
314   |simplify in H5;elim (nat_in_list_case ? ? ? H5)
315      [elim (fresh_name ((fv_type t1) @ (fv_env l)));
316       cut ((¬ (in_list ? a (fv_type t1))) ∧
317            (¬ (in_list ? a (fv_env l))))
318         [elim Hcut;lapply (H4 ? H9 H8)
319            [cut (x ≠ a)
320               [simplify in Hletin;elim (in_cons_case ? ? ? ? Hletin)
321                  [elim (Hcut1 H10)|elim H10;assumption]
322               |intro;apply H8;rewrite < H10;assumption]
323            |apply in_FV_subst;assumption]
324         |split
325            [intro;apply H7;apply natinG_or_inH_to_natinGH;right;assumption
326            |intro;apply H7;apply natinG_or_inH_to_natinGH;left;assumption]]
327      |apply (H2 H6)]]
328 qed.
329            
330 (*** some exotic inductions and related lemmas ***) 
331
332 lemma O_lt_t_len: \forall T.O < (t_len T).
333 intros;elim T
334   [1,2,3:simplify;apply le_n
335   |*:simplify;apply lt_O_S]
336 qed.
337
338 (*
339 lemma not_t_len_lt_SO : \forall T.\lnot (t_len T) < (S O).
340 intros;elim T
341   [1,2,3:simplify;unfold;intro;unfold in H;elim (not_le_Sn_n ? H)
342   |*:simplify;unfold;rewrite > max_case;elim (leb (t_len t) (t_len t1))
343      [1,3:simplify in H2;apply H1;apply (trans_lt ? ? ? ? H2);unfold;constructor 1
344      |*:simplify in H2;apply H;apply (trans_lt ? ? ? ? H2);unfold;constructor 1]]
345 qed.
346 *)
347
348 lemma Typ_len_ind : \forall P:Typ \to Prop.
349                        (\forall U.(\forall V.((t_len V) < (t_len U)) \to (P V))
350                            \to (P U))
351                        \to \forall T.(P T).
352 cut (\forall P:Typ \to Prop.
353         (\forall U.(\forall V.((t_len V) < (t_len U)) \to (P V))
354             \to (P U))
355         \to \forall T,n.(n = (t_len T)) \to (P T))                      
356   [intros;apply (Hcut ? H ? (t_len T));reflexivity
357   |intros 4;generalize in match T;apply (nat_elim1 n);intros;
358    generalize in match H2;elim t 
359      [1,2,3:apply H;intros;simplify in H4;elim (lt_to_not_le ? ? H4 (O_lt_t_len ?))
360      |*:apply H;intros;apply (H1 (t_len V))
361         [1,3:rewrite > H5;assumption
362         |*:reflexivity]]]
363 qed.
364
365 lemma t_len_arrow1 : \forall T1,T2.(t_len T1) < (t_len (Arrow T1 T2)).
366 intros.simplify.
367 apply le_S_S.apply le_m_max_m_n.
368 qed.
369
370 lemma t_len_arrow2 : \forall T1,T2.(t_len T2) < (t_len (Arrow T1 T2)).
371 intros.simplify.
372 apply le_S_S.apply le_n_max_m_n.
373 qed.
374
375 lemma t_len_forall1 : \forall T1,T2.(t_len T1) < (t_len (Forall T1 T2)).
376 intros.simplify.
377 apply le_S_S.apply le_m_max_m_n.
378 qed.
379
380 lemma t_len_forall2 : \forall T1,T2.(t_len T2) < (t_len (Forall T1 T2)).
381 intros.simplify.
382 apply le_S_S.apply le_n_max_m_n.
383 qed.
384
385 lemma eq_t_len_TFree_subst : \forall T,n,X.(t_len T) = 
386                                          (t_len (subst_type_nat T (TFree X) n)).
387 intro.elim T
388   [simplify;elim (eqb n n1);simplify;reflexivity
389   |2,3:simplify;reflexivity
390   |simplify;lapply (H n X);lapply (H1 n X);rewrite < Hletin;rewrite < Hletin1;
391    reflexivity
392   |simplify;lapply (H n X);lapply (H1 (S n) X);rewrite < Hletin;
393    rewrite < Hletin1;reflexivity]
394 qed.
395
396 (*** lemmata relating subtyping and well-formedness ***)
397
398 lemma JS_to_WFE : \forall G,T,U.(G \vdash T ⊴ U) \to (WFEnv G).
399 intros;elim H;assumption.
400 qed.
401
402 lemma JS_to_WFT : \forall G,T,U.(JSubtype G T U) \to ((WFType G T) \land 
403                                                       (WFType G U)).
404 intros;elim H
405   [split [assumption|apply WFT_Top]
406   |split;apply WFT_TFree;assumption
407   |split 
408      [apply WFT_TFree;apply boundinenv_natinfv;apply ex_intro
409         [apply true | apply ex_intro [apply t1 |assumption]]
410      |elim H3;assumption]
411   |elim H2;elim H4;split;apply WFT_Arrow;assumption
412   |elim H2;split
413      [apply (WFT_Forall ? ? ? H6);intros;elim (H4 X H7);
414       apply (WFT_env_incl ? ? H9);simplify;unfold;intros;assumption
415      |apply (WFT_Forall ? ? ? H5);intros;elim (H4 X H7);
416       apply (WFT_env_incl ? ? H10);simplify;unfold;intros;assumption]]
417 qed.
418
419 lemma JS_to_WFT1 : \forall G,T,U.(JSubtype G T U) \to (WFType G T).
420 intros;lapply (JS_to_WFT ? ? ? H);elim Hletin;assumption.
421 qed.
422
423 lemma JS_to_WFT2 : \forall G,T,U.(JSubtype G T U) \to (WFType G U).
424 intros;lapply (JS_to_WFT ? ? ? H);elim Hletin;assumption.
425 qed.
426
427 lemma WFE_Typ_subst : \forall H,x,B,C,T,U,G.
428                       (WFEnv (H @ ((mk_bound B x T) :: G))) \to (WFType G U) \to
429                       (WFEnv (H @ ((mk_bound C x U) :: G))).
430 intros 7;elim H 0
431   [simplify;intros;(*FIXME*)generalize in match H1;intro;inversion H1;intros
432      [lapply (nil_cons ? G (mk_bound B x T));elim (Hletin H4)
433      |destruct H8;rewrite < Hcut2 in H6;rewrite < Hcut in H4;
434       rewrite < Hcut in H6;apply (WFE_cons ? ? ? ? H4 H6 H2)]
435   |intros;simplify;generalize in match H2;elim t;simplify in H4;
436    inversion H4;intros
437      [destruct H5
438      |destruct H9;apply WFE_cons
439         [rewrite < Hcut in H5;apply (H1 H5 H3)
440         |rewrite < (fv_env_extends ? x B C T U);rewrite > Hcut;rewrite > Hcut2;
441          assumption
442         |rewrite < Hcut3 in H8;rewrite > Hcut1;apply (WFT_env_incl ? ? H8);
443          rewrite < (fv_env_extends ? x B C T U);unfold;intros;
444          rewrite > Hcut;assumption]]]
445 qed.
446
447 lemma WFE_bound_bound : \forall B,x,T,U,G. (WFEnv G) \to
448                                   (in_list ? (mk_bound B x T) G) \to
449                                   (in_list ? (mk_bound B x U) G) \to T = U.
450 intros 6;elim H
451   [lapply (in_list_nil ? ? H1);elim Hletin
452   |elim (in_cons_case ? ? ? ? H6)
453      [destruct H7;subst;elim (in_cons_case ? ? ? ? H5)
454         [destruct H7;assumption
455         |elim H7;elim H3;apply boundinenv_natinfv;apply (ex_intro ? ? b);
456          apply (ex_intro ? ? T);assumption]
457      |elim H7;elim (in_cons_case ? ? ? ? H5)
458         [destruct H10;elim H3;apply boundinenv_natinfv;apply (ex_intro ? ? B);
459          apply (ex_intro ? ? U);rewrite < Hcut1;assumption
460         |elim H10;apply (H2 H12 H9)]]]
461 qed.