{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RoleAnnotations #-}
{-# LANGUAGE TypeOperators #-}
module Data.Record.Generic.Rep.Internal (
Rep(..)
, map'
, sequenceA
, unsafeFromList
, unsafeFromListAny
, collapse
, toListAny
, noInlineUnsafeCo
) where
import Prelude hiding (sequenceA)
import qualified Prelude
import Data.Coerce (coerce)
import Data.Foldable (toList)
import Data.Primitive.SmallArray
import Data.SOP.BasicFunctors
import GHC.Exts (Any)
import Unsafe.Coerce (unsafeCoerce)
newtype Rep f a = Rep (SmallArray (f Any))
type role Rep representational nominal
map' :: (forall x. f x -> g x) -> Rep f a -> Rep g a
map' :: (forall x. f x -> g x) -> Rep f a -> Rep g a
map' forall x. f x -> g x
f (Rep SmallArray (f Any)
v) = SmallArray (g Any) -> Rep g a
forall (f :: * -> *) a. SmallArray (f Any) -> Rep f a
Rep (SmallArray (g Any) -> Rep g a) -> SmallArray (g Any) -> Rep g a
forall a b. (a -> b) -> a -> b
$ f Any -> g Any
forall x. f x -> g x
f (f Any -> g Any) -> SmallArray (f Any) -> SmallArray (g Any)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SmallArray (f Any)
v
sequenceA :: Applicative m => Rep (m :.: f) a -> m (Rep f a)
sequenceA :: Rep (m :.: f) a -> m (Rep f a)
sequenceA (Rep SmallArray ((:.:) m f Any)
v) = SmallArray (f Any) -> Rep f a
forall (f :: * -> *) a. SmallArray (f Any) -> Rep f a
Rep (SmallArray (f Any) -> Rep f a)
-> m (SmallArray (f Any)) -> m (Rep f a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SmallArray (m (f Any)) -> m (SmallArray (f Any))
forall (t :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
Prelude.sequenceA (((:.:) m f Any -> m (f Any))
-> SmallArray ((:.:) m f Any) -> SmallArray (m (f Any))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (:.:) m f Any -> m (f Any)
forall l k (f :: l -> *) (g :: k -> l) (p :: k).
(:.:) f g p -> f (g p)
unComp SmallArray ((:.:) m f Any)
v)
collapse :: Rep (K a) b -> [a]
collapse :: Rep (K a) b -> [a]
collapse (Rep SmallArray (K a Any)
v) = [K a Any] -> [a]
coerce (SmallArray (K a Any) -> [K a Any]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList SmallArray (K a Any)
v)
toListAny :: Rep f a -> [f Any]
toListAny :: Rep f a -> [f Any]
toListAny (Rep SmallArray (f Any)
v) = SmallArray (f Any) -> [f Any]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList SmallArray (f Any)
v
unsafeFromList :: [b] -> Rep (K b) a
unsafeFromList :: [b] -> Rep (K b) a
unsafeFromList = SmallArray (K b Any) -> Rep (K b) a
forall (f :: * -> *) a. SmallArray (f Any) -> Rep f a
Rep (SmallArray (K b Any) -> Rep (K b) a)
-> ([b] -> SmallArray (K b Any)) -> [b] -> Rep (K b) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [K b Any] -> SmallArray (K b Any)
forall a. [a] -> SmallArray a
smallArrayFromList ([K b Any] -> SmallArray (K b Any))
-> ([b] -> [K b Any]) -> [b] -> SmallArray (K b Any)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (b -> K b Any) -> [b] -> [K b Any]
forall a b. (a -> b) -> [a] -> [b]
Prelude.map b -> K b Any
forall k a (b :: k). a -> K a b
K
unsafeFromListAny :: [f Any] -> Rep f a
unsafeFromListAny :: [f Any] -> Rep f a
unsafeFromListAny = SmallArray (f Any) -> Rep f a
forall (f :: * -> *) a. SmallArray (f Any) -> Rep f a
Rep (SmallArray (f Any) -> Rep f a)
-> ([f Any] -> SmallArray (f Any)) -> [f Any] -> Rep f a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [f Any] -> SmallArray (f Any)
forall a. [a] -> SmallArray a
smallArrayFromList
instance Show x => Show (Rep (K x) a) where
show :: Rep (K x) a -> String
show (Rep SmallArray (K x Any)
v) =
[x] -> String
forall a. Show a => a -> String
show ([x] -> String) -> [x] -> String
forall a b. (a -> b) -> a -> b
$ (K x Any -> x) -> [K x Any] -> [x]
forall a b. (a -> b) -> [a] -> [b]
Prelude.map K x Any -> x
forall k a (b :: k). K a b -> a
unK (SmallArray (K x Any) -> [K x Any]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList SmallArray (K x Any)
v)
instance Eq x => Eq (Rep (K x) a) where
Rep SmallArray (K x Any)
v == :: Rep (K x) a -> Rep (K x) a -> Bool
== Rep SmallArray (K x Any)
v' =
(K x Any -> x) -> [K x Any] -> [x]
forall a b. (a -> b) -> [a] -> [b]
Prelude.map K x Any -> x
forall k a (b :: k). K a b -> a
unK (SmallArray (K x Any) -> [K x Any]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList SmallArray (K x Any)
v)
[x] -> [x] -> Bool
forall a. Eq a => a -> a -> Bool
== (K x Any -> x) -> [K x Any] -> [x]
forall a b. (a -> b) -> [a] -> [b]
Prelude.map K x Any -> x
forall k a (b :: k). K a b -> a
unK (SmallArray (K x Any) -> [K x Any]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList SmallArray (K x Any)
v')
noInlineUnsafeCo :: forall a b. a -> b
{-# NOINLINE noInlineUnsafeCo #-}
noInlineUnsafeCo :: a -> b
noInlineUnsafeCo = a -> b
forall a b. a -> b
unsafeCoerce