notes · · 5 min · updated 2026-10-27

Arrow Under the Microscope: What 67 Remote AST Tools Found Inside Kotlin's Functional Core

We evaluated prod-code AST tools against arrow-kt/arrow: 72,889 lines of Kotlin across 757 files, 38-module Gradle architecture, structural search for the Raise DSL, and cluster-verified AST refactoring.

On this page · 4 sections
  1. The 38-Module Hierarchy and Low-Level Atomic Cycles
  2. Functional Pipeline Clones: Either Tests and High-Arity Operators
  3. Structural Metavariables: Auditing the Raise DSL Across the Workspace
  4. Polyglot Kotlin AST Refactoring: an Extraction That Did Not Hold Up

First published on 2026-09-30. The measurements reported in this article are historical and have not been re-run for this revision.

Functional programming in Kotlin reaches its architectural maturity in Arrow (arrow-kt/arrow). Offering typed error handling through Either, nullability modeling with Option, composable concurrency primitives, optics, and the modern Raise domain-specific language, Arrow equips Kotlin developers with category-theoretic abstractions adapted to idiomatic JVM and multiplatform execution.

Unlike standard enterprise application codebases dominated by inheritance hierarchies and runtime dependency injection, a pure functional library stresses compiler semantics in unique ways: higher-arity function composition, context receivers, inline value classes, and generic type variance.

Across its modular Gradle structure, Arrow contains 72,889 lines of Kotlin across 757 source files:

$ git ls-files '*.kt' | wc -l
757
$ git ls-files | wc -l
985
$ git ls-files '*.kt' | xargs wc -l | grep -v 'total$' | awk '{s+=$1} END {print s}'
72889

The article reports deploying prod-code’s suite of 67 AST and code intelligence tools against Arrow HEAD on remote cluster nodes; the commit was not recorded, and the sections below show selected operations, not a per-tool record. Parsing, dependency graph construction, clone comparison and language server work ran remotely while we examined the internal anatomy of Kotlin’s functional standard; no local CPU measurement is shown.

The 38-Module Hierarchy and Low-Level Atomic Cycles

Multiplatform Kotlin libraries frequently divide codebases into fine-grained subprojects to isolate platform-specific native stubs from shared common logic. In Arrow, the build is distributed across subprojects defined via Gradle Kotlin DSL configuration scripts (settings.gradle.kts); the dependency report below discovered 38 modules in the unrecorded HEAD checkout, and the article does not say which settings includes or included builds that count covers.

Executing prod-code dependencies across the repository reported the inter-module dependency topology it discovered:

$ prod-code dependencies
discovered 38 modules across Gradle workspace:
  • arrow-core, arrow-fx-coroutines, arrow-atomic, arrow-optics
  • arrow-platform, arrow-annotations, arrow-autoclose, arrow-exception-utils
  • arrow-resilience, arrow-collectors, arrow-core-high-arity
  ... (27 integration, runtime, and sample modules)

inter-module dependencies: 86 edges
circular dependencies: 5 circular dependency paths detected:
  1. arrow-atomic -> arrow-fx-coroutines -> arrow-atomic
  2. arrow-atomic -> arrow-fx-coroutines -> arrow-autoclose -> arrow-atomic
  3. arrow-atomic -> arrow-fx-coroutines -> arrow-autoclose -> arrow-exception-utils -> arrow-core -> arrow-atomic
  4. arrow-exception-utils -> arrow-core -> arrow-exception-utils
  5. arrow-fx-coroutines -> arrow-autoclose -> arrow-exception-utils -> arrow-core -> arrow-fx-coroutines

top coupled modules (by afferent coupling Ca):
  • arrow-core: Ca=18, Ce=5, Instability I=0.22
  • arrow-fx-coroutines: Ca=9, Ce=5, Instability I=0.36
  • arrow-atomic: Ca=7, Ce=2, Instability I=0.22
  • arrow-platform: Ca=7, Ce=0, Instability I=0.00
  • arrow-annotations: Ca=6, Ce=0, Instability I=0.00
  • arrow-optics: Ca=6, Ce=1, Instability I=0.14

