| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Code for converting Vec A n → B to and from n-ary functions |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.Vec.N-ary where |
| 10 |
|
| 11 |
open import Axiom.Extensionality.Propositional using (Extensionality) |
| 12 |
open import Function.Bundles using (_↔_; Inverse; mk↔ₛ′) |
| 13 |
open import Data.Nat.Base hiding (_⊔_) |
| 14 |
open import Data.Product.Base as Product using (∃; _,_) |
| 15 |
open import Data.Vec.Base using (Vec; []; _∷_; head; tail) |
| 16 |
open import Function.Base using (_∘_; id; flip; constᵣ) |
| 17 |
open import Function.Bundles using (_⇔_; mk⇔) |
| 18 |
open import Level using (Level; _⊔_) |
| 19 |
open import Relation.Binary.Core using (REL) |
| 20 |
open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl; cong) |
| 21 |
|
| 22 |
private |
| 23 |
variable |
| 24 |
a b c ℓ ℓ₁ ℓ₂ : Level |
| 25 |
A : Set a |
| 26 |
B : Set b |
| 27 |
C : Set c |
| 28 |
|
| 29 |
------------------------------------------------------------------------ |
| 30 |
-- N-ary functions |
| 31 |
|
| 32 |
N-ary-level : Level → Level → ℕ → Level |
| 33 |
N-ary-level ℓ₁ ℓ₂ zero = ℓ₂ |
| 34 |
N-ary-level ℓ₁ ℓ₂ (suc n) = ℓ₁ ⊔ N-ary-level ℓ₁ ℓ₂ n |
| 35 |
|
| 36 |
N-ary : ∀ (n : ℕ) → Set ℓ₁ → Set ℓ₂ → Set (N-ary-level ℓ₁ ℓ₂ n) |
| 37 |
N-ary zero A B = B |
| 38 |
N-ary (suc n) A B = A → N-ary n A B |
| 39 |
|
| 40 |
------------------------------------------------------------------------ |
| 41 |
-- Conversion |
| 42 |
|
| 43 |
curryⁿ : ∀ {n} → (Vec A n → B) → N-ary n A B |
| 44 |
curryⁿ {n = zero} f = f [] |
| 45 |
curryⁿ {n = suc n} f = λ x → curryⁿ (f ∘ _∷_ x) |
| 46 |
|
| 47 |
infix -1 _$ⁿ_ |
| 48 |
|
| 49 |
_$ⁿ_ : ∀ {n} → N-ary n A B → (Vec A n → B) |
| 50 |
f $ⁿ [] = f |
| 51 |
f $ⁿ (x ∷ xs) = f x $ⁿ xs |
| 52 |
|
| 53 |
------------------------------------------------------------------------ |
| 54 |
-- Quantifiers |
| 55 |
|
| 56 |
module _ {A : Set a} where |
| 57 |
|
| 58 |
-- Universal quantifier. |
| 59 |
|
| 60 |
∀ⁿ : ∀ n → N-ary n A (Set ℓ) → Set (N-ary-level a ℓ n) |
| 61 |
∀ⁿ zero P = P |
| 62 |
∀ⁿ (suc n) P = ∀ x → ∀ⁿ n (P x) |
| 63 |
|
| 64 |
-- Universal quantifier with implicit (hidden) arguments. |
| 65 |
|
| 66 |
∀ⁿʰ : ∀ n → N-ary n A (Set ℓ) → Set (N-ary-level a ℓ n) |
| 67 |
∀ⁿʰ zero P = P |
| 68 |
∀ⁿʰ (suc n) P = ∀ {x} → ∀ⁿʰ n (P x) |
| 69 |
|
| 70 |
-- Existential quantifier. |
| 71 |
|
| 72 |
∃ⁿ : ∀ n → N-ary n A (Set ℓ) → Set (N-ary-level a ℓ n) |
| 73 |
∃ⁿ zero P = P |
| 74 |
∃ⁿ (suc n) P = ∃ λ x → ∃ⁿ n (P x) |
| 75 |
|
| 76 |
------------------------------------------------------------------------ |
| 77 |
-- N-ary function equality |
| 78 |
|
| 79 |
Eq : ∀ {A : Set a} {B : Set b} {C : Set c} n → |
| 80 |
REL B C ℓ → REL (N-ary n A B) (N-ary n A C) (N-ary-level a ℓ n) |
| 81 |
Eq n _∼_ f g = ∀ⁿ n (curryⁿ {n = n} λ xs → (f $ⁿ xs) ∼ (g $ⁿ xs)) |
| 82 |
|
| 83 |
-- A variant where all the arguments are implicit (hidden). |
| 84 |
|
| 85 |
Eqʰ : ∀ {A : Set a} {B : Set b} {C : Set c} n → |
| 86 |
REL B C ℓ → REL (N-ary n A B) (N-ary n A C) (N-ary-level a ℓ n) |
| 87 |
Eqʰ n _∼_ f g = ∀ⁿʰ n (curryⁿ {n = n} λ xs → (f $ⁿ xs) ∼ (g $ⁿ xs)) |
| 88 |
|
| 89 |
------------------------------------------------------------------------ |
| 90 |
-- Some lemmas |
| 91 |
|
| 92 |
-- The functions curryⁿ and _$ⁿ_ are inverses. |
| 93 |
|
| 94 |
left-inverse : ∀ {n} (f : Vec A n → B) → |
| 95 |
∀ xs → (curryⁿ f $ⁿ xs) ≡ f xs |
| 96 |
left-inverse f [] = refl |
| 97 |
left-inverse f (x ∷ xs) = left-inverse (f ∘ _∷_ x) xs |
| 98 |
|
| 99 |
right-inverse : ∀ n (f : N-ary n A B) → |
| 100 |
Eq n _≡_ (curryⁿ (_$ⁿ_ {n = n} f)) f |
| 101 |
right-inverse zero f = refl |
| 102 |
right-inverse (suc n) f = λ x → right-inverse n (f x) |
| 103 |
|
| 104 |
-- ∀ⁿ can be expressed in an "uncurried" way. |
| 105 |
|
| 106 |
uncurry-∀ⁿ : ∀ n {P : N-ary n A (Set ℓ)} → |
| 107 |
∀ⁿ n P ⇔ (∀ (xs : Vec A n) → P $ⁿ xs) |
| 108 |
uncurry-∀ⁿ {a} {A} {ℓ} n = mk⇔ (⇒ n) (⇐ n) |
| 109 |
where |
| 110 |
⇒ : ∀ n {P : N-ary n A (Set ℓ)} → |
| 111 |
∀ⁿ n P → (∀ (xs : Vec A n) → P $ⁿ xs) |
| 112 |
⇒ zero p [] = p |
| 113 |
⇒ (suc n) p (x ∷ xs) = ⇒ n (p x) xs |
| 114 |
|
| 115 |
⇐ : ∀ n {P : N-ary n A (Set ℓ)} → |
| 116 |
(∀ (xs : Vec A n) → P $ⁿ xs) → ∀ⁿ n P |
| 117 |
⇐ zero p = p [] |
| 118 |
⇐ (suc n) p = λ x → ⇐ n (p ∘ _∷_ x) |
| 119 |
|
| 120 |
-- ∃ⁿ can be expressed in an "uncurried" way. |
| 121 |
|
| 122 |
uncurry-∃ⁿ : ∀ n {P : N-ary n A (Set ℓ)} → |
| 123 |
∃ⁿ n P ⇔ (∃ λ (xs : Vec A n) → P $ⁿ xs) |
| 124 |
uncurry-∃ⁿ {a} {A} {ℓ} n = mk⇔ (⇒ n) (⇐ n) |
| 125 |
where |
| 126 |
⇒ : ∀ n {P : N-ary n A (Set ℓ)} → |
| 127 |
∃ⁿ n P → (∃ λ (xs : Vec A n) → P $ⁿ xs) |
| 128 |
⇒ zero p = ([] , p) |
| 129 |
⇒ (suc n) (x , p) = Product.map (_∷_ x) id (⇒ n p) |
| 130 |
|
| 131 |
⇐ : ∀ n {P : N-ary n A (Set ℓ)} → |
| 132 |
(∃ λ (xs : Vec A n) → P $ⁿ xs) → ∃ⁿ n P |
| 133 |
⇐ zero ([] , p) = p |
| 134 |
⇐ (suc n) (x ∷ xs , p) = (x , ⇐ n (xs , p)) |
| 135 |
|
| 136 |
-- Conversion preserves equality. |
| 137 |
|
| 138 |
module _ (_∼_ : REL B C ℓ) where |
| 139 |
|
| 140 |
curryⁿ-cong : ∀ {n} (f : Vec A n → B) (g : Vec A n → C) → |
| 141 |
(∀ xs → f xs ∼ g xs) → |
| 142 |
Eq n _∼_ (curryⁿ f) (curryⁿ g) |
| 143 |
curryⁿ-cong {n = zero} f g hyp = hyp [] |
| 144 |
curryⁿ-cong {n = suc n} f g hyp = λ x → |
| 145 |
curryⁿ-cong (f ∘ _∷_ x) (g ∘ _∷_ x) (λ xs → hyp (x ∷ xs)) |
| 146 |
|
| 147 |
curryⁿ-cong⁻¹ : ∀ {n} (f : Vec A n → B) (g : Vec A n → C) → |
| 148 |
Eq n _∼_ (curryⁿ f) (curryⁿ g) → |
| 149 |
∀ xs → f xs ∼ g xs |
| 150 |
curryⁿ-cong⁻¹ f g hyp [] = hyp |
| 151 |
curryⁿ-cong⁻¹ f g hyp (x ∷ xs) = |
| 152 |
curryⁿ-cong⁻¹ (f ∘ _∷_ x) (g ∘ _∷_ x) (hyp x) xs |
| 153 |
|
| 154 |
appⁿ-cong : ∀ {n} (f : N-ary n A B) (g : N-ary n A C) → |
| 155 |
Eq n _∼_ f g → |
| 156 |
(xs : Vec A n) → (f $ⁿ xs) ∼ (g $ⁿ xs) |
| 157 |
appⁿ-cong f g hyp [] = hyp |
| 158 |
appⁿ-cong f g hyp (x ∷ xs) = appⁿ-cong (f x) (g x) (hyp x) xs |
| 159 |
|
| 160 |
appⁿ-cong⁻¹ : ∀ {n} (f : N-ary n A B) (g : N-ary n A C) → |
| 161 |
((xs : Vec A n) → (f $ⁿ xs) ∼ (g $ⁿ xs)) → |
| 162 |
Eq n _∼_ f g |
| 163 |
appⁿ-cong⁻¹ {n = zero} f g hyp = hyp [] |
| 164 |
appⁿ-cong⁻¹ {n = suc n} f g hyp = λ x → |
| 165 |
appⁿ-cong⁻¹ (f x) (g x) (λ xs → hyp (x ∷ xs)) |
| 166 |
|
| 167 |
-- Eq and Eqʰ are equivalent. |
| 168 |
|
| 169 |
Eq-to-Eqʰ : ∀ n (_∼_ : REL B C ℓ) {f : N-ary n A B} {g : N-ary n A C} → |
| 170 |
Eq n _∼_ f g → Eqʰ n _∼_ f g |
| 171 |
Eq-to-Eqʰ zero _∼_ eq = eq |
| 172 |
Eq-to-Eqʰ (suc n) _∼_ eq = Eq-to-Eqʰ n _∼_ (eq _) |
| 173 |
|
| 174 |
Eqʰ-to-Eq : ∀ n (_∼_ : REL B C ℓ) {f : N-ary n A B} {g : N-ary n A C} → |
| 175 |
Eqʰ n _∼_ f g → Eq n _∼_ f g |
| 176 |
Eqʰ-to-Eq zero _∼_ eq = eq |
| 177 |
Eqʰ-to-Eq (suc n) _∼_ eq = λ _ → Eqʰ-to-Eq n _∼_ eq |
| 178 |
|
| 179 |
module _ (ext : ∀ {a b} → Extensionality a b) where |
| 180 |
|
| 181 |
Vec↔N-ary : ∀ n → (Vec A n → B) ↔ N-ary n A B |
| 182 |
Vec↔N-ary zero = mk↔ₛ′ (λ vxs → vxs []) (flip constᵣ) (λ _ → refl) |
| 183 |
(λ vxs → ext λ where [] → refl) |
| 184 |
Vec↔N-ary (suc n) = let open Inverse (Vec↔N-ary n) in |
| 185 |
mk↔ₛ′ (λ vxs x → to λ xs → vxs (x ∷ xs)) |
| 186 |
(λ any xs → from (any (head xs)) (tail xs)) |
| 187 |
(λ any → ext λ x → strictlyInverseˡ _) |
| 188 |
(λ vxs → ext λ where (x ∷ xs) → cong (λ f → f xs) (strictlyInverseʳ (λ ys → vxs (x ∷ ys)))) |