summaryrefslogtreecommitdiff
path: root/src/Data/Star/Vec.agda
blob: e10b9414e5a372a695d96586d5e96fb57ba44ebe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
------------------------------------------------------------------------
-- The Agda standard library
--
-- Vectors defined in terms of the reflexive-transitive closure, Star
------------------------------------------------------------------------

module Data.Star.Vec where

open import Data.Star.Nat
open import Data.Star.Fin using (Fin)
open import Data.Star.Decoration
open import Data.Star.Pointer as Pointer hiding (lookup)
open import Data.Star.List using (List)
open import Relation.Binary
open import Relation.Binary.Consequences
open import Relation.Binary.Construct.Closure.ReflexiveTransitive
open import Function
open import Data.Unit

-- The vector type. Vectors are natural numbers decorated with extra
-- information (i.e. elements).

Vec : Set  Set
Vec A = All (λ _  A)

-- Nil and cons.

[] :  {A}  Vec A zero
[] = ε

infixr 5 _∷_

_∷_ :  {A n}  A  Vec A n  Vec A (suc n)
x ∷ xs = ↦ x ◅ xs

-- Projections.

head :  {A n}  Vec A (1# + n)  A
head (↦ x ◅ _) = x

tail :  {A n}  Vec A (1# + n)  Vec A n
tail (↦ _ ◅ xs) = xs

-- Append.

infixr 5 _++_

_++_ :  {A m n}  Vec A m  Vec A n  Vec A (m + n)
_++_ = _◅◅◅_

-- Safe lookup.

lookup :  {A n}  Fin n  Vec A n  A
lookup i xs with Pointer.lookup i xs
... | result _ x = x

------------------------------------------------------------------------
-- Conversions

fromList :  {A}  (xs : List A)  Vec A (length xs)
fromList ε        = []
fromList (x ◅ xs) = x ∷ fromList xs

toList :  {A n}  Vec A n  List A
toList = gmap (const tt) decoration