Mojo (programming language)

Last updated
Mojo
Paradigms
Family Python
Designed by Chris Lattner [1]
Developer Modular Inc.
First appeared2023;1 year ago (2023)
Preview release
24.4 [2] / May 24, 2024;6 months ago (2024-05-24)
Typing discipline
OS Cross-platform: Linux, macOS
License Apache 2.0 with LLVM Exceptions [3]
Filename extensions .🔥 (the fire emoji/U+1F525 Unicode character), alternatively .mojo
Website www.modular.com/mojo
Influenced by
Python, Cython,[ citation needed ] C, C++, Rust, Swift, Zig

Mojo is a programming language in the Python family that is currently under development. [4] [5] [6] It is available both in browsers via Jupyter notebooks, [6] [7] and locally on Linux and macOS. [8] [9] Mojo aims to combine the usability of a high-level programming language, specifically Python, with the performance of a system programming language such as C++, Rust, and Zig. [10] As of 2024, the Mojo compiler is opensource software (closed source) with an open source standard library. Modular, the company behind Mojo, has stated an intent to eventually open source the Mojo language, as it matures. [3]

Contents

Mojo builds on the Multi-Level Intermediate Representation (MLIR) compiler software framework instead of directly on the lower level LLVM compiler framework, as do many languages such as Julia, Swift, Clang, and Rust. [11] [12] MLIR is a newer compiler framework that allows Mojo to exploit higher level compiler passes unavailable in LLVM alone, and allows Mojo to compile down and target more than only central processing units (CPUs), including producing code that can run on graphics processing units (GPUs), Tensor Processing Units (TPUs), application-specific integrated circuits (ASICs) and other accelerators. It can also often more effectively use certain types of CPU optimizations directly, like single instruction, multiple data (SIMD) with no direct intervention by a developer, as occurs in many other languages. [13] [14] 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 artificial intelligence (AI). [15]

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. [16] Intention behind Mojo is to bridge the gap between Python’s ease of use and the fast performance required for cutting-edge AI applications. [17]

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

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

Features

Mojo was created for an easy transition from Python. The language has syntax similar to Python's, with inferred static typing, [24] and allows users to import Python modules. [25] It uses LLVM and MLIR as its compilation backend. [6] [26] [27] 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, compiled functions and "struct" for memory-optimized alternatives to classes. Mojo structs support methods, fields, operator overloading, and decorators. [7]

The language also provides a borrow checker, an influence from Rust. [28] 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). [29]

The language is not open source, but it is planned to be made open source in the future. [30] [3] [31] [32]

Programming examples

In Mojo, functions can be declared using both fn (for performant functions) or def (for Python compatibility). [25]

Basic arithmetic operations in Mojo with a def function:

defsub(x,y):"""A pythonic subtraction."""res=x-yreturnres

and with an fn function:

fnadd(x: Int,y: Int)-> Int:     """A rustacean addition."""letres: Int=x+yreturnres

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. [25]

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. [33] 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. [34]

See also

Related Research Articles

<span class="mw-page-title-main">Python (programming language)</span> General-purpose programming language

Python is a high-level, general-purpose programming language. Its design philosophy emphasizes code readability with the use of significant indentation.

Standard ML (SML) is a general-purpose, high-level, modular, functional programming language with compile-time type checking and type inference. It is popular for writing compilers, for programming language research, and for developing theorem provers.

In object-oriented (OO) and functional programming, an immutable object is an object whose state cannot be modified after it is created. This is in contrast to a mutable object, which can be modified after it is created. In some cases, an object is considered immutable even if some internally used attributes change, but the object's state appears unchanging from an external point of view. For example, an object that uses memoization to cache the results of expensive computations could still be considered an immutable object.

<span class="mw-page-title-main">LLVM</span> Compiler backend for multiple programming languages

LLVM is a set of compiler and toolchain technologies that can be used to develop a frontend for any programming language and a backend for any instruction set architecture. LLVM is designed around a language-independent intermediate representation (IR) that serves as a portable, high-level assembly language that can be optimized with a variety of transformations over multiple passes. The name LLVM originally stood for Low Level Virtual Machine, though the project has expanded and the name is no longer officially an initialism.

<span class="mw-page-title-main">History of programming languages</span>

The history of programming languages spans from documentation of early mechanical computers to modern tools for software development. Early programming languages were highly specialized, relying on mathematical notation and similarly obscure syntax. Throughout the 20th century, research in compiler theory led to the creation of high-level programming languages, which use a more accessible syntax to communicate instructions.

