tighten up parsing of numbers (#1579)
[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        ) where
50
51 import Prelude
52
53 import GHC.Prim
54 import GHC.Base
55 import GHC.Magic
56 import GHC.Word
57 import GHC.Int
58 -- import GHC.Float
59 import GHC.Ptr
60 import Data.String
61 import Data.List
62 import Foreign.C
63
64 -- XXX This should really be in Data.Tuple, where the definitions are
65 maxTupleSize :: Int
66 maxTupleSize = 62
67
68 -- | The 'Down' type allows you to reverse sort order conveniently.  A value of type
69 -- @'Down' a@ contains a value of type @a@ (represented as @'Down' a@).
70 -- If @a@ has an @'Ord'@ instance associated with it then comparing two
71 -- values thus wrapped will give you the opposite of their normal sort order.
72 -- This is particularly useful when sorting in generalised list comprehensions,
73 -- as in: @then sortWith by 'Down' x@
74 newtype Down a = Down a deriving (Eq)
75
76 instance Ord a => Ord (Down a) where
77     compare (Down x) (Down y) = y `compare` x
78
79 -- | 'the' ensures that all the elements of the list are identical
80 -- and then returns that unique element
81 the :: Eq a => [a] -> a
82 the (x:xs)
83   | all (x ==) xs = x
84   | otherwise     = error "GHC.Exts.the: non-identical elements"
85 the []            = error "GHC.Exts.the: empty list"
86
87 -- | The 'sortWith' function sorts a list of elements using the
88 -- user supplied function to project something out of each element
89 sortWith :: Ord b => (a -> b) -> [a] -> [a]
90 sortWith f = sortBy (\x y -> compare (f x) (f y))
91
92 -- | The 'groupWith' function uses the user supplied function which
93 -- projects an element out of every list element in order to to first sort the 
94 -- input list and then to form groups by equality on these projected elements
95 {-# INLINE groupWith #-}
96 groupWith :: Ord b => (a -> b) -> [a] -> [[a]]
97 groupWith f xs = build (\c n -> groupByFB c n (\x y -> f x == f y) (sortWith f xs))
98
99 groupByFB :: ([a] -> lst -> lst) -> lst -> (a -> a -> Bool) -> [a] -> lst
100 groupByFB c n eq xs0 = groupByFBCore xs0
101   where groupByFBCore [] = n
102         groupByFBCore (x:xs) = c (x:ys) (groupByFBCore zs)
103             where (ys, zs) = span (eq x) xs
104
105
106 -- -----------------------------------------------------------------------------
107 -- tracing
108
109 traceEvent :: String -> IO ()
110 traceEvent msg = do
111   withCString msg $ \(Ptr p) -> IO $ \s ->
112     case traceEvent# p s of s' -> (# s', () #)