tagged-0.8.5: Haskell 98 phantom types to avoid unsafely passing dummy arguments

Copyright | 2009-2015 Edward Kmett
License | BSD3
Maintainer | Edward Kmett <ekmett@gmail.com>
Stability | experimental
Portability | portable
Safe Haskell | Safe
Language | Haskell98

Data.Tagged

Contents

- Tagged values
- Conversion
- Proxy methods GHC dropped

Description

Synopsis

- newtype Tagged s b = Tagged {
  - unTagged :: b
  }
- retag :: Tagged s b -> Tagged t b
- untag :: Tagged s b -> b
- tagSelf :: a -> Tagged a a
- untagSelf :: Tagged a a -> a
- asTaggedTypeOf :: s -> tagged s b -> s
- witness :: Tagged a b -> a -> b
- proxy :: Tagged s a -> proxy s -> a
- unproxy :: (Proxy s -> a) -> Tagged s a
- tagWith :: proxy s -> a -> Tagged s a
- reproxy :: proxy a -> Proxy b

# Tagged values

newtype Tagged s b #

A ``Tagged` s b` value is a value `b` with an attached phantom type `s`. This can be used in place of the more traditional but less safe idiom of passing in an undefined value with the type, because unlike an `(s -> b)`, a ``Tagged` s b` can't try to use the argument `s` as a real value.

Moreover, you don't have to rely on the compiler to inline away the extra argument, because the newtype is "free"

`Tagged` has kind `k -> * -> *` if the compiler supports `PolyKinds`, therefore there is an extra `k` showing in the instance haddocks that may cause confusion.

Constructors

Tagged |
Fields unTagged :: b

Instances

