Debian Codegraph @

src/Data/Vec/Recursive.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Vectors defined by recursion
5 ------------------------------------------------------------------------
6
7 -- What is the point of this module? The n-ary products below are
8 -- intended to be used with a fixed n, in which case the nil constructor
9 -- can be avoided: pairs are represented as pairs (x , y), not as
10 -- triples (x , y , unit).
11 -- Additionally, vectors defined by recursion enjoy η-rules. That is to
12 -- say that two vectors of known length are definitionally equal
13 -- whenever their elements are.
14
15 {-# OPTIONS --cubical-compatible --safe #-}
16
17 module Data.Vec.Recursive where
18
19 open import Data.Nat.Base as Nat using (ℕ; zero; suc; NonZero; pred)
20 open import Data.Nat.Properties using (+-comm; *-comm)
21 open import Data.Empty.Polymorphic using (⊥)
22 open import Data.Fin.Base as Fin using (Fin; zero; suc)
23 open import Data.Fin.Properties using (1↔⊤; *↔×)
24 open import Data.Product.Base as Product using (_×_; _,_; proj₁; proj₂)
25 open import Data.Product.Algebra using (×-cong)
26 open import Data.Sum.Base as Sum using (_⊎_)
27 open import Data.Unit.Base using (tt)
28 open import Data.Unit.Polymorphic.Base using (⊤)
29 open import Data.Unit.Polymorphic.Properties using (⊤↔⊤*)
30 open import Data.Vec.Base as Vec using (Vec; _∷_)
31 open import Function.Base using (_∘′_; _∘_; id; const)
32 open import Function.Bundles using (_↔_; mk↔ₛ′; mk↔)
33 open import Function.Properties.Inverse using (↔-isEquivalence; ↔-refl; ↔-sym; ↔-trans)
34 open import Level using (Level; lift)
35 open import Relation.Unary using (IUniversal; Universal; _⇒_)
36 open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl; sym; trans; cong; subst)
37 open import Relation.Binary.Structures using (IsEquivalence)
38
39 private
40 variable
41 a b c p : Level
42 A : Set a
43 B : Set b
44 C : Set c
45
46 ------------------------------------------------------------------------
47 -- Types and patterns
48
49 infix 8 _^_
50 _^_ : Set a → ℕ → Set a
51 A ^ 0 = ⊤
52 A ^ 1 = A
53 A ^ (suc n@(suc _)) = A × A ^ n
54
55 pattern [] = lift tt
56
57 infix 3 _∈[_]_
58 _∈[_]_ : {A : Set a} → A → ∀ n → A ^ n → Set a
59 a ∈[ 0 ] as = ⊥
60 a ∈[ 1 ] a′ = a ≡ a′
61 a ∈[ suc n@(suc _) ] a′ , as = a ≡ a′ ⊎ a ∈[ n ] as
62
63 ------------------------------------------------------------------------
64 -- Basic operations
65
66 cons : ∀ n → A → A ^ n → A ^ suc n
67 cons 0 a _ = a
68 cons (suc n) a as = a , as
69
70 uncons : ∀ n → A ^ suc n → A × A ^ n
71 uncons 0 a = a , lift tt
72 uncons (suc n) (a , as) = a , as
73
74 head : ∀ n → A ^ suc n → A
75 head n as = proj₁ (uncons n as)
76
77 tail : ∀ n → A ^ suc n → A ^ n
78 tail n as = proj₂ (uncons n as)
79
80 fromVec : ∀[ Vec A ⇒ (A ^_) ]
81 fromVec = Vec.foldr (_ ^_) (cons _) _
82
83 toVec : Π[ (A ^_) ⇒ Vec A ]
84 toVec 0 as = Vec.[]
85 toVec (suc n) as = head n as ∷ toVec n (tail n as)
86
87 lookup : ∀ {n} → A ^ n → Fin n → A
88 lookup as (zero {n}) = head n as
89 lookup as (suc {n} k) = lookup (tail n as) k
90
91 replicate : ∀ n → A → A ^ n
92 replicate n a = fromVec (Vec.replicate n a)
93
94 tabulate : ∀ n → (Fin n → A) → A ^ n
95 tabulate n f = fromVec (Vec.tabulate f)
96
97 append : ∀ m n → A ^ m → A ^ n → A ^ (m Nat.+ n)
98 append 0 n xs ys = ys
99 append 1 n x ys = cons n x ys
100 append (suc m@(suc _)) n (x , xs) ys = x , append m n xs ys
101
102 splitAt : ∀ m n → A ^ (m Nat.+ n) → A ^ m × A ^ n
103 splitAt 0 n xs = [] , xs
104 splitAt (suc m) n xs =
105 let (ys , zs) = splitAt m n (tail (m Nat.+ n) xs) in
106 cons m (head (m Nat.+ n) xs) ys , zs
107
108 ------------------------------------------------------------------------
109 -- Manipulating N-ary products
110
111 map : (A → B) → ∀ n → A ^ n → B ^ n
112 map f 0 as = lift tt
113 map f 1 a = f a
114 map f (suc n@(suc _)) (a , as) = f a , map f n as
115
116 ap : ∀ n → (A → B) ^ n → A ^ n → B ^ n
117 ap 0 fs ts = []
118 ap 1 f t = f t
119 ap (suc n@(suc _)) (f , fs) (t , ts) = f t , ap n fs ts
120
121 module _ {P : ℕ → Set p} where
122
123 foldr : P 0 → (A → P 1) → (∀ n → A → P (suc n) → P (suc (suc n))) →
124 ∀ n → A ^ n → P n
125 foldr p0 p1 p2+ 0 as = p0
126 foldr p0 p1 p2+ 1 a = p1 a
127 foldr p0 p1 p2+ (suc n′@(suc n)) (a , as) = p2+ n a (foldr p0 p1 p2+ n′ as)
128
129 foldl : (P : ℕ → Set p) →
130 P 0 → (A → P 1) → (∀ n → A → P (suc n) → P (suc (suc n))) →
131 ∀ n → A ^ n → P n
132 foldl P p0 p1 p2+ 0 as = p0
133 foldl P p0 p1 p2+ 1 a = p1 a
134 foldl P p0 p1 p2+ (suc n@(suc _)) (a , as) = let p1′ = p1 a in
135 foldl (P ∘′ suc) p1′ (λ a → p2+ 0 a p1′) (p2+ ∘ suc) n as
136
137 reverse : ∀ n → A ^ n → A ^ n
138 reverse = foldl (_ ^_) [] id (λ n → _,_)
139
140 zipWith : (A → B → C) → ∀ n → A ^ n → B ^ n → C ^ n
141 zipWith f 0 as bs = []
142 zipWith f 1 a b = f a b
143 zipWith f ((suc n@(suc _))) (a , as) (b , bs) = f a b , zipWith f n as bs
144
145 unzipWith : (A → B × C) → ∀ n → A ^ n → B ^ n × C ^ n
146 unzipWith f 0 as = [] , []
147 unzipWith f 1 a = f a
148 unzipWith f (suc n@(suc _)) (a , as) = Product.zip _,_ _,_ (f a) (unzipWith f n as)
149
150 zip : ∀ n → A ^ n → B ^ n → (A × B) ^ n
151 zip = zipWith _,_
152
153 unzip : ∀ n → (A × B) ^ n → A ^ n × B ^ n
154 unzip = unzipWith id
155
156 lift↔ : ∀ n → A ↔ B → A ^ n ↔ B ^ n
157 lift↔ 0 A↔B = mk↔ₛ′ _ _ (const refl) (const refl)
158 lift↔ 1 A↔B = A↔B
159 lift↔ (suc n@(suc _)) A↔B = ×-cong A↔B (lift↔ n A↔B)
160
161 Fin[m^n]↔Fin[m]^n : ∀ m n → Fin (m Nat.^ n) ↔ Fin m ^ n
162 Fin[m^n]↔Fin[m]^n m 0 = ↔-trans 1↔⊤ (↔-sym ⊤↔⊤*)
163 Fin[m^n]↔Fin[m]^n m 1 = subst (λ x → Fin x ↔ Fin m)
164 (trans (sym (+-comm m zero)) (*-comm 1 m)) ↔-refl
165 Fin[m^n]↔Fin[m]^n m (suc (suc n)) = ↔-trans (*↔× {m = m}) (×-cong ↔-refl (Fin[m^n]↔Fin[m]^n _ _))