Debian Codegraph @

doc/README/Data/Container/Indexed/VectorExample.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Example showing how to define an indexed container
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --with-K --safe --guardedness #-}
8
9 module README.Data.Container.Indexed.VectorExample where
10
11 open import Data.Unit
12 open import Data.Empty
13 open import Data.Nat.Base
14 open import Data.Product.Base using (_,_)
15 open import Function.Base using (_∋_)
16 open import Data.W.Indexed
17 open import Data.Container.Indexed
18 open import Data.Container.Indexed.WithK
19
20 module _ {a} (A : Set a) where
21
22 ------------------------------------------------------------------------
23 -- Vector as an indexed container
24
25 -- An indexed container is defined by three things:
26 -- 1. Commands the user can emit
27 -- 2. Responses the indexed container returns to these commands
28 -- 3. Update of the index based on the command and the response issued.
29
30 -- For a vector, commands are constructors, responses are the number
31 -- of subvectors (0 if the vector is empty, 1 otherwise) and the
32 -- update corresponds to setting the size of the tail (if it exists).
33 -- We can formalize these ideas like so:
34
35 -- Depending on the size of the vector, we may have reached the end
36 -- already (nil) or we may specify what the head should be (cons).
37 -- This is the type of commands.
38
39 data VecC : ℕ → Set a where
40 nil : VecC zero
41 cons : ∀ n → A → VecC (suc n)
42
43 Vec : Container ℕ ℕ a _
44 Command Vec = VecC
45
46 -- We then treat each command independently, specifying both the response and the
47 -- next index based on that response.
48
49 -- In the nil case, the response is the empty type: there won't be any tail. As
50 -- a consequence, the next index won't be needed (and we can rely on the fact the
51 -- user will never be able to call it).
52
53 Response Vec nil = ⊥
54 next Vec nil = λ ()
55
56 -- In the cons case, the response is the unit type: there is exactly one tail. The
57 -- next index is the predecessor of the current one. It is handily handed over to
58 -- use by `cons`.
59
60 -- cons
61 Response Vec (cons n a) = ⊤
62 next Vec (cons n a) = λ _ → n
63
64 -- Finally we can define the type of Vector as the least fixed point of Vec.
65
66 Vector : ℕ → Set a
67 Vector = μ Vec
68
69 module _ {a} {A : Set a} where
70
71 -- We can recover the usual constructors by using `sup` to enter the fixpoint
72 -- and then using the appropriate pairing of a command & a handler for the
73 -- response.
74
75 -- For [], the response is ⊥ which makes it easy to conclude.
76
77 [] : Vector A 0
78 [] = sup (nil , λ ())
79
80 -- For _∷_, the response is ⊤ so we need to pass a tail. We give the one we took
81 -- as an argument.
82
83 infixr 3 _∷_
84 _∷_ : ∀ {n} → A → Vector A n → Vector A (suc n)
85 x ∷ xs = sup (cons _ x , λ _ → xs)
86
87 -- We can now use these constructors to build up vectors:
88
89 1⋯3 : Vector ℕ 3
90 1⋯3 = 1 ∷ 2 ∷ 3 ∷ []
91
92
93
94
95
96 -- Horrible thing to check the definition of _∈_ is not buggy.
97 -- Not sure whether we can say anything interesting about it in the case of Vector...
98
99 open import Relation.Binary.HeterogeneousEquality
100
101 _ : _∈_ {C = Vec ℕ} {X = Vector ℕ} 1⋯3 (⟦ Vec ℕ ⟧ (Vector ℕ) 4 ∋ cons _ 0 , λ _ → 1⋯3)
102 _ = _ , refl