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

Language | Haskell2010 |

## Synopsis

- (&) ∷ a → (a → b) → b
- (&&&) ∷ Arrow a ⇒ a b c → a b c' → a b (c, c')
- (>>>) ∷ ∀ {k} cat (a ∷ k) (b ∷ k) (c ∷ k). Category cat ⇒ cat a b → cat b c → cat a c
- (<&>) ∷ Functor f ⇒ f a → (a → b) → f b
- toList ∷ Foldable t ⇒ t a → [a]
- first ∷ Bifunctor p ⇒ (a → b) → p a c → p b c
- second ∷ Bifunctor p ⇒ (b → c) → p a b → p a c
- on ∷ (b → b → c) → (a → b) → a → a → c
- isNothing ∷ Maybe a → Bool
- isJust ∷ Maybe a → Bool
- fromMaybe ∷ a → Maybe a → a
- guard ∷ Alternative f ⇒ Bool → f ()
- foldl' ∷ Foldable t ⇒ (b → a → b) → b → t a → b
- for_ ∷ (Foldable t, Applicative f) ⇒ t a → (a → f b) → f ()
- traverse_ ∷ (Foldable t, Applicative f) ⇒ (a → f b) → t a → f ()
- fold ∷ (Foldable t, Monoid m) ⇒ t m → m
- for ∷ (Traversable t, Applicative f) ⇒ t a → (a → f b) → f (t b)
- throw ∷ ∀ (r ∷ RuntimeRep) (a ∷ TYPE r) e. Exception e ⇒ e → a
- join ∷ Monad m ⇒ m (m a) → m a
- (<=<) ∷ Monad m ⇒ (b → m c) → (a → m b) → a → m c
- (>=>) ∷ Monad m ⇒ (a → m b) → (b → m c) → a → m c
- ($>) ∷ Functor f ⇒ f a → b → f b
- fromRight ∷ b → Either a b → b
- isRight ∷ Either a b → Bool
- isLeft ∷ Either a b → Bool
- void ∷ Functor f ⇒ f a → f ()
- through ∷ Functor f ⇒ (a → f b) → a → f a
- coerce ∷ ∀ {k ∷ RuntimeRep} (a ∷ TYPE k) (b ∷ TYPE k). Coercible a b ⇒ a → b
- coerceVia ∷ Coercible a b ⇒ (a → b) → a → b
- coerceArg ∷ Coercible a b ⇒ (a → s) → b → s
- coerceRes ∷ Coercible s t ⇒ (a → s) → a → t
- class Generic a
- class NFData a
- data Natural
- data NonEmpty a = a :| [a]
- data Word8
- class Applicative f ⇒ Alternative (f ∷ Type → Type) where
- class (Typeable e, Show e) ⇒ Exception e
- newtype PairT b f a = PairT {
- unPairT ∷ f (b, a)

- class a ~R# b ⇒ Coercible (a ∷ k) (b ∷ k)
- class Typeable (a ∷ k)
- type Lens' s a = Lens s s a a
- lens ∷ (s → a) → (s → b → t) → Lens s t a b
- (^.) ∷ s → Getting a s a → a
- view ∷ MonadReader s m ⇒ Getting a s a → m a
- (.~) ∷ ASetter s t a b → b → s → t
- set ∷ ASetter s t a b → b → s → t
- (%~) ∷ ASetter s t a b → (a → b) → s → t
- over ∷ ASetter s t a b → (a → b) → s → t
- (<^>) ∷ Fold s a → Fold s a → Fold s a
- traceShowId ∷ Show a ⇒ a → a
- trace ∷ String → a → a
- (.*) ∷ (c → d) → (a → b → c) → a → b → d
- (<<$>>) ∷ (Functor f1, Functor f2) ⇒ (a → b) → f1 (f2 a) → f1 (f2 b)
- (<<*>>) ∷ (Applicative f1, Applicative f2) ⇒ f1 (f2 (a → b)) → f1 (f2 a) → f1 (f2 b)
- mtraverse ∷ (Monad m, Traversable m, Applicative f) ⇒ (a → f (m b)) → m a → f (m b)
- foldMapM ∷ (Foldable f, Monad m, Monoid b) ⇒ (a → m b) → f a → m b
- reoption ∷ (Foldable f, Alternative g) ⇒ f a → g a
- enumerate ∷ (Enum a, Bounded a) ⇒ [a]
- tabulateArray ∷ (Bounded i, Enum i, Ix i) ⇒ (i → a) → Array i a
- (?) ∷ Alternative f ⇒ Bool → a → f a
- ensure ∷ Alternative f ⇒ (a → Bool) → a → f a
- asksM ∷ MonadReader r m ⇒ (r → m a) → m a
- timesA ∷ Natural → (a → a) → a → a
- data Doc ann
- newtype ShowPretty a = ShowPretty {
- unShowPretty ∷ a

- class Pretty a where
- pretty ∷ a → Doc ann
- prettyList ∷ [a] → Doc ann

- class PrettyBy config a where
- prettyBy ∷ config → a → Doc ann
- prettyListBy ∷ config → [a] → Doc ann

- type family HasPrettyDefaults config ∷ Bool
- type PrettyDefaultBy config = DispatchPrettyDefaultBy (NonStuckHasPrettyDefaults config) config
- newtype PrettyAny a = PrettyAny {
- unPrettyAny ∷ a

- class Render str where
- display ∷ ∀ str a. (Pretty a, Render str) ⇒ a → str
- printPretty ∷ Pretty a ⇒ a → IO ()
- showText ∷ Show a ⇒ a → Text
- class Default a where
- def ∷ a

- zipExact ∷ [a] → [b] → Maybe [(a, b)]
- allSame ∷ Eq a ⇒ [a] → Bool
- distinct ∷ Eq a ⇒ [a] → Bool
- unsafeFromRight ∷ Show e ⇒ Either e a → a
- tryError ∷ MonadError e m ⇒ m a → m (Either e a)
- lowerInitialChar ∷ String → String

# Reexports from base

(&&&) ∷ Arrow a ⇒ a b c → a b c' → a b (c, c') infixr 3 Source #

Fanout: send the input to both argument arrows and combine their output.

The default definition may be overridden with a more efficient version if desired.

(>>>) ∷ ∀ {k} cat (a ∷ k) (b ∷ k) (c ∷ k). Category cat ⇒ cat a b → cat b c → cat a c infixr 1 Source #

Left-to-right composition

toList ∷ Foldable t ⇒ t a → [a] Source #

List of elements of a structure, from left to right. If the entire list is intended to be reduced via a fold, just fold the structure directly bypassing the list.

#### Examples

Basic usage:

`>>>`

[]`toList Nothing`

`>>>`

[42]`toList (Just 42)`

`>>>`

[]`toList (Left "foo")`

`>>>`

[5,17,12,8]`toList (Node (Leaf 5) 17 (Node Empty 12 (Leaf 8)))`

For lists, `toList`

is the identity:

`>>>`

[1,2,3]`toList [1, 2, 3]`

*Since: base-4.8.0.0*

fromMaybe ∷ a → Maybe a → a Source #

The `fromMaybe`

function takes a default value and a `Maybe`

value. If the `Maybe`

is `Nothing`

, it returns the default value;
otherwise, it returns the value contained in the `Maybe`

.

#### Examples

Basic usage:

`>>>`

"Hello, World!"`fromMaybe "" (Just "Hello, World!")`

`>>>`

""`fromMaybe "" Nothing`

Read an integer from a string using `readMaybe`

. If we fail to
parse an integer, we want to return `0`

by default:

`>>>`

`import Text.Read ( readMaybe )`

`>>>`

5`fromMaybe 0 (readMaybe "5")`

`>>>`

0`fromMaybe 0 (readMaybe "")`

guard ∷ Alternative f ⇒ Bool → f () Source #

Conditional failure of `Alternative`

computations. Defined by

guard True =`pure`

() guard False =`empty`

#### Examples

Common uses of `guard`

include conditionally signaling an error in
an error monad and conditionally rejecting the current choice in an
`Alternative`

-based parser.

As an example of signaling an error in the error monad `Maybe`

,
consider a safe division function `safeDiv x y`

that returns
`Nothing`

when the denominator `y`

is zero and

otherwise. For example:`Just`

(x `div`
y)

`>>>`

Nothing`safeDiv 4 0`

`>>>`

Just 2`safeDiv 4 2`

A definition of `safeDiv`

using guards, but not `guard`

:

safeDiv :: Int -> Int -> Maybe Int safeDiv x y | y /= 0 = Just (x `div` y) | otherwise = Nothing

A definition of `safeDiv`

using `guard`

and `Monad`

`do`

-notation:

safeDiv :: Int -> Int -> Maybe Int safeDiv x y = do guard (y /= 0) return (x `div` y)

foldl' ∷ Foldable t ⇒ (b → a → b) → b → t a → b Source #

Left-associative fold of a structure but with strict application of the operator.

This ensures that each step of the fold is forced to Weak Head Normal
Form before being applied, avoiding the collection of thunks that would
otherwise occur. This is often what you want to strictly reduce a
finite structure to a single strict result (e.g. `sum`

).

For a general `Foldable`

structure this should be semantically identical
to,

foldl' f z =`foldl'`

f z .`toList`

*Since: base-4.6.0.0*

for_ ∷ (Foldable t, Applicative f) ⇒ t a → (a → f b) → f () Source #

`for_`

is `traverse_`

with its arguments flipped. For a version
that doesn't ignore the results see `for`

. This
is `forM_`

generalised to `Applicative`

actions.

`for_`

is just like `forM_`

, but generalised to `Applicative`

actions.

#### Examples

Basic usage:

`>>>`

1 2 3 4`for_ [1..4] print`

traverse_ ∷ (Foldable t, Applicative f) ⇒ (a → f b) → t a → f () Source #

Map each element of a structure to an `Applicative`

action, evaluate these
actions from left to right, and ignore the results. For a version that
doesn't ignore the results see `traverse`

.

`traverse_`

is just like `mapM_`

, but generalised to `Applicative`

actions.

#### Examples

Basic usage:

`>>>`

"Hello" "world" "!"`traverse_ print ["Hello", "world", "!"]`

fold ∷ (Foldable t, Monoid m) ⇒ t m → m Source #

Given a structure with elements whose type is a `Monoid`

, combine them
via the monoid's `(`

operator. This fold is right-associative and
lazy in the accumulator. When you need a strict left-associative fold,
use `<>`

)`foldMap'`

instead, with `id`

as the map.

#### Examples

Basic usage:

`>>>`

[1,2,3,4,5,6]`fold [[1, 2, 3], [4, 5], [6], []]`

`>>>`

Sum {getSum = 9}`fold $ Node (Leaf (Sum 1)) (Sum 3) (Leaf (Sum 5))`

Folds of unbounded structures do not terminate when the monoid's
`(`

operator is strict:`<>`

)

`>>>`

* Hangs forever *`fold (repeat Nothing)`

Lazy corecursive folds of unbounded structures are fine:

`>>>`

[0,1,2,1,2,3,2,3,4,3,4,5]`take 12 $ fold $ map (\i -> [i..i+2]) [0..]`

`>>>`

2666668666666`sum $ take 4000000 $ fold $ map (\i -> [i..i+2]) [0..]`

for ∷ (Traversable t, Applicative f) ⇒ t a → (a → f b) → f (t b) Source #

throw ∷ ∀ (r ∷ RuntimeRep) (a ∷ TYPE r) e. Exception e ⇒ e → a Source #

Throw an exception. Exceptions may be thrown from purely
functional code, but may only be caught within the `IO`

monad.

WARNING: You may want to use `throwIO`

instead so that your pure code
stays exception-free.

join ∷ Monad m ⇒ m (m a) → m a Source #

The `join`

function is the conventional monad join operator. It
is used to remove one level of monadic structure, projecting its
bound argument into the outer level.

'

' can be understood as the `join`

bss`do`

expression

do bs <- bss bs

#### Examples

A common use of `join`

is to run an `IO`

computation returned from
an `STM`

transaction, since `STM`

transactions
can't perform `IO`

directly. Recall that

`atomically`

:: STM a -> IO a

is used to run `STM`

transactions atomically. So, by
specializing the types of `atomically`

and `join`

to

`atomically`

:: STM (IO b) -> IO (IO b)`join`

:: IO (IO b) -> IO b

we can compose them as

`join`

.`atomically`

:: STM (IO b) -> IO b

(>=>) ∷ Monad m ⇒ (a → m b) → (b → m c) → a → m c infixr 1 Source #

Left-to-right composition of Kleisli arrows.

'`(bs `

' can be understood as the `>=>`

cs) a`do`

expression

do b <- bs a cs b

($>) ∷ Functor f ⇒ f a → b → f b infixl 4 Source #

Flipped version of `<$`

.

#### Examples

Replace the contents of a

with a constant
`Maybe`

`Int`

`String`

:

`>>>`

Nothing`Nothing $> "foo"`

`>>>`

Just "foo"`Just 90210 $> "foo"`

Replace the contents of an

with a constant `Either`

`Int`

`Int`

`String`

, resulting in an

:`Either`

`Int`

`String`

`>>>`

Left 8675309`Left 8675309 $> "foo"`

`>>>`

Right "foo"`Right 8675309 $> "foo"`

Replace each element of a list with a constant `String`

:

`>>>`

["foo","foo","foo"]`[1,2,3] $> "foo"`

Replace the second element of a pair with a constant `String`

:

`>>>`

(1,"foo")`(1,2) $> "foo"`

*Since: base-4.7.0.0*

fromRight ∷ b → Either a b → b Source #

Return the contents of a `Right`

-value or a default value otherwise.

#### Examples

Basic usage:

`>>>`

3`fromRight 1 (Right 3)`

`>>>`

1`fromRight 1 (Left "foo")`

*Since: base-4.10.0.0*

isRight ∷ Either a b → Bool Source #

Return `True`

if the given value is a `Right`

-value, `False`

otherwise.

#### Examples

Basic usage:

`>>>`

False`isRight (Left "foo")`

`>>>`

True`isRight (Right 3)`

Assuming a `Left`

value signifies some sort of error, we can use
`isRight`

to write a very simple reporting function that only
outputs "SUCCESS" when a computation has succeeded.

This example shows how `isRight`

might be used to avoid pattern
matching when one does not care about the value contained in the
constructor:

`>>>`

`import Control.Monad ( when )`

`>>>`

`let report e = when (isRight e) $ putStrLn "SUCCESS"`

`>>>`

`report (Left "parse error")`

`>>>`

SUCCESS`report (Right 1)`

*Since: base-4.7.0.0*

isLeft ∷ Either a b → Bool Source #

Return `True`

if the given value is a `Left`

-value, `False`

otherwise.

#### Examples

Basic usage:

`>>>`

True`isLeft (Left "foo")`

`>>>`

False`isLeft (Right 3)`

Assuming a `Left`

value signifies some sort of error, we can use
`isLeft`

to write a very simple error-reporting function that does
absolutely nothing in the case of success, and outputs "ERROR" if
any error occurred.

This example shows how `isLeft`

might be used to avoid pattern
matching when one does not care about the value contained in the
constructor:

`>>>`

`import Control.Monad ( when )`

`>>>`

`let report e = when (isLeft e) $ putStrLn "ERROR"`

`>>>`

`report (Right 1)`

`>>>`

ERROR`report (Left "parse error")`

*Since: base-4.7.0.0*

void ∷ Functor f ⇒ f a → f () Source #

discards or ignores the result of evaluation, such
as the return value of an `void`

value`IO`

action.

#### Examples

Replace the contents of a

with unit:`Maybe`

`Int`

`>>>`

Nothing`void Nothing`

`>>>`

Just ()`void (Just 3)`

Replace the contents of an

with unit, resulting in an `Either`

`Int`

`Int`

:`Either`

`Int`

`()`

`>>>`

Left 8675309`void (Left 8675309)`

`>>>`

Right ()`void (Right 8675309)`

Replace every element of a list with unit:

`>>>`

[(),(),()]`void [1,2,3]`

Replace the second element of a pair with unit:

`>>>`

(1,())`void (1,2)`

Discard the result of an `IO`

action:

`>>>`

1 2 [(),()]`mapM print [1,2]`

`>>>`

1 2`void $ mapM print [1,2]`

through ∷ Functor f ⇒ (a → f b) → a → f a Source #

Makes an effectful function ignore its result value and return its input value.

coerce ∷ ∀ {k ∷ RuntimeRep} (a ∷ TYPE k) (b ∷ TYPE k). Coercible a b ⇒ a → b Source #

The function `coerce`

allows you to safely convert between values of
types that have the same representation with no run-time overhead. In the
simplest case you can use it instead of a newtype constructor, to go from
the newtype's concrete type to the abstract type. But it also works in
more complicated settings, e.g. converting a list of newtypes to a list of
concrete types.

When used in conversions involving a newtype wrapper, make sure the newtype constructor is in scope.

This function is representation-polymorphic, but the
`RuntimeRep`

type argument is marked as `Inferred`

, meaning
that it is not available for visible type application. This means
the typechecker will accept

.`coerce`

@`Int`

@Age 42

### Examples

`>>>`

`newtype TTL = TTL Int deriving (Eq, Ord, Show)`

`>>>`

`newtype Age = Age Int deriving (Eq, Ord, Show)`

`>>>`

TTL 42`coerce (Age 42) :: TTL`

`>>>`

TTL 43`coerce (+ (1 :: Int)) (Age 42) :: TTL`

`>>>`

[TTL 43,TTL 25]`coerce (map (+ (1 :: Int))) [Age 42, Age 24] :: [TTL]`

coerceVia ∷ Coercible a b ⇒ (a → b) → a → b Source #

Coerce the second argument to the result type of the first one. The motivation for this
function is that it's often more annoying to explicitly specify a target type for `coerce`

than
to construct an explicit coercion function, so this combinator can be used in cases like that.
Plus the code reads better, as it becomes clear what and where gets wrapped/unwrapped.

coerceArg ∷ Coercible a b ⇒ (a → s) → b → s Source #

Same as `f -> f . coerce`

, but does not create any closures and so is completely free.

coerceRes ∷ Coercible s t ⇒ (a → s) → a → t Source #

Same as `f -> coerce . f`

, but does not create any closures and so is completely free.

Representable types of kind `*`

.
This class is derivable in GHC with the `DeriveGeneric`

flag on.

A `Generic`

instance must satisfy the following laws:

`from`

.`to`

≡`id`

`to`

.`from`

≡`id`