Skip to content

std.data

Lists, options, results, and finite maps and sets.

Every family derives equality, ordering, hashing, printing, folds, maps, and traversals. A derived instance of a parametric family takes the instance at each of its type parameters first, as in List.eq[Int](eq_int, xs, ys); this module exports those instances at the builtin types, and every other family gets its own by deriving.

A finite map and a finite set are kept in one canonical form, their keys in strictly increasing order under the order the caller passes (ord_K, a derived Ord instance or one of the builtin ones below), so two maps with the same entries are the same value and the derived instances compare, hash, and print them by their entries. Every operation here keeps that form; a map built directly from its constructors must keep it too. A key type with finitely many values, such as a family of nullary constructors deriving Ord, is ordered like any other, and the operations that need every key (set_complement) take the keys as a list.

The examples on this page run in this module, which opens std.data:

import std.data
open std.data

FiniteMap (family)

family FiniteMap[K : Type, V : Type] : Type deriving (Eq, Ord, Hash, Show, Fold, Map, Traverse)

Constructors:

  • MapCons: constructor MapCons : K * V * FiniteMap[K, V] -> FiniteMap[K, V]
  • MapNil: constructor MapNil : FiniteMap[K, V]

A finite map from keys to values: its entries in strictly increasing order of their keys.

Example: the derived classes.

example
expect FiniteMap.eq(K=Int, V=Bool, eq_K=eq_int, eq_V=eq_bool, x=MapCons(1, true, MapNil), y=MapCons(1, true, MapNil))
expect FiniteMap.ord(K=Int, V=Bool, ord_K=ord_int, ord_V=ord_bool, x=MapNil, y=MapCons(1, true, MapNil)) == -1
expect FiniteMap.show(K=Int, V=Bool, show_K=show_int, show_V=show_bool, x=MapCons(1, true, MapNil)) == "MapCons(1, true, MapNil)"

List (family)

family List[A : Type] : Type deriving (Eq, Ord, Hash, Show, Fold, Map, Traverse)

Constructors:

  • Cons: constructor Cons : A * List[A] -> List[A]
  • Nil: constructor Nil : List[A]

A finite sequence: Nil, or an element Consed onto a list.

Example: the derived classes.

example
expect List.eq(A=Int, eq_A=eq_int, x=Cons(1, Cons(2, Nil)), y=Cons(1, Cons(2, Nil)))
expect not List.eq(A=Int, eq_A=eq_int, x=Cons(1, Nil), y=Cons(2, Nil))
expect List.ord(A=Int, ord_A=ord_int, x=Cons(1, Nil), y=Cons(2, Nil)) == -1
expect List.show(A=Int, show_A=show_int, x=Cons(1, Nil)) == "Cons(1, Nil)"
expect List.map(A=Int, A_to=Option[Int], f=Some, x=Cons(1, Cons(2, Nil))) == Cons(Some(1), Cons(Some(2), Nil))

NonEmpty (constructor, family)

family NonEmpty[A : Type] : Type deriving (Eq, Ord, Hash, Show, Fold, Map, Traverse)

constructor NonEmpty : A * List[A] -> NonEmpty[A]

A list with at least one element: its first element and the rest.

Example: the derived classes.

example
expect NonEmpty.eq(A=Int, eq_A=eq_int, x=NonEmpty(1, Nil), y=NonEmpty(1, Nil))
expect NonEmpty.ord(A=Int, ord_A=ord_int, x=NonEmpty(2, Nil), y=NonEmpty(1, Cons(5, Nil))) == 1
expect NonEmpty.show(A=Int, show_A=show_int, x=NonEmpty(1, Nil)) == "NonEmpty(1, Nil)"

Option (family)

family Option[A : Type] : Type deriving (Eq, Ord, Hash, Show, Fold, Map, Traverse)

Constructors:

  • None: constructor None : Option[A]
  • Some: constructor Some : A -> Option[A]

An optional value: None, or Some value.

Example: the derived classes.

example
expect Option.eq(A=Int, eq_A=eq_int, x=Some(1), y=Some(1))
expect Option.ord(A=Int, ord_A=ord_int, x=None, y=Some(1)) == -1
expect Option.show(A=Int, show_A=show_int, x=Some(3)) == "Some(3)"
expect Option.map(A=Int, A_to=Option[Int], f=Some, x=Some(3)) == Some(Some(3))

Result (family)

family Result[E : Type, A : Type] : Type deriving (Eq, Ord, Hash, Show, Fold, Map, Traverse)

Constructors:

  • Err: constructor Err : E -> Result[E, A]
  • Ok: constructor Ok : A -> Result[E, A]

A value, or an error that says why there is none.

Example: the derived classes.

example
expect Result.eq(E=Bool, A=Int, eq_E=eq_bool, eq_A=eq_int, x=Ok(1), y=Ok(1))
expect not Result.eq(E=Bool, A=Int, eq_E=eq_bool, eq_A=eq_int, x=Ok(1), y=Err(true))
expect Result.show(E=Bool, A=Int, show_E=show_bool, show_A=show_int, x=Err(false)) == "Err(false)"

Set (family)

family Set[K : Type] : Type deriving (Eq, Ord, Hash, Show, Fold)

Constructors:

  • SetCons: constructor SetCons : K * Set[K] -> Set[K]
  • SetNil: constructor SetNil : Set[K]

A finite set: its elements in strictly increasing order.

Example: the derived classes.

example
expect Set.eq(K=Int, eq_K=eq_int, x=SetCons(1, SetNil), y=SetCons(1, SetNil))
expect Set.ord(K=Int, ord_K=ord_int, x=SetCons(1, SetNil), y=SetCons(2, SetNil)) == -1
expect Set.show(K=Int, show_K=show_int, x=SetCons(1, SetNil)) == "SetCons(1, SetNil)"

add_int (computation)

define add_int(a : Int, b : Int) : Int !{}

The sum of two integers, as a function value.

Example: adding.

example
expect add_int(2, 3) == 5

add_real (computation)

define add_real(a : Real, b : Real) : Real !{}

The sum of two reals, as a function value.

Example: adding reals.

example
expect add_real(0.5, 0.25) == 3/4 exactly

all (computation)

define all[A : Type](keep : A -> Bool !{| rho}, xs : List[A]) : Bool !{| rho}

Whether keep holds of every element; true of the empty list.

Example: every value is present.

example
expect all(A=Option[Int], keep=is_some, xs=Cons(Some(1), Cons(Some(2), Nil)))
expect not all(A=Option[Int], keep=is_some, xs=Cons(Some(1), Cons(None, Nil)))
expect all(A=Option[Int], keep=is_some, xs=Nil)

and_bool (computation)

define and_bool(a : Bool, b : Bool) : Bool !{}

The conjunction of two truth values, as a function value.

Example: both.

example
expect and_bool(true, true)
expect not and_bool(true, false)

any (computation)

define any[A : Type](keep : A -> Bool !{| rho}, xs : List[A]) : Bool !{| rho}

Whether keep holds of some element; false of the empty list.

Example: some value is present.

example
expect any(A=Option[Int], keep=is_some, xs=Cons(None, Cons(Some(1), Nil)))
expect not any(A=Option[Int], keep=is_some, xs=Cons(None, Nil))
expect not any(A=Option[Int], keep=is_some, xs=Nil)

append (computation)

define append[A : Type](xs : List[A], ys : List[A]) : List[A] !{}

The elements of xs followed by those of ys.

Example: appending.

example
expect append(A=Int, xs=Cons(1, Nil), ys=Cons(2, Cons(3, Nil))) == Cons(1, Cons(2, Cons(3, Nil)))

append_string (computation)

define append_string(a : String, b : String) : String !{}

The concatenation of two strings, as a function value.

Example: joining.

example
expect append_string("dog", "s") == "dogs"

concat (computation)

define concat[A : Type](xss : List[List[A]]) : List[A] !{}

The lists of xss joined in order.

Example: joining lists.

example
expect concat(A=Int, xss=Cons(Cons(1, Nil), Cons(Nil, Cons(Cons(2, Cons(3, Nil)), Nil)))) == Cons(1, Cons(2, Cons(3, Nil)))

drop (computation)

define drop[A : Type](n : Int, xs : List[A]) : List[A] !{}

The elements after the first n.

Example: dropping two.

example
expect drop(A=Int, n=2, xs=Cons(1, Cons(2, Cons(3, Nil)))) == Cons(3, Nil)

eq_bool (computation)

define eq_bool(a : Bool, b : Bool) : Bool !{}

Equality of truth values.

Example: equal truth values.

example
expect eq_bool(true, true)
expect not eq_bool(true, false)

