-
Notifications
You must be signed in to change notification settings - Fork 15
/
Internal.hs
676 lines (483 loc) · 13.9 KB
/
Internal.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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
{- ORMOLU_DISABLE -}
-- Options passed to GHC
{-# OPTIONS_GHC -O2 #-}
-- Options passed to Haddock
{-# OPTIONS_HADDOCK hide #-}
-----------------------------------------------------------------------------
{-# LANGUAGE CPP #-}
{-
We use __GLASGOW_HASKELL__ everywhere, so, rather than check if it's defined in
multiple places, we assert an error here if it is not. Since the rest of the
package depends on this module ('Data.DList.Internal'), we don't perform the
same check everywhere else.
-}
#if !defined(__GLASGOW_HASKELL__)
#error "Your compiler is not GHC. Let us know if dlist can be made to work on it."
#endif
-- For the IsList and IsString instances
{-# LANGUAGE TypeFamilies #-}
-- CPP: GHC >= 7.8 for pattern synonyms, Safe Haskell, view patterns
#if __GLASGOW_HASKELL__ >= 708
{- ORMOLU_ENABLE -}
{-# LANGUAGE PatternSynonyms #-}
{-
The 'Data.DList.Internal' module exports 'UnsafeDList' and 'unsafeApplyDList',
which allow breaking the invariant of the 'DList' newtype. Therefore, we
explicitly mark 'Data.DList.Internal' as unsafe.
-}
{-# LANGUAGE Unsafe #-}
{-# LANGUAGE ViewPatterns #-}
{- ORMOLU_DISABLE -}
#endif
-----------------------------------------------------------------------------
{-|
Module: Data.DList.Internal
Copyright: © 2006-2009 Don Stewart, 2013-2020 Sean Leather
License: BSD-3-Clause
Maintainer: sean.leather@gmail.com
Stability: stable
This module includes everything related to 'DList' and is not exposed to users
of the @dlist@ package.
-}
{- ORMOLU_ENABLE -}
module Data.DList.Internal where
-----------------------------------------------------------------------------
import qualified Control.Applicative as Applicative
import Control.DeepSeq (NFData (..))
import qualified Control.Monad as Monad
-- CPP: base >= 4.9 for MonadFail
-- CPP: base >= 4.13 for MonadFail exported from Control.Monad
#if MIN_VERSION_base(4,9,0) && !MIN_VERSION_base(4,13,0)
import qualified Control.Monad.Fail as Monad
#endif
import qualified Data.Foldable as Foldable
import Data.Function (on)
import qualified Data.List as List
import qualified Data.Monoid as Monoid
-- CPP: base >= 4.9 for Semigroup
#if MIN_VERSION_base(4,9,0)
import qualified Data.Semigroup as Semigroup
#endif
import Data.String (IsString (..))
import qualified Data.Traversable as Traversable
-- CPP: GHC >= 7.8 for IsList
#if __GLASGOW_HASKELL__ >= 708
import qualified GHC.Exts as Exts
#endif
import qualified Text.Read as Read
import Prelude hiding (concat, foldr, head, map, replicate, tail)
-----------------------------------------------------------------------------
{- ORMOLU_DISABLE -}
{-|
A difference list is an abstraction representing a list that
supports \(\mathcal{O}\)(@1@) 'append' and 'snoc' operations, making it
useful for replacing frequent applications of '++' such as logging and pretty
printing (esp. if those uses of '++' are left-nested).
-}
{- ORMOLU_ENABLE -}
newtype DList a = UnsafeDList {unsafeApplyDList :: [a] -> [a]}
{- ORMOLU_DISABLE -}
{-|
__@fromList xs@__ is a 'DList' representing the list __@xs@__.
@fromList@ obeys the laws:
@
'toList' . __fromList__ = 'id'
__fromList__ . 'toList' = 'id'
@
This function is implemented with '++'. Repeated uses of @fromList@ are just as
inefficient as repeated uses of '++'. If you find yourself doing some form of
the following (possibly indirectly), you may not be taking advantage of the
'DList' representation and library:
@
__fromList__ . f . 'toList'
@
More likely, you will convert from a list, perform some operation on the
'DList', and convert back to a list:
@
'toList' . g . __fromList__
@
-}
{- ORMOLU_ENABLE -}
{-# INLINE fromList #-}
fromList :: [a] -> DList a
fromList = UnsafeDList . (++)
{- ORMOLU_DISABLE -}
{-|
__@toList xs@__ is the list represented by __@xs@__.
@toList@ obeys the laws:
@
__toList__ . 'fromList' = 'id'
'fromList' . __toList__ = 'id'
@
Evaluating @toList xs@ may “collapse” the chain of function composition
underlying many 'DList' functions ('append' in particular) used to construct
@xs@. This may affect any efficiency you achieved due to laziness in the
construction.
-}
{- ORMOLU_ENABLE -}
{-# INLINE toList #-}
toList :: DList a -> [a]
toList = ($ []) . unsafeApplyDList
-- CPP: GHC >= 7.8 for pattern synonyms
#if __GLASGOW_HASKELL__ >= 708
-- CPP: GHC >= 7.10 for pattern synonym signatures
{- ORMOLU_DISABLE -}
{-|
A unidirectional pattern synonym for 'empty'. This is implemented with 'toList'.
-}
{- ORMOLU_ENABLE -}
#if __GLASGOW_HASKELL__ >= 710
pattern Nil :: DList a
#endif
pattern Nil <- (toList -> [])
{- ORMOLU_DISABLE -}
{-|
A unidirectional pattern synonym for 'cons'. This is implemented with 'toList'.
-}
{- ORMOLU_ENABLE -}
#if __GLASGOW_HASKELL__ >= 710
pattern Cons :: a -> [a] -> DList a
#endif
pattern Cons x xs <- (toList -> x : xs)
#endif
{- ORMOLU_DISABLE -}
{-|
__@apply xs ys@__ is the list represented by the __@xs@__ after appending
__@ys@__ to it.
\(\mathcal{O}\)(@1@).
@apply@ obeys the law:
@
__apply__ xs ys = 'toList' xs '++' ys
@
-}
{- ORMOLU_ENABLE -}
{-# INLINE apply #-}
apply :: DList a -> [a] -> [a]
apply = unsafeApplyDList
{- ORMOLU_DISABLE -}
{-|
__@empty@__ is a 'DList' with no elements.
@empty@ obeys the law:
@
'toList' __empty__ = []
@
-}
{- ORMOLU_ENABLE -}
{-# INLINE empty #-}
empty :: DList a
empty = UnsafeDList id
{- ORMOLU_DISABLE -}
{-|
__@singleton x@__ is a 'DList' with the single element __@x@__.
@singleton@ obeys the law:
@
'toList' (__singleton__ x) = [x]
@
-}
{- ORMOLU_ENABLE -}
{-# INLINE singleton #-}
singleton :: a -> DList a
singleton = UnsafeDList . (:)
{- ORMOLU_DISABLE -}
{-|
__@cons x xs@__ is a 'DList' with the 'head' __@x@__ and the 'tail' __@xs@__.
\(\mathcal{O}\)(@1@).
@cons@ obeys the law:
@
'toList' (__cons__ x xs) = x : 'toList' xs
@
-}
{- ORMOLU_ENABLE -}
infixr 9 `cons`
{-# INLINE cons #-}
cons :: a -> DList a -> DList a
cons x xs = UnsafeDList $ (x :) . unsafeApplyDList xs
infixl 9 `snoc`
{- ORMOLU_DISABLE -}
{-|
__@snoc xs x@__ is a 'DList' with the initial 'DList' __@xs@__ and the last
element __@x@__.
\(\mathcal{O}\)(@1@).
@snoc@ obeys the law:
@
'toList' (__snoc__ xs x) = 'toList' xs '++' [x]
@
-}
{- ORMOLU_ENABLE -}
{-# INLINE snoc #-}
snoc :: DList a -> a -> DList a
snoc xs x = UnsafeDList $ unsafeApplyDList xs . (x :)
{- ORMOLU_DISABLE -}
{-|
__@append xs ys@__ is a 'DList' obtained from the concatenation of the elements
of __@xs@__ and __@ys@__.
\(\mathcal{O}\)(@1@).
@append@ obeys the law:
@
'toList' (__append__ xs ys) = 'toList' xs '++' 'toList' ys
@
-}
{- ORMOLU_ENABLE -}
{-# INLINE append #-}
append :: DList a -> DList a -> DList a
append xs ys = UnsafeDList $ unsafeApplyDList xs . unsafeApplyDList ys
{- ORMOLU_DISABLE -}
{-|
__@concat xss@__ is a 'DList' representing the concatenation of all 'DList's in
the list __@xss@__.
\(\mathcal{O}\)(@'length' xss@).
@concat@ obeys the law:
@
'toList' (__concat__ xss) = 'List.concat' ('List.map' 'toList' xss)
@
-}
{- ORMOLU_ENABLE -}
{-# INLINE concat #-}
concat :: [DList a] -> DList a
concat = List.foldr append empty
{- ORMOLU_DISABLE -}
{-|
__@replicate n x@__ is a 'DList' of length __@n@__ with __@x@__ as the value of
every element.
\(\mathcal{O}\)(@n@).
@replicate@ obeys the law:
@
'toList' (__replicate__ n x) = 'List.replicate' n x
@
-}
{- ORMOLU_ENABLE -}
{-# INLINE replicate #-}
replicate :: Int -> a -> DList a
replicate n x = UnsafeDList $ \xs ->
let go m
| m <= 0 = xs
| otherwise = x : go (m -1)
in go n
{- ORMOLU_DISABLE -}
{-|
__@head xs@__ is the first element of __@xs@__. If @xs@ is empty, an 'error' is
raised.
\(\mathcal{O}\)(@1@).
@head@ obeys the law:
@
__head__ xs = 'List.head' ('toList' xs)
@
-}
{- ORMOLU_ENABLE -}
{-# INLINE head #-}
head :: DList a -> a
head xs = case toList xs of
x : _ -> x
[] -> error "Data.DList.head: empty DList"
{- ORMOLU_DISABLE -}
{-|
__@tail xs@__ is a list of the elements in __@xs@__ excluding the first element.
If @xs@ is empty, an 'error' is raised.
\(\mathcal{O}\)(@'length' ('toList' xs)@).
@tail@ obeys the law:
@
__tail__ xs = 'List.tail' ('toList' xs)
@
-}
{- ORMOLU_ENABLE -}
{-# INLINE tail #-}
tail :: DList a -> [a]
tail xs = case toList xs of
_ : ys -> ys
[] -> error "Data.DList.tail: empty DList"
{- ORMOLU_DISABLE -}
{-|
__@unfoldr f z@__ is the 'DList' constructed from the recursive application of
__@f@__. The recursion starts with the seed value __@z@__ and ends when, for
some @z' : b@, @f z' == 'Nothing'@.
\(\mathcal{O}\)(@'length' ('List.unfoldr' f z)@).
@unfoldr@ obeys the law:
@
'toList' (__unfoldr__ f z) = 'List.unfoldr' f z
@
-}
{- ORMOLU_ENABLE -}
unfoldr :: (b -> Maybe (a, b)) -> b -> DList a
unfoldr f z =
case f z of
Nothing -> empty
Just (x, z') -> cons x $ unfoldr f z'
{- ORMOLU_DISABLE -}
{-|
__@foldr f z xs@__ is the right-fold of __@f@__ over __@xs@__.
\(\mathcal{O}\)(@'length' ('toList' xs)@).
@foldr@ obeys the law:
@
__foldr__ f z xs = 'List.foldr' f z ('toList' xs)
@
-}
{- ORMOLU_ENABLE -}
{-# INLINE foldr #-}
foldr :: (a -> b -> b) -> b -> DList a -> b
foldr f z = List.foldr f z . toList
{- ORMOLU_DISABLE -}
{-|
__@map f xs@__ is the 'DList' obtained by applying __@f@__ to each element of
__@xs@__.
\(\mathcal{O}\)(@'length' ('toList' xs)@).
@map@ obeys the law:
@
'toList' (__map__ f xs) = 'List.map' f ('toList' xs)
@
-}
{- ORMOLU_ENABLE -}
{-# INLINE map #-}
map :: (a -> b) -> DList a -> DList b
map f = foldr (cons . f) empty
{- ORMOLU_DISABLE -}
{-|
__@intercalate xs xss@__ is the concatenation of __@xss@__ after the insertion
of __@xs@__ between every pair of
elements.
\(\mathcal{O}\)(@'length' xss@).
@intercalate@ obeys the law:
@
'toList' (__intercalate__ xs xss) = 'List.intercalate' ('toList' xs) ('map' 'toList' xss)
@
-}
{- ORMOLU_ENABLE -}
{-# INLINE intercalate #-}
intercalate :: DList a -> [DList a] -> DList a
intercalate sep = concat . List.intersperse sep
instance Eq a => Eq (DList a) where
(==) = (==) `on` toList
instance Ord a => Ord (DList a) where
compare = compare `on` toList
-- The 'Read' and 'Show' instances were adapted from 'Data.Sequence'.
instance Read a => Read (DList a) where
readPrec = Read.parens $
Read.prec 10 $ do
Read.Ident "fromList" <- Read.lexP
dl <- Read.readPrec
return (fromList dl)
readListPrec = Read.readListPrecDefault
instance Show a => Show (DList a) where
showsPrec p dl =
showParen (p > 10) $
showString "fromList " . shows (toList dl)
instance Monoid.Monoid (DList a) where
{-# INLINE mempty #-}
mempty = empty
-- CPP: base >= 4.11 for Semigroup as a superclass of Monoid
#if MIN_VERSION_base(4,11,0)
#else
{-# INLINE mappend #-}
-- CPP: base >= 4.9 for Semigroup in base
#if MIN_VERSION_base(4,9,0)
-- Canonical definition
mappend = (Semigroup.<>)
#else
mappend = append
#endif
#endif
instance Functor DList where
{-# INLINE fmap #-}
fmap = map
instance Applicative.Applicative DList where
{-# INLINE pure #-}
pure = singleton
{-# INLINE (<*>) #-}
(<*>) = Monad.ap
instance Applicative.Alternative DList where
{-# INLINE empty #-}
empty = empty
{-# INLINE (<|>) #-}
(<|>) = append
instance Monad DList where
{-# INLINE (>>=) #-}
m >>= k =
-- = concat (toList (fmap k m))
-- = (concat . toList . fromList . List.map k . toList) m
-- = concat . List.map k . toList $ m
-- = List.foldr append empty . List.map k . toList $ m
-- = List.foldr (append . k) empty . toList $ m
foldr (append . k) empty m
{-# INLINE return #-}
return = Applicative.pure
-- CPP: base < 4.13 for fail in Monad
#if !MIN_VERSION_base(4,13,0)
{-# INLINE fail #-}
fail _ = empty
#endif
-- CPP: base >= 4.9 for MonadFail
#if MIN_VERSION_base(4,9,0)
instance Monad.MonadFail DList where
{-# INLINE fail #-}
fail _ = empty
#endif
instance Monad.MonadPlus DList where
{-# INLINE mzero #-}
mzero = empty
{-# INLINE mplus #-}
mplus = append
instance Foldable.Foldable DList where
{-# INLINE fold #-}
fold = Monoid.mconcat . toList
{-# INLINE foldMap #-}
foldMap f = Foldable.foldMap f . toList
{-# INLINE foldr #-}
foldr f x = List.foldr f x . toList
{-# INLINE foldl #-}
foldl f x = List.foldl f x . toList
{-# INLINE foldr1 #-}
foldr1 f = List.foldr1 f . toList
{-# INLINE foldl1 #-}
foldl1 f = List.foldl1 f . toList
-- CPP: GHC >= 7.6 for foldl', foldr' in Foldable
#if __GLASGOW_HASKELL__ >= 706
{-# INLINE foldl' #-}
foldl' f x = List.foldl' f x . toList
{-# INLINE foldr' #-}
foldr' f x = Foldable.foldr' f x . toList
#endif
-- CPP: base >= 4.8 for toList in Foldable
#if MIN_VERSION_base(4,8,0)
{-# INLINE toList #-}
toList = Data.DList.Internal.toList
#endif
instance Traversable.Traversable DList where
{-# INLINE traverse #-}
traverse f = foldr cons_f (Applicative.pure empty)
where
cons_f x = Applicative.liftA2 cons (f x)
instance NFData a => NFData (DList a) where
{-# INLINE rnf #-}
rnf = rnf . toList
{-
The 'IsString' instance is _not_ a flexible instance to allow certain uses of
overloaded strings. See tests/OverloadedStrings.hs for an example and
https://gitlab.haskell.org/ghc/ghc/-/commit/b225b234a6b11e42fef433dcd5d2a38bb4b466bf
for the same change made to the IsString instance for lists.
-}
instance a ~ Char => IsString (DList a) where
{-# INLINE fromString #-}
fromString = fromList
-- CPP: GHC >= 7.8 for IsList
#if __GLASGOW_HASKELL__ >= 708
instance Exts.IsList (DList a) where
type Item (DList a) = a
{-# INLINE fromList #-}
fromList = fromList
{-# INLINE toList #-}
toList = toList
#endif
{-
We use 'compare n 0' in the definition of 'Semigroup.stimes' since the same
expression is used in 'Semigroup.stimesMonoid' and we should get a lazy
advantage. However, we prefer the error to be sourced here instead of
'Semigroup.stimesMonoid'.
-}
-- CPP: base >= 4.9 for Semigroup
#if MIN_VERSION_base(4,9,0)
instance Semigroup.Semigroup (DList a) where
{-# INLINE (<>) #-}
(<>) = append
stimes n = case compare n 0 of
LT -> error "Data.DList.stimes: negative multiplier"
_ -> Semigroup.stimesMonoid n
#endif