Prisma Logo Prisma v0.2.12
Back to Blog & Benchmarks
Type Systems internals 2026-11-20 · Manuel Rubio

From v0.2.1 to v0.2.9: Polymorphic Specs, Bounded Specialization, and Real-World Elixir

Following up on our November 11 correctness post—what changed in Prisma's inference engine, how we tamed a 644s PPLT bottleneck, and what checking Elixir main revealed

#erlang #elixir #prisma #type-systems #gradual-typing #success-typing #pplt #ecto #phoenix #dialyzer #performance

In How Prisma Reasons About Types and Verifies Correctness on November 11 (v0.2.1), we ended our roadmap with an honest admission: our large-corpus evaluation covered Erlang (RabbitMQ), while ra and real-world Elixir codebases were still ahead of us.

Nine days and eight patch releases later—landing on v0.2.9—that gap has been exercised hard against ra, ecto, padmark, a full production Phoenix/Ecto application (3,477 dependency modules), and Elixir main itself.

Along the way, real Elixir code exposed subtle edge cases in polymorphic specs, macro-generated code, open struct maps, and a severe combinatorial blowup in PPLT construction. Here is what changed in the engine between v0.2.1 and v0.2.9, what stayed on the roadmap, and the exact numbers we measured.


1. What Changed in the Type & Inference Engine

1.1 Instantiating bounded_fun Clauses (when var :: bound)

Elixir's @spec ... when clauses (such as Ecto.Repo.Preloader.preload/4, structs when structs: [Ecto.Schema.t()] | Ecto.Schema.t() | nil) compile to 4-tuple bounded_fun AST nodes: {bounded_fun, RawArgs, RawRet, Constraints}.

In v0.2.1, declared_return/6 simply unfolded the bounds, erasing the caller's concrete argument type. Calling Repo.preload(payment, user: [:profile]) with a %Payment{} struct came back typed as nil | [Ecto.Schema.t()] | Ecto.Schema.t(), immediately triggering false gradual_mismatch warnings downstream in Ecto.Changeset.change/2.

declared_return/6 now calls instantiate_bounded_clause/3 to bind type variables to meet(ActualArg, Bound) at the call site before unfolding RawRet, preserving the caller's %Payment{} struct across the call.

1.2 Demand-Driven Specialization Without Exponential Blowup

Fixing Ecto.Repo.Preloader.preload/4 alone wasn't enough, because MyApp.Repo.preload/2 is an un-specced 1-clause default-argument wrapper generated by use Ecto.Repo that forwards to MyApp.Repo.preload/3, which in turn calls Ecto.Repo.Preloader.preload/4.

To preserve types across un-specced wrappers:

The impact on real libraries was dramatic:

Workload Before optimization v0.2.9 Speedup
postgrex (95 modules PPLT build) 358.69 s 1.78 s ~201× faster
ex_gram (492 modules PPLT build) 47.43 s 10.21 s ~4.6× faster
Production Phoenix/Ecto deps (3,477 modules) 644.50 s 48.13 s ~13.4× faster

In prisma_type, two additional bounds keep lattice operations predictable: an $O(1)$ __struct__ mismatch fast-path in subtype_map_fields/2, and a map_size(Visited) >= 5 cap in expand_remote for deeply chained remote types.

1.3 Elixir AST, Macros, and Pattern Refinement

