Train

A function 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. ,, and   are functions (which themselves can be tacit or not), and   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: The left tine of a fork can be an array:

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:

dzaima/APL and Kap allow, which is treated as. 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:

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  is actually parsed as the atop   and the attempted atop   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  and the atop can be enforced by using the explicit Atop operator instead of a 2-train;.

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,  and   are parsed as forks.

History
Function trains were first presented under the name "Phrasal forms" by Ken Iverson and Eugene McDonnell in a 1989 paper of the same name. They called the 2-function form a "hook" and the 3-function form a "fork" based on the shapes of the function call diagrams. On the return flight from APL88, Iverson found the idea when he woke up from a nap and then developed it together with McDonnell. The use of syntax for trains followed a long history of attempts to define train-like behavior in terms of operators.

Trains as defined in Phrasal Forms were included in the first versions of J in 1990. Roger Hui defined the case where the left tine is an array ("noun verb verb") in 2005. They were added to NARS2000 by 2009, and ngn/apl had partial support in 2013. K defined a different and simpler kind of function train based on linear evaluation.

The train definition used in most APL dialects changes the 2-train from a hook to an Atop in behavior. This change was made in Dyalog APL 14.0 in 2014, under the direction of Roger Hui, who had argued for it by 2006. It now appears in APL\iv, dzaima/APL, April, and BQN, and also matches the function composition featured in I in 2012.

Documentation

 * Dyalog
 * NARS2000
 * BQN
 * J: Hook, Fork

Text

 * APL Cultivation: Transcribing to and reading trains
 * gitonthescene: Forks: Spoon fed
 * gitonthescene: Training day

Videos

 * APLtrainer: How to read trains in Dyalog APL code
 * APLtrainer: Function trains in APL
 * Dyalog webinar: Train Spotting in Dyalog APL
 * Dyalog '13: Train Spotting in Version 14.0