eq_int (computation)

define eq_int(a : Int, b : Int) : Bool !{}

Equality of integers.

Example: equal integers.

example
expect eq_int(3, 3)
expect not eq_int(3, 4)

eq_real (computation)

define eq_real(a : Real, b : Real) : Bool !{}

Equality of reals.

Example: equal reals.

example
expect eq_real(0.5, 1/2)

eq_string (computation)

define eq_string(a : String, b : String) : Bool !{}

Equality of strings.

Example: equal strings.

example
expect eq_string("dog", "dog")
expect not eq_string("dog", "cat")

filter (computation)

define filter[A : Type](keep : A -> Bool !{| rho}, xs : List[A]) : List[A] !{| rho}

The elements keep holds of, in order.

Example: keeping the values that are present.

example
expect filter(A=Option[Int], keep=is_some, xs=Cons(Some(1), Cons(None, Cons(Some(3), Nil)))) == Cons(Some(1), Cons(Some(3), Nil))

filter_map (computation)

define filter_map[A : Type, B : Type](f : A -> Option[B] !{| rho}, xs : List[A]) : List[B] !{| rho}

The values f returns for the elements it maps to Some, in order.

Example: the heads of the non-empty lists.

example
expect filter_map(A=List[Int], B=Int, f=head, xs=Cons(Cons(1, Nil), Cons(Nil, Cons(Cons(2, Cons(3, Nil)), Nil)))) == Cons(1, Cons(2, Nil))

find (computation)

define find[A : Type](keep : A -> Bool !{| rho}, xs : List[A]) : Option[A] !{| rho}

The first element keep holds of, if there is one.

Example: the first value that is present.

example
expect find(A=Option[Int], keep=is_some, xs=Cons(None, Cons(Some(2), Cons(Some(3), Nil)))) == Some(Some(2))
expect find(A=Option[Int], keep=is_some, xs=Cons(None, Nil)) == None

fold_left (computation)

define fold_left[A : Type, B : Type](f : B * A -> B !{| rho}, init : B, xs : List[A]) : B !{| rho}

The fold from the left: f(f(f(init, x0), x1), x2) for the elements x0, x1, x2. The derived List.fold folds from the right.

Example: subtracting from the left.

example
expect fold_left(A=Int, B=Int, f=sub_int, init=10, xs=range(1, 4)) == 4

hash_bool (computation)

define hash_bool(a : Bool) : Int !{}

The hash of a truth value: 1 for true, 0 for false.

Example: hashing truth values.

example
expect hash_bool(true) == 1
expect hash_bool(false) == 0

hash_int (computation)

define hash_int(a : Int) : Int !{}

The hash of an integer: the integer itself, as derived instances hash an Int field.

Example: an integer hashes to itself.

example
expect hash_int(42) == 42

hash_string (computation)

define hash_string(a : String) : Int !{}

The hash of a string, the same on every target.

Example: equal strings hash alike.

example
expect hash_string("dog") == hash_string("dog")

head (computation)

define head[A : Type](xs : List[A]) : Option[A] !{}

The first element, if there is one.

Example: the first element.

example
expect head(A=Int, xs=Cons(7, Cons(8, Nil))) == Some(7)
expect head(A=Int, xs=Nil) == None

is_ok (computation)

define is_ok[E : Type, A : Type](r : Result[E, A]) : Bool !{}

Whether the result holds a value.

Example: a value or an error.

example
expect is_ok(E=String, A=Int, r=Ok(1))
expect not is_ok(E=String, A=Int, r=Err("none"))

is_some (computation)

define is_some[A : Type](o : Option[A]) : Bool !{}

Whether the option holds a value.

Example: a value is present.

example
expect is_some(A=Int, o=Some(1))
expect not is_some(A=Int, o=None)

last (computation)

define last[A : Type](xs : List[A]) : Option[A] !{}

The last element, if there is one.

Example: the last element.

example
expect last(A=Int, xs=Cons(7, Cons(8, Nil))) == Some(8)

length (computation)

define length[A : Type](xs : List[A]) : Int !{}

The number of elements.

Example: three elements.

example
expect length(A=Int, xs=Cons(4, Cons(5, Cons(6, Nil)))) == 3
expect length(A=Int, xs=Nil) == 0

lookup (computation)

define lookup[K : Type, V : Type](eq_K : K * K -> Bool !{}, key : K, entries : List[K * V]) : Option[V] !{}

