-- ------------------------------------------------------------

{- |
   Module     : Control.FlatSeq
   Copyright  : Copyright (C) 2011 Uwe Schmidt
   License    : MIT

   Maintainer : Uwe Schmidt (uwe\@fh-wedel.de)
   Stability  : experimental
   Portability: portable

   Force evaluation like deepseq in Control.DeepSeq,
   but control the depth of evaluation.
   flatseq may evaluate more than seq but less than deepseq

-}

-- ------------------------------------------------------------

module Control.FlatSeq
where

import Data.Word

-- ------------------------------------------------------------

infixr 0 $!!

($!!)                           :: WNFData a => (a -> b) -> a -> b
a -> b
f $!! :: (a -> b) -> a -> b
$!! a
x                         = a -> ()
forall a. WNFData a => a -> ()
rwnf a
x () -> b -> b
`seq` a -> b
f a
x
{-# INLINE ($!!) #-}

flatseq                         :: WNFData a => a -> b -> b
flatseq :: a -> b -> b
flatseq a
a b
b                     = a -> ()
forall a. WNFData a => a -> ()
rwnf a
a () -> b -> b
`seq` b
b
{-# INLINE flatseq #-}

rlnf                            :: (a -> ()) -> [a] -> ()
rlnf :: (a -> ()) -> [a] -> ()
rlnf a -> ()
_ []                       = ()
rlnf a -> ()
r (a
x:[a]
xs)                   = a -> ()
r a
x () -> () -> ()
`seq` (a -> ()) -> [a] -> ()
forall a. (a -> ()) -> [a] -> ()
rlnf a -> ()
r [a]
xs
{-# INLINE rlnf #-}

-- | A class of types that can be partially evaluated, but evaluation can be propagated deeper than WHNF

class WNFData a where
    -- | Default for rwnf is reduction to WHNF
    rwnf                        :: a -> ()
    rwnf a
a                      = a
a a -> () -> ()
`seq` ()
    {-# INLINE rwnf #-}

    -- | Default for rwnf2 is rwnf
    rwnf2                       :: a -> ()
    rwnf2                       = a -> ()
forall a. WNFData a => a -> ()
rwnf
    {-# INLINE rwnf2 #-}

instance WNFData Int 
instance WNFData Integer
instance WNFData Float
instance WNFData Double

instance WNFData Char
instance WNFData Bool
instance WNFData ()

instance WNFData Word
instance WNFData Word8
instance WNFData Word16
instance WNFData Word32
instance WNFData Word64

instance WNFData a => WNFData [a] where
    rwnf :: [a] -> ()
rwnf []                     = ()
    rwnf (a
x:[a]
xs)                 = a
x a -> () -> ()
`seq` [a] -> ()
forall a. WNFData a => a -> ()
rwnf [a]
xs
    {-# INLINE rwnf #-}

instance (WNFData a, WNFData b) => WNFData (a,b) where
    rwnf :: (a, b) -> ()
rwnf (a
x,b
y)                  = a -> ()
forall a. WNFData a => a -> ()
rwnf a
x () -> () -> ()
`seq` b -> ()
forall a. WNFData a => a -> ()
rwnf b
y
    {-# INLINE rwnf #-}

instance (WNFData a, WNFData b, WNFData c) => WNFData (a,b,c) where
    rwnf :: (a, b, c) -> ()
rwnf (a
x,b
y,c
z)                = a -> ()
forall a. WNFData a => a -> ()
rwnf a
x () -> () -> ()
`seq` b -> ()
forall a. WNFData a => a -> ()
rwnf b
y () -> () -> ()
`seq` c -> ()
forall a. WNFData a => a -> ()
rwnf c
z 
    {-# INLINE rwnf #-}

instance (WNFData a, WNFData b, WNFData c, WNFData d) => WNFData (a,b,c,d) where
    rwnf :: (a, b, c, d) -> ()
rwnf (a
x1,b
x2,c
x3,d
x4)          = a -> ()
forall a. WNFData a => a -> ()
rwnf a
x1 () -> () -> ()
`seq` 
                                  b -> ()
forall a. WNFData a => a -> ()
rwnf b
x2 () -> () -> ()
`seq` 
                                  c -> ()
forall a. WNFData a => a -> ()
rwnf c
x3 () -> () -> ()
`seq` 
                                  d -> ()
forall a. WNFData a => a -> ()
rwnf d
x4 
    {-# INLINE rwnf #-}

-- ------------------------------------------------------------