Tacit programming: Difference between revisions

Jump to navigation Jump to search
432 bytes added ,  10:31, 11 September 2022
m
Text replacement - "<source" to "<syntaxhighlight"
(function composition now occurs naturally)
m (Text replacement - "<source" to "<syntaxhighlight")
Line 1: Line 1:
'''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 [[train]]s following a small set of rules. This allows creating complex [[derived function]]s without specifying any arguments explicitly.
'''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 (<syntaxhighlight inline lang=apl>⍺ ⍵</source>) and [[tradfn]]s (which have named arguments). Some APL dialects allow to combine functions into [[train]]s 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]].
Dialects which implement trains include [[Dyalog APL]], [[dzaima/APL]], [[ngn/apl]] and [[NARS2000]].
Line 5: Line 5:
== Primitives ==
== Primitives ==
All [[primitive functions]] are tacit. Some APLs allow primitive functions to be named.
All [[primitive functions]] are tacit. Some APLs allow primitive functions to be named.
<source lang=apl>
<syntaxhighlight lang=apl>
       plus ← +
       plus ← +
       times ← ×
       times ← ×
Line 14: Line 14:
== Derived functions ==
== Derived functions ==
Functions derived from a monadic operator and an operand, or from a dyadic operator and two operands are tacit functions:
Functions derived from a monadic operator and an operand, or from a dyadic operator and two operands are tacit functions:
<source lang=apl>
<syntaxhighlight lang=apl>
       Sum ← +/
       Sum ← +/
       Sum ⍳10
       Sum ⍳10
Line 25: Line 25:
== Derived operators ==
== Derived operators ==
A dyadic operator with its right operand forms a tacit monadic operator:
A dyadic operator with its right operand forms a tacit monadic operator:
<source lang=apl>
<syntaxhighlight lang=apl>
       1(+⍣2)10
       1(+⍣2)10
12
12
Line 38: Line 38:
These rules are used for 3-trains:
These rules are used for 3-trains:
{|
{|
|<source lang=apl>  (f g h) ⍵</source>|| {{←→}} ||<source lang=apl>(  f ⍵) g (  h ⍵)</source>
|<syntaxhighlight lang=apl>  (f g h) ⍵</source>|| {{←→}} ||<syntaxhighlight lang=apl>(  f ⍵) g (  h ⍵)</source>
|-
|-
|<source lang=apl>⍺ (f g h) ⍵</source>|| {{←→}} ||<source lang=apl>(⍺ f ⍵) g (⍺ h ⍵)</source>
|<syntaxhighlight lang=apl>⍺ (f g h) ⍵</source>|| {{←→}} ||<syntaxhighlight lang=apl>(⍺ f ⍵) g (⍺ h ⍵)</source>
|}
|}
The ''left tine'' of a fork can be an array:
The ''left tine'' of a fork can be an array:
{|
{|
|<source lang=apl>  (A g h) ⍵</source>|| {{←→}} ||<source lang=apl>A g (  h ⍵)</source>
|<syntaxhighlight lang=apl>  (A g h) ⍵</source>|| {{←→}} ||<syntaxhighlight lang=apl>A g (  h ⍵)</source>
|-
|-
|<source lang=apl>⍺ (A g h) ⍵</source>|| {{←→}} ||<source lang=apl>A g (⍺ h ⍵)</source>
|<syntaxhighlight lang=apl>⍺ (A g h) ⍵</source>|| {{←→}} ||<syntaxhighlight lang=apl>A g (⍺ h ⍵)</source>
|}
|}


In APL (but not [[J]]), these rules are used for 2-trains:
In APL (but not [[J]]), these rules are used for 2-trains:
{|
{|
|<source lang=apl>  (g h) ⍵</source>|| {{←→}} ||<source lang=apl>g (  h ⍵)</source>
|<syntaxhighlight lang=apl>  (g h) ⍵</source>|| {{←→}} ||<syntaxhighlight lang=apl>g (  h ⍵)</source>
|-
|-
|<source lang=apl>⍺ (g h) ⍵</source>|| {{←→}} ||<source lang=apl>g (⍺ h ⍵)</source>
|<syntaxhighlight lang=apl>⍺ (g h) ⍵</source>|| {{←→}} ||<syntaxhighlight lang=apl>g (⍺ h ⍵)</source>
|}
|}


Line 59: Line 59:


{|
{|
|<source lang=apl>(f g h) ⍵</source>|| {{←→}} ||<source lang=apl>g⍨∘f⍨∘h⍨ ⍵</source>
|<syntaxhighlight lang=apl>(f g h) ⍵</source>|| {{←→}} ||<syntaxhighlight lang=apl>g⍨∘f⍨∘h⍨ ⍵</source>
|-
|-
|<source lang=apl>(f g f) ⍵</source>|| {{←→}} ||<source lang=apl>g⍥f⍨ ⍵</source>
|<syntaxhighlight lang=apl>(f g f) ⍵</source>|| {{←→}} ||<syntaxhighlight lang=apl>g⍥f⍨ ⍵</source>
|-
|-
|<source lang=apl>(⊢ g f) ⍵</source>|| {{←→}} ||<source lang=apl>g∘f⍨ ⍵</source>
|<syntaxhighlight lang=apl>(⊢ g f) ⍵</source>|| {{←→}} ||<syntaxhighlight lang=apl>g∘f⍨ ⍵</source>
|}
|}




