1 (**************************************************************************)
4 (* ||A|| A project by Andrea Asperti *)
6 (* ||I|| Developers: *)
7 (* ||T|| The HELM team. *)
8 (* ||A|| http://helm.cs.unibo.it *)
10 (* \ / This file is distributed under the terms of the *)
11 (* v GNU General Public License Version 2 *)
13 (**************************************************************************)
15 set "baseuri" "cic:/matita/Fsub/defn".
16 include "Fsub/util.ma".
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 *)
26 (* representation of bounds *)
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 *)
34 (*** Various kinds of substitution, not all will be used probably ***)
36 (* substitutes i-th dangling index in type T with type U *)
37 let rec subst_type_nat T U i \def
39 [ (TVar n) \Rightarrow match (eqb n i) with
41 | false \Rightarrow T]
42 | (TFree X) \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))) ].
47 (*** height of T's syntactic tree ***)
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)))].
57 (*** definitions about lists ***)
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).
63 let rec fv_type T \def
65 [(TVar n) \Rightarrow []
66 |(TFree x) \Rightarrow [x]
68 |(Arrow U V) \Rightarrow ((fv_type U) @ (fv_type V))
69 |(Forall U V) \Rightarrow ((fv_type U) @ (fv_type V))].
71 (*** Type Well-Formedness judgement ***)
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
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)).
87 (*** Environment Well-Formedness judgement ***)
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)).
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.
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)).
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).
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).
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).
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).
136 notation "⊤" with precedence 90 for @{'toptype}.
137 interpretation "toptype" 'toptype =
138 (cic:/matita/Fsub/defn/Typ.ind#xpointer(1/1/3)).
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).
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).
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).
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).
160 (****** PROOFS ********)
162 (*** theorems about lists ***)
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)).
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]]
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).
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]]]
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)).
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]]
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).
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
205 |intro;destruct H7;elim (H3 Hcut1)]]]
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))).
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;
219 |simplify in H2;simplify;elim (nat_in_list_case ? ? ? H2);
220 apply natinG_or_inH_to_natinGH;
222 |right;apply (H H3)]]
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
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)]
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).
244 [apply WFT_TFree;unfold in H3;apply (H3 ? H1)
246 |apply WFT_Arrow [apply (H2 ? H6)|apply (H4 ? H6)]
249 |intros;apply (H4 ? ? H8)
250 [unfold;intro;apply H7;apply(H6 ? H9)
251 |simplify;apply (incl_nat_cons ? ? ? H6)]]]
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))).
258 [simplify;reflexivity|elim t;simplify;rewrite > H1;reflexivity]
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
264 (in_list ? (mk_bound D y V) (H @ ((mk_bound B x T) :: G))).
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;
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)]]
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))).
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)]]
286 (*** lemma on fresh names ***)
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
293 |apply H;constructor 1]
295 [apply (ex_intro ? ? O);intros;unfold;intro;elim (in_list_nil ? ? H1)
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]]]
305 (*** lemmata on well-formedness ***)
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)).
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)
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]
325 [intro;apply H7;apply natinG_or_inH_to_natinGH;right;assumption
326 |intro;apply H7;apply natinG_or_inH_to_natinGH;left;assumption]]
330 (*** some exotic inductions and related lemmas ***)
332 lemma O_lt_t_len: \forall T.O < (t_len T).
334 [1,2,3:simplify;apply le_n
335 |*:simplify;apply lt_O_S]
339 lemma not_t_len_lt_SO : \forall T.\lnot (t_len T) < (S O).
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]]
348 lemma Typ_len_ind : \forall P:Typ \to Prop.
349 (\forall U.(\forall V.((t_len V) < (t_len U)) \to (P V))
352 cut (\forall P:Typ \to Prop.
353 (\forall U.(\forall V.((t_len V) < (t_len U)) \to (P V))
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
365 lemma t_len_arrow1 : \forall T1,T2.(t_len T1) < (t_len (Arrow T1 T2)).
367 apply le_S_S.apply le_m_max_m_n.
370 lemma t_len_arrow2 : \forall T1,T2.(t_len T2) < (t_len (Arrow T1 T2)).
372 apply le_S_S.apply le_n_max_m_n.
375 lemma t_len_forall1 : \forall T1,T2.(t_len T1) < (t_len (Forall T1 T2)).
377 apply le_S_S.apply le_m_max_m_n.
380 lemma t_len_forall2 : \forall T1,T2.(t_len T2) < (t_len (Forall T1 T2)).
382 apply le_S_S.apply le_n_max_m_n.
385 lemma eq_t_len_TFree_subst : \forall T,n,X.(t_len T) =
386 (t_len (subst_type_nat T (TFree X) n)).
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;
392 |simplify;lapply (H n X);lapply (H1 (S n) X);rewrite < Hletin;
393 rewrite < Hletin1;reflexivity]
396 (*** lemmata relating subtyping and well-formedness ***)
398 lemma JS_to_WFE : \forall G,T,U.(G \vdash T ⊴ U) \to (WFEnv G).
399 intros;elim H;assumption.
402 lemma JS_to_WFT : \forall G,T,U.(JSubtype G T U) \to ((WFType G T) \land
405 [split [assumption|apply WFT_Top]
406 |split;apply WFT_TFree;assumption
408 [apply WFT_TFree;apply boundinenv_natinfv;apply ex_intro
409 [apply true | apply ex_intro [apply t1 |assumption]]
411 |elim H2;elim H4;split;apply WFT_Arrow;assumption
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]]
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.
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.
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))).
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;
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;
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]]]
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.
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)]]]