Direct function explained

A direct function (dfn, pronounced "dee fun") is an alternative way to define a function and operator (a higher-order function) in the programming language APL. A direct operator can also be called a dop (pronounced "dee op"). They were invented by John Scholes in 1996.[1] They are a unique combination of array programming, higher-order function, and functional programming, and are a major distinguishing advance of early 21st century APL over prior versions.

A dfn is a sequence of possibly guarded expressions (or just a guard) between, separated by or new-lines, wherein denotes the left argument and the right, and denotes recursion (function self-reference). For example, the function tests whether each row of is a Pythagorean triplet (by testing whether the sum of squares equals twice the square of the maximum).

PT← PT 3 4 51 x 4 5 3 3 11 6 5 13 1217 16 811 12 417 15 8 PT x1 0 1 0 0 1

The factorial function as a dfn:

fact← fact 5120 fact¨ ⍳10 ⍝ fact applied to each element of 0 to 91 1 2 6 24 120 720 5040 40320 362880

Description

The rules for dfns are summarized by the following "reference card":

   guard
  left argument   left operand   error-guard
  right argument  right operand  default left argument 
  self-reference    self-reference    shy result

A dfn is a sequence of possibly guarded expressions (or just a guard) between, separated by or new-lines. expressionguard: expressionguard:The expressions and/or guards are evaluated in sequence. A guard must evaluate to a 0 or 1; its associated expression is evaluated if the value is 1. A dfn terminates after the first unguarded expression which does not end in assignment, or after the first guarded expression whose guard evaluates to 1, or if there are no more expressions. The result of a dfn is that of the last evaluated expression. If that last evaluated expression ends in assignment, the result is "shy"—not automatically displayed in the session.

Names assigned in a dfn are local by default, with lexical scope.

denotes the left function argument and the right; denotes the left operand and the right. If occurs in the definition, then the dfn is a dyadic operator; if only occurs but not, then it is a monadic operator; if neither or occurs, then the dfn is a function.

The special syntax is used to give a default value to the left argument if a dfn is called monadically, that is, called with no left argument. The is not evaluated otherwise.

denotes recursion or self-reference by the function, and denotes self-reference by the operator. Such denotation permits anonymous recursion.

Error trapping is provided through error-guards, . When an error is generated, the system searches dynamically through the calling functions for an error-guard that matches the error. If one is found, the execution environment is unwound to its state immediately prior to the error-guard's execution and the associated expression of the error-guard is evaluated as the result of the dfn.

Additional descriptions, explanations, and tutorials on dfns are available in the cited articles.[2] [3] [4] [5]

Examples

The examples here illustrate different aspects of dfns. Additional examples are found in the cited articles.

Default left argument

The function adds to (or

\sqrt{-1}

) times .

3 43J4 ∘.⍨ ¯2+⍳5¯2J¯2 ¯2J¯1 ¯2 ¯2J1 ¯2J2¯1J¯2 ¯1J¯1 ¯1 ¯1J1 ¯1J2 0J¯2 0J¯1 0 0J1 0J2 1J¯2 1J¯1 1 1J1 1J2 2J¯2 2J¯1 2 2J1 2J2

The significance of this function can be seen as follows:

Moreover, analogous to that monadic ⇔ (negate) and monadic ⇔ (reciprocal), a monadic definition of the function is useful, effected by specifying a default value of 0 for : if, then ⇔ ⇔ .

j←

3 j 4 ¯5.6 7.893J4 3J¯5.6 3J7.89

j 4 ¯5.6 7.890J4 0J¯5.6 0J7.89

sin← 1∘○ cos← 2∘○ Euler←

Euler (¯0.5+?10⍴0) j (¯0.5+?10⍴0)1 1 1 1 1 1 1 1 1 1

The last expression illustrates Euler's formula on ten random numbers with real and imaginary parts in the interval

\left(-0.5,0.5\right)

.

Single recursion

The ternary construction of the Cantor set starts with the interval [0,1] and at each stage removes the middle third from each remaining subinterval:

l[0,1r]\to

\left[0,1
3

\right]\cup\left[

2
3

,1\right]\to

\left[0,1
9

\right]\cup\left[

2,
9
1
3

\right]\cup\left[

2,
3
7
9

\right]\cup\left[

8
9

,1\right]\to

The Cantor set of order defined as a dfn:

Cantor←

Cantor 01 Cantor 11 0 1 Cantor 21 0 1 0 0 0 1 0 1 Cantor 31 0 1 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 1 0 1

Cantor 0 to Cantor 6 depicted as black bars:

The function computes a bit vector of length so that bit (for and) is 1 if and only if is a prime.

sieve←

