15
17
infixr 5 _∷_ _∷ʳ_ _⁺++⁺_ _++⁺_ _⁺++_
17
data List⁺ (A : Set) : Set where
19
data List⁺ {a} (A : Set a) : Set a where
18
20
[_] : (x : A) → List⁺ A
19
21
_∷_ : (x : A) (xs : List⁺ A) → List⁺ A
21
length_-1 : ∀ {A} → List⁺ A → ℕ
23
length_-1 : ∀ {a} {A : Set a} → List⁺ A → ℕ
22
24
length [ x ] -1 = 0
23
25
length x ∷ xs -1 = 1 + length xs -1
25
27
------------------------------------------------------------------------
28
fromVec : ∀ {n A} → Vec A (suc n) → List⁺ A
30
fromVec : ∀ {n a} {A : Set a} → Vec A (suc n) → List⁺ A
29
31
fromVec {zero} (x ∷ _) = [ x ]
30
32
fromVec {suc n} (x ∷ xs) = x ∷ fromVec xs
32
toVec : ∀ {A} (xs : List⁺ A) → Vec A (suc (length xs -1))
34
toVec : ∀ {a} {A : Set a} (xs : List⁺ A) → Vec A (suc (length xs -1))
33
35
toVec [ x ] = Vec.[_] x
34
36
toVec (x ∷ xs) = x ∷ toVec xs
38
lift : ∀ {a b} {A : Set a} {B : Set b} →
37
39
(∀ {m} → Vec A (suc m) → ∃ λ n → Vec B (suc n)) →
39
41
lift f xs = fromVec (proj₂ (f (toVec xs)))
41
fromList : ∀ {A} → A → List A → List⁺ A
43
fromList : ∀ {a} {A : Set a} → A → List A → List⁺ A
42
44
fromList x xs = fromVec (Vec.fromList (x ∷ xs))
44
toList : ∀ {A} → List⁺ A → List A
45
toList = Vec.toList ∘ toVec
46
toList : ∀ {a} {A : Set a} → List⁺ A → List A
47
toList {a} = Vec.toList {a} ∘ toVec
47
49
------------------------------------------------------------------------
48
50
-- Other operations
50
head : ∀ {A} → List⁺ A → A
51
head = Vec.head ∘ toVec
53
tail : ∀ {A} → List⁺ A → List A
54
tail = Vec.toList ∘ Vec.tail ∘ toVec
56
map : ∀ {A B} → (A → B) → List⁺ A → List⁺ B
52
head : ∀ {a} {A : Set a} → List⁺ A → A
53
head {a} = Vec.head {a} ∘ toVec
55
tail : ∀ {a} {A : Set a} → List⁺ A → List A
56
tail {a} = Vec.toList {a} ∘ Vec.tail {a} ∘ toVec
58
map : ∀ {a b} {A : Set a} {B : Set b} → (A → B) → List⁺ A → List⁺ B
57
59
map f = lift (λ xs → (, Vec.map f xs))
59
61
-- Right fold. Note that s is only applied to the last element (see
60
62
-- the examples below).
62
foldr : {A B : Set} → (A → B → B) → (A → B) → List⁺ A → B
64
foldr : ∀ {a b} {A : Set a} {B : Set b} →
65
(A → B → B) → (A → B) → List⁺ A → B
63
66
foldr c s [ x ] = s x
64
67
foldr c s (x ∷ xs) = c x (foldr c s xs)
66
69
-- Left fold. Note that s is only applied to the first element (see
67
70
-- the examples below).
69
foldl : {A B : Set} → (B → A → B) → (A → B) → List⁺ A → B
72
foldl : ∀ {a b} {A : Set a} {B : Set b} →
73
(B → A → B) → (A → B) → List⁺ A → B
70
74
foldl c s [ x ] = s x
71
75
foldl c s (x ∷ xs) = foldl c (c (s x)) xs
73
77
-- Append (several variants).
75
_⁺++⁺_ : ∀ {A} → List⁺ A → List⁺ A → List⁺ A
79
_⁺++⁺_ : ∀ {a} {A : Set a} → List⁺ A → List⁺ A → List⁺ A
76
80
xs ⁺++⁺ ys = foldr _∷_ (λ x → x ∷ ys) xs
78
_⁺++_ : ∀ {A} → List⁺ A → List A → List⁺ A
82
_⁺++_ : ∀ {a} {A : Set a} → List⁺ A → List A → List⁺ A
79
83
xs ⁺++ ys = foldr _∷_ (λ x → fromList x ys) xs
81
_++⁺_ : ∀ {A} → List A → List⁺ A → List⁺ A
85
_++⁺_ : ∀ {a} {A : Set a} → List A → List⁺ A → List⁺ A
82
86
xs ++⁺ ys = List.foldr _∷_ ys xs
84
concat : ∀ {A} → List⁺ (List⁺ A) → List⁺ A
88
concat : ∀ {a} {A : Set a} → List⁺ (List⁺ A) → List⁺ A
86
90
concat (xs ∷ xss) = xs ⁺++⁺ concat xss
88
monad : RawMonad List⁺
92
monad : ∀ {f} → RawMonad (List⁺ {a = f})
91
95
; _>>=_ = λ xs f → concat (map f xs)
94
reverse : ∀ {A} → List⁺ A → List⁺ A
98
reverse : ∀ {a} {A : Set a} → List⁺ A → List⁺ A
95
99
reverse = lift (,_ ∘′ Vec.reverse)
99
_∷ʳ_ : ∀ {A} → List⁺ A → A → List⁺ A
103
_∷ʳ_ : ∀ {a} {A : Set a} → List⁺ A → A → List⁺ A
100
104
xs ∷ʳ x = foldr _∷_ (λ y → y ∷ [ x ]) xs
102
106
-- A snoc-view of non-empty lists.
106
data SnocView {A} : List⁺ A → Set where
110
data SnocView {a} {A : Set a} : List⁺ A → Set a where
107
111
[_] : (x : A) → SnocView [ x ]
108
112
_∷ʳ′_ : (xs : List⁺ A) (x : A) → SnocView (xs ∷ʳ x)
110
snocView : ∀ {A} (xs : List⁺ A) → SnocView xs
114
snocView : ∀ {a} {A : Set a} (xs : List⁺ A) → SnocView xs
111
115
snocView [ x ] = [ x ]
112
116
snocView (x ∷ xs) with snocView xs
113
117
snocView (x ∷ .([ y ])) | [ y ] = [ x ] ∷ʳ′ y