The value paired with the first occurrence of key in an association list, if there is one.

Example: looking up a key.

example
expect lookup(K=String, V=Int, eq_K=eq_string, key="b", entries=Cons(("a", 1), Cons(("b", 2), Nil))) == Some(2)
expect lookup(K=String, V=Int, eq_K=eq_string, key="c", entries=Cons(("a", 1), Nil)) == None

map_delete (computation)

define map_delete[K : Type, V : Type](ord_K : K * K -> Int !{}, key : K, m : FiniteMap[K, V]) : FiniteMap[K, V] !{}

The map without key.

Example: deleting.

example
expect map_delete(K=Int, V=String, ord_K=ord_int, key=1, m=MapCons(1, "a", MapCons(3, "c", MapNil))) == MapCons(3, "c", MapNil)

map_filter (computation)

define map_filter[K : Type, V : Type](keep : K * V -> Bool !{| rho}, m : FiniteMap[K, V]) : FiniteMap[K, V] !{| rho}

The entries keep holds of.

Example: keeping the keys bound to themselves.

example
expect map_filter(K=Int, V=Int, keep=eq_int, m=MapCons(1, 1, MapCons(2, 5, MapCons(3, 3, MapNil)))) == MapCons(1, 1, MapCons(3, 3, MapNil))

map_from_list (computation)

define map_from_list[K : Type, V : Type](ord_K : K * K -> Int !{}, entries : List[K * V]) : FiniteMap[K, V] !{}

The map of an association list; a later entry of a key replaces an earlier one.

Example: building a map.

example
expect map_from_list(K=Int, V=String, ord_K=ord_int, entries=Cons((3, "c"), Cons((1, "a"), Cons((3, "d"), Nil)))) == MapCons(1, "a", MapCons(3, "d", MapNil))

map_get_or (computation)

define map_get_or[K : Type, V : Type](ord_K : K * K -> Int !{}, key : K, default : V, m : FiniteMap[K, V]) : V !{}

The value bound to key, or default when it is unbound.

Example: a default.

example
expect map_get_or(K=Int, V=String, ord_K=ord_int, key=2, default="-", m=MapNil) == "-"

map_insert (computation)

define map_insert[K : Type, V : Type](ord_K : K * K -> Int !{}, key : K, value : V, m : FiniteMap[K, V]) : FiniteMap[K, V] !{}

The map with key bound to value, replacing the key's earlier value.

Example: inserting in order.

example
expect map_insert(K=Int, V=String, ord_K=ord_int, key=2, value="b", m=MapCons(1, "a", MapCons(3, "c", MapNil))) == MapCons(1, "a", MapCons(2, "b", MapCons(3, "c", MapNil)))
expect map_insert(K=Int, V=String, ord_K=ord_int, key=1, value="z", m=MapCons(1, "a", MapNil)) == MapCons(1, "z", MapNil)

map_insert_with (computation)

define map_insert_with[K : Type, V : Type](ord_K : K * K -> Int !{}, combine : V * V -> V !{}, key : K, value : V, m : FiniteMap[K, V]) : FiniteMap[K, V] !{}

The map with key bound to value, or to combine(value, old) when the key is bound to old.

Example: counting.

example
expect map_insert_with(K=String, V=Int, ord_K=ord_string, combine=add_int, key="a", value=1, m=MapCons("a", 2, MapNil)) == MapCons("a", 3, MapNil)
expect map_insert_with(K=String, V=Int, ord_K=ord_string, combine=add_int, key="b", value=1, m=MapCons("a", 2, MapNil)) == MapCons("a", 2, MapCons("b", 1, MapNil))

map_keys (computation)

define map_keys[K : Type, V : Type](m : FiniteMap[K, V]) : List[K] !{}

The keys, in increasing order.

Example: the keys.

example
expect map_keys(K=Int, V=String, m=MapCons(1, "a", MapCons(3, "c", MapNil))) == Cons(1, Cons(3, Nil))

map_lookup (computation)

define map_lookup[K : Type, V : Type](ord_K : K * K -> Int !{}, key : K, m : FiniteMap[K, V]) : Option[V] !{}

The value bound to key, if there is one.

Example: looking up.

