ea4b8ff2a8933a8c5e0a9ebc5a81099f93ba05ec
[ghc-base.git] / GHC / Exts.hs
1 -----------------------------------------------------------------------------
2 -- |
3 -- Module      :  GHC.Exts
4 -- Copyright   :  (c) The University of Glasgow 2002
5 -- License     :  see libraries/base/LICENSE
6 -- 
7 -- Maintainer  :  cvs-ghc@haskell.org
8 -- Stability   :  internal
9 -- Portability :  non-portable (GHC Extensions)
10 --
11 -- GHC Extensions: this is the Approved Way to get at GHC-specific extensions.
12 --
13 -----------------------------------------------------------------------------
14
15 module GHC.Exts
16        (
17         -- * Representations of some basic types
18         Int(..),Word(..),Float(..),Double(..),
19         Char(..),
20         Ptr(..), FunPtr(..),
21
22         -- * The maximum tuple size
23         maxTupleSize,
24
25         -- * Primitive operations
26         module GHC.Prim,
27         shiftL#, shiftRL#, iShiftL#, iShiftRA#, iShiftRL#,
28         uncheckedShiftL64#, uncheckedShiftRL64#,
29         uncheckedIShiftL64#, uncheckedIShiftRA64#,
30
31         -- * Fusion
32         build, augment,
33
34         -- * Overloaded string literals
35         IsString(..),
36
37         -- * Debugging
38         breakpoint, breakpointCond,
39
40         -- * Ids with special behaviour
41         lazy, inline,
42
43         -- * Transform comprehensions
44         Down(..), groupWith, sortWith, the,
45
46         -- * Event logging
47         traceEvent,
48
49         -- * SpecConstr annotations
50         SpecConstrAnnotation(..)
51
52        ) where
53
54 import Prelude
55
56 import GHC.Prim
57 import GHC.Base
58 import GHC.Magic
59 import GHC.Word
60 import GHC.Int
61 -- import GHC.Float
62 import GHC.Ptr
63 import Data.String
64 import Data.List
65 import Foreign.C
66 import Data.Data
67
68 -- XXX This should really be in Data.Tuple, where the definitions are
69 maxTupleSize :: Int
70 maxTupleSize = 62
71
72 -- | The 'Down' type allows you to reverse sort order conveniently.  A value of type
73 -- @'Down' a@ contains a value of type @a@ (represented as @'Down' a@).
74 -- If @a@ has an @'Ord'@ instance associated with it then comparing two
75 -- values thus wrapped will give you the opposite of their normal sort order.
76 -- This is particularly useful when sorting in generalised list comprehensions,
77 -- as in: @then sortWith by 'Down' x@
78 newtype Down a = Down a deriving (Eq)
79
80 instance Ord a => Ord (Down a) where
81     compare (Down x) (Down y) = y `compare` x
82
83 -- | 'the' ensures that all the elements of the list are identical
84 -- and then returns that unique element
85 the :: Eq a => [a] -> a
86 the (x:xs)
87   | all (x ==) xs = x
88   | otherwise     = error "GHC.Exts.the: non-identical elements"
89 the []            = error "GHC.Exts.the: empty list"
90
91 -- | The 'sortWith' function sorts a list of elements using the
92 -- user supplied function to project something out of each element
93 sortWith :: Ord b => (a -> b) -> [a] -> [a]
94 sortWith f = sortBy (\x y -> compare (f x) (f y))
95
96 -- | The 'groupWith' function uses the user supplied function which
97 -- projects an element out of every list element in order to to first sort the 
98 -- input list and then to form groups by equality on these projected elements
99 {-# INLINE groupWith #-}
100 groupWith :: Ord b => (a -> b) -> [a] -> [[a]]
101 groupWith f xs = build (\c n -> groupByFB c n (\x y -> f x == f y) (sortWith f xs))
102
103 groupByFB :: ([a] -> lst -> lst) -> lst -> (a -> a -> Bool) -> [a] -> lst
104 groupByFB c n eq xs0 = groupByFBCore xs0
105   where groupByFBCore [] = n
106         groupByFBCore (x:xs) = c (x:ys) (groupByFBCore zs)
107             where (ys, zs) = span (eq x) xs
108
109
110 -- -----------------------------------------------------------------------------
111 -- tracing
112
113 traceEvent :: String -> IO ()
114 traceEvent msg = do
115   withCString msg $ \(Ptr p) -> IO $ \s ->
116     case traceEvent# p s of s' -> (# s', () #)
117
118
119
120 {- **********************************************************************
121 *                                                                       *
122 *              SpecConstr annotation                                    *
123 *                                                                       *
124 ********************************************************************** -}
125
126 -- Annotating a type with NoSpecConstr will make SpecConstr 
127 -- not specialise for arguments of that type.
128
129 -- This data type is defined here, rather than in the SpecConstr module
130 -- itself, so that importing it doesn't force stupidly linking the
131 -- entire ghc package at runtime
132
133 data SpecConstrAnnotation = NoSpecConstr | ForceSpecConstr
134                 deriving( Data, Typeable, Eq )
135