ghc-8.2.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
- Integers
- Floating point
- read helpers
- IO-ish utilities
- Filenames and paths
- Utils for defining Data instances
- Utils for printing C code
- Hashing
- Call stacks
- Utils for flags

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])
- zipWithLazy :: (a -> b -> c) -> [a] -> [b] -> [c]
- zipWith3Lazy :: (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d]
- filterByList :: [Bool] -> [a] -> [a]
- filterByLists :: [Bool] -> [a] -> [a] -> [a]
- partitionByList :: [Bool] -> [a] -> ([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]
- spanEnd :: (a -> Bool) -> [a] -> ([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) -> 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
- chunkList :: Int -> [a] -> [[a]]
- changeLast :: [a] -> a -> [a]
- fstOf3 :: (a, b, c) -> a
- sndOf3 :: (a, b, c) -> b
- thdOf3 :: (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)
- fst3 :: (a -> d) -> (a, b, c) -> (d, b, c)
- snd3 :: (b -> d) -> (a, b, c) -> (a, d, c)
- third3 :: (c -> d) -> (a, b, c) -> (a, b, d)
- uncurry3 :: (a -> b -> c -> d) -> (a, b, c) -> d
- liftFst :: (a -> b) -> (a, c) -> (b, c)
- liftSnd :: (a -> b) -> (c, a) -> (c, b)
- takeList :: [b] -> [a] -> [a]
- dropList :: [b] -> [a] -> [a]
- splitAtList :: [b] -> [a] -> ([a], [a])
- split :: Char -> String -> [String]
- dropTail :: Int -> [a] -> [a]
- capitalise :: String -> String
- 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
- looksLikePackageName :: String -> Bool
- getCmd :: String -> Either String (String, String)
- toCmdArgs :: String -> Either String (String, [String])
- toArgs :: String -> Either String [String]
- exactLog2 :: Integer -> Maybe Integer
- 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)
- hSetTranslit :: Handle -> IO ()
- global :: a -> IORef a
- consIORef :: IORef [a] -> a -> IO ()
- globalM :: IO a -> IORef a
- sharedGlobal :: a -> (Ptr (IORef a) -> IO (Ptr (IORef a))) -> IORef a
- sharedGlobalM :: IO a -> (Ptr (IORef a) -> IO (Ptr (IORef 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
- data CallStack :: *
- type HasCallStack = HasCallStack
- type HasDebugCallStack = (() :: Constraint)
- prettyCurrentCallStack :: HasCallStack => String
- data OverridingBool
  - = Auto
  - | Always
  - | Never
- overrideWith :: Bool -> OverridingBool -> Bool

# Flags dependent on the compiler build

ghciSupported :: Bool #

debugIsOn :: Bool #

ncgDebugIsOn :: Bool #

ghciTablesNextToCode :: Bool #

isWindowsHost :: Bool #

isDarwinHost :: Bool #

# General list processing

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)] #

`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] #

`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]) #

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

`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] #

`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] #

`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] #

`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]) #

`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] #

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] #

Like filter, only it reverses the sense of the test

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

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

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

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

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

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

`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 #

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

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

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

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

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

    (lengthIs xs n) = (length xs == n)

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

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

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

