{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE UndecidableSuperClasses #-}
module Data.Extensible.Inclusion (
type (⊆)
, Include
, inclusion
, shrink
, spread
, IncludeAssoc
, Associated
, Associated'
, inclusionAssoc
, shrinkAssoc
, spreadAssoc
) where
import Data.Constraint
import Data.Extensible.Class
import Data.Extensible.Product
import Data.Extensible.Sum
import Data.Extensible.Internal
import Data.Extensible.Internal.Rig
type xs ⊆ ys = Include ys xs
type Include ys = Forall (Member ys)
inclusion :: forall xs ys. Include ys xs => Membership ys :* xs
inclusion = hrepeatFor (Proxy :: Proxy (Member ys)) membership
{-# INLINABLE inclusion #-}
shrink :: (xs ⊆ ys) => h :* ys -> h :* xs
shrink h = hmap (hindex h) inclusion
{-# INLINE shrink #-}
spread :: (xs ⊆ ys) => h :| xs -> h :| ys
spread (EmbedAt i h) = views (pieceAt i) EmbedAt inclusion h
{-# INLINE spread #-}
type family Associated' (xs :: [Assoc k v]) (t :: Assoc k v) :: Constraint where
Associated' xs (k ':> v) = Associate k v xs
class Associated' xs t => Associated xs t where
getAssociation :: Membership xs t
instance (Associated' xs t, t ~ (k ':> v)) => Associated xs t where
getAssociation = association
type IncludeAssoc ys = Forall (Associated ys)
inclusionAssoc :: forall xs ys. IncludeAssoc ys xs => Membership ys :* xs
inclusionAssoc = hrepeatFor (Proxy :: Proxy (Associated ys)) getAssociation
{-# INLINABLE inclusionAssoc #-}
shrinkAssoc :: (IncludeAssoc ys xs) => h :* ys -> h :* xs
shrinkAssoc h = hmap (hindex h) inclusionAssoc
{-# INLINE shrinkAssoc #-}
spreadAssoc :: (IncludeAssoc ys xs) => h :| xs -> h :| ys
spreadAssoc (EmbedAt i h) = views (pieceAt i) EmbedAt inclusionAssoc h
{-# INLINE spreadAssoc #-}