Mojo (programming language)
This article has multiple issues. Please help improve it or discuss these issues on the talk page. (Learn how and when to remove these messages)
|
Paradigm | |
---|---|
Family | Python |
Designed by | Chris Lattner |
Developer | Modular Inc. |
First appeared | 2023 |
Preview release | 24.2.1 [1]
/ April 12, 2024 |
Typing discipline | |
OS | Cross-platform |
License | Proprietary |
Filename extensions | .🔥 (the fire emoji/U+1F525 Unicode character), alternatively .mojo |
Website | www |
Influenced by | |
Python, Cython[citation needed], C, C++, Rust, Swift, Zig |
Mojo is a proprietary programming language that is currently under development.[2][3][4] It is available both in browsers via Jupyter notebooks,[4][5] and locally on both Linux and macOS.[6][7] Mojo combines the usability of Python with its syntax (and aims to become a superset of it[8]) with the high performance of languages like C.[9]
Origin, design and development
- In 2022, the Modular company was founded by Chris Lattner, the original architect of the Swift programming language and LLVM, and Tim Davis, a former Google employee.[10]
- In September 2022, an initial build of Mojo was released internally by Modular Inc.[11]
Features
- Mojo uses inferred static typing.[15]
- Mojo was created for easy transition from Python. The language has syntax similar to Python's, and allows users to import Python modules.[16]
- Mojo is not open source, but it is planned to become open source in the future.[17]
- Mojo has a borrow checker, an influence from Rust.[18]
- 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.[5]
- 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).[19]
- Mojo files use the .🔥 or .mojo file extension.[2]
Programming examples
In Mojo, functions can be declared using both fn (for performant functions) or def (for Python compatibility).[16]
Basic arithmetic operations in Mojo with a def function:
def sub(x, y):
"""A pythonic subtraction."""
res = x - y
return res
and with an fn function:
fn add(x: Int, y: Int) -> Int:
"""A rustacean addition."""
let res: Int = x + y
return res
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.[16]
Variable declaration and usage in Mojo:
fn main():
let x = 1
let y: Int
y = 1
var z = 0
z += 1
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.[20] Additionally, there is a Mojo extension for Visual Studio Code which provides code completion and tooltips.
See also
References
- ^ "Mojo Changelog". Modular. Retrieved 2024-04-14.
- ^ a b "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.
- ^ a b c Krill, Paul (4 May 2023). "Mojo language marries Python and MLIR for AI development". InfoWorld.
- ^ a b Yegulalp, Serdar (7 June 2023). "A first look at the Mojo language". InfoWorld.
- ^ Deutscher, Maria (7 September 2023). "Modular makes its AI-optimized Mojo programming language generally available". Silicon Angle. Retrieved 2023-09-11.
- ^ "Mojo for Mac OS". Modular. Retrieved 2023-10-19.
- ^ "Mojo Manual". docs.modular.com. Modular. 2024-03-02. Retrieved 2024-03-02.
We also designed Mojo as a superset of Python because we love Python
- ^ "Mojo 🔥: Programming language for all of AI". www.modular.com. Retrieved 2024-02-28.
- ^ Claburn, Thomas (2023-05-05). "Modular finds its Mojo, a Python superset with C-level speed". The Register. Retrieved 2023-08-08.
- ^ "Mojo🔥 changelog".
- ^ "llama2.mojo🔥 changelog".
- ^ Lattner, Chris; Pienaar, Jacques (2019). MLIR Primer: A Compiler Infrastructure for the End of Moore's Law (Technical report). Retrieved 2022-09-30.
- ^ Lattner, Chris; Amini, Mehdi; Bondhugula, Uday; Cohen, Albert; Davis, Andy; Pienaar, Jacques; Riddle, River; Shpeisman, Tatiana; Vasilache, Nicolas; Zinenko, Oleksandr (2020-02-29). "MLIR: A Compiler Infrastructure for the End of Moore's Law". arXiv:2002.11054 [cs.PL].
- ^ "Modular Docs - Mojo🔥 programming manual". docs.modular.com. Retrieved 2023-10-19.
- ^ a b c "Modular Docs - Mojo🔥 programming manual". docs.modular.com. Retrieved 2023-10-31.
- ^ "Welcome to Mojo 🔥". GitHub. Modular. 2023-10-31. Retrieved 2023-10-31.
- ^ "Ownership and borrowing | Modular Docs". Modular. Retrieved 2024-02-29.
- ^ "Mojo🔥 programming manual". Modular. 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 def 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.
- ^ "Modular Docs - Mojo🔥 roadmap & sharp edges". docs.modular.com. Retrieved 2023-10-31.