A SAFE and idiomatic (no corner cutting or strange `stuff`

under the hood)
implementation of an immutable `array`

in Haskell aiming for an idiomatic ```
low
memory footprint
```

by storing `chunks`

of `256`

elements in a `fixed vector`

(`data VF256 a = VF … , i00 :: a, … , iFF :: a }`

) by sharing a single
constructor. A side-effect of this approach, is that we achieve `O(log₂₅₆ n)`

asymptotic time complexity for most operations.

Futhermore, in order to avoid the usage of the following pattern ```
data Present a
= Yes a | No
```

for when an element is present as it will add a constructor
(overhead) for each element. This is achieved with the usage of `⊥`

(`bottom`

)
combined with Haskells `lazy`

nature as well as a `bitmap`

, where each of the
`256`

elements presence are stored as a single `bit`

.

**Remark**: Please see the `References`

below for Simon Marlows answer
at `Stack Overflow`

with regard of `memory footprint of Haskell data types`

.

### Basics

#### Step-by-step insert example with an Array Log₈ with height equal to 0

#### Step-by-step insert example with an Array Log₄ with height greater than 0

### Code Snippet

#### src/Data/Array/Log256.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495

--------------------------------------------------------------------------------
--
-- Data.Array.Log256, (c) 2020 SPISE MISU ApS
--
--------------------------------------------------------------------------------
{-# LANGUAGE Safe #-}
--------------------------------------------------------------------------------
module Data.Array.Log256
( AL256
--
, height
, length
--
, create
, update
, exists
, lookup
, remove
--
, pprint
, tuples
, tolist
--
, amount
, sparse
, defrag
, sliver
, expand
, reduce
)
where
--------------------------------------------------------------------------------
import Prelude hiding
( length
, lookup
)
import Data.Bits
( Bits
, shiftL
, shiftR
, (.&.)
)
import Data.List
( dropWhile
, foldl'
, takeWhile
)
import Data.Ratio
( Rational
, (%)
)
import Data.Word
( Word8
)
import Data.Vector.Fixed256
( VF256
)
import qualified Data.Vector.Fixed256 as VF
--------------------------------------------------------------------------------
data AL256 a
= A !Integer !(Tree a)
data Tree a
= L !(VF256 a)
| N !(VF256 (Tree a))
instance Show a => Show (AL256 a) where
show (A _ t) = show t
instance Show a => Show (Tree a) where
show = show . helper []
--------------------------------------------------------------------------------
instance Foldable Tree where
foldMap f (L vfa) = foldMap f vfa
foldMap f (N vft) = foldMap (foldMap f) vft
instance Foldable AL256 where
foldMap f (A _ t) = foldMap f t
instance Functor Tree where
fmap f (L vfa) = L $ fmap f vfa
fmap f (N vft) = N $ fmap (fmap f) vft
instance Functor AL256 where
fmap f (A l t) = A l $ fmap f t
--------------------------------------------------------------------------------
length
:: AL256 a
-> Integer
height
:: AL256 a
-> Integer
{-# INLINE length #-}
{-# INLINE height #-}
length (A l _) =
l
height (A l _) =
log256 $ l - 1
--------------------------------------------------------------------------------
create
:: Integer
-> AL256 a
update
:: Integer
-> a
-> AL256 a
-> AL256 a
exists
:: Integer
-> AL256 a
-> Bool
lookup
:: Integer
-> AL256 a
-> a
remove
:: Integer
-> AL256 a
-> AL256 a
{-# INLINE create #-}
{-# INLINE update #-}
{-# INLINE exists #-}
{-# INLINE lookup #-}
{-# INLINE remove #-}
{-| Length must be greater than 0. Devs responsability to call inside bounds
-}
create l
| l > aux = A l $ N VF.create
| otherwise = assert (l > 0) msg A l $ L VF.create
where
aux = logmod + 1
msg = "The length of an array must be greater than 0"
{-| Length can be read in O(1). Devs responsability to call inside bounds
-}
update i a (A l t) =
assert (i < l) msg
A l $ aux (loglvl l) t
where
aux _ (L fv) =
L $ k `seq` VF.update k a fv
where
k = i2wrd8 i .&. logmod
aux j (N fv) =
N $ v `seq` VF.update k v fv
where
k = i2wrd8 $ i .>. j .&. logmod
n = j - lograt
c =
if VF.exists k fv
then VF.lookup k fv
else branch n
v = aux n c
msg =
"Index " ++ show i ++ " is out of bounds (length " ++ show l ++ ")"
{-| Length can be read in O(1). Devs responsability to call inside bounds
-}
exists i (A l t) =
assert (i < l) msg
aux (loglvl l) t
where
aux _ (L fv) =
VF.exists k fv
where
k = i2wrd8 i .&. logmod
aux j (N fv) =
e && b
where
k = i2wrd8 $ i .>. j .&. logmod
e = VF.exists k fv
n = j - lograt
b = aux n $ VF.lookup k fv
msg =
"Index " ++ show i ++ " is out of bounds (length " ++ show l ++ ")"
{-| * Length can be read in O(1). Devs responsability to call inside bounds
* Existence can be checked in O(log₂₅₆ n). Devs responsability to check
-}
lookup i al@(A l t) =
assert (i < l) msg0
assert (exists i al) msg1
aux (loglvl l) t
where
aux _ (L fv) =
VF.lookup k fv
where
k = i2wrd8 $ i .&. logmod
aux j (N fv) =
aux n c
where
k = i2wrd8 $ i .>. j .&. logmod
n = j - lograt
c = VF.lookup k fv
msg0 =
"Index " ++ show i ++ " is out of bounds (length " ++ show l ++ ")"
msg1 =
"Index " ++ show i ++ " doesn't contain any element"
{-| * Length can be read in O(1). Devs responsability to call inside bounds
* Existence can be checked in O(log₂₅₆ n). Devs responsability to check
-}
remove i al@(A l t) =
assert (i < l) msg0
assert (exists i al) msg1
A l $ aux (loglvl l) t
where
aux _ (L fv) =
L r
where
k = i2wrd8 $ i .&. logmod
r = VF.remove k fv
aux j (N fv) =
N a
where
k = i2wrd8 $ i .>. j .&. logmod
n = j - lograt
c = VF.lookup k fv
b = aux n c
a =
if vacant b
then VF.remove k fv
else VF.update k b fv
msg0 =
"Index " ++ show i ++ " is out of bounds (length " ++ show l ++ ")"
msg1 =
"Index " ++ show i ++ " doesn't contain any element"
--------------------------------------------------------------------------------
pprint
:: AL256 a
-> String
tuples
:: AL256 a
-> [(Integer, a)]
tolist
:: AL256 a
-> [a]
{-# INLINE pprint #-}
{-# INLINE tuples #-}
{-# INLINE tolist #-}
pprint (A l t) =
"├ " ++ show l ++ aux 0 t
where
rep i = concat $ replicate i "│ "
bmp f v =
map f $
VF.bitmap v
aux i (L fv) =
"\n" ++ rep i ++ "├ " ++ bmp (\x -> if x then '■' else '▭') fv
aux i (N fv) =
"\n" ++ rep i ++ "├ " ++ bmp (\x -> if x then '▣' else '□') fv ++ sub
where
sub = concat $ map (aux (i+1) . snd) $ VF.tuples fv
tuples (A _ t) =
helper [] t
tolist (A _ t) =
map snd $ helper [] t
--------------------------------------------------------------------------------
amount :: AL256 a -> Integer
sparse :: AL256 a -> Rational
defrag :: AL256 a -> AL256 a
sliver
:: Integer
-> Integer
-> AL256 a
-> AL256 a
expand :: Integer -> AL256 a -> AL256 a
reduce :: Integer -> AL256 a -> AL256 a
{-# INLINE amount #-}
{-# INLINE sparse #-}
{-# INLINE defrag #-}
{-# INLINE sliver #-}
{-# INLINE expand #-}
{-# INLINE reduce #-}
amount (A _ t) =
aux t
where
aux (L fv) = VF.amount fv
aux (N fv) =
if 0 == VF.amount fv
then 0
else foldl' (+) 0 $ map (aux . snd) $ VF.tuples fv
sparse (A 0 _) = 0
sparse al@(A l _) = amount al % l
defrag al =
-- Reduces the degree of fragmentation. Check sparsity first
foldl (\a (i,x) -> update i x a) (create n) $
zip [ 0 .. ] $
tolist al
where
n = amount al
{-| * Length can be read in O(1). Devs responsability to call inside bounds
* Index + offset must be: i+o<=l. Devs responsability to call inside bounds
-}
sliver i o al@(A l _) =
assert (i + o <= l) msg
foldl (\a (j,x) -> update (j-i) x a) (create o) $
takeWhile ((i + o >) . fst) $
dropWhile ((i >) . fst) $
tuples al
where
msg =
"Index i+o (" ++ show (i+o) ++
") must be less or equal to l (" ++ show l ++
")"
{-| Length can be read in O(1). Devs responsability to call inside bounds
-}
expand n (A l t) =
assert (n > l) msg
A n $ aux $ log256 n
where
msg =
"New length " ++ show n ++ " must be greater than previous " ++ show l
aux i
| i > lvl = N $ VF.update 0 (aux $ i - 1) VF.create
| otherwise = t
lvl = log256 l
{-| Length can be read in O(1). Devs responsability to call inside bounds
-}
reduce n al@(A l _) =
assert (n < l) msg
sliver 0 n al
where
msg =
"New length " ++ show n ++ " must be less than previous " ++ show l
--------------------------------------------------------------------------------
-- HELPERS
helper
:: [Integer]
-> Tree a
-> [(Integer, a)]
{-# INLINE helper #-}
helper ns (L fv) =
map (\(i,a) -> (fromIntegral i + (aux lograt ns), a)) $
VF.tuples fv
where
aux _ [ ] = 0
aux i (x:xs) = (1 .<. i) * x + (aux (i + lograt) xs)
helper ns (N fv) =
concat $
map (\(i,x) -> helper (fromIntegral i:ns) x) $
VF.tuples fv
--------------------------------------------------------------------------------
assert
:: Bool
-> String
-> a
-> a
{-# INLINE assert #-}
assert True ___ a = a
assert False msg _ = error msg
--------------------------------------------------------------------------------
branch
:: Int
-> Tree a
vacant
:: Tree a
-> Bool
{-# INLINE branch #-}
{-# INLINE vacant #-}
branch 0 = L VF.create
branch _ = N VF.create
vacant (L fv) = VF.amount fv == 0
vacant (N fv) = VF.amount fv == 0
--------------------------------------------------------------------------------
(.<.)
:: Bits a
=> a
-> Int
-> a
(.>.)
:: Bits a
=> a
-> Int
-> a
{-# INLINE (.<.) #-}
{-# INLINE (.>.) #-}
(.<.) = shiftL
(.>.) = shiftR
--------------------------------------------------------------------------------
lograt
:: Int
logmod
:: Num a
=> a
loglvl
:: Integer
-> Int
i2wrd8
:: Integral a
=> a
-> Word8
{-# INLINE lograt #-}
{-# INLINE logmod #-}
{-# INLINE loglvl #-}
{-# INLINE i2wrd8 #-}
lograt = 0x08
logmod = 0xFF
loglvl = ((*) lograt) . fromIntegral . log256 . (flip (-) 1)
i2wrd8 = fromIntegral
--------------------------------------------------------------------------------
-- http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious
--
-- log_{2^b} n ≈ m (floor)
log02b
::
( Bits a
, Num a
)
=> Int
-> a
-> a
log256
::
( Bits a
, Num a
)
=> a
-> a
{-# INLINE log02b #-}
{-# INLINE log256 #-}
log02b b n =
aux 0 (n .>. b)
where
aux acc 0 = acc
aux acc x = aux (acc + 1) (x .>. b)
log256 =
log02b lograt

#### src/Data/Vector/Fixed256.hs

#### exe/ArrayLog.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

--------------------------------------------------------------------------------
--
-- ArrayLog, (c) 2020 SPISE MISU ApS
--
--------------------------------------------------------------------------------
{-# LANGUAGE Safe #-}
--------------------------------------------------------------------------------
module Main (main) where
--------------------------------------------------------------------------------
import Prelude hiding
( length
, lookup
)
import Data.Array.Log256
--------------------------------------------------------------------------------
main :: IO ()
main =
(putStrLn "# Before fmap") >>
(putStrLn . show . tuples $ a) >>
(putStrLn . pprint $ a) >>
(putStrLn "") >>
(putStrLn "# After fmap") >>
(putStrLn . show . tuples $ b) >>
(putStrLn . pprint $ b) >>
(putStrLn "") >>
(putStrLn "# Sparse (create)") >>
(putStrLn . show $ sparse a) >>
(putStrLn "") >>
(putStrLn "# Sparse (sliver)") >>
(putStrLn . show $ sparse s) >>
(putStrLn . pprint $ s) >>
(putStrLn "") >>
(putStrLn "# Sparse (expand)") >>
(putStrLn . show $ sparse e) >>
(putStrLn . pprint $ e) >>
(putStrLn "") >>
(putStrLn "# Sparse (defrag)") >>
(putStrLn . show $ sparse d) >>
(putStrLn . pprint $ d)
where
n = 300
m = 150
--
a = foldl (\ acc x -> update x x acc) (create n) $ take m [ 0 .. ]
b = fmap (*2) a
--
i = 050
o = 100
s = sliver i o b
--
e = expand 65536 s
--
d = defrag e

#### arraylog.cabal

#### stack.yaml

#### build.bash (real: 30m7.777s, user: 29m52.651s and sys: 0m15.487s)

### Code Output:

### References:

- Stack Overflow:
- GitLab - Glasgow Haskell Compiler:
- GitLab - SPISE MISU ApS: