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