Train: Difference between revisions

Jump to navigation Jump to search

A train is a compound function made up of a series of functions. It's written as an isolated expression (surrounded by parentheses or named) ending in a function. Defined by Ken Iverson and Eugene McDonnell in 1988 and added to Dyalog APL in 2014, trains are considered important for tacit programming and a characteristic of modern APL.

Definition

Below, `⍺` and `⍵` refer to the arguments of the train. `f`, `g`, and `h` are functions (which themselves can be tacit or not), and `A` is an array. The arguments are processed by the following rules:

3-trains

A 3-train is a fork, so denoted because its structure resembles a three-tines fork, or a three-pronged pitchfork. The two outer functions are applied first, and their results are used as arguments to the middle function:

 ``` (f g h) ⍵ ``` ${\displaystyle \Leftrightarrow }$ ```( f ⍵) g ( h ⍵) ``` ```⍺ (f g h) ⍵ ``` ${\displaystyle \Leftrightarrow }$ ```(⍺ f ⍵) g (⍺ h ⍵) ```

The left tine of a fork can be an array:

 ``` (A g h) ⍵ ``` ${\displaystyle \Leftrightarrow }$ ```A g ( h ⍵) ``` ```⍺ (A g h) ⍵ ``` ${\displaystyle \Leftrightarrow }$ ```A g (⍺ h ⍵) ```

2-trains

Most dialects define a 2-train is an atop, equivalent to the function derived using the Atop operator. The left function is applied monadically on the result of the right function:

 ``` (g h) ⍵ ``` ${\displaystyle \Leftrightarrow }$ ```g ( h ⍵) ``` ```⍺ (g h) ⍵ ``` ${\displaystyle \Leftrightarrow }$ ```g (⍺ h ⍵) ```

Only dzaima/APL allows `(A h)`, which it treats as `A∘h`.[1] See Bind.

J instead defines the 2-train as a hook, equivalent to the function derived using the Withe operator. The left function is always applied dyadically, taking as right argument, the result of applying the right function on the right argument. If there is no left argument, the sole argument is used also as left argument:

 ``` (g h) ⍵ ``` ${\displaystyle \Leftrightarrow }$ ```⍵ g (h ⍵) ``` ```⍺ (g h) ⍵ ``` ${\displaystyle \Leftrightarrow }$ ```⍺ g (h ⍵) ```

Problems caused by function-operator overloading

Trains that use a hybrid function-operator in its function role can run into the problems with the hybrid being parsed as a monadic operator instead of as a function. This happens when a function appears to the immediate left of the hybrid, causing this function to be bound as the hybrid's operand — the hybrid taking on an operator role — rather than supplying a left argument or post-processing the result.

For example, the attempted fork `f/h` is actually parsed as the atop `(f/)h` and the attempted atop `f/` is actually parsed as a Windowed Reduction. There are multiple ways to mitigate this issue. For example, the fork can be enforced using the Atop operator by applying identity to the hybrid's result as `f⊢⍤/h` and the atop can be enforced by using the explicit Atop operator instead of a 2-train; `f⍤/`.

No problem presents when left argument is supplied as an array (literal or by name reference) and when the hybrid is the leftmost token. For example, `1 0 1/⌽` and `/,⊃` are parsed as forks.