The dependency report listed 5 circular dependency paths linking low-level concurrency and utility modules. It does not say which kind of Gradle dependency each edge is, and that decides what a cycle means: a path that closes through a test dependency is not a cycle the main code needs at run time. arrow-atomic provides cross-platform atomic primitives, arrow-autoclose resource management and arrow-exception-utils exception filtering; which of the edges between them are main-code dependencies cannot be read from the report.

Outside these five paths, functional modules like arrow-optics (Cₐ=6, Cₑ=1) and metadata modules like arrow-platform (Cₐ=7, Cₑ=0, I=0.00) appear in no reported cycle.

Correction, 2026-10-27. As published on 2026-09-30, this section said the five cycles stem from foundational runtime requirements and explained them by the lack of a cross-platform atomic reference in older Kotlin compiler targets. The report does not distinguish main from test dependencies. In the build files of the comparison revision e28d8dd5, which is not shown to be the revision the article ran against, the reverse edges are commonTest dependencies: arrow-atomic on arrow-fx-coroutines, arrow-core on arrow-fx-coroutines and arrow-exception-utils on arrow-core. The runtime-requirement and old-compiler explanations are withdrawn, as is “strictly decoupled”; the five paths stand as the report lists them, their dependency kind not established.

Functional Pipeline Clones: Either Tests and High-Arity Operators

Functional programming models computation as transformations over immutable data. However, in languages lacking native variadic generics, implementing tuple and function composition up to 22 parameters often necessitates repetitive code generation. We executed prod-code duplicates to measure token-level replication across the repository:

$ prod-code duplicates
scanned 804 files (74,888 lines of code)
found 20 clone groups across 54 files (2.8% codebase duplication)

top duplication clusters:
  • Clone Group #2545: 6 lines | 27 occurrences (Type-2 Parameterized)
    └─ parameterized Either monadic laws and transformation tests
  • Clone Group #4510: 6 lines | 15 occurrences
    └─ MapK persistent collection transformation assertions
  • Clone Group #1554: 6 lines | 14 occurrences
    └─ ParZipOrAccumulate parallel coroutine accumulation ladders
  • Clone Group #3117: 6 lines | 14 occurrences
    └─ Sequence.kt high-arity iterator stepping sequences

The detector reports a low duplication rate of 2.8% over the 804 files it scanned, and the excerpt shows 4 of its 20 groups. Crucially, the clone patterns reveal the architectural challenges of functional programming on the JVM. Clone Group #3117 accounts for 14 occurrences of repetitive iterator advancing sequences inside Sequence.kt (iterator3.next(), iterator4.next(), iterator5.next()). In arrow-core-high-arity, supporting functions of arity 2 through 22 requires unrolling identical stepping loops.

Similarly, Clone Group #1554 captures parallel concurrency accumulation pipelines in ParZipOrAccumulate.kt. Rather than representing sloppy copy-paste engineering, these clones read as artifacts of arity expansion that provide type-safe multi-argument zip operations. Whether they avoid boxing is a question about the compiled code, which a clone report does not show.

Structural Metavariables: Auditing the Raise DSL Across the Workspace

A key innovation in modern Arrow is the Raise DSL, which replaces monad transformers with delimited continuations and structured error accumulation. Central to this paradigm is the ensure construct: if a boolean condition evaluates to false, computation raises a typed error and short-circuits.

We utilized prod-code structural-search to query the workspace for occurrences of ensure($A) { $B }, binding metavariables $A and $B to arbitrary predicates and lazy error expressions:

$ prod-code structural-search 'ensure($A) { $B }'
scanned 804 files in 273.96 ms
found 20 matches across 14 files:
  • arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/EitherTest.kt:1037:20
    ensure(it > 0) { "negative" }
    └─ [$A = it > 0, $B = "negative"]
  • arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/usage/Context.kt:10:3
    ensure(x > 0) { "x is not positive" }
    └─ [$A = x > 0, $B = "x is not positive"]
  • arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-raise-01.kt:17:3
    ensure(path.isNotEmpty()) { EmptyPath }
    └─ [$A = path.isNotEmpty(), $B = EmptyPath]

