[Haskell-cafe] Type level variants

Tom Ellis tom-lists-haskell-cafe-2013 at jaguarpaw.co.uk
Tue Feb 27 15:10:32 UTC 2018


On Tue, Feb 27, 2018 at 02:53:33PM +0000, Brian Hurt wrote:
> I'm looking for pointers on how to do something.  What I'm trying to do: I
> want to define a newtype wrapper for database connections with a phantom
> type to control whether the connection is read-only or read-write.  So I
> have:
> 
>     newtype Conn a = Conn { unConn :: Connection }
> 
>     data ReadOnly = ReadOnly
> 
>     data ReadWrite = ReadWrite
> 
>     -- Simplifying here
>     openConn :: MonadIO m => a -> Conn a
> 
>     query :: (MonadIO m, ToRow r, FromRow s) => Conn a -> Query -> r -> m
> [s]
> 
>     execute :: (MonadIO m, ToRow r) => Conn a -> Query -> r -> m Int64
> 
> But I want to be able to restrict the type a to be either ReadOnly or
> ReadWrite.  Solutions I've come up with so far are:
[...]
> Are their alternatives I haven't considered yet?

Have you considered

   {-# LANGUAGE DataKinds #-}

   data Read = ReadOnly | ReadWrite

?


More information about the Haskell-Cafe mailing list