jsonnet-0.5.0.0: CHANGELOG.md
# Changelog for haskell-jsonnet
## Unreleased changes
## [0.5.0.0] - 2026-12-01
Five years after the initial version, I sat down to fix object inheritance and
accidentally rewrote the evaluator. On the bright side, it closes the gap in
features/compliance with other implementations while getting decent performance,
making this the very first non-toy release of `haskell-jsonnet`.
### First-class `self` and `super`
`self` and `super` used to masquerade as lexical variables, which they aren't.
Jsonnet uses mixin-based object composition (open-recursive records where
accessing a field ties the knot), and trying to wire that up via `MonadFix`
(`mdo`) was convoluted. Previously, object composition beyond two layers was
outright broken. Because objects were plain `HashMap`s, every `+` eagerly merged
them—wasteful in a lazy language—and tied the knot too early, erasing their
tree-like structure. We now use a two-level object design: an immutable
recursive mixin tree (`Empty`, `Singleton`, `Base`, and `Mixin`, keeping `+`
`O(1)` and avoiding `HashMap`s for empty or single-field objects), while each
composed object pairs that tree with its own per-object cache indexed by layer.
```jsonnet
local base = { a: self.c };
local mid = { a: super.a + 1 };
local o1 = base + mid + { a: super.a + 100, c: 5 }; // a = 106
local o2 = base + mid + { c: 10 }; // a = 11
```
Three constraints drive this structure:
* **Per-object caching:** `base` and `mid` are shared between `o1` and `o2`, but
late-bound `self.c` resolves differently in each. Evaluated fields can only be
memoized once `self` is fixed to a specific final object—never on the
mixin layers themselves.
* **Layer-indexed lookup:** Inside `o1`, field `a` has a different value at
every layer (`5`, `6`, `106`). Indexing the per-object cache by layer
memoizes each layer's view independently, avoiding repeated re-evaluation of
lower layers on `super` accesses.
* **Defining-layer `super`:** Evaluating `o2.a` falls through to `mid`'s
definition. Inside `mid`, `self` remains `o2`, while `super` must step
strictly below `mid` (to `base`) rather than below the top layer.
Side note: unlike `this` in languages like JavaScript, Jsonnet's `self` is not
resolved at call sites: once late-bound to a composed object, it is captured in
the environment of any closures produced by its fields (so detaching a method
keeps its original `self`).
### Performance and allocations
Late cost-centre profiling (tracking time and allocations by call stack after
optimizations), RTS GC stats, and GHC Core dumps (`-ddump-simpl`), alongside
benchmarks—with Gemini/Claude helping read the output and suggest fixes—showed
where time and memory were actually going. That led to a few main changes:
* **Replacing `locally-nameless` with 2D de Bruijn indices:** The biggest win.
`locally-nameless` was a poor fit for a dynamically typed, environment-based
evaluator since closures and thunks already prevent variable capture. On top
of that, I was not even using correctly the library.
* **Reimplementing allocation-heavy paths:** Profiles and Core dumps show
unnecessary allocations, and here is where help from Gemini came in handy.
Other improvements include selective source spans (source locations are only
attached to expressions that can actually fail), fast paths and
short-circuiting for certain primitive operations, e.g. for `+` on empty
arrays and objects, avoid allocating thunks when we can tell up front
something is in WHNF, but here one must be careful to preserve lazy errors:
when `self` is in scope you can skip the thunk; when it isn't, it still falls
back to allocating a thunk so that if inside an unevaluated branch doesn't
fire eagerly.
* **Tightening strictness:** Enabling `StrictData` and making `Env`/`EvalState`
updates strict keeps closures and cached object fields from retaining caller
environments.
### Modules and cycle detection
Modules (imports) previously worked by naively pasting the imported AST inline,
Imports are now properly evaluated and cached, with full support including
cyclic imports. Additionally, thunks are now guarded with cycle detection,
previously the interpreter would just loop indefinitely.
### Miscellanea
A non-exhaustive list of other changes:
- Added several new errors: `UnboundSelf`, `UnboundSuper`, `NoSuperClass`,
`UnresolvedImport`, `InfiniteRecursion`, `ParamAlreadyBound`, and
`TooFewArgs`. Of note is how `super` behaves depending on context:
```console
λ cabal run hs-jsonnet -- -e 'super.x'
Runtime error: can't use super outside of an object.
:1:7-8
λ cabal run hs-jsonnet -- -e '({ x: super.x }).x'
Runtime error: attempt to use super when there is no super class.
:1:13-15
λ cabal run hs-jsonnet -- -e '({ x: "x" in super }).x'
false
```
- Fixed a bug where underlying errors escaped backtrace propagation:
```console
λ cabal run hs-jsonnet -- -e "local json(a) = std.parseJson('{\"%s\": \"bar' % a); json('foo')"
Runtime error: AesonException "Unexpected end-of-input while parsing string literal"
:1:17-30 function <json>
:1:51-55
```
- Fixed parser precedence for implicit object composition (`e { ... }`) so it
chains left-to-right with function calls and field lookups, allowing
expressions like `std.extVar('var2') { x+: 2 }.x` to parse.
- Fixed value ordering failing on arrays: comparisons were previously
restricted to numbers instead of comparing arrays lexicographically.
- Cleaned up module boundaries and internal ergonomics (after plenty of
bikeshedding): `Value` was decoupled from `Core` by _refunctionalizing_
`VThunk` (once the old object `+` implementation was gone, it no longer had
to pattern-match on `VThunk core env` just to inject `self` and `super` into
`env` after the fact). `HasValue` was also split into `Inj` and `Proj` to
separate converting to and from Jsonnet values, and optics were added—lenses
for reading and updating evaluation state, and prisms for inspecting and
constructing `Value` variants.