example
expect map_lookup(K=Int, V=String, ord_K=ord_int, key=3, m=MapCons(1, "a", MapCons(3, "c", MapNil))) == Some("c")
expect map_lookup(K=Int, V=String, ord_K=ord_int, key=2, m=MapCons(1, "a", MapCons(3, "c", MapNil))) == None

map_member (computation)

define map_member[K : Type, V : Type](ord_K : K * K -> Int !{}, key : K, m : FiniteMap[K, V]) : Bool !{}

Whether key is bound.

Example: a bound key.

example
expect map_member(K=Int, V=String, ord_K=ord_int, key=1, m=MapCons(1, "a", MapNil))

map_singleton (computation)

define map_singleton[K : Type, V : Type](key : K, value : V) : FiniteMap[K, V] !{}

The map with one entry.

Example: one entry.

example
expect map_singleton(K=String, V=Int, key="a", value=1) == MapCons("a", 1, MapNil)

map_size (computation)

define map_size[K : Type, V : Type](m : FiniteMap[K, V]) : Int !{}

The number of entries.

Example: two entries.

example
expect map_size(K=Int, V=String, m=MapCons(1, "a", MapCons(3, "c", MapNil))) == 2

map_to_list (computation)

define map_to_list[K : Type, V : Type](m : FiniteMap[K, V]) : List[K * V] !{}

The entries as key-value pairs, in increasing order of their keys.

Example: the entries.

example
expect map_to_list(K=Int, V=String, m=MapCons(1, "a", MapCons(3, "c", MapNil))) == Cons((1, "a"), Cons((3, "c"), Nil))

map_union (computation)

define map_union[K : Type, V : Type](ord_K : K * K -> Int !{}, left : FiniteMap[K, V], right : FiniteMap[K, V]) : FiniteMap[K, V] !{}

The entries of both maps; where both bind a key, the left map's value.

Example: a left-biased union.

example
expect map_union(K=Int, V=String, ord_K=ord_int, left=MapCons(1, "a", MapNil), right=MapCons(1, "b", MapCons(2, "c", MapNil))) == MapCons(1, "a", MapCons(2, "c", MapNil))

map_values (computation)

define map_values[K : Type, V : Type](m : FiniteMap[K, V]) : List[V] !{}

The values, in increasing order of their keys.

Example: the values.

example
expect map_values(K=Int, V=String, m=MapCons(1, "a", MapCons(3, "c", MapNil))) == Cons("a", Cons("c", Nil))

maximum (computation)

define maximum[A : Type](ord_A : A * A -> Int !{}, xs : List[A]) : Option[A] !{}

The greatest element by ord_A, the first of equal greatest ones, if the list is not empty.

Example: the maximum.

example
expect maximum(A=Int, ord_A=ord_int, xs=Cons(3, Cons(9, Cons(2, Nil)))) == Some(9)
expect maximum(A=Int, ord_A=ord_int, xs=Nil) == None

member (computation)

define member[A : Type](eq_A : A * A -> Bool !{}, x : A, xs : List[A]) : Bool !{}

Whether x is an element, by the equality eq_A.

Example: membership.

example
expect member(A=Int, eq_A=eq_int, x=2, xs=range(0, 3))
expect not member(A=Int, eq_A=eq_int, x=5, xs=range(0, 3))

mul_int (computation)

define mul_int(a : Int, b : Int) : Int !{}

The product of two integers, as a function value.

Example: multiplying.

example
expect mul_int(4, 3) == 12

mul_real (computation)

define mul_real(a : Real, b : Real) : Real !{}

The product of two reals, as a function value.

Example: multiplying reals.

example
expect mul_real(0.5, 0.25) == 1/8 exactly

nonempty_from_list (computation)

define nonempty_from_list[A : Type](xs : List[A]) : Option[NonEmpty[A]] !{}

The list as a non-empty one, if it is not empty.

Example: a non-empty list.

example
expect nonempty_from_list(A=Int, xs=Cons(1, Nil)) == Some(NonEmpty(1, Nil))
expect nonempty_from_list(A=Int, xs=Nil) == None

nonempty_head (computation)

define nonempty_head[A : Type](xs : NonEmpty[A]) : A !{}

The first element, which always exists.

Example: the first element.

example
expect nonempty_head(A=Int, xs=NonEmpty(5, Cons(6, Nil))) == 5

nonempty_maximum (computation)

define nonempty_maximum[A : Type](ord_A : A * A -> Int !{}, xs : NonEmpty[A]) : A !{}

