Debian Codegraph @

src/Codata/Sized/Covec.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- The Covec type and some operations
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --sized-types #-}
8
9 module Codata.Sized.Covec where
10
11 open import Size using (Size ; ∞)
12 open import Codata.Sized.Thunk using (Thunk; force)
13 open import Codata.Sized.Conat as Conat
14 using (Conat ; zero ; suc; _+_; infinity; fromℕ; _ℕ+_)
15 open import Codata.Sized.Conat.Bisimilarity using (_⊢_≈_; zero; suc)
16 open import Codata.Sized.Conat.Properties using (0ℕ+-identity)
17 open import Codata.Sized.Cofin as Cofin using (Cofin; zero; suc)
18 open import Codata.Sized.Colist as Colist using (Colist ; [] ; _∷_)
19 open import Codata.Sized.Stream as Stream using (Stream ; _∷_)
20 open import Level using (Level)
21
22 private
23 variable
24 a b : Level
25 A : Set a
26 B : Set b
27
28 data Covec (A : Set a) (i : Size) : Conat ∞ → Set a where
29 [] : Covec A i zero
30 _∷_ : ∀ {n} → A → Thunk (λ i → Covec A i (n .force)) i → Covec A i (suc n)
31
32 infixr 5 _∷_
33
34 head : ∀ {n i} → Covec A i (suc n) → A
35 head (x ∷ _) = x
36
37 tail : ∀ {n} → Covec A ∞ (suc n) → Covec A ∞ (n .force)
38 tail (_ ∷ xs) = xs .force
39
40 lookup : ∀ {n} → Covec A ∞ n → Cofin n → A
41 lookup as zero = head as
42 lookup as (suc k) = lookup (tail as) k
43
44 replicate : ∀ {i} → (n : Conat ∞) → A → Covec A i n
45 replicate zero a = []
46 replicate (suc n) a = a ∷ λ where .force → replicate (n .force) a
47
48 cotake : ∀ {i} → (n : Conat ∞) → Stream A i → Covec A i n
49 cotake zero xs = []
50 cotake (suc n) (x ∷ xs) = x ∷ λ where .force → cotake (n .force) (xs .force)
51
52 infixr 5 _++_
53 _++_ : ∀ {i m n} → Covec A i m → Covec A i n → Covec A i (m + n)
54 [] ++ ys = ys
55 (x ∷ xs) ++ ys = x ∷ λ where .force → xs .force ++ ys
56
57 fromColist : ∀ {i} → (xs : Colist A ∞) → Covec A i (Colist.length xs)
58 fromColist [] = []
59 fromColist (x ∷ xs) = x ∷ λ where .force → fromColist (xs .force)
60
61 toColist : ∀ {i n} → Covec A i n → Colist A i
62 toColist [] = []
63 toColist (x ∷ xs) = x ∷ λ where .force → toColist (xs .force)
64
65 fromStream : ∀ {i} → Stream A i → Covec A i infinity
66 fromStream = cotake infinity
67
68 cast : ∀ {i} {m n} → i ⊢ m ≈ n → Covec A i m → Covec A i n
69 cast zero [] = []
70 cast (suc eq) (a ∷ as) = a ∷ λ where .force → cast (eq .force) (as .force)
71
72 module _ {a b} {A : Set a} {B : Set b} where
73
74 map : ∀ {i n} (f : A → B) → Covec A i n → Covec B i n
75 map f [] = []
76 map f (a ∷ as) = f a ∷ λ where .force → map f (as .force)
77
78 ap : ∀ {i n} → Covec (A → B) i n → Covec A i n → Covec B i n
79 ap [] [] = []
80 ap (f ∷ fs) (a ∷ as) = (f a) ∷ λ where .force → ap (fs .force) (as .force)
81
82 scanl : ∀ {i n} → (B → A → B) → B → Covec A i n → Covec B i (1 ℕ+ n)
83 scanl c n [] = n ∷ λ where .force → []
84 scanl c n (a ∷ as) = n ∷ λ where
85 .force → cast (suc λ where .force → 0ℕ+-identity)
86 (scanl c (c n a) (as .force))
87
88 module _ {a b c} {A : Set a} {B : Set b} {C : Set c} where
89
90 zipWith : ∀ {i n} → (A → B → C) → Covec A i n → Covec B i n → Covec C i n
91 zipWith f [] [] = []
92 zipWith f (a ∷ as) (b ∷ bs) =
93 f a b ∷ λ where .force → zipWith f (as .force) (bs .force)