10 10 ⍴ sieve 1000 0 1 1 0 1 0 1 0 00 1 0 1 0 0 0 1 0 10 0 0 1 0 0 0 0 0 10 1 0 0 0 0 0 1 0 00 1 0 1 0 0 0 1 0 00 0 0 1 0 0 0 0 0 10 1 0 0 0 0 0 1 0 00 1 0 1 0 0 0 0 0 10 0 0 1 0 0 0 0 0 10 0 0 0 0 0 0 1 0 0

b←sieve 1e9 ≢b1000000000 (10*⍳10) (+⌿↑)⍤0 1 ⊢b0 4 25 168 1229 9592 78498 664579 5761455 50847534

The last sequence, the number of primes less than powers of 10, is an initial segment of . The last number, 50847534, is the number of primes less than

109

. It is called Bertelsen's number, memorably described by MathWorld as "an erroneous name erroneously given the erroneous value of

\pi(109)=50847478

".

uses two different methods to mark composites with 0s, both effected using local anonymous dfns: The first uses the sieve of Eratosthenes on an initial mask of 1 and a prefix of the primes 2 3...43, using the insert operator (right fold). (The length of the prefix obtains by comparison with the primorial function .) The second finds the smallest new prime remaining in, and sets to 0 bit itself and bits at times the numbers at remaining 1 bits in an initial segment of . This second dfn uses tail recursion.

Tail recursion

Typically, the factorial function is define recursively (as above), but it can be coded to exploit tail recursion by using an accumulator left argument:fac←

Similarly, the determinant of a square complex matrix using Gaussian elimination can be computed with tail recursion:det←

Multiple recursion

A partition of a non-negative integer

n

is a vector

v

of positive integers such that, where the order in

v

is not significant. For example, and are partitions of 4, and and and are considered to be the same partition.

P(n)

counts the number of partitions. The function is of interest in number theory, studied by Euler, Hardy, Ramanujan, Erdős, and others. The recurrence relation
n
P(n)=\sum
k=1

(-1)k+1[P(n-

1k(3k-1))+P(n-
2
1
2

k(3k+1))]

derived from Euler's pentagonal number theorem. Written as a dfn:

pn ← rec ←

pn 1042 pn¨ ⍳13 ⍝ OEIS A0000411 1 2 3 5 7 11 15 22 30 42 56 77

The basis step states that for, the result of the function is, 1 if ⍵ is 0 or 1 and 0 otherwise. The recursive step is highly multiply recursive. For example, would result in the function being applied to each element of, which are:

rec 200199 195 188 178 165 149 130 108 83 55 24 ¯10198 193 185 174 160 143 123 100 74 45 13 ¯22

and requires longer than the age of the universe to compute (

7.57 x 1047

function calls to itself). The compute time can be reduced by memoization, here implemented as the direct operator (higher-order function) :

M←

pn M 2003.973E12 0 ⍕ pn M 200 ⍝ format to 0 decimal places 3972999029388

This value of agrees with that computed by Hardy and Ramanujan in 1918.

The memo operator defines a variant of its operand function to use a cache and then evaluates it. With the operand the variant is:

Direct operator (dop)

Quicksort on an array works by choosing a "pivot" at random among its major cells, then catenating the sorted major cells which strictly precede the pivot, the major cells equal to the pivot, and the sorted major cells which strictly follow the pivot, as determined by a comparison function . Defined as a direct operator (dop) : Q←

⍝ precedes ⍝ follows ⍝ equals 2 (×-) 8 8 (×-) 2 8 (×-) 8¯1 1 0

x← 2 19 3 8 3 6 9 4 19 7 0 10 15 14

(×-) Q x0 2 3 3 4 6 7 8 9 10 14 15 19 19

is a variant that catenates the three parts enclosed by the function instead of the parts per se. The three parts generated at each recursive step are apparent in the structure of the final result. Applying the function derived from to the same argument multiple times gives different results because the pivots are chosen at random. In-order traversal of the results does yield the same sorted array. Q3←

