ghc-8.0.2: The GHC API

Safe HaskellNone
LanguageHaskell2010

Util

Contents

Description

Highly random utility functions

Synopsis

Flags dependent on the compiler build

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 #

zipWithLazy :: (a -> b -> c) -> [a] -> [b] -> [c] Source #

zipWithLazy is like zipWith but is lazy in the second list. The length of the output is always the same as the length of the first list.

zipWith3Lazy :: (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d] Source #

zipWith3Lazy is like zipWith3 but is lazy in the second and third lists. The length of the output is always the same as the length of the first list.

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.

filterByLists :: [Bool] -> [a] -> [a] -> [a] Source #

filterByLists takes a list of Bools and two lists as input, and outputs a new list consisting of elements from the last two input lists. For each Bool in the list, if it is True, then it takes an element from the former list. If it is False, it takes an element from the latter list. The elements taken correspond to the index of the Bool in its list. For example:

filterByLists [True, False, True, False] "abcd" "wxyz" = "axcz"

This function does not check whether the lists have equal length.

partitionByList :: [Bool] -> [a] -> ([a], [a]) Source #

partitionByList takes a list of Bools and a list of some elements and partitions the list according to the list of Bools. Elements corresponding to True go to the left; elements corresponding to False go to the right. For example, partitionByList [True, False, True] [1,2,3] == ([1,3], [2]) 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 Eithers apart into two lists

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

spanEnd :: (a -> Bool) -> [a] -> ([a], [a]) Source #

spanEnd p l == reverse (span p (reverse l)). The first list returns actually comes after the second list (when you look at the input list).

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 #

atLength :: ([a] -> b) -> 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 ls
  | 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

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 #

chunkList :: Int -> [a] -> [[a]] Source #

Split a list into chunks of n elements

Tuples

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

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

thdOf3 :: (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 #

fst3 :: (a -> d) -> (a, b, c) -> (d, b, c) Source #

snd3 :: (b -> d) -> (a, b, c) -> (a, d, c) Source #

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

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

liftFst :: (a -> b) -> (a, c) -> (b, c) Source #

liftSnd :: (a -> b) -> (c, a) -> (c, b) Source #

List operations controlled by another list

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

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

splitAtList :: [b] -> [a] -> ([a], [a]) 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 #

The sortWith function sorts a list of elements using the user supplied function to project something out of each element

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

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

Comparisons

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

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

cmpList :: (a -> a -> Ordering) -> [a] -> [a] -> Ordering 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

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

Argument processing

Floating point

read helpers

IO-ish utilities

global :: a -> IORef a Source #

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

globalM :: IO a -> IORef a Source #

Filenames and paths

data Direction Source #

Constructors

Forwards 
Backwards 

Utils for defining Data instances

mkNoRepType :: String -> DataType Source #

Constructs a non-representation for a non-representable type

Utils for printing C code

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.