In a programming language, an evaluation strategy is a set of rules for evaluating expressions. The term is often used to refer to the more specific notion of a parameter-passing strategy that defines the kind of value that is passed to the function for each parameter and whether to evaluate the parameters of a function call, and if so in what order. The notion of reduction strategy is distinct, although some authors conflate the two terms and the definition of each term is not widely agreed upon.

<span class="mw-page-title-main">Scala (programming language)</span> General-purpose programming language

Scala is a strong statically typed high-level general-purpose programming language that supports both object-oriented programming and functional programming. Designed to be concise, many of Scala's design decisions are intended to address criticisms of Java.

this, self, and Me are keywords used in some computer programming languages to refer to the object, class, or other entity which the currently running code is a part of. The entity referred to thus depends on the execution context. Different programming languages use these keywords in slightly different ways. In languages where a keyword like "this" is mandatory, the keyword is the only way to access data and methods stored in the current object. Where optional, these keywords can disambiguate variables and functions with the same name.

<span class="mw-page-title-main">Python syntax and semantics</span> Set of rules defining correctly structured programs

The syntax of the Python programming language is the set of rules that defines how a Python program will be written and interpreted. The Python language has many similarities to Perl, C, and Java. However, there are some definite differences between the languages. It supports multiple programming paradigms, including structured, object-oriented programming, and functional programming, and boasts a dynamic type system and automatic memory management.

Haxe is a high-level cross-platform programming language and compiler that can produce applications and source code for many different computing platforms from one code-base. It is free and open-source software, released under an MIT License. The compiler, written in OCaml, is released under the GNU General Public License (GPL) version 2.

A property, in some object-oriented programming languages, is a special sort of class member, intermediate in functionality between a field and a method. The syntax for reading and writing of properties is like for fields, but property reads and writes are (usually) translated to 'getter' and 'setter' method calls. The field-like syntax is easier to read and write than many method calls, yet the interposition of method calls "under the hood" allows for data validation, active updating, or implementation of what may be called "read-only fields".

In computer programming, an anonymous function is a function definition that is not bound to an identifier. Anonymous functions are often arguments being passed to higher-order functions or used for constructing the result of a higher-order function that needs to return a function. If the function is only used once, or a limited number of times, an anonymous function may be syntactically lighter than using a named function. Anonymous functions are ubiquitous in functional programming languages and other languages with first-class functions, where they fulfil the same role for the function type as literals do for other data types.

<span class="mw-page-title-main">Chris Lattner</span> American software engineer (born 1978)

Christopher Arthur Lattner is an American software engineer and creator of LLVM, the Clang compiler, the Swift programming language and the MLIR compiler infrastructure.

<span class="mw-page-title-main">Rust (programming language)</span> General-purpose programming language

Rust is a general-purpose programming language emphasizing performance, type safety, and concurrency. It enforces memory safety, meaning that all references point to valid memory. It does so without a traditional garbage collector; instead, memory safety errors and data races are prevented by the "borrow checker", which tracks the object lifetime of references at compile time.

<span class="mw-page-title-main">MLIR (software)</span> C++ framework for compiler development

MLIR is a unifying software framework for compiler development. MLIR can make optimal use of a variety of computing platforms such as central processing units (CPUs), graphics processing units (GPUs), data processing units (DPUs), Tensor Processing Units (TPUs), field-programmable gate arrays (FPGAs), artificial intelligence (AI) application-specific integrated circuits (ASICs), and quantum computing units (QPUs).

Objective-C is a high-level general-purpose, object-oriented programming language that adds Smalltalk-style message passing (messaging) to the C programming language. Originally developed by Brad Cox and Tom Love in the early 1980s, it was selected by NeXT for its NeXTSTEP operating system. Due to Apple macOS’s direct lineage from NeXTSTEP, Objective-C was the standard language used, supported, and promoted by Apple for developing macOS and iOS applications from 1997, when Apple purchased NeXT until the introduction of the Swift language in 2014.

<span class="mw-page-title-main">GraalVM</span> Virtual machine software

GraalVM is a Java Development Kit (JDK) written in Java. The open-source distribution of GraalVM is based on OpenJDK, and the enterprise distribution is based on Oracle JDK. As well as just-in-time (JIT) compilation, GraalVM can compile a Java application ahead of time. This allows for faster initialization, greater runtime performance, and decreased resource consumption, but the resulting executable can only run on the platform it was compiled for.

