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 |simplify;apply in_Skip;apply H;apply (ex_intro ? ? a);
172 apply (ex_intro ? ? a1);assumption]]
175 lemma natinfv_boundinenv : \forall x,G.(in_list ? x (fv_env G)) \to
176 \exists B,T.(in_list ? (mk_bound B x T) G).
178 [simplify;intro;lapply (in_list_nil ? ? H);elim Hletin
179 |intros 3;elim t;simplify in H1;elim (in_cons_case ? ? ? ? H1)
180 [rewrite < H2;apply (ex_intro ? ? b);apply (ex_intro ? ? t1);apply in_Base
181 |elim (H H2);elim H3;apply (ex_intro ? ? a);
182 apply (ex_intro ? ? a1);apply in_Skip;assumption]]
185 lemma incl_bound_fv : \forall l1,l2.(incl ? l1 l2) \to
186 (incl ? (fv_env l1) (fv_env l2)).
187 intros.unfold in H.unfold.intros.apply boundinenv_natinfv.
188 lapply (natinfv_boundinenv ? ? H1).elim Hletin.elim H2.apply ex_intro
195 lemma incl_cons : \forall x,l1,l2.
196 (incl ? l1 l2) \to (incl nat (x :: l1) (x :: l2)).
197 intros.unfold in H.unfold.intros.elim (in_cons_case ? ? ? ? H1)
198 [rewrite > H2;apply in_Base|apply in_Skip;apply (H ? H2)]
201 lemma WFT_env_incl : \forall G,T.(WFType G T) \to
202 \forall H.(incl ? (fv_env G) (fv_env H)) \to (WFType H T).
204 [apply WFT_TFree;unfold in H3;apply (H3 ? H1)
206 |apply WFT_Arrow [apply (H2 ? H6)|apply (H4 ? H6)]
209 |intros;apply (H4 ? ? H8)
210 [unfold;intro;apply H7;apply(H6 ? H9)
211 |simplify;apply (incl_cons ? ? ? H6)]]]
214 lemma fv_env_extends : \forall H,x,B,C,T,U,G.
215 (fv_env (H @ ((mk_bound B x T) :: G))) =
216 (fv_env (H @ ((mk_bound C x U) :: G))).
218 [simplify;reflexivity|elim t;simplify;rewrite > H1;reflexivity]
221 lemma lookup_env_extends : \forall G,H,B,C,D,T,U,V,x,y.
222 (in_list ? (mk_bound D y V) (H @ ((mk_bound C x U) :: G))) \to
224 (in_list ? (mk_bound D y V) (H @ ((mk_bound B x T) :: G))).
226 [simplify in H1;elim (in_cons_case ? ? ? ? H1)
227 [destruct H3;elim (H2);reflexivity
228 |simplify;apply (in_Skip ? ? ? ? H3);]
229 |simplify in H2;simplify;elim (in_cons_case ? ? ? ? H2)
230 [rewrite > H4;apply in_Base
231 |apply (in_Skip ? ? ? ? (H1 H4 H3))]]
234 lemma in_FV_subst : \forall x,T,U,n.(in_list ? x (fv_type T)) \to
235 (in_list ? x (fv_type (subst_type_nat T U n))).
237 [simplify in H;elim (in_list_nil ? ? H)
238 |2,3:simplify;simplify in H;assumption
239 |*:simplify in H2;simplify;elim (append_to_or_in_list ? ? ? ? H2)
240 [1,3:apply in_list_append1;apply (H ? H3)
241 |*:apply in_list_append2;apply (H1 ? H3)]]
244 (*** lemma on fresh names ***)
246 lemma fresh_name : \forall l:(list nat).\exists n.\lnot (in_list ? n l).
247 cut (\forall l:(list nat).\exists n.\forall m.
248 (n \leq m) \to \lnot (in_list ? m l))
249 [intros;lapply (Hcut l);elim Hletin;apply ex_intro
251 |apply H;constructor 1]
253 [apply (ex_intro ? ? O);intros;unfold;intro;elim (in_list_nil ? ? H1)
255 apply (ex_intro ? ? (S (max a t))).
256 intros.unfold. intro.
257 elim (in_cons_case ? ? ? ? H3)
258 [rewrite > H4 in H2.autobatch
260 [apply (H1 m ? H4).apply (trans_le ? (max a t));autobatch
264 (*** lemmata on well-formedness ***)
266 lemma fv_WFT : \forall T,x,G.(WFType G T) \to (in_list ? x (fv_type T)) \to
267 (in_list ? x (fv_env G)).
269 [simplify in H2;elim (in_cons_case ? ? ? ? H2)
270 [rewrite > H3;assumption|elim (in_list_nil ? ? H3)]
271 |simplify in H1;elim (in_list_nil ? x H1)
272 |simplify in H5;elim (append_to_or_in_list ? ? ? ? H5);autobatch
273 |simplify in H5;elim (append_to_or_in_list ? ? ? ? H5)
275 |elim (fresh_name ((fv_type t1) @ (fv_env l)));
276 cut (¬ (a ∈ (fv_type t1)) ∧ ¬ (a ∈ (fv_env l)))
277 [elim Hcut;lapply (H4 ? H9 H8)
279 [simplify in Hletin;elim (in_cons_case ? ? ? ? Hletin)
282 |intro;apply H8;applyS H6]
283 |apply in_FV_subst;assumption]
285 [intro;apply H7;apply in_list_append1;assumption
286 |intro;apply H7;apply in_list_append2;assumption]]]]
289 (*** lemmata relating subtyping and well-formedness ***)
291 lemma JS_to_WFE : \forall G,T,U.(G \vdash T ⊴ U) \to (WFEnv G).
292 intros;elim H;assumption.
295 lemma JS_to_WFT : \forall G,T,U.(JSubtype G T U) \to ((WFType G T) \land
298 [split [assumption|apply WFT_Top]
299 |split;apply WFT_TFree;assumption
301 [apply WFT_TFree;apply boundinenv_natinfv;apply ex_intro
302 [apply true | apply ex_intro [apply t1 |assumption]]
304 |elim H2;elim H4;split;apply WFT_Arrow;assumption
306 [apply (WFT_Forall ? ? ? H6);intros;elim (H4 X H7);
307 apply (WFT_env_incl ? ? H9);simplify;unfold;intros;assumption
308 |apply (WFT_Forall ? ? ? H5);intros;elim (H4 X H7);
309 apply (WFT_env_incl ? ? H10);simplify;unfold;intros;assumption]]
312 lemma JS_to_WFT1 : \forall G,T,U.(JSubtype G T U) \to (WFType G T).
313 intros;lapply (JS_to_WFT ? ? ? H);elim Hletin;assumption.
316 lemma JS_to_WFT2 : \forall G,T,U.(JSubtype G T U) \to (WFType G U).
317 intros;lapply (JS_to_WFT ? ? ? H);elim Hletin;assumption.
320 lemma WFE_Typ_subst : \forall H,x,B,C,T,U,G.
321 (WFEnv (H @ ((mk_bound B x T) :: G))) \to (WFType G U) \to
322 (WFEnv (H @ ((mk_bound C x U) :: G))).
324 [simplify;intros;(*FIXME*)generalize in match H1;intro;inversion H1;intros
325 [lapply (nil_cons ? G (mk_bound B x T));elim (Hletin H4)
326 |destruct H8;apply (WFE_cons ? ? ? ? H4 H6 H2)]
327 |intros;simplify;generalize in match H2;elim t;simplify in H4;
330 |destruct H9;apply WFE_cons
332 |rewrite < (fv_env_extends ? x B C T U); assumption
333 |apply (WFT_env_incl ? ? H8);
334 rewrite < (fv_env_extends ? x B C T U);unfold;intros;
338 lemma WFE_bound_bound : \forall B,x,T,U,G. (WFEnv G) \to
339 (in_list ? (mk_bound B x T) G) \to
340 (in_list ? (mk_bound B x U) G) \to T = U.
342 [lapply (in_list_nil ? ? H1);elim Hletin
343 |elim (in_cons_case ? ? ? ? H6)
344 [destruct H7;destruct;elim (in_cons_case ? ? ? ? H5)
345 [destruct H7;reflexivity
346 |elim H7;elim H3;apply boundinenv_natinfv;apply (ex_intro ? ? B);
347 apply (ex_intro ? ? T);assumption]
348 |elim (in_cons_case ? ? ? ? H5)
349 [destruct H8;elim H3;apply boundinenv_natinfv;apply (ex_intro ? ? B);
350 apply (ex_intro ? ? U);assumption
354 lemma WFT_to_incl: ∀G,T,U.
355 (∀X.(¬(X ∈ fv_env G)) → (¬(X ∈ fv_type U)) →
356 (WFType (mk_bound true X T::G) (subst_type_nat U (TFree X) O)))
357 → incl ? (fv_type U) (fv_env G).
358 intros.elim (fresh_name ((fv_type U)@(fv_env G))).lapply(H a)
359 [unfold;intros;lapply (fv_WFT ? x ? Hletin)
360 [simplify in Hletin1;inversion Hletin1;intros
361 [destruct H4;elim H1;autobatch
362 |destruct H6;assumption]
363 |apply in_FV_subst;assumption]
364 |*:intro;apply H1;autobatch]
367 lemma incl_fv_env: ∀X,G,G1,U,P.
368 incl ? (fv_env (G1@(mk_bound true X U::G)))
369 (fv_env (G1@(mk_bound true X P::G))).
370 intros.rewrite < fv_env_extends.apply incl_A_A.
373 lemma JSubtype_Top: ∀G,P.G ⊢ ⊤ ⊴ P → P = ⊤.
374 intros.inversion H;intros
375 [assumption|reflexivity
376 |destruct H5|*:destruct H6]
379 (* elim vs inversion *)
380 lemma JS_trans_TFree: ∀G,T,X.G ⊢ T ⊴ (TFree X) →
381 ∀U.G ⊢ (TFree X) ⊴ U → G ⊢ T ⊴ U.
382 intros 4.cut (∀Y.TFree Y = TFree X → ∀U.G ⊢ (TFree Y) ⊴ U → G ⊢ T ⊴ U)
383 [apply Hcut;reflexivity
385 [rewrite > H3 in H4;rewrite > (JSubtype_Top ? ? H4);apply SA_Top;assumption
386 |rewrite < H3;assumption
387 |apply (SA_Trans_TVar ? ? ? ? H1);apply (H3 Y);assumption
391 lemma fv_append : ∀G,H.fv_env (G @ H) = (fv_env G @ fv_env H).
392 intro;elim G;simplify;autobatch paramodulation;