Improve list fusion for [n::Integer..m]
[packages/base.git] / 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 shiftL#, shiftRL#, iShiftL#, iShiftRA#, iShiftRL#,
32 uncheckedShiftL64#, uncheckedShiftRL64#,
33 uncheckedIShiftL64#, uncheckedIShiftRA64#,
34 isTrue#,
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 -- * Safe coercions
49 GHC.Prim.coerce, GHC.Prim.Coercible,
50
51 -- * Transform comprehensions
52 Down(..), groupWith, sortWith, the,
53
54 -- * Event logging
55 traceEvent,
56
57 -- * SpecConstr annotations
58 SpecConstrAnnotation(..),
59
60 -- * The call stack
61 currentCallStack,
62
63 -- * The Constraint kind
64 Constraint,
65
66 -- * Overloaded lists
67 IsList(..)
68 ) where
69
70 import Prelude
71
72 import GHC.Prim hiding (coerce, Coercible)
73 import qualified GHC.Prim
74 import GHC.Base hiding (coerce, Coercible)
75 import GHC.Word
76 import GHC.Int
77 import GHC.Ptr
78 import GHC.Stack
79 import Data.String
80 import Data.List
81 import Data.Data
82 import Data.Ord
83 import qualified Debug.Trace
84
85 -- XXX This should really be in Data.Tuple, where the definitions are
86 maxTupleSize :: Int
87 maxTupleSize = 62
88
89 -- | 'the' ensures that all the elements of the list are identical
90 -- and then returns that unique element
91 the :: Eq a => [a] -> a
92 the (x:xs)
93 | all (x ==) xs = x
94 | otherwise = error "GHC.Exts.the: non-identical elements"
95 the [] = error "GHC.Exts.the: empty list"
96
97 -- | The 'sortWith' function sorts a list of elements using the
98 -- user supplied function to project something out of each element
99 sortWith :: Ord b => (a -> b) -> [a] -> [a]
100 sortWith f = sortBy (\x y -> compare (f x) (f y))
101
102 -- | The 'groupWith' function uses the user supplied function which
103 -- projects an element out of every list element in order to first sort the
104 -- input list and then to form groups by equality on these projected elements
105 {-# INLINE groupWith #-}
106 groupWith :: Ord b => (a -> b) -> [a] -> [[a]]
107 groupWith f xs = build (\c n -> groupByFB c n (\x y -> f x == f y) (sortWith f xs))
108
109 groupByFB :: ([a] -> lst -> lst) -> lst -> (a -> a -> Bool) -> [a] -> lst
110 groupByFB c n eq xs0 = groupByFBCore xs0
111 where groupByFBCore [] = n
112 groupByFBCore (x:xs) = c (x:ys) (groupByFBCore zs)
113 where (ys, zs) = span (eq x) xs
114
115
116 -- -----------------------------------------------------------------------------
117 -- tracing
118
119 traceEvent :: String -> IO ()
120 traceEvent = Debug.Trace.traceEventIO
121 {-# DEPRECATED traceEvent "Use 'Debug.Trace.traceEvent' or 'Debug.Trace.traceEventIO'" #-} -- deprecated in 7.4
122
123
124 {- **********************************************************************
125 * *
126 * SpecConstr annotation *
127 * *
128 ********************************************************************** -}
129
130 -- Annotating a type with NoSpecConstr will make SpecConstr
131 -- not specialise for arguments of that type.
132
133 -- This data type is defined here, rather than in the SpecConstr module
134 -- itself, so that importing it doesn't force stupidly linking the
135 -- entire ghc package at runtime
136
137 data SpecConstrAnnotation = NoSpecConstr | ForceSpecConstr
138 deriving( Data, Typeable, Eq )
139
140
141 {- **********************************************************************
142 * *
143 * The IsList class *
144 * *
145 ********************************************************************** -}
146
147 -- | The 'IsList' class and its methods are intended to be used in
148 -- conjunction with the OverloadedLists extension.
149 --
150 -- /Since: 4.7.0.0/
151 class IsList l where
152 -- | The 'Item' type function returns the type of items of the structure
153 -- @l@.
154 type Item l
155
156 -- | The 'fromList' function constructs the structure @l@ from the given
157 -- list of @Item l@
158 fromList :: [Item l] -> l
159
160 -- | The 'fromListN' function takes the input list's length as a hint. Its
161 -- behaviour should be equivalent to 'fromList'. The hint can be used to
162 -- construct the structure @l@ more efficiently compared to 'fromList'. If
163 -- the given hint does not equal to the input list's length the behaviour of
164 -- 'fromListN' is not specified.
165 fromListN :: Int -> [Item l] -> l
166 fromListN _ = fromList
167
168 -- | The 'toList' function extracts a list of @Item l@ from the structure @l@.
169 -- It should satisfy fromList . toList = id.
170 toList :: l -> [Item l]
171
172 instance IsList [a] where
173 type (Item [a]) = a
174 fromList = id
175 toList = id