{-# LANGUAGE GADTs #-}
module Control.Applicative.Phases
( Phases(..)
, runPhasesForwards, runPhasesBackwards
, now, later, delay
) where
import Control.Applicative (liftA2, (<**>))
data Phases f a where
Lift :: f a -> Phases f a
(:<*>) :: f (a -> b) -> Phases f a -> Phases f b
runPhasesForwards :: Applicative f => Phases f a -> f a
runPhasesForwards :: forall (f :: * -> *) a. Applicative f => Phases f a -> f a
runPhasesForwards (Lift f a
ma) = f a
ma
runPhasesForwards (f (a -> a)
mg :<*> Phases f a
tx) = f (a -> a)
mg forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (f :: * -> *) a. Applicative f => Phases f a -> f a
runPhasesForwards Phases f a
tx
runPhasesBackwards :: Applicative f => Phases f a -> f a
runPhasesBackwards :: forall (f :: * -> *) a. Applicative f => Phases f a -> f a
runPhasesBackwards (Lift f a
ma) = f a
ma
runPhasesBackwards (f (a -> a)
mg :<*> Phases f a
tx) = forall (f :: * -> *) a. Applicative f => Phases f a -> f a
runPhasesBackwards Phases f a
tx forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> f (a -> a)
mg
now :: f a -> Phases f a
now :: forall (f :: * -> *) a. f a -> Phases f a
now = forall (f :: * -> *) a. f a -> Phases f a
Lift
later :: Applicative f => f a -> Phases f a
later :: forall (f :: * -> *) a. Applicative f => f a -> Phases f a
later = forall (f :: * -> *) a. Applicative f => Phases f a -> Phases f a
delay forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. f a -> Phases f a
now
delay :: Applicative f => Phases f a -> Phases f a
delay :: forall (f :: * -> *) a. Applicative f => Phases f a -> Phases f a
delay Phases f a
ta = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. a -> a
id forall (f :: * -> *) a b. f (a -> b) -> Phases f a -> Phases f b
:<*> Phases f a
ta
instance Functor f => Functor (Phases f) where
fmap :: forall a b. (a -> b) -> Phases f a -> Phases f b
fmap a -> b
f (Lift f a
ma) = forall (f :: * -> *) a. f a -> Phases f a
Lift (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f f a
ma)
fmap a -> b
f (f (a -> a)
mg :<*> Phases f a
tx) = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a -> b
fforall b c a. (b -> c) -> (a -> b) -> a -> c
.) f (a -> a)
mg forall (f :: * -> *) a b. f (a -> b) -> Phases f a -> Phases f b
:<*> Phases f a
tx
instance Applicative f => Applicative (Phases f) where
pure :: forall a. a -> Phases f a
pure = forall (f :: * -> *) a. f a -> Phases f a
now forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Applicative f => a -> f a
pure
Lift f (a -> b)
mf <*> :: forall a b. Phases f (a -> b) -> Phases f a -> Phases f b
<*> Lift f a
ma = forall (f :: * -> *) a. f a -> Phases f a
Lift forall a b. (a -> b) -> a -> b
$ f (a -> b)
mf forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> f a
ma
Lift f (a -> b)
mf <*> (f (a -> a)
mh :<*> Phases f a
ty) = forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 forall b c a. (b -> c) -> (a -> b) -> a -> c
(.) f (a -> b)
mf f (a -> a)
mh forall (f :: * -> *) a b. f (a -> b) -> Phases f a -> Phases f b
:<*> Phases f a
ty
(f (a -> a -> b)
mg :<*> Phases f a
tx) <*> Lift f a
ma = forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 forall a b c. (a -> b -> c) -> b -> a -> c
flip f (a -> a -> b)
mg f a
ma forall (f :: * -> *) a b. f (a -> b) -> Phases f a -> Phases f b
:<*> Phases f a
tx
(f (a -> a -> b)
mg :<*> Phases f a
tx) <*> (f (a -> a)
mh :<*> Phases f a
ty) = forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (\a -> a -> b
g a -> a
h ~(a
x,a
y) -> a -> a -> b
g a
x (a -> a
h a
y)) f (a -> a -> b)
mg f (a -> a)
mh forall (f :: * -> *) a b. f (a -> b) -> Phases f a -> Phases f b
:<*> forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (,) Phases f a
tx Phases f a
ty