-- |
-- Module      : Streamly.Internal.Data.Strict
-- Copyright   : (c) 2019 Composewell Technologies
--               (c) 2013 Gabriel Gonzalez
-- License     : BSD3
-- Maintainer  : streamly@composewell.com
-- Stability   : experimental
-- Portability : GHC
--
-- | Strict data types to be used as accumulator for strict left folds and
-- scans. For more comprehensive strict data types see
-- https://hackage.haskell.org/package/strict-base-types . The names have been
-- suffixed by a prime so that programmers can easily distinguish the strict
-- versions from the lazy ones.
--
-- One major advantage of strict data structures as accumulators in folds and
-- scans is that it helps the compiler optimize the code much better by
-- unboxing. In a big tight loop the difference could be huge.
--
module Streamly.Internal.Data.Strict
    (
      Tuple' (..)
    , Tuple3' (..)
    , Tuple4' (..)
    , Maybe' (..)
    , toMaybe
    , Either' (..)
    )
where

-------------------------------------------------------------------------------
-- Tuples
-------------------------------------------------------------------------------
--
data Tuple' a b = Tuple' !a !b deriving Int -> Tuple' a b -> ShowS
[Tuple' a b] -> ShowS
Tuple' a b -> String
(Int -> Tuple' a b -> ShowS)
-> (Tuple' a b -> String)
-> ([Tuple' a b] -> ShowS)
-> Show (Tuple' a b)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall a b. (Show a, Show b) => Int -> Tuple' a b -> ShowS
forall a b. (Show a, Show b) => [Tuple' a b] -> ShowS
forall a b. (Show a, Show b) => Tuple' a b -> String
showList :: [Tuple' a b] -> ShowS
$cshowList :: forall a b. (Show a, Show b) => [Tuple' a b] -> ShowS
show :: Tuple' a b -> String
$cshow :: forall a b. (Show a, Show b) => Tuple' a b -> String
showsPrec :: Int -> Tuple' a b -> ShowS
$cshowsPrec :: forall a b. (Show a, Show b) => Int -> Tuple' a b -> ShowS
Show
data Tuple3' a b c = Tuple3' !a !b !c deriving Int -> Tuple3' a b c -> ShowS
[Tuple3' a b c] -> ShowS
Tuple3' a b c -> String
(Int -> Tuple3' a b c -> ShowS)
-> (Tuple3' a b c -> String)
-> ([Tuple3' a b c] -> ShowS)
-> Show (Tuple3' a b c)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall a b c.
(Show a, Show b, Show c) =>
Int -> Tuple3' a b c -> ShowS
forall a b c. (Show a, Show b, Show c) => [Tuple3' a b c] -> ShowS
forall a b c. (Show a, Show b, Show c) => Tuple3' a b c -> String
showList :: [Tuple3' a b c] -> ShowS
$cshowList :: forall a b c. (Show a, Show b, Show c) => [Tuple3' a b c] -> ShowS
show :: Tuple3' a b c -> String
$cshow :: forall a b c. (Show a, Show b, Show c) => Tuple3' a b c -> String
showsPrec :: Int -> Tuple3' a b c -> ShowS
$cshowsPrec :: forall a b c.
(Show a, Show b, Show c) =>
Int -> Tuple3' a b c -> ShowS
Show
data Tuple4' a b c d = Tuple4' !a !b !c !d deriving Int -> Tuple4' a b c d -> ShowS
[Tuple4' a b c d] -> ShowS
Tuple4' a b c d -> String
(Int -> Tuple4' a b c d -> ShowS)
-> (Tuple4' a b c d -> String)
-> ([Tuple4' a b c d] -> ShowS)
-> Show (Tuple4' a b c d)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall a b c d.
(Show a, Show b, Show c, Show d) =>
Int -> Tuple4' a b c d -> ShowS
forall a b c d.
(Show a, Show b, Show c, Show d) =>
[Tuple4' a b c d] -> ShowS
forall a b c d.
(Show a, Show b, Show c, Show d) =>
Tuple4' a b c d -> String
showList :: [Tuple4' a b c d] -> ShowS
$cshowList :: forall a b c d.
(Show a, Show b, Show c, Show d) =>
[Tuple4' a b c d] -> ShowS
show :: Tuple4' a b c d -> String
$cshow :: forall a b c d.
(Show a, Show b, Show c, Show d) =>
Tuple4' a b c d -> String
showsPrec :: Int -> Tuple4' a b c d -> ShowS
$cshowsPrec :: forall a b c d.
(Show a, Show b, Show c, Show d) =>
Int -> Tuple4' a b c d -> ShowS
Show

-------------------------------------------------------------------------------
-- Maybe
-------------------------------------------------------------------------------
--
-- | A strict 'Maybe'
data Maybe' a = Just' !a | Nothing' deriving Int -> Maybe' a -> ShowS
[Maybe' a] -> ShowS
Maybe' a -> String
(Int -> Maybe' a -> ShowS)
-> (Maybe' a -> String) -> ([Maybe' a] -> ShowS) -> Show (Maybe' a)
forall a. Show a => Int -> Maybe' a -> ShowS
forall a. Show a => [Maybe' a] -> ShowS
forall a. Show a => Maybe' a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Maybe' a] -> ShowS
$cshowList :: forall a. Show a => [Maybe' a] -> ShowS
show :: Maybe' a -> String
$cshow :: forall a. Show a => Maybe' a -> String
showsPrec :: Int -> Maybe' a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> Maybe' a -> ShowS
Show

-- XXX perhaps we can use a type class having fromStrict/toStrict operations.
--
-- | Convert strict Maybe' to lazy Maybe
{-# INLINABLE toMaybe #-}
toMaybe :: Maybe' a -> Maybe a
toMaybe :: Maybe' a -> Maybe a
toMaybe  Maybe' a
Nothing' = Maybe a
forall a. Maybe a
Nothing
toMaybe (Just' a
a) = a -> Maybe a
forall a. a -> Maybe a
Just a
a

-------------------------------------------------------------------------------
-- Either
-------------------------------------------------------------------------------
--
-- | A strict 'Either'
data Either' a b = Left' !a | Right' !b deriving Int -> Either' a b -> ShowS
[Either' a b] -> ShowS
Either' a b -> String
(Int -> Either' a b -> ShowS)
-> (Either' a b -> String)
-> ([Either' a b] -> ShowS)
-> Show (Either' a b)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall a b. (Show a, Show b) => Int -> Either' a b -> ShowS
forall a b. (Show a, Show b) => [Either' a b] -> ShowS
forall a b. (Show a, Show b) => Either' a b -> String
showList :: [Either' a b] -> ShowS
$cshowList :: forall a b. (Show a, Show b) => [Either' a b] -> ShowS
show :: Either' a b -> String
$cshow :: forall a b. (Show a, Show b) => Either' a b -> String
showsPrec :: Int -> Either' a b -> ShowS
$cshowsPrec :: forall a b. (Show a, Show b) => Int -> Either' a b -> ShowS
Show