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 include "Fsub/util.ma".
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 *)
25 (* representation of bounds *)
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 *)
33 (*** Various kinds of substitution, not all will be used probably ***)
35 (* substitutes i-th dangling index in type T with type U *)
36 let rec subst_type_nat T U i \def
38 [ (TVar n) \Rightarrow match (eqb n i) with
40 | false \Rightarrow T]
41 | (TFree X) \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))) ].
46 (*** definitions about lists ***)
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).
52 let rec fv_type T \def
54 [(TVar n) \Rightarrow []
55 |(TFree x) \Rightarrow [x]
57 |(Arrow U V) \Rightarrow ((fv_type U) @ (fv_type V))
58 |(Forall U V) \Rightarrow ((fv_type U) @ (fv_type V))].
60 (*** Type Well-Formedness judgement ***)
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
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)).
76 (*** Environment Well-Formedness judgement ***)
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)).
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.
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)).
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).
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).
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).
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).
125 notation "⊤" with precedence 90 for @{'toptype}.
126 interpretation "toptype" 'toptype =
127 (cic:/matita/Fsub/defn2/Typ.ind#xpointer(1/1/3)).
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).
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).
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).
144 (****** PROOFS ********)
146 (*** theorems about lists ***)
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)).
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 ? ? a1);
156 apply (ex_intro ? ? a2);assumption]]
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).
162 [simplify;intro;lapply (not_in_list_nil ? ? H);elim Hletin
164 elim a;simplify in H1;elim (in_list_cons_case ? ? ? ? H1)
165 [rewrite < H2;apply (ex_intro ? ? b);apply (ex_intro ? ? t);apply in_list_head
166 |elim (H H2);elim H3;apply (ex_intro ? ? a1);
167 apply (ex_intro ? ? a2);apply in_list_cons;assumption]]
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
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)]
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).
189 [apply WFT_TFree;unfold in H3;apply (H3 ? H1)
191 |apply WFT_Arrow [apply (H2 ? H6)|apply (H4 ? H6)]
194 |intros;apply (H4 ? ? H8)
195 [unfold;intro;apply H7;apply(H6 ? H9)
196 |simplify;apply (incl_cons ? ? ? H6)]]]
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))).
203 [simplify;reflexivity|elim a;simplify;rewrite > H1;reflexivity]
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
209 (in_list ? (mk_bound D y V) (H @ ((mk_bound B x T) :: G))).
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))]]
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))).
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)]]
229 (*** lemma on fresh names ***)
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
236 |apply H;constructor 1]
238 [apply (ex_intro ? ? O);intros;unfold;intro;elim (not_in_list_nil ? ? H1)
240 apply (ex_intro ? ? (S (max a1 a))).
241 intros.unfold. intro.
242 elim (in_list_cons_case ? ? ? ? H3)
243 [rewrite > H4 in H2.autobatch
245 [apply (H1 m ? H4).apply (trans_le ? (max a1 a));autobatch
249 (*** lemmata on well-formedness ***)
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)).
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)
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)
264 [simplify in Hletin;elim (in_list_cons_case ? ? ? ? Hletin)
267 |intro;apply H8;applyS H6]
268 |apply in_FV_subst;assumption]
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]]]]
274 (*** lemmata relating subtyping and well-formedness ***)
276 lemma JS_to_WFE : \forall G,T,U.(G \vdash T ⊴ U) \to (WFEnv G).
277 intros;elim H;assumption.
280 lemma JS_to_WFT : \forall G,T,U.(JSubtype G T U) \to ((WFType G T) \land
283 [split [assumption|apply WFT_Top]
284 |split;apply WFT_TFree;assumption
286 [apply WFT_TFree;apply boundinenv_natinfv;apply ex_intro
287 [apply true | apply ex_intro [apply t1 |assumption]]
289 |elim H2;elim H4;split;apply WFT_Arrow;assumption
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]]
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.
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.
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))).
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 a;simplify in H4;
315 |destruct H9;apply WFE_cons
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;
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.
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
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]
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.
358 lemma fv_append : ∀G,H.fv_env (G @ H) = (fv_env G @ fv_env H).
359 intro;elim G;simplify;autobatch paramodulation;