Algebra — Algebraic Types

Part of the std module. Available via import std.

Algebraic data types: Either, Maybe, Tuple, and These, with for instances and fold functions.

Either

union Either l r
  left: l
  right: r

A value that is one of two possibilities. By convention, left represents failure or the "other" case, and right represents success or the primary case.

either

either: l:(l. x). r:(r. x). e:Either l r. x

Fold over an Either by providing handlers for both cases.

forEither

forEither: l:(a. b). r:(c. d). e:Either a c. Either b d

Map over both sides of an Either.

for (Either l)

for (Either l)
  left: x. f. Either.left x
  right: x. f. Either.right (f x)

The for instance maps over the right side only — left values pass through unchanged.

Maybe

union Maybe a
  just: a
  nothing

An optional value. Included for completeness — Paradox has built-in optionals (T?) which are usually preferred.

maybe

maybe: j:(a. x). x:x. m:Maybe a. x

Fold over a Maybe: apply j to the value if just, return x if nothing.

for Maybe

for Maybe
  just: x. f. Maybe.just (f x)
  nothing: f. Maybe.nothing

Maps the function over just values; nothing passes through.

Tuple

type Tuple a b
  first: a
  second: b

A pair of values.

tuple

tuple: f:(a. b. c). t:Tuple a b. c

Apply a binary function to the elements of a tuple.

forTuple

forTuple: t:Tuple a c. f:(a. b). g:(c. d). Tuple b d

Map over both elements of a tuple.

for (Tuple a)

for (Tuple a)
  f. Tuple:
    first: first
    second: f second

Maps the function over the second element only.

These

union These a b
  this: a
  that: b
  these: Tuple a b

A value that is this, that, or both. Generalizes Either by including the "both" case.

these

these: f:(a. x). g:(b. x). j:(a. b. x). t:These a b. x

Fold over a These with three handlers.

forThese

forThese: t:These a c. f:(a. b). g:(c. d). These b d

Map over both sides of a These.

for (These a)

for (These a)
  this: x. f. These.this x
  that: x. f. These.that (f x)
  these: tt. f. These.these (for tt (f))

Maps the function over the that and these sides; this values pass through.