Swift is a high-level general-purpose, multi-paradigm, compiled programming language created by Chris Lattner in 2010 for Apple Inc. and maintained by the open-source community. Swift compiles to machine code and uses an LLVM-based compiler. Swift was first released in June 2014 and the Swift toolchain has shipped in Xcode since Xcode version 6, released in September 2014.

<span class="mw-page-title-main">Zig (programming language)</span> A general-purpose programming language, toolchain to build Zig/C/C++ code

Zig is an imperative, general-purpose, statically typed, compiled system programming language designed by Andrew Kelley. It is intended as a successor to the language C, with the intent of being even smaller and simpler to program in, while offering more functionality. It is free and open-source software, released under an MIT License.

References

  1. "How Modular simplified AI software infrastructure". Fast Company. Retrieved 2024-08-19.
  2. "Mojo Changelog". Modular. Retrieved 2024-04-14.
  3. 1 2 3 Modular Team (28 March 2024). "Modular: The Next Big Step in Mojo🔥 Open Source". Modular. Archived from the original on 2024-10-09. Retrieved 2024-11-09.
  4. "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.
  5. "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.
  6. 1 2 3 Krill, Paul (4 May 2023). "Mojo language marries Python and MLIR for AI development". InfoWorld.
  7. 1 2 Yegulalp, Serdar (7 June 2023). "A first look at the Mojo language". InfoWorld.
  8. Deutscher, Maria (7 September 2023). "Modular makes its AI-optimized Mojo programming language generally available". Silicon Angle. Retrieved 2023-09-11.
  9. "Mojo for Mac OS". Modular. Retrieved 2023-10-19.
  10. "Mojo: Programming language for all of AI". Modular.com. Retrieved 2024-02-28.
  11. Krill, Paul (2023-05-04). "Mojo language marries Python and MLIR for AI development". InfoWorld. Retrieved 2024-05-28.
  12. "Should Julia use MLIR in the future?". Julia Programming Language. 2024-02-20. Retrieved 2024-05-28.
  13. "Modular Docs: Why Mojo". docs.modular.com. Retrieved 2024-05-28.
  14. https://llvm.org/devmtg/2023-10/slides/keynote/Mojo.pdf
  15. Howard, Jeremy (2023-05-04). "fast.ai - Mojo may be the biggest programming language advance in decades". fast.ai. Retrieved 2024-05-28.
  16. Claburn, Thomas (2023-05-05). "Modular finds its Mojo, a Python superset with C-level speed". The Register. Retrieved 2023-08-08.
  17. "Mojo Rising: The resurgence of AI-first programming languages". 21 May 2024.
  18. "Mojo changelog".
  19. "A unified, extensible platform to superpower your AI". Modular.com. Retrieved 2024-04-14.
  20. "Mojo - It's finally here!". Modular.com. Retrieved 2024-04-14.
  21. "Mojo is now available on Mac". Modular.com. Retrieved 2024-04-14.
  22. "Modular open-sources its Mojo AI programming language's core components". SiliconANGLE. 2024-03-28. Retrieved 2024-05-28.
  23. "mojo/stdlib/README.md at nightly · modularml/mojo". GitHub. Retrieved 2024-05-28.
  24. "Modular Docs - Mojo programming manual". docs.modular.com. Retrieved 2023-10-19.
  25. 1 2 3 "Modular Docs - Mojo programming manual". docs.modular.com. Retrieved 2023-10-31.
  26. Lattner, Chris; Pienaar, Jacques (2019). MLIR Primer: A Compiler Infrastructure for the End of Moore's Law (Technical report). Retrieved 2022-09-30.
  27. 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].
  28. "Modular Docs: Ownership and borrowing". Modular. Retrieved 2024-02-29.
  29. "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.
  30. "Open Source | Mojo🔥 FAQ | Modular Docs". docs.modular.com. Retrieved 2024-11-09.
  31. "Modular: Pricing". www.modular.com. Retrieved 2024-11-09.
  32. Modular (2024-08-22). Comment from @modularinc . Retrieved 2024-11-09 via YouTube.
  33. "Modular Docs - Mojo roadmap & sharp edges". docs.modular.com. Retrieved 2023-10-31.
  34. "llama2.mojo changelog". GitHub .