Skip to main content

Using BPL notebooks

BPL cells, Python magics, glyph entry and help.

BPL kernel

Install with pip install basedpl, then select BasedPL in Jupyter’s kernel menu. Write BPL directly in each code cell. Names persist between cells.

v←ι5
+/v

Press Shift-Tab to inspect a name or glyph. ]help name shows help. ]help name -source shows the source. See Getting started for an executable BPL tutorial.

Python magics

create_magic() registers %bpl and %%bpl in a Python notebook. %load_ext basedpl.notebooks is equivalent. Run bpl-install-magic once to load the extension automatically in IPython and Jupyter.

The magics and Python code that imports bpl share one persistent workspace, basedpl.bpl. %bpl ]clear removes every name from the shared workspace.

from basedpl.notebooks import create_magic
magic = create_magic()

Hold the left Alt or Option key and press a key from the glyph reference: Alt-h types ←, Alt-m types × and Alt-u types ÷. Some glyphs take a second key. For example, Alt-Minus then a types ⍶, and Alt-6 then a digit types a superscript. Hold Alt or Option and tap the same dead key twice to type its character: Option-o twice types ○, and Option-backslash twice types ⍭. The sequence ends after the second tap. On a UK Mac keyboard, Option-Shift-3 types #, and plain Shift-3 still types £. Right Option keeps its native behavior. Alt shortcuts work in BPL input, including strings and comments. The glyph map is the same as the BPL REPL’s.

In the browser, Alt shortcuts use US physical key positions. The native macOS layouts support US, UK, German QWERTZ, French AZERTY and Spanish ISO keyboards. They preserve local typing and move conflicting BPL shortcuts to Option-Shift, then Control-Option or Control-Option-Shift.

To enter a glyph by name, type a backtick followed by its name. For example, `io then Tab inserts ι, and 2`times3 becomes 2×3. As you type, suggestions appear beside the cursor, with each glyph’s key shortcut in the REPL’s notation. In that notation, h means Alt-h, and o c means Alt-o then c. Matches are ranked by exact name, then prefix, then abbreviation. Click a suggestion or keep typing to resolve an ambiguous name. Names and aliases come from BPL’s REPL catalogue.

Backtick completion works in %%bpl and %%apl cells, on %bpl lines such as x = %bpl ..., and in Monaco editors configured for BPL. Ordinary Python and Markdown input is unchanged. Backtick sequences in strings, comments and pasted text are not expanded. Press Tab to expand a backtick name that is already in the text. Enter accepts a unique match, then performs the notebook’s normal newline or execution action. Press Escape or move the cursor to cancel automatic expansion.

Running a bpl magic for the first time also adds a clickable symbol bar, based on Adám Brudzewsky’s APL language bar. Hover over a glyph to see its names and shortcut. The ▲/▼ button switches the symbol bar between pushing the page down and overlaying it. This choice is remembered per site.

The %%bpl cell magic displays BPL output in Adám’s SAX2 APL font:

%%bpl
m←3 3ρι9
m×10
┌→───────┐
↓ 0 10 20│
│30 40 50│
│60 70 80│
└~───────┘

Assignments are shy: the m← line printed nothing. The %bpl line magic returns a basedpl.Array. Use .np to copy its values to NumPy:

v = %bpl 3×ι4
v.np
array([0., 3., 6., 9.])
text = %bpl "APL in Python"
text.py
'APL in Python'

.py converts numeric atoms to Python numbers and character vectors to strings. With .py, unkeyed numeric arrays become NumPy arrays, keyed vectors become dictionaries, and higher-rank keyed arrays become pandas DataFrames. Numeric scalars (rank-0 arrays) become zero-dimensional NumPy arrays. .np copies values to NumPy without labels:

z = %bpl m
z.np
array([[0., 1., 2.],
       [3., 4., 5.],
       [6., 7., 8.]])

To suppress a cell’s output, end the last line with a ;:

%%bpl
m×10;

⎕← displays a value explicitly, which is how you show something that would otherwise be shy:

%%bpl
v←2×ι5
⎕←v
0 2 4 6 8

Convert to NumPy to use its methods:

a = %bpl m
a.np.sum(axis=0)
array([ 9., 12., 15.])

Names and help

Press Tab to complete user and system names in BPL input. Use ]help name to show help from comments and ]help name -source to show BPL source:

%%bpl
double←{⍝ Double the argument
2×ω}
%bpl ]help double

Calls: f Y or X f Y

Double the argument

%bpl ]help double -source
{⍝ Double the argument
2×ω}

Python function objects support f? and f??. See Python and Help and introspection.

Errors and interruption

%bpl and %%bpl raise basedpl.BplError for a BPL error. The magics display output produced before the error. Incomplete input raises a syntax error without resetting the workspace.

Interrupt a calculation with the notebook’s stop button. Set a per-evaluation deadline with bpl.timeout = seconds. Cancelling an evaluation keeps the workspace and any assignments that have already completed. Native-library calls and individual BigInt operations can delay cancellation.

BPL is an APL-derived array language, borrowing from J and BQN. See the glyph reference for glyphs, System functions for system functions, and Evaluation for how functions apply to arrays.

Other interpreters

Dyalog sessions run an independent reference interpreter.

The magics implementation documents display, completion and workspace handling.