tagged 0.2 → 0.2.1
raw patch · 3 files changed
+61/−51 lines, 3 filesdep +semigroupsdep ~base
Dependencies added: semigroups
Dependency ranges changed: base
Files
- Data/Proxy.hs +3/−0
- Data/Tagged.hs +56/−50
- tagged.cabal +2/−1
Data/Proxy.hs view
@@ -162,3 +162,6 @@ asProxyTypeOf :: a -> Proxy a -> a asProxyTypeOf = const {-# INLINE asProxyTypeOf #-}++instance Semigroup (Proxy a) where+ Proxy <> Proxy = Proxy
Data/Tagged.hs view
@@ -1,4 +1,4 @@-{-# LANGUAGE CPP, GeneralizedNewtypeDeriving #-}+{-# LANGUAGE CPP #-} ---------------------------------------------------------------------------- -- | -- Module : Data.Tagged@@ -14,7 +14,8 @@ module Data.Tagged ( -- * Tagged values- Tagged(..)+ Tagged+ , TaggedT(..) , retag , untag , tagSelf@@ -28,6 +29,7 @@ import Data.Foldable (Foldable(..)) import Data.Data (Data,Typeable) import Data.Ix (Ix(..))+-- import Data.Functor.Identity import Text.Read -- | A @'Tagged' s b@ value is a value @b@ with an attached phantom type @s@.@@ -38,64 +40,65 @@ -- Moreover, you don't have to rely on the compiler to inline away the extra -- argument, because the newtype is "free" -newtype Tagged s b = Tagged { unTagged :: b } deriving - ( Eq, Ord, Ix, Enum, Bounded, Num, Real, Integral, Fractional, Floating, RealFrac, RealFloat-#ifdef LANGUAGE_DeriveDataTypeable- , Data, Typeable-#endif+newtype TaggedT s m b = TaggedT { unTagged :: m b } deriving + ( Eq, Ord, Ix, Enum, Bounded +-- , Num, Real, Integral, Fractional, Floating, RealFrac, RealFloat+-- #ifdef LANGUAGE_DeriveDataTypeable+-- , Data, Typeable+-- #endif ) instance Show b => Show (Tagged s b) where- showsPrec n (Tagged b) = showParen (n > 10) $- showString "Tagged " .- showsPrec 11 b+ showsPrec n (Tagged b) = showParen (n > 10) $+ showString "Tagged " .+ showsPrec 11 b instance Read b => Read (Tagged s b) where- readPrec = parens $ prec 10 $ do- Ident "Tagged" <- lexP- Tagged <$> step readPrec+ readPrec = parens $ prec 10 $ do+ Ident "Tagged" <- lexP+ Tagged <$> step readPrec instance Functor (Tagged s) where - fmap f (Tagged x) = Tagged (f x)- {-# INLINE fmap #-}+ fmap f (Tagged x) = Tagged (f x)+ {-# INLINE fmap #-} instance Applicative (Tagged s) where- pure = Tagged- {-# INLINE pure #-}- Tagged f <*> Tagged x = Tagged (f x)- {-# INLINE (<*>) #-}+ pure = Tagged+ {-# INLINE pure #-}+ Tagged f <*> Tagged x = Tagged (f x)+ {-# INLINE (<*>) #-} instance Monad (Tagged s) where- return = Tagged- {-# INLINE return #-}- Tagged m >>= k = k m - {-# INLINE (>>=) #-}- _ >> n = n- {-# INLINE (>>) #-}+ return = Tagged+ {-# INLINE return #-}+ Tagged m >>= k = k m + {-# INLINE (>>=) #-}+ _ >> n = n+ {-# INLINE (>>) #-} instance Foldable (Tagged s) where- foldMap f (Tagged x) = f x- {-# INLINE foldMap #-}- fold (Tagged x) = x- {-# INLINE fold #-}- foldr f z (Tagged x) = f x z- {-# INLINE foldr #-}- foldl f z (Tagged x) = f z x- {-# INLINE foldl #-}- foldl1 _ (Tagged x) = x - {-# INLINE foldl1 #-}- foldr1 _ (Tagged x) = x- {-# INLINE foldr1 #-}+ foldMap f (Tagged x) = f x+ {-# INLINE foldMap #-}+ fold (Tagged x) = x+ {-# INLINE fold #-}+ foldr f z (Tagged x) = f x z+ {-# INLINE foldr #-}+ foldl f z (Tagged x) = f z x+ {-# INLINE foldl #-}+ foldl1 _ (Tagged x) = x + {-# INLINE foldl1 #-}+ foldr1 _ (Tagged x) = x+ {-# INLINE foldr1 #-} instance Traversable (Tagged s) where- traverse f (Tagged x) = Tagged <$> f x- {-# INLINE traverse #-}- sequenceA (Tagged x) = Tagged <$> x- {-# INLINE sequenceA #-}- mapM f (Tagged x) = liftM Tagged (f x)- {-# INLINE mapM #-}- sequence (Tagged x) = liftM Tagged x- {-# INLINE sequence #-}+ traverse f (Tagged x) = Tagged <$> f x+ {-# INLINE traverse #-}+ sequenceA (Tagged x) = Tagged <$> x+ {-# INLINE sequenceA #-}+ mapM f (Tagged x) = liftM Tagged (f x)+ {-# INLINE mapM #-}+ sequence (Tagged x) = liftM Tagged x+ {-# INLINE sequence #-} -- | 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@@ -104,21 +107,21 @@ -- > data Succ n -- > retagSucc :: Tagged n a -> Tagged (Succ n) a -- > retagSucc = retag-retag :: Tagged s b -> Tagged t b-retag = Tagged . unTagged +retag :: TaggedT s m b -> TaggedT t m b+retag = TaggedT . unTaggedT {-# INLINE retag #-} -- | Alias for 'unTagged'-untag :: Tagged s b -> b+untag :: TaggedT s m b -> b untag = unTagged -- | Tag a value with its own type. tagSelf :: a -> Tagged a a-tagSelf = Tagged+tagSelf = tagged {-# INLINE tagSelf #-} -- | '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.-asTaggedTypeOf :: s -> Tagged s b -> s+asTaggedTypeOf :: s -> TaggedT s m b -> s asTaggedTypeOf = const {-# INLINE asTaggedTypeOf #-} @@ -126,4 +129,7 @@ untagSelf :: Tagged a a -> a untagSelf (Tagged x) = x {-# INLINE untagSelf #-}++instance Semigroup a => Semigroup (Tagged s a) where+ Tagged a <> Tagged b = Tagged (a <> b)
tagged.cabal view
@@ -1,5 +1,5 @@ name: tagged-version: 0.2+version: 0.2.1 license: BSD3 license-file: LICENSE author: Edward A. Kmett@@ -27,6 +27,7 @@ extensions: DeriveDataTypeable build-depends: base >= 4 && < 5,+ semigroups >= 0.4 && < 0.5, data-default >= 0.2 && < 3 exposed-modules: Data.Tagged