{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE Safe #-}
module Control.Applicative.Free
(
Ap(..)
, runAp
, runAp_
, liftAp
, iterAp
, hoistAp
, retractAp
) where
import Control.Applicative
import Control.Comonad (Comonad(..))
import Data.Functor.Apply
import Data.Foldable
import Data.Semigroup.Foldable
import Data.Functor.Classes
import Prelude hiding (null)
data Ap f a where
Pure :: a -> Ap f a
Ap :: f a -> Ap f (a -> b) -> Ap f b
runAp :: Applicative g => (forall x. f x -> g x) -> Ap f a -> g a
runAp :: forall (g :: * -> *) (f :: * -> *) a.
Applicative g =>
(forall x. f x -> g x) -> Ap f a -> g a
runAp forall x. f x -> g x
_ (Pure a
x) = forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x
runAp forall x. f x -> g x
u (Ap f a
f Ap f (a -> a)
x) = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a. a -> a
id forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall x. f x -> g x
u f a
f forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (g :: * -> *) (f :: * -> *) a.
Applicative g =>
(forall x. f x -> g x) -> Ap f a -> g a
runAp forall x. f x -> g x
u Ap f (a -> a)
x
runAp_ :: Monoid m => (forall a. f a -> m) -> Ap f b -> m
runAp_ :: forall m (f :: * -> *) b.
Monoid m =>
(forall a. f a -> m) -> Ap f b -> m
runAp_ forall a. f a -> m
f = forall {k} a (b :: k). Const a b -> a
getConst forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (g :: * -> *) (f :: * -> *) a.
Applicative g =>
(forall x. f x -> g x) -> Ap f a -> g a
runAp (forall {k} a (b :: k). a -> Const a b
Const forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. f a -> m
f)
instance Functor (Ap f) where
fmap :: forall a b. (a -> b) -> Ap f a -> Ap f b
fmap a -> b
f (Pure a
a) = forall a (f :: * -> *). a -> Ap f a
Pure (a -> b
f a
a)
fmap a -> b
f (Ap f a
x Ap f (a -> a)
y) = forall (f :: * -> *) a b. f a -> Ap f (a -> b) -> Ap f b
Ap f a
x ((a -> b
f forall b c a. (b -> c) -> (a -> b) -> a -> c
.) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ap f (a -> a)
y)
instance Apply (Ap f) where
Pure a -> b
f <.> :: forall a b. Ap f (a -> b) -> Ap f a -> Ap f b
<.> Ap f a
y = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f Ap f a
y
Ap f a
x Ap f (a -> a -> b)
y <.> Ap f a
z = forall (f :: * -> *) a b. f a -> Ap f (a -> b) -> Ap f b
Ap f a
x (forall a b c. (a -> b -> c) -> b -> a -> c
flip forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ap f (a -> a -> b)
y forall (f :: * -> *) a b. Apply f => f (a -> b) -> f a -> f b
<.> Ap f a
z)
instance Applicative (Ap f) where
pure :: forall a. a -> Ap f a
pure = forall a (f :: * -> *). a -> Ap f a
Pure
Pure a -> b
f <*> :: forall a b. Ap f (a -> b) -> Ap f a -> Ap f b
<*> Ap f a
y = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f Ap f a
y
Ap f a
x Ap f (a -> a -> b)
y <*> Ap f a
z = forall (f :: * -> *) a b. f a -> Ap f (a -> b) -> Ap f b
Ap f a
x (forall a b c. (a -> b -> c) -> b -> a -> c
flip forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ap f (a -> a -> b)
y forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Ap f a
z)
instance Comonad f => Comonad (Ap f) where
extract :: forall a. Ap f a -> a
extract (Pure a
a) = a
a
extract (Ap f a
x Ap f (a -> a)
y) = forall (w :: * -> *) a. Comonad w => w a -> a
extract Ap f (a -> a)
y (forall (w :: * -> *) a. Comonad w => w a -> a
extract f a
x)
duplicate :: forall a. Ap f a -> Ap f (Ap f a)
duplicate (Pure a
a) = forall a (f :: * -> *). a -> Ap f a
Pure (forall a (f :: * -> *). a -> Ap f a
Pure a
a)
duplicate (Ap f a
x Ap f (a -> a)
y) = forall (f :: * -> *) a b. f a -> Ap f (a -> b) -> Ap f b
Ap (forall (w :: * -> *) a. Comonad w => w a -> w (w a)
duplicate f a
x) (forall (w :: * -> *) a b. Comonad w => (w a -> b) -> w a -> w b
extend (forall a b c. (a -> b -> c) -> b -> a -> c
flip forall (f :: * -> *) a b. f a -> Ap f (a -> b) -> Ap f b
Ap) Ap f (a -> a)
y)
instance Foldable f => Foldable (Ap f) where
foldMap :: forall m a. Monoid m => (a -> m) -> Ap f a -> m
foldMap a -> m
f (Pure a
a) = a -> m
f a
a
foldMap a -> m
f (Ap f a
x Ap f (a -> a)
y) = forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (\a
a -> forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (\a -> a
g -> a -> m
f (a -> a
g a
a)) Ap f (a -> a)
y) f a
x
null :: forall a. Ap f a -> Bool
null (Pure a
_) = Bool
False
null (Ap f a
x Ap f (a -> a)
y) = forall (t :: * -> *) a. Foldable t => t a -> Bool
null f a
x Bool -> Bool -> Bool
|| forall (t :: * -> *) a. Foldable t => t a -> Bool
null Ap f (a -> a)
y
length :: forall a. Ap f a -> Int
length = forall (t :: * -> *) a. Foldable t => Int -> Ap t a -> Int
go Int
1
where
go :: Foldable t => Int -> Ap t a -> Int
go :: forall (t :: * -> *) a. Foldable t => Int -> Ap t a -> Int
go Int
n (Pure a
_) = Int
n
go Int
n (Ap t a
x Ap t (a -> a)
y) = case Int
n forall a. Num a => a -> a -> a
* forall (t :: * -> *) a. Foldable t => t a -> Int
length t a
x of
Int
0 -> Int
0
Int
n' -> forall (t :: * -> *) a. Foldable t => Int -> Ap t a -> Int
go Int
n' Ap t (a -> a)
y
instance Foldable1 f => Foldable1 (Ap f) where
foldMap1 :: forall m a. Semigroup m => (a -> m) -> Ap f a -> m
foldMap1 a -> m
f (Pure a
a) = a -> m
f a
a
foldMap1 a -> m
f (Ap f a
x Ap f (a -> a)
y) = forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 (\a
a -> forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 (\a -> a
g -> a -> m
f (a -> a
g a
a)) Ap f (a -> a)
y) f a
x
boringEqAp :: Eq1 f => Ap f a -> Ap f b -> Bool
boringEqAp :: forall (f :: * -> *) a b. Eq1 f => Ap f a -> Ap f b -> Bool
boringEqAp (Pure a
_) (Pure b
_) = Bool
True
boringEqAp (Ap f a
x1 Ap f (a -> a)
y1) (Ap f a
x2 Ap f (a -> b)
y2) = forall (f :: * -> *) a b. Eq1 f => f a -> f b -> Bool
boringEq f a
x1 f a
x2 Bool -> Bool -> Bool
&& forall (f :: * -> *) a b. Eq1 f => Ap f a -> Ap f b -> Bool
boringEqAp Ap f (a -> a)
y1 Ap f (a -> b)
y2
boringEqAp Ap f a
_ Ap f b
_ = Bool
False
liftEqAp :: Eq1 f => (a -> b -> Bool) -> Ap f a -> Ap f b -> Bool
liftEqAp :: forall (f :: * -> *) a b.
Eq1 f =>
(a -> b -> Bool) -> Ap f a -> Ap f b -> Bool
liftEqAp a -> b -> Bool
eq (Pure a
a1) (Pure b
a2) = a -> b -> Bool
eq a
a1 b
a2
liftEqAp a -> b -> Bool
eq (Ap f a
x1 Ap f (a -> a)
y1) (Ap f a
x2 Ap f (a -> b)
y2)
| forall (f :: * -> *) a b. Eq1 f => f a -> f b -> Bool
emptyEq f a
x1 f a
x2 = forall (f :: * -> *) a b. Eq1 f => Ap f a -> Ap f b -> Bool
boringEqAp Ap f (a -> a)
y1 Ap f (a -> b)
y2
| Bool
otherwise =
forall (f :: * -> *) a b.
Eq1 f =>
(a -> b -> Bool) -> f a -> f b -> Bool
liftEq (\a
a1 a
a2 -> forall (f :: * -> *) a b.
Eq1 f =>
(a -> b -> Bool) -> Ap f a -> Ap f b -> Bool
liftEqAp (\a -> a
g1 a -> b
g2 -> a -> b -> Bool
eq (a -> a
g1 a
a1) (a -> b
g2 a
a2)) Ap f (a -> a)
y1 Ap f (a -> b)
y2) f a
x1 f a
x2
liftEqAp a -> b -> Bool
_ Ap f a
_ Ap f b
_ = Bool
False
boringEq :: Eq1 f => f a -> f b -> Bool
boringEq :: forall (f :: * -> *) a b. Eq1 f => f a -> f b -> Bool
boringEq = forall (f :: * -> *) a b.
Eq1 f =>
(a -> b -> Bool) -> f a -> f b -> Bool
liftEq (\a
_ b
_ -> Bool
True)
emptyEq :: Eq1 f => f a -> f b -> Bool
emptyEq :: forall (f :: * -> *) a b. Eq1 f => f a -> f b -> Bool
emptyEq = forall (f :: * -> *) a b.
Eq1 f =>
(a -> b -> Bool) -> f a -> f b -> Bool
liftEq (\a
_ b
_ -> Bool
False)
instance Eq1 f => Eq1 (Ap f) where
liftEq :: forall a b. (a -> b -> Bool) -> Ap f a -> Ap f b -> Bool
liftEq = forall (f :: * -> *) a b.
Eq1 f =>
(a -> b -> Bool) -> Ap f a -> Ap f b -> Bool
liftEqAp
instance (Eq1 f, Eq a) => Eq (Ap f a) where
== :: Ap f a -> Ap f a -> Bool
(==) = forall (f :: * -> *) a. (Eq1 f, Eq a) => f a -> f a -> Bool
eq1
boringCompareAp :: Ord1 f => Ap f a -> Ap f b -> Ordering
boringCompareAp :: forall (f :: * -> *) a b. Ord1 f => Ap f a -> Ap f b -> Ordering
boringCompareAp (Pure a
_) (Pure b
_) = Ordering
EQ
boringCompareAp (Pure a
_) (Ap f a
_ Ap f (a -> b)
_) = Ordering
LT
boringCompareAp (Ap f a
x1 Ap f (a -> a)
y1) (Ap f a
x2 Ap f (a -> b)
y2) = forall (f :: * -> *) a b. Ord1 f => f a -> f b -> Ordering
boringCompare f a
x1 f a
x2 forall a. Monoid a => a -> a -> a
`mappend` forall (f :: * -> *) a b. Ord1 f => Ap f a -> Ap f b -> Ordering
boringCompareAp Ap f (a -> a)
y1 Ap f (a -> b)
y2
boringCompareAp (Ap f a
_ Ap f (a -> a)
_) (Pure b
_) = Ordering
GT
liftCompareAp :: Ord1 f => (a -> b -> Ordering) -> Ap f a -> Ap f b -> Ordering
liftCompareAp :: forall (f :: * -> *) a b.
Ord1 f =>
(a -> b -> Ordering) -> Ap f a -> Ap f b -> Ordering
liftCompareAp a -> b -> Ordering
cmp (Pure a
a1) (Pure b
a2) = a -> b -> Ordering
cmp a
a1 b
a2
liftCompareAp a -> b -> Ordering
_ (Pure a
_) (Ap f a
_ Ap f (a -> b)
_) = Ordering
LT
liftCompareAp a -> b -> Ordering
cmp (Ap f a
x1 Ap f (a -> a)
y1) (Ap f a
x2 Ap f (a -> b)
y2)
| forall (f :: * -> *) a b. Eq1 f => f a -> f b -> Bool
emptyEq f a
x1 f a
x2 = forall (f :: * -> *) a b. Ord1 f => Ap f a -> Ap f b -> Ordering
boringCompareAp Ap f (a -> a)
y1 Ap f (a -> b)
y2
| Bool
otherwise = forall (f :: * -> *) a b.
Ord1 f =>
(a -> b -> Ordering) -> f a -> f b -> Ordering
liftCompare (\a
a1 a
a2 -> forall (f :: * -> *) a b.
Ord1 f =>
(a -> b -> Ordering) -> Ap f a -> Ap f b -> Ordering
liftCompareAp (\a -> a
g1 a -> b
g2 -> a -> b -> Ordering
cmp (a -> a
g1 a
a1) (a -> b
g2 a
a2)) Ap f (a -> a)
y1 Ap f (a -> b)
y2) f a
x1 f a
x2
liftCompareAp a -> b -> Ordering
_ (Ap f a
_ Ap f (a -> a)
_) (Pure b
_) = Ordering
GT
boringCompare :: Ord1 f => f a -> f b -> Ordering
boringCompare :: forall (f :: * -> *) a b. Ord1 f => f a -> f b -> Ordering
boringCompare = forall (f :: * -> *) a b.
Ord1 f =>
(a -> b -> Ordering) -> f a -> f b -> Ordering
liftCompare (\a
_ b
_ -> Ordering
EQ)
instance Ord1 f => Ord1 (Ap f) where
liftCompare :: forall a b. (a -> b -> Ordering) -> Ap f a -> Ap f b -> Ordering
liftCompare = forall (f :: * -> *) a b.
Ord1 f =>
(a -> b -> Ordering) -> Ap f a -> Ap f b -> Ordering
liftCompareAp
instance (Ord1 f, Ord a) => Ord (Ap f a) where
compare :: Ap f a -> Ap f a -> Ordering
compare = forall (f :: * -> *) a. (Ord1 f, Ord a) => f a -> f a -> Ordering
compare1
liftAp :: f a -> Ap f a
liftAp :: forall (f :: * -> *) a. f a -> Ap f a
liftAp f a
x = forall (f :: * -> *) a b. f a -> Ap f (a -> b) -> Ap f b
Ap f a
x (forall a (f :: * -> *). a -> Ap f a
Pure forall a. a -> a
id)
{-# INLINE liftAp #-}
iterAp :: Functor g => (g a -> a) -> Ap g a -> a
iterAp :: forall (g :: * -> *) a. Functor g => (g a -> a) -> Ap g a -> a
iterAp g a -> a
algebra = Ap g a -> a
go
where go :: Ap g a -> a
go (Pure a
a) = a
a
go (Ap g a
underlying Ap g (a -> a)
apply) = g a -> a
algebra (Ap g a -> a
go forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Ap g (a -> a)
apply forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*>) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Applicative f => a -> f a
pure forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> g a
underlying)
hoistAp :: (forall a. f a -> g a) -> Ap f b -> Ap g b
hoistAp :: forall (f :: * -> *) (g :: * -> *) b.
(forall a. f a -> g a) -> Ap f b -> Ap g b
hoistAp forall a. f a -> g a
_ (Pure b
a) = forall a (f :: * -> *). a -> Ap f a
Pure b
a
hoistAp forall a. f a -> g a
f (Ap f a
x Ap f (a -> b)
y) = forall (f :: * -> *) a b. f a -> Ap f (a -> b) -> Ap f b
Ap (forall a. f a -> g a
f f a
x) (forall (f :: * -> *) (g :: * -> *) b.
(forall a. f a -> g a) -> Ap f b -> Ap g b
hoistAp forall a. f a -> g a
f Ap f (a -> b)
y)
retractAp :: Applicative f => Ap f a -> f a
retractAp :: forall (f :: * -> *) a. Applicative f => Ap f a -> f a
retractAp (Pure a
a) = forall (f :: * -> *) a. Applicative f => a -> f a
pure a
a
retractAp (Ap f a
x Ap f (a -> a)
y) = f a
x forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> forall (f :: * -> *) a. Applicative f => Ap f a -> f a
retractAp Ap f (a -> a)
y