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.[17] Intention behind Mojo is to bridge the gap between Pythonβs ease of use and the fast performance required for cutting-edge AI applications.[18]
According to public change logs, Mojo development goes back to 2022.[19] In May of 2023, the first publicly testable version was made available online via a hosted playground.[20] By September 2023 Mojo was available for local download for Linux[21] and by October 2023 it was also made available for download on Apple's macOS.[22]
In March of 2024, Modular open sourced the Mojo standard library and started accepting community contributions under the Apache 2.0 license.[23][24]
Features
Mojo was created for an easy transition from Python. The language has syntax similar to Python's, with inferred static typing,[25] and allows users to import Python modules.[26] It uses LLVM and MLIR as its compilation backend.[7][27][28] The language also intends to add a foreign function interface to call C/C++ and Python code. The language 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, compiledfunctions and "struct" for memory-optimized alternatives to classes. Mojo structs support methods, fields, operator overloading, and decorators.[8]
The language also provides a borrow checker, an influence from Rust.[29] 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).[30]
The language is not open source, but it is planned to be made open source in the future.[31][3][32][33]
Programming examples
In Mojo, functions can be declared using both fn (for performant functions) or def (for Python compatibility).[26]
Basic arithmetic operations in Mojo with a def 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.[26]
Variable declaration and usage in Mojo:
fnmain():
letx=1lety: Inty=1varz=0z+=1
Usage
The Mojo SDK allows Mojo programmers to compile and execute Mojo source files locally from a command-line interface and currently supports Ubuntu and macOS.[34] 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.[35]
^"Mojo programming manual". docs.modular.com. Modular. 2023. Retrieved 2023-09-26. Mojo is a programming language that is as easy to use as Python but with the performance of C++ and Rust. Furthermore, Mojo provides the ability to leverage the entire Python library ecosystem.
^"Why Mojo - A language for next-generation compiler technology". docs.modular.com. Modular. 2023. Retrieved 2023-09-26. While many other projects now use MLIR, Mojo is the first major language designed expressly for MLIR, which makes Mojo uniquely powerful when writing systems-level code for AI workloads.
^"Mojo programming manual". Modular. Archived from the original on 2023-06-11. Retrieved 2023-06-11. All values passed into a Python def function use reference semantics. This means the function can modify mutable objects passed into it and those changes are visible outside the function. However, the behavior is sometimes surprising for the uninitiated, because you can change the object that an argument points to and that change is not visible outside the function. All values passed into a Mojo function use value semantics by default. Compared to Python, this is an important difference: A Mojo def function receives a copy of all arguments: it can modify arguments inside the function, but the changes are not visible outside the function.