(×-) Q3 x┌────────────────────────────────────────────┬─────┬┐│┌──────────────┬─┬─────────────────────────┐│19 19││││┌──────┬───┬─┐│6│┌──────┬─┬──────────────┐││ │││││┌┬─┬─┐│3 3│4││ ││┌┬─┬─┐│9│┌┬──┬────────┐│││ │││││││0│2││ │ ││ ││││7│8││ │││10│┌──┬──┬┐││││ │││││└┴─┴─┘│ │ ││ ││└┴─┴─┘│ │││ ││14│15││││││ ││││└──────┴───┴─┘│ ││ │ │││ │└──┴──┴┘││││ ││││ │ ││ │ │└┴──┴────────┘│││ ││││ │ │└──────┴─┴──────────────┘││ │││└──────────────┴─┴─────────────────────────┘│ ││└────────────────────────────────────────────┴─────┴┘ (×-) Q3 x┌───────────────────────────┬─┬─────────────────────────────┐│┌┬─┬──────────────────────┐│7│┌────────────────────┬─────┬┐││││0│┌┬─┬─────────────────┐││ ││┌──────┬──┬────────┐│19 19││││││ │││2│┌────────────┬─┬┐│││ │││┌┬─┬─┐│10│┌──┬──┬┐││ ││││││ │││ ││┌───────┬─┬┐│6│││││ │││││8│9││ ││14│15││││ ││││││ │││ │││┌┬───┬┐│4│││ │││││ │││└┴─┴─┘│ │└──┴──┴┘││ ││││││ │││ │││││3 3│││ │││ │││││ ││└──────┴──┴────────┘│ ││││││ │││ │││└┴───┴┘│ │││ │││││ │└────────────────────┴─────┴┘││││ │││ ││└───────┴─┴┘│ │││││ │ ││││ │││ │└────────────┴─┴┘│││ │ ││││ │└┴─┴─────────────────┘││ │ ││└┴─┴──────────────────────┘│ │ │└───────────────────────────┴─┴─────────────────────────────┘

The above formulation is not new; see for example Figure 3.7 of the classic The Design and Analysis of Computer Algorithms. However, unlike the pidgin ALGOL program in Figure 3.7, is executable, and the partial order used in the sorting is an operand, the the examples above.

Dfns with operators and trains

Dfns, especially anonymous dfns, work well with operators and trains. The following snippet solves a "Programming Pearls" puzzle:[6] given a dictionary of English words, here represented as the character matrix, find all sets of anagrams.

a ⍤1 ⊢a (⍤1 ⌸ ⊢) apats apst ┌────┬────┬────┐spat apst │pats│teas│star│teas aest │spat│sate│ │sate aest │taps│etas│ │taps apst │past│seat│ │etas aest │ │eats│ │past apst │ │tase│ │seat aest │ │east│ │eats aest │ │seta│ │tase aest └────┴────┴────┘star arsteast aestseta aest

The algorithm works by sorting the rows individually, and these sorted rows are used as keys ("signature" in the Programming Pearls description) to the key operator to group the rows of the matrix. The expression on the right is a train, a syntactic form employed by APL to achieve tacit programming. Here, it is an isolated sequence of three functions such that ⇔, whence the expression on the right is equivalent to .

Lexical scope

When an inner (nested) dfn refers to a name, it is sought by looking outward through enclosing dfns rather than down the call stack. This regime is said to employ lexical scope instead of APL's usual dynamic scope. The distinction becomes apparent only if a call is made to a function defined at an outer level. For the more usual inward calls, the two regimes are indistinguishable.[7]

For example, in the following function, the variable is defined both in itself and in the inner function . When calls outward to and refers to, it finds the outer one (with value) rather than the one defined in (with value):

which←

which ' scope'lexical scope

Error-guard

The following function illustrates use of error guards:[7] plus← 2 plus 3 ⍝ no errors5 2 3 4 5 plus 'three' ⍝ argument lengths don't matchlength 2 3 4 5 plus 'four' ⍝ can't add charactersdomain 2 3 plus 3 4⍴5 ⍝ can't add vector to matrixcatch all

In APL, error number 5 is "length error"; error number 11 is "domain error"; and error number 0 is a "catch all" for error numbers 1 to 999.

The example shows the unwinding of the local environment before an error-guard's expression is evaluated. The local name is set to describe the purview of its following error-guard. When an error occurs, the environment is unwound to expose 's statically correct value.

Dfns versus tradfns

Since direct functions are dfns, APL functions defined in the traditional manner are referred to as tradfns, pronounced "trad funs". Here, dfns and tradfns are compared by consideration of the function : On the left is a dfn (as defined above); in the middle is a tradfn using control structures; on the right is a tradfn using gotos and line labels.

History

Kenneth E. Iverson, the inventor of APL, was dissatisfied with the way user functions (tradfns) were defined. In 1974, he devised "formal function definition" or "direct definition" for use in exposition. A direct definition has two or four parts, separated by colons:name : expressionname : expression0 : proposition : expression1Within a direct definition, denotes the left argument and the right argument. In the first instance, the result of is the result of the function; in the second instance, the result of the function is that of if evaluates to 0, or if it evaluates to 1. Assignments within a direct definition are dynamically local. Examples of using direct definition are found in the 1979 Turing Award Lecture[8] and in books and application papers.[9] [10] [11] [12]

