-
Notifications
You must be signed in to change notification settings - Fork 134
/
T.hs
803 lines (644 loc) · 28 KB
/
T.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
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
{-# OPTIONS_GHC -cpp -fglasgow-exts -fno-warn-orphans #-}
{-@ LIQUID "--prune-unsorted" @-}
-- |
-- Module : Data.ByteString.Fusion
-- License : BSD-style
-- Maintainer : dons@cse.unsw.edu.au
-- Stability : experimental
-- Portability : portable
--
-- Functional array fusion for ByteStrings.
--
-- Originally based on code from the Data Parallel Haskell project,
-- <http://www.cse.unsw.edu.au/~chak/project/dph>
--
-- #hide
module Data.ByteString.Fusion.T (
liquidCanaryFusion,
-- * Fusion utilities
loopU, loopL, fuseEFL,
NoAcc(NoAcc), loopArr, loopAcc, loopSndAcc, unSP,
mapEFL, filterEFL, foldEFL, foldEFL', scanEFL, mapAccumEFL, mapIndexEFL,
-- ** Alternative Fusion stuff
-- | This replaces 'loopU' with 'loopUp'
-- and adds several further special cases of loops.
loopUp, loopDown, loopNoAcc, loopMap, loopFilter,
loopWrapper, loopWrapperLE, sequenceLoops,
doUpLoop, doDownLoop, doNoAccLoop, doMapLoop, doFilterLoop,
-- | These are the special fusion cases for combining each loop form perfectly.
fuseAccAccEFL, fuseAccNoAccEFL, fuseNoAccAccEFL, fuseNoAccNoAccEFL,
fuseMapAccEFL, fuseAccMapEFL, fuseMapNoAccEFL, fuseNoAccMapEFL,
fuseMapMapEFL, fuseAccFilterEFL, fuseFilterAccEFL, fuseNoAccFilterEFL,
fuseFilterNoAccEFL, fuseFilterFilterEFL, fuseMapFilterEFL, fuseFilterMapEFL,
-- * Strict pairs and sums
PairS(..), MaybeS(..)
) where
import Data.ByteString.Internal
import qualified Data.ByteString.Lazy.Internal as L
import Foreign.ForeignPtr
import Foreign.Ptr
import Foreign.Storable (Storable(..))
import Data.Word (Word8, Word64)
import System.IO.Unsafe (unsafePerformIO)
-- LIQUID
import Language.Haskell.Liquid.Prelude (liquidAssume, liquidAssert)
{-@ qualif PlusOnePos(v: int): 0 <= (v + 1) @-}
{-@ qualif LePlusOne(v: int, x: int): v <= (x + 1) @-}
{-@ qualif LeDiff(v: int, x: int, y:int): v <= (x - y) @-}
{-@ qualif PlenEq(v: Ptr a, x: int): x <= (plen v) @-}
{-@ qualif BlenEq(v: int, x:ByteString): v = (bLength x) @-}
{-@ qualif PSnd(v: a, x:b): v = (psnd x) @-}
{-@ data PairS a b <p :: x0:a -> b -> Bool> = (:*:) (x::a) (y::b<p x>) @-}
{-@ measure pfst :: (PairS a b) -> a
pfst ((:*:) x y) = x
@-}
{-@ measure psnd :: (PairS a b) -> b
psnd ((:*:) x y) = y
@-}
{-@ measure isJustS :: (MaybeS a) -> Bool
isJustS (JustS x) = true
isJustS NothingS = false
@-}
{-@ qualif PlusOne(v:int, x:int): v = x + 1 @-}
{-@ type MaybeSJ a = {v: MaybeS a | (isJustS v)} @-}
{-@ type AccEFLJ acc = acc -> Word8 -> (PairS acc (MaybeSJ Word8)) @-}
{-@ type NoAccEFLJ = Word8 -> (MaybeSJ Word8) @-}
{- liquidCanaryFusion :: x:Int -> {v: Int | v > x} @-}
liquidCanaryFusion :: Int -> Int
liquidCanaryFusion x = x - 1
-- -----------------------------------------------------------------------------
--
-- Useful macros, until we have bang patterns
--
#define STRICT1(f) f a | a `seq` False = undefined
#define STRICT2(f) f a b | a `seq` b `seq` False = undefined
#define STRICT3(f) f a b c | a `seq` b `seq` c `seq` False = undefined
#define STRICT4(f) f a b c d | a `seq` b `seq` c `seq` d `seq` False = undefined
#define STRICT5(f) f a b c d e | a `seq` b `seq` c `seq` d `seq` e `seq` False = undefined
infixl 2 :*:
-- |Strict pair
data PairS a b = !a :*: !b deriving (Eq,Ord,Show)
-- |Strict Maybe
data MaybeS a = NothingS | JustS !a deriving (Eq,Ord,Show)
-- |Data type for accumulators which can be ignored. The rewrite rules rely on
-- the fact that no bottoms of this type are ever constructed; hence, we can
-- assume @(_ :: NoAcc) `seq` x = x@.
--
data NoAcc = NoAcc
-- |Type of loop functions
type AccEFL acc = acc -> Word8 -> (PairS acc (MaybeS Word8))
type NoAccEFL = Word8 -> MaybeS Word8
type MapEFL = Word8 -> Word8
type FilterEFL = Word8 -> Bool
infixr 9 `fuseEFL`
-- |Fuse to flat loop functions
fuseEFL :: AccEFL acc1 -> AccEFL acc2 -> AccEFL (PairS acc1 acc2)
fuseEFL f g (acc1 :*: acc2) e1 =
case f acc1 e1 of
acc1' :*: NothingS -> (acc1' :*: acc2) :*: NothingS
acc1' :*: JustS e2 ->
case g acc2 e2 of
acc2' :*: res -> (acc1' :*: acc2') :*: res
#if defined(__GLASGOW_HASKELL__)
{-# INLINE [1] fuseEFL #-}
#endif
-- | Special forms of loop arguments
--
-- * These are common special cases for the three function arguments of gen
-- and loop; we give them special names to make it easier to trigger RULES
-- applying in the special cases represented by these arguments. The
-- "INLINE [1]" makes sure that these functions are only inlined in the last
-- two simplifier phases.
--
-- * In the case where the accumulator is not needed, it is better to always
-- explicitly return a value `()', rather than just copy the input to the
-- output, as the former gives GHC better local information.
--
-- | Element function expressing a mapping only
#if !defined(LOOPNOACC_FUSION)
mapEFL :: (Word8 -> Word8) -> AccEFL NoAcc
mapEFL f = \_ e -> (NoAcc :*: (JustS $ f e))
#else
mapEFL :: (Word8 -> Word8) -> NoAccEFL
mapEFL f = \e -> JustS (f e)
#endif
#if defined(__GLASGOW_HASKELL__)
{-# INLINE [1] mapEFL #-}
#endif
-- | Element function implementing a filter function only
#if !defined(LOOPNOACC_FUSION)
filterEFL :: (Word8 -> Bool) -> AccEFL NoAcc
filterEFL p = \_ e -> if p e then (NoAcc :*: JustS e) else (NoAcc :*: NothingS)
#else
filterEFL :: (Word8 -> Bool) -> NoAccEFL
filterEFL p = \e -> if p e then JustS e else NothingS
#endif
#if defined(__GLASGOW_HASKELL__)
{-# INLINE [1] filterEFL #-}
#endif
-- |Element function expressing a reduction only
foldEFL :: (acc -> Word8 -> acc) -> AccEFL acc
foldEFL f = \a e -> (f a e :*: NothingS)
#if defined(__GLASGOW_HASKELL__)
{-# INLINE [1] foldEFL #-}
#endif
-- | A strict foldEFL.
foldEFL' :: (acc -> Word8 -> acc) -> AccEFL acc
foldEFL' f = \a e -> let a' = f a e in a' `seq` (a' :*: NothingS)
#if defined(__GLASGOW_HASKELL__)
{-# INLINE [1] foldEFL' #-}
#endif
-- | Element function expressing a prefix reduction only
--
{-@ scanEFL :: (Word8 -> Word8 -> Word8) -> AccEFLJ Word8 @-}
scanEFL :: (Word8 -> Word8 -> Word8) -> AccEFL Word8
scanEFL f = \a e -> (f a e :*: JustS a)
#if defined(__GLASGOW_HASKELL__)
{-# INLINE [1] scanEFL #-}
#endif
-- | Element function implementing a map and fold
--
{-@ mapAccumEFL :: (acc -> Word8 -> (acc, Word8)) -> AccEFLJ acc @-}
mapAccumEFL :: (acc -> Word8 -> (acc, Word8)) -> AccEFL acc
mapAccumEFL f = \a e -> case f a e of (a', e') -> (a' :*: JustS e')
#if defined(__GLASGOW_HASKELL__)
{-# INLINE [1] mapAccumEFL #-}
#endif
-- | Element function implementing a map with index
--
{-@ mapIndexEFL :: (Int -> Word8 -> Word8) -> AccEFLJ Int @-}
mapIndexEFL :: (Int -> Word8 -> Word8) -> AccEFL Int
mapIndexEFL f = \i e -> let i' = i+1 in i' `seq` (i' :*: JustS (f i e))
#if defined(__GLASGOW_HASKELL__)
{-# INLINE [1] mapIndexEFL #-}
#endif
-- | Projection functions that are fusion friendly (as in, we determine when
-- they are inlined)
{-@ loopArr :: p:(PairS acc arr) -> {v:arr | v = (psnd p)} @-}
loopArr :: (PairS acc arr) -> arr
loopArr (_ :*: arr) = arr
#if defined(__GLASGOW_HASKELL__)
{-# INLINE [1] loopArr #-}
#endif
{-@ loopAcc :: p:(PairS acc arr) -> {v:acc | v = (pfst p)} @-}
loopAcc :: (PairS acc arr) -> acc
loopAcc (acc :*: _) = acc
#if defined(__GLASGOW_HASKELL__)
{-# INLINE [1] loopAcc #-}
#endif
loopSndAcc :: (PairS (PairS acc1 acc2) arr) -> (PairS acc2 arr)
loopSndAcc ((_ :*: acc) :*: arr) = (acc :*: arr)
#if defined(__GLASGOW_HASKELL__)
{-# INLINE [1] loopSndAcc #-}
#endif
{-@ unSP :: p:(PairS acc arr) -> ({v:acc | v = (pfst p)}, {v:arr | v = (psnd p)}) @-}
unSP :: (PairS acc arr) -> (acc, arr)
unSP (acc :*: arr) = (acc, arr)
#if defined(__GLASGOW_HASKELL__)
{-# INLINE [1] unSP #-}
#endif
------------------------------------------------------------------------
--
-- Loop combinator and fusion rules for flat arrays
-- |Iteration over over ByteStrings
-- | Iteration over over ByteStrings
loopU :: AccEFL acc -- ^ mapping & folding, once per elem
-> acc -- ^ initial acc value
-> ByteString -- ^ input ByteString
-> (PairS acc ByteString)
{-@ loopU :: AccEFLJ acc -> acc -> b:ByteString -> (PairS acc (ByteStringSZ b)) @-}
loopU f start (PS z s i) = unsafePerformIO $ withForeignPtr z $ \a -> do
(ps, acc) <- createAndTrimEQ i $ \p -> do
(acc' :*: i') <- go (a `plusPtr` s) p start
return (0 :: Int, i', acc')
return (acc :*: ps)
where
go p ma = trans i 0 0
where
STRICT4(trans)
{- LIQUID WITNESS -}
trans (d :: Int) a_off ma_off acc
| a_off >= i = return (acc :*: ma_off)
| otherwise = do
x <- peekByteOff p a_off
let (acc' :*: oe) = f acc x
ma_off' <- case oe of
NothingS -> return ma_off
JustS e -> do pokeByteOff ma ma_off e
return $ ma_off + 1
trans (d-1) (a_off+1) ma_off' acc'
-- a_off = i - d
{-@ qualif Decr(v:Int, x: Int, y:Int): v = x - y @-}
#if defined(__GLASGOW_HASKELL__)
{-# INLINE [1] loopU #-}
#endif
{- RULES
"FPS loop/loop fusion!" forall em1 em2 start1 start2 arr.
loopU em2 start2 (loopArr (loopU em1 start1 arr)) =
loopSndAcc (loopU (em1 `fuseEFL` em2) (start1 :*: start2) arr)
#-}
-- Functional list/array fusion for lazy ByteStrings.
--
{-@ loopL :: (AccEFLJ acc) -> acc -> b:L.ByteString -> (PairS acc (LByteStringSZ b)) @-}
loopL :: AccEFL acc -- ^ mapping & folding, once per elem
-> acc -- ^ initial acc value
-> L.ByteString -- ^ input ByteString
-> PairS acc L.ByteString
loopL f = loop
where loop s L.Empty = (s :*: L.Empty)
loop s (L.Chunk x xs)
| l == 0 = (s'' :*: ys)
| otherwise = (s'' :*: L.Chunk y ys)
where (s' :*: y@(PS _ _ l)) = loopU f s x -- avoid circular dep on S.null
(s'' :*: ys) = loop s' xs
#if defined(__GLASGOW_HASKELL__)
{-# INLINE [1] loopL #-}
#endif
{- RULES
"FPS lazy loop/loop fusion!" forall em1 em2 start1 start2 arr.
loopL em2 start2 (loopArr (loopL em1 start1 arr)) =
loopSndAcc (loopL (em1 `fuseEFL` em2) (start1 :*: start2) arr)
#-}
{-
Alternate experimental formulation of loopU which partitions it into
an allocating wrapper and an imperitive array-mutating loop.
The point in doing this split is that we might be able to fuse multiple
loops into a single wrapper. This would save reallocating another buffer.
It should also give better cache locality by reusing the buffer.
Note that this stuff needs ghc-6.5 from May 26 or later for the RULES to
really work reliably.
-}
{-@ loopUp :: AccEFLJ acc -> acc -> b:ByteString -> (PairS acc (ByteStringSZ b)) @-}
loopUp :: AccEFL acc -> acc -> ByteString -> PairS acc ByteString
loopUp f a arr = loopWrapper (doUpLoop f a) arr
{-# INLINE loopUp #-}
{-@ loopDown :: AccEFLJ acc -> acc -> b:ByteString -> (PairS acc (ByteStringSZ b)) @-}
loopDown :: AccEFL acc -> acc -> ByteString -> PairS acc ByteString
loopDown f a arr = loopWrapper (doDownLoop f a) arr
{-# INLINE loopDown #-}
{-@ loopNoAcc :: NoAccEFLJ -> b:ByteString -> (PairS NoAcc (ByteStringSZ b)) @-}
loopNoAcc :: NoAccEFL -> ByteString -> PairS NoAcc ByteString
loopNoAcc f arr = loopWrapper (doNoAccLoop f NoAcc) arr
{-# INLINE loopNoAcc #-}
{-@ loopMap :: MapEFL -> b:ByteString -> (PairS NoAcc (ByteStringSZ b)) @-}
loopMap :: MapEFL -> ByteString -> PairS NoAcc ByteString
loopMap f arr = loopWrapper (doMapLoop f NoAcc) arr
{-# INLINE loopMap #-}
{-@ loopFilter :: FilterEFL -> b:ByteString -> (PairS NoAcc (ByteStringLE b)) @-}
loopFilter :: FilterEFL -> ByteString -> PairS NoAcc ByteString
loopFilter f arr = loopWrapperLE (doFilterLoop f NoAcc) arr
{-# INLINE loopFilter #-}
-- The type of imperitive loops that fill in a destination array by
-- reading a source array. They may not fill in the whole of the dest
-- array if the loop is behaving as a filter, this is why we return
-- the length that was filled in. The loop may also accumulate some
-- value as it loops over the source array.
{-@ type TripleSLE a N = PairS <{\z v -> v <= (N - (psnd z))}> (PairS <{\x y -> true}> a Nat) {v:Nat | v <= N} @-}
{-@ type TripleS a N = PairS <{\z v -> v <= (N - (psnd z))}> (PairS <{\x y -> true}> a Nat) {v:Nat | v = N} @-}
{-@ type ImperativeLoopLE acc = s:(PtrV Word8)
-> d:(PtrV Word8)
-> n:{v: Nat | ((v <= (plen d)) && (v <= (plen s))) }
-> IO (TripleSLE acc n)
@-}
{-@ type ImperativeLoop acc = s:(PtrV Word8)
-> d:(PtrV Word8)
-> n:{v: Nat | ((v <= (plen d)) && (v <= (plen s))) }
-> IO (TripleS acc n)
@-}
type ImperativeLoop acc =
Ptr Word8 -- pointer to the start of the source byte array
-> Ptr Word8 -- pointer to ther start of the destination byte array
-> Int -- length of the source byte array
-> IO (PairS (PairS acc Int) Int) -- result and offset, length of dest that was filled
{-@ loopWrapperLE :: ImperativeLoopLE acc -> b:ByteString -> PairS acc (ByteStringLE b) @-}
loopWrapperLE :: ImperativeLoop acc -> ByteString -> PairS acc ByteString
loopWrapperLE body (PS srcFPtr srcOffset srcLen) = unsafePerformIO $
withForeignPtr srcFPtr $ \srcPtr -> do
(ps, acc) <- createAndTrim' srcLen $ \destPtr -> do
(acc :*: destOffset :*: destLen) <- body (srcPtr `plusPtr` srcOffset) destPtr srcLen
return $ (destOffset, destLen, acc)
return (acc :*: ps)
-- LIQUID DUPLICATECODE
{-@ loopWrapper :: ImperativeLoop acc -> b:ByteString -> PairS acc (ByteStringSZ b) @-}
loopWrapper :: ImperativeLoop acc -> ByteString -> PairS acc ByteString
loopWrapper body (PS srcFPtr srcOffset srcLen) = unsafePerformIO $
withForeignPtr srcFPtr $ \srcPtr -> do
(ps, acc) <- createAndTrimEQ srcLen $ \destPtr -> do
(acc :*: destOffset :*: destLen) <- body (srcPtr `plusPtr` srcOffset) destPtr srcLen
return $ (destOffset, id destLen, acc)
return (acc :*: ps)
{-@ doUpLoop :: AccEFLJ acc -> acc -> ImperativeLoop acc @-}
doUpLoop :: AccEFL acc -> acc -> ImperativeLoop acc
doUpLoop f acc0 src dest len = loop len 0 0 acc0
where STRICT4(loop)
{- LIQUID WITNESS -}
loop (d :: Int) src_off dest_off acc
| src_off >= len = return (acc :*: (0 :: Int) {- LIQUID CAST -} :*: dest_off)
| otherwise = do
x <- peekByteOff src src_off
case f acc x of
(acc' :*: NothingS) -> loop (d-1) (src_off+1) dest_off acc'
(acc' :*: JustS x') -> pokeByteOff dest dest_off x'
>> loop (d-1) (src_off+1) (dest_off+1) acc'
{-@ doDownLoop :: AccEFLJ acc -> acc -> ImperativeLoop acc @-}
doDownLoop :: AccEFL acc -> acc -> ImperativeLoop acc
doDownLoop f acc0 src dest len = loop len (len-1) (len-1) acc0
where STRICT4(loop)
{- LIQUID WITNESS -}
loop (d :: Int) src_offDOWN dest_offDOWN acc
| src_offDOWN < 0 = return (acc :*: dest_offDOWN + 1 :*: len - (dest_offDOWN + 1))
| otherwise = do
x <- peekByteOff src src_offDOWN
case f acc x of
(acc' :*: NothingS) -> loop (d-1) (src_offDOWN - 1) dest_offDOWN acc'
(acc' :*: JustS x') -> pokeByteOff dest dest_offDOWN x'
>> loop (d-1) (src_offDOWN - 1) (dest_offDOWN - 1) acc'
{-@ doNoAccLoop :: NoAccEFLJ -> noAcc -> ImperativeLoop noAcc @-}
doNoAccLoop :: NoAccEFL -> noAcc -> ImperativeLoop noAcc
doNoAccLoop f noAcc src dest len = loop len 0 0
where STRICT3(loop)
{- LIQUID WITNESS -}
loop (d :: Int) src_off dest_off
| src_off >= len = return (noAcc :*: (0 :: Int) {- LIQUID CAST -} :*: dest_off)
| otherwise = do
x <- peekByteOff src src_off
case f x of
NothingS -> loop (d-1) (src_off+1) dest_off
JustS x' -> pokeByteOff dest dest_off x'
>> loop (d-1) (src_off+1) (dest_off+1)
{-@ doMapLoop :: MapEFL -> noAcc -> ImperativeLoop noAcc @-}
doMapLoop :: MapEFL -> noAcc -> ImperativeLoop noAcc
doMapLoop f noAcc src dest len = loop len 0
where STRICT2(loop)
{- LIQUID WITNESS -}
loop (d :: Int) n
| n >= len = return (noAcc :*: (0 :: Int) {- LIQUID CAST -} :*: len)
| otherwise = do
x <- peekByteOff src n
pokeByteOff dest n (f x)
loop (d-1) (n+1) -- offset always the same, only pass 1 arg
{-@ doFilterLoop :: FilterEFL -> noAcc -> ImperativeLoopLE noAcc @-}
doFilterLoop :: FilterEFL -> noAcc -> ImperativeLoop noAcc
doFilterLoop f noAcc src dest len = loop len 0 0
where STRICT3(loop)
{- LIQUID WITNESS -}
loop (d :: Int) src_off dest_off
| src_off >= len = return (noAcc :*: (0 :: Int) {- LIQUID CAST -} :*: dest_off)
| otherwise = do
x <- peekByteOff src src_off
if f x
then pokeByteOff dest dest_off x
>> loop (d-1) (src_off+1) (dest_off+1)
else loop (d-1) (src_off+1) dest_off
-- LIQUID
-- run two loops in sequence,
-- think of it as: loop1 >> loop2
{-@ sequenceLoops :: ImperativeLoop acc1 -> ImperativeLoop acc2 -> ImperativeLoop (PairS acc1 acc2) @-}
sequenceLoops :: ImperativeLoop acc1
-> ImperativeLoop acc2
-> ImperativeLoop (PairS acc1 acc2)
sequenceLoops loop1 loop2 src dest len0 = do
(acc1 :*: off1 :*: len1) <- loop1 src dest len0
(acc2 :*: off2 :*: len2) <-
let src' = dest `plusPtr` off1
dest' = src' -- note that we are using dest == src
-- for the second loop as we are
-- mutating the dest array in-place!
in loop2 src' dest' len1
return ((acc1 :*: acc2) :*: off1 + off2 :*: len2)
-- TODO: prove that this is associative! (I think it is)
-- since we can't be sure how the RULES will combine loops.
#if defined(__GLASGOW_HASKELL__)
{-# INLINE [1] doUpLoop #-}
{-# INLINE [1] doDownLoop #-}
{-# INLINE [1] doNoAccLoop #-}
{-# INLINE [1] doMapLoop #-}
{-# INLINE [1] doFilterLoop #-}
{-# INLINE [1] loopWrapper #-}
{-# INLINE [1] sequenceLoops #-}
{-# INLINE [1] fuseAccAccEFL #-}
{-# INLINE [1] fuseAccNoAccEFL #-}
{-# INLINE [1] fuseNoAccAccEFL #-}
{-# INLINE [1] fuseNoAccNoAccEFL #-}
{-# INLINE [1] fuseMapAccEFL #-}
{-# INLINE [1] fuseAccMapEFL #-}
{-# INLINE [1] fuseMapNoAccEFL #-}
{-# INLINE [1] fuseNoAccMapEFL #-}
{-# INLINE [1] fuseMapMapEFL #-}
{-# INLINE [1] fuseAccFilterEFL #-}
{-# INLINE [1] fuseFilterAccEFL #-}
{-# INLINE [1] fuseNoAccFilterEFL #-}
{-# INLINE [1] fuseFilterNoAccEFL #-}
{-# INLINE [1] fuseFilterFilterEFL #-}
{-# INLINE [1] fuseMapFilterEFL #-}
{-# INLINE [1] fuseFilterMapEFL #-}
#endif
{- RULES
"FPS loopArr/loopSndAcc" forall x.
loopArr (loopSndAcc x) = loopArr x
"FPS seq/NoAcc" forall (u::NoAcc) e.
u `seq` e = e
"FPS loop/loop wrapper elimination" forall loop1 loop2 arr.
loopWrapper loop2 (loopArr (loopWrapper loop1 arr)) =
loopSndAcc (loopWrapper (sequenceLoops loop1 loop2) arr)
--
-- n.b in the following, when reading n/m fusion, recall sequenceLoops
-- is monadic, so its really n >> m fusion (i.e. m.n), not n . m fusion.
--
"FPS up/up loop fusion" forall f1 f2 acc1 acc2.
sequenceLoops (doUpLoop f1 acc1) (doUpLoop f2 acc2) =
doUpLoop (f1 `fuseAccAccEFL` f2) (acc1 :*: acc2)
"FPS map/map loop fusion" forall f1 f2 acc1 acc2.
sequenceLoops (doMapLoop f1 acc1) (doMapLoop f2 acc2) =
doMapLoop (f1 `fuseMapMapEFL` f2) (acc1 :*: acc2)
"FPS filter/filter loop fusion" forall f1 f2 acc1 acc2.
sequenceLoops (doFilterLoop f1 acc1) (doFilterLoop f2 acc2) =
doFilterLoop (f1 `fuseFilterFilterEFL` f2) (acc1 :*: acc2)
"FPS map/filter loop fusion" forall f1 f2 acc1 acc2.
sequenceLoops (doMapLoop f1 acc1) (doFilterLoop f2 acc2) =
doNoAccLoop (f1 `fuseMapFilterEFL` f2) (acc1 :*: acc2)
"FPS filter/map loop fusion" forall f1 f2 acc1 acc2.
sequenceLoops (doFilterLoop f1 acc1) (doMapLoop f2 acc2) =
doNoAccLoop (f1 `fuseFilterMapEFL` f2) (acc1 :*: acc2)
"FPS map/up loop fusion" forall f1 f2 acc1 acc2.
sequenceLoops (doMapLoop f1 acc1) (doUpLoop f2 acc2) =
doUpLoop (f1 `fuseMapAccEFL` f2) (acc1 :*: acc2)
"FPS up/map loop fusion" forall f1 f2 acc1 acc2.
sequenceLoops (doUpLoop f1 acc1) (doMapLoop f2 acc2) =
doUpLoop (f1 `fuseAccMapEFL` f2) (acc1 :*: acc2)
"FPS filter/up loop fusion" forall f1 f2 acc1 acc2.
sequenceLoops (doFilterLoop f1 acc1) (doUpLoop f2 acc2) =
doUpLoop (f1 `fuseFilterAccEFL` f2) (acc1 :*: acc2)
"FPS up/filter loop fusion" forall f1 f2 acc1 acc2.
sequenceLoops (doUpLoop f1 acc1) (doFilterLoop f2 acc2) =
doUpLoop (f1 `fuseAccFilterEFL` f2) (acc1 :*: acc2)
"FPS down/down loop fusion" forall f1 f2 acc1 acc2.
sequenceLoops (doDownLoop f1 acc1) (doDownLoop f2 acc2) =
doDownLoop (f1 `fuseAccAccEFL` f2) (acc1 :*: acc2)
"FPS map/down fusion" forall f1 f2 acc1 acc2.
sequenceLoops (doMapLoop f1 acc1) (doDownLoop f2 acc2) =
doDownLoop (f1 `fuseMapAccEFL` f2) (acc1 :*: acc2)
"FPS down/map loop fusion" forall f1 f2 acc1 acc2.
sequenceLoops (doDownLoop f1 acc1) (doMapLoop f2 acc2) =
doDownLoop (f1 `fuseAccMapEFL` f2) (acc1 :*: acc2)
"FPS filter/down fusion" forall f1 f2 acc1 acc2.
sequenceLoops (doFilterLoop f1 acc1) (doDownLoop f2 acc2) =
doDownLoop (f1 `fuseFilterAccEFL` f2) (acc1 :*: acc2)
"FPS down/filter loop fusion" forall f1 f2 acc1 acc2.
sequenceLoops (doDownLoop f1 acc1) (doFilterLoop f2 acc2) =
doDownLoop (f1 `fuseAccFilterEFL` f2) (acc1 :*: acc2)
"FPS noAcc/noAcc loop fusion" forall f1 f2 acc1 acc2.
sequenceLoops (doNoAccLoop f1 acc1) (doNoAccLoop f2 acc2) =
doNoAccLoop (f1 `fuseNoAccNoAccEFL` f2) (acc1 :*: acc2)
"FPS noAcc/up loop fusion" forall f1 f2 acc1 acc2.
sequenceLoops (doNoAccLoop f1 acc1) (doUpLoop f2 acc2) =
doUpLoop (f1 `fuseNoAccAccEFL` f2) (acc1 :*: acc2)
"FPS up/noAcc loop fusion" forall f1 f2 acc1 acc2.
sequenceLoops (doUpLoop f1 acc1) (doNoAccLoop f2 acc2) =
doUpLoop (f1 `fuseAccNoAccEFL` f2) (acc1 :*: acc2)
"FPS map/noAcc loop fusion" forall f1 f2 acc1 acc2.
sequenceLoops (doMapLoop f1 acc1) (doNoAccLoop f2 acc2) =
doNoAccLoop (f1 `fuseMapNoAccEFL` f2) (acc1 :*: acc2)
"FPS noAcc/map loop fusion" forall f1 f2 acc1 acc2.
sequenceLoops (doNoAccLoop f1 acc1) (doMapLoop f2 acc2) =
doNoAccLoop (f1 `fuseNoAccMapEFL` f2) (acc1 :*: acc2)
"FPS filter/noAcc loop fusion" forall f1 f2 acc1 acc2.
sequenceLoops (doFilterLoop f1 acc1) (doNoAccLoop f2 acc2) =
doNoAccLoop (f1 `fuseFilterNoAccEFL` f2) (acc1 :*: acc2)
"FPS noAcc/filter loop fusion" forall f1 f2 acc1 acc2.
sequenceLoops (doNoAccLoop f1 acc1) (doFilterLoop f2 acc2) =
doNoAccLoop (f1 `fuseNoAccFilterEFL` f2) (acc1 :*: acc2)
"FPS noAcc/down loop fusion" forall f1 f2 acc1 acc2.
sequenceLoops (doNoAccLoop f1 acc1) (doDownLoop f2 acc2) =
doDownLoop (f1 `fuseNoAccAccEFL` f2) (acc1 :*: acc2)
"FPS down/noAcc loop fusion" forall f1 f2 acc1 acc2.
sequenceLoops (doDownLoop f1 acc1) (doNoAccLoop f2 acc2) =
doDownLoop (f1 `fuseAccNoAccEFL` f2) (acc1 :*: acc2)
#-}
{-
up = up loop
down = down loop
map = map special case
filter = filter special case
noAcc = noAcc undirectional loop (unused)
heirarchy:
up down
^ ^
\ /
noAcc
^ ^
/ \
map filter
each is a special case of the things above
so we get rules that combine things on the same level
and rules that combine things on different levels
to get something on the higher level
so all the cases:
up/up --> up fuseAccAccEFL
down/down --> down fuseAccAccEFL
noAcc/noAcc --> noAcc fuseNoAccNoAccEFL
noAcc/up --> up fuseNoAccAccEFL
up/noAcc --> up fuseAccNoAccEFL
noAcc/down --> down fuseNoAccAccEFL
down/noAcc --> down fuseAccNoAccEFL
and if we do the map, filter special cases then it adds a load more:
map/map --> map fuseMapMapEFL
filter/filter --> filter fuseFilterFilterEFL
map/filter --> noAcc fuseMapFilterEFL
filter/map --> noAcc fuseFilterMapEFL
map/noAcc --> noAcc fuseMapNoAccEFL
noAcc/map --> noAcc fuseNoAccMapEFL
map/up --> up fuseMapAccEFL
up/map --> up fuseAccMapEFL
map/down --> down fuseMapAccEFL
down/map --> down fuseAccMapEFL
filter/noAcc --> noAcc fuseNoAccFilterEFL
noAcc/filter --> noAcc fuseFilterNoAccEFL
filter/up --> up fuseFilterAccEFL
up/filter --> up fuseAccFilterEFL
filter/down --> down fuseFilterAccEFL
down/filter --> down fuseAccFilterEFL
-}
fuseAccAccEFL :: AccEFL acc1 -> AccEFL acc2 -> AccEFL (PairS acc1 acc2)
fuseAccAccEFL f g (acc1 :*: acc2) e1 =
case f acc1 e1 of
acc1' :*: NothingS -> (acc1' :*: acc2) :*: NothingS
acc1' :*: JustS e2 ->
case g acc2 e2 of
acc2' :*: res -> (acc1' :*: acc2') :*: res
fuseAccNoAccEFL :: AccEFL acc -> NoAccEFL -> AccEFL (PairS acc noAcc)
fuseAccNoAccEFL f g (acc :*: noAcc) e1 =
case f acc e1 of
acc' :*: NothingS -> (acc' :*: noAcc) :*: NothingS
acc' :*: JustS e2 -> (acc' :*: noAcc) :*: g e2
fuseNoAccAccEFL :: NoAccEFL -> AccEFL acc -> AccEFL (PairS noAcc acc)
fuseNoAccAccEFL f g (noAcc :*: acc) e1 =
case f e1 of
NothingS -> (noAcc :*: acc) :*: NothingS
JustS e2 ->
case g acc e2 of
acc' :*: res -> (noAcc :*: acc') :*: res
fuseNoAccNoAccEFL :: NoAccEFL -> NoAccEFL -> NoAccEFL
fuseNoAccNoAccEFL f g e1 =
case f e1 of
NothingS -> NothingS
JustS e2 -> g e2
fuseMapAccEFL :: MapEFL -> AccEFL acc -> AccEFL (PairS noAcc acc)
fuseMapAccEFL f g (noAcc :*: acc) e1 =
case g acc (f e1) of
(acc' :*: res) -> (noAcc :*: acc') :*: res
fuseAccMapEFL :: AccEFL acc -> MapEFL -> AccEFL (PairS acc noAcc)
fuseAccMapEFL f g (acc :*: noAcc) e1 =
case f acc e1 of
(acc' :*: NothingS) -> (acc' :*: noAcc) :*: NothingS
(acc' :*: JustS e2) -> (acc' :*: noAcc) :*: JustS (g e2)
fuseMapMapEFL :: MapEFL -> MapEFL -> MapEFL
fuseMapMapEFL f g e1 = g (f e1) -- n.b. perfect fusion
fuseMapNoAccEFL :: MapEFL -> NoAccEFL -> NoAccEFL
fuseMapNoAccEFL f g e1 = g (f e1)
fuseNoAccMapEFL :: NoAccEFL -> MapEFL -> NoAccEFL
fuseNoAccMapEFL f g e1 =
case f e1 of
NothingS -> NothingS
JustS e2 -> JustS (g e2)
fuseAccFilterEFL :: AccEFL acc -> FilterEFL -> AccEFL (PairS acc noAcc)
fuseAccFilterEFL f g (acc :*: noAcc) e1 =
case f acc e1 of
acc' :*: NothingS -> (acc' :*: noAcc) :*: NothingS
acc' :*: JustS e2 ->
case g e2 of
False -> (acc' :*: noAcc) :*: NothingS
True -> (acc' :*: noAcc) :*: JustS e2
fuseFilterAccEFL :: FilterEFL -> AccEFL acc -> AccEFL (PairS noAcc acc)
fuseFilterAccEFL f g (noAcc :*: acc) e1 =
case f e1 of
False -> (noAcc :*: acc) :*: NothingS
True ->
case g acc e1 of
acc' :*: res -> (noAcc :*: acc') :*: res
fuseNoAccFilterEFL :: NoAccEFL -> FilterEFL -> NoAccEFL
fuseNoAccFilterEFL f g e1 =
case f e1 of
NothingS -> NothingS
JustS e2 ->
case g e2 of
False -> NothingS
True -> JustS e2
fuseFilterNoAccEFL :: FilterEFL -> NoAccEFL -> NoAccEFL
fuseFilterNoAccEFL f g e1 =
case f e1 of
False -> NothingS
True -> g e1
fuseFilterFilterEFL :: FilterEFL -> FilterEFL -> FilterEFL
fuseFilterFilterEFL f g e1 = f e1 && g e1
fuseMapFilterEFL :: MapEFL -> FilterEFL -> NoAccEFL
fuseMapFilterEFL f g e1 =
case f e1 of
e2 -> case g e2 of
False -> NothingS
True -> JustS e2
fuseFilterMapEFL :: FilterEFL -> MapEFL -> NoAccEFL
fuseFilterMapEFL f g e1 =
case f e1 of
False -> NothingS
True -> JustS (g e1)