@@ -13,7 +13,11 @@ module Streamly.Internal.Data.Fold.Tee
1313 )
1414where
1515
16+import Control.Applicative (liftA2)
1617import Data.Coerce (coerce)
18+#if __GLASGOW_HASKELL__ < 808
19+import Data.Semigroup (Semigroup(..))
20+#endif
1721import Streamly.Internal.Data.Fold.Types (Fold)
1822
1923import qualified Streamly.Internal.Data.Fold.Types as Fold
@@ -49,3 +53,106 @@ instance Monad m => Applicative (Tee m a) where
4953
5054 {-# INLINE (<*>) #-}
5155 (<*>) a b = fromFold (Fold.teeWith ($) (toFold a) (toFold b))
56+
57+-- | Combines the outputs (the type @b@) using their 'Semigroup' instances.
58+instance (Semigroup b, Monad m) => Semigroup (Tee m a b) where
59+ {-# INLINE (<>) #-}
60+ (<>) = liftA2 (<>)
61+
62+-- | Combines the outputs (the type @b@) using their 'Monoid' instances.
63+instance (Semigroup b, Monoid b, Monad m) => Monoid (Tee m a b) where
64+ {-# INLINE mempty #-}
65+ mempty = pure mempty
66+
67+ {-# INLINE mappend #-}
68+ mappend = (<>)
69+
70+-- | Combines the outputs (type @b@) using their 'Num' instances.
71+instance (Monad m, Num b) => Num (Tee m a b) where
72+ {-# INLINE fromInteger #-}
73+ fromInteger = pure . fromInteger
74+
75+ {-# INLINE negate #-}
76+ negate = fmap negate
77+
78+ {-# INLINE abs #-}
79+ abs = fmap abs
80+
81+ {-# INLINE signum #-}
82+ signum = fmap signum
83+
84+ {-# INLINE (+) #-}
85+ (+) = liftA2 (+)
86+
87+ {-# INLINE (*) #-}
88+ (*) = liftA2 (*)
89+
90+ {-# INLINE (-) #-}
91+ (-) = liftA2 (-)
92+
93+-- | Combines the outputs (type @b@) using their 'Fractional' instances.
94+instance (Monad m, Fractional b) => Fractional (Tee m a b) where
95+ {-# INLINE fromRational #-}
96+ fromRational = pure . fromRational
97+
98+ {-# INLINE recip #-}
99+ recip = fmap recip
100+
101+ {-# INLINE (/) #-}
102+ (/) = liftA2 (/)
103+
104+-- | Combines the outputs using their 'Floating' instances.
105+instance (Monad m, Floating b) => Floating (Tee m a b) where
106+ {-# INLINE pi #-}
107+ pi = pure pi
108+
109+ {-# INLINE exp #-}
110+ exp = fmap exp
111+
112+ {-# INLINE sqrt #-}
113+ sqrt = fmap sqrt
114+
115+ {-# INLINE log #-}
116+ log = fmap log
117+
118+ {-# INLINE sin #-}
119+ sin = fmap sin
120+
121+ {-# INLINE tan #-}
122+ tan = fmap tan
123+
124+ {-# INLINE cos #-}
125+ cos = fmap cos
126+
127+ {-# INLINE asin #-}
128+ asin = fmap asin
129+
130+ {-# INLINE atan #-}
131+ atan = fmap atan
132+
133+ {-# INLINE acos #-}
134+ acos = fmap acos
135+
136+ {-# INLINE sinh #-}
137+ sinh = fmap sinh
138+
139+ {-# INLINE tanh #-}
140+ tanh = fmap tanh
141+
142+ {-# INLINE cosh #-}
143+ cosh = fmap cosh
144+
145+ {-# INLINE asinh #-}
146+ asinh = fmap asinh
147+
148+ {-# INLINE atanh #-}
149+ atanh = fmap atanh
150+
151+ {-# INLINE acosh #-}
152+ acosh = fmap acosh
153+
154+ {-# INLINE (**) #-}
155+ (**) = liftA2 (**)
156+
157+ {-# INLINE logBase #-}
158+ logBase = liftA2 logBase