Portability | portable |
---|---|
Stability | experimental |
Maintainer | Edward Kmett <ekmett@gmail.com> |
Safe Haskell | Safe-Inferred |
Tagged values
A
value is a value Tagged
s bb
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
can't try to use the argument Tagged
s bs
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"
Typeable2 Tagged | |
Monad (Tagged s) | |
Functor (Tagged s) | |
Functor (Tagged s) => Applicative (Tagged s) | |
Foldable (Tagged s) | |
(Functor (Tagged s), Foldable (Tagged s)) => Traversable (Tagged s) | |
Bounded b => Bounded (Tagged s b) | |
Enum a => Enum (Tagged s a) | |
Eq b => Eq (Tagged s b) | |
(Fractional (Tagged s a), Floating a) => Floating (Tagged s a) | |
(Num (Tagged s a), Fractional a) => Fractional (Tagged s a) | |
(Real (Tagged s a), Enum (Tagged s a), Integral a) => Integral (Tagged s a) | |
(Typeable (Tagged s b), Data s, Data b) => Data (Tagged s b) | |
Num a => Num (Tagged s a) | |
(Eq (Tagged s b), Ord b) => Ord (Tagged s b) | |
Read b => Read (Tagged s b) | |
(Num (Tagged s a), Ord (Tagged s a), Real a) => Real (Tagged s a) | |
(RealFrac (Tagged s a), Floating (Tagged s a), RealFloat a) => RealFloat (Tagged s a) | |
(Real (Tagged s a), Fractional (Tagged s a), RealFrac a) => RealFrac (Tagged s a) | |
Show b => Show (Tagged s b) | |
(Ord (Tagged s b), Ix b) => Ix (Tagged s b) | |
Monoid a => Monoid (Tagged 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
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.