Pervasives

GitHub   Edit on GitHub

This module is automatically imported into every Grain program. You can think of it as the global environment. Although it is automatically imported, it can still be imported manually.

1
import Pervasives from 'pervasives'

Types

Type declarations included in the Pervasives module.

Pervasives.Number

1
data Number

The type of Grain numbers, i.e. 42, 0x2a.

Pervasives.Bool

1
data Bool = true | false

The type of Grain booleans, i.e. the type of true and false.

Pervasives.String

1
data String

The type of Grain strings, i.e. 'The quick brown fox jumps over the lazy dog.'.

Pervasives.Void

1
data Void = void

The type of void.

Pervasives.Box

1
data Box<a>

The type of Grain boxes.

Pervasives.List

1
data List<a> = [] | [...](a, List<a>)

The type of Grain lists, i.e. [1, 2, 3], [].

Pervasives.Array

1
data Array<a>

The type of Grain arrays, i.e. [> 1, 2, 3].

Pervasives.Option

1
data Option<a> = Some(a) | None

Grain’s type representing something that may or may not contain data. Think of this like a better, type-safe “null”.

Pervasives.Result

1
data Result<t, e> = Ok(t) | Err(e)

Grain’s type representing the result of something that might error.


Comparison Operations

Operations to compare values.

Pervasives.(==)

Check that two values are equal. This checks for structural equality, so it also works for comparing things like tuples and lists.

Pervasives.(!=)

Check that two values are not equal. This checks for structural equality, so it also works for comparing things like tuples and lists.

Pervasives.(is)

Checks that two values are physically equal. Use this operator if you don’t need or want structural equality.


Math Operations

Operations on numbers.

Pervasives.incr

1
incr : Number -> Number

Increments a number by 1.

Pervasives.decr

1
decr : Number -> Number

Decrements a number by 1.

Pervasives.(+)

1
(+) : (Number, Number) -> Number

Computes the sum of two numbers.

Pervasives.(-)

1
(-) : (Number, Number) -> Number

Computes the difference of two numbers.

Pervasives.(*)

1
(*) : (Number, Number) -> Number

Computes the product of two numbers.

Pervasives.(/)

1
(/) : (Number, Number) -> Number

Computes the quotient of two numbers.

Pervasives.(%)

1
(%) : (Number, Number) -> Number

Computes the modulo of the first argument by the second argument.


Number Comparisons

Comparisons on numbers.

Pervasives.(<)

1
(<) : (Number, Number) -> Bool

Checks if the first argument is less than the second argument.

Pervasives.(>)

1
(>) : (Number, Number) -> Bool

Checks if the first argument is greater than the second argument.

Pervasives.(<=)

1
(<=) : (Number, Number) -> Bool

Checks if the first argument is less than or equal to the second argument.

Pervasives.(>=)

1
(>=) : (Number, Number) -> Bool

Checks if the first argument is greater than or equal to the second argument.


Boolean Operations

Operations on booleans.

Pervasives.(!)

1
(!) : Bool -> Bool

Computes the boolean “not” of the argument.

1
2
!true # false
!false # true

Pervasives.(&&)

1
(&&) : (Bool, Bool) -> Bool

Computes the boolean “and” of the two arguments.

Pervasives.(||)

1
(||) : (Bool, Bool) -> Bool

Computes the boolean “or” of the two arguments.


Box Operations

Operations on boxes.

Pervasives.box

1
box : a -> Box<a>

Creates a box with the same type as the argument. The argument is used as the initial value.

Pervasives.unbox

1
unbox : Box<a> -> a

Retrieves the current value from the box.

Pervasives.(^)

1
(^) : Box<a> -> a

An alias for unbox.

1
2
let counter = box(0)
^counter # 0

Pervasives.(+=)

1
(+=) : (Box<Number>, Number) -> Number

Computes the sum of the value in the box and the second argument and stores the result in the box.

Pervasives.(-=)

1
(-=) : (Box<Number>, Number) -> Number

Computes the difference of the value in the box and the second argument and stores the result in the box.

Pervasives.(*=)

1
(*=) : (Box<Number>, Number) -> Number

Computes the product of the value in the box and the second argument and stores the result in the box.

Pervasives.(/=)

1
(/=) : (Box<Number>, Number) -> Number

Computes the quotient of the value in the box and the second argument and stores the result in the box.


Type Helpers

Functions that help with typechecking.

Pervasives.ignore

1
ignore : a -> Void

Accepts any argument and returns void.


Assertions

Functions that raise if conditions are not met.

Pervasives.assert

1
assert : Bool -> Void

Assert that the given condition (boolean) is true. Raises AssertionError if the condition is false.

1
2
assert 3 > 2
assert true

Failure

Functions that raise unconditionally.

Pervasives.fail

1
fail : String -> a

Unconditionally raise Failure with a message.

1
2
3
4
5
if (true) {
print('success')
} else {
fail 'condition may not be false'
}

Printing

Functions that deal with printing.

Pervasives.print

1
print : a -> Void

Prints the value of the argument to the console. Works for any type. Internally, print calls toString on the argument.

Pervasives.toString

1
toString : a -> String

Returns a string representation of the argument. Provides a better representation of data types if those types are exported from the module.


Other Values

Miscellaneous operations.

Pervasives.identity

1
identity : a -> a

Returns the argument untouched.

This is a notification!