Debian Codegraph @

src/Data/Vec.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Vectors
5 ------------------------------------------------------------------------
6
7 -- This implementation is designed for reasoning about dynamic
8 -- vectors which may increase or decrease in size.
9
10 -- See `Data.Vec.Functional` for an alternative implementation as
11 -- functions from finite sets, which is more suitable for reasoning
12 -- about fixed sized vectors and for when ease of retrieval is
13 -- important.
14
15 {-# OPTIONS --cubical-compatible --safe #-}
16
17 module Data.Vec where
18
19 open import Level
20 open import Data.Bool.Base
21 import Data.Nat.Properties as ℕ
22 open import Data.Vec.Bounded.Base as Vec≤
23 using (Vec≤; ≤-cast; fromVec)
24 open import Function.Base using (_$_)
25 open import Relation.Nullary
26 open import Relation.Unary
27
28 private
29 variable
30 a p : Level
31 A : Set a
32
33 ------------------------------------------------------------------------
34 -- Publicly re-export the contents of the base module
35
36 open import Data.Vec.Base public
37
38 ------------------------------------------------------------------------
39 -- Additional operations
40
41 module _ {P : A → Set p} (P? : Decidable P) where
42
43 filter : ∀ {n} → Vec A n → Vec≤ A n
44 filter [] = Vec≤.[]
45 filter (a ∷ as) = if does (P? a) then a Vec≤.∷_ else ≤-cast (ℕ.n≤1+n _) $ filter as
46
47 takeWhile : ∀ {n} → Vec A n → Vec≤ A n
48 takeWhile [] = Vec≤.[]
49 takeWhile (a ∷ as) = if does (P? a) then a Vec≤.∷ takeWhile as else Vec≤.[]
50
51 dropWhile : ∀ {n} → Vec A n → Vec≤ A n
52 dropWhile Vec.[] = Vec≤.[]
53 dropWhile (a Vec.∷ as) =
54 if does (P? a) then ≤-cast (ℕ.n≤1+n _) (dropWhile as)
55 else fromVec (a Vec.∷ as)