Running against production Elixir apps and Elixir's own stdlib surfaced four Elixir-specific frontend gaps, all fixed and covered by EUnit tests (bringing the suite from 249 tests on November 11 to 297/297 passing today alongside 18/18 PropEr properties):

  1. Direct elixir_v1 debug info & external macro attribution (prisma_abstract / prisma_elixir): BEAM files compiled by Elixir also embed an Erlang abstract_code chunk that strips macro context metadata. abstract_forms/2 now defers {debug_info_v1, elixir_erl, _} to prisma_elixir:from_elixir_dbgi/5, and GenAttrs inspects all {context, CtxMod} entries in Meta (not just the first, which is often elixir_def), properly marking external macro injections like use Hammer (context: Hammer.ETS) as compiler_generated.
  2. Dynamic %mod{} struct patterns: In translate_pattern, %mod{} (where mod is a pattern variable rather than an alias) previously fell through to unwrap_atom/1 and turned into 'Elixir.Any', breaking variable binding with guards like when mod in @formatter_exceptions (a false discrepancy in ExUnit.Formatter). It now translates the variable pattern directly.
  3. Private macros in oxbow_code: defmacrop functions compile to MACRO-* helpers that are expanded at compile time rather than called at runtime. prisma_oxbow now ignores MACRO-* names, eliminating 70 false unused-function warnings across Elixir's stdlib.
  4. Match-scrutinee variable refinement: refine_match_scrutinee_vars/4 now refines bare {var, _, V} scrutinees in case branches (not only {match, ...} nodes), preventing false warnings after if/case truthiness checks.

2. Production Validation & Checking Elixir main

2.1 Production Phoenix/Ecto App (3,477 Dependency Modules)

On a real production Phoenix + Ecto + Oban + Hammer codebase (dymmer.com):

2.2 Checking Elixir main (3bce30a)

Running prisma check across all 6 core Elixir applications (elixir, eex, ex_unit, iex, logger, mix) on main (3bce30a) finishes in ~9.2 s with 0 discrepancy warnings:

Category Count Notes
discrepancy 0 Clean across all 6 core apps
opacity_violation 2 Real: Version inspecting @opaque Version.Requirement.t() (version.ex:325, 492)
oxbow_code 23 Real: 2 uncalled private functions (System.strip/1, System.read_stripped/1) and 21 unused private parameters
dead_code 32 Mostly defensive error clauses outside narrow @specs (List.first!([]), Access.*, Calendar.ISO.valid_time?/4, Logger.Formatter.prune/1)
gradual_mismatch 116 Spec-vs-code mismatches in stdlib (e.g., Agent.start/2,4 missing :ignore, Code.eval_quoted/3 options vs env_for_eval/1, DateTime.add/4 passing :day)

By comparison, Elixir's own make dialyze target (which is not gated in .github/workflows/ci.yml) currently exits with Error 2 and 465 warnings in 12.92 s (after building .elixir.plt). Several unused private parameters and tight @specs surfaced by Prisma were already merged upstream in elixir-lang/elixir#16151.


3. Regression Check: RabbitMQ Benchmark vs v0.2.0

With all of these precision and Elixir improvements enabled, we re-ran the RabbitMQ corpus from DISCREPANCIES.md using hyperfine --runs 5 --warmup 1:

Corpus / Mode v0.2.0 v0.2.8 / v0.2.9 Dialyzer (DISCREPANCIES.md)
rabbit (234 modules) — discrepancy 1.08 s / 295.5 MiB 1.71 s / 382.5 MiB 6.75 s / 405.1 MiB (3.9× faster)
rabbit (234 modules) — all 1.18 s / 287.8 MiB 1.86 s / 387.2 MiB —
440 modules (7 apps) — discrepancy 3.02 s / 536.0 MiB 4.40 s / 574.9 MiB 17.27 s / 1,456.7 MiB (3.9× faster)
440 modules (7 apps) — all 3.38 s / 546.1 MiB 5.37 s / 626.2 MiB —

The added polymorphic instantiation and cross-module wrapper specialization cost 45–59% relative to v0.2.0 while keeping Prisma ** 3.9× faster than Dialyzer and using ~2.5× less peak RAM** on the 440-module corpus.


4. Still on the Roadmap

Just as on November 11, three items from that original list remain open:

  1. Nominal Types (-nominal, EEP-69 / OTP 28+): Parsed as kind = nominal, still expanded structurally.
  2. Native Records (-record #name{}, EEP-79 / OTP 29+): Not yet extracted or checked.
  3. Value-Correlated Spec Overloads & Formalization: Dispatch correlations like rabbit_ff_controller:run_callback/5 and the standalone TYPESYSTEM.md / Lean 4 / Rocq formalization remain queued.