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

Modulesafe-0.3.21Haskell2010

Safe

A module wrapping Prelude/Data.List functions that can throw exceptions, such as head and !!. Each unsafe function has up to five variants, e.g. with tail:

  • tail :: [a] -> [a], raises an error on tail [], as provided by Prelude.

  • tailErr :: [a] -> [a], alias for tail that doesn't trigger an x-partial warning and does raise errors.

  • tailMay :: [a] -> Maybe [a], turns errors into Nothing.

  • tailDef :: [a] -> [a] -> [a], takes a default to return on errors.

  • tailNote :: Partial => String -> [a] -> [a], takes an extra argument which supplements the error message.

  • tailSafe :: [a] -> [a], returns some sensible default if possible, [] in the case of tail.

All functions marked with the Partial constraint are not total, and will produce stack traces on error, on GHC versions which support them (see GHC.Stack).

This module also introduces some new functions, documented at the top of the module.

  • 91 values
  • Packagesafe-0.3.21
  • Exports91
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceSafe.hs

New functions

6 declarations
valueabort :: Partial => String -> a
#

Synonym for error. Used for instances where the program has decided to exit because of invalid user input, or the user pressed quit etc. This function allows error to be reserved for programmer errors.

valueat :: Partial => [a] -> Int -> a
#

Synonym for !!, but includes more information in the error message.

valuelookupJust :: (Eq a, Partial) => a -> [(a, b)] -> b
#
lookupJust key = fromJust . lookup key
valuefindJust :: (a -> Bool) -> [a] -> a
#
findJust op = fromJust . find op

Partial functions

2 declarations
valuetailErr :: Partial => [a] -> [a]
#

Identical to tail, namely that fails on an empty list. Useful to avoid the x-partial warning introduced in GHC 9.8.

tailErr [] = error "Prelude.tail: empty list"
tailErr [1,2,3] = [2,3]
valueheadErr :: Partial => [a] -> a
#

Identical to head, namely that fails on an empty list. Useful to avoid the x-partial warning introduced in GHC 9.8.

headErr [] = error "Prelude.head: empty list"
headErr [1,2,3] = 1

Safe wrappers

79 declarations
valuetailMay :: [a] -> Maybe [a]
#
tailMay [] = Nothing
tailMay [1,3,4] = Just [3,4]
valuetailDef :: [a] -> [a] -> [a]
#
tailDef [12] [] = [12]
tailDef [12] [1,3,4] = [3,4]
valuetailNote :: Partial => String -> [a] -> [a]
#
tailNote "help me" [] = error "Safe.tailNote [], help me"
tailNote "help me" [1,3,4] = [3,4]
valuetailSafe :: [a] -> [a]
#
tailSafe [] = []
tailSafe [1,3,4] = [3,4]
valueminimumBoundBy :: a -> (a -> a -> Ordering) -> [a] -> a
#

The smallest element of a list with respect to the given comparison function. The result is bounded by the value given as the first argument.

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

The largest element of a list with respect to the given comparison function. The result is bounded by the value given as the first argument.

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

The largest element of a list. The result is bounded by the value given as the first argument.

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

The smallest element of a list. The result is bounded by the value given as the first argument.

valuefoldr1Def :: a -> (a -> a -> a) -> [a] -> a
#

Deprecated. Use foldr1May instead.

valuefoldl1Def :: a -> (a -> a -> a) -> [a] -> a
#

Deprecated. Use foldl1May instead.

valuefoldl1Def' :: a -> (a -> a -> a) -> [a] -> a
#

Deprecated. Use foldl1May' instead.

valuescanl1Def :: [a] -> (a -> a -> a) -> [a] -> [a]
#
valuescanr1Def :: [a] -> (a -> a -> a) -> [a] -> [a]
#
valuefromJustDef :: a -> Maybe a -> a
#

An alternative name for fromMaybe, to fit the naming scheme of this package. Generally using fromMaybe directly would be considered better style.

Discouraged

4 declarations