koka

Koka language compiler and interpreter

Github stars Tracking Chart

Koka: a function-oriented language with effect inference

Build Status
Join the chat at https://gitter.im/koka-lang/koka

For more background information, see:

  • The Koka book for a specification of the Koka language and a primer on algebraic effects.
  • The library documentation.
  • The Koka research page and the slides of a talk presented Lang.Next (April 2012).
  • The article Algebraic Effects for Functional Programming (#references) about the algebraic effects in Koka.

Installing the compiler

At this point there are no binary releases of Koka and you need to build
the compiler yourself. Fortunately, Koka has few dependencies and builds
without problems on most common platforms, e.g. Windows, MacOSX, and
Unix.

The following programs are required to build Koka:

All these programs are very easy to install on most platforms.
Now we can build Koka itself:

  1. First clone the Koka sources with algebraic effects support:

    > git clone https://github.com/koka-lang/koka.git

    You can also use the flag -b dev to get the latest development version.

  2. Go to the newly created Koka directory:

    > cd koka

  3. Install any needed Node libraries using the Node package manager:

    > npm install

    If you are running on MacOSX or Unix, you may have to run this as
    sudo npm install so that the npm package manager has enough
    permissions to install the jake and madoko tools.

  4. Install alex, a lexer generator used by the Koka compiler,

    a. if you use cabal,

    > cabal update
    > cabal install alex
    

    b. or if you use stack,

    > stack install alex
    
  5. Finally, build the compiler and run the Koka interactive environment:

    > jake

    jake uses ghc to compile Haskell programs by default.
    If you use stack to build the project, set environment variable build_with_stack:

    > jake compiler build_with_stack=true
    > jake # enter interactive shell
    

    only once to build.

    You can type jake help to see an overview of all make targets.

The Atom text editor is recommended
to edit Koka programs. You can install support for Koka programs using

> jake atom

(or use jake sublime) for the Sublime editor).

Running the interactive compiler

After running a plain jake command, the Koka interactive environment will start:

__          _, __ ___, __ __ _, / // _ \, / // _`, welcome to the koka interpreter, <, (_), <, (_, 0.9.0-dev (debug), Nov 10 2018, _, \_\\___/, _, \_\\__,_, type :? for help

loading: std/core

Now you can test some expressions:

> println("hi koka")
hi koka

> :t "hi"
string

> :t println("hi")
console ()

Or load a demo:

> :l demo/collatz
compile: lib/demo/collatz.kk
check  : demo/collatz
modules:
  demo/collatz

> main()
Collatz(27) took 111 steps.

And quit the interpreter:

> :q

Before the effect one believes in different causes than one does after the effect.
 -- Friedrich Nietzsche

You can also run examples in the browser by setting the host:

> :set --host=browser
> 1+2

Some browser specific demo to try is for example demo/dom/conway.kk.

Algebraic effect handlers

A novel feature of Koka is a compiled and typed implementation of algebraic
effect handlers (described in detail in (#references)).
In the interactive environment, you can load various demo files with algebraic
effects which are located in the test/algeff directory. This is by default
included in the search path, so we can load them directly using
the load (:l) command:

> :l scoped

Use the :? command to get an overview of all commands. After
loading the scoped demo, we can run it directly from the interpreter:

> :l scoped
compile: test/algeff/scoped.kk
check  : scoped
modules:
  scoped

> main()
[,[2,1],[1,2],[1,1,1]]
(state=12, [,[2,1],[1,2],[1,1,1]])
[(state=1, ),(state=5, [2,1]),(state=5, [1,2]),(state=9, [1,1,1])]
[]
[42]

Some interesting demos are:

  • common.kk: Various examples from the paper "Algebraic Effects for
    Functional Programming
    " (#references). Shows how to implement
    common control-flow abstractions like exceptions, state, iterators,
    ambiguity, and asynchronous programming.

  • scoped.kk: Various examples from the paper "Effect handlers in
    Scope
    " (#references).

  • nim.kk: Various examples from the paper "Liberating effects with
    rows and handlers
    " (#references).

  • async*.kk: Various asynchronous effect examples.

  • parser.kk: Implements parser combinators as an effect.

Enjoy,
-- Daan

References

  1. Daniel Hillerström, and Sam Lindley. “Liberating Effects with Rows and Handlers.” In Proceedings of the 1st International Workshop on Type-Driven Development, 15--27. TyDe 2016. Nara, Japan. 2016. doi:10.1145/2976022.2976033.

  2. Daan Leijen. “Koka: Programming with Row Polymorphic Effect Types.” In Mathematically Structured Functional Programming 2014. EPTCS. Mar. 2014. arXiv:1406.2061.

  3. Daan Leijen. Algebraic Effects for Functional Programming. MSR-TR-2016-29. Microsoft Research. Aug. 2016. https://www.microsoft.com/en-us/research/publication/algebraic-effects-for-functional-programming. Extended version of [4].

  4. Daan Leijen. “Type Directed Compilation of Row-Typed Algebraic Effects.” In Proceedings of Principles of Programming Languages (POPL’17). Paris, France. Jan. 2017.

  5. Nicolas Wu, Tom Schrijvers, and Ralf Hinze. “Effect Handlers in Scope.” In Proceedings of the 2014 ACM SIGPLAN Symposium on Haskell, 1--12. Haskell ’14. ACM, New York, NY, USA. 2014. doi:10.1145/2633357.2633358

Main metrics

Overview
Name With Ownerkoka-lang/koka
Primary LanguageHaskell
Program languageJavaScript (Language Count: 21)
Platform
License:Other
所有者活动
Created At2016-12-08 23:00:37
Pushed At2025-04-25 05:03:26
Last Commit At2024-03-04 09:57:13
Release Count67
Last Release Namev3.1.3-alpha14 (Posted on )
First Release Name0.5.1 (Posted on )
用户参与
Stargazers Count3.5k
Watchers Count60
Fork Count178
Commits Count5.3k
Has Issues Enabled
Issues Count366
Issue Open Count159
Pull Requests Count74
Pull Requests Open Count25
Pull Requests Close Count50
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private