Trailing whitespaces
[ghc.git] / libraries / base / GHC / Exts.hs
1 {-# LANGUAGE Unsafe #-}
2 {-# LANGUAGE MagicHash, UnboxedTuples, DeriveDataTypeable, TypeFamilies, MultiParamTypeClasses, FlexibleInstances #-}
3
4 -----------------------------------------------------------------------------
5 -- |
6 -- Module : GHC.Exts
7 -- Copyright : (c) The University of Glasgow 2002
8 -- License : see libraries/base/LICENSE
9 --
10 -- Maintainer : cvs-ghc@haskell.org
11 -- Stability : internal
12 -- Portability : non-portable (GHC Extensions)
13 --
14 -- GHC Extensions: this is the Approved Way to get at GHC-specific extensions.
15 --
16 -- Note: no other base module should import this module.
17 -----------------------------------------------------------------------------
18
19 module GHC.Exts
20 (
21 -- * Representations of some basic types
22 Int(..),Word(..),Float(..),Double(..),
23 Char(..),
24 Ptr(..), FunPtr(..),
25
26 -- * The maximum tuple size
27 maxTupleSize,
28
29 -- * Primitive operations
30 module GHC.Prim,
31 module GHC.PrimWrappers,
32 shiftL#, shiftRL#, iShiftL#, iShiftRA#, iShiftRL#,
33 uncheckedShiftL64#, uncheckedShiftRL64#,
34 uncheckedIShiftL64#, uncheckedIShiftRA64#,
35
36 -- * Fusion
37 build, augment,
38
39 -- * Overloaded string literals
40 IsString(..),
41
42 -- * Debugging
43 breakpoint, breakpointCond,
44
45 -- * Ids with special behaviour
46 lazy, inline,
47
48 -- * Transform comprehensions
49 Down(..), groupWith, sortWith, the,
50
51 -- * Event logging
52 traceEvent,
53
54 -- * SpecConstr annotations
55 SpecConstrAnnotation(..),
56
57 -- * The call stack
58 currentCallStack,
59
60 -- * The Constraint kind
61 Constraint,
62
63 -- * Overloaded lists
64 IsList(..)
65 ) where
66
67 import Prelude
68
69 import GHC.Prim
70 import GHC.PrimWrappers
71 import GHC.Base
72 import GHC.Word
73 import GHC.Int
74 import GHC.Ptr
75 import GHC.Stack
76 import Data.String
77 import Data.List
78 import Data.Data
79 import Data.Ord
80 import qualified Debug.Trace
81
82 -- XXX This should really be in Data.Tuple, where the definitions are
83 maxTupleSize :: Int
84 maxTupleSize = 62
85
86 -- | 'the' ensures that all the elements of the list are identical
87 -- and then returns that unique element
88 the :: Eq a => [a] -> a
89 the (x:xs)
90 | all (x ==) xs = x
91 | otherwise = error "GHC.Exts.the: non-identical elements"
92 the [] = error "GHC.Exts.the: empty list"
93
94 -- | The 'sortWith' function sorts a list of elements using the
95 -- user supplied function to project something out of each element
96 sortWith :: Ord b => (a -> b) -> [a] -> [a]
97 sortWith f = sortBy (\x y -> compare (f x) (f y))
98
99 -- | The 'groupWith' function uses the user supplied function which
100 -- projects an element out of every list element in order to first sort the
101 -- input list and then to form groups by equality on these projected elements
102 {-# INLINE groupWith #-}
103 groupWith :: Ord b => (a -> b) -> [a] -> [[a]]
104 groupWith f xs = build (\c n -> groupByFB c n (\x y -> f x == f y) (sortWith f xs))
105
106 groupByFB :: ([a] -> lst -> lst) -> lst -> (a -> a -> Bool) -> [a] -> lst
107 groupByFB c n eq xs0 = groupByFBCore xs0
108 where groupByFBCore [] = n
109 groupByFBCore (x:xs) = c (x:ys) (groupByFBCore zs)
110 where (ys, zs) = span (eq x) xs
111
112
113 -- -----------------------------------------------------------------------------
114 -- tracing
115
116 traceEvent :: String -> IO ()
117 traceEvent = Debug.Trace.traceEventIO
118 {-# DEPRECATED traceEvent "Use Debug.Trace.traceEvent or Debug.Trace.traceEventIO" #-} -- deprecated in 7.4
119
120
121 {- **********************************************************************
122 * *
123 * SpecConstr annotation *
124 * *
125 ********************************************************************** -}
126
127 -- Annotating a type with NoSpecConstr will make SpecConstr
128 -- not specialise for arguments of that type.
129
130 -- This data type is defined here, rather than in the SpecConstr module
131 -- itself, so that importing it doesn't force stupidly linking the
132 -- entire ghc package at runtime
133
134 data SpecConstrAnnotation = NoSpecConstr | ForceSpecConstr
135 deriving( Data, Typeable, Eq )
136
137
138 {- **********************************************************************
139 * *
140 * The IsList class *
141 * *
142 ********************************************************************** -}
143
144 -- | The 'IsList' class and its methods are intended to be used in
145 -- conjunction with the OverloadedLists extension.
146 class IsList l where
147 -- | The 'Item' type function returns the type of items of the structure
148 -- @l@.
149 type Item l
150
151 -- | The 'fromList' function constructs the structure @l@ from the given
152 -- list of @Item l@
153 fromList :: [Item l] -> l
154
155 -- | The 'fromListN' function takes the input list's length as a hint. Its
156 -- behaviour should be equivalent to 'fromList'. The hint can be used to
157 -- construct the structure @l@ more efficiently compared to 'fromList'. If
158 -- the given hint does not equal to the input list's length the behaviour of
159 -- 'fromListN' is not specified.
160 fromListN :: Int -> [Item l] -> l
161 fromListN _ = fromList
162
163 -- | The 'toList' function extracts a list of @Item l@ from the structure @l@.
164 -- It should satisfy fromList . toList = id.
165 toList :: l -> [Item l]
166
167 instance IsList [a] where
168 type (Item [a]) = a
169 fromList = id
170 toList = id