Debian Codegraph @

src/Data/Star/BoundedVec.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Bounded vectors (inefficient implementation)
5 ------------------------------------------------------------------------
6
7 -- Vectors of a specified maximum length.
8
9 {-# OPTIONS --with-K --safe #-}
10
11 module Data.Star.BoundedVec where
12
13 import Data.Maybe.Base as Maybe using (Maybe; map)
14 open import Data.Star.Nat using (ℕ; suc; length)
15 open import Data.Star.Decoration using (decoration)
16 open import Data.Star.Pointer
17 using (Any; this; that; Pointer; step; done; init)
18 open import Data.Star.List using (List)
19 open import Data.Unit.Base using (⊤; tt)
20 open import Function.Base using (const)
21 open import Relation.Binary.Core using (_=[_]⇒_)
22 open import Relation.Binary.Consequences using (map-NonEmpty)
23 open import Relation.Binary.Construct.Closure.ReflexiveTransitive
24 using (gmap; ε; _◅_)
25
26 ------------------------------------------------------------------------
27 -- The type
28
29 -- Finite sets decorated with elements (note the use of suc).
30
31 BoundedVec : Set → ℕ → Set
32 BoundedVec a n = Any (λ _ → a) (λ _ → ⊤) (suc n)
33
34 [] : ∀ {a n} → BoundedVec a n
35 [] = this tt
36
37 infixr 5 _∷_
38
39 _∷_ : ∀ {a n} → a → BoundedVec a n → BoundedVec a (suc n)
40 _∷_ = that
41
42 ------------------------------------------------------------------------
43 -- Increasing the bound
44
45 -- Note that this operation is linear in the length of the list.
46
47 ↑ : ∀ {a n} → BoundedVec a n → BoundedVec a (suc n)
48 ↑ {a} = gmap inc lift
49 where
50 inc = Maybe.map (map-NonEmpty suc)
51
52 lift : Pointer (λ _ → a) (λ _ → ⊤) =[ inc ]⇒
53 Pointer (λ _ → a) (λ _ → ⊤)
54 lift (step x) = step x
55 lift (done _) = done _
56
57 ------------------------------------------------------------------------
58 -- Conversions
59
60 fromList : ∀ {a} → (xs : List a) → BoundedVec a (length xs)
61 fromList ε = []
62 fromList (x ◅ xs) = x ∷ fromList xs
63
64 toList : ∀ {a n} → BoundedVec a n → List a
65 toList xs = gmap (const tt) decoration (init xs)