{-# LANGUAGE Safe #-}
-----------------------------------------------------------------------------
-- |
-- Copyright   :  (C) 2011-2015 Edward Kmett
-- License     :  BSD-style (see the file LICENSE)
--
-- Maintainer  :  Edward Kmett <ekmett@gmail.com>
-- Stability   :  provisional
-- Portability :  portable
--
-- Re-exports a subset of the "Data.Bifoldable1" module along with some
-- additional combinators that require 'Bifoldable1' constraints.
--
----------------------------------------------------------------------------
module Data.Semigroup.Bifoldable
  ( -- @Data.Bifoldable1@ re-exports
    Bifoldable1(bifold1, bifoldMap1)

    -- Additional @Bifoldable1@ functionality
  , bitraverse1_
  , bifor1_
  , bisequenceA1_
  , bifoldMapDefault1
  ) where

import Control.Applicative
import Data.Bifoldable
import Data.Bifoldable1
import Data.Functor.Apply
import Data.Semigroup
import Prelude hiding (foldr)

newtype Act f a = Act { forall (f :: * -> *) a. Act f a -> f a
getAct :: f a }

instance Apply f => Semigroup (Act f a) where
  Act f a
a <> :: Act f a -> Act f a -> Act f a
<> Act f a
b = forall (f :: * -> *) a. f a -> Act f a
Act (f a
a forall (f :: * -> *) a b. Apply f => f a -> f b -> f b
.> f a
b)
  {-# INLINE (<>) #-}

instance Functor f => Functor (Act f) where
  fmap :: forall a b. (a -> b) -> Act f a -> Act f b
fmap a -> b
f (Act f a
a) = forall (f :: * -> *) a. f a -> Act f a
Act (a -> b
f forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f a
a)
  {-# INLINE fmap #-}
  a
b <$ :: forall a b. a -> Act f b -> Act f a
<$ Act f b
a = forall (f :: * -> *) a. f a -> Act f a
Act (a
b forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ f b
a)
  {-# INLINE (<$) #-}

bitraverse1_ :: (Bifoldable1 t, Apply f) => (a -> f b) -> (c -> f d) -> t a c -> f ()
bitraverse1_ :: forall (t :: * -> * -> *) (f :: * -> *) a b c d.
(Bifoldable1 t, Apply f) =>
(a -> f b) -> (c -> f d) -> t a c -> f ()
bitraverse1_ a -> f b
f c -> f d
g t a c
t = forall (f :: * -> *) a. Act f a -> f a
getAct (forall (t :: * -> * -> *) m a b.
(Bifoldable1 t, Semigroup m) =>
(a -> m) -> (b -> m) -> t a b -> m
bifoldMap1 (forall (f :: * -> *) a. f a -> Act f a
Act forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Functor f => f a -> f ()
ignore forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> f b
f) (forall (f :: * -> *) a. f a -> Act f a
Act forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Functor f => f a -> f ()
ignore forall b c a. (b -> c) -> (a -> b) -> a -> c
. c -> f d
g) t a c
t)
{-# INLINE bitraverse1_ #-}

bifor1_ :: (Bifoldable1 t, Apply f) => t a c -> (a -> f b) -> (c -> f d) -> f ()
bifor1_ :: forall (t :: * -> * -> *) (f :: * -> *) a c b d.
(Bifoldable1 t, Apply f) =>
t a c -> (a -> f b) -> (c -> f d) -> f ()
bifor1_ t a c
t a -> f b
f c -> f d
g = forall (t :: * -> * -> *) (f :: * -> *) a b c d.
(Bifoldable1 t, Apply f) =>
(a -> f b) -> (c -> f d) -> t a c -> f ()
bitraverse1_ a -> f b
f c -> f d
g t a c
t
{-# INLINE bifor1_ #-}

ignore :: Functor f => f a -> f ()
ignore :: forall (f :: * -> *) a. Functor f => f a -> f ()
ignore = (() forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$)
{-# INLINE ignore #-}

bisequenceA1_ :: (Bifoldable1 t, Apply f) => t (f a) (f b) -> f ()
bisequenceA1_ :: forall (t :: * -> * -> *) (f :: * -> *) a b.
(Bifoldable1 t, Apply f) =>
t (f a) (f b) -> f ()
bisequenceA1_ t (f a) (f b)
t = forall (f :: * -> *) a. Act f a -> f a
getAct (forall (t :: * -> * -> *) m a b.
(Bifoldable1 t, Semigroup m) =>
(a -> m) -> (b -> m) -> t a b -> m
bifoldMap1 (forall (f :: * -> *) a. f a -> Act f a
Act forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Functor f => f a -> f ()
ignore) (forall (f :: * -> *) a. f a -> Act f a
Act forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Functor f => f a -> f ()
ignore) t (f a) (f b)
t)
{-# INLINE bisequenceA1_ #-}

-- | Usable default for foldMap, but only if you define bifoldMap1 yourself
bifoldMapDefault1 :: (Bifoldable1 t, Monoid m) => (a -> m) -> (b -> m) -> t a b -> m
bifoldMapDefault1 :: forall (t :: * -> * -> *) m a b.
(Bifoldable1 t, Monoid m) =>
(a -> m) -> (b -> m) -> t a b -> m
bifoldMapDefault1 a -> m
f b -> m
g = forall m. WrappedMonoid m -> m
unwrapMonoid forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (p :: * -> * -> *) m a b.
(Bifoldable p, Monoid m) =>
(a -> m) -> (b -> m) -> p a b -> m
bifoldMap (forall m. m -> WrappedMonoid m
WrapMonoid forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m
f) (forall m. m -> WrappedMonoid m
WrapMonoid forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> m
g)
{-# INLINE bifoldMapDefault1 #-}