{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE Safe #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE UndecidableInstances #-}
module Data.Bifunctor.Fix
( Fix(..)
) where
import Data.Biapplicative
import Data.Bifoldable
import Data.Bitraversable
import Data.Functor.Classes
import GHC.Generics
newtype Fix p a = In { forall {k} (p :: * -> k -> *) (a :: k). Fix p a -> p (Fix p a) a
out :: p (Fix p a) a }
deriving forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall k (p :: * -> k -> *) (a :: k) x. Rep (Fix p a) x -> Fix p a
forall k (p :: * -> k -> *) (a :: k) x. Fix p a -> Rep (Fix p a) x
$cto :: forall k (p :: * -> k -> *) (a :: k) x. Rep (Fix p a) x -> Fix p a
$cfrom :: forall k (p :: * -> k -> *) (a :: k) x. Fix p a -> Rep (Fix p a) x
Generic
deriving instance Eq (p (Fix p a) a) => Eq (Fix p a)
deriving instance Ord (p (Fix p a) a) => Ord (Fix p a)
deriving instance Show (p (Fix p a) a) => Show (Fix p a)
deriving instance Read (p (Fix p a) a) => Read (Fix p a)
instance Eq2 p => Eq1 (Fix p) where
liftEq :: forall a b. (a -> b -> Bool) -> Fix p a -> Fix p b -> Bool
liftEq a -> b -> Bool
f (In p (Fix p a) a
x) (In p (Fix p b) b
y) = forall (f :: * -> * -> *) a b c d.
Eq2 f =>
(a -> b -> Bool) -> (c -> d -> Bool) -> f a c -> f b d -> Bool
liftEq2 (forall (f :: * -> *) a b.
Eq1 f =>
(a -> b -> Bool) -> f a -> f b -> Bool
liftEq a -> b -> Bool
f) a -> b -> Bool
f p (Fix p a) a
x p (Fix p b) b
y
instance Ord2 p => Ord1 (Fix p) where
liftCompare :: forall a b. (a -> b -> Ordering) -> Fix p a -> Fix p b -> Ordering
liftCompare a -> b -> Ordering
f (In p (Fix p a) a
x) (In p (Fix p b) b
y) = forall (f :: * -> * -> *) a b c d.
Ord2 f =>
(a -> b -> Ordering)
-> (c -> d -> Ordering) -> f a c -> f b d -> Ordering
liftCompare2 (forall (f :: * -> *) a b.
Ord1 f =>
(a -> b -> Ordering) -> f a -> f b -> Ordering
liftCompare a -> b -> Ordering
f) a -> b -> Ordering
f p (Fix p a) a
x p (Fix p b) b
y
instance Read2 p => Read1 (Fix p) where
liftReadsPrec :: forall a. (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (Fix p a)
liftReadsPrec Int -> ReadS a
rp1 ReadS [a]
rl1 Int
p = forall a. Bool -> ReadS a -> ReadS a
readParen (Int
p forall a. Ord a => a -> a -> Bool
> Int
10) forall a b. (a -> b) -> a -> b
$ \String
s0 -> do
(String
"In", String
s1) <- ReadS String
lex String
s0
(String
"{", String
s2) <- ReadS String
lex String
s1
(String
"out", String
s3) <- ReadS String
lex String
s2
(p (Fix p a) a
x, String
s4) <- forall (f :: * -> * -> *) a b.
Read2 f =>
(Int -> ReadS a)
-> ReadS [a]
-> (Int -> ReadS b)
-> ReadS [b]
-> Int
-> ReadS (f a b)
liftReadsPrec2 (forall (f :: * -> *) a.
Read1 f =>
(Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (f a)
liftReadsPrec Int -> ReadS a
rp1 ReadS [a]
rl1) (forall (f :: * -> *) a.
Read1 f =>
(Int -> ReadS a) -> ReadS [a] -> ReadS [f a]
liftReadList Int -> ReadS a
rp1 ReadS [a]
rl1)
Int -> ReadS a
rp1 ReadS [a]
rl1 Int
0 String
s3
(String
"}", String
s5) <- ReadS String
lex String
s4
forall (m :: * -> *) a. Monad m => a -> m a
return (forall {k} (p :: * -> k -> *) (a :: k). p (Fix p a) a -> Fix p a
In p (Fix p a) a
x, String
s5)
instance Show2 p => Show1 (Fix p) where
liftShowsPrec :: forall a.
(Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> Fix p a -> ShowS
liftShowsPrec Int -> a -> ShowS
sp1 [a] -> ShowS
sl1 Int
p (In p (Fix p a) a
x) = Bool -> ShowS -> ShowS
showParen (Int
p forall a. Ord a => a -> a -> Bool
> Int
10) forall a b. (a -> b) -> a -> b
$
String -> ShowS
showString String
"In {out = "
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> * -> *) a b.
Show2 f =>
(Int -> a -> ShowS)
-> ([a] -> ShowS)
-> (Int -> b -> ShowS)
-> ([b] -> ShowS)
-> Int
-> f a b
-> ShowS
liftShowsPrec2 (forall (f :: * -> *) a.
Show1 f =>
(Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> f a -> ShowS
liftShowsPrec Int -> a -> ShowS
sp1 [a] -> ShowS
sl1) (forall (f :: * -> *) a.
Show1 f =>
(Int -> a -> ShowS) -> ([a] -> ShowS) -> [f a] -> ShowS
liftShowList Int -> a -> ShowS
sp1 [a] -> ShowS
sl1)
Int -> a -> ShowS
sp1 [a] -> ShowS
sl1 Int
0 p (Fix p a) a
x
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> ShowS
showChar Char
'}'
instance Bifunctor p => Functor (Fix p) where
fmap :: forall a b. (a -> b) -> Fix p a -> Fix p b
fmap a -> b
f (In p (Fix p a) a
p) = forall {k} (p :: * -> k -> *) (a :: k). p (Fix p a) a -> Fix p a
In (forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f) a -> b
f p (Fix p a) a
p)
{-# INLINE fmap #-}
instance Biapplicative p => Applicative (Fix p) where
pure :: forall a. a -> Fix p a
pure a
a = forall {k} (p :: * -> k -> *) (a :: k). p (Fix p a) a -> Fix p a
In (forall (p :: * -> * -> *) a b. Biapplicative p => a -> b -> p a b
bipure (forall (f :: * -> *) a. Applicative f => a -> f a
pure a
a) a
a)
{-# INLINE pure #-}
In p (Fix p (a -> b)) (a -> b)
p <*> :: forall a b. Fix p (a -> b) -> Fix p a -> Fix p b
<*> In p (Fix p a) a
q = forall {k} (p :: * -> k -> *) (a :: k). p (Fix p a) a -> Fix p a
In (forall (p :: * -> * -> *) a b c d e f.
Biapplicative p =>
(a -> b -> c) -> (d -> e -> f) -> p a d -> p b e -> p c f
biliftA2 forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
(<*>) forall a b. (a -> b) -> a -> b
($) p (Fix p (a -> b)) (a -> b)
p p (Fix p a) a
q)
{-# INLINE (<*>) #-}
instance Bifoldable p => Foldable (Fix p) where
foldMap :: forall m a. Monoid m => (a -> m) -> Fix p a -> m
foldMap a -> m
f (In p (Fix p a) a
p) = forall (p :: * -> * -> *) m a b.
(Bifoldable p, Monoid m) =>
(a -> m) -> (b -> m) -> p a b -> m
bifoldMap (forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap a -> m
f) a -> m
f p (Fix p a) a
p
{-# INLINE foldMap #-}
instance Bitraversable p => Traversable (Fix p) where
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Fix p a -> f (Fix p b)
traverse a -> f b
f (In p (Fix p a) a
p) = forall {k} (p :: * -> k -> *) (a :: k). p (Fix p a) a -> Fix p a
In forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (t :: * -> * -> *) (f :: * -> *) a c b d.
(Bitraversable t, Applicative f) =>
(a -> f c) -> (b -> f d) -> t a b -> f (t c d)
bitraverse (forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> f b
f) a -> f b
f p (Fix p a) a
p
{-# INLINE traverse #-}