tagged 0.1.0 → 0.1.1
raw patch · 2 files changed
+41/−15 lines, 2 files
Files
- Data/Tagged.hs +39/−14
- tagged.cabal +2/−1
Data/Tagged.hs view
@@ -36,7 +36,8 @@ import Data.Monoid (Monoid(..)) import Data.Default (Default(..)) import Data.Data (Data,Typeable)-import Data.Ix (Ix)+import Data.Ix (Ix(..))+import GHC.Arr (unsafeIndex, unsafeRangeSize) import Text.Read -- | A @'Tagged' s b@ value is a value @b@ with an attached phantom type @s@.@@ -127,16 +128,27 @@ asTaggedTypeOf = const {-# INLINE asTaggedTypeOf #-} --- | 'selfUntag' +-- | 'untagSelf' is a type-restricted version of 'untag'. untagSelf :: Tagged a a -> a untagSelf (Tagged x) = x {-# INLINE untagSelf #-} data Proxy p = Proxy- -- deriving (Eq,Ord,Ix,Show,Read)+ deriving (Eq,Ord,Show,Read,Data,Typeable) +instance Enum (Proxy s) where+ succ _ = error "Proxy.succ"+ pred _ = error "Proxy.pred"+ fromEnum _ = 0+ toEnum 0 = Proxy+ toEnum _ = error "Proxy.toEnum: 0 expected"+ enumFrom _ = [Proxy]+ enumFromThen _ _ = [Proxy]+ enumFromThenTo _ _ _ = [Proxy]+ enumFromTo _ _ = [Proxy]+ {- -Work around for the following GHC bug with deriving:+Work around for the following GHC bug with deriving Ix instances with a phantom type: Data/Tagged.hs:1:0: Expecting an ordinary type, but found a type of kind * -> *@@ -148,16 +160,13 @@ -} -instance Enum (Proxy s) where- succ _ = error "Proxy.succ"- pred _ = error "Proxy.pred"- fromEnum _ = 0- toEnum 0 = Proxy- toEnum _ = error "Proxy.toEnum: 0 expected"- enumFrom _ = [Proxy]- enumFromThen _ _ = [Proxy]- enumFromThenTo _ _ _ = [Proxy]- enumFromTo _ _ = [Proxy]+instance Ix (Proxy s) where+ range _ = [Proxy]+ index _ _ = 0+ unsafeIndex _ _ = 0+ inRange _ _ = True+ rangeSize _ = 1+ unsafeRangeSize _ = 1 instance Bounded (Proxy s) where minBound = Proxy@@ -215,18 +224,34 @@ def = Proxy {-# INLINE def #-} +-- | 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+-- > reproxySucc :: Proxy n -> Proxy (Succ n)+-- > reproxySucc = reproxy reproxy :: Proxy s -> Proxy t reproxy _ = Proxy {-# INLINE reproxy #-} +-- | Convert from a 'Tagged' representation to a representation +-- based on a 'Proxy'. proxy :: Tagged s a -> Proxy s -> a proxy (Tagged x) _ = x {-# INLINE proxy #-} +-- | Convert from a representation based on a 'Proxy' to a 'Tagged' +-- representation. unproxy :: (Proxy s -> a) -> Tagged s a unproxy f = Tagged (f Proxy) {-# INLINE unproxy #-} +-- | 'asProxyTypeOf' 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. asProxyTypeOf :: a -> Proxy a -> a asProxyTypeOf = const {-# INLINE asProxyTypeOf #-}
tagged.cabal view
@@ -1,5 +1,5 @@ name: tagged-version: 0.1.0+version: 0.1.1 license: BSD3 license-file: LICENSE author: Edward A. Kmett@@ -7,6 +7,7 @@ stability: experimental category: Data, Phantom Types synopsis: Provides newtype wrappers for phantom types to avoid unsafely passing dummy arguments+homepage: http://github.com/ekmett/tagged copyright: 2009 Edward A. Kmett description: Provides newtype wrappers for phantom types to avoid unsafely passing dummy arguments build-type: Simple