ghc-7.10.2: The GHC API

Safe Haskell | None
Language | Haskell2010

Util

Contents

- Flags dependent on the compiler build
- General list processing
- Tuples
- List operations controlled by another list
- For loop
- Sorting
- Comparisons
- Edit distance
- Transitive closures
- Strictness
- Module names
- Argument processing
- Floating point
- read helpers
- IO-ish utilities
- Filenames and paths
- Utils for defining Data instances
- Utils for printing C code
- Hashing

Description

Highly random utility functions

Synopsis

- ghciSupported :: Bool
- debugIsOn :: Bool
- ncgDebugIsOn :: Bool
- ghciTablesNextToCode :: Bool
- isWindowsHost :: Bool
- isDarwinHost :: Bool
- zipEqual :: String -> [a] -> [b] -> [(a, b)]
- zipWithEqual :: String -> (a -> b -> c) -> [a] -> [b] -> [c]
- zipWith3Equal :: String -> (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d]
- zipWith4Equal :: String -> (a -> b -> c -> d -> e) -> [a] -> [b] -> [c] -> [d] -> [e]
- zipLazy :: [a] -> [b] -> [(a, b)]
- stretchZipWith :: (a -> Bool) -> b -> (a -> b -> c) -> [a] -> [b] -> [c]
- zipWithAndUnzip :: (a -> b -> (c, d)) -> [a] -> [b] -> ([c], [d])
- filterByList :: [Bool] -> [a] -> [a]
- unzipWith :: (a -> b -> c) -> [(a, b)] -> [c]
- mapFst :: (a -> c) -> [(a, b)] -> [(c, b)]
- mapSnd :: (b -> c) -> [(a, b)] -> [(a, c)]
- chkAppend :: [a] -> [a] -> [a]
- mapAndUnzip :: (a -> (b, c)) -> [a] -> ([b], [c])
- mapAndUnzip3 :: (a -> (b, c, d)) -> [a] -> ([b], [c], [d])
- mapAccumL2 :: (s1 -> s2 -> a -> (s1, s2, b)) -> s1 -> s2 -> [a] -> (s1, s2, [b])
- nOfThem :: Int -> a -> [a]
- filterOut :: (a -> Bool) -> [a] -> [a]
- partitionWith :: (a -> Either b c) -> [a] -> ([b], [c])
- splitEithers :: [Either a b] -> ([a], [b])
- dropWhileEndLE :: (a -> Bool) -> [a] -> [a]
- foldl1' :: (a -> a -> a) -> [a] -> a
- foldl2 :: (acc -> a -> b -> acc) -> acc -> [a] -> [b] -> acc
- count :: (a -> Bool) -> [a] -> Int
- all2 :: (a -> b -> Bool) -> [a] -> [b] -> Bool
- lengthExceeds :: [a] -> Int -> Bool
- lengthIs :: [a] -> Int -> Bool
- lengthAtLeast :: [a] -> Int -> Bool
- listLengthCmp :: [a] -> Int -> Ordering
- atLength :: ([a] -> b) -> (Int -> b) -> [a] -> Int -> b
- equalLength :: [a] -> [b] -> Bool
- compareLength :: [a] -> [b] -> Ordering
- leLength :: [a] -> [b] -> Bool
- isSingleton :: [a] -> Bool
- only :: [a] -> a
- singleton :: a -> [a]
- notNull :: [a] -> Bool
- snocView :: [a] -> Maybe ([a], a)
- isIn :: Eq a => String -> a -> [a] -> Bool
- isn'tIn :: Eq a => String -> a -> [a] -> Bool
- fstOf3 :: (a, b, c) -> a
- sndOf3 :: (a, b, c) -> b
- thirdOf3 :: (a, b, c) -> c
- firstM :: Monad m => (a -> m c) -> (a, b) -> m (c, b)
- first3M :: Monad m => (a -> m d) -> (a, b, c) -> m (d, b, c)
- third3 :: (c -> d) -> (a, b, c) -> (a, b, d)
- uncurry3 :: (a -> b -> c -> d) -> (a, b, c) -> d
- takeList :: [b] -> [a] -> [a]
- dropList :: [b] -> [a] -> [a]
- splitAtList :: [b] -> [a] -> ([a], [a])
- split :: Char -> String -> [String]
- dropTail :: Int -> [a] -> [a]
- nTimes :: Int -> (a -> a) -> a -> a
- sortWith :: Ord b => (a -> b) -> [a] -> [a]
- minWith :: Ord b => (a -> b) -> [a] -> a
- nubSort :: Ord a => [a] -> [a]
- isEqual :: Ordering -> Bool
- eqListBy :: (a -> a -> Bool) -> [a] -> [a] -> Bool
- eqMaybeBy :: (a -> a -> Bool) -> Maybe a -> Maybe a -> Bool
- thenCmp :: Ordering -> Ordering -> Ordering
- cmpList :: (a -> a -> Ordering) -> [a] -> [a] -> Ordering
- removeSpaces :: String -> String
- (<&&>) :: Applicative f => f Bool -> f Bool -> f Bool
- (<||>) :: Applicative f => f Bool -> f Bool -> f Bool
- fuzzyMatch :: String -> [String] -> [String]
- fuzzyLookup :: String -> [(String, a)] -> [a]
- transitiveClosure :: (a -> [a]) -> (a -> a -> Bool) -> [a] -> [a]
- seqList :: [a] -> b -> b
- looksLikeModuleName :: String -> Bool
- getCmd :: String -> Either String (String, String)
- toCmdArgs :: String -> Either String (String, [String])
- toArgs :: String -> Either String [String]
- readRational :: String -> Rational
- maybeRead :: Read a => String -> Maybe a
- maybeReadFuzzy :: Read a => String -> Maybe a
- doesDirNameExist :: FilePath -> IO Bool
- getModificationUTCTime :: FilePath -> IO UTCTime
- modificationTimeIfExists :: FilePath -> IO (Maybe UTCTime)
- global :: a -> IORef a
- consIORef :: IORef [a] -> a -> IO ()
- globalM :: IO a -> IORef a
- type Suffix = String
- splitLongestPrefix :: String -> (Char -> Bool) -> (String, String)
- escapeSpaces :: String -> String
- data Direction
  - = Forwards
  - | Backwards