The greatest element by ord_A, the first of equal greatest ones.

Example: the maximum.

example
expect nonempty_maximum(A=String, ord_A=ord_string, xs=NonEmpty("b", Cons("c", Cons("a", Nil)))) == "c"

nonempty_reduce (computation)

define nonempty_reduce[A : Type](f : A * A -> A !{| rho}, xs : NonEmpty[A]) : A !{| rho}

The elements combined from the left by f, which needs no starting value: f(f(x0, x1), x2).

Example: summing from the left.

example
expect nonempty_reduce(A=Int, f=add_int, xs=NonEmpty(1, Cons(2, Cons(3, Nil)))) == 6

nonempty_to_list (computation)

define nonempty_to_list[A : Type](xs : NonEmpty[A]) : List[A] !{}

The elements as a list.

Example: two elements.

example
expect nonempty_to_list(A=Int, xs=NonEmpty(1, Cons(2, Nil))) == Cons(1, Cons(2, Nil))

not_bool (computation)

define not_bool(a : Bool) : Bool !{}

The negation of a truth value, as a function value.

Example: negating.

example
expect not_bool(false)

nth (computation)

define nth[A : Type](xs : List[A], index : Int) : Option[A] !{}

The element at position index, counting from zero, if there is one.

Example: indexing.

example
expect nth(A=String, xs=Cons("a", Cons("b", Nil)), index=1) == Some("b")
expect nth(A=String, xs=Cons("a", Cons("b", Nil)), index=2) == None

option_bind (computation)

define option_bind[A : Type, B : Type](o : Option[A], f : A -> Option[B] !{| rho}) : Option[B] !{| rho}

f applied to the option's value, or None when it holds none.

Example: the head of an optional list.

example
expect option_bind(A=List[Int], B=Int, o=Some(Cons(4, Nil)), f=head) == Some(4)
expect option_bind(A=List[Int], B=Int, o=Some(Nil), f=head) == None
expect option_bind(A=List[Int], B=Int, o=None, f=head) == None

option_get_or (computation)

define option_get_or[A : Type](o : Option[A], default : A) : A !{}

The option's value, or default when it holds none.

Example: a default.

example
expect option_get_or(A=Int, o=None, default=0) == 0
expect option_get_or(A=Int, o=Some(4), default=0) == 4

option_or (computation)

define option_or[A : Type](first : Option[A], second : Option[A]) : Option[A] !{}

The first option that holds a value.

Example: an alternative.

example
expect option_or(A=Int, first=None, second=Some(2)) == Some(2)
expect option_or(A=Int, first=Some(1), second=Some(2)) == Some(1)

option_to_list (computation)

define option_to_list[A : Type](o : Option[A]) : List[A] !{}

The option's value as a list of zero or one elements.

Example: one element.

example
expect option_to_list(A=Int, o=Some(3)) == Cons(3, Nil)

option_to_result (computation)

define option_to_result[E : Type, A : Type](error : E, o : Option[A]) : Result[E, A] !{}

The option as a result, with error when it holds no value.

Example: naming what is missing.

example
expect option_to_result(E=String, A=Int, error="missing", o=None) == Err("missing")
expect option_to_result(E=String, A=Int, error="missing", o=Some(1)) == Ok(1)

or_bool (computation)

define or_bool(a : Bool, b : Bool) : Bool !{}

The disjunction of two truth values, as a function value.

Example: either.

example
expect or_bool(false, true)
expect not or_bool(false, false)

ord_bool (computation)

define ord_bool(a : Bool, b : Bool) : Int !{}

The order of truth values, false before true.

Example: false comes first.

example
expect ord_bool(false, true) == -1
expect ord_bool(true, true) == 0

ord_int (computation)

define ord_int(a : Int, b : Int) : Int !{}

The order of integers: -1, 0, or 1 as a is below, equal to, or above b.

Example: comparing integers.

example
expect ord_int(2, 5) == -1
expect ord_int(5, 5) == 0
expect ord_int(7, 5) == 1

ord_real (computation)

define ord_real(a : Real, b : Real) : Int !{}

The order of reals: -1, 0, or 1.

Example: comparing reals.

example
expect ord_real(0.25, 0.5) == -1

ord_string (computation)

define ord_string(a : String, b : String) : Int !{}

The lexicographic order of strings: -1, 0, or 1.

Example: comparing strings.

