{-# LANGUAGE GADTs #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
module Data.Functor.Compose.List where
import Data.Type.List
data FList (fs :: [* -> *]) (a :: *) where
Id :: { unId :: a } -> FList '[] a
F :: { unF :: f a } -> FList '[f] a
FComp :: { unFComp :: FList (g ': gs) (f a) } -> FList (f ': g ': gs) a
instance Functor (FList '[]) where
fmap f = Id . f . unId
instance Functor f => Functor (FList '[f]) where
fmap f = F . fmap f . unF
instance (Functor f, Functor (FList (g ': gs))) => Functor (FList (f ': g ': gs)) where
fmap f = FComp . fmap (fmap f) . unFComp
type family PlainF (fs :: [* -> *]) (a :: *) :: *
type instance PlainF '[] a = a
type instance PlainF (f ': fs) a = PlainF fs (f a)
class IsFList fs where
fappend :: Functor (FList gs) => FList gs (FList fs a) -> FList (fs ++ gs) a
funappend :: Functor (FList gs) => FList (fs ++ gs) a -> FList gs (FList fs a)
toPlainF :: FList fs a -> PlainF fs a
fromPlainF :: PlainF fs a -> FList fs a
instance IsFList '[] where
fappend = fmap unId
funappend = fmap Id
toPlainF (Id a) = a
fromPlainF a = Id a
instance IsFList '[f] where
fappend (Id fa) = F (unF fa)
fappend f@F{} = FComp $ fmap unF f
fappend f@FComp{} = FComp $ fmap unF f
funappend fa@F{} = Id fa
funappend (FComp fga@F{}) = fmap F fga
funappend (FComp fga@FComp{}) = fmap F fga
toPlainF (F fa) = fa
fromPlainF fa = F fa
instance IsFList (g ': gs) => IsFList (f ': g ': gs) where
fappend = FComp . fappend . fmap unFComp
funappend = fmap FComp . funappend . unFComp
toPlainF (FComp fgs) = toPlainF fgs
fromPlainF fgs = FComp (fromPlainF fgs)
type f ~> g = forall a. f a -> g a