X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=GHC%2FExts.hs;h=661f5aa275934b6bb43909afd65606ef6f69d72c;hb=b5c54282dc54cc861277ae532224775076a4818e;hp=d75dc5a02b18dbeb3acc099ec3df5154e41bd757;hpb=5cd7cad9d46e4759829638d84a011e622b1ecfb6;p=ghc-base.git diff --git a/GHC/Exts.hs b/GHC/Exts.hs index d75dc5a..661f5aa 100644 --- a/GHC/Exts.hs +++ b/GHC/Exts.hs @@ -1,39 +1,112 @@ ----------------------------------------------------------------------------- --- +-- | -- Module : GHC.Exts --- Copyright : (c) The University of Glasgow 2001 --- License : BSD-style (see the file libraries/core/LICENSE) +-- Copyright : (c) The University of Glasgow 2002 +-- License : see libraries/base/LICENSE -- --- Maintainer : libraries@haskell.org --- Stability : experimental --- Portability : non-portable --- --- $Id: Exts.hs,v 1.2 2002/01/02 15:01:27 simonmar Exp $ +-- Maintainer : cvs-ghc@haskell.org +-- Stability : internal +-- Portability : non-portable (GHC Extensions) -- --- GHC Extensions: this is the Approved Way to get at GHC-specific stuff. +-- GHC Extensions: this is the Approved Way to get at GHC-specific extensions. -- ----------------------------------------------------------------------------- module GHC.Exts ( - -- the representation of some basic types: - Int(..),Word(..),Float(..),Double(..),Integer(..),Char(..), + -- * Representations of some basic types + Int(..),Word(..),Float(..),Double(..), + Char(..), + Ptr(..), FunPtr(..), + + -- * The maximum tuple size + maxTupleSize, + + -- * Primitive operations + module GHC.Prim, + shiftL#, shiftRL#, iShiftL#, iShiftRA#, iShiftRL#, + uncheckedShiftL64#, uncheckedShiftRL64#, + uncheckedIShiftL64#, uncheckedIShiftRA64#, + + -- * Fusion + build, augment, - -- Fusion - build, augment, + -- * Overloaded string literals + IsString(..), - -- shifty wrappers from GHC.Base - shiftL#, shiftRL#, iShiftL#, iShiftRA#, iShiftRL#, + -- * Debugging + breakpoint, breakpointCond, - -- and finally, all the unboxed primops of GHC! - module GHC.Prim + -- * Ids with special behaviour + lazy, inline, + + -- * Transform comprehensions + Down(..), groupWith, sortWith, the, + + -- * Event logging + traceEvent ) where import Prelude -import {-# SOURCE #-} GHC.Prim +import GHC.Prim import GHC.Base +import GHC.Magic import GHC.Word -import GHC.Num -import GHC.Float +import GHC.Int +-- import GHC.Float +import GHC.Ptr +import Data.String +import Data.List +import Foreign.C + +-- XXX This should really be in Data.Tuple, where the definitions are +maxTupleSize :: Int +maxTupleSize = 62 + +-- | The 'Down' type allows you to reverse sort order conveniently. A value of type +-- @'Down' a@ contains a value of type @a@ (represented as @'Down' a@). +-- If @a@ has an @'Ord'@ instance associated with it then comparing two +-- values thus wrapped will give you the opposite of their normal sort order. +-- This is particularly useful when sorting in generalised list comprehensions, +-- as in: @then sortWith by 'Down' x@ +newtype Down a = Down a deriving (Eq) + +instance Ord a => Ord (Down a) where + compare (Down x) (Down y) = y `compare` x + +-- | 'the' ensures that all the elements of the list are identical +-- and then returns that unique element +the :: Eq a => [a] -> a +the (x:xs) + | all (x ==) xs = x + | otherwise = error "GHC.Exts.the: non-identical elements" +the [] = error "GHC.Exts.the: empty list" + +-- | The 'sortWith' function sorts a list of elements using the +-- user supplied function to project something out of each element +sortWith :: Ord b => (a -> b) -> [a] -> [a] +sortWith f = sortBy (\x y -> compare (f x) (f y)) + +-- | The 'groupWith' function uses the user supplied function which +-- projects an element out of every list element in order to to first sort the +-- input list and then to form groups by equality on these projected elements +{-# INLINE groupWith #-} +groupWith :: Ord b => (a -> b) -> [a] -> [[a]] +groupWith f xs = build (\c n -> groupByFB c n (\x y -> f x == f y) (sortWith f xs)) + +groupByFB :: ([a] -> lst -> lst) -> lst -> (a -> a -> Bool) -> [a] -> lst +groupByFB c n eq xs0 = groupByFBCore xs0 + where groupByFBCore [] = n + groupByFBCore (x:xs) = c (x:ys) (groupByFBCore zs) + where (ys, zs) = span (eq x) xs + + +-- ----------------------------------------------------------------------------- +-- tracing + +traceEvent :: String -> IO () +traceEvent msg = do + withCString msg $ \(Ptr p) -> IO $ \s -> + case traceEvent# p s of s' -> (# s', () #)