---
title: Dyalog sessions
description: Run Dyalog APL through RIDE for reference checks.
skip_showdoc: true
---
<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

Import [`Apl`](dyalog.md#apl) and [`AplError`](dyalog.md#aplerror) from `basedpl.dyalog`. Sessions return Dyalog output or JSON-converted Python values, not BPL arrays. Importing this module does not start Dyalog or register notebook magics.

Install Dyalog separately. The examples use a local interpreter and run with `nbdev-test --flags dyalog`. The default test run skips them.

```python
from fastcore.test import *
```

## Connecting

[`find_dyalog`](dyalog.md#find_dyalog) checks the command path and standard macOS/Linux installation locations. Pass `dyalog=` to [`Apl`](dyalog.md#apl) to choose another executable. [`Apl`](dyalog.md#apl) uses `timeout` only during the startup handshake, not while evaluating code.

RIDE uses length-prefixed messages over a local socket. [`ride_run`](dyalog.md#ride_run) collects session output until Dyalog is ready for another expression. [`Apl.run`](dyalog.md#apl.run) handles interactive and incomplete-input prompts.

## Evaluating APL

`run` returns session output as text. Assignments persist across calls:

```python
dyalog = Apl()
out = dyalog.run('x←1 2 3\n+/x')
test_eq(out.strip(), '6')
out
```

```output
'6\n'
```

Calling a session returns [`AplOut`](magics.md#aplout), the same text with notebook display support. A silent assignment returns `None`:

```python
test_is(dyalog('quiet←7'), None)
dyalog('2×x')
```

## Python values

`pyval` uses Dyalog's JSON conversion. Scalars become Python values and numeric arrays become lists. `basedpl.reference` calls `pyval` for its reference checks:

```python
test_eq(dyalog.pyval('+/x'), 6)
matrix = dyalog.pyval('2 3⍴⍳6')
test_eq(matrix, [[1,2,3], [4,5,6]])
matrix
```

```output
[[1, 2, 3], [4, 5, 6]]
```

Use square brackets to evaluate an APL expression or assign a JSON-compatible Python value. Single quotes in strings are escaped for APL:

```python
dyalog['values'] = [3,1,4]
dyalog['message'] = "can't"
test_eq(dyalog['message'], "can't")
test_eq(dyalog['values'], [3,1,4])
dyalog['values']
```

```output
[3, 1, 4]
```

The callable returned by `fn` converts Python arguments through JSON and evaluates the APL function expression on each call. Passing one argument sets `⍵`, and passing two sets `⍺` and `⍵`:

```python
mean = dyalog.fn('{(+/⍵)÷≢⍵}')
test_eq(mean([1,2,3]), 2)
add = dyalog.fn('+')
test_eq(add([1,2,3], 10), [11,12,13])
add([1,2,3], 10)
```

```output
[11, 12, 13]
```

## Errors and lifetime

`run` raises this module's [`AplError`](dyalog.md#aplerror) for a Dyalog error, not `BplError`. Assignments that run before an error stay in the session:

```python
with expect_fail(AplError, contains='DOMAIN ERROR'): dyalog.run('saved←42 ⋄ 1÷0')
test_eq(dyalog['saved'], 42)
dyalog['saved']
```

```output
42
```

[`Apl.run`](dyalog.md#apl.run) rejects interactive [`⎕`](https://bpl.fast.ai//glyphs/quad.html) and `⍞` input with [`AplError`](dyalog.md#aplerror). When Dyalog shows an incomplete-input prompt, [`Apl.run`](dyalog.md#apl.run) restarts the interpreter, losing the workspace, and raises [`AplError`](dyalog.md#aplerror) with `reset=True`.

Call `close()` when finished, or use [`Apl`](dyalog.md#apl) as a context manager to close the session automatically:

```python
dyalog.close()
with Apl() as reference: total = reference.pyval('+/⍳10')
test_eq(total, 55)
total
```

```output
55
```
