IsEmpty

IsEmpty is a concept from functional programming that extends PlusEmpty, adding an emptiness-checking operation. It allows determining whether a value within a certain context is "empty" (e.g., absence of a value, an empty list, a failed computation, etc.).

Main characteristics of IsEmpty:

Examples of IsEmpty

Formal Definition

IsEmpty is a type class that allows checking whether a type with an empty representation is truly empty. Extends PlusEmpty.

The laws of isEmpty (besides the parent laws) describe how the emptiness-checking operation should behave:

Code

trait IsEmpty[F[_]] extends PlusEmpty[F]:
  def isEmpty[A](fa: F[A]): Boolean

Laws

Example with List

given IsEmpty[List] with
  override def isEmpty[A](fa: List[A]): Boolean = fa.isEmpty
  override def plus[A](fa1: List[A], fa2: => List[A]): List[A] = fa1 ++ fa2
  override def empty[A]: List[A] = List.empty[A]

Schema

classDiagram
    class Plus~A~{
        +plus[A](fa1: F[A], fa2: => F[A]) F[A]
    }
    class PlusEmpty~A~{
        +empty[A]: F[A]
    }
    Plus <|-- PlusEmpty
    class IsEmpty~A~{
        +isEmpty[A](fa: F[A]) Boolean
    }
    PlusEmpty <|-- IsEmpty

Library Implementations

Implementation in ScalaZ

import scalaz.*
import Scalaz.*

// ... All parent operations

summon[IsEmpty[List]].isEmpty(List.empty[Int]) // true

References: