HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

ModuleCabal-syntax-3.12.1.0Haskell2010

Distribution.Utils.Generic

A large and somewhat miscellaneous collection of utility functions used throughout the rest of the Cabal lib and in other tools that use the Cabal lib like cabal-install. It has a very simple set of logging actions. It has low level functions for running programs, a bunch of wrappers for various directory and file functions that do extra logging.

  • 44 values

reading and writing files safely

2 declarations
valuewithFileContents :: FilePath -> (String -> IO a) -> IO a
#

Gets the contents of a file, but guarantee that it gets closed.

The file is read lazily but if it is not fully consumed by the action then the remaining input is truncated and the file is closed.

valuewriteFileAtomic :: FilePath -> ByteString -> IO ()
#

Writes a file atomically.

The file is either written successfully or an IO exception is raised and the original file is left unchanged.

On windows it is not possible to delete a file that is open by a process. This case will give an IO exception but the atomic property is not affected.

Unicode

0 declarations

Conversions

valuefromUTF8BS :: ByteString -> String
#

Decode String from UTF8-encoded BS.ByteString

Invalid data in the UTF8 stream (this includes code-points U+D800 through U+DFFF) will be decoded as the replacement character (U+FFFD).

File I/O

BOM

Misc

generic utils

27 declarations
valuedropWhileEndLE :: (a -> Bool) -> [a] -> [a]
#

dropWhileEndLE p is equivalent to reverse . dropWhile p . reverse, but quite a bit faster. The difference between "Data.List.dropWhileEnd" and this version is that the one in Data.List is strict in elements, but spine-lazy, while this one is spine-strict but lazy in elements. That's what LE stands for - "lazy in elements".

Example:

Example1 expression
safeTail $ Data.List.dropWhileEnd (<3) [undefined, 5, 4, 3, 2, 1]*** Exception: Prelude.undefined...
Example1 expression
safeTail $ dropWhileEndLE (<3) [undefined, 5, 4, 3, 2, 1][5,4,3]
Example1 expression
take 3 $ Data.List.dropWhileEnd (<3) [5, 4, 3, 2, 1, undefined][5,4,3]
Example1 expression
take 3 $ dropWhileEndLE (<3) [5, 4, 3, 2, 1, undefined]*** Exception: Prelude.undefined...
valuetakeWhileEndLE :: (a -> Bool) -> [a] -> [a]
#

takeWhileEndLE p is equivalent to reverse . takeWhile p . reverse, but is usually faster (as well as being easier to read).

valuecomparing :: Ord a => (b -> a) -> b -> b -> Ordering
#
comparing p x y = compare (p x) (p y)

Useful combinator for use in conjunction with the xxxBy family of functions from Data.List, for example:

  ... sortBy (comparing fst) ...
valueisInfixOf :: Eq a => [a] -> [a] -> Bool
#

The isInfixOf function takes two lists and returns True iff the first list is contained, wholly and intact, anywhere within the second.

Examples
Example1 expression
isInfixOf "Haskell" "I really like Haskell."True
Example1 expression
isInfixOf "Ial" "I really like Haskell."False

For the result to be True, the first list must be finite; for the result to be False, the second list must be finite:

Example1 expression
[20..50] `isInfixOf` [0..]True
Example1 expression
[0..] `isInfixOf` [20..50]False
Example1 expression
[0..] `isInfixOf` [0..]* Hangs forever *
valueintercalate :: [a] -> [[a]] -> [a]
#

intercalate xs xss is equivalent to (concat (intersperse xs xss)). It inserts the list xs in between the lists in xss and concatenates the result.

Laziness

intercalate has the following properties:

Example1 expression
take 5 (intercalate undefined ("Lorem" : undefined))"Lorem"
Example1 expression
take 6 (intercalate ", " ("Lorem" : undefined))"Lorem*** Exception: Prelude.undefined
Examples
Example1 expression
intercalate ", " ["Lorem", "ipsum", "dolor"]"Lorem, ipsum, dolor"
Example1 expression
intercalate [0, 1] [[2, 3], [4, 5, 6], []][2,3,0,1,4,5,6,0,1]
Example1 expression
intercalate [1, 2, 3] [[], []][1,2,3]
valueisAsciiAlphaNum :: Char -> Bool
#

