]> matita.cs.unibo.it Git - helm.git/blob - matita/contribs/POPLmark/Fsub/defn.ma
tagged 0.5.0-rc1
[helm.git] / matita / contribs / POPLmark / 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 (*** definitions about lists ***)
48
49 definition fv_env : (list bound) \to (list nat) \def
50   \lambda G.(map ? ? (\lambda b.match b with
51       [(mk_bound B X T) \Rightarrow X]) G).
52
53 let rec fv_type T \def
54   match T with
55     [(TVar n) \Rightarrow []
56     |(TFree x) \Rightarrow [x]
57     |Top \Rightarrow []
58     |(Arrow U V) \Rightarrow ((fv_type U) @ (fv_type V))
59     |(Forall U V) \Rightarrow ((fv_type U) @ (fv_type V))].
60
61 (*** Type Well-Formedness judgement ***)
62
63 inductive WFType : (list bound) \to Typ \to Prop \def
64   | WFT_TFree : \forall X,G.(in_list ? X (fv_env G)) 
65                 \to (WFType G (TFree X))
66   | WFT_Top : \forall G.(WFType G Top)
67   | WFT_Arrow : \forall G,T,U.(WFType G T) \to (WFType G U) \to 
68                 (WFType G (Arrow T U))
69   | WFT_Forall : \forall G,T,U.(WFType G T) \to
70                  (\forall X:nat.
71                     (\lnot (in_list ? X (fv_env G))) \to
72                     (\lnot (in_list ? X (fv_type U))) \to
73                     (WFType ((mk_bound true X T) :: G) 
74                        (subst_type_nat U (TFree X) O))) \to 
75                  (WFType G (Forall T U)).
76
77 (*** Environment Well-Formedness judgement ***)
78
79 inductive WFEnv : (list bound) \to Prop \def
80   | WFE_Empty : (WFEnv (nil ?))
81   | WFE_cons : \forall B,X,T,G.(WFEnv G) \to 
82                \lnot (in_list ? X (fv_env G)) \to
83                   (WFType G T) \to (WFEnv ((mk_bound B X T) :: G)).
84             
85 (*** Subtyping judgement ***)              
86 inductive JSubtype : (list bound) \to Typ \to Typ \to Prop \def
87   | SA_Top : \forall G.\forall T:Typ.(WFEnv G) \to
88              (WFType G T) \to (JSubtype G T Top)
89   | SA_Refl_TVar : \forall G.\forall X:nat.(WFEnv G) 
90                    \to (in_list ? X (fv_env G)) 
91                    \to (JSubtype G (TFree X) (TFree X))
92   | SA_Trans_TVar : \forall G.\forall X:nat.\forall T:Typ.
93                     \forall U:Typ.
94                     (in_list ? (mk_bound true X U) G) \to
95                     (JSubtype G U T) \to (JSubtype G (TFree X) T)
96   | SA_Arrow : \forall G.\forall S1,S2,T1,T2:Typ.
97                (JSubtype G T1 S1) \to (JSubtype G S2 T2) \to
98                (JSubtype G (Arrow S1 S2) (Arrow T1 T2))
99   | SA_All : \forall G.\forall S1,S2,T1,T2:Typ.
100              (JSubtype G T1 S1) \to
101              (\forall X:nat.\lnot (in_list ? X (fv_env G)) \to
102                 (JSubtype ((mk_bound true X T1) :: G) 
103                    (subst_type_nat S2 (TFree X) O) (subst_type_nat T2 (TFree X) O))) \to
104              (JSubtype G (Forall S1 S2) (Forall T1 T2)).
105
106 notation "hvbox(e ⊢ break ta ⊴  break tb)" 
107   non associative with precedence 30 for @{ 'subjudg $e $ta $tb }.  
108 interpretation "Fsub subtype judgement" 'subjudg e ta tb =
109  (cic:/matita/Fsub/defn/JSubtype.ind#xpointer(1/1) e ta tb).
110
111 notation > "hvbox(\Forall S.T)" 
112   non associative with precedence 60 for @{ 'forall $S $T}.
113 notation < "hvbox('All' \sub S. break T)" 
114   non associative with precedence 60 for @{ 'forall $S $T}.
115 interpretation "universal type" 'forall S T = 
116   (cic:/matita/Fsub/defn/Typ.ind#xpointer(1/1/5) S T).
117   
118 notation "#x" with precedence 79 for @{'tvar $x}.
119 interpretation "bound tvar" 'tvar x = 
120   (cic:/matita/Fsub/defn/Typ.ind#xpointer(1/1/1) x).
121
122 notation "!x" with precedence 79 for @{'tname $x}.
123 interpretation "bound tname" 'tname x = 
124   (cic:/matita/Fsub/defn/Typ.ind#xpointer(1/1/2) x).
125   
126 notation "⊤" with precedence 90 for @{'toptype}.
127 interpretation "toptype" 'toptype = 
128   (cic:/matita/Fsub/defn/Typ.ind#xpointer(1/1/3)).
129
130 notation "hvbox(s break ⇛ t)"
131   right associative with precedence 55 for @{ 'arrow $s $t }.
132 interpretation "arrow type" 'arrow S T = 
133   (cic:/matita/Fsub/defn/Typ.ind#xpointer(1/1/4) S T).
134   
135 notation "hvbox(S [# n ↦ T])"
136   non associative with precedence 80 for @{ 'substvar $S $T $n }.
137 interpretation "subst bound var" 'substvar S T n =
138   (cic:/matita/Fsub/defn/subst_type_nat.con S T n).  
139
140 notation "hvbox(!X ⊴ T)"
141   non associative with precedence 60 for @{ 'subtypebound $X $T }.
142 interpretation "subtyping bound" 'subtypebound X T =
143   (cic:/matita/Fsub/defn/bound.ind#xpointer(1/1/1) true X T).  
144
145 (****** PROOFS ********)
146
147 (*** theorems about lists ***)
148
149 lemma boundinenv_natinfv : \forall x,G.
150                               (\exists B,T.(in_list ? (mk_bound B x T) G)) \to
151                               (in_list ? x (fv_env G)).
152 intros 2;elim G
153   [elim H;elim H1;lapply (not_in_list_nil ? ? H2);elim Hletin
154   |elim H1;elim H2;elim (in_list_cons_case ? ? ? ? H3)
155      [rewrite < H4;simplify;apply in_list_head
156      |simplify;apply in_list_cons;apply H;apply (ex_intro ? ? a);
157       apply (ex_intro ? ? a1);assumption]]
158 qed.
159
160 lemma natinfv_boundinenv : \forall x,G.(in_list ? x (fv_env G)) \to
161                               \exists B,T.(in_list ? (mk_bound B x T) G).
162 intros 2;elim G 0
163   [simplify;intro;lapply (not_in_list_nil ? ? H);elim Hletin
164   |intros 3;elim t;simplify in H1;elim (in_list_cons_case ? ? ? ? H1)
165      [rewrite < H2;apply (ex_intro ? ? b);apply (ex_intro ? ? t1);apply in_list_head
166      |elim (H H2);elim H3;apply (ex_intro ? ? a);
167       apply (ex_intro ? ? a1);apply in_list_cons;assumption]]
168 qed.
169
170 lemma incl_bound_fv : \forall l1,l2.(incl ? l1 l2) \to 
171                          (incl ? (fv_env l1) (fv_env l2)).
172 intros.unfold in H.unfold.intros.apply boundinenv_natinfv.
173 lapply (natinfv_boundinenv ? ? H1).elim Hletin.elim H2.apply ex_intro
174   [apply a
175   |apply ex_intro
176      [apply a1
177      |apply (H ? H3)]]
178 qed.
179
180 lemma incl_cons : \forall x,l1,l2.
181                   (incl ? l1 l2) \to (incl nat (x :: l1) (x :: l2)).
182 intros.unfold in H.unfold.intros.elim (in_list_cons_case ? ? ? ? H1)
183   [rewrite > H2;apply in_list_head|apply in_list_cons;apply (H ? H2)]
184 qed.
185
186 lemma WFT_env_incl : \forall G,T.(WFType G T) \to
187                      \forall H.(incl ? (fv_env G) (fv_env H)) \to (WFType H T).
188 intros 3.elim H
189   [apply WFT_TFree;unfold in H3;apply (H3 ? H1)
190   |apply WFT_Top
191   |apply WFT_Arrow [apply (H2 ? H6)|apply (H4 ? H6)]
192   |apply WFT_Forall 
193      [apply (H2 ? H6)
194      |intros;apply (H4 ? ? H8)
195         [unfold;intro;apply H7;apply(H6 ? H9)
196         |simplify;apply (incl_cons ? ? ? H6)]]]
197 qed.
198
199 lemma fv_env_extends : \forall H,x,B,C,T,U,G.
200                           (fv_env (H @ ((mk_bound B x T) :: G))) = 
201                           (fv_env (H @ ((mk_bound C x U) :: G))).
202 intros;elim H
203   [simplify;reflexivity|elim t;simplify;rewrite > H1;reflexivity]
204 qed.
205
206 lemma lookup_env_extends : \forall G,H,B,C,D,T,U,V,x,y.
207             (in_list ? (mk_bound D y V) (H @ ((mk_bound C x U) :: G))) \to
208             (y \neq x) \to
209             (in_list ? (mk_bound D y V) (H @ ((mk_bound B x T) :: G))).
210 intros 10;elim H
211   [simplify in H1;elim (in_list_cons_case ? ? ? ? H1)
212      [destruct H3;elim (H2);reflexivity
213      |simplify;apply (in_list_cons ? ? ? ? H3);]
214   |simplify in H2;simplify;elim (in_list_cons_case ? ? ? ? H2)
215      [rewrite > H4;apply in_list_head
216      |apply (in_list_cons ? ? ? ? (H1 H4 H3))]]
217 qed.
218
219 lemma in_FV_subst : \forall x,T,U,n.(in_list ? x (fv_type T)) \to
220                                 (in_list ? x (fv_type (subst_type_nat T U n))).
221 intros 3;elim T
222   [simplify in H;elim (not_in_list_nil ? ? H)
223   |2,3:simplify;simplify in H;assumption
224   |*:simplify in H2;simplify;elim (in_list_append_to_or_in_list ? ? ? ? H2)
225      [1,3:apply in_list_to_in_list_append_l;apply (H ? H3)
226      |*:apply in_list_to_in_list_append_r;apply (H1 ? H3)]]
227 qed.
228
229 (*** lemma on fresh names ***)
230
231 lemma fresh_name : \forall l:(list nat).\exists n.\lnot (in_list ? n l).
232 cut (\forall l:(list nat).\exists n.\forall m.
233         (n \leq m) \to \lnot (in_list ? m l))
234   [intros;lapply (Hcut l);elim Hletin;apply ex_intro
235      [apply a
236      |apply H;constructor 1]
237   |intros;elim l
238     [apply (ex_intro ? ? O);intros;unfold;intro;elim (not_in_list_nil ? ? H1)
239     |elim H;
240      apply (ex_intro ? ? (S (max a t))).
241      intros.unfold. intro.
242      elim (in_list_cons_case ? ? ? ? H3)
243       [rewrite > H4 in H2.autobatch
244       |elim H4
245          [apply (H1 m ? H4).apply (trans_le ? (max a t));autobatch
246          |assumption]]]]
247 qed.
248
249 (*** lemmata on well-formedness ***)
250
251 lemma fv_WFT : \forall T,x,G.(WFType G T) \to (in_list ? x (fv_type T)) \to
252                   (in_list ? x (fv_env G)).
253 intros 4.elim H
254   [simplify in H2;elim (in_list_cons_case ? ? ? ? H2)
255      [rewrite > H3;assumption|elim (not_in_list_nil ? ? H3)]
256   |simplify in H1;elim (not_in_list_nil ? x H1)
257   |simplify in H5;elim (in_list_append_to_or_in_list ? ? ? ? H5);autobatch
258   |simplify in H5;elim (in_list_append_to_or_in_list ? ? ? ? H5)
259      [apply (H2 H6)
260      |elim (fresh_name ((fv_type t1) @ (fv_env l)));
261       cut (¬ (a ∈ (fv_type t1)) ∧ ¬ (a ∈ (fv_env l)))
262         [elim Hcut;lapply (H4 ? H9 H8)
263            [cut (x ≠ a)
264               [simplify in Hletin;elim (in_list_cons_case ? ? ? ? Hletin)
265                  [elim (Hcut1 H10)
266                  |assumption]
267               |intro;apply H8;applyS H6]
268            |apply in_FV_subst;assumption]
269         |split
270            [intro;apply H7;apply in_list_to_in_list_append_l;assumption
271            |intro;apply H7;apply in_list_to_in_list_append_r;assumption]]]]
272 qed.
273
274 (*** lemmata relating subtyping and well-formedness ***)
275
276 lemma JS_to_WFE : \forall G,T,U.(G \vdash T ⊴ U) \to (WFEnv G).
277 intros;elim H;assumption.
278 qed.
279
280 lemma JS_to_WFT : \forall G,T,U.(JSubtype G T U) \to ((WFType G T) \land 
281                                                       (WFType G U)).
282 intros;elim H
283   [split [assumption|apply WFT_Top]
284   |split;apply WFT_TFree;assumption
285   |split 
286      [apply WFT_TFree;apply boundinenv_natinfv;apply ex_intro
287         [apply true | apply ex_intro [apply t1 |assumption]]
288      |elim H3;assumption]
289   |elim H2;elim H4;split;apply WFT_Arrow;assumption
290   |elim H2;split
291      [apply (WFT_Forall ? ? ? H6);intros;elim (H4 X H7);
292       apply (WFT_env_incl ? ? H9);simplify;unfold;intros;assumption
293      |apply (WFT_Forall ? ? ? H5);intros;elim (H4 X H7);
294       apply (WFT_env_incl ? ? H10);simplify;unfold;intros;assumption]]
295 qed.
296
297 lemma JS_to_WFT1 : \forall G,T,U.(JSubtype G T U) \to (WFType G T).
298 intros;lapply (JS_to_WFT ? ? ? H);elim Hletin;assumption.
299 qed.
300
301 lemma JS_to_WFT2 : \forall G,T,U.(JSubtype G T U) \to (WFType G U).
302 intros;lapply (JS_to_WFT ? ? ? H);elim Hletin;assumption.
303 qed.
304
305 lemma WFE_Typ_subst : \forall H,x,B,C,T,U,G.
306                       (WFEnv (H @ ((mk_bound B x T) :: G))) \to (WFType G U) \to
307                       (WFEnv (H @ ((mk_bound C x U) :: G))).
308 intros 7;elim H 0
309   [simplify;intros;(*FIXME*)generalize in match H1;intro;inversion H1;intros
310      [lapply (nil_cons ? G (mk_bound B x T));elim (Hletin H4)
311      |destruct H8;apply (WFE_cons ? ? ? ? H4 H6 H2)]
312   |intros;simplify;generalize in match H2;elim t;simplify in H4;
313    inversion H4;intros
314      [destruct H5
315      |destruct H9;apply WFE_cons
316         [apply (H1 H5 H3)
317         |rewrite < (fv_env_extends ? x B C T U); assumption
318         |apply (WFT_env_incl ? ? H8);
319          rewrite < (fv_env_extends ? x B C T U);unfold;intros;
320          assumption]]]
321 qed.
322
323 lemma WFE_bound_bound : \forall B,x,T,U,G. (WFEnv G) \to
324                                   (in_list ? (mk_bound B x T) G) \to
325                                   (in_list ? (mk_bound B x U) G) \to T = U.
326 intros 6;elim H
327   [lapply (not_in_list_nil ? ? H1);elim Hletin
328   |elim (in_list_cons_case ? ? ? ? H6)
329      [destruct H7;destruct;elim (in_list_cons_case ? ? ? ? H5)
330         [destruct H7;reflexivity
331         |elim H7;elim H3;apply boundinenv_natinfv;apply (ex_intro ? ? B);
332          apply (ex_intro ? ? T);assumption]
333      |elim (in_list_cons_case ? ? ? ? H5)
334         [destruct H8;elim H3;apply boundinenv_natinfv;apply (ex_intro ? ? B);
335          apply (ex_intro ? ? U);assumption
336         |apply (H2 H8 H7)]]]
337 qed.
338
339 lemma WFT_to_incl: ∀G,T,U.
340   (∀X.(¬(X ∈ fv_env G)) → (¬(X ∈ fv_type U)) →
341     (WFType (mk_bound true X T::G) (subst_type_nat U (TFree X) O)))
342   → incl ? (fv_type U) (fv_env G).
343 intros.elim (fresh_name ((fv_type U)@(fv_env G))).lapply(H a)
344   [unfold;intros;lapply (fv_WFT ? x ? Hletin)
345      [simplify in Hletin1;inversion Hletin1;intros
346         [destruct H4;elim H1;autobatch
347         |destruct H6;assumption]
348      |apply in_FV_subst;assumption]
349   |*:intro;apply H1;autobatch]
350 qed.
351
352 lemma incl_fv_env: ∀X,G,G1,U,P.
353       incl ? (fv_env (G1@(mk_bound true X U::G))) 
354              (fv_env (G1@(mk_bound true X P::G))).
355 intros.rewrite < fv_env_extends.apply incl_A_A.
356 qed.
357
358 lemma JSubtype_Top: ∀G,P.G ⊢ ⊤ ⊴ P → P = ⊤.
359 intros.inversion H;intros
360   [assumption|reflexivity
361   |destruct H5|*:destruct H6]
362 qed.
363
364 (*
365 (* elim vs inversion *)
366 lemma JS_trans_TFree: ∀G,T,X.G ⊢ T ⊴ (TFree X) →
367   ∀U.G ⊢ (TFree X) ⊴ U → G ⊢ T ⊴ U.
368 intros 4.cut (∀Y.TFree Y = TFree X → ∀U.G ⊢ (TFree Y) ⊴ U → G ⊢ T ⊴ U)
369   [apply Hcut;reflexivity
370   |elim H;intros
371     [rewrite > H3 in H4;rewrite > (JSubtype_Top ? ? H4);apply SA_Top;assumption
372     |rewrite < H3;assumption
373     |apply (SA_Trans_TVar ? ? ? ? H1);apply (H3 Y);assumption
374     |*:destruct H5]]
375 qed.
376 *)
377
378 lemma fv_append : ∀G,H.fv_env (G @ H) = (fv_env G @ fv_env H).
379 intro;elim G;simplify;autobatch paramodulation;
380 qed.