Saturday, December 5, 2026

knowledge-view-of-Julia-and_Rust

knowledge-view-of-Julia-and_Rust

Mini Knowledge Systems in Programming Languages: Lessons from Rust, Julia, and C

1. A Different Lens on Programming Languages

When people talk about “knowledge systems,” they usually imagine something massive: knowledge graphs, ontologies, reasoning databases. But there is a humbler — and, I would argue, more powerful — lens: viewing a programming language as a mini knowledge system.

The core idea is simple. A good programming language is not just syntax and semantics; it is a set of local rules checked before the program runs. This rule set is small enough to check piece by piece, yet tight enough that if every piece is correct, the whole is correct. This is the “local to global” property — guarantees that start local and propagate outward.

Two modern languages embody this spirit most clearly: Rust and Julia. But they do it in different ways, and that difference is worth studying.

Before going further, one clarification: this is not an article about syntax. It is about how a small system can guarantee a large property — and why that pattern matters for any serious knowledge system.

2. From C to Rust and Julia: The Safety Problem

C — absolute freedom, absolute responsibility

C is the language of freedom. Pointers are numbers; anyone can point anywhere, anytime. C has no notion of “who owns this memory.” This gives C its performance and flexibility, but it is also the source of nearly every classic memory bug: use-after-free, double-free, data races, dangling pointers.

The key point: C does not “have a global problem” — C lacks an access-rights model to check locally. Without a rights model, there is nothing for the compiler to check, so safety depends entirely on programmer discipline. That is why C remains dangerous after decades.

TypeScript — a step forward on types

TypeScript emerged to solve part of the problem: adding static types to JavaScript. It lets programmers describe “this variable has this type,” and the compiler statically checks that operations are valid. This was an important step — for the first time, a set of local rules (type checking at each expression) propagated globally (the whole program is well-typed).

But TypeScript leaves gaps: it does not manage memory, has no notion of ownership, and its type system is far weaker than strongly functional languages.

Julia — a strong, flexible type system

Julia goes further at the type layer. It has:

  • Multiple dispatch — selecting a method based on argument types at the call site.
  • Local type inference — inferring types within each function.
  • A compositional subtyping lattice — the subtype relation is transitive, and local checks compose into global conclusions.

Julia’s design choice is revealing: it has local rules with compositionality, but it does not reject compilation when types cannot be fully inferred. It runs, and may throw a MethodError at runtime. This is the “soft” mode: infer statically what can be inferred, let runtime handle the rest, and rely on tools like @code_warntype for human oversight.

Rust — the “hard” mode

Rust takes the opposite stance. It adds two major mechanisms on top of a strong type system:

  • Traits — contracts that a type can implement, with coherence rules ensuring the set of implementations is closed and knowable.
  • The borrow checker — ownership, borrowing, and lifetimes, ensuring memory safety without a garbage collector.

Rust’s defining choice: it refuses to compile when a rule is violated. No runtime fallback. This is the “hard” mode: prove everything statically, or reject.

The new lens: a local checker with core rules, propagating globally

Here is the reframing that matters. Both Rust and Julia can be seen as mini knowledge systems:

  • They have a small set of core rules (like Lean’s kernel rules).
  • These rules are local — each one talks about a small judgment (a borrow at a program point, a call site’s type).
  • The rules are compositional — local correctness implies global correctness.
  • The system propagates local guarantees outward to the whole program.

This is exactly the structure of a formal proof assistant like Lean: a small trusted kernel with a few core rules, and everything else derived from them. Rust and Julia are, in their own domains, the same pattern.

3. How the Local Mechanisms Work — and How They Propagate

Julia: dispatch, inference, subtyping

Julia’s local-to-global story rests on three mechanisms:

Multiple dispatch. Each function call selects a method based on the local types of its arguments. The decision is local — it does not need to see the whole program.

Local type inference. Within each function, Julia infers types from input types through intermediate variables to the return type. This is dataflow-local reasoning.

Compositional subtyping. The subtype relation <: is transitive: if A <: B and B <: C, then A <: C. A local check (“is A a subtype of B?”) composes into global conclusions about the type hierarchy.

Together: if every expression is locally well-typed under dispatch and subtyping rules, the whole program has consistent type semantics. That is Julia’s “local to global.”

But Julia’s guarantee is conditional. When inference succeeds statically, the guarantee is as strong as Rust’s. When dynamic dispatch is needed (e.g., Vector{Any}), the global guarantee weakens — the system still maintains type consistency, but cannot prove ahead of time that every call has a matching method. That is why MethodError exists.

Rust: borrow, trait, lifetime

Rust’s local-to-global story has three independent layers, each with its own core rules and its own composition mechanism:

Layer 1 — Borrow and ownership. At each program point, each variable has a rights state: owned, shared-borrowed, mutably-borrowed, or moved. The borrow checker verifies each point locally. Composition works by induction over control flow: the initial state is valid, each step preserves the invariant, so the whole execution is safe. This is the layer formalized by RustBelt using Iris separation logic.

Layer 2 — Traits and generics. At each generic call site, the compiler checks that the concrete type satisfies the trait bound. Composition works by modus ponens plus coherence: the orphan rule ensures the set of implementations is closed — no hidden implementation can appear later — so local checks yield global conclusions about contract satisfaction. This closure property is the precondition for static reasoning.

Layer 3 — Lifetimes. Each borrow has a lifetime 'a. The relation 'a: 'b (a outlives b) is a partial order. Composition works by transitivity: local outlives relations compose into global ones, and the compiler verifies the whole chain. This is the bridge between the borrow layer and the type layer.

These three layers are independent but interacting — lifetime connects borrow and type; trait bounds use types; generics use all three. This is why Rust is complex but powerful: it has three compositional rule systems stacked, each formally justified.

The contrast in one table

Aspect Julia Rust
Local rules Dispatch, inference, subtyping Borrow, trait, lifetime
Composition Transitive subtyping + dispatch resolution Induction + coherence + transitivity
Static guarantee Partial (conditional on inference) Full (or reject)
On violation Runtime error (MethodError) Compile-time rejection
Formally proven? No full formalization Yes (RustBelt, Iris)

What this means for knowledge systems

The lesson is not “copy Rust” or “copy Julia.” It is that a small set of local, compositional rules can guarantee a large global property — and that the choice between hard rejection and soft runtime fallback depends on whether you can prove compositionality.

For a legal knowledge system, the mapping is direct:

  • Borrow/ownership → provenance and validity scope. Every fact and rule has an owner and a validity window. Local rule: at each inference step, every reference must still be valid. Composition: induction over the inference chain — no step uses an expired reference.
  • Trait/contract → rule contracts with coherence. Every rule is a contract: what inputs it requires, what outputs it guarantees. Local rule: at each rule application, inputs satisfy the contract. Composition: modus ponens plus a coherence-equivalent — no hidden rule can appear later and change semantics. That closure is the precondition for static reasoning.
  • Lifetime → legal validity periods. Every provision has a lifetime from enactment to repeal. Local rule: every reference falls within the lifetime. Composition: transitivity of the “still in force” relation.

And the deepest lesson: counting rules is meaningless; structure is everything. A system with two compositional rules is stronger than one with ten non-compositional rules. Rust and Julia succeed not because of how many rules they have, but because their rules are local, compositional, and — in Rust’s case — formally proven to compose.

That is the pattern worth borrowing. Not the syntax, not the number of rules — the architecture: a small trusted core, local checks, compositional guarantees, and a clear boundary between what is proven statically and what is left to runtime.

Popular Posts