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.
8 \ / This file is distributed under the terms of the
9 \ / GNU General Public License Version 2
10 V_______________________________________________________________ *)
12 include "basics/logic.ma".
15 inductive void : Type[0] ≝.
18 inductive unit : Type[0] ≝ it: unit.
21 inductive Sum (A,B:Type[0]) : Type[0] ≝
25 interpretation "Disjoint union" 'plus A B = (Sum A B).
28 inductive option (A:Type[0]) : Type[0] ≝
30 | Some : A → option A.
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) ].
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 ]
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.
43 [ #v normalize #E destruct
44 | #y #v normalize #E %{y} destruct % //
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 ].
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)).
54 [ #H cases (H (refl ??))
55 | #a #H #p normalize @p @refl
59 record DPair (A:Type[0]) (f:A→Type[0]) : Type[0] ≝ {
64 interpretation "DPair" 'sigma x = (DPair ? x).
66 notation "hvbox(« term 19 a, break term 19 b»)"
67 with precedence 90 for @{ 'dp $a $b }.
69 interpretation "mk_DPair" 'dp x y = (mk_DPair ?? x y).
72 record Sig (A:Type[0]) (f:A→Prop) : Type[0] ≝ {
77 interpretation "Sigma" 'sigma x = (Sig ? x).
79 interpretation "mk_Sig" 'dp x y = (mk_Sig ?? x y).
83 record Prod (A,B:Type[0]) : Type[0] ≝ {
88 interpretation "Pair construction" 'pair x y = (mk_Prod ? ? x y).
90 interpretation "Product" 'product x y = (Prod x y).
92 interpretation "pair pi1" 'pi1 = (fst ? ?).
93 interpretation "pair pi2" 'pi2 = (snd ? ?).
94 interpretation "pair pi1" 'pi1a x = (fst ? ? x).
95 interpretation "pair pi2" 'pi2a x = (snd ? ? x).
96 interpretation "pair pi1" 'pi1b x y = (fst ? ? x y).
97 interpretation "pair pi2" 'pi2b x y = (snd ? ? x y).
99 notation "π1" with precedence 10 for @{ (proj1 ??) }.
100 notation "π2" with precedence 10 for @{ (proj2 ??) }.
102 (* Yeah, I probably ought to do something more general... *)
103 notation "hvbox(\langle term 19 a, break term 19 b, break term 19 c\rangle)"
104 with precedence 90 for @{ 'triple $a $b $c}.
105 interpretation "Triple construction" 'triple x y z = (mk_Prod ? ? (mk_Prod ? ? x y) z).
107 notation "hvbox(\langle term 19 a, break term 19 b, break term 19 c, break term 19 d\rangle)"
108 with precedence 90 for @{ 'quadruple $a $b $c $d}.
109 interpretation "Quadruple construction" 'quadruple w x y z = (mk_Prod ? ? (mk_Prod ? ? w x) (mk_Prod ? ? y z)).
112 theorem eq_pair_fst_snd: ∀A,B.∀p:A × B.
113 p = 〈 \fst p, \snd p 〉.
114 #A #B #p (cases p) // qed.
116 lemma fst_eq : ∀A,B.∀a:A.∀b:B. \fst 〈a,b〉 = a.
119 lemma snd_eq : ∀A,B.∀a:A.∀b:B. \snd 〈a,b〉 = b.
122 notation > "hvbox('let' 〈ident x,ident y〉 ≝ t 'in' s)"
124 for @{ match $t with [ mk_Prod ${ident x} ${ident y} ⇒ $s ] }.
126 notation < "hvbox('let' \nbsp hvbox(〈ident x,ident y〉 \nbsp≝ break t \nbsp 'in' \nbsp) break s)"
128 for @{ match $t with [ mk_Prod (${ident x}:$X) (${ident y}:$Y) ⇒ $s ] }.
130 (* Also extracts an equality proof (useful when not using Russell). *)
131 notation > "hvbox('let' 〈ident x,ident y〉 'as' ident E ≝ t 'in' s)"
133 for @{ match $t return λx.x = $t → ? with [ mk_Prod ${ident x} ${ident y} ⇒
134 λ${ident E}.$s ] (refl ? $t) }.
136 notation < "hvbox('let' \nbsp hvbox(〈ident x,ident y〉 \nbsp 'as'\nbsp ident E\nbsp ≝ break t \nbsp 'in' \nbsp) break s)"
138 for @{ match $t return λ${ident k}:$X.$eq $T $k $t → ? with [ mk_Prod (${ident x}:$U) (${ident y}:$W) ⇒
139 λ${ident E}:$e.$s ] ($refl $T $t) }.
141 notation > "hvbox('let' 〈ident x,ident y,ident z〉 'as' ident E ≝ t 'in' s)"
143 for @{ match $t return λx.x = $t → ? with [ mk_Prod ${fresh xy} ${ident z} ⇒
144 match ${fresh xy} return λx. ? = $t → ? with [ mk_Prod ${ident x} ${ident y} ⇒
145 λ${ident E}.$s ] ] (refl ? $t) }.
147 notation < "hvbox('let' \nbsp hvbox(〈ident x,ident y,ident z〉 \nbsp'as'\nbsp ident E\nbsp ≝ break t \nbsp 'in' \nbsp) break s)"
149 for @{ match $t return λ${ident x}.$eq $T $x $t → $U with [ mk_Prod (${fresh xy}:$V) (${ident z}:$Z) ⇒
150 match ${fresh xy} return λ${ident y}. $eq $R $r $t → ? with [ mk_Prod (${ident x}:$L) (${ident y}:$I) ⇒
151 λ${ident E}:$J.$s ] ] ($refl $A $t) }.
153 notation > "hvbox('let' 〈ident w,ident x,ident y,ident z〉 ≝ t 'in' s)"
155 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 ] ] ] }.
157 notation > "hvbox('let' 〈ident x,ident y,ident z〉 ≝ t 'in' s)"
159 for @{ match $t with [ mk_Prod ${fresh xy} ${ident z} ⇒ match ${fresh xy} with [ mk_Prod ${ident x} ${ident y} ⇒ $s ] ] }.
161 (* This appears to upset automation (previously provable results require greater
162 depth or just don't work), so use example rather than lemma to prevent it
164 example contract_pair : ∀A,B.∀e:A×B. (let 〈a,b〉 ≝ e in 〈a,b〉) = e.
167 lemma extract_pair : ∀A,B,C,D. ∀u:A×B. ∀Q:A → B → C×D. ∀x,y.
168 ((let 〈a,b〉 ≝ u in Q a b) = 〈x,y〉) →
169 ∃a,b. 〈a,b〉 = u ∧ Q a b = 〈x,y〉.
170 #A #B #C #D * #a #b #Q #x #y normalize #E1 %{a} %{b} % try @refl @E1 qed.
172 lemma breakup_pair : ∀A,B,C:Type[0].∀x. ∀R:C → Prop. ∀P:A → B → C.
173 R (P (\fst x) (\snd x)) → R (let 〈a,b〉 ≝ x in P a b).
174 #A #B #C *; normalize /2/
182 (∀lft, rgt. p = 〈lft,rgt〉 → P 〈lft,rgt〉 (T lft rgt)) →
183 P p (let 〈lft, rgt〉 ≝ p in T lft rgt).
192 ∀P: A×B → C → C' → Prop.
193 (∀lft, rgt. p = 〈lft,rgt〉 → P 〈lft,rgt〉 (T lft rgt) (T' lft rgt)) →
194 P p (let 〈lft, rgt〉 ≝ p in T lft rgt) (let 〈lft, rgt〉 ≝ p in T' lft rgt).
195 #A #B #C #C' #T #T' * /2/
198 (* Useful for avoiding destruct's full normalization. *)
199 lemma pair_eq1: ∀A,B. ∀a1,a2:A. ∀b1,b2:B. 〈a1,b1〉 = 〈a2,b2〉 → a1 = a2.
200 #A #B #a1 #a2 #b1 #b2 #H destruct //
203 lemma pair_eq2: ∀A,B. ∀a1,a2:A. ∀b1,b2:B. 〈a1,b1〉 = 〈a2,b2〉 → b1 = b2.
204 #A #B #a1 #a2 #b1 #b2 #H destruct //
207 lemma pair_destruct_1:
208 ∀A,B.∀a:A.∀b:B.∀c. 〈a,b〉 = c → a = \fst c.
212 lemma pair_destruct_2:
213 ∀A,B.∀a:A.∀b:B.∀c. 〈a,b〉 = c → b = \snd c.