Mojo (programming language)

Mojo is a programming language in the Python family that is currently under development. It is available both in browsers via Jupyter notebooks, and locally on Linux and macOS. Mojo aims to combine the usability of higher level programming languages, specifically Python, with the performance of lower level programming languages like C++, Rust, and Zig. The Mojo compiler is currently closed source with an open source standard library, although Modular, the company behind Mojo, has stated their intent to eventually open source the Mojo programming language itself as it matures.

Mojo builds upon the MLIR compiler framework instead of directly on the lower level LLVM compiler framework that many languages like Julia, Swift, clang and Rust do. MLIR is a newer compiler framework that allows Mojo to take advantage of higher level compiler passes not available in LLVM alone and allows Mojo to compile down and target more than just CPUs, including producing code that can run on GPUs, TPUs, ASICs and other accelerators. It can also often more effectively use certain types of CPU optimizations directly, like SIMD without direct intervention by the developer like in many other languages. According to Jeremy Howard of fast.ai, Mojo can be seen as "syntax sugar for MLIR" and for that reason Mojo is well optimized for applications like AI.

Origin and Development History
The Mojo programming language was created by Modular Inc, which was founded by Chris Lattner, the original architect of the Swift programming language and LLVM, and Tim Davis, a former Google employee.

According to public change logs, Mojo development goes back to 2022. In May of 2023, the first publicly testable version was made available online via a hosted playground. By September 2023 Mojo was available for local download for Linux and by October 2023 it was also made available for download on Apple's macOS.

In March of 2024, Modular open sourced the Mojo standard library and started accepting community contributions under the Apache 2.0 license.

Features
Mojo has the following features and characteristics:


 * Mojo uses LLVM and MLIR as its compilation backend.


 * Mojo uses inferred static typing.


 * Mojo was created for easy transition from Python. The language has syntax similar to Python's, and allows users to import Python modules.


 * Mojo is not open source, but it is planned to become open source in the future.


 * Mojo has a borrow checker, an influence from Rust.


 * Mojo plans to add a foreign function interface to call C/C++ and Python code.


 * Mojo is not source-compatible with Python 3, only providing a subset of its syntax, e.g. missing the global keyword, list and dictionary comprehensions, and support for classes. Further, Mojo also adds features that enable performant low-level programming: fn for creating typed, compiled functions and "struct" for memory-optimized alternatives to classes. Mojo structs support methods, fields, operator overloading, and decorators.


 * Mojo def functions use value semantics by default (functions receive a copy of all arguments and any modifications are not visible outside the function), while Python functions use reference semantics (functions receive a reference on their arguments and any modification of a mutable argument inside the function is visible outside).


 * Mojo files use the .🔥 or .mojo file extension.

Programming examples
In Mojo, functions can be declared using both fn (for performant functions) or def (for Python compatibility).

Basic arithmetic operations in Mojo with a def function:

and with an fn function:

The manner in which Mojo employs var and let for mutable and immutable variable declarations respectively mirrors the syntax found in Swift. In Swift, var is used for mutable variables, while let is designated for constants or immutable variables.

Variable declaration and usage in Mojo:

Usage
The Mojo SDK allows Mojo programmers to compile and execute Mojo source files locally from the command line and currently supports Ubuntu and macOS. Additionally, there is a Mojo extension for Visual Studio Code which provides code completion and tooltips.

In January 2024, an inference model of LLaMA2 written in Mojo was released to the public.