Debian Codegraph @

src/Data/Product/Base.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Products
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Data.Product.Base where
10
11 open import Function.Base
12 open import Level using (Level; _⊔_)
13
14 private
15 variable
16 a b c d e f ℓ p q r : Level
17 A : Set a
18 B : Set b
19 C : Set c
20 D : Set d
21 E : Set e
22 F : Set f
23
24 ------------------------------------------------------------------------
25 -- Definition of dependent products
26
27 open import Agda.Builtin.Sigma public
28 renaming (fst to proj₁; snd to proj₂)
29 hiding (module Σ)
30
31 module Σ = Agda.Builtin.Sigma.Σ
32 renaming (fst to proj₁; snd to proj₂)
33
34 ------------------------------------------------------------------------
35 -- Existential quantifiers
36
37 ∃ : ∀ {A : Set a} → (A → Set b) → Set (a ⊔ b)
38 ∃ = Σ _
39
40 ∃₂ : ∀ {A : Set a} {B : A → Set b}
41 (C : (x : A) → B x → Set c) → Set (a ⊔ b ⊔ c)
42 ∃₂ C = ∃ λ a → ∃ λ b → C a b
43
44 ------------------------------------------------------------------------
45 -- Syntaxes
46
47 -- The syntax declaration below is attached to Σ-syntax, to make it
48 -- easy to import Σ without the special syntax.
49
50 infix 2 Σ-syntax
51
52 Σ-syntax : (A : Set a) → (A → Set b) → Set (a ⊔ b)
53 Σ-syntax = Σ
54
55 syntax Σ-syntax A (λ x → B) = Σ[ x ∈ A ] B
56
57 infix 2 ∃-syntax
58
59 ∃-syntax : ∀ {A : Set a} → (A → Set b) → Set (a ⊔ b)
60 ∃-syntax = ∃
61
62 syntax ∃-syntax (λ x → B) = ∃[ x ] B
63
64 ------------------------------------------------------------------------
65 -- Definition of non-dependent products
66
67 infixr 4 _,′_
68 infixr 2 _×_
69
70 _×_ : ∀ (A : Set a) (B : Set b) → Set (a ⊔ b)
71 A × B = Σ[ x ∈ A ] B
72
73 _,′_ : A → B → A × B
74 _,′_ = _,_
75
76 ------------------------------------------------------------------------
77 -- Operations over dependent products
78
79 infix 4 -,_
80 infixr 2 _-×-_ _-,-_
81 infixl 2 _<*>_
82
83 -- Sometimes the first component can be inferred.
84
85 -,_ : ∀ {A : Set a} {B : A → Set b} {x} → B x → Σ _ B
86 -, y = _ , y
87
88 <_,_> : ∀ {A : Set a} {B : A → Set b} {C : ∀ {x} → B x → Set c}
89 (f : (x : A) → B x) → ((x : A) → C (f x)) →
90 ((x : A) → Σ (B x) C)
91 < f , g > x = (f x , g x)
92
93 map : ∀ {P : A → Set p} {Q : B → Set q} →
94 (f : A → B) → (∀ {x} → P x → Q (f x)) →
95 Σ A P → Σ B Q
96 map f g (x , y) = (f x , g y)
97
98 map₁ : (A → B) → A × C → B × C
99 map₁ f = map f id
100
101 map₂ : ∀ {A : Set a} {B : A → Set b} {C : A → Set c} →
102 (∀ {x} → B x → C x) → Σ A B → Σ A C
103 map₂ f = map id f
104
105 -- A version of map where the output can depend on the input
106 dmap : ∀ {B : A → Set b} {P : A → Set p} {Q : ∀ {a} → P a → B a → Set q} →
107 (f : (a : A) → B a) → (∀ {a} (b : P a) → Q b (f a)) →
108 ((a , b) : Σ A P) → Σ (B a) (Q b)
109 dmap f g (x , y) = f x , g y
110
111 zip : ∀ {P : A → Set p} {Q : B → Set q} {R : C → Set r} →
112 (_∙_ : A → B → C) →
113 (∀ {x y} → P x → Q y → R (x ∙ y)) →
114 Σ A P → Σ B Q → Σ C R
115 zip _∙_ _∘_ (a , p) (b , q) = ((a ∙ b) , (p ∘ q))
116
117 curry : ∀ {A : Set a} {B : A → Set b} {C : Σ A B → Set c} →
118 ((p : Σ A B) → C p) →
119 ((x : A) → (y : B x) → C (x , y))
120 curry f x y = f (x , y)
121
122 uncurry : ∀ {A : Set a} {B : A → Set b} {C : Σ A B → Set c} →
123 ((x : A) → (y : B x) → C (x , y)) →
124 ((p : Σ A B) → C p)
125 uncurry f (x , y) = f x y
126
127 -- Rewriting dependent products
128 assocʳ : {B : A → Set b} {C : (a : A) → B a → Set c} →
129 Σ (Σ A B) (uncurry C) → Σ A (λ a → Σ (B a) (C a))
130 assocʳ ((a , b) , c) = (a , (b , c))
131
132 assocˡ : {B : A → Set b} {C : (a : A) → B a → Set c} →
133 Σ A (λ a → Σ (B a) (C a)) → Σ (Σ A B) (uncurry C)
134 assocˡ (a , (b , c)) = ((a , b) , c)
135
136 -- Alternate form of associativity for dependent products
137 -- where the C parameter is uncurried.
138 assocʳ-curried : {B : A → Set b} {C : Σ A B → Set c} →
139 Σ (Σ A B) C → Σ A (λ a → Σ (B a) (curry C a))
140 assocʳ-curried ((a , b) , c) = (a , (b , c))
141
142 assocˡ-curried : {B : A → Set b} {C : Σ A B → Set c} →
143 Σ A (λ a → Σ (B a) (curry C a)) → Σ (Σ A B) C
144 assocˡ-curried (a , (b , c)) = ((a , b) , c)
145
146 ------------------------------------------------------------------------
147 -- Operations for non-dependent products
148
149 -- Any of the above operations for dependent products will also work for
150 -- non-dependent products but sometimes Agda has difficulty inferring
151 -- the non-dependency. Primed (′ = \prime) versions of the operations
152 -- are therefore provided below that sometimes have better inference
153 -- properties.
154
155 zip′ : (A → B → C) → (D → E → F) → A × D → B × E → C × F
156 zip′ f g = zip f g
157
158 curry′ : (A × B → C) → (A → B → C)
159 curry′ = curry
160
161 uncurry′ : (A → B → C) → (A × B → C)
162 uncurry′ = uncurry
163
164 map₂′ : (B → C) → A × B → A × C
165 map₂′ f = map₂ f
166
167 dmap′ : ∀ {x y} {X : A → Set x} {Y : B → Set y} →
168 ((a : A) → X a) → ((b : B) → Y b) →
169 ((a , b) : A × B) → X a × Y b
170 dmap′ f g = dmap f g
171
172 _<*>_ : ∀ {x y} {X : A → Set x} {Y : B → Set y} →
173 ((a : A) → X a) × ((b : B) → Y b) →
174 ((a , b) : A × B) → X a × Y b
175 _<*>_ = uncurry dmap′
176
177 -- Operations that can only be defined for non-dependent products
178
179 swap : A × B → B × A
180 swap (x , y) = (y , x)
181
182 _-×-_ : (A → B → Set p) → (A → B → Set q) → (A → B → Set _)
183 f -×- g = f -⟪ _×_ ⟫- g
184
185 _-,-_ : (A → B → C) → (A → B → D) → (A → B → C × D)
186 f -,- g = f -⟪ _,_ ⟫- g
187
188 -- Rewriting non-dependent products
189 assocʳ′ : (A × B) × C → A × (B × C)
190 assocʳ′ ((a , b) , c) = (a , (b , c))
191
192 assocˡ′ : A × (B × C) → (A × B) × C
193 assocˡ′ (a , (b , c)) = ((a , b) , c)