Safe Haskell | Safe |
---|---|

Language | Haskell2010 |

A module wrapping `Prelude`

/`Data.List`

functions that can throw exceptions, such as `head`

and `!!`

.
Each unsafe function has up to four variants, e.g. with `tail`

:

, raises an error on`tail`

:: [a] -> [a]`tail []`

.

, turns errors into`tailMay`

:: [a] ->*Maybe*[a]`Nothing`

.

, takes a default to return on errors.`tailDef`

::*[a]*-> [a] -> [a]

, takes an extra argument which supplements the error message.`tailNote`

::`Partial`

=>*String*-> [a] -> [a]

, returns some sensible default if possible,`tailSafe`

:: [a] -> [a]`[]`

in the case of`tail`

.

All functions marked with the

constraint are not total, and will produce stack traces on error, on GHC
versions which support them (see GHC.Stack).`Partial`

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

- abort :: Partial => String -> a
- at :: Partial => [a] -> Int -> a
- lookupJust :: Eq a => a -> [(a, b)] -> b
- findJust :: (a -> Bool) -> [a] -> a
- elemIndexJust :: Eq a => a -> [a] -> Int
- findIndexJust :: (a -> Bool) -> [a] -> Int
- tailMay :: [a] -> Maybe [a]
- tailDef :: [a] -> [a] -> [a]
- tailNote :: Partial => String -> [a] -> [a]
- tailSafe :: [a] -> [a]
- initMay :: [a] -> Maybe [a]
- initDef :: [a] -> [a] -> [a]
- initNote :: Partial => String -> [a] -> [a]
- initSafe :: [a] -> [a]
- headMay :: [a] -> Maybe a
- headDef :: a -> [a] -> a
- headNote :: String -> [a] -> a
- lastMay :: [a] -> Maybe a
- lastDef :: a -> [a] -> a
- lastNote :: String -> [a] -> a
- minimumMay :: Ord a => [a] -> Maybe a
- minimumDef :: Ord a => a -> [a] -> a
- minimumNote :: (Partial, Ord a) => String -> [a] -> a
- maximumMay :: Ord a => [a] -> Maybe a
- maximumDef :: Ord a => a -> [a] -> a
- maximumNote :: (Partial, Ord a) => String -> [a] -> a
- minimumByMay :: (a -> a -> Ordering) -> [a] -> Maybe a
- minimumByDef :: a -> (a -> a -> Ordering) -> [a] -> a
- minimumByNote :: Partial => String -> (a -> a -> Ordering) -> [a] -> a
- maximumByMay :: (a -> a -> Ordering) -> [a] -> Maybe a
- maximumByDef :: a -> (a -> a -> Ordering) -> [a] -> a
- maximumByNote :: Partial => String -> (a -> a -> Ordering) -> [a] -> a
- foldr1May :: (a -> a -> a) -> [a] -> Maybe a
- foldr1Def :: a -> (a -> a -> a) -> [a] -> a
- foldr1Note :: Partial => String -> (a -> a -> a) -> [a] -> a
- foldl1May :: (a -> a -> a) -> [a] -> Maybe a
- foldl1Def :: a -> (a -> a -> a) -> [a] -> a
- foldl1Note :: Partial => String -> (a -> a -> a) -> [a] -> a
- foldl1May' :: (a -> a -> a) -> [a] -> Maybe a
- foldl1Def' :: a -> (a -> a -> a) -> [a] -> a
- foldl1Note' :: Partial => String -> (a -> a -> a) -> [a] -> a
- scanl1May :: (a -> a -> a) -> [a] -> Maybe [a]
- scanl1Def :: [a] -> (a -> a -> a) -> [a] -> [a]
- scanl1Note :: Partial => String -> (a -> a -> a) -> [a] -> [a]
- scanr1May :: (a -> a -> a) -> [a] -> Maybe [a]
- scanr1Def :: [a] -> (a -> a -> a) -> [a] -> [a]
- scanr1Note :: Partial => String -> (a -> a -> a) -> [a] -> [a]
- cycleMay :: [a] -> Maybe [a]
- cycleDef :: [a] -> [a] -> [a]
- cycleNote :: Partial => String -> [a] -> [a]
- fromJustDef :: a -> Maybe a -> a
- fromJustNote :: Partial => String -> Maybe a -> a
- assertNote :: Partial => String -> Bool -> a -> a
- atMay :: [a] -> Int -> Maybe a
- atDef :: a -> [a] -> Int -> a
- atNote :: Partial => String -> [a] -> Int -> a
- readMay :: Read a => String -> Maybe a
- readDef :: Read a => a -> String -> a
- readNote :: (Partial, Read a) => String -> String -> a
- readEitherSafe :: Read a => String -> Either String a
- lookupJustDef :: Eq a => b -> a -> [(a, b)] -> b
- lookupJustNote :: (Partial, Eq a) => String -> a -> [(a, b)] -> b
- findJustDef :: a -> (a -> Bool) -> [a] -> a
- findJustNote :: Partial => String -> (a -> Bool) -> [a] -> a
- elemIndexJustDef :: Eq a => Int -> a -> [a] -> Int
- elemIndexJustNote :: (Partial, Eq a) => String -> a -> [a] -> Int
- findIndexJustDef :: Int -> (a -> Bool) -> [a] -> Int
- findIndexJustNote :: Partial => String -> (a -> Bool) -> [a] -> Int
- toEnumMay :: (Enum a, Bounded a) => Int -> Maybe a
- toEnumDef :: (Enum a, Bounded a) => a -> Int -> a
- toEnumNote :: (Partial, Enum a, Bounded a) => String -> Int -> a
- toEnumSafe :: (Enum a, Bounded a) => Int -> a
- succMay :: (Enum a, Eq a, Bounded a) => a -> Maybe a
- succDef :: (Enum a, Eq a, Bounded a) => a -> a -> a
- succNote :: (Partial, Enum a, Eq a, Bounded a) => String -> a -> a
- succSafe :: (Enum a, Eq a, Bounded a) => a -> a
- predMay :: (Enum a, Eq a, Bounded a) => a -> Maybe a
- predDef :: (Enum a, Eq a, Bounded a) => a -> a -> a
- predNote :: (Partial, Enum a, Eq a, Bounded a) => String -> a -> a
- predSafe :: (Enum a, Eq a, Bounded a) => a -> a

