{-# LANGUAGE InstanceSigs #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
-- |
-- Copyright  : (c) Ivan Perez and Manuel Baerenz, 2016
-- License    : BSD3
-- Maintainer : ivan.perez@keera.co.uk
--
-- Instance of 'ArrowChoice' for Monadic Stream Functions ('MSF').
--
-- Import this module to include that (orphan) instance.
module Data.MonadicStreamFunction.Instances.ArrowChoice where

-- External imports
import Control.Arrow (ArrowChoice (..))

-- Internal imports
import Data.MonadicStreamFunction.Core         ()
import Data.MonadicStreamFunction.InternalCore (MSF (MSF, unMSF))

-- | 'ArrowChoice' instance for MSFs.
instance Monad m => ArrowChoice (MSF m) where
  left :: MSF m a b -> MSF m (Either a c) (Either b c)
  left :: forall b c d. MSF m b c -> MSF m (Either b d) (Either c d)
left MSF m a b
sf = forall (m :: * -> *) a b. (a -> m (b, MSF m a b)) -> MSF m a b
MSF forall {b} {d}.
Either a b -> m (Either b b, MSF m (Either a d) (Either b d))
f
    where
      f :: Either a b -> m (Either b b, MSF m (Either a d) (Either b d))
f (Left a
a) = do (b
b, MSF m a b
sf') <- forall (m :: * -> *) a b. MSF m a b -> a -> m (b, MSF m a b)
unMSF MSF m a b
sf a
a
                      forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. a -> Either a b
Left b
b, forall (a :: * -> * -> *) b c d.
ArrowChoice a =>
a b c -> a (Either b d) (Either c d)
left MSF m a b
sf')
      f (Right b
c) = forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. b -> Either a b
Right b
c, forall (a :: * -> * -> *) b c d.
ArrowChoice a =>
a b c -> a (Either b d) (Either c d)
left MSF m a b
sf)