Skip to content

Commit c1478b1

Browse files
Port old TypeIsomorphisms over to the new Function hierarchy. (#1203)
1 parent f8f19ec commit c1478b1

File tree

10 files changed

+536
-13
lines changed

10 files changed

+536
-13
lines changed

CHANGELOG.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,18 @@ New modules
170170
Data.Vec.Functional.Properties
171171
```
172172

173+
* Modules replacing `Function.Related.TypeIsomorphisms` using the new
174+
`Inverse` definitions.
175+
```
176+
Data.Sum.Algebra
177+
Data.Product.Algebra
178+
```
179+
180+
* Basic properties of the function type `A → B`:
181+
```agda
182+
Function.Properties
183+
```
184+
173185
* Symmetry for various functional properties
174186
```agda
175187
Function.Construct.Symmetry
@@ -205,6 +217,7 @@ New modules
205217
* Indexed nullary relations/sets:
206218
```
207219
Relation.Nullary.Indexed
220+
Relation.Nullary.Indexed.Negation
208221
```
209222

210223
* Symmetric transitive closures of binary relations:
@@ -433,8 +446,15 @@ Other minor additions
433446
recompute : .(Coprime n d) → Coprime n d
434447
```
435448

436-
* Added new functions to `Data.Product`:
449+
* Add new functions to `Data.Product`:
437450
```agda
451+
assocʳ-curried : Σ (Σ A B) C → Σ A (λ a → Σ (B a) (curry C a))
452+
assocˡ-curried : Σ A (λ a → Σ (B a) (curry C a)) → Σ (Σ A B) C
453+
assocʳ : Σ (Σ A B) (uncurry C) → Σ A (λ a → Σ (B a) (C a))
454+
assocˡ : Σ A (λ a → Σ (B a) (C a)) → Σ (Σ A B) (uncurry C)
455+
assocʳ′ : (A × B) × C → A × (B × C)
456+
assocˡ′ : A × (B × C) → (A × B) × C
457+
438458
dmap : (f : (a : A) → B a) → (∀ {a} (p : P a) → Q p (f a)) →
439459
(ap : Σ A P) → Σ (B (proj₁ ap)) (Q (proj₂ ap))
440460
dmap : ((a : A) → X a) → ((b : B) → Y b) →
@@ -443,6 +463,19 @@ Other minor additions
443463
((a , b) : A × B) → X a × Y b
444464
```
445465

466+
* Added new proofs to `Data.Product.Properties`:
467+
```agda
468+
Σ-≡,≡↔≡ : {p₁@(a₁ , b₁) p₂@(a₂ , b₂) : Σ A B} → (∃ λ (p : a₁ ≡ a₂) → subst B p b₁ ≡ b₂) ↔ (p₁ ≡ p₂)
469+
×-≡,≡↔≡ : {p₁@(a₁ , b₁) p₂@(a₂ , b₂) : A × B} → (a₁ ≡ a₂ × b₁ ≡ b₂) ↔ p₁ ≡ p₂
470+
∃∃↔∃∃ : (R : A → B → Set ℓ) → (∃₂ λ x y → R x y) ↔ (∃₂ λ y x → R x y)
471+
```
472+
473+
* Add new functions to `Data.Sum.Base`:
474+
```agda
475+
assocʳ : (A ⊎ B) ⊎ C → A ⊎ B ⊎ C
476+
assocˡ : A ⊎ B ⊎ C → (A ⊎ B) ⊎ C
477+
```
478+
446479
* Made first argument of `[,]-∘-distr` in `Data.Sum.Properties` explicit
447480

448481
* Added new proofs to `Data.Sum.Properties`:
@@ -583,6 +616,11 @@ Other minor additions
583616
_on₂_ : (C → C → D) → (A → B → C) → (A → B → D)
584617
```
585618

619+
* Added new function in `Function.Bundles`:
620+
```agda
621+
mk↔′ : ∀ (f : A → B) (f⁻¹ : B → A) → Inverseˡ f f⁻¹ → Inverseʳ f f⁻¹ → A ↔ B
622+
```
623+
586624
* Added new operator to `Relation.Binary`:
587625
```agda
588626
_⇔_ : REL A B ℓ₁ → REL A B ℓ₂ → Set _
@@ -604,6 +642,11 @@ Other minor additions
604642
icong′ : {f : A → B} x → f x ≡ f x
605643
```
606644

645+
* Added new proof to `Relation.Nullary.Decidable`:
646+
```agda
647+
True-↔ : (dec : Dec P) → Irrelevant P → True dec ↔ P
648+
```
649+
607650
* Added new proofs to `Relation.Binary.Construct.NonStrictToStrict`:
608651
```agda
609652
<-isDecStrictPartialOrder : IsDecPartialOrder _≈_ _≤_ → IsDecStrictPartialOrder _≈_ _<_

src/Data/Product.agda

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,25 @@ uncurry : ∀ {A : Set a} {B : A → Set b} {C : Σ A B → Set c} →
136136
((p : Σ A B) C p)
137137
uncurry f (x , y) = f x y
138138

139+
-- Rewriting dependent products
140+
assocʳ : {B : A Set b} {C : (a : A) B a Set c}
141+
Σ (Σ A B) (uncurry C) Σ A (λ a Σ (B a) (C a))
142+
assocʳ ((a , b) , c) = (a , (b , c))
143+
144+
assocˡ : {B : A Set b} {C : (a : A) B a Set c}
145+
Σ A (λ a Σ (B a) (C a)) Σ (Σ A B) (uncurry C)
146+
assocˡ (a , (b , c)) = ((a , b) , c)
147+
148+
-- Alternate form of associativity for dependent products
149+
-- where the C parameter is uncurried.
150+
assocʳ-curried : {B : A Set b} {C : Σ A B Set c}
151+
Σ (Σ A B) C Σ A (λ a Σ (B a) (curry C a))
152+
assocʳ-curried ((a , b) , c) = (a , (b , c))
153+
154+
assocˡ-curried : {B : A Set b} {C : Σ A B Set c}
155+
Σ A (λ a Σ (B a) (curry C a)) Σ (Σ A B) C
156+
assocˡ-curried (a , (b , c)) = ((a , b) , c)
157+
139158
------------------------------------------------------------------------
140159
-- Operations for non-dependent products
141160

@@ -174,3 +193,10 @@ f -×- g = f -⟪ _×_ ⟫- g
174193

175194
_-,-_ : (A B C) (A B D) (A B C × D)
176195
f -,- g = f -⟪ _,_ ⟫- g
196+
197+
-- Rewriting non-dependent products
198+
assocʳ′ : (A × B) × C A × (B × C)
199+
assocʳ′ ((a , b) , c) = (a , (b , c))
200+
201+
assocˡ′ : A × (B × C) (A × B) × C
202+
assocˡ′ (a , (b , c)) = ((a , b) , c)

src/Data/Product/Algebra.agda

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
------------------------------------------------------------------------
2+
-- The Agda standard library
3+
--
4+
-- Algebraic properties of products
5+
------------------------------------------------------------------------
6+
7+
{-# OPTIONS --without-K --safe #-}
8+
9+
module Data.Product.Algebra where
10+
11+
open import Algebra
12+
open import Data.Bool.Base using (true; false)
13+
open import Data.Empty.Polymorphic using (⊥; ⊥-elim)
14+
open import Data.Product
15+
open import Data.Product.Properties
16+
open import Data.Sum as Sum using (_⊎_; inj₁; inj₂; [_,_]′)
17+
open import Data.Sum.Algebra
18+
open import Data.Unit.Polymorphic using (⊤; tt)
19+
open import Function.Base using (_∘′_)
20+
open import Function.Bundles using (_↔_; Inverse; mk↔′)
21+
open import Function.Properties.Inverse using (↔-isEquivalence)
22+
open import Level using (Level; suc)
23+
open import Relation.Binary.PropositionalEquality.Core
24+
25+
import Function.Definitions as FuncDef
26+
27+
------------------------------------------------------------------------
28+
29+
private
30+
variable
31+
a b c d p : Level
32+
A : Set a
33+
B : Set b
34+
C : Set c
35+
D : Set d
36+
37+
module _ {A : Set a} {B : Set b} where
38+
open FuncDef {A = A} {B} _≡_ _≡_
39+
40+
------------------------------------------------------------------------
41+
-- Properties of Σ
42+
43+
-- Σ is associative
44+
Σ-assoc : {B : A Set b} {C : (a : A) B a Set c}
45+
Σ (Σ A B) (uncurry C) ↔ Σ A (λ a Σ (B a) (C a))
46+
Σ-assoc = mk↔′ assocʳ assocˡ cong′ cong′
47+
48+
-- Σ is associative, alternate formulation
49+
Σ-assoc-alt : {B : A Set b} {C : Σ A B Set c}
50+
Σ (Σ A B) C ↔ Σ A (λ a Σ (B a) (curry C a))
51+
Σ-assoc-alt = mk↔′ assocʳ-curried assocˡ-curried cong′ cong′
52+
53+
------------------------------------------------------------------------
54+
-- Algebraic properties
55+
56+
-- × is a congruence
57+
×-cong : A ↔ B C ↔ D (A × C) ↔ (B × D)
58+
×-cong i j = mk↔′ (map I.f J.f) (map I.f⁻¹ J.f⁻¹)
59+
(λ {(a , b) cong₂ _,_ (I.inverseˡ a) (J.inverseˡ b)})
60+
(λ {(a , b) cong₂ _,_ (I.inverseʳ a) (J.inverseʳ b)})
61+
where module I = Inverse i; module J = Inverse j
62+
63+
-- × is commutative.
64+
-- (we don't use Commutative because it isn't polymorphic enough)
65+
×-comm : (A : Set a) (B : Set b) (A × B) ↔ (B × A)
66+
×-comm _ _ = mk↔′ swap swap swap-involutive swap-involutive
67+
68+
module _ (ℓ : Level) where
69+
70+
-- × is associative
71+
×-assoc : Associative {ℓ = ℓ} _↔_ _×_
72+
×-assoc _ _ _ = mk↔′ assocʳ′ assocˡ′ cong′ cong′
73+
74+
-- ⊤ is the identity for ×
75+
×-identityˡ : LeftIdentity {ℓ = ℓ} _↔_ ⊤ _×_
76+
×-identityˡ _ = mk↔′ proj₂ (tt ,_) cong′ cong′
77+
78+
×-identityʳ : RightIdentity {ℓ = ℓ} _↔_ ⊤ _×_
79+
×-identityʳ _ = mk↔′ proj₁ (_, tt) cong′ cong′
80+
81+
×-identity : Identity _↔_ ⊤ _×_
82+
×-identity = ×-identityˡ , ×-identityʳ
83+
84+
-- ⊥ is the zero for ×
85+
×-zeroˡ : LeftZero {ℓ = ℓ} _↔_ ⊥ _×_
86+
×-zeroˡ A = mk↔′ proj₁ ⊥-elim ⊥-elim λ ()
87+
88+
×-zeroʳ : RightZero {ℓ = ℓ} _↔_ ⊥ _×_
89+
×-zeroʳ A = mk↔′ proj₂ ⊥-elim ⊥-elim λ ()
90+
91+
×-zero : Zero _↔_ ⊥ _×_
92+
×-zero = ×-zeroˡ , ×-zeroʳ
93+
94+
-- × distributes over ⊎
95+
×-distribˡ-⊎ : _DistributesOverˡ_ {ℓ = ℓ} _↔_ _×_ _⊎_
96+
×-distribˡ-⊎ _ _ _ = mk↔′
97+
(uncurry λ x [ inj₁ ∘′ (x ,_) , inj₂ ∘′ (x ,_) ]′)
98+
[ map₂ inj₁ , map₂ inj₂ ]′
99+
Sum.[ cong′ , cong′ ]
100+
(uncurry λ _ Sum.[ cong′ , cong′ ])
101+
102+
×-distribʳ-⊎ : _DistributesOverʳ_ {ℓ = ℓ} _↔_ _×_ _⊎_
103+
×-distribʳ-⊎ _ _ _ = mk↔′
104+
(uncurry [ curry inj₁ , curry inj₂ ]′)
105+
[ map₁ inj₁ , map₁ inj₂ ]′
106+
Sum.[ cong′ , cong′ ]
107+
(uncurry Sum.[ (λ _ cong′) , (λ _ cong′) ])
108+
109+
×-distrib-⊎ : _DistributesOver_ {ℓ = ℓ} _↔_ _×_ _⊎_
110+
×-distrib-⊎ = ×-distribˡ-⊎ , ×-distribʳ-⊎
111+
112+
------------------------------------------------------------------------
113+
-- Algebraic structures
114+
115+
×-isMagma : IsMagma {ℓ = ℓ} _↔_ _×_
116+
×-isMagma = record
117+
{ isEquivalence = ↔-isEquivalence
118+
; ∙-cong = ×-cong
119+
}
120+
121+
×-isSemigroup : IsSemigroup _↔_ _×_
122+
×-isSemigroup = record
123+
{ isMagma = ×-isMagma
124+
; assoc = λ _ _ _ Σ-assoc
125+
}
126+
127+
×-isMonoid : IsMonoid _↔_ _×_ ⊤
128+
×-isMonoid = record
129+
{ isSemigroup = ×-isSemigroup
130+
; identity = ×-identityˡ , ×-identityʳ
131+
}
132+
133+
×-isCommutativeMonoid : IsCommutativeMonoid _↔_ _×_ ⊤
134+
×-isCommutativeMonoid = record
135+
{ isMonoid = ×-isMonoid
136+
; comm = ×-comm
137+
}
138+
139+
⊎-×-isSemiringWithoutAnnihilatingZero : IsSemiringWithoutAnnihilatingZero _↔_ _⊎_ _×_ ⊥ ⊤
140+
⊎-×-isSemiringWithoutAnnihilatingZero = record
141+
{ +-isCommutativeMonoid = ⊎-isCommutativeMonoid ℓ
142+
; *-isMonoid = ×-isMonoid
143+
; distrib = ×-distrib-⊎
144+
}
145+
146+
⊎-×-isSemiring : IsSemiring _↔_ _⊎_ _×_ ⊥ ⊤
147+
⊎-×-isSemiring = record
148+
{ isSemiringWithoutAnnihilatingZero = ⊎-×-isSemiringWithoutAnnihilatingZero
149+
; zero = ×-zero
150+
}
151+
152+
⊎-×-isCommutativeSemiring : IsCommutativeSemiring _↔_ _⊎_ _×_ ⊥ ⊤
153+
⊎-×-isCommutativeSemiring = record
154+
{ isSemiring = ⊎-×-isSemiring
155+
; *-comm = ×-comm
156+
}
157+
------------------------------------------------------------------------
158+
-- Algebraic bundles
159+
160+
×-magma : Magma (suc ℓ) ℓ
161+
×-magma = record
162+
{ isMagma = ×-isMagma
163+
}
164+
165+
×-semigroup : Semigroup (suc ℓ) ℓ
166+
×-semigroup = record
167+
{ isSemigroup = ×-isSemigroup
168+
}
169+
170+
×-monoid : Monoid (suc ℓ) ℓ
171+
×-monoid = record
172+
{ isMonoid = ×-isMonoid
173+
}
174+
175+
×-commutativeMonoid : CommutativeMonoid (suc ℓ) ℓ
176+
×-commutativeMonoid = record
177+
{ isCommutativeMonoid = ×-isCommutativeMonoid
178+
}
179+
180+
×-⊎-commutativeSemiring : CommutativeSemiring (suc ℓ) ℓ
181+
×-⊎-commutativeSemiring = record
182+
{ isCommutativeSemiring = ⊎-×-isCommutativeSemiring
183+
}
184+

src/Data/Product/Properties.agda

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,24 @@ module Data.Product.Properties where
1010

1111
open import Axiom.UniquenessOfIdentityProofs
1212
open import Data.Product
13-
open import Function using (_∘_;_∋_)
13+
open import Function
14+
open import Level using (Level)
1415
open import Relation.Binary using (DecidableEquality)
1516
open import Relation.Binary.PropositionalEquality
1617
open import Relation.Nullary.Product
1718
import Relation.Nullary.Decidable as Dec
1819
open import Relation.Nullary using (Dec; yes; no)
1920

21+
private
22+
variable
23+
a b ℓ : Level
24+
A : Set a
25+
B : Set b
26+
2027
------------------------------------------------------------------------
2128
-- Equality (dependent)
2229

23-
module _ {a b} {A : Set a} {B : A Set b} where
30+
module _ {B : A Set b} where
2431

2532
,-injectiveˡ : {a c} {b : B a} {d : B c} (a , b) ≡ (c , d) a ≡ c
2633
,-injectiveˡ refl = refl
@@ -40,10 +47,57 @@ module _ {a b} {A : Set a} {B : A → Set b} where
4047
------------------------------------------------------------------------
4148
-- Equality (non-dependent)
4249

43-
module _ {a b} {A : Set a} {B : Set b} where
50+
,-injectiveʳ : {a c : A} {b d : B} (a , b) ≡ (c , d) b ≡ d
51+
,-injectiveʳ refl = refl
52+
53+
,-injective : {a c : A} {b d : B} (a , b) ≡ (c , d) a ≡ c × b ≡ d
54+
,-injective refl = refl , refl
55+
56+
-- The following properties are definitionally true (because of η)
57+
-- but for symmetry with ⊎ it is convenient to define and name them.
58+
59+
swap-involutive : swap {A = A} {B = B} ∘ swap ≗ id
60+
swap-involutive _ = refl
61+
62+
------------------------------------------------------------------------
63+
-- Equality between pairs can be expressed as a pair of equalities
64+
65+
Σ-≡,≡↔≡ : {A : Set a} {B : A Set b} {p₁@(a₁ , b₁) p₂@(a₂ , b₂) : Σ A B}
66+
(∃ λ (p : a₁ ≡ a₂) subst B p b₁ ≡ b₂) ↔ (p₁ ≡ p₂)
67+
Σ-≡,≡↔≡ {A = A} {B = B} = mk↔ {f = to} (right-inverse-of , left-inverse-of)
68+
where
69+
to : {p₁@(a₁ , b₁) p₂@(a₂ , b₂) : Σ A B}
70+
Σ (a₁ ≡ a₂) (λ p subst B p b₁ ≡ b₂) p₁ ≡ p₂
71+
to (refl , refl) = refl
72+
73+
from : {p₁@(a₁ , b₁) p₂@(a₂ , b₂) : Σ A B}
74+
p₁ ≡ p₂ Σ (a₁ ≡ a₂) (λ p subst B p b₁ ≡ b₂)
75+
from refl = refl , refl
76+
77+
left-inverse-of : {p₁@(a₁ , b₁) p₂@(a₂ , b₂) : Σ A B}
78+
(p : Σ (a₁ ≡ a₂) (λ x subst B x b₁ ≡ b₂))
79+
from (to p) ≡ p
80+
left-inverse-of (refl , refl) = refl
81+
82+
right-inverse-of : {p₁ p₂ : Σ A B} (p : p₁ ≡ p₂) to (from p) ≡ p
83+
right-inverse-of refl = refl
84+
85+
-- the non-dependent case. Proofs are exactly as above, and straightforward.
86+
×-≡,≡↔≡ : {p₁@(a₁ , b₁) p₂@(a₂ , b₂) : A × B} (a₁ ≡ a₂ × b₁ ≡ b₂) ↔ p₁ ≡ p₂
87+
×-≡,≡↔≡ = mk↔′
88+
(λ {(refl , refl) refl})
89+
(λ { refl refl , refl})
90+
(λ {refl refl})
91+
(λ {(refl , refl) refl})
92+
93+
------------------------------------------------------------------------
94+
-- The order of ∃₂ can be swapped
4495

45-
,-injectiveʳ : {a c : A} {b d : B} (a , b) ≡ (c , d) b ≡ d
46-
,-injectiveʳ refl = refl
96+
∃∃↔∃∃ : (R : A B Set ℓ) (∃₂ λ x y R x y) ↔ (∃₂ λ y x R x y)
97+
∃∃↔∃∃ R = mk↔′ to from cong′ cong′
98+
where
99+
to : (∃₂ λ x y R x y) (∃₂ λ y x R x y)
100+
to (x , y , Rxy) = (y , x , Rxy)
47101

48-
,-injective : {a c : A} {b d : B} (a , b) ≡ (c , d) a ≡ c × b ≡ d
49-
,-injective refl = refl , refl
102+
from : (∃₂ λ y x R x y) (∃₂ λ x y R x y)
103+
from (y , x , Rxy) = (x , y , Rxy)

0 commit comments

Comments
 (0)