== Debugging ==
== 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.
In [[Dyalog APL]], analysis of trains is assisted by a [[user command]] <syntaxhighlight lang=apl inline>]Boxing on</source>. This is achieved by executing the command <syntaxhighlight 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:
For example, the "accursed train" from the section below can be analysed like this:
<source lang=apl>
<syntaxhighlight lang=apl>
       ]Boxing on
       ]Boxing on
Was OFF
Was OFF
Line 89: Line 89:


Alternatively, a train can be represented in form of a tree:
Alternatively, a train can be represented in form of a tree:
<source lang=apl>
<syntaxhighlight lang=apl>
       ]Boxing on -trains=tree
       ]Boxing on -trains=tree
Was ON -trains=box
Was ON -trains=box
Line 103: Line 103:
</source>
</source>
Or fully parenthesised:
Or fully parenthesised:
<source lang=apl>
<syntaxhighlight lang=apl>
       ]Boxing on -trains=parens
       ]Boxing on -trains=parens
Was OFF -trains=box
Was OFF -trains=box
Line 111: Line 111:


=== Conversion to dfns ===
=== Conversion to dfns ===
It can help understanding to convert a tacit function to a dfn. For many tacit functions, it is not immediately clear if the intention of the function is to be used monadically or dyadically, or even both. Such knowledge can be conveyed by comments, but sometimes it is possible to spot patterns that are exclusively monadic or dyadic: A function with a bound argument (for example <source lang=apl inline>+∘1</source>) can indicate a monadic function, and in some contexts, <source lang=apl inline>=</source>, which can only be used dyadically, would indicate a dyadic function. The website [https://tacit.help tacit.help] provides automated translation of most tacit functions, into both monadic and dyadic, fully parenthesised dfns.
It can help understanding to convert a tacit function to a dfn. For many tacit functions, it is not immediately clear if the intention of the function is to be used monadically or dyadically, or even both. Such knowledge can be conveyed by comments, but sometimes it is possible to spot patterns that are exclusively monadic or dyadic: A function with a bound argument (for example <syntaxhighlight lang=apl inline>+∘1</source>) can indicate a monadic function, and in some contexts, <syntaxhighlight lang=apl inline>=</source>, which can only be used dyadically, would indicate a dyadic function. The website [https://tacit.help tacit.help] provides automated translation of most tacit functions, into both monadic and dyadic, fully parenthesised dfns.
== Examples ==
== Examples ==
One of the major benefits of tacit programming is the ability to convey a short, well-defined idea as an isolated expression. This aids both human readability ([[semantic density]]) and the computer's ability to interpret code, potentially executing special code for particular [[idiom]]s.
One of the major benefits of tacit programming is the ability to convey a short, well-defined idea as an isolated expression. This aids both human readability ([[semantic density]]) and the computer's ability to interpret code, potentially executing special code for particular [[idiom]]s.


=== Plus and minus ===
=== Plus and minus ===
<source lang=apl>
<syntaxhighlight lang=apl>
       (+,-) 2    ⍝ ±2
       (+,-) 2    ⍝ ±2
2 ¯2
2 ¯2
Line 124: Line 124:


=== Arithmetic mean ===
=== Arithmetic mean ===
<source lang=apl>
<syntaxhighlight lang=apl>
       (+⌿÷≢) ⍳10      ⍝ Mean of the first ten integers
       (+⌿÷≢) ⍳10      ⍝ Mean of the first ten integers
5.5
5.5
Line 133: Line 133:
=== Fractions ===
=== Fractions ===
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
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>
<syntaxhighlight 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\tfrac{5}{8}</math> with a mixed fraction:
Alternatively, we can convert it to the mixed fraction <math>2\tfrac{5}{8}</math> with a mixed fraction:
<source lang=apl>
<syntaxhighlight lang=apl>
       (1∧0 1∘⊤,÷)2.625
       (1∧0 1∘⊤,÷)2.625
2 5 8
2 5 8
Line 144: Line 144:


=== Is it a palindrome? ===
=== Is it a palindrome? ===
<source lang=apl>
<syntaxhighlight lang=apl>
       (⌽≡⊢)'racecar'
       (⌽≡⊢)'racecar'
1
1
Line 152: Line 152:


=== Split delimited text ===
=== Split delimited text ===
<source lang=apl>
<syntaxhighlight lang=apl>
       ','(≠⊆⊢)'comma,delimited,text'
       ','(≠⊆⊢)'comma,delimited,text'
┌─────┬─────────┬────┐
┌─────┬─────────┬────┐
Line 166: Line 166:
Sometimes a train can make an expression nicely resemble its equivalent definition in traditional mathematical notation. As an example, here is a program to compute the component of a vector <math>\textbf{a}</math> in the direction of another vector <math>\textbf{b}</math>:
Sometimes a train can make an expression nicely resemble its equivalent definition in traditional mathematical notation. As an example, here is a program to compute the component of a vector <math>\textbf{a}</math> in the direction of another vector <math>\textbf{b}</math>:
:::<math>\textbf{a}_\textbf{b} = (\textbf{a}\cdot\hat{\textbf{b}})\hat{\textbf{b}}</math>
:::<math>\textbf{a}_\textbf{b} = (\textbf{a}\cdot\hat{\textbf{b}})\hat{\textbf{b}}</math>
<source lang=apl>
<syntaxhighlight lang=apl>
       Root ← *∘÷⍨              ⍝ Nth root
       Root ← *∘÷⍨              ⍝ Nth root
       Norm ← 2 Root +.×⍨      ⍝ Magnitude (norm) of numeric vector in Euclidean space
       Norm ← 2 Root +.×⍨      ⍝ Magnitude (norm) of numeric vector in Euclidean space
Line 178: Line 178:
===The Number of the Beast===
===The Number of the Beast===
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.
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>
<syntaxhighlight lang=apl>
       ((+.×⍨⊢~∘.×⍨)1↓⍳)17 ⍝ Accursed train
       ((+.×⍨⊢~∘.×⍨)1↓⍳)17 ⍝ Accursed train
666
666
</source>
</source>
First, <source lang=apl inline>((+.×⍨⊢~∘.×)1↓⍳)</source> is supplied with only one argument <source lang=apl inline>17</source> and is thus interpreted monadically.
First, <syntaxhighlight lang=apl inline>((+.×⍨⊢~∘.×)1↓⍳)</source> is supplied with only one argument <syntaxhighlight lang=apl inline>17</source> and is thus interpreted monadically.


Second, <source lang=apl inline>(+.×⍨⊢~∘.×⍨)1↓⍳</source> is a 4-train: reading right-to-left, the last 3 components are interpreted as the fork <source lang=apl inline>1↓⍳</source> and the 4-train is interpreted as the atop <source lang=apl inline>(+.×⍨⊢~∘.×⍨)(1↓⍳)</source>.
Second, <syntaxhighlight lang=apl inline>(+.×⍨⊢~∘.×⍨)1↓⍳</source> is a 4-train: reading right-to-left, the last 3 components are interpreted as the fork <syntaxhighlight lang=apl inline>1↓⍳</source> and the 4-train is interpreted as the atop <syntaxhighlight lang=apl inline>(+.×⍨⊢~∘.×⍨)(1↓⍳)</source>.
Similarly, <source lang=apl inline>(+.×⍨⊢~∘.×⍨)</source> is also a 4-train and interpreted as the atop <source lang=apl inline>+.×⍨(⊢~∘.×⍨)</source>.  
Similarly, <syntaxhighlight lang=apl inline>(+.×⍨⊢~∘.×⍨)</source> is also a 4-train and interpreted as the atop <syntaxhighlight lang=apl inline>+.×⍨(⊢~∘.×⍨)</source>.  


Thus the accursed train is interpreted as <source lang=apl inline>((+.×⍨(⊢~∘.×⍨))(1↓⍳))17</source>. Having read the train, we now evaluate it monadically.
Thus the accursed train is interpreted as <syntaxhighlight lang=apl inline>((+.×⍨(⊢~∘.×⍨))(1↓⍳))17</source>. Having read the train, we now evaluate it monadically.
<source lang=apl>
<syntaxhighlight lang=apl>
       ((+.×⍨(⊢~∘.×⍨))(1↓⍳))17 ⍝ Accursed train as an atop over a fork atop a fork
       ((+.×⍨(⊢~∘.×⍨))(1↓⍳))17 ⍝ Accursed train as an atop over a fork atop a fork
       +.×⍨(⊢~∘.×⍨)1↓⍳17      ⍝ Atop evalution
       +.×⍨(⊢~∘.×⍨)1↓⍳17      ⍝ Atop evalution
Line 196: Line 196:
666                          ⍝ the sum of the squares of the primes up to 17
666                          ⍝ the sum of the squares of the primes up to 17
</source>
</source>
Note that <source lang=apl inline>((⊢⍨∘.×⍨)1↓⍳)</source> is a train computing primes up to the given input.
Note that <syntaxhighlight lang=apl inline>((⊢⍨∘.×⍨)1↓⍳)</source> is a train computing primes up to the given input.


A more satisfying variation of the accursed train is the following.
A more satisfying variation of the accursed train is the following.
<source lang=apl>
<syntaxhighlight lang=apl>
       (⍎⊢,⍕∘≢)'((+.×⍨⊢~∘.×⍨)1↓⍳)'                    ⍝ Accursed train 2.0
       (⍎⊢,⍕∘≢)'((+.×⍨⊢~∘.×⍨)1↓⍳)'                    ⍝ Accursed train 2.0
       ⍎(⊢,⍕∘≢)'((+.×⍨⊢~∘.×⍨)1↓⍳)'                    ⍝ 4-train intepreted as an atop
       ⍎(⊢,⍕∘≢)'((+.×⍨⊢~∘.×⍨)1↓⍳)'                    ⍝ 4-train intepreted as an atop

Navigation menu