-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathCommon.purs
More file actions
284 lines (265 loc) Β· 6.93 KB
/
Common.purs
File metadata and controls
284 lines (265 loc) Β· 6.93 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
module Data.FastVect.Common
( Replicate
, Empty
, Sparse
, Singleton
, Append
, Drop
, Take
, SplitAt
, Modify
, Set
, Index
, IndexModulo
, Head
, IndexM
, IndexModuloM
, HeadM
, Cons
, Snoc
, Zero
, NegOne
, One
, class IsVect
, term
, toInt
) where
import Data.Maybe (Maybe)
import Data.Reflectable (class Reflectable, reflectType)
import Data.TraversableWithIndex (class TraversableWithIndex)
import Prim.Int (class Add, class Compare)
import Prim.Ordering (GT, LT)
import Type.Proxy (Proxy(..))
type Zero = 0
type One = 1
type NegOne = -1
class TraversableWithIndex Int f <= IsVect f
term β· forall (i β· Int). Proxy i
term = Proxy
toInt β· forall (@len β· Int). Reflectable len Int β Int
toInt = reflectType (Proxy :: _ len)
-- -- | Create a `Vect` by replicating `len` times the given element
-- -- |
-- -- | ```
-- -- | vect β· Vect 300 String
-- -- | vect = replicate (term β· _ 300) "a"
-- -- | ```
type Replicate vect len elem =
Compare len NegOne GT
β Reflectable len Int
β elem
β vect len elem
-- -- | Creates the empty `Vect`.
-- -- |
-- -- | ```
-- -- | vect β· Vect 0 String
-- -- | vect = empty
-- -- | ```
type Empty :: forall k1 k2. (Int -> k1 -> k2) -> k1 -> k2
type Empty vect elem = vect Zero elem
-- -- | Creates the sparse `Vect`.
-- -- |
-- -- | ```
-- -- | vect β· Vect 40 String
-- -- | vect = sparse
-- -- | ```
type Sparse :: forall k1 k2 k3. (k1 -> k2 -> k3) -> k1 -> k2 -> k3
type Sparse vect n elem = vect n elem
-- -- | Create a `Vect` of one element.
-- -- |
-- -- | ```
-- -- | vect β· Vect 1 String
-- -- | vect = singleton "a"
-- -- | ```
type Singleton vect elem = elem β vect One elem
-- -- | Append two `Vect`s.
-- -- |
-- -- | ```
-- -- | as β· Vect 300 String
-- -- | as = replicate (term β· _ 300) "a"
-- -- |
-- -- | bs β· Vect 200 String
-- -- | bs = replicate (term β· _ 200) "b"
-- -- |
-- -- | cs β· Vect 500 String
-- -- | cs = append as bs
-- -- | ```
type Append :: forall k. (Int -> k -> Type) -> Int -> Int -> Int -> k -> Type
type Append vect m n m_plus_n elem =
Add m n m_plus_n
β Compare m NegOne GT
β Reflectable m Int
β Compare n NegOne GT
β vect m elem
β vect n elem
β vect m_plus_n elem
-- -- | Safely drop `m` elements from a `Vect`.
-- -- | Will result in a compile-time error if you are trying to drop more elements than exist in the vector.
-- -- |
-- -- | ```
-- -- | vect β· Vect 300 String
-- -- | vect = replicate (term β· _ 300) "a"
-- -- |
-- -- | newVect β· Vect 200 String
-- -- | newVect = drop (term β· _ 100) vect
-- -- | ```
type Drop :: forall k. (Int -> k -> Type) -> Int -> Int -> Int -> k -> Type
type Drop vect m n m_plus_n elem =
Add m n m_plus_n
β Reflectable m Int
β Compare m NegOne GT
β Compare n NegOne GT
β vect m_plus_n elem
β vect n elem
-- -- | Safely take `m` elements from a `Vect`.
-- -- | Will result in a compile-time error if you are trying to take more elements than exist in the vector.
-- -- |
-- -- | ```
-- -- | vect β· Vect 300 String
-- -- | vect = replicate (term β· _ 300) "a"
-- -- |
-- -- | newVect β· Vect 100 String
-- -- | newVect = take (term β· _ 100) vect
-- -- | ```
type Take :: forall k. (Int -> k -> Type) -> Int -> Int -> Int -> k -> Type
type Take vect m n m_plus_n elem =
Add m n m_plus_n
β Reflectable m Int
β Compare m NegOne GT
β Compare n NegOne GT
β vect m_plus_n elem
β vect m elem
-- -- | Safely modify element `m` from a `Vect`.
-- -- |
-- -- | ```
-- -- | vect β· Vect 300 String
-- -- | vect = replicate (term β· _ 300) "a"
-- -- |
-- -- | newVect β· Vect 100 String
-- -- | newVect = modify (term β· _ 100) (append "b") vect
-- -- | ```
type Modify vect m n elem =
Reflectable m Int
β Compare m NegOne GT
β Compare n NegOne GT
β Compare m n LT
β (elem β elem)
β vect n elem
β vect n elem
-- -- | Safely set element `m` from a `Vect`.
-- -- |
-- -- | ```
-- -- | vect β· Vect 300 String
-- -- | vect = replicate (term β· _ 300) "a"
-- -- |
-- -- | newVect β· Vect 100 String
-- -- | newVect = modify (term β· _ 100) "b" vect
-- -- | `
type Set vect m n elem =
Reflectable m Int
β Compare m NegOne GT
β Compare n NegOne GT
β Compare m n LT
β elem
β vect n elem
β vect n elem
-- -- | Split the `Vect` into two sub vectors `before` and `after`, where before contains up to `m` elements.
-- -- |
-- -- | ```
-- -- | vect β· Vect 10 String
-- -- | vect = replicate (term β· _ 10) "a"
-- -- |
-- -- | split β·
-- -- | { after β· Vect 7 String
-- -- | , before β· Vect 3 String
-- -- | }
-- -- | split = splitAt (term β· _ 3) vect
-- -- | ```
type SplitAt :: forall k. (Int -> k -> Type) -> Int -> Int -> Int -> k -> Type
type SplitAt vect m n m_plus_n elem =
Add m n m_plus_n
β Reflectable m Int
β Compare m NegOne GT
β Compare n NegOne GT
β vect m_plus_n elem
β { before β· vect m elem, after β· vect n elem }
-- -- | Safely access the `n`-th modulo m element of a `Vect`.
-- -- |
-- -- | ```
-- -- | vect β· Vect 300 String
-- -- | vect = replicate (term β· _ 300) "a"
-- -- |
-- -- | elem β· String
-- -- | elem = indexModulo 5352523 vect
-- -- | ```
type IndexModulo vect m elem =
Compare m Zero GT
β Reflectable m Int
β Int
β vect m elem
β elem
type IndexModuloM vect m elem =
Compare m Zero GT
β Reflectable m Int
β Int
β vect m elem
β Maybe elem
-- -- | Safely access the `i`-th element of a `Vect`.
-- -- |
-- -- | ```
-- -- | vect β· Vect 300 String
-- -- | vect = replicate (term β· _ 300) "a"
-- -- |
-- -- | elem β· String
-- -- | elem = index (term β· _ 299) vect
-- -- | ```
type Index vect m m_minus_one i n elem =
Compare m_minus_one NegOne GT
β Add One m_minus_one m
β Compare n NegOne GT
β Add i n m_minus_one
β Compare i NegOne GT
β Reflectable i Int
β vect m elem
β elem
type IndexM vect m m_minus_one i n elem =
Compare m_minus_one NegOne GT
β Add One m_minus_one m
β Compare n NegOne GT
β Add i n m_minus_one
β Compare i NegOne GT
β Reflectable i Int
β vect m elem
β Maybe elem
-- -- | Safely access the head of a `Vect`.
-- -- |
-- -- | ```
-- -- | vect β· Vect 300 String
-- -- | vect = replicate (term β· _ 300) "a"
-- -- |
-- -- | elem β· String
-- -- | elem = head vect
-- -- | ```
type Head vect m elem =
Compare m Zero GT
β vect m elem
β elem
type HeadM vect m elem =
Compare m Zero GT
β vect m elem
β Maybe elem
-- -- | Attaches an element to the front of the `Vect`, creating a new `Vect` with size incremented.
type Cons vect len len_plus_1 elem =
Add One len len_plus_1
β Compare len NegOne GT
β elem
β vect len elem
β vect len_plus_1 elem
-- -- | Attaches an element to the end of the `Vect`, creating a new `Vect` with size incremented.
type Snoc vect len len_plus_1 elem =
Add One len len_plus_1
β Reflectable len Int
β Compare len NegOne GT
β vect len elem
β elem
β vect len_plus_1 elem