1 -----------------------------------------------------------------------------
4 -- Copyright : (c) The University of Glasgow 2002
5 -- License : see libraries/base/LICENSE
7 -- Maintainer : cvs-ghc@haskell.org
8 -- Stability : internal
9 -- Portability : non-portable (GHC Extensions)
11 -- GHC Extensions: this is the Approved Way to get at GHC-specific extensions.
13 -----------------------------------------------------------------------------
17 -- * Representations of some basic types
18 Int(..),Word(..),Float(..),Double(..),
22 -- * The maximum tuple size
25 -- * Primitive operations
27 shiftL#, shiftRL#, iShiftL#, iShiftRA#, iShiftRL#,
28 uncheckedShiftL64#, uncheckedShiftRL64#,
29 uncheckedIShiftL64#, uncheckedIShiftRA64#,
34 -- * Overloaded string literals
38 breakpoint, breakpointCond,
40 -- * Ids with special behaviour
43 -- * Transform comprehensions
44 Down(..), groupWith, sortWith, the
60 -- XXX This should really be in Data.Tuple, where the definitions are
64 -- | The 'Down' type allows you to reverse sort order conveniently. A value of type
65 -- @'Down' a@ contains a value of type @a@ (represented as @'Down' a@).
66 -- If @a@ has an @'Ord'@ instance associated with it then comparing two
67 -- values thus wrapped will give you the opposite of their normal sort order.
68 -- This is particularly useful when sorting in generalised list comprehensions,
69 -- as in: @then sortWith by 'Down' x@
70 newtype Down a = Down a deriving (Eq)
72 instance Ord a => Ord (Down a) where
73 compare (Down x) (Down y) = y `compare` x
75 -- | 'the' ensures that all the elements of the list are identical
76 -- and then returns that unique element
77 the :: Eq a => [a] -> a
80 | otherwise = error "GHC.Exts.the: non-identical elements"
81 the [] = error "GHC.Exts.the: empty list"
83 -- | The 'sortWith' function sorts a list of elements using the
84 -- user supplied function to project something out of each element
85 sortWith :: Ord b => (a -> b) -> [a] -> [a]
86 sortWith f = sortBy (\x y -> compare (f x) (f y))
88 -- | The 'groupWith' function uses the user supplied function which
89 -- projects an element out of every list element in order to to first sort the
90 -- input list and then to form groups by equality on these projected elements
91 {-# INLINE groupWith #-}
92 groupWith :: Ord b => (a -> b) -> [a] -> [[a]]
93 groupWith f xs = build (\c n -> groupByFB c n (\x y -> f x == f y) (sortWith f xs))
95 groupByFB :: ([a] -> lst -> lst) -> lst -> (a -> a -> Bool) -> [a] -> lst
96 groupByFB c n eq xs = groupByFBCore xs
97 where groupByFBCore [] = n
98 groupByFBCore (x:xs) = c (x:ys) (groupByFBCore zs)
99 where (ys, zs) = span (eq x) xs