Skip to content

std.fn

Functions as values: identity, constants, composition, and the combinators that build one function from others.

A combinator that returns a function returns a pure one: the function it builds captures its arguments and performs nothing. A combinator that applies a function it is given (apply, apply2, iterate) performs whatever that function performs, through an open effect row. Every function value here is eliminated before a program reaches a backend: a function known at its call is specialized into the callee, so each transpile target sees first-order code.

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

import std.fn
open std.fn

apply (computation)

define apply[A : Type, B : Type](f : A -> B !{| rho}, x : A) : B !{| rho}

f applied to x.

Example: applying a function.

example
expect apply(A=Int, B=Int, f=identity, x=4) == 4

apply2 (computation)

define apply2[A : Type, B : Type, C : Type](f : A * B -> C !{| rho}, a : A, b : B) : C !{| rho}

f applied to a and b.

Example: applying a function of two arguments.

example
expect apply2(A=Int, B=String, C=Int, f=first, a=5, b="x") == 5

compose (computation)

define compose[A : Type, B : Type, C : Type](f : B -> C !{}, g : A -> B !{}) : A -> C !{}

The function that applies g and then f.

Example: composing.

example
expect apply(A=Int, B=Int, f=compose(A=Int, B=Int, C=Int, f=identity, g=identity), x=6) == 6

constant (computation)

define constant[A : Type, B : Type](x : A) : B -> A !{}

The function that ignores its argument and returns x.

Example: a constant function.

example
expect apply(A=String, B=Int, f=constant(A=Int, B=String, x=7), x="ignored") == 7

curry (computation)

define curry[A : Type, B : Type, C : Type](f : A * B -> C) : A -> B -> C !{}

The function that takes the first argument of f and returns the function that takes the second.

Example: currying and uncurrying.

example
expect apply2(A=Int, B=String, C=Int, f=uncurry(A=Int, B=String, C=Int, f=curry(A=Int, B=String, C=Int, f=first)), a=5, b="x") == 5

fanout (computation)

define fanout[A : Type, B : Type, C : Type](f : A -> B !{}, g : A -> C !{}) : A -> B * C !{}

The function that applies f and g to one argument and pairs the results.

Example: two views of one value.

example
expect swap(A=Int, B=Int, p=apply(A=Int, B=Int * Int, f=fanout(A=Int, B=Int, C=Int, f=identity, g=identity), x=3)) == (3, 3)

first (computation)

define first[A : Type, B : Type](a : A, b : B) : A !{}

The first component of a pair.

Example: the first component.

example
expect first(A=Int, B=String, a=1, b="x") == 1

flip (computation)

define flip[A : Type, B : Type, C : Type](f : A * B -> C !{}) : B * A -> C !{}

The function of two arguments that applies f to them in the other order.

Example: flipping a projection.

example
expect apply2(A=String, B=Int, C=Int, f=flip(A=Int, B=String, C=Int, f=first), a="x", b=5) == 5

identity (computation)

define identity[A : Type](x : A) : A !{}

The argument itself.

Example: identity.

example
expect identity(A=Int, x=3) == 3

iterate (computation)

define iterate[A : Type](n : Int, f : A -> A !{| rho}, x : A) : A !{| rho}

f applied n times to x; x itself when n is not positive.

Example: applying a function three times.

example
expect iterate(A=Int * Int, n=3, f=swap, x=(1, 2)) == (2, 1)

on (computation)

define on[A : Type, B : Type, C : Type](f : B * B -> C !{}, key : A -> B !{}) : A * A -> C !{}

The function of two arguments that compares them by key: f applied to the keys of both, as in sorting by a field.

Example: comparing by a key.

example
expect apply2(A=Int, B=Int, C=Int, f=on(A=Int, B=Int, C=Int, f=first, key=identity), a=4, b=9) == 4

pair (computation)

define pair[A : Type, B : Type](a : A, b : B) : A * B !{}

A pair of two values.

Example: pairing.

example
expect pair(A=Int, B=String, a=1, b="x") == (1, "x")

second (computation)

define second[A : Type, B : Type](a : A, b : B) : B !{}

The second component of a pair.

Example: the second component.

example
expect second(A=Int, B=String, a=1, b="x") == "x"

swap (computation)

define swap[A : Type, B : Type](p : A * B) : B * A !{}

A pair with its components exchanged.

Example: swapping.

example
expect swap(A=Int, B=String, p=(1, "x")) == ("x", 1)

uncurry (computation)

define uncurry[A : Type, B : Type, C : Type](f : A -> B -> C) : A * B -> C !{}

The function of two arguments that applies f to the first and the function it returns to the second.

Example: uncurrying a curried function.

example
expect apply2(A=Int, B=String, C=Int, f=uncurry(A=Int, B=String, C=Int, f=curry(A=Int, B=String, C=Int, f=first)), a=5, b="x") == 5