# New functions

at :: Partial => [a] -> Int -> a Source

Synonym for `!!`

, but includes more information in the error message.

lookupJust :: Eq a => a -> [(a, b)] -> b Source

lookupJust key = fromJust . lookup key

elemIndexJust :: Eq a => a -> [a] -> Int Source

elemIndexJust op = fromJust . elemIndex op

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

findIndexJust op = fromJust . findIndex op

# Safe wrappers

tailNote :: Partial => String -> [a] -> [a] Source

tailNote "help me" [] = error "Safe.tailNote [], help me" tailNote "help me" [1,3,4] = [3,4]

minimumMay :: Ord a => [a] -> Maybe a Source

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

minimumNote :: (Partial, Ord a) => String -> [a] -> a Source

maximumMay :: Ord a => [a] -> Maybe a Source

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

maximumNote :: (Partial, Ord a) => String -> [a] -> a Source

minimumByMay :: (a -> a -> Ordering) -> [a] -> Maybe a Source

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

minimumByNote :: Partial => String -> (a -> a -> Ordering) -> [a] -> a Source

maximumByMay :: (a -> a -> Ordering) -> [a] -> Maybe a Source

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

maximumByNote :: Partial => String -> (a -> a -> Ordering) -> [a] -> a Source

foldr1Note :: Partial => String -> (a -> a -> a) -> [a] -> a Source

foldl1Note :: Partial => String -> (a -> a -> a) -> [a] -> a Source

foldl1May' :: (a -> a -> a) -> [a] -> Maybe a Source

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

foldl1Note' :: Partial => String -> (a -> a -> a) -> [a] -> a Source

scanl1Note :: Partial => String -> (a -> a -> a) -> [a] -> [a] Source

scanr1Note :: Partial => String -> (a -> a -> a) -> [a] -> [a] Source

fromJustDef :: a -> Maybe a -> a Source

fromJustNote :: Partial => String -> Maybe a -> a Source

assertNote :: Partial => String -> Bool -> a -> a Source

readNote :: (Partial, Read a) => String -> String -> a Source

`readNote`

uses `readEitherSafe`

for the error message.

readEitherSafe :: Read a => String -> Either String a Source

This function provides a more precise error message than `readEither`

from `base`

.

lookupJustDef :: Eq a => b -> a -> [(a, b)] -> b Source

lookupJustNote :: (Partial, Eq a) => String -> a -> [(a, b)] -> b Source

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

findJustNote :: Partial => String -> (a -> Bool) -> [a] -> a Source

elemIndexJustDef :: Eq a => Int -> a -> [a] -> Int Source

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

toEnumSafe :: (Enum a, Bounded a) => Int -> a Source