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 ***)
19 | TVar : nat → Typ (* type var *)
20 | TFree: nat → Typ (* free type name *)
21 | Top : Typ (* maximum type *)
22 | Arrow : Typ → Typ → Typ (* functions *)
23 | Forall : Typ → Typ → Typ. (* universal type *)
25 (* representation of bounds *)
27 record bound : Set ≝ {
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 ≝
38 [ TVar n ⇒ match eqb n i with
43 | Arrow T1 T2 ⇒ Arrow (subst_type_nat T1 U i) (subst_type_nat T2 U i)
44 | Forall T1 T2 ⇒ Forall (subst_type_nat T1 U i) (subst_type_nat T2 U (S i)) ].
46 (*** definitions about lists ***)
48 definition filter_types : list bound → list bound ≝
49 λG.(filter ? G (λB.match B with [mk_bound B X T ⇒ B])).
51 definition fv_env : list bound → list nat ≝
52 λG.(map ? ? (λb.match b with [mk_bound B X T ⇒ X]) (filter_types G)).
59 |Arrow U V ⇒ fv_type U @ fv_type V
60 |Forall U V ⇒ fv_type U @ fv_type V].
62 (*** Type Well-Formedness judgement ***)
64 inductive WFType : list bound → Typ → Prop ≝
65 | WFT_TFree : ∀X,G.in_list ? X (fv_env G) → WFType G (TFree X)
66 | WFT_Top : ∀G.WFType G Top
67 | WFT_Arrow : ∀G,T,U.WFType G T → WFType G U → WFType G (Arrow T U)
68 | WFT_Forall : ∀G,T,U.WFType G T →
70 (¬ (in_list ? X (fv_env G))) →
71 (¬ (in_list ? X (fv_type U))) →
72 (WFType ((mk_bound true X T) :: G)
73 (subst_type_nat U (TFree X) O))) →
74 (WFType G (Forall T U)).
76 (*** Environment Well-Formedness judgement ***)
78 inductive WFEnv : list bound → Prop ≝
79 | WFE_Empty : WFEnv (nil ?)
80 | WFE_cons : ∀B,X,T,G.WFEnv G → ¬ (in_list ? X (fv_env G)) →
81 WFType G T → WFEnv ((mk_bound B X T) :: G).
83 (*** Subtyping judgement ***)
84 inductive JSubtype : list bound → Typ → Typ → Prop ≝
85 | SA_Top : ∀G,T.WFEnv G → WFType G T → JSubtype G T Top
86 | SA_Refl_TVar : ∀G,X.WFEnv G → in_list ? X (fv_env G)
87 → JSubtype G (TFree X) (TFree X)
88 | SA_Trans_TVar : ∀G,X,T,U.in_list ? (mk_bound true X U) G →
89 JSubtype G U T → JSubtype G (TFree X) T
90 | SA_Arrow : ∀G,S1,S2,T1,T2. JSubtype G T1 S1 → JSubtype G S2 T2 →
91 JSubtype G (Arrow S1 S2) (Arrow T1 T2)
92 | SA_All : ∀G,S1,S2,T1,T2. JSubtype G T1 S1 →
93 (∀X.¬ (in_list ? X (fv_env G)) →
94 JSubtype ((mk_bound true X T1) :: G)
95 (subst_type_nat S2 (TFree X) O) (subst_type_nat T2 (TFree X) O)) →
96 JSubtype G (Forall S1 S2) (Forall T1 T2).
98 notation "mstyle color #007f00 (hvbox(e ⊢ break ta ⊴ break tb))"
99 non associative with precedence 30 for @{ 'subjudg $e $ta $tb }.
100 interpretation "Fsub subtype judgement" 'subjudg e ta tb = (JSubtype e ta tb).
102 notation "mstyle color #007f00 (hvbox(e ⊢ ♦))"
103 non associative with precedence 30 for @{ 'wfejudg $e }.
104 interpretation "Fsub WF env judgement" 'wfejudg e = (WFEnv e).
106 notation "mstyle color #007f00 (hvbox(e ⊢ break t))"
107 non associative with precedence 30 for @{ 'wftjudg $e $t }.
108 interpretation "Fsub WF type judgement" 'wftjudg e t = (WFType e t).
110 notation > "\Forall S.T"
111 non associative with precedence 60 for @{ 'forall $S $T}.
112 notation < "hvbox(⊓ \sub S. break T)"
113 non associative with precedence 60 for @{ 'forall $S $T}.
114 interpretation "universal type" 'forall S T = (Forall S T).
116 notation "#x" with precedence 79 for @{'tvar $x}.
117 interpretation "bound tvar" 'tvar x = (TVar x).
119 notation "!x" with precedence 79 for @{'tname $x}.
120 interpretation "bound tname" 'tname x = (TFree x).
122 notation "⊤" with precedence 90 for @{'toptype}.
123 interpretation "toptype" 'toptype = Top.
125 notation "hvbox(s break ⇛ t)"
126 right associative with precedence 55 for @{ 'arrow $s $t }.
127 interpretation "arrow type" 'arrow S T = (Arrow S T).
129 notation "hvbox(S [#n ↦ T])"
130 non associative with precedence 80 for @{ 'substvar $S $T $n }.
131 interpretation "subst bound var" 'substvar S T n = (subst_type_nat S T n).
133 notation "hvbox(!X ⊴ T)"
134 non associative with precedence 60 for @{ 'subtypebound $X $T }.
135 interpretation "subtyping bound" 'subtypebound X T = (mk_bound true X T).
137 (****** PROOFS ********)
139 (*** theorems about lists ***)
141 lemma boundinenv_natinfv : ∀x,G.(∃T.!x ⊴ T ∈ G) → x ∈ (fv_env G).
142 intros 2;elim G;decompose
143 [elim (not_in_list_nil ? ? H1)
144 |elim (in_list_cons_case ? ? ? ? H2)
145 [rewrite < H1;simplify;apply in_list_head
146 |elim a;apply (bool_elim ? b);intro;simplify;try apply in_list_cons;
150 lemma natinfv_boundinenv : ∀x,G.x ∈ (fv_env G) → ∃T.!x ⊴ T ∈ G.
152 [simplify;intro;lapply (not_in_list_nil ? ? H);elim Hletin
154 elim a;simplify in H1;elim b in H1;simplify in H1
155 [elim (in_list_cons_case ? ? ? ? H1)
156 [rewrite < H2;autobatch
157 |elim (H H2);autobatch]
158 |elim (H H1);autobatch]]
161 lemma incl_bound_fv : ∀l1,l2.l1 ⊆ l2 → (fv_env l1) ⊆ (fv_env l2).
162 intros;unfold in H;unfold;intros;apply boundinenv_natinfv;
163 lapply (natinfv_boundinenv ? ? H1);decompose;autobatch depth=4;
166 lemma WFT_env_incl : ∀G,T.(G ⊢ T) → ∀H.fv_env G ⊆ fv_env H → (H ⊢ T).
168 [apply WFT_TFree;unfold in H3;apply (H3 ? H1)
170 |apply WFT_Arrow;autobatch
173 |intros;apply (H4 ? ? H8)
174 [unfold;intro;autobatch
175 |simplify;apply (incl_cons ???? H6)]]]
178 lemma fv_env_extends : ∀H,x,T,U,G,B.
179 fv_env (H @ mk_bound B x T :: G) =
180 fv_env (H @ mk_bound B x U :: G).
181 intros 5;elim H;elim B
183 |*:elim a;elim b;simplify;lapply (H1 true);lapply (H1 false);
184 try rewrite > Hletin;try rewrite > Hletin1;reflexivity]
187 lemma lookup_env_extends : ∀G,H,B,C,D,T,U,V,x,y.
188 mk_bound D y V ∈ H @ mk_bound C x U :: G → y ≠ x →
189 mk_bound D y V ∈ H @ mk_bound B x T :: G.
191 [simplify in H1;elim (in_list_cons_case ? ? ? ? H1)
192 [destruct H3;elim H2;reflexivity
193 |simplify;apply (in_list_cons ? ? ? ? H3);]
194 |simplify in H2;simplify;elim (in_list_cons_case ? ? ? ? H2)
195 [rewrite > H4;apply in_list_head
196 |apply (in_list_cons ? ? ? ? (H1 H4 H3))]]
199 lemma in_FV_subst : ∀x,T,U,n.x ∈ fv_type T → x ∈ fv_type (subst_type_nat T U n).
201 [simplify in H;elim (not_in_list_nil ? ? H)
202 |2,3:simplify;simplify in H;assumption
203 |*:simplify in H2;simplify;elim (in_list_append_to_or_in_list ? ? ? ? H2);
207 (*** lemma on fresh names ***)
209 lemma fresh_name : ∀l:list nat.∃n.n ∉ l.
210 cut (∀l:list nat.∃n.∀m.n ≤ m → ¬ in_list ? m l);intros
211 [lapply (Hcut l);elim Hletin;apply ex_intro;autobatch
213 [apply ex_intro[apply O];intros;unfold;intro;elim (not_in_list_nil ? ? H1)
214 |elim H;apply ex_intro[apply (S (max a1 a))];
216 elim (in_list_cons_case ? ? ? ? H3)
217 [rewrite > H4 in H2;autobatch
219 [apply (H1 m ? H4);autobatch
223 (*** lemmata on well-formedness ***)
225 lemma fv_WFT : ∀T,x,G.(G ⊢ T) → x ∈ fv_type T → x ∈ fv_env G.
227 [simplify in H2;elim (in_list_cons_case ? ? ? ? H2)
228 [applyS H1|elim (not_in_list_nil ? ? H3)]
229 |simplify in H1;elim (not_in_list_nil ? x H1)
230 |simplify in H5;elim (in_list_append_to_or_in_list ? ? ? ? H5);autobatch
231 |simplify in H5;elim (in_list_append_to_or_in_list ? ? ? ? H5)
233 |elim (fresh_name (fv_type t1 @ fv_env l));
234 cut (¬ in_list ? a (fv_type t1) ∧ ¬ in_list ? a (fv_env l))
235 [elim Hcut;lapply (H4 ? H9 H8)
237 [simplify in Hletin;elim (in_list_cons_case ? ? ? ? Hletin)
240 |intro;apply H8;applyS H6]
242 |split;intro;apply H7;autobatch]]]
245 (*** lemmata relating subtyping and well-formedness ***)
247 lemma JS_to_WFE : ∀G,T,U.G ⊢ T ⊴ U → G ⊢ ♦.
248 intros;elim H;assumption.
251 lemma JS_to_WFT : ∀G,T,U.G ⊢ T ⊴ U → (G ⊢ T) ∧ (G ⊢ U).
255 [apply WFT_TFree;(* FIXME! qui bastava autobatch, ma si e` rotto *) apply boundinenv_natinfv;autobatch
257 |decompose;autobatch size=7
259 [apply (WFT_Forall ? ? ? H6);intros;elim (H4 X H7);
260 apply (WFT_env_incl ? ? H9);simplify;unfold;intros;assumption
261 |apply (WFT_Forall ? ? ? H5);intros;elim (H4 X H7);
262 apply (WFT_env_incl ? ? H10);simplify;unfold;intros;assumption]]
265 lemma JS_to_WFT1 : ∀G,T,U.G ⊢ T ⊴ U → G ⊢ T.
266 intros;elim (JS_to_WFT ? ? ? H);assumption;
269 lemma JS_to_WFT2 : ∀G,T,U.G ⊢ T ⊴ U → G ⊢ U.
270 intros;elim (JS_to_WFT ? ? ? H);assumption;
273 lemma WFE_Typ_subst : ∀H,x,B,T,U,G.
274 H @ mk_bound B x T :: G ⊢ ♦ → (G ⊢ U) →
275 H @ mk_bound B x U :: G ⊢ ♦.
277 [simplify;intros;inversion H1;intros
278 [elim (nil_cons ? G (mk_bound B x T) H3)
279 |destruct H7;autobatch]
280 |intros;simplify;generalize in match H2;elim a;simplify in H4;
283 |destruct H9;apply WFE_cons
285 |rewrite < (fv_env_extends ? x T U); assumption
286 |apply (WFT_env_incl ? ? H8);
287 rewrite < (fv_env_extends ? x T U);unfold;intros;
291 lemma WFE_bound_bound : ∀x,T,U,G.G ⊢ ♦ → !x ⊴ T ∈ G → !x ⊴ U ∈ G → T = U.
293 [lapply (not_in_list_nil ? ? H1);elim Hletin
294 |elim (in_list_cons_case ? ? ? ? H6)
295 [destruct H7;destruct;elim (in_list_cons_case ? ? ? ? H5)
296 [destruct H7;reflexivity
297 |elim H7;elim H3;apply boundinenv_natinfv;autobatch]
298 |elim (in_list_cons_case ? ? ? ? H5)
299 [destruct H8;elim H3;apply boundinenv_natinfv;autobatch
303 lemma WFT_to_incl: ∀G,T,U.(∀X.X ∉ fv_env G → X ∉ fv_type U →
304 (mk_bound true X T::G ⊢ (subst_type_nat U (TFree X) O))) →
305 fv_type U ⊆ fv_env G.
306 intros;elim (fresh_name (fv_type U@fv_env G));lapply(H a)
307 [unfold;intros;lapply (fv_WFT ? x ? Hletin)
308 [simplify in Hletin1;inversion Hletin1;intros
309 [destruct H4;elim H1;autobatch
310 |destruct H6;assumption]
311 |apply in_FV_subst;assumption]
312 |*:intro;apply H1;autobatch]
315 lemma incl_fv_env: ∀X,G,G1,U,P.
316 fv_env (G1@ !X ⊴ U::G) ⊆ fv_env (G1@ !X ⊴ P::G).
317 intros.rewrite < fv_env_extends.apply incl_A_A.
320 lemma fv_append : ∀G,H.fv_env (G @ H) = fv_env G @ fv_env H.
321 intro;elim G;simplify;
323 |elim a;simplify;elim b;simplify;lapply (H H1);rewrite > Hletin;reflexivity]