-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathParser.hs
More file actions
886 lines (762 loc) · 30.3 KB
/
Parser.hs
File metadata and controls
886 lines (762 loc) · 30.3 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
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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
#undef FUSION_CHECK
#ifdef FUSION_CHECK
{-# OPTIONS_GHC -ddump-simpl -ddump-to-file -dsuppress-all #-}
#endif
-- |
-- Module : Streamly.Benchmark.Data.Parser
-- Copyright : (c) 2020 Composewell Technologies
--
-- License : BSD-3-Clause
-- Maintainer : streamly@composewell.com
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# OPTIONS_GHC -Wno-orphans #-}
module Main
(
main
) where
import Control.Applicative ((<|>))
import Control.DeepSeq (NFData(..))
import Data.Function ((&))
import Data.Functor (($>))
import Data.Monoid (Sum(..))
import GHC.Magic (inline)
import GHC.Magic (noinline)
import System.IO (Handle)
import System.Random (randomRIO)
import Streamly.Internal.Data.Fold (Fold(..))
import Streamly.Internal.Data.Parser
(ParseError(..), Parser(..), Initial(..), Step(..))
import Streamly.Internal.Data.Stream (Stream)
import Prelude hiding
(any, all, take, sequence, sequence_, sequenceA, takeWhile, dropWhile, span)
import qualified Control.Applicative as AP
import qualified Data.Foldable as F
import qualified Data.Traversable as TR
import qualified Streamly.FileSystem.Handle as Handle
import qualified Streamly.Internal.Data.Array as Array
import qualified Streamly.Internal.Data.Fold as Fold
import qualified Streamly.Internal.Data.Parser as PR
import qualified Streamly.Data.Stream as Stream
import qualified Streamly.Internal.Data.Producer as Producer
import qualified Streamly.Internal.Data.Stream as Stream
import Test.Tasty.Bench hiding (env)
import Streamly.Benchmark.Common
import Streamly.Benchmark.Common.Handle
-------------------------------------------------------------------------------
-- Utilities
-------------------------------------------------------------------------------
-- XXX these can be moved to the common module
{-# INLINE sourceUnfoldrM #-}
sourceUnfoldrM :: Monad m => Int -> Int -> Stream m Int
sourceUnfoldrM value n = Stream.unfoldrM step n
where
step cnt =
if cnt > n + value
then return Nothing
else return (Just (cnt, cnt + 1))
-- | Generates something like this: { { \{ \{ } }. The stream consists of
-- three parts, the first part is contains a sequence of `{`. The second part
-- contains a sequence pf escaped values `\{`. The third part contains a
-- sequence of `}`.
{-# INLINE sourceEscapedFrames #-}
sourceEscapedFrames ::
Monad m
=> Int
-> Int
-> Stream m Char
sourceEscapedFrames value n = Stream.unfoldrM step n
where
bs = '\\'
cbOpen = '{'
cbClose = '}'
value1 = value `div` 4
step cnt
| cnt > 4 * value1 = return Nothing
| cnt <= value1 = return $ Just (cbOpen, cnt + 1)
| cnt > 3 * value1 = return $ Just (cbClose, cnt + 1)
| otherwise =
return
$ Just
$ if (cnt - value1) `mod` 2 == 1
then (bs, cnt + 1)
else (cbOpen, cnt + 1)
{-# INLINE benchIOSrc #-}
benchIOSrc
:: NFData b
=> (Int -> Int -> Stream IO a)
-> Int
-> String
-> (Stream IO a -> IO b)
-> Benchmark
benchIOSrc src value name f =
bench name $ nfIO $ randomRIO (1,1) >>= f . src value
-- | Takes a fold method, and uses it with a default source.
{-# INLINE benchIOSink #-}
benchIOSink
:: NFData b
=> Int -> String -> (Stream IO Int -> IO b) -> Benchmark
benchIOSink value name f =
bench name $ nfIO $ randomRIO (1,1) >>= f . sourceUnfoldrM value
-------------------------------------------------------------------------------
-- Parsers
-------------------------------------------------------------------------------
{-# INLINE takeBetween #-}
takeBetween :: Monad m => Int -> Stream m a -> m (Either ParseError ())
takeBetween value = Stream.parse (PR.takeBetween 0 value Fold.drain)
{-# INLINE takeEQ #-}
takeEQ :: Monad m => Int -> Stream m a -> m (Either ParseError ())
takeEQ value = Stream.parse (PR.takeEQ value Fold.drain)
{-# INLINE takeGE #-}
takeGE :: Monad m => Int -> Stream m a -> m (Either ParseError ())
takeGE value = Stream.parse (PR.takeGE value Fold.drain)
{-# INLINE dropWhile #-}
dropWhile :: Monad m => Int -> Stream m Int -> m (Either ParseError ())
dropWhile value = Stream.parse (PR.dropWhile (<= value))
{-# INLINE takeBeginBy #-}
takeBeginBy :: Monad m => Int -> Stream m Int -> m (Either ParseError ())
takeBeginBy value stream = do
let stream2 = value `Stream.cons` stream
Stream.parse (PR.takeBeginBy (== value) Fold.drain) stream2
takeFramedByEsc_ :: Monad m => Int -> Stream m Char -> m (Either ParseError ())
takeFramedByEsc_ _ = Stream.parse parser
where
isEsc = (== '\\')
isBegin = (== '{')
isEnd = (== '}')
parser = PR.takeFramedByEsc_ isEsc isBegin isEnd Fold.drain
{-# INLINE listEqBy #-}
listEqBy :: Int -> Stream IO Int -> IO (Either ParseError [Int])
listEqBy len = Stream.parse (PR.listEqBy (==) [1 .. len])
{-# INLINE streamEqBy #-}
streamEqBy :: Int -> Stream IO Int -> IO (Either ParseError ())
streamEqBy len = Stream.parse (PR.streamEqBy (==) (Stream.enumerateFromTo 1 len))
{-# INLINE takeWhile #-}
takeWhile :: Monad m => Int -> Stream m Int -> m (Either ParseError ())
takeWhile value = Stream.parse (PR.takeWhile (<= value) Fold.drain)
takeWhileP :: Monad m => Int -> Stream m Int -> m (Either ParseError ())
takeWhileP value =
Stream.parse (PR.takeWhileP (<= value) (PR.takeWhile (<= value - 1) Fold.drain))
{-# INLINE takeP #-}
takeP :: Monad m => Int -> Stream m a -> m (Either ParseError ())
takeP value = Stream.parse (PR.takeP value (PR.fromFold Fold.drain))
{-# INLINE groupBy #-}
groupBy :: Monad m => Stream m Int -> m (Either ParseError ())
groupBy = Stream.parse (PR.groupBy (<=) Fold.drain)
{-# INLINE groupByRolling #-}
groupByRolling :: Monad m => Stream m Int -> m (Either ParseError ())
groupByRolling = Stream.parse (PR.groupByRolling (<=) Fold.drain)
{-# INLINE wordBy #-}
wordBy :: Monad m => Int -> Stream m Int -> m (Either ParseError ())
wordBy value = Stream.parse (PR.wordBy (>= value) Fold.drain)
{-# INLINE sepByWords #-}
sepByWords :: Monad m => Stream m Int -> m (Either ParseError ())
sepByWords = Stream.parse (wrds even Fold.drain)
where
wrds p = PR.sepBy (PR.takeWhile (not . p) Fold.drain) (PR.dropWhile p)
{-# INLINE sepByAllWords #-}
sepByAllWords :: Monad m => Stream m Int -> m (Either ParseError ())
sepByAllWords = Stream.parse (wrds even Fold.drain)
where
wrds p = PR.sepByAll (PR.takeWhile (not . p) Fold.drain) (PR.dropWhile p)
-- Returning a list to compare with the sepBy1 in ParserK
{-# INLINE sepBy1 #-}
sepBy1 :: Monad m => Stream m Int -> m (Either ParseError [Int])
sepBy1 xs = do
Stream.parse (PR.sepBy1 (PR.satisfy odd) (PR.satisfy even) Fold.toList) xs
{-# INLINE sepByWords1 #-}
sepByWords1 :: Monad m => Stream m Int -> m (Either ParseError ())
sepByWords1 = Stream.parse (wrds even Fold.drain)
where
wrds p = PR.sepBy1 (PR.takeWhile (not . p) Fold.drain) (PR.dropWhile p)
{-# INLINE deintercalate #-}
deintercalate :: Monad m => Int -> Stream m Int -> m (Either ParseError ())
deintercalate _ = Stream.parse (partition even)
where
partition p =
PR.deintercalate
(PR.takeWhile (not . p) Fold.sum) (PR.takeWhile p Fold.sum) Fold.drain
{-# INLINE deintercalate1 #-}
deintercalate1 :: Monad m => Int -> Stream m Int -> m (Either ParseError ())
deintercalate1 _ = Stream.parse (partition even)
where
partition p =
PR.deintercalate1
(PR.takeWhile (not . p) Fold.sum) (PR.takeWhile p Fold.sum) Fold.drain
{-# INLINE deintercalateAll #-}
deintercalateAll :: Monad m => Int -> Stream m Int -> m (Either ParseError ())
deintercalateAll _ = Stream.parse (partition even)
where
partition p =
PR.deintercalateAll
(PR.takeWhile (not . p) Fold.sum) (PR.takeWhile p Fold.sum) Fold.drain
{-# INLINE manyWordByEven #-}
manyWordByEven :: Monad m => Stream m Int -> m (Either ParseError ())
manyWordByEven = Stream.parse (PR.many (PR.wordBy even Fold.drain) Fold.drain)
{-# INLINE many #-}
many :: Monad m => Stream m Int -> m (Either ParseError Int)
many = Stream.parse (PR.many (PR.satisfy (> 0)) Fold.length)
{-# INLINE manyAlt #-}
manyAlt :: Monad m => Stream m Int -> m Int
manyAlt xs = do
x <- Stream.parse (AP.many (PR.satisfy (> 0))) xs
return $ Prelude.length x
{-# INLINE some #-}
some :: Monad m => Stream m Int -> m (Either ParseError Int)
some = Stream.parse (PR.some (PR.satisfy (> 0)) Fold.length)
{-# INLINE someAlt #-}
someAlt :: Monad m => Stream m Int -> m Int
someAlt xs = do
x <- Stream.parse (AP.some (PR.satisfy (> 0))) xs
return $ Prelude.length x
{-# INLINE manyTill #-}
manyTill :: Monad m => Int -> Stream m Int -> m (Either ParseError Int)
manyTill value =
Stream.parse (PR.manyTill (PR.satisfy (> 0)) (PR.satisfy (== value)) Fold.length)
{-# INLINE splitAp2 #-}
splitAp2 :: Monad m
=> Int -> Stream m Int -> m (Either ParseError ((), ()))
splitAp2 value =
Stream.parse
((,)
<$> PR.dropWhile (<= (value `div` 2))
<*> PR.dropWhile (<= value)
)
{-# INLINE splitAp4 #-}
splitAp4 :: Monad m
=> Int -> Stream m Int -> m (Either ParseError ())
splitAp4 value =
Stream.parse
( (\() () () () -> ())
<$> PR.dropWhile (<= (value * 1 `div` 4))
<*> PR.dropWhile (<= (value * 2 `div` 4))
<*> PR.dropWhile (<= (value * 3 `div` 4))
<*> PR.dropWhile (<= value)
)
{-# INLINE splitAp8 #-}
splitAp8 :: Monad m
=> Int -> Stream m Int -> m (Either ParseError ())
splitAp8 value =
Stream.parse
( (\() () () () () () () () -> ())
<$> PR.dropWhile (<= (value * 1 `div` 8))
<*> PR.dropWhile (<= (value * 2 `div` 8))
<*> PR.dropWhile (<= (value * 3 `div` 8))
<*> PR.dropWhile (<= (value * 4 `div` 8))
<*> PR.dropWhile (<= (value * 5 `div` 8))
<*> PR.dropWhile (<= (value * 6 `div` 8))
<*> PR.dropWhile (<= (value * 7 `div` 8))
<*> PR.dropWhile (<= value)
)
{-# INLINE splitApBefore #-}
splitApBefore :: Monad m
=> Int -> Stream m Int -> m (Either ParseError ())
splitApBefore value =
Stream.parse
( PR.dropWhile (<= (value `div` 2))
*> PR.dropWhile (<= value)
)
{-# INLINE splitApAfter #-}
splitApAfter :: Monad m
=> Int -> Stream m Int -> m (Either ParseError ())
splitApAfter value =
Stream.parse
( PR.dropWhile (<= (value `div` 2))
<* PR.dropWhile (<= value)
)
{-# INLINE splitWith2 #-}
splitWith2 :: Monad m
=> Int -> Stream m Int -> m (Either ParseError ((), ()))
splitWith2 value =
Stream.parse
(PR.splitWith (,)
(PR.dropWhile (<= (value `div` 2)))
(PR.dropWhile (<= value))
)
{-# INLINE split_ #-}
split_ :: Monad m
=> Int -> Stream m Int -> m (Either ParseError ())
split_ value =
Stream.parse
(PR.split_
(PR.dropWhile (<= (value `div` 2)))
(PR.dropWhile (<= value))
)
-- XXX dropWhile with applicative does not fuse
-- PR.dropWhile (<= (value * 1 `div` 4)) *> PR.die "alt"
{-# INLINE takeWhileFail #-}
takeWhileFail :: Monad m => (a -> Bool) -> Fold m a b -> Parser a m b
takeWhileFail predicate (Fold fstep finitial _ ffinal) =
Parser step initial extract
where
initial = do
res <- finitial
return $ case res of
Fold.Partial s -> IPartial s
Fold.Done b -> IDone b
step s a =
if predicate a
then do
fres <- fstep s a
return
$ case fres of
Fold.Partial s1 -> SPartial 1 s1
Fold.Done b -> SDone 1 b
else return $ Error "fail"
extract s = fmap (SDone 0) (ffinal s)
{-# INLINE alt2 #-}
alt2 :: Monad m
=> Int -> Stream m Int -> m (Either ParseError ())
alt2 value =
Stream.parse
(PR.alt
(takeWhileFail (<= (value `div` 2)) Fold.drain)
(PR.dropWhile (<= value))
)
{-# INLINE alt4 #-}
alt4 :: Monad m
=> Int -> Stream m Int -> m (Either ParseError ())
alt4 value =
Stream.parse
( takeWhileFail (<= (value * 1 `div` 4)) Fold.drain
<|> takeWhileFail (<= (value * 2 `div` 4)) Fold.drain
<|> takeWhileFail (<= (value * 3 `div` 4)) Fold.drain
<|> PR.dropWhile (<= value)
)
{-# INLINE alt8 #-}
alt8 :: Monad m
=> Int -> Stream m Int -> m (Either ParseError ())
alt8 value =
Stream.parse
( takeWhileFail (<= (value * 1 `div` 8)) Fold.drain
<|> takeWhileFail (<= (value * 2 `div` 8)) Fold.drain
<|> takeWhileFail (<= (value * 3 `div` 8)) Fold.drain
<|> takeWhileFail (<= (value * 4 `div` 8)) Fold.drain
<|> takeWhileFail (<= (value * 5 `div` 8)) Fold.drain
<|> takeWhileFail (<= (value * 6 `div` 8)) Fold.drain
<|> takeWhileFail (<= (value * 7 `div` 8)) Fold.drain
<|> PR.dropWhile (<= value)
)
{-# INLINE alt16 #-}
alt16 :: Monad m
=> Int -> Stream m Int -> m (Either ParseError ())
alt16 value =
Stream.parse
( takeWhileFail (<= (value * 1 `div` 16)) Fold.drain
<|> takeWhileFail (<= (value * 2 `div` 16)) Fold.drain
<|> takeWhileFail (<= (value * 3 `div` 16)) Fold.drain
<|> takeWhileFail (<= (value * 4 `div` 16)) Fold.drain
<|> takeWhileFail (<= (value * 5 `div` 16)) Fold.drain
<|> takeWhileFail (<= (value * 6 `div` 16)) Fold.drain
<|> takeWhileFail (<= (value * 8 `div` 16)) Fold.drain
<|> takeWhileFail (<= (value * 9 `div` 16)) Fold.drain
<|> takeWhileFail (<= (value * 10 `div` 16)) Fold.drain
<|> takeWhileFail (<= (value * 11 `div` 16)) Fold.drain
<|> takeWhileFail (<= (value * 12 `div` 16)) Fold.drain
<|> takeWhileFail (<= (value * 13 `div` 16)) Fold.drain
<|> takeWhileFail (<= (value * 14 `div` 16)) Fold.drain
<|> takeWhileFail (<= (value * 15 `div` 16)) Fold.drain
<|> PR.dropWhile (<= value)
)
{-# INLINE altSmall #-}
altSmall :: Monad m
=> Int -> Stream m Int -> m ()
altSmall value =
Stream.fold Fold.drain .
Stream.parseMany
(PR.alt
(PR.satisfy (>= value) *> PR.die "alt")
(PR.satisfy (<= value))
)
{-# INLINE monad #-}
monad :: Monad m
=> Int -> Stream m Int -> m (Either ParseError ())
monad value =
Stream.parse
$ do
PR.dropWhile (<= (value `div` 2))
PR.dropWhile (<= value)
{-# INLINE monad4 #-}
monad4 :: Monad m
=> Int -> Stream m Int -> m (Either ParseError ())
monad4 value =
Stream.parse $ do
PR.dropWhile (<= (value `div` 4))
PR.dropWhile (<= (value `div` 2))
PR.dropWhile (<= (value * 3 `div` 4))
PR.dropWhile (<= value)
{-# INLINE monad8 #-}
monad8 :: Monad m
=> Int -> Stream m Int -> m (Either ParseError ())
monad8 value =
Stream.parse $ do
PR.dropWhile (<= (value * 1 `div` 8))
PR.dropWhile (<= (value * 2 `div` 8))
PR.dropWhile (<= (value * 3 `div` 8))
PR.dropWhile (<= (value * 4 `div` 8))
PR.dropWhile (<= (value * 5 `div` 8))
PR.dropWhile (<= (value * 6 `div` 8))
PR.dropWhile (<= (value * 7 `div` 8))
PR.dropWhile (<= value)
{-# INLINE monad16 #-}
monad16 :: Monad m
=> Int -> Stream m Int -> m (Either ParseError ())
monad16 value =
Stream.parse $ do
PR.dropWhile (<= (value * 1 `div` 16))
PR.dropWhile (<= (value * 2 `div` 16))
PR.dropWhile (<= (value * 3 `div` 16))
PR.dropWhile (<= (value * 4 `div` 16))
PR.dropWhile (<= (value * 5 `div` 16))
PR.dropWhile (<= (value * 6 `div` 16))
PR.dropWhile (<= (value * 7 `div` 16))
PR.dropWhile (<= (value * 8 `div` 16))
PR.dropWhile (<= (value * 9 `div` 16))
PR.dropWhile (<= (value * 10 `div` 16))
PR.dropWhile (<= (value * 11 `div` 16))
PR.dropWhile (<= (value * 12 `div` 16))
PR.dropWhile (<= (value * 13 `div` 16))
PR.dropWhile (<= (value * 14 `div` 16))
PR.dropWhile (<= (value * 15 `div` 16))
PR.dropWhile (<= value)
{-# INLINE takeEndBy_ #-}
takeEndBy_ :: Monad m
=> Int -> Stream m Int -> m (Either ParseError ())
takeEndBy_ value = Stream.parse (PR.takeEndBy_ (>= value) (PR.fromFold Fold.drain))
{-
{-# INLINE teeAllAny #-}
teeAllAny :: Monad m
=> Int -> Stream m Int -> m ((), ())
teeAllAny value =
Stream.parse
(PR.teeWith (,)
(PR.dropWhile (<= value))
(PR.dropWhile (<= value))
)
{-# INLINE teeFstAllAny #-}
teeFstAllAny :: Monad m
=> Int -> Stream m Int -> m ((), ())
teeFstAllAny value =
Stream.parse
(PR.teeWithFst (,)
(PR.dropWhile (<= value))
(PR.dropWhile (<= value))
)
{-# INLINE shortestAllAny #-}
shortestAllAny :: Monad m
=> Int -> Stream m Int -> m (Either ParseError ())
shortestAllAny value =
Stream.parse
(PR.shortest
(PR.dropWhile (<= value))
(PR.dropWhile (<= value))
)
{-# INLINE longestAllAny #-}
longestAllAny :: Monad m
=> Int -> Stream m Int -> m (Either ParseError ())
longestAllAny value =
Stream.parse
(PR.longest
(PR.dropWhile (<= value))
(PR.dropWhile (<= value))
)
-}
-------------------------------------------------------------------------------
-- Spanning
-------------------------------------------------------------------------------
{-# INLINE span #-}
span :: Monad m => Int -> Stream m Int -> m (Either ParseError ((), ()))
span value = Stream.parse (PR.span (<= (value `div` 2)) Fold.drain Fold.drain)
{-# INLINE spanBy #-}
spanBy :: Monad m => Int -> Stream m Int -> m (Either ParseError ((), ()))
spanBy value =
Stream.parse (PR.spanBy (\_ i -> i <= (value `div` 2)) Fold.drain Fold.drain)
{-# INLINE spanByRolling #-}
spanByRolling :: Monad m => Int -> Stream m Int -> m (Either ParseError ((), ()))
spanByRolling value =
Stream.parse (PR.spanByRolling (\_ i -> i <= value `div` 2) Fold.drain Fold.drain)
parseManyChunksOfSum :: Int -> Handle -> IO Int
parseManyChunksOfSum n inh =
Stream.fold Fold.length
$ Stream.parseMany
(PR.fromFold $ Fold.take n Fold.sum)
(Stream.unfold Handle.reader inh)
-------------------------------------------------------------------------------
-- Parsers in which -fspec-constr-recursive=16 is problematic
-------------------------------------------------------------------------------
-- XXX -fspec-constr-recursive=16 makes GHC go beserk when compiling these.
-- We need to fix GHC so that we can have better control over that option or do
-- not have to rely on it.
--
{-# INLINE lookAhead #-}
lookAhead :: Monad m => Int -> Stream m Int -> m (Either ParseError ())
lookAhead value =
Stream.parse (PR.lookAhead (PR.takeWhile (<= value) Fold.drain) $> ())
-- XXX The timing of this increased 3x after the stepify extract changes.
{-# INLINE sequenceA_ #-}
sequenceA_ :: Monad m => Int -> Stream m Int -> m (Either ParseError ())
sequenceA_ value =
Stream.parse (F.sequenceA_ $ replicate value (PR.satisfy (> 0)))
-- quadratic complexity
{-# INLINE sequenceA #-}
sequenceA :: Monad m => Int -> Stream m Int -> m Int
sequenceA value xs = do
x <- Stream.parse (TR.sequenceA (replicate value (PR.satisfy (> 0)))) xs
return $ length x
-- quadratic complexity
{-# INLINE sequence #-}
sequence :: Monad m => Int -> Stream m Int -> m Int
sequence value xs = do
x <- Stream.parse (TR.sequence (replicate value (PR.satisfy (> 0)))) xs
return $ length x
{-# INLINE sequence_ #-}
sequence_ :: Monad m => Int -> Stream m Int -> m (Either ParseError ())
sequence_ value xs =
Stream.parse (foldr f (return ()) (replicate value (PR.takeBetween 0 1 Fold.drain))) xs
where
{-# INLINE f #-}
f m k = m >>= (\_ -> k)
-- choice using the "Alternative" instance with direct style parser type has
-- quadratic performance complexity.
--
{-# INLINE choiceAsum #-}
choiceAsum :: Monad m => Int -> Stream m Int -> m (Either ParseError Int)
choiceAsum value =
Stream.parse (F.asum (replicate value (PR.satisfy (< 0)))
AP.<|> PR.satisfy (> 0))
{-
{-# INLINE choice #-}
choice :: Monad m => Int -> Stream m Int -> m (Either ParseError Int)
choice value =
Stream.parse
(PR.choice (replicate value (PR.satisfy (< 0))) AP.<|> PR.satisfy (> 0))
-}
-------------------------------------------------------------------------------
-- Parsing with unfolds
-------------------------------------------------------------------------------
{-# INLINE parseManyUnfoldArrays #-}
parseManyUnfoldArrays :: Int -> [Array.Array Int] -> IO ()
parseManyUnfoldArrays count arrays = do
let src = Producer.source (Just (Producer.OuterLoop arrays))
let parser = PR.fromFold (Fold.take count Fold.drain)
let readSrc =
Producer.producer
$ Producer.concat Producer.fromList Array.producer
let streamParser =
Producer.simplify (Producer.parseMany parser readSrc)
Stream.fold Fold.drain $ Stream.unfold streamParser src
-------------------------------------------------------------------------------
-- Stream transformation
-------------------------------------------------------------------------------
{-# INLINE parseMany #-}
parseMany :: Monad m => Int -> Stream m Int -> m ()
parseMany n =
Stream.fold Fold.drain
. fmap getSum
. Stream.catRights . Stream.parseMany (PR.fromFold $ Fold.take n Fold.mconcat)
. fmap Sum
{-# INLINE parseManyGroupBy #-}
parseManyGroupBy :: Monad m => (Int -> Int -> Bool) -> Stream m Int -> m ()
parseManyGroupBy cmp =
Stream.fold Fold.drain . Stream.parseMany (PR.groupBy cmp Fold.drain)
{-# INLINE parseManyGroupsRolling #-}
parseManyGroupsRolling :: Monad m => Bool -> Stream m Int -> m ()
parseManyGroupsRolling b =
Stream.fold Fold.drain
. Stream.parseMany (PR.groupByRolling (\_ _ -> b) Fold.drain)
{-# INLINE parseManyGroupsRollingEither #-}
parseManyGroupsRollingEither :: Monad m =>
(Int -> Int -> Bool) -> Int -> m ()
parseManyGroupsRollingEither cmp value = do
sourceUnfoldrM value 1
& Stream.parseMany (PR.groupByRollingEither cmp Fold.drain Fold.drain)
& Stream.fold Fold.drain
{-# INLINE parseManyGroupsRollingEitherAlt #-}
parseManyGroupsRollingEitherAlt :: Monad m =>
(Int -> Int -> Bool) -> Int -> m ()
parseManyGroupsRollingEitherAlt cmp value = do
sourceUnfoldrM value 1
-- Make the input unsorted.
& fmap (\x -> if even x then x + 2 else x)
& Stream.parseMany (PR.groupByRollingEither cmp Fold.drain Fold.drain)
& Stream.fold Fold.drain
{-# INLINE parseIterate #-}
parseIterate :: Monad m => Int -> Stream m Int -> m ()
parseIterate n =
Stream.fold Fold.drain
. fmap getSum
. Stream.catRights
. Stream.parseIterate
(PR.fromFold . Fold.take n . Fold.sconcat)
(Sum 0)
. fmap Sum
{-# INLINE concatSequence #-}
concatSequence :: Monad m => Stream m Int -> m (Either ParseError ())
concatSequence =
Stream.parse $ PR.sequence (Stream.repeat PR.one) Fold.drain
-------------------------------------------------------------------------------
-- Benchmarks
-------------------------------------------------------------------------------
moduleName :: String
moduleName = "Data.Parser"
instance NFData ParseError where
{-# INLINE rnf #-}
rnf (ParseError x) = rnf x
o_1_space_serial :: Int -> [Benchmark]
o_1_space_serial value =
[ benchIOSink value "takeBetween" $ takeBetween value
, benchIOSink value "takeWhile" $ takeWhile value
, benchIOSink value "takeWhileP" $ takeWhileP value
, benchIOSink value "takeP" $ takeP value
, benchIOSink value "dropWhile" $ dropWhile value
, benchIOSink value "takeBeginBy" $ takeBeginBy value
, benchIOSink value "takeEndBy_" $ takeEndBy_ value
, benchIOSink value "groupBy" $ groupBy
, benchIOSink value "groupByRolling" $ groupByRolling
, benchIOSink value "wordBy" $ wordBy value
, benchIOSink value "sepBy (words)" sepByWords
, benchIOSink value "sepByAll (words)" sepByAllWords
, benchIOSink value "sepBy1 (words)" sepByWords1
, benchIOSink value "deintercalate" $ deintercalate value
, benchIOSink value "deintercalate1" $ deintercalate1 value
, benchIOSink value "deintercalateAll" $ deintercalateAll value
-- Applicative and Monad
, benchIOSink value "splitAp2" $ splitAp2 value
, benchIOSink value "splitAp4" $ splitAp4 value
, benchIOSink value "splitAp8" $ splitAp8 value
, benchIOSink value "splitApBefore" $ splitApBefore value
, benchIOSink value "splitApAfter" $ splitApAfter value
, benchIOSink value "splitWith2" $ splitWith2 value
, benchIOSink value "span" $ span value
, benchIOSink value "spanBy" $ spanBy value
, benchIOSink value "spanByRolling" $ spanByRolling value
, benchIOSink value "monad2" $ monad value
, benchIOSink value "monad4" $ monad4 value
, benchIOSink value "monad8" $ monad8 value
-- Alternative
, benchIOSink value "alt2parseMany" $ altSmall value
, benchIOSink value "alt2" $ alt2 value
, benchIOSink value "alt4" $ alt4 value
, benchIOSink value "alt8" $ alt8 value
, benchIOSink value "alt16" $ alt16 value
, benchIOSink value "many" many
, benchIOSink value "many (wordBy even)" $ manyWordByEven
, benchIOSink value "some" some
, benchIOSink value "manyTill" $ manyTill value
, benchIOSink value "parseMany" $ parseMany value
, benchIOSink value "parseMany (take 1)" (parseMany 1)
, benchIOSink value "parseMany (take all)" (parseMany value)
, benchIOSink value "parseMany (groupBy (<))" (parseManyGroupBy (<))
, benchIOSink value "parseMany (groupBy (==))" (parseManyGroupBy (==))
, benchIOSink value "parseMany groupRollingBy (bound groups)"
$ parseManyGroupsRolling False
, benchIOSink value "parseMany groupRollingBy (1 group)"
$ parseManyGroupsRolling True
, bench "parseMany groupRollingByEither (Left)"
$ nfIO $ parseManyGroupsRollingEitherLeft
, bench "parseMany groupRollingByEither (Right)"
$ nfIO $ parseManyGroupsRollingEitherRight
, bench "parseMany groupRollingByEither (Alternating)"
$ nfIO $ parseManyGroupsRollingEitherAlt1
, benchIOSink value "parseIterate (take 1)" (parseIterate 1)
, benchIOSink value "parseIterate (take all)" (parseIterate value)
, benchIOSink value "concatSequence" concatSequence
{-
, benchIOSink value "tee" $ teeAllAny value
, benchIOSink value "teeFst" $ teeFstAllAny value
, benchIOSink value "shortest" $ shortestAllAny value
, benchIOSink value "longest" $ longestAllAny value
-}
, benchIOSink value "streamEqBy" (streamEqBy value)
]
where
{-# NOINLINE parseManyGroupsRollingEitherLeft #-}
parseManyGroupsRollingEitherLeft = parseManyGroupsRollingEither (<) value
{-# NOINLINE parseManyGroupsRollingEitherRight #-}
parseManyGroupsRollingEitherRight = parseManyGroupsRollingEither (>) value
{-# NOINLINE parseManyGroupsRollingEitherAlt1 #-}
parseManyGroupsRollingEitherAlt1 =
parseManyGroupsRollingEitherAlt (>) value
o_1_space_filesystem :: BenchEnv -> [Benchmark]
o_1_space_filesystem env =
[ mkBench ("S.parseMany (Fold.take " ++ show (bigSize env) ++ " Fold.sum)") env
$ \inh _ -> noinline parseManyChunksOfSum (bigSize env) inh
, mkBench "S.parseMany (Fold.take 1 Fold.sum)" env
$ \inh _ -> inline parseManyChunksOfSum 1 inh
]
o_1_space_serial_unfold :: Int -> [Array.Array Int] -> [Benchmark]
o_1_space_serial_unfold bound arrays =
[ bench "parseMany/Unfold/1000 arrays/take all"
$ nfIO $ parseManyUnfoldArrays bound arrays
, bench "parseMany/Unfold/1000 arrays/take 1"
$ nfIO $ parseManyUnfoldArrays 1 arrays
]
o_n_heap_serial :: Int -> [Benchmark]
o_n_heap_serial value =
[
benchIOSink value "takeEQ" $ takeEQ value
, benchIOSink value "takeGE" $ takeGE value
-- lookahead benchmark holds the entire input till end
, benchIOSink value "lookAhead" $ lookAhead value
-- o-n-heap because of backtracking
, benchIOSrc sourceEscapedFrames value "takeFramedByEsc_"
$ takeFramedByEsc_ value
-- non-linear time complexity (parserD)
, benchIOSink value "split_" $ split_ value
-- XXX Takes lot of space when run on a long stream, why?
, benchIOSink value "monad16" $ monad16 value
-- These show non-linear time complexity.
-- They accumulate the results in a list.
, benchIOSink value "sepBy1" sepBy1
, benchIOSink value "manyAlt" manyAlt
, benchIOSink value "someAlt" someAlt
, benchIOSink value "listEqBy" (listEqBy value)
]
-- accumulate results in a list in IO
o_n_space_serial :: Int -> [Benchmark]
o_n_space_serial value =
[ benchIOSink value "sequenceA/100" $ sequenceA (value `div` 100)
, benchIOSink value "sequenceA_/100" $ sequenceA_ (value `div` 100)
, benchIOSink value "sequence/100" $ sequence (value `div` 100)
, benchIOSink value "sequence_/100" $ sequence_ (value `div` 100)
, benchIOSink value "choice (asum)/100" $ choiceAsum (value `div` 100)
-- , benchIOSink value "choice/100" $ choice (value `div` 100)
]
-------------------------------------------------------------------------------
-- Driver
-------------------------------------------------------------------------------
main :: IO ()
main = do
#ifndef FUSION_CHECK
env <- mkHandleBenchEnv
runWithCLIOptsEnv defaultStreamSize alloc (allBenchmarks env)
where
alloc value = Stream.fold Fold.toList $ Array.chunksOf 100 $ sourceUnfoldrM value 0
allBenchmarks env arrays value =
[ bgroup (o_1_space_prefix moduleName) (o_1_space_serial value)
, bgroup
(o_1_space_prefix moduleName ++ "/filesystem")
(o_1_space_filesystem env)
, bgroup (o_1_space_prefix moduleName)
(o_1_space_serial_unfold value arrays)
, bgroup (o_n_heap_prefix moduleName) (o_n_heap_serial value)
, bgroup (o_n_space_prefix moduleName) (o_n_space_serial value)
]
#else
-- Enable FUSION_CHECK macro at the beginning of the file
-- Enable one benchmark below, and run the benchmark
-- Check the .dump-simpl output
let value = 100000
-- let input = sourceUnfoldrM value 1
-- manyTill value input
-- deintercalate value input
-- deintercalate1 value input
-- deintercalateAll value input
-- sepByWords input
-- sepByAllWords input
-- sepBy1 input
-- sepByWords1 input
takeFramedByEsc_ value (sourceEscapedFrames value 1)
return ()
#endif