Ascii letters and digits.

Example1 expression
isAsciiAlphaNum 'a'True
Example1 expression
isAsciiAlphaNum 'ä'False
valuelistUnion :: Ord a => [a] -> [a] -> [a]
#

Like "Data.List.union", but has O(n log n) complexity instead of O(n^2).

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

A right-biased version of listUnion.

Example:

Example1 expression
listUnion [1,2,3,4,3] [2,1,1][1,2,3,4,3]
Example1 expression
listUnionRight [1,2,3,4,3] [2,1,1][4,3,2,1,1]
valueordNub :: Ord a => [a] -> [a]
#

Like nub, but has O(n log n) complexity instead of O(n^2). Code for ordNub and listUnion taken from Niklas Hambüchen's ordnub package.

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

Like ordNub and nubBy. Selects a key for each element and takes the nub based on that key.

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

A right-biased version of ordNub.

Example:

Example1 expression
ordNub [1,2,1] :: [Int][1,2]
Example1 expression
ordNubRight [1,2,1] :: [Int][2,1]
valuewrapText :: String -> String
#

Wraps text to the default line width. Existing newlines are preserved.

valuewrapLine :: Int -> [String] -> [[String]]
#

Wraps a list of words to a list of lines of words of a particular width.

valueunfoldrM :: Monad m => (b -> m (Maybe (a, b))) -> b -> m [a]
#

unfoldr with monadic action.

Example1 expression
take 5 $ unfoldrM (\b r -> Just (r + b, b + 1)) (1 :: Int) 2[3,4,5,6,7]
valuespanMaybe :: (a -> Maybe b) -> [a] -> ([b], [a])
#

Like span but with Maybe predicate

Example1 expression
spanMaybe listToMaybe [[1,2],[3],[],[4,5],[6,7]]([1,3],[[],[4,5],[6,7]])
Example1 expression
spanMaybe (readMaybe :: String -> Maybe Int) ["1", "2", "foo"]([1,2],["foo"])
valuebreakMaybe :: (a -> Maybe b) -> [a] -> ([a], Maybe (b, [a]))
#

Like break, but with Maybe predicate

Example1 expression
breakMaybe (readMaybe :: String -> Maybe Int) ["foo", "bar", "1", "2", "quu"](["foo","bar"],Just (1,["2","quu"]))
Example1 expression
breakMaybe (readMaybe :: String -> Maybe Int) ["foo", "bar"](["foo","bar"],Nothing)
valueunsnoc :: [a] -> Maybe ([a], a)
#

The opposite of snoc, which is the reverse of cons

Example:

Example1 expression
unsnoc [1, 2, 3]Just ([1,2],3)
Example1 expression
unsnoc []Nothing
valueunsnocNE :: NonEmpty a -> ([a], a)
#

Like unsnoc, but for NonEmpty so without the Maybe

Example:

Example1 expression
unsnocNE (1 :| [2, 3])([1,2],3)
Example1 expression
unsnocNE (1 :| [])([],1)

Triples

3 declarations
valuefstOf3 :: (a, b, c) -> a
#
valuesndOf3 :: (a, b, c) -> b
#
valuetrdOf3 :: (a, b, c) -> c
#

FilePath stuff

2 declarations

isAbsoluteOnAnyPlatform and isRelativeOnAnyPlatform are like System.FilePath.isAbsolute and System.FilePath.isRelative but have platform independent heuristics. The System.FilePath exists in two versions, Windows and Posix. The two versions don't agree on what is a relative path and we don't know if we're given Windows or Posix paths. This results in false positives when running on Posix and inspecting Windows paths, like the hackage server does. System.FilePath.Posix.isAbsolute "C:\hello" == False System.FilePath.Windows.isAbsolute "/hello" == False This means that we would treat paths that start with "/" to be absolute. On Posix they are indeed absolute, while on Windows they are not.

The portable versions should be used when we might deal with paths that are from another OS than the host OS. For example, the Hackage Server deals with both Windows and Posix paths while performing the PackageDescription checks. In contrast, when we run 'cabal configure' we do expect the paths to be correct for our OS and we should not have to use the platform independent heuristics.