]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/basics/types.ma
a5e35c9d218a5d756319dd429dd7e1b6d4e8363e
[helm.git] / matita / matita / lib / basics / types.ma
1 (*
2     ||M||  This file is part of HELM, an Hypertextual, Electronic        
3     ||A||  Library of Mathematics, developed at the Computer Science     
4     ||T||  Department of the University of Bologna, Italy.                     
5     ||I||                                                                 
6     ||T||  
7     ||A||  
8     \   /  This file is distributed under the terms of the       
9      \ /   GNU General Public License Version 2   
10       V_______________________________________________________________ *)
11
12 include "basics/logic.ma".
13
14 (* void *)
15 inductive void : Type[0] ≝.
16
17 (* unit *)
18 inductive unit : Type[0] ≝ it: unit.
19
20 (* sum *)
21 inductive Sum (A,B:Type[0]) : Type[0] ≝
22   inl : A → Sum A B
23 | inr : B → Sum A B.
24
25 interpretation "Disjoint union" 'plus A B = (Sum A B).
26
27 (* option *)
28 inductive option (A:Type[0]) : Type[0] ≝
29    None : option A
30  | Some : A → option A.
31
32 definition option_map : ∀A,B:Type[0]. (A → B) → option A → option B ≝
33 λA,B,f,o. match o with [ None ⇒ None B | Some a ⇒ Some B (f a) ].
34
35 lemma option_map_none : ∀A,B,f,x.
36   option_map A B f x = None B → x = None A.
37 #A #B #f * [ // | #a #E whd in E:(??%?); destruct ]
38 qed.
39
40 lemma option_map_some : ∀A,B,f,x,v.
41   option_map A B f x = Some B v → ∃y. x = Some ? y ∧ f y = v.
42 #A #B #f *
43 [ #v normalize #E destruct
44 | #y #v normalize #E %{y} destruct % //
45 ] qed.
46
47 definition option_map_def : ∀A,B:Type[0]. (A → B) → B → option A → B ≝
48 λA,B,f,d,o. match o with [ None ⇒ d | Some a ⇒ f a ].
49
50 lemma refute_none_by_refl : ∀A,B:Type[0]. ∀P:A → B. ∀Q:B → Type[0]. ∀x:option A. ∀H:x = None ? → False.
51   (∀v. x = Some ? v → Q (P v)) →
52   Q (match x return λy.x = y → ? with [ Some v ⇒ λ_. P v | None ⇒ λE. match H E in False with [ ] ] (refl ? x)).
53 #A #B #P #Q *
54 [ #H cases (H (refl ??))
55 | #a #H #p normalize @p @refl
56 ] qed.
57
58 (* dependent pair *)
59 record DPair (A:Type[0]) (f:A→Type[0]) : Type[0] ≝ {
60     dpi1:> A
61   ; dpi2: f dpi1
62   }.
63
64 interpretation "DPair" 'dpair x = (DPair ? x).
65
66 interpretation "mk_DPair" 'mk_DPair x y = (mk_DPair ?? x y).
67
68 (* sigma *)
69 record Sig (A:Type[0]) (f:A→Prop) : Type[0] ≝ {
70     pi1: A  (* not a coercion due to problems with Cerco *)
71   ; pi2: f pi1
72   }.
73   
74 interpretation "Sigma" 'sigma x = (Sig ? x).
75
76 interpretation "mk_Sig" 'dp x y = (mk_Sig ?? x y).
77
78 lemma sub_pi2 : ∀A.∀P,P':A → Prop. (∀x.P x → P' x) → ∀x:Σx:A.P x. P' (pi1 … x).
79 #A #P #P' #H1 * #x #H2 @H1 @H2
80 qed.
81
82 lemma inj_mk_Sig: ∀A,P.∀x. x = mk_Sig A P (pi1 A P x) (pi2 A P x).
83 #A #P #x cases x //
84 qed-. 
85 (* Prod *)
86
87 record Prod (A,B:Type[0]) : Type[0] ≝ {
88    fst: A
89  ; snd: B
90  }.
91
92 interpretation "Pair construction" 'pair x y = (mk_Prod ? ? x y).
93
94 interpretation "Product" 'product x y = (Prod x y).
95
96 interpretation "pair pi1" 'pi1 = (fst ? ?).
97 interpretation "pair pi2" 'pi2 = (snd ? ?).
98 interpretation "pair pi1" 'pi1a x = (fst ? ? x).
99 interpretation "pair pi2" 'pi2a x = (snd ? ? x).
100 interpretation "pair pi1" 'pi1b x y = (fst ? ? x y).
101 interpretation "pair pi2" 'pi2b x y = (snd ? ? x y).
102
103 notation "π1" with precedence 10 for @{ (proj1 ??) }.
104 notation "π2" with precedence 10 for @{ (proj2 ??) }.
105
106 (* Yeah, I probably ought to do something more general... *)
107 notation "hvbox(\langle term 19 a, break term 19 b, break term 19 c\rangle)"
108 with precedence 90 for @{ 'triple $a $b $c}.
109 interpretation "Triple construction" 'triple x y z = (mk_Prod ? ? (mk_Prod ? ? x y) z).
110
111 notation "hvbox(\langle term 19 a, break term 19 b, break term 19 c, break term 19 d\rangle)"
112 with precedence 90 for @{ 'quadruple $a $b $c $d}.
113 interpretation "Quadruple construction" 'quadruple w x y z = (mk_Prod ? ? (mk_Prod ? ? w x) (mk_Prod ? ? y z)).
114
115
116 theorem eq_pair_fst_snd: ∀A,B.∀p:A × B.
117   p = 〈 \fst p, \snd p 〉.
118 #A #B #p (cases p) // qed.
119
120 lemma fst_eq : ∀A,B.∀a:A.∀b:B. \fst 〈a,b〉 = a.
121 // qed.
122
123 lemma snd_eq : ∀A,B.∀a:A.∀b:B. \snd 〈a,b〉 = b.
124 // qed.
125
126 notation > "hvbox('let' 〈ident x,ident y〉 ≝ t 'in' s)"
127  with precedence 10
128 for @{ match $t with [ mk_Prod ${ident x} ${ident y} ⇒ $s ] }.
129
130 notation < "hvbox('let' \nbsp hvbox(〈ident x,ident y〉 \nbsp≝ break t \nbsp 'in' \nbsp) break s)"
131  with precedence 10
132 for @{ match $t with [ mk_Prod (${ident x}:$X) (${ident y}:$Y) ⇒ $s ] }.
133
134 (* Also extracts an equality proof (useful when not using Russell). *)
135 notation > "hvbox('let' 〈ident x,ident y〉 'as' ident E ≝ t 'in' s)"
136  with precedence 10
137 for @{ match $t return λx.x = $t → ? with [ mk_Prod ${ident x} ${ident y} ⇒
138         λ${ident E}.$s ] (refl ? $t) }.
139
140 notation < "hvbox('let' \nbsp hvbox(〈ident x,ident y〉 \nbsp 'as'\nbsp ident E\nbsp ≝ break t \nbsp 'in' \nbsp) break s)"
141  with precedence 10
142 for @{ match $t return λ${ident k}:$X.$eq $T $k $t → ? with [ mk_Prod (${ident x}:$U) (${ident y}:$W) ⇒
143         λ${ident E}:$e.$s ] ($refl $T $t) }.
144
145 notation > "hvbox('let' 〈ident x,ident y,ident z〉 'as' ident E ≝ t 'in' s)"
146  with precedence 10
147 for @{ match $t return λx.x = $t → ? with [ mk_Prod ${fresh xy} ${ident z} ⇒
148        match ${fresh xy} return λx. ? = $t → ? with [ mk_Prod ${ident x} ${ident y} ⇒
149         λ${ident E}.$s ] ] (refl ? $t) }.
150
151 notation < "hvbox('let' \nbsp hvbox(〈ident x,ident y,ident z〉 \nbsp 'as' \nbsp ident E\nbsp ≝ break t \nbsp 'in' \nbsp) break s)"
152  with precedence 10
153 for @{ match $t return λ${ident k}:$X.$eq $T $k $t → $U with [ mk_Prod (${ident xy}:$V) (${ident z}:$Z) ⇒
154        match $xy return λ${ident a}. $eq $R $r $t → ? with [ mk_Prod (${ident x}:$L) (${ident y}:$I) ⇒
155         λ${ident E}:$J.$s ] ] ($refl $A $t) }.
156
157 notation > "hvbox('let' 〈ident w,ident x,ident y,ident z〉 ≝ t 'in' s)"
158  with precedence 10
159 for @{ match $t with [ mk_Prod ${fresh wx} ${fresh yz} ⇒ match ${fresh wx} with [ mk_Prod ${ident w} ${ident x} ⇒ match ${fresh yz} with [ mk_Prod ${ident y} ${ident z} ⇒ $s ] ] ] }.
160
161 notation > "hvbox('let' 〈ident x,ident y,ident z〉 ≝ t 'in' s)"
162  with precedence 10
163 for @{ match $t with [ mk_Prod ${fresh xy} ${ident z} ⇒ match ${fresh xy} with [ mk_Prod ${ident x} ${ident y} ⇒ $s ] ] }.
164
165 (* This appears to upset automation (previously provable results require greater
166    depth or just don't work), so use example rather than lemma to prevent it
167    being indexed. *)
168 example contract_pair : ∀A,B.∀e:A×B. (let 〈a,b〉 ≝ e in 〈a,b〉) = e.
169 #A #B * // qed.
170
171 lemma extract_pair : ∀A,B,C,D.  ∀u:A×B. ∀Q:A → B → C×D. ∀x,y.
172 ((let 〈a,b〉 ≝ u in Q a b) = 〈x,y〉) →
173 ∃a,b. 〈a,b〉 = u ∧ Q a b = 〈x,y〉.
174 #A #B #C #D * #a #b #Q #x #y normalize #E1 %{a} %{b} % try @refl @E1 qed.
175
176 lemma breakup_pair : ∀A,B,C:Type[0].∀x. ∀R:C → Prop. ∀P:A → B → C.
177   R (P (\fst x) (\snd x)) → R (let 〈a,b〉 ≝ x in P a b).
178 #A #B #C *; normalize /2/
179 qed.
180
181 lemma pair_elim:
182   ∀A,B,C: Type[0].
183   ∀T: A → B → C.
184   ∀p.
185   ∀P: A×B → C → Prop.
186     (∀lft, rgt. p = 〈lft,rgt〉 → P 〈lft,rgt〉 (T lft rgt)) →
187       P p (let 〈lft, rgt〉 ≝ p in T lft rgt).
188  #A #B #C #T * /2/
189 qed.
190
191 lemma pair_elim2:
192   ∀A,B,C,C': Type[0].
193   ∀T: A → B → C.
194   ∀T': A → B → C'.
195   ∀p.
196   ∀P: A×B → C → C' → Prop.
197     (∀lft, rgt. p = 〈lft,rgt〉 → P 〈lft,rgt〉 (T lft rgt) (T' lft rgt)) →
198       P p (let 〈lft, rgt〉 ≝ p in T lft rgt) (let 〈lft, rgt〉 ≝ p in T' lft rgt).
199  #A #B #C #C' #T #T' * /2/
200 qed.
201
202 (* Useful for avoiding destruct's full normalization. *)
203 lemma pair_eq1: ∀A,B. ∀a1,a2:A. ∀b1,b2:B. 〈a1,b1〉 = 〈a2,b2〉 → a1 = a2.
204 #A #B #a1 #a2 #b1 #b2 #H destruct //
205 qed.
206
207 lemma pair_eq2: ∀A,B. ∀a1,a2:A. ∀b1,b2:B. 〈a1,b1〉 = 〈a2,b2〉 → b1 = b2.
208 #A #B #a1 #a2 #b1 #b2 #H destruct //
209 qed.
210
211 lemma pair_destruct_1:
212  ∀A,B.∀a:A.∀b:B.∀c. 〈a,b〉 = c → a = \fst c.
213  #A #B #a #b *; /2/
214 qed.
215
216 lemma pair_destruct_2:
217  ∀A,B.∀a:A.∀b:B.∀c. 〈a,b〉 = c → b = \snd c.
218  #A #B #a #b *; /2/
219 qed.
220
221 lemma coerc_pair_sigma:
222  ∀A,B,P. ∀p:A × B. P (\snd p) → A × (Σx:B.P x).
223 #A #B #P * #a #b #p % [@a | /2/]
224 qed.
225 coercion coerc_pair_sigma:∀A,B,P. ∀p:A × B. P (\snd p) → A × (Σx:B.P x)
226 ≝ coerc_pair_sigma on p: (? × ?) to (? × (Sig ??)).