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 -- * Primitive operations
24 shiftL#, shiftRL#, iShiftL#, iShiftRA#, iShiftRL#,
25 uncheckedShiftL64#, uncheckedShiftRL64#,
26 uncheckedIShiftL64#, uncheckedIShiftRA64#,
31 -- * Overloaded string literals
35 breakpoint, breakpointCond,
37 -- * Ids with special behaviour
40 -- * Transform comprehensions
41 Down(..), groupWith, sortWith, the
57 -- | The 'Down' type allows you to reverse sort order conveniently. A value of type
58 -- @'Down' a@ contains a value of type @a@ (represented as @'Down' a@).
59 -- If @a@ has an @'Ord'@ instance associated with it then comparing two
60 -- values thus wrapped will give you the opposite of their normal sort order.
61 -- This is particularly useful when sorting in generalised list comprehensions,
62 -- as in: @then sortWith by 'Down' x@
63 newtype Down a = Down a deriving (Eq)
65 instance Ord a => Ord (Down a) where
66 compare (Down x) (Down y) = y `compare` x
68 -- | 'the' ensures that all the elements of the list are identical
69 -- and then returns that unique element
70 the :: Eq a => [a] -> a
73 | otherwise = error "GHC.Exts.the: non-identical elements"
74 the [] = error "GHC.Exts.the: empty list"
76 -- | The 'sortWith' function sorts a list of elements using the
77 -- user supplied function to project something out of each element
78 sortWith :: Ord b => (a -> b) -> [a] -> [a]
79 sortWith f = sortBy (\x y -> compare (f x) (f y))
81 -- | The 'groupWith' function uses the user supplied function which
82 -- projects an element out of every list element in order to to first sort the
83 -- input list and then to form groups by equality on these projected elements
84 {-# INLINE groupWith #-}
85 groupWith :: Ord b => (a -> b) -> [a] -> [[a]]
86 groupWith f xs = build (\c n -> groupByFB c n (\x y -> f x == f y) (sortWith f xs))
88 groupByFB :: ([a] -> lst -> lst) -> lst -> (a -> a -> Bool) -> [a] -> lst
89 groupByFB c n eq xs = groupByFBCore xs
90 where groupByFBCore [] = n
91 groupByFBCore (x:xs) = c (x:ys) (groupByFBCore zs)
92 where (ys, zs) = span (eq x) xs