{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
module Servant.Server.Internal.Context where
import Data.Proxy
import GHC.TypeLits
data Context contextTypes where
EmptyContext :: Context '[]
(:.) :: x -> Context xs -> Context (x ': xs)
infixr 5 :.
instance Show (Context '[]) where
show :: Context '[] -> String
show Context '[]
EmptyContext = String
"EmptyContext"
instance (Show a, Show (Context as)) => Show (Context (a ': as)) where
showsPrec :: Int -> Context (a : as) -> ShowS
showsPrec Int
outerPrecedence (x
a :. Context xs
as) =
Bool -> ShowS -> ShowS
showParen (Int
outerPrecedence forall a. Ord a => a -> a -> Bool
> Int
5) forall a b. (a -> b) -> a -> b
$
forall a. Show a => a -> ShowS
shows x
a forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
" :. " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> ShowS
shows Context xs
as
instance Eq (Context '[]) where
Context '[]
_ == :: Context '[] -> Context '[] -> Bool
== Context '[]
_ = Bool
True
instance (Eq a, Eq (Context as)) => Eq (Context (a ': as)) where
x
x1 :. Context xs
y1 == :: Context (a : as) -> Context (a : as) -> Bool
== x
x2 :. Context xs
y2 = x
x1 forall a. Eq a => a -> a -> Bool
== x
x2 Bool -> Bool -> Bool
&& Context xs
y1 forall a. Eq a => a -> a -> Bool
== Context xs
y2
type family (.++) (l1 :: [*]) (l2 :: [*]) where
'[] .++ a = a
(a ': as) .++ b = a ': (as .++ b)
(.++) :: Context l1 -> Context l2 -> Context (l1 .++ l2)
Context l1
EmptyContext .++ :: forall (l1 :: [*]) (l2 :: [*]).
Context l1 -> Context l2 -> Context (l1 .++ l2)
.++ Context l2
a = Context l2
a
(x
a :. Context xs
as) .++ Context l2
b = x
a forall x (xs :: [*]). x -> Context xs -> Context (x : xs)
:. (Context xs
as forall (l1 :: [*]) (l2 :: [*]).
Context l1 -> Context l2 -> Context (l1 .++ l2)
.++ Context l2
b)
class HasContextEntry (context :: [*]) (val :: *) where
getContextEntry :: Context context -> val
instance {-# OVERLAPPABLE #-}
HasContextEntry xs val => HasContextEntry (notIt ': xs) val where
getContextEntry :: Context (notIt : xs) -> val
getContextEntry (x
_ :. Context xs
xs) = forall (context :: [*]) val.
HasContextEntry context val =>
Context context -> val
getContextEntry Context xs
xs
instance {-# OVERLAPPING #-}
HasContextEntry (val ': xs) val where
getContextEntry :: Context (val : xs) -> val
getContextEntry (x
x :. Context xs
_) = x
x
data NamedContext (name :: Symbol) (subContext :: [*])
= NamedContext (Context subContext)
descendIntoNamedContext :: forall context name subContext .
HasContextEntry context (NamedContext name subContext) =>
Proxy (name :: Symbol) -> Context context -> Context subContext
descendIntoNamedContext :: forall (context :: [*]) (name :: Symbol) (subContext :: [*]).
HasContextEntry context (NamedContext name subContext) =>
Proxy name -> Context context -> Context subContext
descendIntoNamedContext Proxy name
Proxy Context context
context =
let NamedContext Context subContext
subContext = forall (context :: [*]) val.
HasContextEntry context val =>
Context context -> val
getContextEntry Context context
context :: NamedContext name subContext
in Context subContext
subContext