Direct definition was too limited for use in larger systems. The ideas were further developed by multiple authors in multiple works[13] [14] [15] [16] but the results were unwieldy. Of these, the "alternative APL function definition" of Bunda in 1987[15] came closest to current facilities, but is flawed in conflicts with existing symbols and in error handling which would have caused practical difficulties, and was never implemented. The main distillates from the different proposals were that (a) the function being defined is anonymous, with subsequent naming (if required) being effected by assignment; (b) the function is denoted by a symbol and thereby enables anonymous recursion.[12]

In 1996, John Scholes of Dyalog Limited invented direct functions (dfns).[1] [5] The ideas originated in 1989 when he read a special issue of The Computer Journal on functional programming.[17] He then proceeded to study functional programming and became strongly motivated ("sick with desire", like Yeats) to bring these ideas to APL.[5] He initially operated in stealth because he was concerned the changes might be judged too radical and an unnecessary complication of the language; other observers say that he operated in stealth because Dyalog colleagues were not so enamored and thought he was wasting his time and causing trouble for people. Dfns were first presented in the Dyalog Vendor Forum at the APL '96 Conference and released in Dyalog APL in early 1997.[1] Acceptance and recognition were slow in coming. As late as 2008, in Dyalog at 25,[18] a publication celebrating the 25th anniversary of Dyalog Limited, dfns were barely mentioned (mentioned twice as "dynamic functions" and without elaboration)., dfns are implemented in Dyalog APL,[7] NARS2000, and ngn/apl.[19] They also play a key role in efforts to exploit the computing abilities of a graphics processing unit (GPU).[20] [12]

External links

Notes and References

  1. Scholes. John. Direct Functions in Dyalog APL. Vector. 13. 2. October 1996. 16 September 2019.
  2. Scholes . John . D: A Functional Subset of Dyalog APL . Vector . 17 . 4 . April 2001 . 21 September 2019.
  3. Scholes . John . 13 September 2009 . Introduction to D-functions: 1 of 2 . video . Dyalog '09 User Conference . 21 September 2019.
  4. Scholes . John . 13 September 2009 . Introduction to D-functions: 2 of 2 . video . Dyalog '09 User Conference . 21 September 2019.
  5. Scholes . John . 31 October 2018 . Dfns—Past, Present and Future . video . Dyalog '18 User Meeting . 21 September 2019.
  6. Bentley. Jon. Programming Pearls. Communications of the ACM. 26. 8 and 9. August 1983.
  7. Book: Dyalog. Dyalog Programming Reference Guide, version 17.1, Dfns & Dops, pp. 133-147. Dyalog Ltd.. 15 August 2019. 30 September 2019.
  8. Iverson. Kenneth E.. Kenneth E. Iverson. Notation as a Tool of Thought. Communications of the ACM. 23. 8. August 1980. 8 April 2016. 10.1145/358896.358899. 444–465. free.
  9. Book: Iverson, Kenneth E.. Kenneth E. Iverson. Elementary Analysis. APL Press. 1976.
  10. Book: Orth, D.L.. Calculus in a New Key. APL Press. 1976.
  11. Hui. Roger. Roger Hui. Some Uses of . APL 87 Conference Proceedings. May 1987. 15 April 2016.
  12. Hui. Roger. Kromberg. Morten. APL Since 1978. Proceedings of the ACM on Programming Languages. 4. HOPL. June 2020. 1–108. 10.1145/3386319. 218517570. free.
  13. Iverson. Kenneth E.. Kenneth E. Iverson. Wooster. Peter. A Function Definition Operator. APL81 Conference Proceedings, APL Quote Quad. 12. 1. September 1981.
  14. Iverson. Kenneth E.. Kenneth E. Iverson. A Dictionary of APL. APL Quote Quad. 18. 1. September 1987. 5–40. 10.1145/36983.36984. 18301178. 19 September 2019.
  15. Bunda. John. APL Function Definition Notation. APL87 Conference Proceedings, APL Quote Quad. 17. 4. May 1987.
  16. Book: Hui, Roger. Conference proceedings on APL 90: For the future . APL\? . Roger Hui. etal. 20. 4. July 1990. 192–200. 10.1145/97808.97845. 089791371X. 235453656 . http://www.jsoftware.com/papers/J1990.htm. 2019-09-10.
  17. Wadler. Philip L.. etal. Special Issue on Functional Programming. The Computer Journal. 32. 2. 1 January 1989.
  18. Dyalog. Dyalog at 25. Vector. September 2008. 2019-09-20.
  19. Nickolov. Nick. Compiling APL to JavaScript. Vector. 26. 1. September 2013. 19 September 2019.
  20. Hsu. Aaron. A Data Parallel Compiler Hosted on a GPU. Ph.D.. Indiana University. 2019. 25 December 2019.