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