module Data.Vinyl.Utils.Const (
ConstApplicative(..)
, rconst
, constCommute
, rtraverseconst
) where
import Data.Vinyl.Utils.Compose
import Control.Applicative hiding (Const)
import Data.Vinyl.Functor
import Data.Vinyl
cfmap :: (a -> b) -> Rec (Const a) rs -> Rec (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 (Const a) rs
instance ConstApplicative '[] where
cpure = const RNil
instance ConstApplicative rs => ConstApplicative (r ': rs) where
cpure x = Const x :& cpure x
capp :: (Rec (Const (a -> b)) rs) -> Rec (Const a) rs -> Rec (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 (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
rtraverseconst :: Applicative f => Rec (Const (f a)) rs -> f (Rec (Const a) rs)
rtraverseconst rec = rtraverse1 $ constCommute <<$>> rec