Kiki for programmers

Kiki is a statically typed query, markup, and programming language where every program is a live, readable document.

It is built on two core ideas:

• Lisp syntax. Minimal syntax that works well across query, markup, and programming use cases.

• Algebraic effects. One system that supports errors, I/O, optionals, and state.

A Kiki program is literate, meaning it is a doc that contains code. This tour itself is a valid Kiki program.

New to tables and queries? Start with doc/table.ki and doc/query.ki.

Types and values

Kiki is statically typed. A type starts with : and always decorates the value rather than the symbol.

:int

Here are the primitive types; => is a form for unit tests.

Here are the collections: list, record, and table.

A table can be viewed as:

• A list of records, users.0 is {name = "alice" age = 20}.

• A record of lists. users.name is ["alice" "bob"].

The records in a table are ordered, like rows in a spreadsheet, rather than unordered as in SQL.

Internally, a table is stored in columnar format at runtime. Queries are served by a vectorized query engine.

Functions

Kiki is a functional language. A function is defined by fn and called in (callee args) style.

7

:(fn num num -> num)

12

do creates the same local sequence. Bindings are visible only within the scope and the last expression is its value.

3

42

|> pipes a result as the first argument to a function.

11

. composes functions:

14

Recursive functions:

89

Pattern matching

Pattern matching is defined by multiple fn clauses:

89

Pattern matching on a list:

3

Pattern matching with guarded conditions:

"pos"

Data types

Create a new nominal type:

Create a type with variants. Variants are prefixed with '.

:color

A variant can contain some payload data.

Variants work well with pattern matching. The :shape cast tells the clauses which type they are matching:

Effects

Effects are the system for dealing with impure functions, such as errors, I/O, optionals, and state. An effect is prefixed with !.

!div-by-zero is an effect raised by the division and caught by the => testing framework.

handle can be used to catch an effect explicitly, like catch in other languages. It applies to the remaining code in its lexical scope.

6

1000

The ability to resume from the raising point is how effects differ from traditional exceptions.

6

1001

The compiler tracks and analyzes what effects each function may raise.

:(fn int int -> decimal !div-by-zero)

:(fn int int -> decimal)

The term "algebraic effect" means that users can define custom effects.

107.00

Optionals

A type ending with ? might hold the value null. null only equals itself.

Using a null value, or a list position past the end, raises a !null effect, which can be handled with a normal handle !null or the syntactic sugar or-else.

20

0

Modules

Each .ki file is a module: mark public bindings with export and load another module by its path without .ki (a = import "notes/a").

Paths starting with ./ or ../ resolve from the importing file's folder (import "./records"), keeping a folder of pages portable when copied or renamed. On the command line, workspace paths resolve from the current working directory (kiki test notes/b.ki).

These symbols are implicitly imported in every module:

• The prelude's length, concat, and show, plus promoted list.range and math.abs/min/max.

• Standard-library namespaces, implicit > string = import "std/string" etc.

Where next

The language chapters:

• doc/markup.ki — write and format documents

• doc/table.ki — enter, load, and edit tables

• doc/query.ki — filter, transform, and summarize data

• doc/chart.ki — turn data into charts

• doc/ui.ki — controls, buttons, and layout

• doc/type.ki — types, including named and variant types

• doc/effect.ki — errors, optionals, state, and effects

• doc/program.ki — tasks, files, and command-line programs

• std/index.ki — the standard library