Debian Codegraph @

src/Data/Star/Vec.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Vectors defined in terms of the reflexive-transitive closure, Star
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --with-K --safe #-}
8
9 module Data.Star.Vec where
10
11 open import Data.Star.Nat using (ℕ; zero; suc; 1#; _+_; length)
12 open import Data.Star.Fin using (Fin)
13 open import Data.Star.Decoration using (All; ↦; _◅◅◅_; decoration)
14 open import Data.Star.Pointer as Pointer using (result)
15 open import Data.Star.List using (List)
16 open import Level using (Level)
17 open import Relation.Binary.Construct.Closure.ReflexiveTransitive
18 using (ε; _◅_; gmap)
19 open import Function.Base using (const; case_of_)
20 open import Data.Unit.Base using (tt)
21
22 private
23 variable
24 a : Level
25 A : Set a
26
27 -- The vector type. Vectors are natural numbers decorated with extra
28 -- information (i.e. elements).
29
30 Vec : Set a → ℕ → Set a
31 Vec A = All (λ _ → A)
32
33 -- Nil and cons.
34
35 [] : Vec A zero
36 [] = ε
37
38 infixr 5 _∷_
39
40 _∷_ : ∀ {n} → A → Vec A n → Vec A (suc n)
41 x ∷ xs = ↦ x ◅ xs
42
43 -- Projections.
44
45 head : ∀ {n} → Vec A (1# + n) → A
46 head (↦ x ◅ _) = x
47
48 tail : ∀ {n} → Vec A (1# + n) → Vec A n
49 tail (↦ _ ◅ xs) = xs
50
51 -- Append.
52
53 infixr 5 _++_
54
55 _++_ : ∀ {m n} → Vec A m → Vec A n → Vec A (m + n)
56 _++_ = _◅◅◅_
57
58 -- Safe lookup.
59
60 lookup : ∀ {n} → Vec A n → Fin n → A
61 lookup xs i with result _ x ← Pointer.lookup xs i = x
62
63 ------------------------------------------------------------------------
64 -- Conversions
65
66 fromList : (xs : List A) → Vec A (length xs)
67 fromList ε = []
68 fromList (x ◅ xs) = x ∷ fromList xs
69
70 toList : ∀ {n} → Vec A n → List A
71 toList = gmap (const tt) decoration