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