DEV Community

Alex Spinov
Alex Spinov

Posted on

Koka Has a Free API: The Language With Algebraic Effects That Makes Side Effects Safe and Composable

Every function has side effects — IO, exceptions, state, async. Most languages ignore them in the type system. Koka tracks every effect, making your code safer and more composable without sacrificing readability.

What Is Koka?

Koka is a functional programming language developed at Microsoft Research. Its killer feature is algebraic effect handlers — a system that tracks and controls side effects in the type system. Every function signature tells you exactly what effects it can perform.

Why Koka Is Different

  • Algebraic effects: First-class effect system in the type signature
  • Effect handlers: Define custom control flow (async, exceptions, state)
  • Perceus reference counting: Automatic memory management without GC pauses
  • FBIP: Functional-but-in-place optimization for zero-copy updates
  • C compilation: Compiles to efficient C code
  • Completely free and open source

Quick Start

Install Koka:

curl -sSL https://github.com/koka-lang/koka/releases/latest | sh
# Or build from source
git clone https://github.com/koka-lang/koka
cd koka && stack build
Enter fullscreen mode Exit fullscreen mode

Write your first Koka program:

fun main()
  println("Hello from Koka!")
  val result = add-with-logging(3, 4)
  println("Result: " ++ result.show)

// Effect is tracked: this function performs console IO
fun add-with-logging(x: int, y: int): console int
  println("Adding " ++ x.show ++ " + " ++ y.show)
  x + y
Enter fullscreen mode Exit fullscreen mode

Algebraic effects:

// Define a custom effect
effect ask<a>
  fun ask(): a

// Use the effect
fun greeting(): ask<string> string
  val name = ask()
  "Hello, " ++ name ++ "!"

// Handle the effect
fun main()
  with fun ask() "World"
  println(greeting())  // "Hello, World!"
Enter fullscreen mode Exit fullscreen mode

Effect handlers for state:

effect state<s>
  fun get(): s
  fun set(x: s): ()

fun counter(): state<int> int
  val current = get()
  set(current + 1)
  current

fun main()
  var s := 0
  with handler
    fun get() s
    fun set(x) s := x

  println(counter().show)  // 0
  println(counter().show)  // 1
  println(counter().show)  // 2
Enter fullscreen mode Exit fullscreen mode

Why Developers Choose Koka

A compiler team was building an optimization pass with multiple interleaved effects: logging, error handling, and mutable state. In Haskell, they stacked monad transformers — the code became unreadable. In Koka, each effect was declared independently and composed naturally. Their optimization pass was half the code and twice as readable.

Who Is This For?

  • PL researchers exploring algebraic effects in practice
  • Functional programmers who want better effect management than monads
  • Systems programmers wanting predictable memory with Perceus RC
  • Anyone frustrated by hidden side effects in their codebase

Try Koka

Koka shows what programming looks like when effects are first-class citizens. If you have ever fought with monad transformer stacks or wished your type system tracked IO, Koka is worth exploring.

Need help with compiler development or language tooling? I build specialized developer tools — reach out to discuss your project.


Found this useful? I publish daily deep-dives into developer tools and APIs. Follow for more.

Top comments (0)