]> matita.cs.unibo.it Git - helm.git/blob - matita/library/Fsub/defn.ma
new implementation of the destruct tactic,
[helm.git] / matita / library / Fsub / defn.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 set "baseuri" "cic:/matita/Fsub/defn".
16 include "Fsub/util.ma".
17
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 *)
25
26 (* representation of bounds *)
27
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 *)
32                         }.
33                
34 (*** Various kinds of substitution, not all will be used probably ***)
35
36 (* substitutes i-th dangling index in type T with type U *)
37 let rec subst_type_nat T U i \def
38     match T with
39     [ (TVar n) \Rightarrow match (eqb n i) with
40       [ true \Rightarrow U
41       | false \Rightarrow T]
42     | (TFree X) \Rightarrow T
43     | Top \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))) ].
46
47 (*** height of T's syntactic tree ***)
48
49 let rec t_len T \def
50   match T with
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)))].
56
57 (*** definitions about lists ***)
58
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).
62
63 let rec fv_type T \def
64   match T with
65     [(TVar n) \Rightarrow []
66     |(TFree x) \Rightarrow [x]
67     |Top \Rightarrow []
68     |(Arrow U V) \Rightarrow ((fv_type U) @ (fv_type V))
69     |(Forall U V) \Rightarrow ((fv_type U) @ (fv_type V))].
70
71 (*** Type Well-Formedness judgement ***)
72
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
80                  (\forall X:nat.
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)).
86
87 (*** Environment Well-Formedness judgement ***)
88
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)).
94             
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.
103                     \forall U: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)).
115
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).
120
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).
127   
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).
131
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).
135   
136 notation "⊤" with precedence 90 for @{'toptype}.
137 interpretation "toptype" 'toptype = 
138   (cic:/matita/Fsub/defn/Typ.ind#xpointer(1/1/3)).
139
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).
144   
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).  
149
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).  
154
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).  
159
160 (****** PROOFS ********)
161
162 (*** theorems about lists ***)
163
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)).
167 intros 2;elim 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]]
173 qed.
174
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).
177 intros 2;elim G 0
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]]
183 qed.
184
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
189   [apply a
190   |apply ex_intro
191      [apply a1
192      |apply (H ? H3)]]
193 qed.
194
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)]
199 qed.
200
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).
203 intros 3.elim H
204   [apply WFT_TFree;unfold in H3;apply (H3 ? H1)
205   |apply WFT_Top
206   |apply WFT_Arrow [apply (H2 ? H6)|apply (H4 ? H6)]
207   |apply WFT_Forall 
208      [apply (H2 ? H6)
209      |intros;apply (H4 ? ? H8)
210         [unfold;intro;apply H7;apply(H6 ? H9)
211         |simplify;apply (incl_cons ? ? ? H6)]]]
212 qed.
213
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))).
217 intros;elim H
218   [simplify;reflexivity|elim t;simplify;rewrite > H1;reflexivity]
219 qed.
220
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
223             (y \neq x) \to
224             (in_list ? (mk_bound D y V) (H @ ((mk_bound B x T) :: G))).
225 intros 10;elim H
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))]]
232 qed.
233
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))).
236 intros 3;elim T
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)]]
242 qed.
243
244 (*** lemma on fresh names ***)
245
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
250      [apply a
251      |apply H;constructor 1]
252   |intros;elim l
253     [apply (ex_intro ? ? O);intros;unfold;intro;elim (in_list_nil ? ? H1)
254     |elim H;
255      apply (ex_intro ? ? (S (max a t))).
256      intros.unfold. intro.
257      elim (in_cons_case ? ? ? ? H3)
258       [rewrite > H4 in H2.autobatch
259       |elim H4
260          [apply (H1 m ? H4).apply (trans_le ? (max a t));autobatch
261          |assumption]]]]
262 qed.
263
264 (*** lemmata on well-formedness ***)
265
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)).
268 intros 4.elim H
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)
274      [apply (H2 H6)
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)
278            [cut (x ≠ a)
279               [simplify in Hletin;elim (in_cons_case ? ? ? ? Hletin)
280                  [elim (Hcut1 H10)
281                  |assumption]
282               |intro;apply H8;applyS H6]
283            |apply in_FV_subst;assumption]
284         |split
285            [intro;apply H7;apply in_list_append1;assumption
286            |intro;apply H7;apply in_list_append2;assumption]]]]
287 qed.
288
289 (*** lemmata relating subtyping and well-formedness ***)
290
291 lemma JS_to_WFE : \forall G,T,U.(G \vdash T ⊴ U) \to (WFEnv G).
292 intros;elim H;assumption.
293 qed.
294
295 lemma JS_to_WFT : \forall G,T,U.(JSubtype G T U) \to ((WFType G T) \land 
296                                                       (WFType G U)).
297 intros;elim H
298   [split [assumption|apply WFT_Top]
299   |split;apply WFT_TFree;assumption
300   |split 
301      [apply WFT_TFree;apply boundinenv_natinfv;apply ex_intro
302         [apply true | apply ex_intro [apply t1 |assumption]]
303      |elim H3;assumption]
304   |elim H2;elim H4;split;apply WFT_Arrow;assumption
305   |elim H2;split
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]]
310 qed.
311
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.
314 qed.
315
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.
318 qed.
319
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))).
323 intros 7;elim H 0
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;
328    inversion H4;intros
329      [destruct H5
330      |destruct H9;apply WFE_cons
331         [apply (H1 H5 H3)
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;
335          assumption]]]
336 qed.
337
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.
341 intros 6;elim H
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
351         |apply (H2 H8 H7)]]]
352 qed.
353
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]
365 qed.
366
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.
371 qed.
372
373 lemma JSubtype_Top: ∀G,P.G ⊢ ⊤ ⊴ P → P = ⊤.
374 intros.inversion H;intros
375   [assumption|reflexivity
376   |destruct H5|*:destruct H6]
377 qed.
378
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
384   |elim H;intros
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
388     |*:destruct H5]]
389 qed.
390
391 lemma fv_append : ∀G,H.fv_env (G @ H) = (fv_env G @ fv_env H).
392 intro;elim G;simplify;autobatch paramodulation;
393 qed.