Pervasives
Edit on GitHubThis 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.
Types
Type declarations included in the Pervasives module.
Pervasives.Number
The type of Grain numbers, i.e. 42
, 0x2a
.
Pervasives.Bool
The type of Grain booleans, i.e. the type of true
and false
.
Pervasives.String
The type of Grain strings, i.e. "The quick brown fox jumps over the lazy dog."
.
Pervasives.Void
The type of void
.
Pervasives.Box
The type of Grain boxes.
Pervasives.List
The type of Grain lists, i.e. [1, 2, 3]
, []
.
Pervasives.Array
The type of Grain arrays, i.e. [> 1, 2, 3]
.
Pervasives.Option
Grain’s type representing something that may or may not contain data. Think of this like a better, type-safe “null”.
Pervasives.Result
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
Increments a number by 1.
Pervasives.decr
Decrements a number by 1.
Pervasives.(+)
Computes the sum of two numbers.
Pervasives.(-)
Computes the difference of two numbers.
Pervasives.(*)
Computes the product of two numbers.
Pervasives.(/)
Computes the quotient of two numbers.
Pervasives.(%)
Computes the modulo of the first argument by the second argument.
Number Comparisons
Comparisons on numbers.
Pervasives.(<)
Checks if the first argument is less than the second argument.
Pervasives.(>)
Checks if the first argument is greater than the second argument.
Pervasives.(<=)
Checks if the first argument is less than or equal to the second argument.
Pervasives.(>=)
Checks if the first argument is greater than or equal to the second argument.
Boolean Operations
Operations on booleans.
Pervasives.(!)
Computes the boolean “not” of the argument.
Pervasives.(&&)
Computes the boolean “and” of the two arguments.
Pervasives.(||)
Computes the boolean “or” of the two arguments.
Box Operations
Operations on boxes.
Pervasives.box
Creates a box with the same type as the argument. The argument is used as the initial value.
Pervasives.unbox
Retrieves the current value from the box.
Pervasives.(+=)
Computes the sum of the value in the box and the second argument and stores the result in the box.
Pervasives.(-=)
Computes the difference of the value in the box and the second argument and stores the result in the box.
Pervasives.(*=)
Computes the product of the value in the box and the second argument and stores the result in the box.
Pervasives.(/=)
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
Accepts any argument and returns void
.
Assertions
Functions that raise if conditions are not met.
Pervasives.assert
Assert that the given condition (boolean) is true
. Raises AssertionError
if the condition is false
.
Failure
Functions that raise unconditionally.
Pervasives.fail
Unconditionally raise Failure
with a message.
Printing
Functions that deal with printing.
Pervasives.print
Prints the value of the argument to the console. Works for any type. Internally, print
calls toString
on the argument.
Pervasives.toString
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
Returns the argument untouched.