/usr/lib/hugs/packages/base/Data/ByteString/Fusion.hs is in libhugs-base-bundled 98.200609.21-5.4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | {-# OPTIONS_GHC -cpp -fglasgow-exts -fno-warn-orphans #-}
--
-- Module : Data.ByteString.Fusion
-- License : BSD-style
-- Maintainer : dons@cse.unsw.edu.au
-- Stability : experimental
-- Portability : portable, requires ffi and cpp
-- Tested with : GHC 6.4.1 and Hugs March 2005
--
-- #hide
-- | Functional array fusion for ByteStrings.
--
-- Originally based on code from the Data Parallel Haskell project,
-- <http://www.cse.unsw.edu.au/~chak/project/dph>
--
module Data.ByteString.Fusion (
-- * 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, 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.Base
import Foreign.ForeignPtr
import Foreign.Ptr
import Foreign.Storable (Storable(..))
import Data.Word (Word8)
import System.IO.Unsafe (unsafePerformIO)
-- -----------------------------------------------------------------------------
--
-- Useful macros, until we have bang patterns
--
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
-- | 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
mapEFL :: (Word8 -> Word8) -> AccEFL NoAcc
mapEFL f = \_ e -> (NoAcc :*: (JustS $ f e))
-- | Element function implementing a filter function only
filterEFL :: (Word8 -> Bool) -> AccEFL NoAcc
filterEFL p = \_ e -> if p e then (NoAcc :*: JustS e) else (NoAcc :*: NothingS)
-- |Element function expressing a reduction only
foldEFL :: (acc -> Word8 -> acc) -> AccEFL acc
foldEFL f = \a e -> (f a e :*: NothingS)
-- | A strict foldEFL.
foldEFL' :: (acc -> Word8 -> acc) -> AccEFL acc
foldEFL' f = \a e -> let a' = f a e in a' `seq` (a' :*: NothingS)
-- | Element function expressing a prefix reduction only
--
scanEFL :: (Word8 -> Word8 -> Word8) -> AccEFL Word8
scanEFL f = \a e -> (f a e :*: JustS a)
-- | Element function implementing a map and fold
--
mapAccumEFL :: (acc -> Word8 -> (acc, Word8)) -> AccEFL acc
mapAccumEFL f = \a e -> case f a e of (a', e') -> (a' :*: JustS e')
-- | Element function implementing a map with index
--
mapIndexEFL :: (Int -> Word8 -> Word8) -> AccEFL Int
mapIndexEFL f = \i e -> let i' = i+1 in i' `seq` (i' :*: JustS (f i e))
-- | Projection functions that are fusion friendly (as in, we determine when
-- they are inlined)
loopArr :: (PairS acc arr) -> arr
loopArr (_ :*: arr) = arr
loopAcc :: (PairS acc arr) -> acc
loopAcc (acc :*: _) = acc
loopSndAcc :: (PairS (PairS acc1 acc2) arr) -> (PairS acc2 arr)
loopSndAcc ((_ :*: acc) :*: arr) = (acc :*: arr)
unSP :: (PairS acc arr) -> (acc, arr)
unSP (acc :*: arr) = (acc, arr)
------------------------------------------------------------------------
--
-- 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 f start (PS z s i) = unsafePerformIO $ withForeignPtr z $ \a -> do
(ps, acc) <- createAndTrim' i $ \p -> do
(acc' :*: i') <- go (a `plusPtr` s) p start
return (0, i', acc')
return (acc :*: ps)
where
go p ma = trans 0 0
where
trans a b c | a `seq` b `seq` c `seq` False = undefined
trans 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 (a_off+1) ma_off' acc'
{-# 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 :: AccEFL acc -- ^ mapping & folding, once per elem
-> acc -- ^ initial acc value
-> [ByteString] -- ^ input ByteString
-> PairS acc [ByteString]
loopL f = loop
where loop s [] = (s :*: [])
loop s (x:xs)
| l == 0 = (s'' :*: ys)
| otherwise = (s'' :*: y:ys)
where (s' :*: y@(PS _ _ l)) = loopU f s x -- avoid circular dep on P.null
(s'' :*: ys) = loop s' xs
{-# 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 :: AccEFL acc -> acc -> ByteString -> PairS acc ByteString
loopUp f a arr = loopWrapper (doUpLoop f a) arr
{-# INLINE loopUp #-}
loopDown :: AccEFL acc -> acc -> ByteString -> PairS acc ByteString
loopDown f a arr = loopWrapper (doDownLoop f a) arr
{-# INLINE loopDown #-}
loopNoAcc :: NoAccEFL -> ByteString -> PairS NoAcc ByteString
loopNoAcc f arr = loopWrapper (doNoAccLoop f NoAcc) arr
{-# INLINE loopNoAcc #-}
loopMap :: MapEFL -> ByteString -> PairS NoAcc ByteString
loopMap f arr = loopWrapper (doMapLoop f NoAcc) arr
{-# INLINE loopMap #-}
loopFilter :: FilterEFL -> ByteString -> PairS NoAcc ByteString
loopFilter f arr = loopWrapper (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 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
loopWrapper :: ImperativeLoop acc -> ByteString -> PairS acc ByteString
loopWrapper 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)
doUpLoop :: AccEFL acc -> acc -> ImperativeLoop acc
doUpLoop f acc0 src dest len = loop 0 0 acc0
where loop a b c | a `seq` b `seq` c `seq` False = undefined
loop src_off dest_off acc
| src_off >= len = return (acc :*: 0 :*: dest_off)
| otherwise = do
x <- peekByteOff src src_off
case f acc x of
(acc' :*: NothingS) -> loop (src_off+1) dest_off acc'
(acc' :*: JustS x') -> pokeByteOff dest dest_off x'
>> loop (src_off+1) (dest_off+1) acc'
doDownLoop :: AccEFL acc -> acc -> ImperativeLoop acc
doDownLoop f acc0 src dest len = loop (len-1) (len-1) acc0
where loop a b c | a `seq` b `seq` c `seq` False = undefined
loop src_off dest_off acc
| src_off < 0 = return (acc :*: dest_off + 1 :*: len - (dest_off + 1))
| otherwise = do
x <- peekByteOff src src_off
case f acc x of
(acc' :*: NothingS) -> loop (src_off-1) dest_off acc'
(acc' :*: JustS x') -> pokeByteOff dest dest_off x'
>> loop (src_off-1) (dest_off-1) acc'
doNoAccLoop :: NoAccEFL -> noAcc -> ImperativeLoop noAcc
doNoAccLoop f noAcc src dest len = loop 0 0
where loop a b | a `seq` b `seq` False = undefined
loop src_off dest_off
| src_off >= len = return (noAcc :*: 0 :*: dest_off)
| otherwise = do
x <- peekByteOff src src_off
case f x of
NothingS -> loop (src_off+1) dest_off
JustS x' -> pokeByteOff dest dest_off x'
>> loop (src_off+1) (dest_off+1)
doMapLoop :: MapEFL -> noAcc -> ImperativeLoop noAcc
doMapLoop f noAcc src dest len = loop 0
where loop a | a `seq` False = undefined
loop n
| n >= len = return (noAcc :*: 0 :*: len)
| otherwise = do
x <- peekByteOff src n
pokeByteOff dest n (f x)
loop (n+1) -- offset always the same, only pass 1 arg
doFilterLoop :: FilterEFL -> noAcc -> ImperativeLoop noAcc
doFilterLoop f noAcc src dest len = loop 0 0
where loop a b | a `seq` b `seq` False = undefined
loop src_off dest_off
| src_off >= len = return (noAcc :*: 0 :*: dest_off)
| otherwise = do
x <- peekByteOff src src_off
if f x
then pokeByteOff dest dest_off x
>> loop (src_off+1) (dest_off+1)
else loop (src_off+1) dest_off
-- run two loops in sequence,
-- think of it as: loop1 >> loop2
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.
{-# 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)
|