| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Examples showing how the generic n-ary operations the stdlib provides |
| 5 |
-- can be used |
| 6 |
------------------------------------------------------------------------ |
| 7 |
|
| 8 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 9 |
|
| 10 |
module README.Nary where |
| 11 |
|
| 12 |
open import Level using (Level) |
| 13 |
open import Data.Nat.Base |
| 14 |
open import Data.Nat.Properties |
| 15 |
open import Data.Fin using (Fin; fromℕ; #_; inject₁) |
| 16 |
open import Data.List |
| 17 |
open import Data.List.Properties |
| 18 |
open import Data.Product.Base using (_×_; _,_) |
| 19 |
open import Data.Sum.Base using (inj₁; inj₂) |
| 20 |
open import Function.Base using (id; flip; _∘′_) |
| 21 |
open import Relation.Nullary |
| 22 |
open import Relation.Binary.Definitions using (module Tri); open Tri |
| 23 |
open import Relation.Binary.PropositionalEquality |
| 24 |
|
| 25 |
private |
| 26 |
variable |
| 27 |
a b c d e : Level |
| 28 |
A : Set a |
| 29 |
B : Set b |
| 30 |
C : Set c |
| 31 |
D : Set d |
| 32 |
E : Set e |
| 33 |
|
| 34 |
------------------------------------------------------------------------ |
| 35 |
-- Introduction |
| 36 |
------------------------------------------------------------------------ |
| 37 |
|
| 38 |
-- Function.Nary.NonDependent and Data.Product.N-ary.Heterogeneous provide |
| 39 |
-- a generic representation of n-ary heterogeneous (non dependent) products |
| 40 |
-- and the corresponding types of (non-dependent) n-ary functions. The |
| 41 |
-- representation works well with inference thus allowing us to use generic |
| 42 |
-- combinators to manipulate such functions. |
| 43 |
|
| 44 |
open import Data.Product.Nary.NonDependent |
| 45 |
open import Function.Nary.NonDependent |
| 46 |
open import Relation.Nary |
| 47 |
|
| 48 |
|
| 49 |
------------------------------------------------------------------------ |
| 50 |
-- Generalised equality-manipulating combinators |
| 51 |
------------------------------------------------------------------------ |
| 52 |
|
| 53 |
-- By default the standard library provides users with (we are leaving out |
| 54 |
-- the implicit arguments here): |
| 55 |
-- |
| 56 |
-- cong : (f : A₁ → B) → a₁ ≡ b₁ → f a₁ ≡ f b₁ |
| 57 |
-- cong₂ : (f : A₁ → A₂ → B) → a₁ ≡ b₁ → a₂ ≡ b₂ → f a₁ a₂ ≡ f b₁ b₂ |
| 58 |
-- |
| 59 |
-- and |
| 60 |
-- |
| 61 |
-- subst : (P : A₁ → Set p) → a₁ ≡ b₁ → P a₁ → P b₁ |
| 62 |
-- subst₂ : (P : A₁ → A₂ → Set p) → a₁ ≡ b₁ → a₂ ≡ b₂ → P a₁ a₂ → P b₁ b₂ |
| 63 |
-- |
| 64 |
-- This pattern can be generalised to any natural number `n`. Thanks to our |
| 65 |
-- library for n-ary functions, we can write the types and implementations |
| 66 |
-- of `congₙ` and `substₙ`. |
| 67 |
|
| 68 |
------------------------------------------------------------------------ |
| 69 |
-- congₙ : ∀ n (f : A₁ → ⋯ → Aₙ → B) → |
| 70 |
-- a₁ ≡ b₁ → ⋯ aₙ ≡ bₙ → f a₁ ⋯ aₙ ≡ f b₁ ⋯ bₙ |
| 71 |
|
| 72 |
-- It may be used directly to prove something: |
| 73 |
|
| 74 |
_ : ∀ (as bs cs : List ℕ) → |
| 75 |
zip (zip (as ++ []) (map id cs)) (reverse (reverse bs)) |
| 76 |
≡ zip (zip as cs) bs |
| 77 |
_ = λ as bs cs → congₙ 3 (λ as bs → zip (zip as bs)) |
| 78 |
(++-identityʳ as) |
| 79 |
(map-id cs) |
| 80 |
(reverse-involutive bs) |
| 81 |
|
| 82 |
-- Or as part of a longer derivation: |
| 83 |
|
| 84 |
_ : ∀ m n p q → suc (m + (p * n) + (q ^ (m + n))) |
| 85 |
≡ (m + 0) + (n * p) + (q ^ m * q ^ n) + 1 |
| 86 |
_ = λ m n p q → begin |
| 87 |
suc (m + (p * n) + (q ^ (m + n))) ≡⟨ +-comm 1 _ ⟩ |
| 88 |
m + (p * n) + (q ^ (m + n)) + 1 ≡⟨ congₙ 3 (λ m n p → m + n + p + 1) |
| 89 |
(+-comm 0 m) |
| 90 |
(*-comm p n) |
| 91 |
(^-distribˡ-+-* q m n) |
| 92 |
⟩ |
| 93 |
m + 0 + n * p + (q ^ m) * (q ^ n) + 1 ∎ where open ≡-Reasoning |
| 94 |
|
| 95 |
-- Partial application of the functional argument is fine: the number of arguments |
| 96 |
-- `congₙ` is going to take is determined by its first argument (a natural number) |
| 97 |
-- and not by the type of the function it works on. |
| 98 |
|
| 99 |
_ : ∀ m → (m +_) ≡ ((m + 0) +_) |
| 100 |
_ = λ m → congₙ 1 _+_ (+-comm 0 m) |
| 101 |
|
| 102 |
-- We don't have to work on the function's first argument either: we can just as |
| 103 |
-- easily use `congₙ` to act on the second one by `flip`ping it. See `holeₙ` for |
| 104 |
-- a generalisation of this idea allowing to target *any* of the function's |
| 105 |
-- arguments and not just the first or second one. |
| 106 |
|
| 107 |
_ : ∀ m → (_+ m) ≡ (_+ (m + 0)) |
| 108 |
_ = λ m → congₙ 1 (flip _+_) (+-comm 0 m) |
| 109 |
|
| 110 |
------------------------------------------------------------------------ |
| 111 |
-- substₙ : (P : A₁ → ⋯ → Aₙ → Set p) → |
| 112 |
-- a₁ ≡ b₁ → ⋯ aₙ ≡ bₙ → P a₁ ⋯ aₙ → P b₁ ⋯ bₙ |
| 113 |
|
| 114 |
-- We can play the same type of game with subst |
| 115 |
|
| 116 |
open import Agda.Builtin.Nat using (mod-helper) |
| 117 |
|
| 118 |
-- Because we know from the definition `mod-helper` that this equation holds: |
| 119 |
-- mod-helper k m (suc n) (suc j) = mod-helper (suc k) m n j |
| 120 |
-- we should be able to prove the slightly modified statement by transforming |
| 121 |
-- all the `x + 1` into `suc x`. We can do so using `substₙ`. |
| 122 |
|
| 123 |
_ : ∀ k m n j → mod-helper k m (n + 1) (j + 1) ≡ mod-helper (k + 1) m n j |
| 124 |
_ = λ k m n j → |
| 125 |
let P sk sn sj = mod-helper k m sn sj ≡ mod-helper sk m n j |
| 126 |
in substₙ P (+-comm 1 k) (+-comm 1 n) (+-comm 1 j) refl |
| 127 |
|
| 128 |
----------------------------------------------------------------------- |
| 129 |
-- Generic programs working on n-ary products & functions |
| 130 |
----------------------------------------------------------------------- |
| 131 |
|
| 132 |
----------------------------------------------------------------------- |
| 133 |
-- curryₙ : ∀ n → (A₁ × ⋯ × Aₙ → B) → A₁ → ⋯ → Aₙ → B |
| 134 |
-- uncurryₙ : ∀ n → (A₁ → ⋯ → Aₙ → B) → A₁ × ⋯ × Aₙ → B |
| 135 |
|
| 136 |
-- The first thing we may want to do generically is convert between |
| 137 |
-- curried function types and uncurried ones. We can do this by using: |
| 138 |
|
| 139 |
-- They both work the same way so we will focus on curryₙ only here. |
| 140 |
-- If we pass to `curryₙ` the arity of its argument then we obtain a |
| 141 |
-- fully curried function. |
| 142 |
|
| 143 |
curry₁ : (A × B × C × D → E) → A → B → C → D → E |
| 144 |
curry₁ = curryₙ 4 |
| 145 |
|
| 146 |
-- Note that here we are not flattening arbitrary nestings: products have |
| 147 |
-- to be right nested. Which means that if you have a deeply-nested product |
| 148 |
-- then it won't be affected by the procedure. |
| 149 |
|
| 150 |
curry₁′ : (A × (B × C) × D → E) → A → (B × C) → D → E |
| 151 |
curry₁′ = curryₙ 3 |
| 152 |
|
| 153 |
-- When we are currying a function, we have no obligation to pass its exact |
| 154 |
-- arity as the parameter: we can decide to only curry part of it like so: |
| 155 |
-- Indeed (A₁ × ⋯ × Aₙ → B) can also be seen as (A₁ × ⋯ × (Aₖ × ⋯ × Aₙ) → B) |
| 156 |
|
| 157 |
curry₂ : (A × B × C × D → E) → A → B → (C × D) → E |
| 158 |
curry₂ = curryₙ 3 |
| 159 |
|
| 160 |
----------------------------------------------------------------------- |
| 161 |
-- projₙ : ∀ n (k : Fin n) → (A₁ × ⋯ × Aₙ) → Aₖ₊₁ |
| 162 |
|
| 163 |
-- Another useful class of functions to manipulate n-ary product is a |
| 164 |
-- generic projection function. Note the (k + 1) in the return index: |
| 165 |
-- Fin counts from 0 up. |
| 166 |
|
| 167 |
-- It behaves as one expects (Data.Fin's #_ comes in handy to write down |
| 168 |
-- Fin literals): |
| 169 |
|
| 170 |
proj₃ : (A × B × C × D × E) → C |
| 171 |
proj₃ = projₙ 5 (# 2) |
| 172 |
|
| 173 |
-- Of course we can once more project the "tail" of the n-ary product by |
| 174 |
-- passing `projₙ` a natural number which is smaller than the size of the |
| 175 |
-- n-ary product, seeing (A₁ × ⋯ × Aₙ) as (A₁ × ⋯ × (Aₖ × ⋯ × Aₙ)). |
| 176 |
|
| 177 |
proj₃′ : (A × B × C × D × E) → C × D × E |
| 178 |
proj₃′ = projₙ 3 (# 2) |
| 179 |
|
| 180 |
----------------------------------------------------------------------- |
| 181 |
-- insertₙ : ∀ n (k : Fin (suc n)) → |
| 182 |
-- B → (A₁ × ⋯ Aₙ) → (A₁ × ⋯ × Aₖ × B × Aₖ₊₁ × ⋯ Aₙ) |
| 183 |
|
| 184 |
insert₁ : C → (A × B × D × E) → (A × B × C × D × E) |
| 185 |
insert₁ = insertₙ 4 (# 2) |
| 186 |
|
| 187 |
insert₁′ : C → (A × B × D × E) → (A × B × C × D × E) |
| 188 |
insert₁′ = insertₙ 3 (# 2) |
| 189 |
|
| 190 |
-- Note that `insertₙ` takes a `Fin (suc n)`. Indeed in an n-ary product |
| 191 |
-- there are (suc n) positions at which one may insert a value. We may |
| 192 |
-- insert at the front or the back of the product: |
| 193 |
|
| 194 |
insert-front : A → (B × C × D × E) → (A × B × C × D × E) |
| 195 |
insert-front = insertₙ 4 (# 0) |
| 196 |
|
| 197 |
insert-back : E → (A × B × C × D) → (A × B × C × D × E) |
| 198 |
insert-back = insertₙ 4 (# 4) |
| 199 |
|
| 200 |
----------------------------------------------------------------------- |
| 201 |
-- removeₙ : ∀ n (k : Fin n) → (A₁ × ⋯ Aₙ) → (A₁ × ⋯ × Aₖ × Aₖ₊₂ × ⋯ Aₙ) |
| 202 |
|
| 203 |
-- Dual to `insertₙ`, we may remove a value. |
| 204 |
|
| 205 |
remove₁ : (A × B × C × D × E) → (A × B × D × E) |
| 206 |
remove₁ = removeₙ 5 (# 2) |
| 207 |
|
| 208 |
-- Inserting at `k` and then removing at `inject₁ k` should yield the identity |
| 209 |
|
| 210 |
remove-insert : C → (A × B × D × E) → (A × B × D × E) |
| 211 |
remove-insert c = removeₙ 5 (inject₁ k) ∘′ insertₙ 4 k c |
| 212 |
where k = # 2 |
| 213 |
|
| 214 |
----------------------------------------------------------------------- |
| 215 |
-- updateₙ : ∀ n (k : Fin n) (f : (a : Aₖ₊₁) → B a) → |
| 216 |
-- (p : A₁ × ⋯ Aₙ) → (A₁ × ⋯ × Aₖ × B (projₙ n k p) × Aₖ₊₂ × ⋯ Aₙ) |
| 217 |
|
| 218 |
-- We can not only project out, insert or remove values: we can update them |
| 219 |
-- in place. The type (and value) of the replacement at position k may depend |
| 220 |
-- upon the current value at position k. |
| 221 |
|
| 222 |
update₁ : (p : A × B × ℕ × C × D) → (A × B × Fin _ × C × D) |
| 223 |
update₁ = updateₙ 5 (# 2) fromℕ |
| 224 |
|
| 225 |
-- We can explicitly use the primed version of `updateₙ` to make it known to |
| 226 |
-- Agda that the update function is non dependent. This type of information |
| 227 |
-- is useful for inference: the tighter the constraints, the easier it is to |
| 228 |
-- find a solution (if possible). |
| 229 |
|
| 230 |
update₂ : (p : A × B × ℕ × C × D) → (A × B × List D × C × D) |
| 231 |
update₂ = λ p → updateₙ′ 5 (# 2) (λ n → replicate n (projₙ 5 (# 4) p)) p |
| 232 |
|
| 233 |
----------------------------------------------------------------------- |
| 234 |
-- _%=_⊢_ : ∀ n → (C → D) → (A₁ → ⋯ Aₙ → D → B) → A₁ → ⋯ → Aₙ → C → B |
| 235 |
|
| 236 |
-- Traditional composition (also known as the index update operator `_⊢_` |
| 237 |
-- in `Relation.Unary`) focuses solely on the first argument of an n-ary |
| 238 |
-- function. `_%=_⊢_` on the other hand allows us to touch any one of the |
| 239 |
-- arguments. |
| 240 |
|
| 241 |
-- In the following example we have a function `f : A → B` and `replicate` |
| 242 |
-- of type `ℕ → B → List B`. We want ̀f` to act on the second argument of |
| 243 |
-- replicate. Which we can do like so. |
| 244 |
|
| 245 |
compose₁ : (A → B) → ℕ → A → List B |
| 246 |
compose₁ f = 1 %= f ⊢ replicate |
| 247 |
|
| 248 |
-- Here we spell out the equivalent explicit variable-manipulation and |
| 249 |
-- prove the two functions equal. |
| 250 |
|
| 251 |
compose₁′ : (A → B) → ℕ → A → List B |
| 252 |
compose₁′ f n a = replicate n (f a) |
| 253 |
|
| 254 |
compose₁-eq : compose₁ {a} {A} {b} {B} ≡ compose₁′ |
| 255 |
compose₁-eq = refl |
| 256 |
|
| 257 |
----------------------------------------------------------------------- |
| 258 |
-- _∷=_⊢_ : ∀ n → A → (A₁ → ⋯ Aₙ → A → B) → A₁ → ⋯ → Aₙ → B |
| 259 |
|
| 260 |
-- Partial application usually focuses on the first argument of a function. |
| 261 |
-- We can now partially apply a function in any of its arguments using |
| 262 |
-- `_∷=_⊢_`. Reusing our example involving replicate: we can specialise it |
| 263 |
-- to only output finite lists of `0`: |
| 264 |
|
| 265 |
apply₁ : ℕ → List ℕ |
| 266 |
apply₁ = 1 ∷= 0 ⊢ replicate |
| 267 |
|
| 268 |
apply₁-eq : apply₁ 3 ≡ 0 ∷ 0 ∷ 0 ∷ [] |
| 269 |
apply₁-eq = refl |
| 270 |
|
| 271 |
------------------------------------------------------------------------ |
| 272 |
-- holeₙ : ∀ n → (A → (A₁ → ⋯ Aₙ → B)) → A₁ → ⋯ → Aₙ → (A → B) |
| 273 |
|
| 274 |
-- As we have seen earlier, `cong` acts on a function's first variable. |
| 275 |
-- If we want to access the second one, we can use `flip`. But what about |
| 276 |
-- the fourth one? We typically use an explicit λ-abstraction shuffling |
| 277 |
-- variables. Not anymore. |
| 278 |
|
| 279 |
-- Reusing mod-helper just because it takes a lot of arguments: |
| 280 |
|
| 281 |
hole₁ : ∀ k m n j → mod-helper k (m + 1) n j ≡ mod-helper k (suc m) n j |
| 282 |
hole₁ = λ k m n j → cong (holeₙ 2 (mod-helper k) n j) (+-comm m 1) |
| 283 |
|
| 284 |
----------------------------------------------------------------------- |
| 285 |
-- mapₙ : ∀ n → (B → C) → (A₁ → ⋯ Aₙ → B) → (A₁ → ⋯ → Aₙ → C) |
| 286 |
|
| 287 |
-- (R →_) gives us the reader monad (and, a fortiori, functor). That is to |
| 288 |
-- say that given a function (A → B) and an (R → A) we can get an (R → B) |
| 289 |
-- This generalises to n-ary functions. |
| 290 |
|
| 291 |
-- Reusing our `composeₙ` example: instead of applying `f` to the replicated |
| 292 |
-- element, we can map it on the resulting list. Giving us: |
| 293 |
|
| 294 |
map₁ : (A → B) → ℕ → A → List B |
| 295 |
map₁ f = mapₙ 2 (map f) replicate |
| 296 |
|
| 297 |
------------------------------------------------------------------------ |
| 298 |
-- constₙ : ∀ n → B → A₁ → ⋯ → Aₙ → B |
| 299 |
|
| 300 |
-- `const` is basically `pure` for the reader monad discussed above. Just |
| 301 |
-- like we can generalise the functorial action corresponding to the reader |
| 302 |
-- functor to n-ary functions, we can do the same for `pure`. |
| 303 |
|
| 304 |
const₁ : A → B → C → D → E → A |
| 305 |
const₁ = constₙ 4 |
| 306 |
|
| 307 |
-- Together with `holeₙ`, this means we can make a constant function out |
| 308 |
-- of any of the arguments. The fourth for instance: |
| 309 |
|
| 310 |
const₂ : A → B → C → D → E → D |
| 311 |
const₂ = holeₙ 3 (constₙ 4) |
| 312 |
|
| 313 |
------------------------------------------------------------------------ |
| 314 |
-- Generalised quantifiers |
| 315 |
------------------------------------------------------------------------ |
| 316 |
|
| 317 |
-- As we have seen multiple times already, one of the advantages of working |
| 318 |
-- with non-dependent products is that they can be easily inferred. This is |
| 319 |
-- a prime opportunity to define generic quantifiers. |
| 320 |
|
| 321 |
-- And because n-ary relations are Set-terminated, there is no ambiguity |
| 322 |
-- where to split between arguments & codomain. As a consequence Agda can |
| 323 |
-- infer even `n`, the number of arguments. We can use notations which are |
| 324 |
-- just like the ones defined in `Relation.Unary`. |
| 325 |
|
| 326 |
------------------------------------------------------------------------ |
| 327 |
-- ∃⟨_⟩ : (A₁ → ⋯ → Aₙ → Set r) → Set _ |
| 328 |
-- ∃⟨ P ⟩ = ∃ λ a₁ → ⋯ → ∃ λ aₙ → P a₁ ⋯ aₙ |
| 329 |
|
| 330 |
-- Returning to our favourite function taking a lot of arguments: we can |
| 331 |
-- find a set of input for which it evaluates to 666 |
| 332 |
|
| 333 |
exist₁ : ∃⟨ (λ k m n j → mod-helper k m n j ≡ 666) ⟩ |
| 334 |
exist₁ = 19 , 793 , 3059 , 10 , refl |
| 335 |
|
| 336 |
------------------------------------------------------------------------ |
| 337 |
-- ∀[_] : (A₁ → ⋯ → Aₙ → Set r) → Set _ |
| 338 |
-- ∀[_] P = ∀ {a₁} → ⋯ → ∀ {aₙ} → P a₁ ⋯ aₙ |
| 339 |
|
| 340 |
all₁ : ∀[ (λ (a₁ a₂ : ℕ) → Dec (a₁ ≡ a₂)) ] |
| 341 |
all₁ {a₁} {a₂} = a₁ ≟ a₂ |
| 342 |
|
| 343 |
------------------------------------------------------------------------ |
| 344 |
-- Π : (A₁ → ⋯ → Aₙ → Set r) → Set _ |
| 345 |
-- Π P = ∀ a₁ → ⋯ → ∀ aₙ → P a₁ ⋯ aₙ |
| 346 |
|
| 347 |
all₂ : Π[ (λ (a₁ a₂ : ℕ) → Dec (a₁ ≡ a₂)) ] |
| 348 |
all₂ = _≟_ |
| 349 |
|
| 350 |
------------------------------------------------------------------------ |
| 351 |
-- _⇒_ : (A₁ → ⋯ → Aₙ → Set r) → (A₁ → ⋯ → Aₙ → Set s) → (A₁ → ⋯ → Aₙ → Set _) |
| 352 |
-- P ⇒ Q = λ a₁ → ⋯ → λ aₙ → P a₁ ⋯ aₙ → Q a₁ ⋯ aₙ |
| 353 |
|
| 354 |
antisym : ∀[ _≤_ ⇒ _≥_ ⇒ _≡_ ] |
| 355 |
antisym = ≤-antisym |
| 356 |
|
| 357 |
------------------------------------------------------------------------ |
| 358 |
-- _∪_ : (A₁ → ⋯ → Aₙ → Set r) → (A₁ → ⋯ → Aₙ → Set s) → (A₁ → ⋯ → Aₙ → Set _) |
| 359 |
-- P ∪ Q = λ a₁ → ⋯ → λ aₙ → P a₁ ⋯ aₙ ⊎ Q a₁ ⋯ aₙ |
| 360 |
|
| 361 |
≤->-connex : Π[ _≤_ ∪ _>_ ] |
| 362 |
≤->-connex m n with <-cmp m n |
| 363 |
... | tri< a ¬b ¬c = inj₁ (<⇒≤ a) |
| 364 |
... | tri≈ ¬a b ¬c = inj₁ (≤-reflexive b) |
| 365 |
... | tri> ¬a ¬b c = inj₂ c |
| 366 |
|
| 367 |
------------------------------------------------------------------------ |
| 368 |
-- _∩_ : (A₁ → ⋯ → Aₙ → Set r) → (A₁ → ⋯ → Aₙ → Set s) → (A₁ → ⋯ → Aₙ → Set _) |
| 369 |
-- P ∩ Q = λ a₁ → ⋯ → λ aₙ → P a₁ ⋯ aₙ × Q a₁ ⋯ aₙ |
| 370 |
|
| 371 |
<-inversion : ∀[ _<_ ⇒ _≤_ ∩ _≢_ ] |
| 372 |
<-inversion m<n = <⇒≤ m<n , <⇒≢ m<n |
| 373 |
|
| 374 |
------------------------------------------------------------------------ |
| 375 |
-- ∁ : (A₁ → ⋯ → Aₙ → Set r) → (A₁ → ⋯ → Aₙ → Set _) |
| 376 |
-- ∁ P = λ a₁ → ⋯ → λ aₙ → ¬ (P a₁ ⋯ aₙ) |
| 377 |
|
| 378 |
m<n⇒m≱n : ∀[ _>_ ⇒ ∁ _≤_ ] |
| 379 |
m<n⇒m≱n m>n m≤n = <⇒≱ m>n m≤n |