Next, we searched for standard Kotlin argument preconditions using require($A) { $B } (20 matches across 10 files in 191.74 ms) and simulated an automated migration to state assertions using prod-code codemod:

$ prod-code codemod 'require($A) { $B } ==>> check($A) { $B }'
`require($A) { $B } ==>> check($A) { $B }`
58 changed line(s) in 10 file(s)

sample diff (Bracket.kt):
-      require(failure !is CancellationException) { "Failure must not wrap a CancellationException" }
+      check(failure !is CancellationException) { "Failure must not wrap a CancellationException" }

nothing was written; pass `apply: true` to make these edits

In under 300 milliseconds per query, structural pattern matching found the ensure and require sites the excerpts show; both queries report exactly 20 matches, and the article does not say whether that is a result cap. The codemod preview is a syntax demonstration, not a safe migration: a false require throws IllegalArgumentException and a false check throws IllegalStateException, so applying it would change the exception callers see.

Correction, 2026-10-27. As published on 2026-09-30, this paragraph said the queries isolated validation boundaries and confirmed that the Raise DSL operates cleanly alongside Kotlin’s preconditions. The excerpts show matched sites and a preview diff that was not written; per the Kotlin standard library documentation the rewrite changes the exception type from IllegalArgumentException to IllegalStateException. The safe-boundary and confirmation claims are withdrawn.

Polyglot Kotlin AST Refactoring: an Extraction That Did Not Hold Up

The true test of automated code intelligence lies in performing surgical refactorings and asking a live compiler language server about them before changes reach the disk. The excerpt below, as published, does not establish that check: the helper it shows is invalid, and whether the check ran, and with what result, cannot be told from it (see the correction after the excerpt). In arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Option.kt, the companion object provides nullable conversion via fromNullable:

public companion object {
    @JvmStatic
    public fun <A> fromNullable(a: A?): Option<A> = if (a != null) Some(a) else None
}

We targeted the conditional expression if (a != null) Some(a) else None on line 306 and invoked prod-code extract-function to extract the nullable wrapping logic into a dedicated helper:

$ prod-code extract-function \
    arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Option.kt 306 53 \
    --to 306:85 \
    --name wrapNullable

`fn wrapNullable` extracted (arrow-libs/core/.../Option.kt);
the selection now reads `wrapNullable(a)`

--- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Option.kt
+++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Option.kt
@@ -300,2 +300,6 @@
  */
+private fun wrapNullable(a: A?) {
+    if (a != null) Some(a) else None
+}
+
 public sealed class Option<out A> {
@@ -305,3 +309,3 @@
     @JvmStatic
-    public fun <A> fromNullable(a: A?): Option<A> = if (a != null) Some(a) else None
+    public fun <A> fromNullable(a: A?): Option<A> = wrapNullable(a)

the analyzer accepts the result: 0 errors
nothing was written; pass `apply: true` to make this edit

Correction, 2026-10-27. The helper shown above, as published on 2026-09-30, is not valid Kotlin: wrapNullable is a top-level function that uses a type parameter A it never declares, and its block body has no return, so it returns Unit where fromNullable needs an Option<A>. The line the analyzer accepts the result: 0 errors therefore cannot be a verification of this patch. The excerpt is kept as it was published; what produced that line was not established in the review, and no claim of compiler verification is made for it.

As published, this paragraph said the engine preserved the generic parameter context and that the language server verified the modification; both claims are withdrawn. The call-site edit is as the published excerpt shows it; whether this tool generated it, and from what run, is not established. The paragraph as published went on to say that the remote kotlin-language-server running on an isolated cluster node verified the modification in a shadow workspace and confirmed 0 compilation diagnostics; that is the withdrawn claim, kept here only as what was published, not as a result.

Functional software derives its correctness from composable type invariants; verify dependency cycles in foundational runtime modules and let compiler language servers validate every AST transformation.

Cite this article
Citation
Alexander Panasenko (2026-09-30). Arrow Under the Microscope: What 67 Remote AST Tools Found Inside Kotlin's Functional Core. https://prod.codes/blog/arrow-under-the-microscope-67-ast-tools/