{-# LANGUAGE DataKinds #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
module Data.Vinyl.Curry where
import Data.Kind (Type)
import Data.Vinyl
import Data.Vinyl.Functor
import Data.Vinyl.XRec
class RecordCurry ts where
rcurry :: (Rec f ts -> a) -> CurriedF f ts a
class RecordCurry' ts where
rcurry' :: (Rec Identity ts -> a) -> Curried ts a
instance RecordCurry '[] where
rcurry :: (Rec f '[] -> a) -> CurriedF f '[] a
rcurry Rec f '[] -> a
f = Rec f '[] -> a
f Rec f '[]
forall u (f :: u -> *). Rec f '[]
RNil
{-# INLINABLE rcurry #-}
instance RecordCurry' '[] where
rcurry' :: (Rec Identity '[] -> a) -> Curried '[] a
rcurry' Rec Identity '[] -> a
f = Rec Identity '[] -> a
f Rec Identity '[]
forall u (f :: u -> *). Rec f '[]
RNil
{-# INLINABLE rcurry' #-}
instance RecordCurry ts => RecordCurry (t ': ts) where
rcurry :: (Rec f (t : ts) -> a) -> CurriedF f (t : ts) a
rcurry Rec f (t : ts) -> a
f f t
x = (Rec f ts -> a) -> CurriedF f ts a
forall u (ts :: [u]) (f :: u -> *) a.
RecordCurry ts =>
(Rec f ts -> a) -> CurriedF f ts a
rcurry (\Rec f ts
xs -> Rec f (t : ts) -> a
f (f t
x f t -> Rec f ts -> Rec f (t : ts)
forall a (f :: a -> *) (r :: a) (rs :: [a]).
f r -> Rec f rs -> Rec f (r : rs)
:& Rec f ts
xs))
{-# INLINABLE rcurry #-}
instance RecordCurry' ts => RecordCurry' (t ': ts) where
rcurry' :: (Rec Identity (t : ts) -> a) -> Curried (t : ts) a
rcurry' Rec Identity (t : ts) -> a
f t
x = (Rec Identity ts -> a) -> Curried ts a
forall (ts :: [*]) a.
RecordCurry' ts =>
(Rec Identity ts -> a) -> Curried ts a
rcurry' (\Rec Identity ts
xs -> Rec Identity (t : ts) -> a
f (t -> Identity t
forall a. a -> Identity a
Identity t
x Identity t -> Rec Identity ts -> Rec Identity (t : ts)
forall a (f :: a -> *) (r :: a) (rs :: [a]).
f r -> Rec f rs -> Rec f (r : rs)
:& Rec Identity ts
xs))
{-# INLINABLE rcurry' #-}
runcurry :: CurriedF f ts a -> Rec f ts -> a
runcurry :: CurriedF f ts a -> Rec f ts -> a
runcurry CurriedF f ts a
x Rec f ts
RNil = a
CurriedF f ts a
x
runcurry CurriedF f ts a
f (f r
x :& Rec f rs
xs) = CurriedF f rs a -> Rec f rs -> a
forall u (f :: u -> *) (ts :: [u]) a.
CurriedF f ts a -> Rec f ts -> a
runcurry (CurriedF f ts a
f r -> CurriedF f rs a
f f r
x) Rec f rs
xs
{-# INLINABLE runcurry #-}
runcurry' :: Curried ts a -> Rec Identity ts -> a
runcurry' :: Curried ts a -> Rec Identity ts -> a
runcurry' Curried ts a
x Rec Identity ts
RNil = a
Curried ts a
x
runcurry' Curried ts a
f (Identity r
x :& Rec Identity rs
xs) = Curried rs a -> Rec Identity rs -> a
forall (ts :: [*]) a. Curried ts a -> Rec Identity ts -> a
runcurry' (Curried ts a
r -> Curried rs a
f r
x) Rec Identity rs
xs
{-# INLINABLE runcurry' #-}
xruncurry :: CurriedX f ts a -> XRec f ts -> a
xruncurry :: CurriedX f ts a -> XRec f ts -> a
xruncurry CurriedX f ts a
x XRec f ts
RNil = a
CurriedX f ts a
x
xruncurry CurriedX f ts a
f (XData f r
x :& Rec (XData f) rs
xs) = CurriedX f rs a -> Rec (XData f) rs -> a
forall u (f :: u -> *) (ts :: [u]) a.
CurriedX f ts a -> XRec f ts -> a
xruncurry (CurriedX f ts a
HKD f r -> CurriedX f rs a
f (XData f r -> HKD f r
forall k (t :: k -> *) (a :: k). XData t a -> HKD t a
unX XData f r
x)) Rec (XData f) rs
xs
{-# INLINABLE xruncurry #-}
runcurryX :: IsoXRec f ts => CurriedX f ts a -> Rec f ts -> a
runcurryX :: CurriedX f ts a -> Rec f ts -> a
runcurryX CurriedX f ts a
f = CurriedX f ts a -> XRec f ts -> a
forall u (f :: u -> *) (ts :: [u]) a.
CurriedX f ts a -> XRec f ts -> a
xruncurry CurriedX f ts a
f (XRec f ts -> a) -> (Rec f ts -> XRec f ts) -> Rec f ts -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rec f ts -> XRec f ts
forall u (f :: u -> *) (ts :: [u]).
IsoXRec f ts =>
Rec f ts -> XRec f ts
toXRec
{-# INLINE runcurryX #-}
runcurryA' :: (Applicative f) => Curried ts a -> Rec f ts -> f a
runcurryA' :: Curried ts a -> Rec f ts -> f a
runcurryA' Curried ts a
f = (Rec Identity ts -> a) -> f (Rec Identity ts) -> f a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Curried ts a -> Rec Identity ts -> a
forall (ts :: [*]) a. Curried ts a -> Rec Identity ts -> a
runcurry' Curried ts a
f) (f (Rec Identity ts) -> f a)
-> (Rec f ts -> f (Rec Identity ts)) -> Rec f ts -> f a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall x. f x -> f (Identity x))
-> Rec f ts -> f (Rec Identity ts)
forall u (h :: * -> *) (f :: u -> *) (g :: u -> *) (rs :: [u]).
Applicative h =>
(forall (x :: u). f x -> h (g x)) -> Rec f rs -> h (Rec g rs)
rtraverse ((x -> Identity x) -> f x -> f (Identity x)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap x -> Identity x
forall a. a -> Identity a
Identity)
{-# INLINE runcurryA' #-}
runcurryA :: (Applicative f) => CurriedF g ts a -> Rec (Compose f g) ts -> f a
runcurryA :: CurriedF g ts a -> Rec (Compose f g) ts -> f a
runcurryA CurriedF g ts a
f = (Rec g ts -> a) -> f (Rec g ts) -> f a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (CurriedF g ts a -> Rec g ts -> a
forall u (f :: u -> *) (ts :: [u]) a.
CurriedF f ts a -> Rec f ts -> a
runcurry CurriedF g ts a
f) (f (Rec g ts) -> f a)
-> (Rec (Compose f g) ts -> f (Rec g ts))
-> Rec (Compose f g) ts
-> f a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall (x :: u). Compose f g x -> f (g x))
-> Rec (Compose f g) ts -> f (Rec g ts)
forall u (h :: * -> *) (f :: u -> *) (g :: u -> *) (rs :: [u]).
Applicative h =>
(forall (x :: u). f x -> h (g x)) -> Rec f rs -> h (Rec g rs)
rtraverse forall (x :: u). Compose f g x -> f (g x)
forall l (f :: l -> *) k (g :: k -> l) (x :: k).
Compose f g x -> f (g x)
getCompose
{-# INLINE runcurryA #-}
type family Curried ts a where
Curried '[] a = a
Curried (t ': ts) a = t -> Curried ts a
type family CurriedF (f :: u -> Type) (ts :: [u]) a where
CurriedF f '[] a = a
CurriedF f (t ': ts) a = f t -> CurriedF f ts a
type family CurriedX (f :: u -> Type) (ts :: [u]) a where
CurriedX f '[] a = a
CurriedX f (t ': ts) a = HKD f t -> CurriedX f ts a