- reslash :: Direction -> FilePath -> FilePath
- makeRelativeTo :: FilePath -> FilePath -> FilePath
- abstractConstr :: String -> Constr
- abstractDataType :: String -> DataType
- mkNoRepType :: String -> DataType
- charToC :: Word8 -> String
- hashString :: String -> Int32

# Flags dependent on the compiler build

ghciSupported :: Bool Source

debugIsOn :: Bool Source

ncgDebugIsOn :: Bool Source

ghciTablesNextToCode :: Bool Source

isWindowsHost :: Bool Source

isDarwinHost :: Bool Source

# General list processing

zipEqual :: String -> [a] -> [b] -> [(a, b)] Source

zipWithEqual :: String -> (a -> b -> c) -> [a] -> [b] -> [c] Source

zipWith3Equal :: String -> (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d] Source

zipWith4Equal :: String -> (a -> b -> c -> d -> e) -> [a] -> [b] -> [c] -> [d] -> [e] Source

zipLazy :: [a] -> [b] -> [(a, b)] Source

`zipLazy` is a kind of `zip` that is lazy in the second list (observe the ~)

stretchZipWith :: (a -> Bool) -> b -> (a -> b -> c) -> [a] -> [b] -> [c] Source

`stretchZipWith p z f xs ys` stretches `ys` by inserting `z` in the places where `p` returns `True`

zipWithAndUnzip :: (a -> b -> (c, d)) -> [a] -> [b] -> ([c], [d]) Source

