Using the Motoko base library

For various practical language engineering reasons, the design of Motoko strives to minimize built-in types and operations.

Instead, whenever possible, the Motoko base library provides the types and operations that make the language feel complete. However, this base library is still currently under development, and is still incomplete.

This section describes a selected subset of modules from the Motoko base library, with an emphasis on the modules that support core features and the libraries that are referenced in the examples. The libraries documented in this section are the most stable and least likely to change. However, all of the base library APIs are subject to change over time to varying degrees. You should note, in particular, that the size and number of modules and functions included in the base library may increase dramatically as Motoko evolves.

Importing from the base library

To import from the base library, use the import keyword. Give a local module name to introduce, in this example P for “Prelude”, and a URL where the import declaration may locate the imported module:

import Debug "mo:base/Debug";
Debug.print("hello world");

This example illustrates how to import Motoko code—indicated by using the mo: prefix—using the base/ base library path and the module name prelude. You can also import Motoko code and other modules using relative paths. For example, if you have created a Motoko program named in the same folder as your main program, you could include it with an import declaration like this:

import Types "./types";

Using predefined compiler prelude function

The compiler itself contains a prelude in every program, effectively extending the language definition with a set of types and declarations that are always available.

In addition to these built-in compiler definitions, the prelude of the base library contributes some additional functions in a supplemental Prelude module.

In contrast to the definitions in other modules, the compiler prelude definitions are always present, and do not require any import declaration to bring them into the current scope.