1 {-# LANGUAGE MagicHash, UnboxedTuples, DeriveDataTypeable #-}
3 -----------------------------------------------------------------------------
6 -- Copyright : (c) The University of Glasgow 2002
7 -- License : see libraries/base/LICENSE
9 -- Maintainer : cvs-ghc@haskell.org
10 -- Stability : internal
11 -- Portability : non-portable (GHC Extensions)
13 -- GHC Extensions: this is the Approved Way to get at GHC-specific extensions.
15 -----------------------------------------------------------------------------
19 -- * Representations of some basic types
20 Int(..),Word(..),Float(..),Double(..),
24 -- * The maximum tuple size
27 -- * Primitive operations
29 shiftL#, shiftRL#, iShiftL#, iShiftRA#, iShiftRL#,
30 uncheckedShiftL64#, uncheckedShiftRL64#,
31 uncheckedIShiftL64#, uncheckedIShiftRA64#,
36 -- * Overloaded string literals
40 breakpoint, breakpointCond,
42 -- * Ids with special behaviour
45 -- * Transform comprehensions
46 Down(..), groupWith, sortWith, the,
51 -- * SpecConstr annotations
52 SpecConstrAnnotation(..)
70 -- XXX This should really be in Data.Tuple, where the definitions are
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)
82 instance Ord a => Ord (Down a) where
83 compare (Down x) (Down y) = y `compare` x
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
90 | otherwise = error "GHC.Exts.the: non-identical elements"
91 the [] = error "GHC.Exts.the: empty list"
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))
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))
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
112 -- -----------------------------------------------------------------------------
115 traceEvent :: String -> IO ()
117 withCString msg $ \(Ptr p) -> IO $ \s ->
118 case traceEvent# p s of s' -> (# s', () #)
122 {- **********************************************************************
124 * SpecConstr annotation *
126 ********************************************************************** -}
128 -- Annotating a type with NoSpecConstr will make SpecConstr
129 -- not specialise for arguments of that type.
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
135 data SpecConstrAnnotation = NoSpecConstr | ForceSpecConstr
136 deriving( Data, Typeable, Eq )