filterByList :: [Bool] -> [a] -> [a] Source

`filterByList` takes a list of Bools and a list of some elements and filters out these elements for which the corresponding value in the list of Bools is False. This function does not check whether the lists have equal length.

unzipWith :: (a -> b -> c) -> [(a, b)] -> [c] Source

mapFst :: (a -> c) -> [(a, b)] -> [(c, b)] Source

mapSnd :: (b -> c) -> [(a, b)] -> [(a, c)] Source

chkAppend :: [a] -> [a] -> [a] Source

mapAndUnzip :: (a -> (b, c)) -> [a] -> ([b], [c]) Source

mapAndUnzip3 :: (a -> (b, c, d)) -> [a] -> ([b], [c], [d]) Source

mapAccumL2 :: (s1 -> s2 -> a -> (s1, s2, b)) -> s1 -> s2 -> [a] -> (s1, s2, [b]) Source

nOfThem :: Int -> a -> [a] Source

filterOut :: (a -> Bool) -> [a] -> [a] Source

Like filter, only it reverses the sense of the test

partitionWith :: (a -> Either b c) -> [a] -> ([b], [c]) Source

Uses a function to determine which of two output lists an input element should join

splitEithers :: [Either a b] -> ([a], [b]) Source

Teases a list of `Either`s apart into two lists

dropWhileEndLE :: (a -> Bool) -> [a] -> [a] Source

foldl1' :: (a -> a -> a) -> [a] -> a Source

A strict version of `foldl1`

foldl2 :: (acc -> a -> b -> acc) -> acc -> [a] -> [b] -> acc Source

count :: (a -> Bool) -> [a] -> Int Source

all2 :: (a -> b -> Bool) -> [a] -> [b] -> Bool Source

lengthExceeds :: [a] -> Int -> Bool Source

    (lengthExceeds xs n) = (length xs > n)

lengthIs :: [a] -> Int -> Bool Source

lengthAtLeast :: [a] -> Int -> Bool Source

listLengthCmp :: [a] -> Int -> Ordering Source

atLength :: ([a] -> b) -> (Int -> b) -> [a] -> Int -> b Source

`atLength atLen atEnd ls n` unravels list `ls` to position `n`. Precisely:

     atLength atLenPred atEndPred ls n
      | n < 0         = atLenPred n
      | length ls < n = atEndPred (n - length ls)
      | otherwise     = atLenPred (drop n ls)

equalLength :: [a] -> [b] -> Bool Source

compareLength :: [a] -> [b] -> Ordering Source

leLength :: [a] -> [b] -> Bool Source

True if length xs <= length ys

isSingleton :: [a] -> Bool Source

only :: [a] -> a Source

singleton :: a -> [a] Source

notNull :: [a] -> Bool Source

snocView :: [a] -> Maybe ([a], a) Source

isIn :: Eq a => String -> a -> [a] -> Bool Source

isn'tIn :: Eq a => String -> a -> [a] -> Bool Source

# Tuples

fstOf3 :: (a, b, c) -> a Source

sndOf3 :: (a, b, c) -> b Source

thirdOf3 :: (a, b, c) -> c Source

firstM :: Monad m => (a -> m c) -> (a, b) -> m (c, b) Source

first3M :: Monad m => (a -> m d) -> (a, b, c) -> m (d, b, c) Source

third3 :: (c -> d) -> (a, b, c) -> (a, b, d) Source

uncurry3 :: (a -> b -> c -> d) -> (a, b, c) -> d Source

# List operations controlled by another list

takeList :: [b] -> [a] -> [a] Source

dropList :: [b] -> [a] -> [a] Source

splitAtList :: [b] -> [a] -> ([a], [a]) Source

split :: Char -> String -> [String] Source

dropTail :: Int -> [a] -> [a] Source

# For loop

