{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE NoImplicitPrelude #-}
module Control.Applicative (
Applicative(..),
Alternative(..),
Const(..), WrappedMonad(..), WrappedArrow(..), ZipList(..),
(<$>), (<$), (<**>),
liftA, liftA3,
optional,
) where
import Control.Category hiding ((.), id)
import Control.Arrow
import Data.Maybe
import Data.Tuple
import Data.Eq
import Data.Ord
import Data.Foldable (Foldable(..))
import Data.Functor ((<$>))
import Data.Functor.Const (Const(..))
import GHC.Base
import GHC.Generics
import GHC.List (repeat, zipWith, drop)
import GHC.Read (Read)
import GHC.Show (Show)
newtype WrappedMonad m a = WrapMonad { WrappedMonad m a -> m a
unwrapMonad :: m a }
deriving ( Generic
, Generic1
, Monad
)
instance Monad m => Functor (WrappedMonad m) where
fmap :: (a -> b) -> WrappedMonad m a -> WrappedMonad m b
fmap f :: a -> b
f (WrapMonad v :: m a
v) = m b -> WrappedMonad m b
forall (m :: * -> *) a. m a -> WrappedMonad m a
WrapMonad ((a -> b) -> m a -> m b
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM a -> b
f m a
v)
instance Monad m => Applicative (WrappedMonad m) where
pure :: a -> WrappedMonad m a
pure = m a -> WrappedMonad m a
forall (m :: * -> *) a. m a -> WrappedMonad m a
WrapMonad (m a -> WrappedMonad m a) -> (a -> m a) -> a -> WrappedMonad m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
WrapMonad f :: m (a -> b)
f <*> :: WrappedMonad m (a -> b) -> WrappedMonad m a -> WrappedMonad m b
<*> WrapMonad v :: m a
v = m b -> WrappedMonad m b
forall (m :: * -> *) a. m a -> WrappedMonad m a
WrapMonad (m (a -> b)
f m (a -> b) -> m a -> m b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
`ap` m a
v)
liftA2 :: (a -> b -> c)
-> WrappedMonad m a -> WrappedMonad m b -> WrappedMonad m c
liftA2 f :: a -> b -> c
f (WrapMonad x :: m a
x) (WrapMonad y :: m b
y) = m c -> WrappedMonad m c
forall (m :: * -> *) a. m a -> WrappedMonad m a
WrapMonad ((a -> b -> c) -> m a -> m b -> m c
forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 a -> b -> c
f m a
x m b
y)
instance MonadPlus m => Alternative (WrappedMonad m) where
empty :: WrappedMonad m a
empty = m a -> WrappedMonad m a
forall (m :: * -> *) a. m a -> WrappedMonad m a
WrapMonad m a
forall (m :: * -> *) a. MonadPlus m => m a
mzero
WrapMonad u :: m a
u <|> :: WrappedMonad m a -> WrappedMonad m a -> WrappedMonad m a
<|> WrapMonad v :: m a
v = m a -> WrappedMonad m a
forall (m :: * -> *) a. m a -> WrappedMonad m a
WrapMonad (m a
u m a -> m a -> m a
forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
`mplus` m a
v)
newtype WrappedArrow a b c = WrapArrow { WrappedArrow a b c -> a b c
unwrapArrow :: a b c }
deriving ( Generic
, Generic1
)
instance Arrow a => Functor (WrappedArrow a b) where
fmap :: (a -> b) -> WrappedArrow a b a -> WrappedArrow a b b
fmap f :: a -> b
f (WrapArrow a :: a b a
a) = a b b -> WrappedArrow a b b
forall (a :: * -> * -> *) b c. a b c -> WrappedArrow a b c
WrapArrow (a b a
a a b a -> a a b -> a b b
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> (a -> b) -> a a b
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr a -> b
f)
instance Arrow a => Applicative (WrappedArrow a b) where
pure :: a -> WrappedArrow a b a
pure x :: a
x = a b a -> WrappedArrow a b a
forall (a :: * -> * -> *) b c. a b c -> WrappedArrow a b c
WrapArrow ((b -> a) -> a b a
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr (a -> b -> a
forall a b. a -> b -> a
const a
x))
liftA2 :: (a -> b -> c)
-> WrappedArrow a b a -> WrappedArrow a b b -> WrappedArrow a b c
liftA2 f :: a -> b -> c
f (WrapArrow u :: a b a
u) (WrapArrow v :: a b b
v) =
a b c -> WrappedArrow a b c
forall (a :: * -> * -> *) b c. a b c -> WrappedArrow a b c
WrapArrow (a b a
u a b a -> a b b -> a b (a, b)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& a b b
v a b (a, b) -> a (a, b) c -> a b c
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> ((a, b) -> c) -> a (a, b) c
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr ((a -> b -> c) -> (a, b) -> c
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry a -> b -> c
f))
instance (ArrowZero a, ArrowPlus a) => Alternative (WrappedArrow a b) where
empty :: WrappedArrow a b a
empty = a b a -> WrappedArrow a b a
forall (a :: * -> * -> *) b c. a b c -> WrappedArrow a b c
WrapArrow a b a
forall (a :: * -> * -> *) b c. ArrowZero a => a b c
zeroArrow
WrapArrow u :: a b a
u <|> :: WrappedArrow a b a -> WrappedArrow a b a -> WrappedArrow a b a
<|> WrapArrow v :: a b a
v = a b a -> WrappedArrow a b a
forall (a :: * -> * -> *) b c. a b c -> WrappedArrow a b c
WrapArrow (a b a
u a b a -> a b a -> a b a
forall (a :: * -> * -> *) b c.
ArrowPlus a =>
a b c -> a b c -> a b c
<+> a b a
v)
newtype ZipList a = ZipList { ZipList a -> [a]
getZipList :: [a] }
deriving ( Show
, Eq
, Ord
, Read
, Functor
, Foldable
, Generic
, Generic1
)
instance Applicative ZipList where
pure :: a -> ZipList a
pure x :: a
x = [a] -> ZipList a
forall a. [a] -> ZipList a
ZipList (a -> [a]
forall a. a -> [a]
repeat a
x)
liftA2 :: (a -> b -> c) -> ZipList a -> ZipList b -> ZipList c
liftA2 f :: a -> b -> c
f (ZipList xs :: [a]
xs) (ZipList ys :: [b]
ys) = [c] -> ZipList c
forall a. [a] -> ZipList a
ZipList ((a -> b -> c) -> [a] -> [b] -> [c]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith a -> b -> c
f [a]
xs [b]
ys)
instance Alternative ZipList where
empty :: ZipList a
empty = [a] -> ZipList a
forall a. [a] -> ZipList a
ZipList []
ZipList xs :: [a]
xs <|> :: ZipList a -> ZipList a -> ZipList a
<|> ZipList ys :: [a]
ys = [a] -> ZipList a
forall a. [a] -> ZipList a
ZipList ([a]
xs [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
drop ([a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
xs) [a]
ys)
optional :: Alternative f => f a -> f (Maybe a)
optional :: f a -> f (Maybe a)
optional v :: f a
v = a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> f a -> f (Maybe a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f a
v f (Maybe a) -> f (Maybe a) -> f (Maybe a)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Maybe a -> f (Maybe a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe a
forall a. Maybe a
Nothing