Synopsis

:- use_module(library(maybe)).
% staff(Name, Spouse)
staff(tom, just(teresa)).
staff(bob, nothing).
staff(sue, just(william)).

?- staff(Name, MaybeSpouse),
   maybe_default_value(MaybeSpouse, '(none)', Spouse).
 Name = tom,
 Spouse = teresa ;
 Name = bob,
 Spouse = '(none)' ;
 Name = sue,
 Spouse = william.

Description

The maybe type encapsulates an optional value. When a value is present, we have just(Value). When it's absent we have nothing. In some circumstances, this can be a more natural model than using Prolog failure. For example, one might model a nullable SQL column using maybe.

This module draws inspiration from similar libraries for Mercury and Haskell. We make predicates available even where it's clearer to use unification. That facilitates using maybe values with maplist and friends.

In addition to the predicates described below, this module defines clauses for the multifile predicate error:has_type/2 which describe the type maybe and maybe(T) where T is a type parameter. It also defines clauses for working with library(quickcheck).

Changes in this Version

Installation

Using SWI-Prolog 6.3 or later:

?- pack_install(maybe).

This module uses semantic versioning.

Source code available and pull requests accepted at http://github.com/mndrix/maybe

author
- Michael Hendricks <michael@ndrix.org>
license
- BSD

Prolog files

maybe.pl
call_maybe/3If call(Goal) succeeds, Maybe=just(Value), otherwise Maybe=nothing.
default_maybe_value/3Like maybe_default_value/3 with different argument order.
fold_maybe/4nothing leaves Accum0=Accum while just relates them via Goal.
is_just/1True if Maybe is just(_).
is_nothing/1True if Maybe is nothing.
just_value/2True if Just wraps Value.
map_maybe/3True if call(Goal, Value0, Value) succeeds for just values.
maybe_default_value/3True if Maybe wraps Value with a Default for the nothing case.
maybe_list/2Relates a List to a Maybe value.
maybe_value/2True if Maybe is just(Value); fails for nothing.