K

From APL Wiki
Jump to navigation Jump to search

K denotes a family of programming languages designed by Arthur Whitney, which is sold by Kx Systems and Shakti and also supported by several independent implementations. K is an ASCII-only language influenced by Whitney's previous APL design A+. It has fewer primitives in part because it represents arrays as nested lists, unifying rank and depth, and encourages Scheme-like functional programming with first-class functions. Whitney presented the first K implementation (K0) in 1992, and soon founded Kx Systems to develop it further with versions numbering K1 through K6. K4 is now the implementation language for the time-series database kdb+ as well as derivative language Q. Whitney has developed further versions of K at Shakti, beginning with K7. Notable non-commercial implementations include Kona based on K3, and ngn/k and oK based on K6.

Language overview

The name K encompasses many versions with differing behavior. Which primitives are defined, and particularly the syntax for each primitive, is subject to major changes. However, the principles behind the design are more stable.

K uses the nested list model, in which the only fundamental array type is a list (vector) and arrays with rank other than 1 are represented as nested lists. It supports several other types that are treated as conceptually equivalent to possibly-nested lists: functions, dictionaries, and tables. A table behaves like a list of dictionaries with the same set of keys but is represented as a dictionary of lists by the implementation. Any of these types can be applied using bracket syntax f[x;y;z], which might for example call a function, or index into a nested structure of lists and dictionaries. Non-collection, or atom types, include integers (generally 64-bit), floating-point numbers, characters, and symbols which are a string-like type using string interning for fast comparison and lookups (they are often used as dictionary keys).

K uses a simplified and context-free form of APL's syntax with nouns for arrays, verbs for functions and adverbs for monadic operators. Core primitive verbs are written with single ASCII symbols, and adverbs with one symbol and possibly a following colon like ':, although additional primitives may use other rules, for example names like exp. APL's convention of function calling by juxtaposition is largely limited to primitives, since only a primitive or the result of an adverb can be a verb (and only primitives can be adverbs). Additionally there is a rule that two nouns indicates a monadic function call, and the dyadic @ verb applies its left argument to its right. Numbers separated by spaces form a list as part of token formation, like APL's vector notation. An expression ending in a verb may form a train; unlike in APL, the flow is strictly linear, to be evaluated as though the argument were placed at the end of the expression.

Because of the limited number of ASCII characters, many forms of overloading are used to pack functionality into each one, often distinguishing different cases based on the type of a function's arguments when called. Most primitive verbs have both a monadic and a dyadic meaning, but this is inferred statically so that any instance of the primitive indicates one function or the other (if both are possible, dyadic is usually assumed and an explicit trailing colon indicates monadic, such as #:' for count-each). Thus each function value takes a fixed number of arguments—it may be called with fewer arguments to form a projection that binds these arguments to it. Adverbs may check not only the type of their operand but its number of arguments if it's a function. Definitions on dictionaries usually extend the list behavior—that is, the definition on lists follows from the one for dictionaries using indices for the keys.

Functions can be defined with an anonymous style enclosed in curly braces—similar to dfns, but designed before them. Most versions use local lexical scoping, but some implementations including Kona and oK extend to full lexical scoping with closures (and K3 allows the use of variables from one scope above to implicitly create a projection). The function body may start with a header in square brackets [] giving the argument names, overriding the default x, y, and z.

History

Arthur Whitney created the first version of K in 1992. At the time he was employed by Morgan Stanley, where he had worked on A+ since 1988. In 1993 he left to form Kx Systems with CEO Janet Lustgarten,[1] where he further developed K and the associated time-series database system kdb. Versions K1 to K4, and databases kdb and Kdb+, were released by Kx Systems, while K5 and K6 were built but not released as products. The step from K3 to K4 was the most significant from a language perspective, as it generalized dictionaries to allow arbitrary keys and extended many primitives to apply to them. Eventually K4 would also take a secondary status at Kx in favor of Q, which Whitney created in 2003 as an alternative syntax for K functionality with English words for monadic functions to reduce ambiguity.[2]

