module Database.PostgreSQL.Store.Tuple (
Tuple (..),
HasElement (..),
getElement0,
getElement1,
getElement2,
getElement3,
getElement4,
getElement5,
getElement6,
getElement7,
getElement8,
getElement9,
Function,
WithTuple,
withTuple
) where
import GHC.TypeLits
import Data.Kind
import Data.List
import Data.Tagged
data Tuple (ts :: [Type]) where
Nil :: Tuple '[]
Cons :: t -> !(Tuple ts) -> Tuple (t ': ts)
class ShowElement ts where
gatherShown :: Tuple ts -> [String]
instance ShowElement '[] where
gatherShown _ = []
instance (Show t, ShowElement ts) => ShowElement (t ': ts) where
gatherShown (Cons x rest) = show x : gatherShown rest
instance (ShowElement ts) => Show (Tuple ts) where
show params = concat ["(", intercalate ", " (gatherShown params), ")"]
class HasElement (n :: Nat) (ts :: [Type]) r | n ts -> r where
getElement :: Tuple ts -> Tagged n r
instance HasElement 0 (t ': ts) t where
getElement (Cons x _) = Tagged x
instance (1 <= n, HasElement (n 1) ts r) => HasElement n (t ': ts) r where
getElement (Cons _ !xs) = retag (getElement xs :: Tagged (n 1) r)
getElement0 :: Tuple (r ': ts) -> r
getElement0 p = untag (getElement @0 p)
getElement1 :: Tuple (t0 ': r ': ts) -> r
getElement1 p = untag (getElement @1 p)
getElement2 :: Tuple (t0 ': t1 ': r ': ts) -> r
getElement2 p = untag (getElement @2 p)
getElement3 :: Tuple (t0 ': t1 ': t2 ': r ': ts) -> r
getElement3 p = untag (getElement @3 p)
getElement4 :: Tuple (t0 ': t1 ': t2 ': t3 ': r ': ts) -> r
getElement4 p = untag (getElement @4 p)
getElement5 :: Tuple (t0 ': t1 ': t2 ': t3 ': t4 ': r ': ts) -> r
getElement5 p = untag (getElement @5 p)
getElement6 :: Tuple (t0 ': t1 ': t2 ': t3 ': t4 ': t5 ': r ': ts) -> r
getElement6 p = untag (getElement @6 p)
getElement7 :: Tuple (t0 ': t1 ': t2 ': t3 ': t4 ': t5 ': t6 ': r ': ts) -> r
getElement7 p = untag (getElement @7 p)
getElement8 :: Tuple (t0 ': t1 ': t2 ': t3 ': t4 ': t5 ': t6 ': t7 ': r ': ts) -> r
getElement8 p = untag (getElement @8 p)
getElement9 :: Tuple (t0 ': t1 ': t2 ': t3 ': t4 ': t5 ': t6 ': t7 ': t8 ': r ': ts) -> r
getElement9 p = untag (getElement @9 p)
type family Function (ps :: [Type]) r where
Function '[] r = r
Function (p : ps) r = p -> Function ps r
class WithTuple (ts :: [Type]) where
withTuple :: (Tuple ts -> r) -> Function ts r
instance WithTuple '[] where
withTuple f = f Nil
instance (WithTuple ts) => WithTuple (t : ts) where
withTuple f x = withTuple (f . Cons x)