{-# LANGUAGE MultiParamTypeClasses, UndecidableInstances, ScopedTypeVariables, TypeFamilies #-}
{-# LANGUAGE UndecidableSuperClasses #-}
module Data.Extensible.Class (
Extensible(..)
, piece
, pieceAssoc
, itemAt
, item
, itemAssoc
, Membership
, mkMembership
, getMemberId
, compareMembership
, leadership
, Member(..)
, remember
, type (∈)
, FindType
, Generate(..)
, Forall(..)
, ForallF
, Assoc(..)
, type (>:)
, Associate(..)
, FindAssoc
, Elaborate
, Elaborated(..)
) where
import Data.Constraint
import Data.Extensible.HList
import Data.Extensible.Internal
import Data.Extensible.Internal.Rig (Optic')
import Data.Extensible.Wrapper
import Data.Profunctor
class (Functor f, Profunctor p) => Extensible f p (t :: (k -> *) -> [k] -> *) where
type ExtensibleConstr t (h :: k -> *) (xs :: [k]) (x :: k) :: Constraint
type ExtensibleConstr t h xs x = ()
pieceAt :: ExtensibleConstr t h xs x => Membership xs x -> Optic' p f (t h xs) (h x)
piece :: (x ∈ xs, Extensible f p t, ExtensibleConstr t h xs x) => Optic' p f (t h xs) (h x)
piece = pieceAt membership
{-# INLINE piece #-}
pieceAssoc :: (Associate k v xs, Extensible f p t, ExtensibleConstr t h xs (k ':> v)) => Optic' p f (t h xs) (h (k ':> v))
pieceAssoc = pieceAt association
{-# INLINE pieceAssoc #-}
itemAt :: (Wrapper h, Extensible f p t, ExtensibleConstr t h xs x) => Membership xs x -> Optic' p f (t h xs) (Repr h x)
itemAt m = pieceAt m . _Wrapper
{-# INLINE itemAt #-}
item :: (Wrapper h, Extensible f p t, x ∈ xs, ExtensibleConstr t h xs x) => proxy x -> Optic' p f (t h xs) (Repr h x)
item p = piece . _WrapperAs p
{-# INLINE item #-}
itemAssoc :: (Wrapper h, Extensible f p t, Associate k v xs, ExtensibleConstr t h xs (k ':> v))
=> proxy k -> Optic' p f (t h xs) (Repr h (k ':> v))
itemAssoc p = pieceAssoc . _WrapperAs (proxyKey p)
{-# INLINE itemAssoc #-}
proxyKey :: proxy k -> Proxy (k ':> v)
proxyKey _ = Proxy
{-# INLINE proxyKey #-}
class Generate (xs :: [k]) where
henumerate :: (forall x. Membership xs x -> r -> r) -> r -> r
hcount :: proxy xs -> Int
hgenerateList :: Applicative f
=> (forall x. Membership xs x -> f (h x)) -> f (HList h xs)
instance Generate '[] where
henumerate _ r = r
hcount _ = 0
hgenerateList _ = pure HNil
instance Generate xs => Generate (x ': xs) where
henumerate f r = f here $ henumerate (f . navNext) r
hcount _ = 1 + hcount (Proxy :: Proxy xs)
hgenerateList f = HCons <$> f here <*> hgenerateList (f . navNext)
class (ForallF c xs, Generate xs) => Forall (c :: k -> Constraint) (xs :: [k]) where
henumerateFor :: proxy c -> proxy' xs -> (forall x. c x => Membership xs x -> r -> r) -> r -> r
hgenerateListFor :: Applicative f
=> proxy c -> (forall x. c x => Membership xs x -> f (h x)) -> f (HList h xs)
instance Forall c '[] where
henumerateFor _ _ _ r = r
hgenerateListFor _ _ = pure HNil
instance (c x, Forall c xs) => Forall c (x ': xs) where
henumerateFor p _ f r = f here $ henumerateFor p (Proxy :: Proxy xs) (f . navNext) r
hgenerateListFor p f = HCons <$> f here <*> hgenerateListFor p (f . navNext)
type family ForallF (c :: k -> Constraint) (xs :: [k]) :: Constraint where
ForallF c '[] = ()
ForallF c (x ': xs) = (c x, Forall c xs)