Notes on MOJO
Why Mojo?
Launch of Mojo programming language
On May the 2nd 2023, Mojo was announced 1 to the world by Modular. Check out the
Advantages of using Mojo
- Powerful compile time metaprogramming
- Integration of adaptive compilation techniques
- Caching thorught the compilation flow
- Leverages the MLIR ecosystem
- Systems level programming
- AI/ML
- Member of the Python family (like the big brother of Python)
- It is a superset of Python
- Embraces Python ecosystem
- Uses CPYthon to run Python3 out of the box
- Migrator to move from Python to Mojo - you can progressively move code
Python problems
- Not suitable for systems programming
- Complicated to build hybrid libraries (C/C++), requires low level understanding of CPython. The two world problem. Issues with debuggers, packaging is a pain.
- Pushes the “graph based” paradigm, which is not very usable. (Pytorch 2 approach)
- Deployment of Python based applications is challenging, specially when it comes to controlling dependencies.
Other approaches to improve Python
Improving CPython and JIT compiling: Great advances in CPython for Python 3.11 and Python 3.12 will include a trace optimizer. Great efforts but not help to get a unified language onto an accelerator. Systems programmers dont just seek performance, they also want a lot of “predictability and control” over how compute happens.
Python subsets: For example TorchScript, which uses Python like syntax, they can be easier to learn. Sadly not wide adoption. Generally dont interoperate with the Python ecosystem, missing tooling (debugger). This approaches attempt to solve a weak point of Python, but are not as good as Python’s strong points. This approaches drive fragmentation and incompatibility makes migration difficult.
Embedded DSLs in Python: Think of Python decorators. They maintain compatibility with all Python ecosystem. Unfortunately the mini-language usually dont integrate well with debuggers and other workflow tooling.
Features
let
and var
Assign values to a name. let
declarations are immutable, var
declarations mutable. The values defined use lexical scoping and support name shadowing.
Support: type specifiers, patters and late initialization.
struct
s
Static, bound at compile time (not lookedup with dictionary indirection) and inlined into their container. Not implicitly indirect nor reference counted.
All instance properties must be explicitly declared with var
or let
, which allows compiler to layout and access property values precisely in memory with no indirection or overhead. Compiler can guarantee static dispatch, use guarandteed static access to fields and inline a struct into the stack frame or enclosing type.
Strong type checking
One of the primary ways to do strong type checking is with struct
type.
Overloaded functions(fn
) and methods(def
)
Mojo can infer the data types if you dont specify them, but you can define multiple functions with the same name but with different arguments, everywhere.
There is no support overloading soely on result type and does not use result type or contextual type information for type inference.
fn
definitions