Eq2 (Tagged *) # |
Methods liftEq2 :: (a -> b -> Bool) -> (c -> d -> Bool) -> Tagged * a c -> Tagged * b d -> Bool #
Ord2 (Tagged *) # |
Methods liftCompare2 :: (a -> b -> Ordering) -> (c -> d -> Ordering) -> Tagged * a c -> Tagged * b d -> Ordering #
Read2 (Tagged *) # |
Methods liftReadsPrec2 :: (Int -> ReadS a) -> ReadS [a] -> (Int -> ReadS b) -> ReadS [b] -> Int -> ReadS (Tagged * a b) # liftReadList2 :: (Int -> ReadS a) -> ReadS [a] -> (Int -> ReadS b) -> ReadS [b] -> ReadS [Tagged * a b] #
Show2 (Tagged *) # |
Methods liftShowsPrec2 :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> (Int -> b -> ShowS) -> ([b] -> ShowS) -> Int -> Tagged * a b -> ShowS # liftShowList2 :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> (Int -> b -> ShowS) -> ([b] -> ShowS) -> [Tagged * a b] -> ShowS #
Bifunctor (Tagged *) # |
Methods bimap :: (a -> b) -> (c -> d) -> Tagged * a c -> Tagged * b d # first :: (a -> b) -> Tagged * a c -> Tagged * b c # second :: (b -> c) -> Tagged * a b -> Tagged * a c #
Monad (Tagged k s) # |
Methods (>>=) :: Tagged k s a -> (a -> Tagged k s b) -> Tagged k s b # (>>) :: Tagged k s a -> Tagged k s b -> Tagged k s b # return :: a -> Tagged k s a # fail :: String -> Tagged k s a #
Functor (Tagged k s) # |
Methods fmap :: (a -> b) -> Tagged k s a -> Tagged k s b # (<$) :: a -> Tagged k s b -> Tagged k s a #
Applicative (Tagged k s) # |
Methods pure :: a -> Tagged k s a # (<*>) :: Tagged k s (a -> b) -> Tagged k s a -> Tagged k s b # (*>) :: Tagged k s a -> Tagged k s b -> Tagged k s b # (<*) :: Tagged k s a -> Tagged k s b -> Tagged k s a #
Foldable (Tagged k s) # |
Methods fold :: Monoid m => Tagged k s m -> m # foldMap :: Monoid m => (a -> m) -> Tagged k s a -> m # foldr :: (a -> b -> b) -> b -> Tagged k s a -> b # foldr' :: (a -> b -> b) -> b -> Tagged k s a -> b # foldl :: (b -> a -> b) -> b -> Tagged k s a -> b # foldl' :: (b -> a -> b) -> b -> Tagged k s a -> b # foldr1 :: (a -> a -> a) -> Tagged k s a -> a # foldl1 :: (a -> a -> a) -> Tagged k s a -> a # toList :: Tagged k s a -> [a] # null :: Tagged k s a -> Bool # length :: Tagged k s a -> Int # elem :: Eq a => a -> Tagged k s a -> Bool # maximum :: Ord a => Tagged k s a -> a # minimum :: Ord a => Tagged k s a -> a # sum :: Num a => Tagged k s a -> a # product :: Num a => Tagged k s a -> a #
Traversable (Tagged k s) # |
Methods traverse :: Applicative f => (a -> f b) -> Tagged k s a -> f (Tagged k s b) # sequenceA :: Applicative f => Tagged k s (f a) -> f (Tagged k s a) # mapM :: Monad m => (a -> m b) -> Tagged k s a -> m (Tagged k s b) # sequence :: Monad m => Tagged k s (m a) -> m (Tagged k s a) #
Generic1 (Tagged k s) # |
Associated Types type Rep1 (Tagged k s :: * -> *) :: * -> * # Methods from1 :: Tagged k s a -> Rep1 (Tagged k s) a # to1 :: Rep1 (Tagged k s) a -> Tagged k s a #
Eq1 (Tagged k s) # |
Methods liftEq :: (a -> b -> Bool) -> Tagged k s a -> Tagged k s b -> Bool #
Ord1 (Tagged k s) # |
Methods liftCompare :: (a -> b -> Ordering) -> Tagged k s a -> Tagged k s b -> Ordering #
Read1 (Tagged k s) # |
Methods liftReadsPrec :: (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (Tagged k s a) # liftReadList :: (Int -> ReadS a) -> ReadS [a] -> ReadS [Tagged k s a] #
Show1 (Tagged k s) # |
Methods liftShowsPrec :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> Tagged k s a -> ShowS # liftShowList :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> [Tagged k s a] -> ShowS #
Bounded b => Bounded (Tagged k s b) # |
Methods minBound :: Tagged k s b # maxBound :: Tagged k s b #
Enum a => Enum (Tagged k s a) # |
Methods succ :: Tagged k s a -> Tagged k s a # pred :: Tagged k s a -> Tagged k s a # toEnum :: Int -> Tagged k s a # fromEnum :: Tagged k s a -> Int # enumFrom :: Tagged k s a -> [Tagged k s a] # enumFromThen :: Tagged k s a -> Tagged k s a -> [Tagged k s a] # enumFromTo :: Tagged k s a -> Tagged k s a -> [Tagged k s a] # enumFromThenTo :: Tagged k s a -> Tagged k s a -> Tagged k s a -> [Tagged k s a] #
Eq b => Eq (Tagged k s b) # |
Methods (==) :: Tagged k s b -> Tagged k s b -> Bool # (/=) :: Tagged k s b -> Tagged k s b -> Bool #
Floating a => Floating (Tagged k s a) # |
Methods pi :: Tagged k s a # exp :: Tagged k s a -> Tagged k s a # log :: Tagged k s a -> Tagged k s a # sqrt :: Tagged k s a -> Tagged k s a # (**) :: Tagged k s a -> Tagged k s a -> Tagged k s a # logBase :: Tagged k s a -> Tagged k s a -> Tagged k s a # sin :: Tagged k s a -> Tagged k s a # cos :: Tagged k s a -> Tagged k s a # tan :: Tagged k s a -> Tagged k s a # asin :: Tagged k s a -> Tagged k s a # acos :: Tagged k s a -> Tagged k s a # atan :: Tagged k s a -> Tagged k s a # sinh :: Tagged k s a -> Tagged k s a # cosh :: Tagged k s a -> Tagged k s a # tanh :: Tagged k s a -> Tagged k s a # asinh :: Tagged k s a -> Tagged k s a # acosh :: Tagged k s a -> Tagged k s a # atanh :: Tagged k s a -> Tagged k s a # log1p :: Tagged k s a -> Tagged k s a # expm1 :: Tagged k s a -> Tagged k s a # log1pexp :: Tagged k s a -> Tagged k s a # log1mexp :: Tagged k s a -> Tagged k s a #
Fractional a => Fractional (Tagged k s a) # |
Methods (/) :: Tagged k s a -> Tagged k s a -> Tagged k s a # recip :: Tagged k s a -> Tagged k s a # fromRational :: Rational -> Tagged k s a #
Integral a => Integral (Tagged k s a) # |
Methods quot :: Tagged k s a -> Tagged k s a -> Tagged k s a # rem :: Tagged k s a -> Tagged k s a -> Tagged k s a # div :: Tagged k s a -> Tagged k s a -> Tagged k s a # mod :: Tagged k s a -> Tagged k s a -> Tagged k s a # quotRem :: Tagged k s a -> Tagged k s a -> (Tagged k s a, Tagged k s a) # divMod :: Tagged k s a -> Tagged k s a -> (Tagged k s a, Tagged k s a) # toInteger :: Tagged k s a -> Integer #
(Data s, Data b) => Data (Tagged * s b) # |
Methods gfoldl :: (forall d a. Data d => c (d -> a) -> d -> c a) -> (forall g. g -> c g) -> Tagged * s b -> c (Tagged * s b) # gunfold :: (forall a r. Data a => c (a -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (Tagged * s b) # toConstr :: Tagged * s b -> Constr # dataTypeOf :: Tagged * s b -> DataType # dataCast1 :: Typeable (* -> *) t => (forall d. Data d => c (t d)) -> Maybe (c (Tagged * s b)) # dataCast2 :: Typeable (* -> * -> *) t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Tagged * s b)) # gmapT :: (forall a. Data a => a -> a) -> Tagged * s b -> Tagged * s b # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Tagged * s b -> r # gmapQr :: (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Tagged * s b -> r # gmapQ :: (forall d. Data d => d -> u) -> Tagged * s b -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> Tagged * s b -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Tagged * s b -> m (Tagged * s b) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Tagged * s b -> m (Tagged * s b) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Tagged * s b -> m (Tagged * s b) #
Num a => Num (Tagged k s a) # |
Methods (+) :: Tagged k s a -> Tagged k s a -> Tagged k s a # (-) :: Tagged k s a -> Tagged k s a -> Tagged k s a # (*) :: Tagged k s a -> Tagged k s a -> Tagged k s a # negate :: Tagged k s a -> Tagged k s a # abs :: Tagged k s a -> Tagged k s a # signum :: Tagged k s a -> Tagged k s a # fromInteger :: Integer -> Tagged k s a #
Ord b => Ord (Tagged k s b) # |
Methods compare :: Tagged k s b -> Tagged k s b -> Ordering # (<) :: Tagged k s b -> Tagged k s b -> Bool # (<=) :: Tagged k s b -> Tagged k s b -> Bool # (>) :: Tagged k s b -> Tagged k s b -> Bool # (>=) :: Tagged k s b -> Tagged k s b -> Bool # max :: Tagged k s b -> Tagged k s b -> Tagged k s b # min :: Tagged k s b -> Tagged k s b -> Tagged k s b #
Read b => Read (Tagged k s b) # |
Methods readsPrec :: Int -> ReadS (Tagged k s b) # readList :: ReadS [Tagged k s b] # readPrec :: ReadPrec (Tagged k s b) # readListPrec :: ReadPrec [Tagged k s b] #
Real a => Real (Tagged k s a) # |
Methods toRational :: Tagged k s a -> Rational #
RealFloat a => RealFloat (Tagged k s a) # |
Methods floatRadix :: Tagged k s a -> Integer # floatDigits :: Tagged k s a -> Int # floatRange :: Tagged k s a -> (Int, Int) # decodeFloat :: Tagged k s a -> (Integer, Int) # encodeFloat :: Integer -> Int -> Tagged k s a # exponent :: Tagged k s a -> Int # significand :: Tagged k s a -> Tagged k s a # scaleFloat :: Int -> Tagged k s a -> Tagged k s a # isNaN :: Tagged k s a -> Bool # isInfinite :: Tagged k s a -> Bool # isDenormalized :: Tagged k s a -> Bool # isNegativeZero :: Tagged k s a -> Bool # isIEEE :: Tagged k s a -> Bool # atan2 :: Tagged k s a -> Tagged k s a -> Tagged k s a #
RealFrac a => RealFrac (Tagged k s a) # |
Methods properFraction :: Integral b => Tagged k s a -> (b, Tagged k s a) # truncate :: Integral b => Tagged k s a -> b # round :: Integral b => Tagged k s a -> b # ceiling :: Integral b => Tagged k s a -> b # floor :: Integral b => Tagged k s a -> b #
Show b => Show (Tagged k s b) # |
Methods showsPrec :: Int -> Tagged k s b -> ShowS # show :: Tagged k s b -> String # showList :: [Tagged k s b] -> ShowS #
Ix b => Ix (Tagged k s b) # |
Methods range :: (Tagged k s b, Tagged k s b) -> [Tagged k s b] # index :: (Tagged k s b, Tagged k s b) -> Tagged k s b -> Int # unsafeIndex :: (Tagged k s b, Tagged k s b) -> Tagged k s b -> Int inRange :: (Tagged k s b, Tagged k s b) -> Tagged k s b -> Bool # rangeSize :: (Tagged k s b, Tagged k s b) -> Int # unsafeRangeSize :: (Tagged k s b, Tagged k s b) -> Int
IsString a => IsString (Tagged k s a) # |
Methods fromString :: String -> Tagged k s a #
Generic (Tagged k s b) # |
Associated Types type Rep (Tagged k s b) :: * -> * # Methods from :: Tagged k s b -> Rep (Tagged k s b) x # to :: Rep (Tagged k s b) x -> Tagged k s b #
Semigroup a => Semigroup (Tagged k s a) # |
Methods (<>) :: Tagged k s a -> Tagged k s a -> Tagged k s a # sconcat :: NonEmpty (Tagged k s a) -> Tagged k s a # stimes :: Integral b => b -> Tagged k s a -> Tagged k s a #
(Semigroup a, Monoid a) => Monoid (Tagged k s a) # |
Methods mempty :: Tagged k s a # mappend :: Tagged k s a -> Tagged k s a -> Tagged k s a # mconcat :: [Tagged k s a] -> Tagged k s a #
Storable a => Storable (Tagged k s a) # |
Methods sizeOf :: Tagged k s a -> Int # alignment :: Tagged k s a -> Int # peekElemOff :: Ptr (Tagged k s a) -> Int -> IO (Tagged k s a) # pokeElemOff :: Ptr (Tagged k s a) -> Int -> Tagged k s a -> IO () # peekByteOff :: Ptr b -> Int -> IO (Tagged k s a) # pokeByteOff :: Ptr b -> Int -> Tagged k s a -> IO () # peek :: Ptr (Tagged k s a) -> IO (Tagged k s a) # poke :: Ptr (Tagged k s a) -> Tagged k s a -> IO () #
Bits a => Bits (Tagged k s a) # |
Methods (.&.) :: Tagged k s a -> Tagged k s a -> Tagged k s a # (.|.) :: Tagged k s a -> Tagged k s a -> Tagged k s a # xor :: Tagged k s a -> Tagged k s a -> Tagged k s a # complement :: Tagged k s a -> Tagged k s a # shift :: Tagged k s a -> Int -> Tagged k s a # rotate :: Tagged k s a -> Int -> Tagged k s a # zeroBits :: Tagged k s a # bit :: Int -> Tagged k s a # setBit :: Tagged k s a -> Int -> Tagged k s a # clearBit :: Tagged k s a -> Int -> Tagged k s a # complementBit :: Tagged k s a -> Int -> Tagged k s a # testBit :: Tagged k s a -> Int -> Bool # bitSizeMaybe :: Tagged k s a -> Maybe Int # bitSize :: Tagged k s a -> Int # isSigned :: Tagged k s a -> Bool # shiftL :: Tagged k s a -> Int -> Tagged k s a # unsafeShiftL :: Tagged k s a -> Int -> Tagged k s a # shiftR :: Tagged k s a -> Int -> Tagged k s a # unsafeShiftR :: Tagged k s a -> Int -> Tagged k s a # rotateL :: Tagged k s a -> Int -> Tagged k s a # rotateR :: Tagged k s a -> Int -> Tagged k s a # popCount :: Tagged k s a -> Int #
FiniteBits a => FiniteBits (Tagged k s a) # |
Methods finiteBitSize :: Tagged k s a -> Int # countLeadingZeros :: Tagged k s a -> Int # countTrailingZeros :: Tagged k s a -> Int #
NFData b => NFData (Tagged k s b) # |
Methods rnf :: Tagged k s b -> () #
type Rep1 (Tagged k s) # |
type Rep1 (Tagged k s) = D1 (MetaData "Tagged" "Data.Tagged" "tagged-0.8.5-DG0qxw3gXxx4NfDcDx4Iai" True) (C1 (MetaCons "Tagged" PrefixI True) (S1 (MetaSel (Just Symbol "unTagged") NoSourceUnpackedness NoSourceStrictness DecidedLazy) Par1))
type Rep (Tagged k s b) # |
type Rep (Tagged k s b) = D1 (MetaData "Tagged" "Data.Tagged" "tagged-0.8.5-DG0qxw3gXxx4NfDcDx4Iai" True) (C1 (MetaCons "Tagged" PrefixI True) (S1 (MetaSel (Just Symbol "unTagged") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 b)))

retag :: Tagged s b -> Tagged t b #

Some times you need to change the tag you have lying around. Idiomatic usage is to make a new combinator for the relationship between the tags that you want to enforce, and define that combinator using `retag`.

    data Succ n
    retagSucc :: Tagged n a -> Tagged (Succ n) a
    retagSucc = retag

untag :: Tagged s b -> b #

Alias for `unTagged`

tagSelf :: a -> Tagged a a #

Tag a value with its own type.

untagSelf :: Tagged a a -> a #

`untagSelf` is a type-restricted version of `untag`.

asTaggedTypeOf :: s -> tagged s b -> s #

`asTaggedTypeOf` is a type-restricted version of `const`. 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.

witness :: Tagged a b -> a -> b #

# Conversion

proxy :: Tagged s a -> proxy s -> a #

Convert from a `Tagged` representation to a representation based on a `Proxy`.

unproxy :: (Proxy s -> a) -> Tagged s a #

Convert from a representation based on a `Proxy` to a `Tagged` representation.

tagWith :: proxy s -> a -> Tagged s a #

Another way to convert a proxy to a tag.

# Proxy methods GHC dropped

reproxy :: proxy a -> Proxy b #

Some times you need to change the proxy you have lying around. Idiomatic usage is to make a new combinator for the relationship between the proxies that you want to enforce, and define that combinator using `reproxy`.

    data Succ n
    reproxySucc :: proxy n -> Proxy (Succ n)
    reproxySucc = reproxy