nTimes :: Int -> (a -> a) -> a -> a Source

Compose a function with itself n times. (nth rather than twice)

# Sorting

sortWith :: Ord b => (a -> b) -> [a] -> [a] Source

minWith :: Ord b => (a -> b) -> [a] -> a Source

nubSort :: Ord a => [a] -> [a] Source

# Comparisons

isEqual :: Ordering -> Bool Source

eqListBy :: (a -> a -> Bool) -> [a] -> [a] -> Bool Source

eqMaybeBy :: (a -> a -> Bool) -> Maybe a -> Maybe a -> Bool Source

thenCmp :: Ordering -> Ordering -> Ordering infixr 9 Source

cmpList :: (a -> a -> Ordering) -> [a] -> [a] -> Ordering Source

removeSpaces :: String -> String Source

(<&&>) :: Applicative f => f Bool -> f Bool -> f Bool infixr 3 Source

(<||>) :: Applicative f => f Bool -> f Bool -> f Bool infixr 2 Source

# Edit distance

fuzzyMatch :: String -> [String] -> [String] Source

fuzzyLookup :: String -> [(String, a)] -> [a] Source

Search for possible matches to the users input in the given list, returning a small number of ranked results

# Transitive closures

transitiveClosure :: (a -> [a]) -> (a -> a -> Bool) -> [a] -> [a] Source

# Strictness

seqList :: [a] -> b -> b Source

# Module names

looksLikeModuleName :: String -> Bool Source

# Argument processing

getCmd :: String -> Either String (String, String) Source

toCmdArgs :: String -> Either String (String, [String]) Source

toArgs :: String -> Either String [String] Source

# Floating point

readRational :: String -> Rational Source

# read helpers

maybeRead :: Read a => String -> Maybe a Source

maybeReadFuzzy :: Read a => String -> Maybe a Source

# IO-ish utilities

doesDirNameExist :: FilePath -> IO Bool Source

getModificationUTCTime :: FilePath -> IO UTCTime Source

modificationTimeIfExists :: FilePath -> IO (Maybe UTCTime) Source

global :: a -> IORef a Source

consIORef :: IORef [a] -> a -> IO () Source

globalM :: IO a -> IORef a Source

# Filenames and paths

type Suffix = String Source

splitLongestPrefix :: String -> (Char -> Bool) -> (String, String) Source

escapeSpaces :: String -> String Source

data Direction Source

Constructors

Forwards |
Backwards |

reslash :: Direction -> FilePath -> FilePath Source

makeRelativeTo :: FilePath -> FilePath -> FilePath Source

# Utils for defining Data instances

abstractConstr :: String -> Constr Source

abstractDataType :: String -> DataType Source

mkNoRepType :: String -> DataType Source

Constructs a non-representation for a non-representable type

# Utils for printing C code

charToC :: Word8 -> String Source

# Hashing

hashString :: String -> Int32 Source

A sample hash function for Strings. We keep multiplying by the golden ratio and adding. The implementation is:

    hashString = foldl' f golden
      where f m c = fromIntegral (ord c) * magic + hashInt32 m
            magic = 0xdeadbeef

Where hashInt32 works just as hashInt shown above.

Knuth argues that repeated multiplication by the golden ratio will minimize gaps in the hash space, and thus it's a good choice for combining together multiple keys to form one.

Here we know that individual characters c are often small, and this produces frequent collisions if we use ord c alone. A particular problem are the shorter low ASCII and ISO-8859-1 character strings. We pre-multiply by a magic twiddle factor to obtain a good distribution. In fact, given the following test:

    testp :: Int32 -> Int
    testp k = (n - ) . length . group . sort . map hs . take n $ ls
      where ls = [] : [c : l | l <- ls, c <- ['\0'..'\xff']]
            hs = foldl' f golden
            f m c = fromIntegral (ord c) * k + hashInt32 m
            n = 100000

We discover that testp magic = 0.