{-# LANGUAGE RankNTypes #-}
module Control.Selective.Free (
Select (..), liftSelect,
getPure, getEffects, getNecessaryEffects, runSelect, foldSelect
) where
import Control.Selective
import Data.Functor
newtype Select f a = Select (forall g. Selective g => (forall x. f x -> g x) -> g a)
{-# ANN module "HLint: ignore Use fmap" #-}
instance Functor (Select f) where
fmap :: forall a b. (a -> b) -> Select f a -> Select f b
fmap a -> b
f (Select forall (g :: * -> *). Selective g => (forall x. f x -> g x) -> g a
x) = forall (f :: * -> *) a.
(forall (g :: * -> *).
Selective g =>
(forall x. f x -> g x) -> g a)
-> Select f a
Select forall a b. (a -> b) -> a -> b
$ \forall x. f x -> g x
k -> a -> b
f forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (g :: * -> *). Selective g => (forall x. f x -> g x) -> g a
x forall x. f x -> g x
k
instance Applicative (Select f) where
pure :: forall a. a -> Select f a
pure a
a = forall (f :: * -> *) a.
(forall (g :: * -> *).
Selective g =>
(forall x. f x -> g x) -> g a)
-> Select f a
Select forall a b. (a -> b) -> a -> b
$ \forall x. f x -> g x
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure a
a
Select forall (g :: * -> *).
Selective g =>
(forall x. f x -> g x) -> g (a -> b)
x <*> :: forall a b. Select f (a -> b) -> Select f a -> Select f b
<*> Select forall (g :: * -> *). Selective g => (forall x. f x -> g x) -> g a
y = forall (f :: * -> *) a.
(forall (g :: * -> *).
Selective g =>
(forall x. f x -> g x) -> g a)
-> Select f a
Select forall a b. (a -> b) -> a -> b
$ \forall x. f x -> g x
k -> forall (g :: * -> *).
Selective g =>
(forall x. f x -> g x) -> g (a -> b)
x forall x. f x -> g x
k forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (g :: * -> *). Selective g => (forall x. f x -> g x) -> g a
y forall x. f x -> g x
k
instance Selective (Select f) where
select :: forall a b.
Select f (Either a b) -> Select f (a -> b) -> Select f b
select (Select forall (g :: * -> *).
Selective g =>
(forall x. f x -> g x) -> g (Either a b)
x) (Select forall (g :: * -> *).
Selective g =>
(forall x. f x -> g x) -> g (a -> b)
y) = forall (f :: * -> *) a.
(forall (g :: * -> *).
Selective g =>
(forall x. f x -> g x) -> g a)
-> Select f a
Select forall a b. (a -> b) -> a -> b
$ \forall x. f x -> g x
k -> forall (g :: * -> *).
Selective g =>
(forall x. f x -> g x) -> g (Either a b)
x forall x. f x -> g x
k forall (f :: * -> *) a b.
Selective f =>
f (Either a b) -> f (a -> b) -> f b
<*? forall (g :: * -> *).
Selective g =>
(forall x. f x -> g x) -> g (a -> b)
y forall x. f x -> g x
k
liftSelect :: f a -> Select f a
liftSelect :: forall (f :: * -> *) a. f a -> Select f a
liftSelect f a
x = forall (f :: * -> *) a.
(forall (g :: * -> *).
Selective g =>
(forall x. f x -> g x) -> g a)
-> Select f a
Select (\forall x. f x -> g x
f -> forall x. f x -> g x
f f a
x)
runSelect :: Selective g => (forall x. f x -> g x) -> Select f a -> g a
runSelect :: forall (g :: * -> *) (f :: * -> *) a.
Selective g =>
(forall x. f x -> g x) -> Select f a -> g a
runSelect forall x. f x -> g x
k (Select forall (g :: * -> *). Selective g => (forall x. f x -> g x) -> g a
x) = forall (g :: * -> *). Selective g => (forall x. f x -> g x) -> g a
x forall x. f x -> g x
k
foldSelect :: Monoid m => (forall x. f x -> m) -> Select f a -> m
foldSelect :: forall m (f :: * -> *) a.
Monoid m =>
(forall x. f x -> m) -> Select f a -> m
foldSelect forall x. f x -> m
f = forall m a. Over m a -> m
getOver forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (g :: * -> *) (f :: * -> *) a.
Selective g =>
(forall x. f x -> g x) -> Select f a -> g a
runSelect (forall m a. m -> Over m a
Over forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall x. f x -> m
f)
getPure :: Select f a -> Maybe a
getPure :: forall (f :: * -> *) a. Select f a -> Maybe a
getPure = forall (g :: * -> *) (f :: * -> *) a.
Selective g =>
(forall x. f x -> g x) -> Select f a -> g a
runSelect (forall a b. a -> b -> a
const forall a. Maybe a
Nothing)
getEffects :: Functor f => Select f a -> [f ()]
getEffects :: forall (f :: * -> *) a. Functor f => Select f a -> [f ()]
getEffects = forall m (f :: * -> *) a.
Monoid m =>
(forall x. f x -> m) -> Select f a -> m
foldSelect (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Functor f => f a -> f ()
void)
getNecessaryEffects :: Functor f => Select f a -> [f ()]
getNecessaryEffects :: forall (f :: * -> *) a. Functor f => Select f a -> [f ()]
getNecessaryEffects = forall m a. Under m a -> m
getUnder forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (g :: * -> *) (f :: * -> *) a.
Selective g =>
(forall x. f x -> g x) -> Select f a -> g a
runSelect (forall m a. m -> Under m a
Under forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Functor f => f a -> f ()
void)