# Built-in Functions Available in Monty: Complete Guide to the Rust Python Interpreter

> Explore Monty's 35 Rust compiled built-in functions like print len and sum for efficient VM dispatch. Get the complete guide to the Rust Python interpreter.

- Repository: [Pydantic/monty](https://github.com/pydantic/monty)
- Tags: api-reference
- Published: 2026-02-16

---

**Monty implements 35 Python built-in functions—including `print`, `len`, `sum`, and `enumerate`—as a Rust `BuiltinsFunctions` enum that compiles to a `CALL_BUILTIN` opcode for efficient VM dispatch.**

The `pydantic/monty` repository is a Rust-based Python interpreter designed for high-performance execution of Python code. Understanding which built-in functions are available in Monty is essential for developers porting existing Python scripts to this runtime. Monty exposes these functions through a centralized `BuiltinsFunctions` enum defined in [`crates/monty/src/builtins/mod.rs`](https://github.com/pydantic/monty/blob/main/crates/monty/src/builtins/mod.rs), providing direct Rust implementations of CPython's standard utilities.

## How Monty Dispatches Built-in Functions

Monty handles built-in function calls through a three-stage pipeline that avoids Python-level attribute lookups by encoding built-ins directly into the bytecode.

### Parsing and Resolution

When the parser encounters an identifier matching a built-in name, it resolves the token to a `BuiltinsFunctions` variant. This occurs in [`crates/monty/src/parse.rs`](https://github.com/pydantic/monty/blob/main/crates/monty/src/parse.rs), where the parser maintains a mapping of valid built-in names to their corresponding enum discriminants.

### Bytecode Generation

During compilation in [`crates/monty/src/bytecode/compiler.rs`](https://github.com/pydantic/monty/blob/main/crates/monty/src/bytecode/compiler.rs), the compiler emits a `CALL_BUILTIN` opcode with the enum discriminant (as a `u8`) as the operand. This hardcodes the target function into the bytecode, eliminating the need for runtime name resolution.

### VM Execution

The virtual machine executes the `CALL_BUILTIN` opcode in [`crates/monty/src/bytecode/vm/call.rs`](https://github.com/pydantic/monty/blob/main/crates/monty/src/bytecode/vm/call.rs). The VM uses `BuiltinsFunctions::from_repr` to convert the discriminant back to the enum variant, then invokes `BuiltinsFunctions::call`, which forwards to the appropriate implementation module (e.g., [`crates/monty/src/builtins/print.rs`](https://github.com/pydantic/monty/blob/main/crates/monty/src/builtins/print.rs)).

## Complete List of Built-in Functions Available in Monty

Monty currently implements **35 built-in functions**, covering core computational, type conversion, and iterable operations. The following table lists all available functions alphabetically with their implementation modules:

| Function | Module | Behavior |
|----------|--------|----------|
| `abs` | [`abs.rs`](https://github.com/pydantic/monty/blob/main/abs.rs) | Returns absolute value of a number |
| `all` | [`all.rs`](https://github.com/pydantic/monty/blob/main/all.rs) | Returns `True` if all elements of an iterable are true |
| `any` | [`any.rs`](https://github.com/pydantic/monty/blob/main/any.rs) | Returns `True` if any element of an iterable is true |
| `bin` | [`bin.rs`](https://github.com/pydantic/monty/blob/main/bin.rs) | Converts integer to binary string representation |
| `chr` | [`chr.rs`](https://github.com/pydantic/monty/blob/main/chr.rs) | Converts Unicode code point to character string |
| `divmod` | [`divmod.rs`](https://github.com/pydantic/monty/blob/main/divmod.rs) | Returns tuple of quotient and remainder |
| `enumerate` | [`enumerate.rs`](https://github.com/pydantic/monty/blob/main/enumerate.rs) | Returns iterator of tuples with index and value |
| `hash` | [`hash.rs`](https://github.com/pydantic/monty/blob/main/hash.rs) | Returns hash value of an object |
| `hex` | [`hex.rs`](https://github.com/pydantic/monty/blob/main/hex.rs) | Converts integer to hexadecimal string |
| `id` | [`id.rs`](https://github.com/pydantic/monty/blob/main/id.rs) | Returns identity (memory address) of an object |
| `isinstance` | [`isinstance.rs`](https://github.com/pydantic/monty/blob/main/isinstance.rs) | Checks object type membership |
| `len` | [`len.rs`](https://github.com/pydantic/monty/blob/main/len.rs) | Returns length of a container |
| `max` | [`min_max.rs`](https://github.com/pydantic/monty/blob/main/min_max.rs) | Returns maximum of iterable or arguments |
| `min` | [`min_max.rs`](https://github.com/pydantic/monty/blob/main/min_max.rs) | Returns minimum of iterable or arguments |
| `next` | [`next.rs`](https://github.com/pydantic/monty/blob/main/next.rs) | Retrieves next item from iterator |
| `oct` | [`oct.rs`](https://github.com/pydantic/monty/blob/main/oct.rs) | Converts integer to octal string |
| `ord` | [`ord.rs`](https://github.com/pydantic/monty/blob/main/ord.rs) | Returns Unicode code point of a character |
| `pow` | [`pow.rs`](https://github.com/pydantic/monty/blob/main/pow.rs) | Performs exponentiation |
| `print` | [`print.rs`](https://github.com/pydantic/monty/blob/main/print.rs) | Writes to stdout via `PrintWriter` |
| `repr` | [`repr.rs`](https://github.com/pydantic/monty/blob/main/repr.rs) | Returns official string representation |
| `reversed` | [`reversed.rs`](https://github.com/pydantic/monty/blob/main/reversed.rs) | Returns reversed iterator |
| `round` | [`round.rs`](https://github.com/pydantic/monty/blob/main/round.rs) | Rounds number to nearest integer |
| `sorted` | [`sorted.rs`](https://github.com/pydantic/monty/blob/main/sorted.rs) | Returns sorted list from iterable |
| `sum` | [`sum.rs`](https://github.com/pydantic/monty/blob/main/sum.rs) | Sums items of an iterable |
| `type` | [`type_.rs`](https://github.com/pydantic/monty/blob/main/type_.rs) | Returns type object of an argument |
| `zip` | [`zip.rs`](https://github.com/pydantic/monty/blob/main/zip.rs) | Aggregates elements from multiple iterables |

### Future Implementations

The `BuiltinsFunctions` enum in [`crates/monty/src/builtins/mod.rs`](https://github.com/pydantic/monty/blob/main/crates/monty/src/builtins/mod.rs) contains commented-out placeholders for additional built-ins not yet implemented. These include `bool`, `bytes`, `dict`, `open`, `range`, and `__import__`. These variants are reserved for future releases as the interpreter matures.

## Practical Examples Using Monty Built-ins

The following examples demonstrate how to use the built-in functions available in Monty through both the Python bindings and the native Rust API.

### Python API via `pydantic_monty`

When using Monty through its Python bindings, built-in functions work identically to CPython:

```python
from pydantic_monty import Monty

source = """
print('Hello, Monty!')
print('Length of list:', len([1, 2, 3]))
print('Sum of range:', sum(range(5)))
print('Sorted list:', sorted([3, 1, 4, 2]))
print('Absolute value:', abs(-42))
print('Binary of 10:', bin(10))
print('Max of args:', max(1, 4, 2))
"""

m = Monty(source)
result = m.run()
print('Monty returned:', result)

```

### Rust API Integration

For Rust developers embedding Monty directly, built-ins are invoked through the VM's execution context:

```rust
use monty::{
    Monty,
    heap::Heap,
    resource::SimpleResourceTracker,
    io::StdoutWriter,
};

let code = r#"
print('abs(-7) =', abs(-7))
print('max(1, 4, 2) =', max(1, 4, 2))
print('enumerate result:', list(enumerate(['a', 'b'])))
"#;

let mut heap = Heap::new(SimpleResourceTracker::default());
let mut writer = StdoutWriter;
let mut vm = Monty::new(&code, &mut heap, &mut writer);
vm.run().expect("execution should succeed");

```

Both examples demonstrate that Monty's built-in functions accept the same arguments and produce the same results as CPython, routed through the `BuiltinsFunctions` dispatch system.

## Key Source Files for Built-in Functions

Understanding the architecture of Monty's built-ins requires examining these specific source files:

| File | Role |
|------|------|
| [`crates/monty/src/builtins/mod.rs`](https://github.com/pydantic/monty/blob/main/crates/monty/src/builtins/mod.rs) | Defines the `BuiltinsFunctions` enum and central `call` dispatch method |
| [`crates/monty/src/builtins/print.rs`](https://github.com/pydantic/monty/blob/main/crates/monty/src/builtins/print.rs) | Implements `print` with `PrintWriter` integration for stdout handling |
| [`crates/monty/src/builtins/len.rs`](https://github.com/pydantic/monty/blob/main/crates/monty/src/builtins/len.rs) | Container length implementation |
| [`crates/monty/src/builtins/sum.rs`](https://github.com/pydantic/monty/blob/main/crates/monty/src/builtins/sum.rs) | Iterable summation logic |
| [`crates/monty/src/builtins/min_max.rs`](https://github.com/pydantic/monty/blob/main/crates/monty/src/builtins/min_max.rs) | Shared implementation for `min` and `max` |
| [`crates/monty/src/parse.rs`](https://github.com/pydantic/monty/blob/main/crates/monty/src/parse.rs) | Resolves identifier tokens to `BuiltinsFunctions` variants during AST construction |
| [`crates/monty/src/bytecode/compiler.rs`](https://github.com/pydantic/monty/blob/main/crates/monty/src/bytecode/compiler.rs) | Emits `CALL_BUILTIN` opcodes with enum discriminants as operands |
| [`crates/monty/src/bytecode/vm/call.rs`](https://github.com/pydantic/monty/blob/main/crates/monty/src/bytecode/vm/call.rs) | VM opcode handler that converts discriminants back to enum variants and invokes the implementation |

These files collectively demonstrate how Monty discovers, compiles, and executes built-in functions without requiring runtime symbol table lookups.

## Summary

- Monty implements **35 built-in functions** available in the `pydantic/monty` interpreter, ranging from `abs` and `bin` to `zip` and `enumerate`.
- The dispatch mechanism uses a Rust `BuiltinsFunctions` enum in [`crates/monty/src/builtins/mod.rs`](https://github.com/pydantic/monty/blob/main/crates/monty/src/builtins/mod.rs), compiled to a `CALL_BUILTIN` opcode for zero-overhead runtime performance.
- Current implementations cover core computational, iterable, and introspection utilities, while placeholders exist for future additions like `bool`, `bytes`, `dict`, `open`, `range`, and `__import__`.
- Both Python bindings (`pydantic_monty`) and native Rust APIs provide seamless access to these functions with CPython-compatible semantics.

## Frequently Asked Questions

### How does Monty's built-in function dispatch differ from CPython?

Monty encodes each built-in as a variant of the `BuiltinsFunctions` enum rather than using global function objects. During bytecode generation in [`crates/monty/src/bytecode/compiler.rs`](https://github.com/pydantic/monty/blob/main/crates/monty/src/bytecode/compiler.rs), the compiler emits a `CALL_BUILTIN` opcode containing the enum discriminant as a `u8` operand. The VM then uses `BuiltinsFunctions::from_repr` to resolve this discriminant to the correct Rust implementation, eliminating the runtime overhead of Python attribute lookups and dictionary searches required by CPython's `builtins` module.

### Which essential Python built-ins are not yet available in Monty?

While Monty offers 35 built-in functions covering mathematical operations (`abs`, `pow`, `round`), iterable processing (`len`, `sum`, `max`, `min`, `sorted`), and type conversion (`bin`, `hex`, `chr`, `ord`), several critical built-ins remain unimplemented. The `BuiltinsFunctions` enum in [`crates/monty/src/builtins/mod.rs`](https://github.com/pydantic/monty/blob/main/crates/monty/src/builtins/mod.rs) contains commented-out placeholders for `bool`, `bytes`, `dict`, `open`, `range`, and `__import__`. These will be enabled in future releases as the interpreter's standard library support expands.

### How is the `print` function implemented in Monty?

The `print` built-in is implemented in [`crates/monty/src/builtins/print.rs`](https://github.com/pydantic/monty/blob/main/crates/monty/src/builtins/print.rs) and integrates with Monty's I/O subsystem through the `PrintWriter` trait. Unlike a simple wrapper around stdout, this implementation allows the VM to direct output to configurable sinks during execution. When the VM encounters a `CALL_BUILTIN` opcode for the `print` discriminant, it invokes `BuiltinsFunctions::call`, which delegates to the formatting and writing logic in [`print.rs`](https://github.com/pydantic/monty/blob/main/print.rs) to process arguments and emit them via the configured `PrintWriter`.

### Can I use Monty as a drop-in replacement for CPython in existing projects?

Monty can execute Python scripts that rely on the 35 currently implemented built-in functions, making it suitable for pure computational workloads, data processing with iterables, and basic introspection tasks. However, because essential built-ins like `open`, `range`, `dict`, and `bool` are not yet implemented—as indicated by their placeholder status in [`crates/monty/src/builtins/mod.rs`](https://github.com/pydantic/monty/blob/main/crates/monty/src/builtins/mod.rs)—Monty cannot yet run full CPython applications that require file I/O, dictionary data structures, or boolean type construction. For supported operations, the `pydantic_monty` Python bindings provide CPython-compatible semantics.