-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector.hs
201 lines (158 loc) · 5.09 KB
/
Vector.hs
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
{-# LANGUAGE GADTs, TypeOperators, TypeFamilies, UndecidableInstances #-}
module Data.Vec
( Vec(..)
, Fin(..)
, head
, tail
, last
, init
, length
, map
, append
, concat
, interleave
, interleave'
, reverse
, transpose
, zipWith
, zip
, (!)
, (!?)
, take
, drop
) where
import Control.Applicative
import Data.Foldable hiding (concat)
import Data.Traversable
import Prelude hiding (foldr,head,tail,last,init,length,map,concat,
reverse,zip,zipWith,take,drop)
-- Type-level natural numbers with addition and multiplication
data Z
data S n
type OneT = S Z
type TwoT = S OneT
type family a :+: b
type instance Z :+: n = n
type instance S m :+: n = S (m :+: n)
type family x :*: y
type instance Z :*: n = Z
type instance S m :*: n = n :+: (m :*: n)
class IsNat n where
units :: Vec n ()
instance IsNat Z where
units = Nil
instance IsNat n => IsNat (S n) where
units = () :< units
infixr 5 :<
data Vec n a where
Nil :: Vec Z a
(:<) :: a -> Vec n a -> Vec (S n) a
data Fin m n where
FZero :: Fin Z (S n)
FSucc :: Fin m n -> Fin (S m) (S n)
instance Eq a => Eq (Vec n a) where
Nil == Nil = True
(x :< xs) == (y :< ys) = x == y && xs == ys
instance Functor (Vec n) where
fmap _ Nil = Nil
fmap f (x :< xs) = f x :< fmap f xs
instance Foldable (Vec n) where
foldr _ y Nil = y
foldr f y (x :< xs) = x `f` foldr f y xs
instance IsNat n => Applicative (Vec n) where
pure = pureV
(<*>) = applyV
instance IsNat n => Monad (Vec n) where
return = pureV
x >>= f = joinV (f <$> x)
instance Traversable (Vec n) where
traverse _ Nil = pure Nil
traverse f (x :< xs) = (:<) <$> f x <*> traverse f xs
instance Show a => Show (Vec n a) where
showsPrec _ Nil = showString "Nil"
showsPrec p (x :< xs) = showParen (p > consPrecedence) $
showsPrec (consPrecedence + 1) x .
showString " :< " .
showsPrec consPrecedence xs
where consPrecedence = 5
-- Used by the Foldable instance.
pureV :: IsNat n => a -> Vec n a
pureV x = const x <$> units
applyV :: Vec n (a -> b) -> Vec n a -> Vec n b
Nil `applyV` Nil = Nil
(f :< fs) `applyV` (x :< xs) = f x :< (fs `applyV` xs)
-- For the Monad instance.
joinV :: Vec n (Vec n a) -> Vec n a
joinV Nil = Nil
joinV (x :< xs) = head x :< joinV (tail <$> xs)
-- Exported functions.
-- This is a total function :-)
head :: Vec (S n) a -> a
head (x :< _) = x
-- Same here.
tail :: Vec (S n) a -> Vec n a
tail (_ :< xs) = xs
last :: Vec (S n) a -> a
last (x :< Nil) = x
last (_ :< xs@(_ :< _)) = last xs
init :: Vec (S n) a -> Vec n a
init (_ :< Nil) = Nil
init (x :< xs@(_ :< _)) = x :< init xs
length :: Vec n a -> Int
length = foldl' (const . (+1)) 0
map :: (a -> b) -> Vec n a -> Vec n b
map = fmap
append :: Vec m a -> Vec n a -> Vec (m :+: n) a
append Nil ys = ys
append (x :< xs) ys = x :< append xs ys
-- The :!: type operator is nearly the same as :+: except that the m and n
-- type parameters are flipped in the recursive case of the type family
-- instance, just like the parameters are flipped in the recursive case of
-- the interleave function. I'm forced to define another type family matching
-- this recursion scheme rather than just using :+: because apparently, there
-- is no way to tell GHC that :+: is indeed commutative.
type family m :!: n
type instance Z :!: n = n
type instance S m :!: n = S (n :!: m) -- We flip m and n
interleave :: Vec m a -> Vec n a -> Vec (m :!: n) a
interleave Nil ys = ys
interleave (x :< xs) ys = x :< interleave ys xs
-- A third way to define addition at the type-level, which matches the way
-- we recurse in a reverse function using an accumulating parameter.
type family m :^: n
type instance Z :^: n = n
type instance S m :^: n = m :^: S n
reverse :: Vec n a -> Vec (n :^: Z) a
reverse as = go as Nil
where go :: Vec m a -> Vec n a -> Vec (m :^: n) a
go Nil ys = ys
go (x :< xs) ys = go xs (x :< ys)
-- Interleave two vectors of the same length.
interleave' :: Vec n a -> Vec n a -> Vec (n :*: TwoT) a
interleave' Nil Nil = Nil
interleave' (x :< xs) (y :< ys) = x :< y :< interleave' xs ys
transpose :: IsNat n => Vec m (Vec n a) -> Vec n (Vec m a)
transpose = sequenceA
concat :: Vec m (Vec n a) -> Vec (m :*: n) a
concat Nil = Nil
concat (xs :< xss) = append xs (concat xss)
zipWith :: IsNat n => (a -> b -> c) -> Vec n a -> Vec n b -> Vec n c
zipWith f xs ys = f <$> xs <*> ys
zip :: IsNat n => Vec n a -> Vec n b -> Vec n (a,b)
zip = zipWith (,)
(!) :: Vec n a -> Fin m n -> a
(x :< _) ! FZero = x
(_ :< xs) ! FSucc n = xs ! n
(!?) :: Vec n a -> Int -> Maybe a
Nil !? _ = Nothing
(x :< _) !? 0 = Just x
(_ :< xs) !? n = xs !? (n - 1)
take :: Fin m (S n) -> Vec n a -> Vec m a
take FZero _ = Nil
take (FSucc n) (x :< xs) = x :< take n xs
type family m :-: n
type instance m :-: Z = m
type instance S m :-: S n = m :-: n
drop :: Fin m (S n) -> Vec n a -> Vec (n :-: m) a
drop FZero xs = xs
drop (FSucc n) (_ :< xs) = drop n xs