example
expect ord_string("cat", "dog") == -1
expect ord_string("dog", "cat") == 1
expect ord_string("dog", "dog") == 0

range (computation)

define range(low : Int, high : Int) : List[Int] !{}

The integers from low up to, but not including, high.

Example: a half-open range.

example
expect range(2, 5) == Cons(2, Cons(3, Cons(4, Nil)))
expect range(5, 2) == Nil

replicate (computation)

define replicate[A : Type](n : Int, x : A) : List[A] !{}

A list of n copies of x; empty when n is not positive.

Example: three copies.

example
expect replicate(A=String, n=3, x="a") == Cons("a", Cons("a", Cons("a", Nil)))
expect replicate(A=String, n=0, x="a") == Nil

result_bind (computation)

define result_bind[E : Type, A : Type, B : Type](r : Result[E, A], f : A -> Result[E, B] !{| rho}) : Result[E, B] !{| rho}

f applied to the result's value, or its error unchanged.

Example: a step that succeeds and one that fails.

example
expect result_bind(E=Int, A=Int, B=Int, r=Ok(3), f=Ok) == Ok(3)
expect result_bind(E=Int, A=Int, B=Int, r=Ok(3), f=Err) == Err(3)
expect result_bind(E=Int, A=Int, B=Int, r=Err(5), f=Ok) == Err(5)

result_map_err (computation)

define result_map_err[E : Type, F : Type, A : Type](f : E -> F !{| rho}, r : Result[E, A]) : Result[F, A] !{| rho}

The result with f applied to its error; a value is unchanged.

Example: explaining an error.

example
expect result_map_err(E=Int, F=String, A=Int, f=show_int, r=Err(3)) == Err("3")
expect result_map_err(E=Int, F=String, A=Int, f=show_int, r=Ok(3)) == Ok(3)

result_to_option (computation)

define result_to_option[E : Type, A : Type](r : Result[E, A]) : Option[A] !{}

The result's value as an option, forgetting the error.

Example: forgetting the error.

example
expect result_to_option(E=String, A=Int, r=Err("none")) == None
expect result_to_option(E=String, A=Int, r=Ok(2)) == Some(2)

reverse (computation)

define reverse[A : Type](xs : List[A]) : List[A] !{}

The elements in the opposite order.

Example: reversing.

example
expect reverse(A=Int, xs=Cons(1, Cons(2, Cons(3, Nil)))) == Cons(3, Cons(2, Cons(1, Nil)))

set_complement (computation)

define set_complement[K : Type](ord_K : K * K -> Int !{}, universe : List[K], s : Set[K]) : Set[K] !{}

The elements of universe that are not in s: the complement of a set of a key type whose every value universe lists.

Example: the complement among three keys.

example
expect set_complement(K=Int, ord_K=ord_int, universe=range(0, 3), s=SetCons(1, SetNil)) == SetCons(0, SetCons(2, SetNil))

set_delete (computation)

define set_delete[K : Type](ord_K : K * K -> Int !{}, x : K, s : Set[K]) : Set[K] !{}

The set without x.

Example: removing.

example
expect set_delete(K=Int, ord_K=ord_int, x=1, s=SetCons(1, SetCons(2, SetNil))) == SetCons(2, SetNil)

set_difference (computation)

define set_difference[K : Type](ord_K : K * K -> Int !{}, left : Set[K], right : Set[K]) : Set[K] !{}

The elements of left that are not in right.

Example: a difference.

example
expect set_difference(K=Int, ord_K=ord_int, left=SetCons(1, SetCons(3, SetNil)), right=SetCons(3, SetNil)) == SetCons(1, SetNil)

set_from_list (computation)

define set_from_list[K : Type](ord_K : K * K -> Int !{}, xs : List[K]) : Set[K] !{}

The set of a list's elements.

Example: building a set.

example
expect set_from_list(K=Int, ord_K=ord_int, xs=Cons(3, Cons(1, Cons(3, Nil)))) == SetCons(1, SetCons(3, SetNil))

set_insert (computation)

define set_insert[K : Type](ord_K : K * K -> Int !{}, x : K, s : Set[K]) : Set[K] !{}

The set with x added.

Example: adding in order.

example
expect set_insert(K=Int, ord_K=ord_int, x=2, s=SetCons(1, SetCons(3, SetNil))) == SetCons(1, SetCons(2, SetCons(3, SetNil)))
expect set_insert(K=Int, ord_K=ord_int, x=3, s=SetCons(3, SetNil)) == SetCons(3, SetNil)

