IsNull- A typeclass to determine if a given value is null.

CopyrightJoão Cristóvão, 2014
Safe HaskellSafe-Inferred



A typeclass to determine if a given value is null.

Strongly inspired by mono-traversable but with a simpler goal: supporting IsNull and nested IsNull operations.

While the isNull function is equivalent to (==) mempty for most of the instances, not all Foldables are monoids, and not all monoids mempty means null:

  • Either is an example of a Foldable which is not a Monoid, but where it makes sense to consider a Left as an Null value. While this is not strictly true, the Left option does carries a value, we take the more liberal approach: Empty ~ Null ~ Invalid Value. If you need proper type reasoning, you should not be using this package, just regular pattern matching instead.
  • Product Monoid instance is 1. Hardly qualifies as an Empty or Null value. For this reason no default implementation is provided for the Monoid class. It's up to you to use (==) mempty instead.

The main use case for this package are boolean conditions, namely the if then else construct.

Bugs, suggestions and comments are most welcomed!


class IsNull a where Source

Minimal complete definition



isNull :: a -> Bool Source

isNull ~ isEmpty ~ isInvalid?

>>> isNull (Left 5)
>>> isNull ("abc" :: T.Text)
>>> isNull [""] -- see isNullN

isN :: a -> Bool Source

Typing causes arthritis. Alias for isNull.

notNull :: a -> Bool Source

the logical negation of isNull

isNullN :: Foldable f => f a -> Bool Source

Nested isNull

>>> isNullN (Just "abc")
>>> isNullN (Just "")
>>> isNullN (Nothing :: Maybe String)

notNullN :: Foldable f => f a -> Bool Source

Nested isNotNull

isNullM :: Monad m => m a -> m Bool Source

Monadic isNull

>>> isNullM [""]

isNullNM :: (Monad m, Foldable f) => m (f a) -> m Bool Source

Monadic Nested isNull

(<\>) :: a -> a -> a infixl 3 Source

Alternative's <|> operator does not always operate as choice, at least not in an intuitive way (for example with lists). This one does:

>>> [2,3] <\> [4,5]
>>> [] <\> [4,5]
>>> [] <\> []