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