https://koka-lang.github.io/koka/doc/index.html
koka-logo
Koka
A Functional Language with Effect Types and Handlers
// A generator effect with one operation
effect yield
fun yield( x : a ) : ()
// Traverse a list and yield the elements
fun traverse( xs : list ) : yield ()
match xs
Cons(x,xx) -> { yield(x); traverse(xx) }
Nil -> ()
fun main() : console ()
with fun yield(i : int)
println("yielded " ++ i.show)
[1,2,3].traverse
// A generator effect with one operation
effect yieldindex/yield: V -> HX
fun yieldindex/yield: forall (x : a) -> (yield) ()( xx: $134 : aa: V ) : (std/core/types/(): V)std/core/types/(): V
// Traverse a list and yield the elements
fun traverseindex/traverse: forall (xs : list) -> (yield) ()( xsxs: list<$162> : liststd/core/list: V -> V ) : yieldindex/yield: V -> HX (std/core/types/(): V)std/core/types/(): V
match xsxs: list<$162>
Consstd/core/Cons: forall (head : a, tail : list) -> list(xx: $162,xxxx: list<$162>) -> { yieldindex/yield: forall (x : a) -> (yield) ()(xx: $162); traverseindex/traverse: forall (xs : list) -> (yield) ()(xxxx: list<$162>) }
Nilstd/core/Nil: forall list -> (std/core/types/(): ())std/core/types/(): ()
fun mainindex/main: () -> console ()() : consolestd/core/console: X (std/core/types/(): V)std/core/types/(): V
withhandler: (() -> ,console> ()) -> console () fun yieldyield: (i : int) -> console ()(ii: int : intstd/core/types/int: V)
printlnstd/core/println: (s : string) -> console ()("yielded " ++std/core/(++).1: (x : string, y : string) -> string ii: int.showstd/core/show: (i : int) -> string)
[std/core/Cons: forall (head : a, tail : list) -> list1,2,3]std/core/Nil: forall list.traverseindex/traverse: forall (xs : list) -> (yield) ()
Welcome to Koka - a strongly typed functional-style language with
effect types and handlers.
Install Get Started Documentation Github
Note: Koka v2 is a research language that is currently under
development and not ready for production use. Nevertheless, the
language is stable and the compiler implements the full
specification. The main things lacking at the moment are libraries,
package management, and deep IDE integration.
News:
* 2023-12-30: Koka v2.6.0 released with VS Code language
integration with type information, jump to definition, run test
functions directly from the editor, automatic Koka installation,
and many more things. Special thanks to Tim Whiting and Fredrik
Wieczerkowski for all their work on making this possible!
* 2023-12-27: Update of the technical report on "The functional
essence of binary trees" where we use fully-in-place programming
and the new hole contexts to create fully verified functional
implementations of binary search tree algorithms with performance
on par with imperative C implementations.
* 2023-07-03: Koka v2.4.2 released: add support for fip and fbip
keywords described in "FP^2: Fully in-Place Functional
Programming" (ICFP'23) [pdf]. Various fixes and performance
improvements.
* 2021-02-04 (pinned) The Context Free youtube channel posted a
short and fun video about effects in Koka (and 12 (!) other
languages).
* 2021-09-01 (pinned) The ICFP'21 tutorial "Programming with Effect
Handlers and FBIP in Koka" is now available on youtube.
* 2022-02-07: Koka v2.4.0 released: improved specialization and int
operations, add rbtree-fbip sample, improve grammar (pub (instead
of public, remove private (as everything is private by default
now)), final ctl (instead of brk), underscores in number
literals, etc), rename double to float64, various bug fixes.
* 2021-12-27: Koka v2.3.8 released: improved int performance,
various bug fixes, update wasm backend, initial conan support,
fix js backend.
* 2021-11-26: Koka v2.3.6 released: maybe-like types are already
value types, but now also no longer need heap allocation if not
nested (and [Just(1)] uses the same heap space as [1]), improved
atomic refcounting (by Anton Lorenzen), improved specialization
(by Steven Fontanella), various small fixes, add std/os/readline,
fix build on freeBSD
* 2021-10-15: Koka v2.3.2 released, with initial wasm support (use
--target=wasm, and install emscripten and wasmtime), improved
reuse specialization (by Anton Lorenzen), and various bug fixes.
* 2021-09-29: Koka v2.3.1 released, with improved TRMC
optimizations, and improved reuse (the rbtree benchmark is as
fast as C++ now), and faster effect operations. Experimental:
allow elision of -> in anonymous function expressions (e.g. xs.
map( fn(x) x + 1 )) and operation clauses. Command line options
changed a bit with .koka as the standard output directory.
* 2021-09-20: Koka v2.3.0 released, with new brace elision and if/
match conditions without parenthesis. Updated the javascript
backend using ES6 modules and BigInt. new module std/num/int64,
improved effect operation performance.
* 2021-09-05: Koka v2.2.1 released, with initial parallel tasks,
the binary-trees benchmark, and brace elision.
* 2021-08-26: Koka v2.2.0 released, improved simplification (by
Rashika B), cross-module specialization (Steven Fontanella), and
borrowing annotations with improved reuse analysis (Anton
Lorenzen).
* 2021-08-26: At 12:30 EST was the live Koka tutorial at ICFP'21,
see it on youtube.
* 2021-08-23: "Generalized Evidence Passing for Effect Handlers",
by Ningning Xie and Daan Leijen presented at ICFP'21. See it on
youtube or read the paper.
* 2021-08-22: "First-class Named Effect Handlers", by Youyou Cong,
Ningning Xie, and Daan Leijen presented at HOPE'21. See it on
youtube or read the paper.
* 2021-06-23: Koka v2.1.9 released, initial cross-module
specialization (by Steven Fontanella).
* 2021-06-17: Koka v2.1.8 released, initial Apple M1 support.
* The Perceus paper won a distinguished paper award at PLDI'21!
* 2021-06-10: Koka v2.1.6 released.
* 2021-05-31: Koka v2.1.4 released.
* 2021-05-01: Koka v2.1.2 released.
* 2021-03-08: Koka v2.1.1 released.
* 2021-02-14: Koka v2.0.16 released.
* 2020-12-12: Koka v2.0.14 released.
* 2020-12-02: Koka v2.0.12 released.
* 2020-11-29: Perceus technical report publised (pdf).
Why Koka?
Minimal but GeneralThe core of Koka consists of a small set of
well-studied language features, like first-class functions, a
polymorphic type- and effect system, algebraic data types, and effect
handlers. Each of these is composable and avoid the addition of
"special" extensions by being as general as possible.
Effect TypesKoka tracks the (side) effects of every function in its
type, where pure and effectful computations are distinguished. The
precise effect typing gives Koka rock-solid semantics backed by
well-studied category theory, which makes Koka particularly easy to
reason about for both humans and compilers.
Effect HandlersEffect handlers let you define advanced control
abstractions, like exceptions, async/await, iterators, parsers,
ambient state, or probabilistic programs, as a user library in a
typed and composable way.
Perceus Reference CountingPerceus is an advanced compilation method
for reference counting. This lets Koka compile directly to C code
without needing a garbage collector or runtime system! This also
gives Koka excellent performance in practice.
Reuse AnalysisPerceus also enables reuse analysis and lets Koka
optimize functional-style programs to use in-place updates when
possible.
Learn more Read more about these core concepts
Install
Install with the VS Code editor
vscode-install
The easiest way to start with Koka is to use the excellent VS Code
editor and install the Koka extension. Go to the extension panel,
search for Koka and install the official extension as shown on the
right.
Installing the extension also prompts to install the latest Koka
compiler on your platform (available for Windows x64, MacOS x64 and
arm64, and Linux x64).
Once installed, the samples directory is opened. You can also open
this manually from the command panel (Ctrl/Cmd + Shift + P), and
running the Koka: Open samples command (when you start typing the
command will surface to the top). Open for example the basic/
caesar.kk file:
vscode-caesar
When you click run debug (or optimized) Koka compiles and runs the
function, showing the output in the VS Code terminal.
Manual Installation
On Windows (x64), open a cmd prompt and use:
curl -sSL -o %tmp%\install-koka.bat https://github.com/koka-lang/koka/releases/latest/download/install.bat && %tmp%\install-koka.bat
On Linux (x64) and macOS (x64, arm64 (M1/M2)), you can install Koka
using:
curl -sSL https://github.com/koka-lang/koka/releases/latest/download/install.sh | sh
(If you previously installed Koka on macOS using brew, do an brew
uninstall koka first). On other platforms it is usually easy to build
Koka from source instead.
After installation, verify if Koka installed correctly:
$ koka
_ _
| | | |
| | _ ___ | | _ __ _
| |/ / _ \| |/ / _' | welcome to the koka interactive compiler
| ( (_) | ( (_| | version 2.4.0, Feb 7 2022, libc x64 (gcc)
|_|\_\___/|_|\_\__,_| type :? for help, and :q to quit
loading: std/core
loading: std/core/types
loading: std/core/hnd
>
Type :q to exit the interactive environment.
For detailed installation instructions and other platforms see the
releases page. It is also straightforward to build the compiler from
source.
Get started with the compiler