# Elias Mårtenson

## Control structures in KAP

This post was inspired by the latest episode of Array Cast. In the episode, control structures in different array languages were discussed, with particular focus on the (what should probably be referred to as) big three: Dyalog, J and BQN. They also mentioned K, Q and GNU APL.

The participants were very thorough in going through the various options available to programmers of the above-mentioned languages, and it was a very informative episode. One of the more interesting ones they've had, since the series on tacit programming, in my opinion.

What is needed is some commentary on control structures in KAP, and this post serves to provide this.

## About the need for control structures

As was touched upon in the episode, array languages generally don't need to use them as often as other languages. As much as KAP attempts to be better at imperative control structures than other array languages, it's somewhat ironic that the main example given for a routine that needs a loop actually does not need one in KAP.

The example was along the lines of:

Multiply a number by 2 until the value is greater than 1000, and return that number.

Of course, this can be solved without any iteration at all just using maths, but for the purpose of this example, the assumption is that the multiplication has to be done some number of times.

The most straightforward solution in APL is:

{1 ⍳⍨ 1000 ≤ {2×⍺}\⍵+⍳1000}


In fact, thanks to KAP's lazy evaluation, this function will only perform the computation until a valid result is found. Thus, a loop is not needed in this case. This feature was discussed in a previous blog post, and also this post.

That said, a looping version would look like this:

{
i ← 0
c ← ⍵
while (c < 1000) {
i ← i+1
c ← c×2
}
i
}


## Basic control structures

KAP provides control structures that are very similar in syntax and behaviour to C, Java and other similar languages.

### If statement

Here's an example of the if statement:

if (a < b) {
io:println "a is less than b"
} else {
io:println "a is greater than or equal to b"
}


In Dyalog, control structures are not values. An :If statement cannot return a value. In KAP, everything returns a value, and in the case of the if statement, the value returned is that of the last expression in the clause that was evaluated. Hence, the following will print 3:

a ← 10
io:println if(a<100) { a-7 } else { 100 }


### While statement

KAP also provides a while loop that also have the same behaviour as that of C:

i ← 0
while (i < 5) {
io:println i
i ← i+1
}


The control structures that were mentioned above are not native to the language and are instead implemented as part of the standard library. How this is done is discussed further below.

### When statement

The when is an alternative syntax to chained if/else statements. As KAP doesn't have an else if, such chains results in too much indentation, so when is useful as an alternative.

It consists of a series of conditions and associated clauses. It attempts each condition in turn, and once one returns true, the corresponding clause is evaluated and its return value is used as the return value of the entire statement. If none of the clauses evaluates to true, ⍬ is returned. Here is a simple example:

a ← 1
res ← when {
(a=0) { "a is zero" }
(a=1) { "a is one" }
(1)   { "a is not in the set [0,1]" }
}
io:println res


## The power operator

KAP implements the power operator (⍣), and is a subset of the capabilities available in Dyalog. For the purposes of the discussion of using the power operator to provide the behaviour of an if statement, the functionality is identical. The following expression prints “foo” if a is true:

({ io:println "foo" }⍣a) 0


This code is not very good at communicating what it actually does, and while it's a lot better than the example mentioned in the podcast episode where people were generating code in a string and evaluated it, it's still not particularly understandable.

I agree with Marshall who pointed out that the power operator is very specific and is not good as a general primitive operator. I agree with this, and I would even go a bit further to say that it's not very good at all. The problem in Dyalog is that it's overloaded to do three very different things:

• Apply a function some number of times
• An implementation of C's do/while statement
• Calling the inverse of a function

The first point is OK. If this was the only thing the power operator did then I wouldn't have much problems with it.

The second point makes for a really bad looping construct, and as Adám mentioned on the show if you want to create a normal while loop then one has to use two separate invocations of ⍣.

The third point reuses an existing symbol to provide a completely different functionality. If the right argument to ⍣ is ¯1, then the derived function is the inverse of the function specified on the left. For example, the following returns 6 in Dyalog, since that is the value that has to be added to 4 in order to get 10.

4 (+⍣¯1) 10


I have to admit that using this syntax is really clever, as it relates to common maths notation. But, as a programming construct it's really bad. Imagine if the right argument is used as a variable. If that variable happens to have the value ¯1, then the behaviour of the function will change completely. I really can't see any situation in which this would be desired behaviour. For this reason, KAP uses ˝ to indicate function inverse.

That said, the KAP implementation of while is basically nothing more than a wrapper around ⍣ implemented using custom syntax, as explained below.

## Signals

The goal is to implement a condition system similar to what Common Lisp provides, although this is not available at this time. Thus, the current functionality is very similar to that of languages like Java.

Since KAP does not support goto, the symbol used to provide goto in APL, the right arrow (→), has been repurposed to throw exceptions. All exceptions have a type, which is simply any APL object (although it's usually a symbol) and a value, which is another APL object. The general syntax is as follows:

type → value


When called monadically, the type becomes :error.

When an exception is thrown, the stack is unwound and execution continues in the innermost catch handler which is declared to catch exceptions of the given type.

Declaring catch handlers are currently somewhat cumbersome. There is currently no custom syntax defined to make this nicer, so the catch operator have to be used. At the risk of scaring away any interested readers, here's an example of how it's used:

{
:foo → 1
io:println "this will never be printed"
} catch 1 2 ⍴ :foo λ{ io:println "caught exception with data: " , ⍕⍺ }


## Custom syntax

One of the reasons APL is an attractive language is that at its core, the language itself is very simple. It consists of a limited number of primitives on top of which the rest of the language is built. This is similar to another favourite language of mine: Lisp. A lot of the extensions that KAP provides on top of APL are inspired by Lisp.

Lisp allows the developer to extend the syntax using macros. For the reader without experience in Lisp, the one-line summary is that it allows you to treat the code as data, and rewrite the code while it's being compiled. This allows you to completely change the behaviour of the language to suit the problem at hand.

KAP does not implement a full macro system, although this has been considered and may yet happen at some point in the future. Instead, the language provides a keyword: defsyntax that gives instructions to the parser to interpret the code in a different way. The parsed components are then passed to a function that is called at runtime.

As was mentioned earlier, the power operator can serve as a foundation for various control structures. As it already implements a repeat operation, let's use defsyntax to create such a facility:

defsyntax repeat (:value count :nfunction clause) {
(⍞clause ⍣ count) 0
}


Once the above has been run, we can print “test” 5 times:

repeat (5) {
io:println "test"
}