{-# OPTIONS_GHC -fno-omit-interface-pragmas #-}
module PlutusTx.Applicative where
import Control.Applicative (Const (..))
import Data.Functor.Identity (Identity (..))
import PlutusTx.Base
import PlutusTx.Bool (Bool)
import PlutusTx.Either (Either (..))
import PlutusTx.Functor
import PlutusTx.Maybe (Maybe (..))
import PlutusTx.Monoid (Monoid (..), mappend)
infixl 4 <*>, <*, *>
class Functor f => Applicative f where
{-# MINIMAL pure, (<*>) #-}
pure :: a -> f a
(<*>) :: f (a -> b) -> f a -> f b
{-# INLINABLE liftA2 #-}
liftA2 :: Applicative f => (a -> b -> c) -> f a -> f b -> f c
liftA2 :: (a -> b -> c) -> f a -> f b -> f c
liftA2 a -> b -> c
f f a
x = f (b -> c) -> f b -> f c
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
(<*>) ((a -> b -> c) -> f a -> f (b -> c)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b -> c
f f a
x)
{-# INLINABLE (*>) #-}
(*>) :: Applicative f => f a -> f b -> f b
f a
a1 *> :: f a -> f b -> f b
*> f b
a2 = (b -> b
forall a. a -> a
id (b -> b) -> f a -> f (b -> b)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ f a
a1) f (b -> b) -> f b -> f b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> f b
a2
{-# INLINABLE (<*) #-}
(<*) :: Applicative f => f a -> f b -> f a
<* :: f a -> f b -> f a
(<*) = (a -> b -> a) -> f a -> f b -> f a
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> b -> a
forall a b. a -> b -> a
const
{-# INLINABLE unless #-}
unless :: (Applicative f) => Bool -> f () -> f ()
unless :: Bool -> f () -> f ()
unless Bool
p f ()
s = if Bool
p then () -> f ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure () else f ()
s
instance Applicative Maybe where
{-# INLINABLE pure #-}
pure :: a -> Maybe a
pure = a -> Maybe a
forall a. a -> Maybe a
Just
{-# INLINABLE (<*>) #-}
Maybe (a -> b)
Nothing <*> :: Maybe (a -> b) -> Maybe a -> Maybe b
<*> Maybe a
_ = Maybe b
forall a. Maybe a
Nothing
Maybe (a -> b)
_ <*> Maybe a
Nothing = Maybe b
forall a. Maybe a
Nothing
Just a -> b
f <*> Just a
x = b -> Maybe b
forall a. a -> Maybe a
Just (a -> b
f a
x)
instance Applicative (Either a) where
{-# INLINABLE pure #-}
pure :: a -> Either a a
pure = a -> Either a a
forall a a. a -> Either a a
Right
{-# INLINABLE (<*>) #-}
Left a
e <*> :: Either a (a -> b) -> Either a a -> Either a b
<*> Either a a
_ = a -> Either a b
forall a b. a -> Either a b
Left a
e
Right a -> b
f <*> Either a a
r = (a -> b) -> Either a a -> Either a b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f Either a a
r
instance Applicative Identity where
{-# INLINABLE pure #-}
pure :: a -> Identity a
pure = a -> Identity a
forall a. a -> Identity a
Identity
{-# INLINABLE (<*>) #-}
Identity a -> b
f <*> :: Identity (a -> b) -> Identity a -> Identity b
<*> Identity a
a = b -> Identity b
forall a. a -> Identity a
Identity (a -> b
f a
a)
instance Monoid m => Applicative (Const m) where
{-# INLINABLE pure #-}
pure :: a -> Const m a
pure a
_ = m -> Const m a
forall k a (b :: k). a -> Const a b
Const m
forall a. Monoid a => a
mempty
{-# INLINABLE (<*>) #-}
Const m
m1 <*> :: Const m (a -> b) -> Const m a -> Const m b
<*> Const m
m2 = m -> Const m b
forall k a (b :: k). a -> Const a b
Const (m -> m -> m
forall a. Monoid a => a -> a -> a
mappend m
m1 m
m2)