std.prelude

Definitions which gets implicit re-export in every file.

Types

type Ordering = 
    | LT
    | EQ
    | GT

Ordering represents the result of comparing two values

type Semigroup a = { append : a -> a -> a }

Semigroup a represents an associative operation on a. This means the following laws must hold:

  • forall x . append x (append y z) == append (append x y) z

type Monoid a = { semigroup : std.semigroup.Semigroup a, empty : a }

Monoid a represents an semigroup an which has an identity. This means the following additional laws must hold:

  • forall x . append x empty == x
  • forall x . append empty x == x

type Group a = { monoid : std.monoid.Monoid a, inverse : a -> a }

Group a represents an monoid an which has an inverse element. This means the following additional laws must hold:

  • forall x . append (inverse x) x = empty = append x (inverse x)

type Eq a = { (==) : a -> a -> std.types.Bool }

Eq a defines equality (==) on a

type Ord a = { eq : std.cmp.Eq a, compare : a -> a -> std.types.Ordering }

Ord a defines an ordering on a

type Category cat = {
    id : forall a . cat a a,
    compose : forall a b c . cat b c -> cat a b -> cat a c
}

type Functor f = { map : forall a b . (a -> b) -> f a -> f b }

A Functor represents an action on a parameterized type which does not change the structure with the mapped type.

The following laws should hold:

  • map id == id
  • map (f << g) == map f << map g

type Applicative f = {
    functor : std.functor.Functor f,
    apply : forall a b . f (a -> b) -> f a -> f b,
    wrap : forall a . a -> f a
}

A Functor with application.

The following laws should hold:

  • wrap id <*> v = v
  • wrap (<<) <> u <> v <> w = u <> (v <*> w)
  • wrap f <*> wrap x = wrap (f x)
  • u <> wrap y = wrap (\g -> g x) <> u

type Alternative f = {
    applicative : std.applicative.Applicative f,
    or : forall a . f a -> f a -> f a,
    empty : forall a . f a
}

A monoid on applicative functors.

type Monad m = {
    applicative : std.applicative.Applicative m,
    flat_map : forall a b . (a -> m b) -> m a -> m b
}

A generalised interface for imperatively sequencing actions

type Num a = {
    ord : std.cmp.Ord a,
    (+) : a -> a -> a,
    (-) : a -> a -> a,
    (*) : a -> a -> a,
    (/) : a -> a -> a,
    negate : a -> a
}

The basic operation on numbers. Defined for both the primitive type Int and Float

type Show a = { show : a -> String }

Show a represents a conversion function from a to a readable string.

type Option a = 
    | None
    | Some a

Option represents a value which may not exist.

type Bool = 
    | False
    | True

Bool represents a value which can only be True or False

Values

let append ?s : forall a . [std.semigroup.Semigroup a] -> a -> a -> a

Note
  • Known as (<>) or mappend in Haskell

let <> : forall a . [std.semigroup.Semigroup a] -> a -> a -> a

let empty ?m : forall a . [std.monoid.Monoid a] -> a

Note
  • Known as mempty in Haskell

let == ?eq : forall a . [std.cmp.Eq a] -> a -> a -> std.types.Bool

Tests whether the values are equal.

let /= ?eq l r : forall a . [std.cmp.Eq a] -> a -> a -> std.types.Bool

Tests whether the values are not equal.

let < l r : forall a . [std.cmp.Ord a] -> a -> a -> std.types.Bool

Returns whether l is less than r.

let <= l r : forall a . [std.cmp.Ord a] -> a -> a -> std.types.Bool

Returns whether l is less than or equal to r.

let >= l r : forall a . [std.cmp.Ord a] -> a -> a -> std.types.Bool

Returns whether l is greater than or equal to r.

let > l r : forall a . [std.cmp.Ord a] -> a -> a -> std.types.Bool

Returns whether l is greater than r.

let id ?cat : forall a a0 . [std.category.Category a] -> a a0 a0

let compose ?cat : forall a a0 a1 a2 . [std.category.Category a] -> a a0 a1 -> a a2 a0 -> a a2 a1

let + ?num : forall a . [std.num.Num a] -> a -> a -> a

The addition operator

let - ?num : forall a . [std.num.Num a] -> a -> a -> a

The subtraction operator

let * ?num : forall a . [std.num.Num a] -> a -> a -> a

The multiplication operator

let / ?num : forall a . [std.num.Num a] -> a -> a -> a

The division operator

let negate ?num : forall a . [std.num.Num a] -> a -> a

The negation function

let show ?s : forall a . [std.show.Show a] -> a -> String

Converts a value into a string.

let { assert } = import! std.test
let list @ { ? } = import! std.list

assert (show 123 == "123")
assert (show 3.14 == "3.14")
assert (show "abc" == "\"abc\"")
assert (show (list.of [1, 2, 3]) == "[1, 2, 3]")

let ++ : String -> String -> String

Appends two strings.

Re-export of semigroup.append.

let not x : std.types.Bool -> std.types.Bool

Boolean 'not'

let error : forall a . String -> a

let flat_map ?m : forall a a0 a1 . [std.monad.Monad a] -> (a0 -> a a1) -> a a0 -> a a1

This can be seen as akin to sequential variable binding in an imperative language. For example in Javascript:

var x = call_fallible("hello");
do_something(x);

In gluon this would look like:

result.monad.flat_map (\x -> do_something x) (call_fallible "hello")

Note that it is sometimes more ergonomic to use the (>>=) operator:

let { (>>=) } = import! std.prelude

call_fallible "hello" >>= (\x -> do_something x)
Note
  • Known as (=<<) orflip (>>=)` in Haskell
  • Known as Option::and_then and Result::and_then in Rust