Move Integer out into its own package
[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         -- * Primitive operations
23         module GHC.Prim,
24         shiftL#, shiftRL#, iShiftL#, iShiftRA#, iShiftRL#,
25         uncheckedShiftL64#, uncheckedShiftRL64#,
26         uncheckedIShiftL64#, uncheckedIShiftRA64#,
27
28         -- * Fusion
29         build, augment,
30
31         -- * Overloaded string literals
32         IsString(..),
33
34         -- * Debugging
35         breakpoint, breakpointCond,
36
37         -- * Ids with special behaviour
38         lazy, inline,
39
40         -- * Transform comprehensions
41         Down(..), groupWith, sortWith, the
42
43        ) where
44
45 import Prelude
46
47 import GHC.Prim
48 import GHC.Base
49 import GHC.Word
50 import GHC.Int
51 import GHC.Num
52 import GHC.Float
53 import GHC.Ptr
54 import Data.String
55 import Data.List
56
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)
64
65 instance Ord a => Ord (Down a) where
66     compare (Down x) (Down y) = y `compare` x
67
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
71 the (x:xs)
72   | all (x ==) xs = x
73   | otherwise     = error "GHC.Exts.the: non-identical elements"
74 the []            = error "GHC.Exts.the: empty list"
75
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))
80
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))
87
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
93