In the 2010s, programmers who had worked with K in a professional capacity began to develop open-source versions of the language. The first to become known was Kevin Lawler's Kona, based on K3 and started in 2010. oK and ngn/k are notable later efforts: both initially based on K5, they have coordinated to offer similar functionality based on saved K6 binaries.[3]

Whitney began publishing information about new K work through Shakti Software around 2019. Version numbers used at Shakti include K7 and K9 (with K8 skipped). While Shakti's main products have not generally been accessible to the public, some demonstration codebases have been released with names like k/simple, k/fun, and k/edu. With very limited functionality (only a few primitive functions, and no way to make user-defined ones), these are intended more to teach Whitney's programming style than to be used.

Naming

K's single-letter name parallels J, and Whitney had offered design input on J shortly before publishing K. Unlike J, K is often written k, without capitalization. Whitney has given various explanations of the choice of letter,[4] including "keys to the kingdom".[5]

Release timeline

The table below lists releases by Whitney's companies, external implementations (which may still have significant differences from the relevant K version), and derivative languages. The Evolution of Database Software shows some releases; years below may not all be accurate.

Year Releases Implementations Derivatives
1992 K0
1994 K1
1996 K2
1998 K3
2000 K4
2003 Q
2010 kuc (K5)
2014 Kona (K3)
2015 oK (K6) Klong
2017 ngn/k (K6)
2018 K7
2019 ktye/i (K6)
2020 K9 Special K xs
2022 Goal, Lil
2023 gk (K3)
2024 k/simple

Primitives

K3

From the Kona Wiki.

Verb Monadic Dyadic Triadic Tetradic
+ flip (Transpose) plus
- negate minus
* first times
% reciprocal divide
| reverse max|or
& where min|and
^ shape power
! enumerate (includes Iota) rotate|mod
< grade up less than
> grade down greater than
= group equals
~ not, attribute match
@ atom at index amend/trap amend
? range (Unique) function inverse/find/draw/deal/sample invert-guess
_ floor drop|cut
, enlist (like Enclose) join
# count take|reshape
$ format dollar (conversions)
. make/unmake dictionary dot index amend/trap amend
: colon assignment
Adverb Definition
/ over (includes Reduce and Power)
\ scan
' each
/: each right
\: each left
': eachpair (like Windowed Reduce)

External links

References

  1. Clare McDonald. "Janet Lustgarten, CEO at Kx Systems, on Shampoo Apps, Databases and Founding Her Own Company". Computer Weekly. 2011-06-01.
  2. Simon Garland. "Q Language Widening the Appeal of Vectors". Archived from the original on January 1, 2007.
  3. Conor Hoekstra and others. John Earnest and Versions of k. The Array Cast episode 41. 2022-11-25.
  4. Arthur Whitney. Re: Why call it K? response 1, response 2.
  5. Arthur Whitney. "K". Vector Journal volume 10 issue 1.
APL dialects [edit]
Maintained APL+WinAPL2APL64APL\ivApletteAprilCo-dfnsDyalog APLDyalog APL Visiondzaima/APLGNU APLKapNARS2000PometoTinyAPL
Historical A+ (A) ∙ APL#APL2CAPL.68000APL-10APL*PLUSAPL.jlAPLXExtended Dyalog APLI-APLngn/aplopenAPLParrot APLRowanSAXSHARP APLVisualAPL (APLNext) ∙ VIZ::APL
Mainframe APL\360APL/700APL\1130APL\3000APL/B5500APLBAPL.SVAPLGOLAPLUMCDC APL 2IVSYS/7090NARSPATUMASS-APLVS APLXerox APLYork APL
Publications A Programming LanguageIverson notationOperators and FunctionsRationalized APL
Derivatives AHPLAPL/SAppleBQNCoSyELIEMPLFIXAPLGleeIIvyJJellyK (Goal, Klong, Q) ∙ KamilaLispLang5LilNialRADUiua
Overviews Comparison of APL dialectsTimeline of array languagesTimeline of influential array languagesFamily tree of array languages