untabify
[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(..),Integer(..),Char(..),
19         Ptr(..), FunPtr(..),
20
21         -- * Primitive operations
22         module GHC.Prim,
23         shiftL#, shiftRL#, iShiftL#, iShiftRA#, iShiftRL#,
24         uncheckedShiftL64#, uncheckedShiftRL64#,
25         uncheckedIShiftL64#, uncheckedIShiftRA64#,
26
27         -- * Fusion
28         build, augment,
29
30         -- * Overloaded string literals
31         IsString(..),
32
33         -- * Debugging
34         breakpoint, breakpointCond,
35
36         -- * Ids with special behaviour
37         lazy, inline,
38
39         -- * Transform comprehensions
40         Down(..), groupWith, sortWith, the
41
42        ) where
43
44 import Prelude
45
46 import GHC.Prim
47 import GHC.Base
48 import GHC.Word
49 import GHC.Int
50 import GHC.Num
51 import GHC.Float
52 import GHC.Ptr
53 import Data.String
54 import Data.List
55
56 -- | The 'Down' type allows you to reverse sort order conveniently.  A value of type
57 -- @'Down' a@ contains a value of type @a@ (represented as @'Down' a@).
58 -- If @a@ has an @'Ord'@ instance associated with it then comparing two
59 -- values thus wrapped will give you the opposite of their normal sort order.
60 -- This is particularly useful when sorting in generalised list comprehensions,
61 -- as in: @then sortWith by 'Down' x@
62 newtype Down a = Down a deriving (Eq)
63
64 instance Ord a => Ord (Down a) where
65     compare (Down x) (Down y) = y `compare` x
66
67 -- | 'the' ensures that all the elements of the list are identical
68 -- and then returns that unique element
69 the :: Eq a => [a] -> a
70 the (x:xs)
71   | all (x ==) xs = x
72   | otherwise     = error "GHC.Exts.the: non-identical elements"
73 the []            = error "GHC.Exts.the: empty list"
74
75 -- | The 'sortWith' function sorts a list of elements using the
76 -- user supplied function to project something out of each element
77 sortWith :: Ord b => (a -> b) -> [a] -> [a]
78 sortWith f = sortBy (\x y -> compare (f x) (f y))
79
80 -- | The 'groupWith' function uses the user supplied function which
81 -- projects an element out of every list element in order to to first sort the 
82 -- input list and then to form groups by equality on these projected elements
83 {-# INLINE groupWith #-}
84 groupWith :: Ord b => (a -> b) -> [a] -> [[a]]
85 groupWith f xs = build (\c n -> groupByFB c n (\x y -> f x == f y) (sortWith f xs))
86
87 groupByFB :: ([a] -> lst -> lst) -> lst -> (a -> a -> Bool) -> [a] -> lst
88 groupByFB c n eq xs = groupByFBCore xs
89   where groupByFBCore [] = n
90         groupByFBCore (x:xs) = c (x:ys) (groupByFBCore zs)
91             where (ys, zs) = span (eq x) xs
92