Replace hand-written Bounded instances with derived ones
[ghc.git] / libraries / base / Data / Proxy.hs
1 {-# LANGUAGE Trustworthy #-}
2 {-# LANGUAGE NoImplicitPrelude #-}
3 {-# LANGUAGE PolyKinds #-}
4
5 -----------------------------------------------------------------------------
6 -- |
7 -- Module : Data.Proxy
8 -- License : BSD-style (see the LICENSE file in the distribution)
9 --
10 -- Maintainer : libraries@haskell.org
11 -- Stability : experimental
12 -- Portability : portable
13 --
14 -- Definition of a Proxy type (poly-kinded in GHC)
15 --
16 -- @since 4.7.0.0
17 -----------------------------------------------------------------------------
18
19 module Data.Proxy
20 (
21 Proxy(..), asProxyTypeOf
22 , KProxy(..)
23 ) where
24
25 import GHC.Base
26 import GHC.Show
27 import GHC.Read
28 import GHC.Enum
29 import GHC.Arr
30
31 -- | A concrete, poly-kinded proxy type
32 data Proxy t = Proxy deriving Bounded
33
34 -- | A concrete, promotable proxy type, for use at the kind level
35 -- There are no instances for this because it is intended at the kind level only
36 data KProxy (t :: *) = KProxy
37
38 -- It's common to use (undefined :: Proxy t) and (Proxy :: Proxy t)
39 -- interchangeably, so all of these instances are hand-written to be
40 -- lazy in Proxy arguments.
41
42 instance Eq (Proxy s) where
43 _ == _ = True
44
45 instance Ord (Proxy s) where
46 compare _ _ = EQ
47
48 instance Show (Proxy s) where
49 showsPrec _ _ = showString "Proxy"
50
51 instance Read (Proxy s) where
52 readsPrec d = readParen (d > 10) (\r -> [(Proxy, s) | ("Proxy",s) <- lex r ])
53
54 instance Enum (Proxy s) where
55 succ _ = errorWithoutStackTrace "Proxy.succ"
56 pred _ = errorWithoutStackTrace "Proxy.pred"
57 fromEnum _ = 0
58 toEnum 0 = Proxy
59 toEnum _ = errorWithoutStackTrace "Proxy.toEnum: 0 expected"
60 enumFrom _ = [Proxy]
61 enumFromThen _ _ = [Proxy]
62 enumFromThenTo _ _ _ = [Proxy]
63 enumFromTo _ _ = [Proxy]
64
65 instance Ix (Proxy s) where
66 range _ = [Proxy]
67 index _ _ = 0
68 inRange _ _ = True
69 rangeSize _ = 1
70 unsafeIndex _ _ = 0
71 unsafeRangeSize _ = 1
72
73 instance Monoid (Proxy s) where
74 mempty = Proxy
75 mappend _ _ = Proxy
76 mconcat _ = Proxy
77
78 instance Functor Proxy where
79 fmap _ _ = Proxy
80 {-# INLINE fmap #-}
81
82 instance Applicative Proxy where
83 pure _ = Proxy
84 {-# INLINE pure #-}
85 _ <*> _ = Proxy
86 {-# INLINE (<*>) #-}
87
88 instance Alternative Proxy where
89 empty = Proxy
90 {-# INLINE empty #-}
91 _ <|> _ = Proxy
92 {-# INLINE (<|>) #-}
93
94 instance Monad Proxy where
95 _ >>= _ = Proxy
96 {-# INLINE (>>=) #-}
97
98 instance MonadPlus Proxy
99
100 -- | 'asProxyTypeOf' is a type-restricted version of 'const'.
101 -- It is usually used as an infix operator, and its typing forces its first
102 -- argument (which is usually overloaded) to have the same type as the tag
103 -- of the second.
104 asProxyTypeOf :: a -> Proxy a -> a
105 asProxyTypeOf = const
106 {-# INLINE asProxyTypeOf #-}