Data.Tagged

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

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

Data.Tagged

Contents

- Tagged values
- Conversion

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

# 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"

Constructors

Tagged |
Fields unTagged :: b

Instances

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 # liftA2 :: (a -> b -> c) -> Tagged k s a -> Tagged k s b -> Tagged k s c # (*>) :: 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) #
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
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 #

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.