`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 #

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

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

True if length xs <= length ys

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 #

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

Split a list into chunks of n elements

changeLast :: [a] -> a -> [a] #

Replace the last element of a list with another element.

# Tuples

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

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

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

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

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

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

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

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

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

# List operations controlled by another list

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

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

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

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

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

capitalise :: String -> String #

Convert a word to title case by capitalising the first letter

# For loop

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

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 #

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

# Comparisons

isEqual :: Ordering -> Bool #

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

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

thenCmp :: Ordering -> Ordering -> Ordering infixr 9 #

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

removeSpaces :: String -> String #

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

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

# Edit distance

fuzzyMatch :: String -> [String] -> [String] #

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

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] #

# Strictness

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

# Module names

looksLikeModuleName :: String -> Bool #

looksLikePackageName :: String -> Bool #

# Argument processing

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

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

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

# Integers

exactLog2 :: Integer -> Maybe Integer #

# Floating point

readRational :: String -> Rational #

# read helpers

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

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

# IO-ish utilities

doesDirNameExist :: FilePath -> IO Bool #

getModificationUTCTime :: FilePath -> IO UTCTime #

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

hSetTranslit :: Handle -> IO () #

global :: a -> IORef a #

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

globalM :: IO a -> IORef a #

sharedGlobal :: a -> (Ptr (IORef a) -> IO (Ptr (IORef a))) -> IORef a #

sharedGlobalM :: IO a -> (Ptr (IORef a) -> IO (Ptr (IORef a))) -> IORef a #

# Filenames and paths

type Suffix = String #

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

escapeSpaces :: String -> String #

data Direction #

Constructors

Forwards |
Backwards |

reslash :: Direction -> FilePath -> FilePath #

makeRelativeTo :: FilePath -> FilePath -> FilePath #

# Utils for defining Data instances

abstractConstr :: String -> Constr #

abstractDataType :: String -> DataType #

mkNoRepType :: String -> DataType Source #

Constructs a non-representation for a non-representable type

# Utils for printing C code

charToC :: Word8 -> String #

# Hashing

hashString :: String -> Int32 #

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.

# Call stacks

data CallStack :: * Source #

`CallStack`s are a lightweight method of obtaining a partial call-stack at any point in the program.

A function can request its call-site with the `HasCallStack` constraint. For example, we can define

    errorWithCallStack :: HasCallStack => String -> a

as a variant of `error` that will get its call-site. We can access the call-stack inside `errorWithCallStack` with `callStack`.

    errorWithCallStack :: HasCallStack => String -> a
    errorWithCallStack msg = error (msg ++ "n" ++ prettyCallStack callStack)

Thus, if we call `errorWithCallStack` we will get a formatted call-stack alongside our error message.

    >>> errorWithCallStack "die"
    *** Exception: die
    CallStack (from HasCallStack):
      errorWithCallStack, called at <interactive>:2:1 in interactive:Ghci1

GHC solves `HasCallStack` constraints in three steps:

1. If there is a `CallStack` in scope -- i.e. the enclosing function has a `HasCallStack` constraint -- GHC will append the new call-site to the existing `CallStack`.
2. If there is no `CallStack` in scope -- e.g. in the GHCi session above -- and the enclosing definition does not have an explicit type signature, GHC will infer a `HasCallStack` constraint for the enclosing definition (subject to the monomorphism restriction).
3. If there is no `CallStack` in scope and the enclosing definition has an explicit type signature, GHC will solve the `HasCallStack` constraint for the singleton `CallStack` containing just the current call-site.

`CallStack`s do not interact with the RTS and do not require compilation with `-prof`. On the other hand, as they are built up explicitly via the `HasCallStack` constraints, they will generally not contain as much information as the simulated call-stacks maintained by the RTS.

A `CallStack` is a `[(String, SrcLoc)]`. The `String` is the name of function that was called, the `SrcLoc` is the call-site. The list is ordered with the most recently called function at the head.

NOTE: The intrepid user may notice that `HasCallStack` is just an alias for an implicit parameter `?callStack :: CallStack`. This is an implementation detail and should not be considered part of the `CallStack` API, we may decide to change the implementation in the future.

Since: 4.8.1.0

Instances

IsList CallStack | Be aware that 'fromList . toList = id' only for unfrozen `CallStack`s, since `toList` removes frozenness information. Since: 4.9.0.0
Associated Types type Item CallStack :: * Source # Methods fromList :: [Item CallStack] -> CallStack Source # fromListN :: Int -> [Item CallStack] -> CallStack Source # toList :: CallStack -> [Item CallStack] Source #
Show CallStack | Since: 4.9.0.0
Methods showsPrec :: Int -> CallStack -> ShowS Source # show :: CallStack -> String Source # showList :: [CallStack] -> ShowS Source #
NFData CallStack | Since: 1.4.2.0
Methods rnf :: CallStack -> () Source #
type Item CallStack |
type Item CallStack = (String, SrcLoc)

type HasCallStack = HasCallStack #

A compatibility wrapper for the `GHC.Stack.HasCallStack` constraint.

type HasDebugCallStack = (() :: Constraint) #

A call stack constraint, but only when `isDebugOn`.

prettyCurrentCallStack :: HasCallStack => String #

Pretty-print the current callstack

# Utils for flags

data OverridingBool #

Constructors

Auto |
Always |
Never |

Instances

Show OverridingBool # |
Methods showsPrec :: Int -> OverridingBool -> ShowS Source # show :: OverridingBool -> String Source # showList :: [OverridingBool] -> ShowS Source #

overrideWith :: Bool -> OverridingBool -> Bool #