{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeOperators #-}
module Data.Vinyl.Recursive where
import Data.Proxy (Proxy(..))
import Data.Vinyl.Core (rpure, RecApplicative, Rec(..), Dict(..))
import Data.Vinyl.Functor (Compose(..), (:.), Lift(..), Const(..))
import Data.Vinyl.TypeLevel
rappend
:: Rec f as
-> Rec f bs
-> Rec f (as ++ bs)
rappend RNil ys = ys
rappend (x :& xs) ys = x :& (xs `rappend` ys)
(<+>)
:: Rec f as
-> Rec f bs
-> Rec f (as ++ bs)
(<+>) = rappend
rmap
:: (forall x. f x -> g x)
-> Rec f rs
-> Rec g rs
rmap _ RNil = RNil
rmap η (x :& xs) = η x :& (η `rmap` xs)
{-# INLINE rmap #-}
(<<$>>)
:: (forall x. f x -> g x)
-> Rec f rs
-> Rec g rs
(<<$>>) = rmap
{-# INLINE (<<$>>) #-}
(<<&>>)
:: Rec f rs
-> (forall x. f x -> g x)
-> Rec g rs
xs <<&>> f = rmap f xs
{-# INLINE (<<&>>) #-}
rapply
:: Rec (Lift (->) f g) rs
-> Rec f rs
-> Rec g rs
rapply RNil RNil = RNil
rapply (f :& fs) (x :& xs) = getLift f x :& (fs `rapply` xs)
{-# INLINE rapply #-}
(<<*>>)
:: Rec (Lift (->) f g) rs
-> Rec f rs
-> Rec g rs
(<<*>>) = rapply
{-# INLINE (<<*>>) #-}
rtraverse
:: Applicative h
=> (forall x. f x -> h (g x))
-> Rec f rs
-> h (Rec g rs)
rtraverse _ RNil = pure RNil
rtraverse f (x :& xs) = (:&) <$> f x <*> rtraverse f xs
{-# INLINABLE rtraverse #-}
rzipWith
:: (forall x . f x -> g x -> h x)
-> (forall xs . Rec f xs -> Rec g xs -> Rec h xs)
rzipWith m = \r -> case r of
RNil -> \RNil -> RNil
(fa :& fas) -> \(ga :& gas) -> m fa ga :& rzipWith m fas gas
rfoldMap :: forall f m rs.
Monoid m
=> (forall x. f x -> m)
-> Rec f rs
-> m
rfoldMap f = go mempty
where
go :: forall ss. m -> Rec f ss -> m
go !m record = case record of
RNil -> m
r :& rs -> go (mappend m (f r)) rs
{-# INLINABLE go #-}
{-# INLINE rfoldMap #-}
recordToList
:: Rec (Const a) rs
-> [a]
recordToList RNil = []
recordToList (x :& xs) = getConst x : recordToList xs
reifyConstraint
:: RecAll f rs c
=> proxy c
-> Rec f rs
-> Rec (Dict c :. f) rs
reifyConstraint prx rec =
case rec of
RNil -> RNil
(x :& xs) -> Compose (Dict x) :& reifyConstraint prx xs
rpureConstrained :: forall c (f :: u -> *) proxy ts.
(AllConstrained c ts, RecApplicative ts)
=> proxy c -> (forall a. c a => f a) -> Rec f ts
rpureConstrained _ f = go (rpure Proxy)
where go :: AllConstrained c ts' => Rec Proxy ts' -> Rec f ts'
go RNil = RNil
go (_ :& xs) = f :& go xs
rpureConstraints :: forall cs (f :: * -> *) proxy ts. (AllAllSat cs ts, RecApplicative ts)
=> proxy cs -> (forall a. AllSatisfied cs a => f a) -> Rec f ts
rpureConstraints _ f = go (rpure Nothing)
where go :: AllAllSat cs ts' => Rec Maybe ts' -> Rec f ts'
go RNil = RNil
go (_ :& xs) = f :& go xs