%%apl
m←3 3⍴⍳9
m×1010 20 30 40 50 60 70 80 90
apl magics
j magicsiversonnb brings the Iverson languages to notebooks. It adds %%apl to Jupyter and IPython, which executes code in Dyalog APL, %%j to J, plus persistent APL and J kernels for LLM agents (including MCP). APL runs over the RIDE protocol, the same protocol Dyalog’s own IDE uses, so nothing needs to be loaded into your workspace, output looks exactly like a Dyalog session, and errors are detected reliably. J runs in-process through libj, the engine library that ships with every J install.
See core and j for step by step walkthroughs of how each is built.
Install the languages you plan to use; nothing starts until a magic first runs, so neither is required.
pip install jlanguage, or install it from jsoftware.com – iversonnb finds either.Then:
pip install iversonnb
Once that’s complete, you can install the magics to all IPython and Jupyter sessions automatically by running in your terminal:
iversonnb_install
An interpreter only starts the first time you run its magic, so having iversonnb installed everywhere costs nothing when you don’t use it.
apl magicsAfter first running an apl magic in a notebook, the APL language bar by Adám Brudzewsky is automatically added to the current page. (iversonnb bundles a modified copy of Adám’s lb.js; the file header lists the changes. The most visible ones: type a backtick twice in a row to enter triple backticks, get a ⋄ glyph with backtick-q, and use the ▲/▼ button beside the close button to choose whether the bar pushes the page down or overlays it, remembered per site.)
The cell magic (%%apl) runs APL code and displays the session’s own output, rendered in Adám’s SAX2 APL font, so results look exactly as they do in Dyalog:
Assignments are shy, just like in the Dyalog session, so the m← line above printed nothing. The line magic (%apl) instead evaluates one expression and returns it as a Python value:
Because the line magic returns a value, you can store it in a Python variable. Scalars come back as numbers or strings, vectors as lists, and higher-rank arrays as nested lists:
To suppress a cell’s output, end the last line with a ;:
⎕← displays a value explicitly, which is how you show something that would otherwise be shy:
To use numpy, just pass the result of %apl into np.array:
The fibonacci sequence:
Explanation:
1 1: Initial seed (first two Fibonacci numbers){⍵,+/¯2↑⍵}: Function that appends the sum of the last two elements⍣15: Apply the function 15 times⊢: Identity function, passes the initial argument (1 1) to the iterationPrime number sieve:
Explanation:
⍳50 generates integers 1 to 50⍵∘.|⍵ creates a 50x50 matrix of divisibility (1 if divisible, 0 if not)0= inverts the matrix (1 for non-divisible)+⌿ sums columns, counting non-divisors for each number2= checks if count equals 2 (prime property)⍵× multiplies result with original numbers, keeping primes~0 removes zero from the resultj magicsJ works the same way: %%j runs code in a persistent J session and displays its output verbatim, and %j expr returns an expression’s value as a Python object. The session keeps nouns and verbs across cells:
Under the hood there’s no separate process or protocol: iversonnb loads libj via ctypes, so completion, errors, and interrupts come straight from the engine’s C API. The J session class mirrors Apl – call it to run code, move values in both directions with [], lift verbs into Python callables with fn – see j for the full walkthrough.
The magics are a thin layer over the Apl class, which you can use directly in scripts, tests, and other tooling. Calling the session runs code and returns the output exactly as Dyalog formats it (or None if there’s no output); APL errors raise AplError:
Square brackets move values between Python and the workspace, in both directions, and take any expression:
fn lifts an APL function into a Python callable (one argument applies it monadically, two dyadically):
Sessions shut themselves down at process exit; use close, or a with Apl() as apl: block, to do it sooner:
iversonnb also ships persistent APL and J sessions for LLM agents, built on clikernel’s stream protocol. aplkernel and jkernel run the stdin/stdout workers directly, and aplkernel-mcp/jkernel-mcp wrap them as MCP servers with execute, restart, and interrupt tools. Workspace state persists across calls, and interrupt stops a long computation while keeping it; send )OFF (APL) or exit 0 (J) to stop a worker. If a session has to be replaced mid-conversation (a crash, or the APL incomplete-input bug below), the response starts with a NOTE saying workspace state was lost.
On startup each kernel runs ~/.config/iversonnb/startup.apl or startup.ijs (if it exists) in the session, and reports its source and output in the banner, which the MCP form forwards as the server’s instructions.
⎕ or ⍞ can’t work in a notebook, so it raises an error. The session survives.:If, wedges the Dyalog interpreter with no way back (Dyalog/ride#1401). iversonnb detects this and starts a fresh session, raising an error that says so (with reset set on it), but workspace state is lost when it happens.%apl transfers values with ⎕JSON, so it’s limited to arrays and scalars that JSON can represent, serializing to at most 32767 characters. For bigger data, write a file from APL instead.%j transfers values through the engine’s typed-array API, so it’s limited to booleans, integers, floats, and characters; boxed, extended, and rational values raise (display them with %%j instead).To start learning APL, follow the 17 video series run by Jeremy Howard, and have a look at the study notes. The ] user commands mentioned there, such as ]Help ≠, work in iversonnb too.
For J, start with Learning J and the J wiki, whose NuVoc page is the reference for every primitive.