module Data.Vinyl.Utils.Const (
ConstApplicative(..)
, rconst
, constCommute
, rconstdist
) where
import Data.Vinyl.Utils.Compose
import Control.Applicative
import Data.Functor.Compose
import Data.Vinyl
import Data.Vinyl.TyFun
cfmap :: (a -> b) -> Rec el (Const a) rs -> Rec el (Const b) rs
cfmap _ RNil = RNil
cfmap f (Const r :& rs) = Const (f r) :& cfmap f rs
class ConstApplicative (rs :: [k]) where
cpure :: a -> Rec el (Const a) rs
instance ConstApplicative '[] where
cpure = const RNil
instance ConstApplicative rs => ConstApplicative (r ': rs) where
cpure x = Const x :& cpure x
capp :: (Rec el (Const (a -> b)) rs) -> Rec el (Const a) rs -> Rec el (Const b) rs
capp RNil RNil = RNil
capp (Const f :& fs) (Const x :& xs) = Const (f x) :& capp fs xs
rconst :: (Applicative f, RecApplicative rs) => f a -> f (Rec el (Const a) rs)
rconst = rtraverse1 . rcpure
where rcpure a = rpure (Compose $ Const <$> a)
constCommute :: Functor f => Const (f a) b -> Compose f (Const a) b
constCommute (Const a) = Compose $ Const <$> a
rconstdist :: Applicative f => Rec el (Const (f a)) rs -> f (Rec el (Const a) rs)
rconstdist rec = rtraverse1 $ constCommute <<$>> rec