diff options
Diffstat (limited to 'libraries/base/Data/Proxy.hs')
-rw-r--r-- | libraries/base/Data/Proxy.hs | 56 |
1 files changed, 48 insertions, 8 deletions
diff --git a/libraries/base/Data/Proxy.hs b/libraries/base/Data/Proxy.hs index d6f03548f3..557cc1e4dd 100644 --- a/libraries/base/Data/Proxy.hs +++ b/libraries/base/Data/Proxy.hs @@ -28,12 +28,38 @@ import GHC.Read import GHC.Enum import GHC.Arr --- | A concrete, poly-kinded proxy type -data Proxy t = Proxy deriving Bounded +-- $setup +-- >>> import Data.Void +-- >>> import Prelude + +-- | 'Proxy' is a type that holds no data, but has a phantom parameter of +-- arbitrary type (or even kind). Its use is to provide type information, even +-- though there is no value available of that type (or it may be too costly to +-- create one). +-- +-- Historically, @'Proxy' :: 'Proxy' a@ is a safer alternative to the +-- @'undefined :: a'@ idiom. +-- +-- >>> Proxy :: Proxy (Void, Int -> Int) +-- Proxy +-- +-- Proxy can even hold types of higher kinds, +-- +-- >>> Proxy :: Proxy Either +-- Proxy +-- +-- >>> Proxy :: Proxy Functor +-- Proxy +-- +-- >>> Proxy :: Proxy complicatedStructure +-- Proxy +data Proxy t = Proxy deriving ( Bounded -- ^ @since 4.7.0.0 + , Read -- ^ @since 4.7.0.0 + ) -- | A concrete, promotable proxy type, for use at the kind level -- There are no instances for this because it is intended at the kind level only -data KProxy (t :: *) = KProxy +data KProxy (t :: Type) = KProxy -- It's common to use (undefined :: Proxy t) and (Proxy :: Proxy t) -- interchangeably, so all of these instances are hand-written to be @@ -52,10 +78,6 @@ instance Show (Proxy s) where showsPrec _ _ = showString "Proxy" -- | @since 4.7.0.0 -instance Read (Proxy s) where - readsPrec d = readParen (d > 10) (\r -> [(Proxy, s) | ("Proxy",s) <- lex r ]) - --- | @since 4.7.0.0 instance Enum (Proxy s) where succ _ = errorWithoutStackTrace "Proxy.succ" pred _ = errorWithoutStackTrace "Proxy.pred" @@ -76,10 +98,15 @@ instance Ix (Proxy s) where unsafeIndex _ _ = 0 unsafeRangeSize _ = 1 +-- | @since 4.9.0.0 +instance Semigroup (Proxy s) where + _ <> _ = Proxy + sconcat _ = Proxy + stimes _ _ = Proxy + -- | @since 4.7.0.0 instance Monoid (Proxy s) where mempty = Proxy - mappend _ _ = Proxy mconcat _ = Proxy -- | @since 4.7.0.0 @@ -113,6 +140,19 @@ instance MonadPlus Proxy -- It is usually used as an infix operator, and its typing forces its first -- argument (which is usually overloaded) to have the same type as the tag -- of the second. +-- +-- >>> import Data.Word +-- >>> :type asProxyTypeOf 123 (Proxy :: Proxy Word8) +-- asProxyTypeOf 123 (Proxy :: Proxy Word8) :: Word8 +-- +-- Note the lower-case @proxy@ in the definition. This allows any type +-- constructor with just one argument to be passed to the function, for example +-- we could also write +-- +-- >>> import Data.Word +-- >>> :type asProxyTypeOf 123 (Just (undefined :: Word8)) +-- asProxyTypeOf 123 (Just (undefined :: Word8)) :: Word8 asProxyTypeOf :: a -> proxy a -> a asProxyTypeOf = const {-# INLINE asProxyTypeOf #-} + |