Debian Codegraph @

src/Data/List/Nary/NonDependent.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Nondependent N-ary functions manipulating lists
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Data.List.Nary.NonDependent where
10
11 open import Data.Nat.Base using (zero; suc)
12 open import Data.List.Base as List using (List; []; _∷_)
13 open import Data.Product.Base as Product using (_,_)
14 open import Data.Product.Nary.NonDependent using (Product)
15 open import Function.Nary.NonDependent.Base using (Arrows; Sets; _<$>_)
16
17 ------------------------------------------------------------------------
18 -- n-ary smart constructors
19
20 nilₙ : ∀ n {ls} {as : Sets n ls} → Product n (List <$> as)
21 nilₙ 0 = _
22 nilₙ 1 = []
23 nilₙ (suc n@(suc _)) = [] , nilₙ n
24
25 consₙ : ∀ n {ls} {as : Sets n ls} →
26 Product n as → Product n (List <$> as) → Product n (List <$> as)
27 consₙ 0 _ _ = _
28 consₙ 1 a as = a ∷ as
29 consₙ (suc n@(suc _)) (a , xs) (as , xss) = a ∷ as , consₙ n xs xss
30
31 ------------------------------------------------------------------------
32 -- n-ary zipWith-like operations
33
34 zipWith : ∀ n {ls} {as : Sets n ls} {r} {R : Set r} →
35 Arrows n as R → Arrows n (List <$> as) (List R)
36 zipWith 0 f = []
37 zipWith 1 f xs = List.map f xs
38 zipWith (suc n@(suc _)) f xs ys =
39 zipWith n (Product.uncurry f) (List.zipWith _,_ xs ys)
40
41 unzipWith : ∀ n {ls} {as : Sets n ls} {a} {A : Set a} →
42 (A → Product n as) → (List A → Product n (List <$> as))
43 unzipWith n f [] = nilₙ n
44 unzipWith n f (a ∷ as) = consₙ n (f a) (unzipWith n f as)