4,494
edits
(→Trains: hooks) |
|||
(17 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
Tacit | '''Tacit programming''', also called '''[[wikipedia:Tacit_programming|point-free style]]''', refers to usage of tacit [[function]]s that are defined in terms of implicit [[argument]]s. This is in contrast to the explicit use of arguments in [[dfn]]s (<source inline lang=apl>⍺ ⍵</source>) and [[tradfn]]s (which have named arguments). Some APL dialects allow to combine functions into [[#trains|trains]] following a small set of rules. This allows creating complex [[derived function]]s without specifying any arguments explicitly. | ||
Dialects which implement trains include [[Dyalog APL]], [[dzaima/APL]], [[ngn/apl]] and [[NARS2000]]. | |||
== Primitives == | == Primitives == | ||
Line 34: | Line 36: | ||
A train is a series of functions in isolation. An isolated function is either surrounded by parentheses or named. Below, <source lang=apl inline>⍺</source> and <source lang=apl inline>⍵</source> refer to the arguments of the train. <source lang=apl inline>f</source>, <source lang=apl inline>g</source>, and <source lang=apl inline>h</source> are functions (which themselves can be tacit or not), and <source lang=apl inline>A</source> is an array. The arguments are processed by the following rules: | A train is a series of functions in isolation. An isolated function is either surrounded by parentheses or named. Below, <source lang=apl inline>⍺</source> and <source lang=apl inline>⍵</source> refer to the arguments of the train. <source lang=apl inline>f</source>, <source lang=apl inline>g</source>, and <source lang=apl inline>h</source> are functions (which themselves can be tacit or not), and <source lang=apl inline>A</source> 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: | |||
A 3-train is a ''fork'': | |||
{| | {| | ||
|<source lang=apl> (f g h) ⍵</source>|| {{←→}} ||<source lang=apl>( f ⍵) g ( h ⍵)</source> | |<source lang=apl> (f g h) ⍵</source>|| {{←→}} ||<source lang=apl>( f ⍵) g ( h ⍵)</source> | ||
Line 53: | Line 50: | ||
|} | |} | ||
Only [[dzaima/APL]] allows <source lang=apl inline>(A h)</source>, which it treats as <source lang=apl inline>A∘h</source>.<ref>dzaima/APL: [https://github.com/dzaima/APL/blob/ceea05e25687988ed0980a4abf4b9249b736543f/docs/differences.txt#L19 Differences from Dyalog APL]. Retrieved 09 Jan 2020.</ref> | === 2-trains === | ||
Most dialects define a 2-train is an ''atop'', equivalent to the function derived using the [[Atop (operator)|Atop]] operator. The left function is applied [[monadic function|monadically]] on the result of the right function: | |||
{| | |||
|<source lang=apl> (g h) ⍵</source>|| {{←→}} ||<source lang=apl>g ( h ⍵)</source> | |||
|- | |||
|<source lang=apl>⍺ (g h) ⍵</source>|| {{←→}} ||<source lang=apl>g (⍺ h ⍵)</source> | |||
|} | |||
Only [[dzaima/APL]] allows <source lang=apl inline>(A h)</source>, which it treats as <source lang=apl inline>A∘h</source>.<ref>dzaima/APL: [https://github.com/dzaima/APL/blob/ceea05e25687988ed0980a4abf4b9249b736543f/docs/differences.txt#L19 Differences from Dyalog APL]. Retrieved 09 Jan 2020.</ref> 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 [[dyadic function|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: | |||
{| | |||
|<source lang=apl> (g h) ⍵</source>|| {{←→}} ||<source lang=apl>⍵ g (h ⍵)</source> | |||
|- | |||
|<source lang=apl>⍺ (g h) ⍵</source>|| {{←→}} ||<source lang=apl>⍺ g (h ⍵)</source> | |||
|} | |||
== Debugging == | |||
In [[Dyalog APL]], analysis of trains is assisted by a [[user command]] <source lang=apl inline>]Boxing on</source>. This is achieved by executing the command <source lang=apl inline>]Boxing on</source> and then entering a train without any parameters. A structure of the train will be displayed. | |||
For example, the "accursed train" from the section below can be analysed like this: | |||
<source lang=apl> | |||
]Boxing on | |||
Was OFF | |||
((+.×⍨⊢~∘.×⍨)1↓⍳) ⍝ the train to be analysed | |||
┌───────────────────────────────┬───────┐ | |||
│┌───────────┬─────────────────┐│┌─┬─┬─┐│ | |||
││┌───────┬─┐│┌─┬─┬───────────┐│││1│↓│⍳││ | |||
│││┌─┬─┬─┐│⍨│││⊢│~│┌───────┬─┐│││└─┴─┴─┘│ | |||
││││+│.│×││ │││ │ ││┌─┬─┬─┐│⍨││││ │ | |||
│││└─┴─┴─┘│ │││ │ │││∘│.│×││ ││││ │ | |||
││└───────┴─┘││ │ ││└─┴─┴─┘│ ││││ │ | |||
││ ││ │ │└───────┴─┘│││ │ | |||
││ │└─┴─┴───────────┘││ │ | |||
│└───────────┴─────────────────┘│ │ | |||
└───────────────────────────────┴───────┘ | |||
</source> | |||
Alternatively, a train can be represented in form of a tree: | |||
<source lang=apl> | |||
]Boxing on -trains=tree | |||
Was ON -trains=box | |||
((+.×⍨⊢~∘.×⍨)1↓⍳) ⍝ the train to be analysed | |||
┌───┴───┐ | |||
┌─┴─┐ ┌─┼─┐ | |||
⍨ ┌─┼─┐ 1 ↓ ⍳ | |||
┌─┘ ⊢ ~ ⍨ | |||
. ┌─┘ | |||
┌┴┐ . | |||
+ × ┌┴┐ | |||
∘ × | |||
</source> | |||
Or fully parenthesised: | |||
<source lang=apl> | |||
]Boxing on -trains=parens | |||
Was OFF -trains=box | |||
((+.×⍨⊢~∘.×⍨)1↓⍳) ⍝ the train to be analysed | |||
(((+.×)⍨)(⊢~((∘.×)⍨)))(1↓⍳) | |||
</source> | |||
== Examples == | == Examples == | ||
Line 75: | Line 130: | ||
=== Fractions === | === Fractions === | ||
We can convert decimal numbers to fractions. For example, we can convert <math>2.625</math> to the improper fraction <math>21 | We can convert decimal numbers to fractions. For example, we can convert <math>2.625</math> to the improper fraction <math>\tfrac{21}{8}</math> with | ||
<source lang=apl> | <source lang=apl> | ||
(1∧⊢,÷)2.625 | (1∧⊢,÷)2.625 | ||
21 8 | 21 8 | ||
</source> | </source> | ||
Alternatively, we can convert it to the mixed fraction <math>2{5 | Alternatively, we can convert it to the mixed fraction <math>2\tfrac{5}{8}</math> with a mixed fraction: | ||
<source lang=apl> | <source lang=apl> | ||
(1∧0 1∘⊤,÷)2.625 | (1∧0 1∘⊤,÷)2.625 | ||
Line 121: | Line 175: | ||
===The Number of the Beast=== | ===The Number of the Beast=== | ||
The following expression for computing the [[wikipedia:666 (number)| | The following expression for computing the [[wikipedia:666 (number)|number of the Beast]] (and of [[I.P. Sharp]]'s APL-based email system, [[666 BOX]]) nicely illustrates how to read a train. | ||
<source lang=apl> | <source lang=apl> | ||
((+.×⍨⊢~∘.×⍨)1↓⍳)17 ⍝ Accursed train | ((+.×⍨⊢~∘.×⍨)1↓⍳)17 ⍝ Accursed train | ||
Line 151: | Line 205: | ||
666 ⍝ ⍎ executes original Accursed train | 666 ⍝ ⍎ executes original Accursed train | ||
</source> | </source> | ||
== See also == | |||
* [[Function composition]] | |||
== External links == | == External links == | ||
== Tutorials == | === Tutorials === | ||
* Dyalog: [ | * Dyalog: [https://help.dyalog.com/16.0/Content/RelNotes14.0/Function%20Trains.htm version 14.0 release notes] | ||
* [[Dfns workspace]]: [https://dfns.dyalog.com/n_tacit.htm Translation of <nowiki>[dfns]</nowiki> into tacit form] | |||
* [[APL Cultivation]]: [https://chat.stackexchange.com/rooms/52405/conversation/lesson-23-transcribing-to-and-reading-trains Transcribing to and reading trains] | * [[APL Cultivation]]: [https://chat.stackexchange.com/rooms/52405/conversation/lesson-23-transcribing-to-and-reading-trains Transcribing to and reading trains] | ||
* APLtrainer: [https://www.youtube.com/watch?v=kt4lMZbn-so How to read trains in Dyalog APL code] (video) | * [[APLtrainer]]: [https://www.youtube.com/watch?v=kt4lMZbn-so How to read trains in Dyalog APL code] (video) | ||
* APLtrainer: [https://www.youtube.com/watch?v=A2LqqBosvY0 Function trains in APL] (video) | * [[APLtrainer]]: [https://www.youtube.com/watch?v=A2LqqBosvY0 Function trains in APL] (video) | ||
* [[Dyalog webinar]]: [https://www.youtube.com/watch?v=Enlh5qwwDuY?t=440 Train Spotting in Dyalog APL] (video) | * [[Dyalog webinar]]: [https://www.youtube.com/watch?v=Enlh5qwwDuY?t=440 Train Spotting in Dyalog APL] (video) | ||
* [[Dyalog '13]]: [https://www.youtube.com/watch?v=7-93GzDqC08 Train Spotting in Version 14.0] (video) | * [[Dyalog '13]]: [https://www.youtube.com/watch?v=7-93GzDqC08 Train Spotting in Version 14.0] (video) | ||
== Documentation == | === Documentation === | ||
* [ | * [https://help.dyalog.com/16.0/Content/RelNotes14.0/Function%20Trains.htm Announcement] | ||
* [ | * [https://help.dyalog.com/latest/Content/Language/Introduction/Trains.htm Dyalog] | ||
==References== | == References == | ||
<references/> | <references/> | ||
{{APL syntax}}[[Category:Tacit programming| ]] | {{APL syntax}}[[Category:Tacit programming| ]] |