set_intersection (computation)

define set_intersection[K : Type](ord_K : K * K -> Int !{}, left : Set[K], right : Set[K]) : Set[K] !{}

The elements of both sets.

Example: an intersection.

example
expect set_intersection(K=Int, ord_K=ord_int, left=SetCons(1, SetCons(3, SetNil)), right=SetCons(2, SetCons(3, SetNil))) == SetCons(3, SetNil)

set_member (computation)

define set_member[K : Type](ord_K : K * K -> Int !{}, x : K, s : Set[K]) : Bool !{}

Whether x is an element.

Example: membership.

example
expect set_member(K=String, ord_K=ord_string, x="b", s=SetCons("a", SetCons("b", SetNil)))
expect not set_member(K=String, ord_K=ord_string, x="c", s=SetCons("a", SetCons("b", SetNil)))

set_singleton (computation)

define set_singleton[K : Type](x : K) : Set[K] !{}

The set of one element.

Example: one element.

example
expect set_singleton(K=Int, x=4) == SetCons(4, SetNil)

set_size (computation)

define set_size[K : Type](s : Set[K]) : Int !{}

The number of elements.

Example: two elements.

example
expect set_size(K=Int, s=SetCons(1, SetCons(2, SetNil))) == 2

set_subset (computation)

define set_subset[K : Type](ord_K : K * K -> Int !{}, left : Set[K], right : Set[K]) : Bool !{}

Whether every element of left is an element of right.

Example: a subset.

example
expect set_subset(K=Int, ord_K=ord_int, left=SetCons(3, SetNil), right=SetCons(1, SetCons(3, SetNil)))
expect not set_subset(K=Int, ord_K=ord_int, left=SetCons(2, SetNil), right=SetCons(1, SetCons(3, SetNil)))

set_to_list (computation)

define set_to_list[K : Type](s : Set[K]) : List[K] !{}

The elements, in increasing order.

Example: the elements.

example
expect set_to_list(K=Int, s=SetCons(1, SetCons(2, SetNil))) == Cons(1, Cons(2, Nil))

set_union (computation)

define set_union[K : Type](ord_K : K * K -> Int !{}, left : Set[K], right : Set[K]) : Set[K] !{}

The elements of either set.

Example: a union.

example
expect set_union(K=Int, ord_K=ord_int, left=SetCons(1, SetCons(3, SetNil)), right=SetCons(2, SetCons(3, SetNil))) == SetCons(1, SetCons(2, SetCons(3, SetNil)))

show_bool (computation)

define show_bool(a : Bool) : String !{}

A truth value as true or false.

Example: printing a truth value.

example
expect show_bool(false) == "false"

show_int (computation)

define show_int(a : Int) : String !{}

An integer in decimal.

Example: printing an integer.

example
expect show_int(-12) == "-12"

show_string (computation)

define show_string(a : String) : String !{}

A string between single quotes, as derived instances print a String field.

Example: printing a string.

example
expect show_string("dog") == "'dog'"

sort (computation)

define sort[A : Type](ord_A : A * A -> Int !{}, xs : List[A]) : List[A] !{}

The elements in increasing order by ord_A, keeping the order of equal elements (a merge sort).

Example: sorting.

example
expect sort(A=Int, ord_A=ord_int, xs=Cons(3, Cons(1, Cons(2, Cons(1, Nil))))) == Cons(1, Cons(1, Cons(2, Cons(3, Nil))))

sub_int (computation)

define sub_int(a : Int, b : Int) : Int !{}

The difference of two integers, as a function value.

Example: subtracting.

example
expect sub_int(2, 3) == -1

take (computation)

define take[A : Type](n : Int, xs : List[A]) : List[A] !{}

The first n elements, or all of them when there are fewer.

Example: taking two.

example
expect take(A=Int, n=2, xs=Cons(1, Cons(2, Cons(3, Nil)))) == Cons(1, Cons(2, Nil))

zip (computation)

define zip[A : Type, B : Type](xs : List[A], ys : List[B]) : List[A * B] !{}

Pairs of the elements at each position, as long as the shorter list.

Example: zipping.

example
expect zip(A=Int, B=String, xs=Cons(1, Cons(2, Nil)), ys=Cons("a", Nil)) == Cons((1, "a"), Nil)