{-# LANGUAGE TypeOperators, EmptyDataDecls, RankNTypes #-}
{-# LANGUAGE TypeFamilies, DataKinds, PolyKinds, KindSignatures #-}
{-# LANGUAGE GADTs, TypeInType, PatternGuards #-}
module Data.Type.RList where
import Prelude hiding (map, foldr)
import Data.Kind
import Data.Type.Equality
import Data.Proxy (Proxy(..))
import Data.Functor.Constant
import Data.Typeable
data RList a
= RNil
| (RList a) :> a
type family ((r1 :: RList k) :++: (r2 :: RList k)) :: RList k
infixr 5 :++:
type instance (r :++: 'RNil) = r
type instance (r1 :++: (r2 ':> a)) = (r1 :++: r2) ':> a
data Member (ctx :: RList k1) (a :: k2) where
Member_Base :: Member (ctx :> a) a
Member_Step :: Member ctx a -> Member (ctx :> b) a
deriving Typeable
instance Show (Member r a) where
showsPrec p = showsPrecMember (p > 10) where
showsPrecMember :: Bool -> Member ctx a -> ShowS
showsPrecMember _ Member_Base = showString "Member_Base"
showsPrecMember p (Member_Step prf) = showParen p $
showString "Member_Step" . showsPrec 10 prf
instance TestEquality (Member ctx) where
testEquality Member_Base Member_Base = Just Refl
testEquality (Member_Step memb1) (Member_Step memb2)
| Just Refl <- testEquality memb1 memb2
= Just Refl
testEquality _ _ = Nothing
instance Eq (Member ctx a) where
Member_Base == Member_Base = True
(Member_Step memb1) == (Member_Step memb2) = memb1 == memb2
_ == _ = False
weakenMemberL :: Proxy r1 -> Member r2 a -> Member (r1 :++: r2) a
weakenMemberL _ Member_Base = Member_Base
weakenMemberL tag (Member_Step mem) = Member_Step (weakenMemberL tag mem)
data Append ctx1 ctx2 ctx where
Append_Base :: Append ctx RNil ctx
Append_Step :: Append ctx1 ctx2 ctx -> Append ctx1 (ctx2 :> a) (ctx :> a)
mkAppend :: RAssign f c2 -> Append c1 c2 (c1 :++: c2)
mkAppend MNil = Append_Base
mkAppend (c :>: _) = Append_Step (mkAppend c)
mkMonoAppend :: Proxy c1 -> RAssign f c2 -> Append c1 c2 (c1 :++: c2)
mkMonoAppend _ = mkAppend
proxiesFromAppend :: Append c1 c2 c -> RAssign Proxy c2
proxiesFromAppend Append_Base = MNil
proxiesFromAppend (Append_Step a) = proxiesFromAppend a :>: Proxy
data RAssign (f :: k -> *) (c :: RList k) where
MNil :: RAssign f RNil
(:>:) :: RAssign f c -> f a -> RAssign f (c :> a)
empty :: RAssign f RNil
empty = MNil
singleton :: f a -> RAssign f (RNil :> a)
singleton x = MNil :>: x
get :: Member c a -> RAssign f c -> f a
get Member_Base (_ :>: x) = x
get (Member_Step mem') (mc :>: _) = get mem' mc
data HApply (f :: k1 -> Type) (a :: k2) where
HApply :: forall (f :: k -> Type) (a :: k). f a -> HApply f a
hget :: forall (f :: k1 -> Type) (c :: RList k1) (a :: k2).
Member c a -> RAssign f c -> HApply f a
hget Member_Base (_ :>: x) = HApply x
hget (Member_Step mem') (mc :>: _) = hget mem' mc
modify :: Member c a -> (f a -> f a) -> RAssign f c -> RAssign f c
modify Member_Base f (xs :>: x) = xs :>: f x
modify (Member_Step mem') f (xs :>: x) = modify mem' f xs :>: x
set :: Member c a -> f a -> RAssign f c -> RAssign f c
set memb x = modify memb (const x)
memberElem :: TestEquality f => f a -> RAssign f ctx -> Maybe (Member ctx a)
memberElem _ MNil = Nothing
memberElem x (_ :>: y) | Just Refl <- testEquality x y = Just Member_Base
memberElem x (xs :>: _) = fmap Member_Step $ memberElem x xs
map :: (forall x. f x -> g x) -> RAssign f c -> RAssign g c
map _ MNil = MNil
map f (mc :>: x) = map f mc :>: f x
mapRAssign :: (forall x. f x -> g x) -> RAssign f c -> RAssign g c
mapRAssign = map
map2 :: (forall x. f x -> g x -> h x) ->
RAssign f c -> RAssign g c -> RAssign h c
map2 _ MNil MNil = MNil
map2 f (xs :>: x) (ys :>: y) = map2 f xs ys :>: f x y
tail :: RAssign f (ctx :> a) -> RAssign f ctx
tail (xs :>: _) = xs
toList :: RAssign (Constant a) c -> [a]
toList = mapToList getConstant
mapToList :: (forall a. f a -> b) -> RAssign f ctx -> [b]
mapToList _ MNil = []
mapToList f (xs :>: x) = mapToList f xs ++ [f x]
append :: RAssign f c1 -> RAssign f c2 -> RAssign f (c1 :++: c2)
append mc MNil = mc
append mc1 (mc2 :>: x) = append mc1 mc2 :>: x
foldr :: (forall a. f a -> r -> r) -> r -> RAssign f ctx -> r
foldr _ r MNil = r
foldr f r (xs :>: x) = f x $ foldr f r xs
split :: (c ~ (c1 :++: c2)) => prx c1 ->
RAssign any c2 -> RAssign f c -> (RAssign f c1, RAssign f c2)
split _ MNil mc = (mc, MNil)
split _ (any :>: _) (mc :>: x) = (mc1, mc2 :>: x)
where (mc1, mc2) = split Proxy any mc
members :: RAssign f c -> RAssign (Member c) c
members MNil = MNil
members (c :>: _) = map Member_Step (members c) :>: Member_Base
class TypeCtx ctx where
typeCtxProxies :: RAssign Proxy ctx
instance TypeCtx RNil where
typeCtxProxies = MNil
instance TypeCtx ctx => TypeCtx (ctx :> a) where
typeCtxProxies = typeCtxProxies :>: Proxy