.. index:: single: maybe
.. _maybe/0:

.. rst-class:: right

**object**

``maybe``
=========

Types and predicates for type-checking and handling optional terms. Inspired by Haskell.

| **Availability:** 
|    ``logtalk_load(optionals(loader))``

| **Author:** Paulo Moura
| **Version:** 0:9:0
| **Date:** 2025-06-19

| **Compilation flags:**
|    ``static, context_switching_calls``


| **Provides:**
|    :ref:`type::type/1 <type/0::type/1>`
|    :ref:`type::check/2 <type/0::check/2>`
|    :ref:`arbitrary::arbitrary/1 <arbitrary/0::arbitrary/1>`
|    :ref:`arbitrary::arbitrary/2 <arbitrary/0::arbitrary/2>`
|    :ref:`arbitrary::shrinker/1 <arbitrary/0::shrinker/1>`
|    :ref:`arbitrary::shrink/3 <arbitrary/0::shrink/3>`
|    :ref:`arbitrary::edge_case/2 <arbitrary/0::edge_case/2>`
| **Uses:**
|    :ref:`optional <optional/0>`
|    :ref:`optional(Optional) <optional/1>`
|    :ref:`random <random/0>`
|    :ref:`type <type/0>`

| **Remarks:**

   - Type-checking support: Defines type ``maybe(Type)`` for checking optional terms where the value hold by the optional term must be of the given type.
   - QuickCheck support: Defines clauses for the ``arbitrary::arbitrary/1-2``, ``arbitrary::shrinker/1``, ``arbitrary::shrink/3``, and ``arbitrary::edge_case/2`` predicates to allow generating random values for the ``maybe(Type)`` type.

| **Inherited public predicates:**
|    (none)

.. contents::
   :local:
   :backlinks: top

Public predicates
-----------------

.. index:: cat/2
.. _maybe/0::cat/2:

``cat/2``
^^^^^^^^^

Returns the values stored in the non-empty optional terms.

| **Compilation flags:**
|    ``static``

| **Template:**
|    ``cat(Optionals,Values)``
| **Mode and number of proofs:**
|    ``cat(+list(optional),-list)`` - ``one``


------------

.. index:: sequence/2
.. _maybe/0::sequence/2:

``sequence/2``
^^^^^^^^^^^^^^

Returns an optional term with a list of all values when all optional terms are not empty. Otherwise returns an empty optional term.

| **Compilation flags:**
|    ``static``

| **Template:**
|    ``sequence(Optionals,Optional)``
| **Mode and number of proofs:**
|    ``sequence(+list(optional),--nonvar)`` - ``one``


------------

.. index:: traverse/3
.. _maybe/0::traverse/3:

``traverse/3``
^^^^^^^^^^^^^^

Applies a closure to each list element to generate optional terms and then sequences them into a single optional term holding all values or an empty optional term.

| **Compilation flags:**
|    ``static``

| **Template:**
|    ``traverse(Closure,Terms,Optional)``
| **Meta-predicate template:**
|    ``traverse(2,*,*)``
| **Mode and number of proofs:**
|    ``traverse(+callable,+list,--nonvar)`` - ``one``


------------

Protected predicates
--------------------

(no local declarations; see entity ancestors if any)

Private predicates
------------------

(no local declarations; see entity ancestors if any)

Operators
---------

(none)

.. seealso::

   :ref:`optional <optional/0>`, :ref:`optional(Optional) <optional/1>`, :ref:`type <type/0>`, :ref:`arbitrary <arbitrary/0>`

