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

Portabilityportable
Stabilityexperimental
MaintainerEdward Kmett <ekmett@gmail.com>
Safe HaskellNone

Data.Tagged

Contents

Description

 

Synopsis

Tagged values

newtype Tagged s b Source

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

Typeable2 (Tagged *) 
Monad (Tagged k s) 
Functor (Tagged k s) 
Functor (Tagged k s) => Applicative (Tagged k s) 
Foldable (Tagged k s) 
(Functor (Tagged k s), Foldable (Tagged k s)) => Traversable (Tagged k s) 
Bounded b => Bounded (Tagged k s b) 
Enum a => Enum (Tagged k s a) 
Eq b => Eq (Tagged k s b) 
(Fractional (Tagged k s a), Floating a) => Floating (Tagged k s a) 
(Num (Tagged k s a), Fractional a) => Fractional (Tagged k s a) 
(Real (Tagged k s a), Enum (Tagged k s a), Integral a) => Integral (Tagged k s a) 
(Typeable (Tagged * s b), Data s, Data b) => Data (Tagged * s b) 
Num a => Num (Tagged k s a) 
(Eq (Tagged k s b), Ord b) => Ord (Tagged k s b) 
Read b => Read (Tagged k s b) 
(Num (Tagged k s a), Ord (Tagged k s a), Real a) => Real (Tagged k s a) 
(RealFrac (Tagged k s a), Floating (Tagged k s a), RealFloat a) => RealFloat (Tagged k s a) 
(Real (Tagged k s a), Fractional (Tagged k s a), RealFrac a) => RealFrac (Tagged k s a) 
Show b => Show (Tagged k s b) 
(Ord (Tagged k s b), Ix b) => Ix (Tagged k s b) 
Monoid a => Monoid (Tagged k s a) 

retag :: Tagged s b -> Tagged t bSource

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 -> bSource

Alias for unTagged

tagSelf :: a -> Tagged a aSource

Tag a value with its own type.

untagSelf :: Tagged a a -> aSource

untagSelf is a type-restricted version of untag.

asTaggedTypeOf :: s -> Tagged s b -> sSource

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.