Koka: a function-oriented language with effect inference
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:
- The Haskell platform (version 7.4 or later) or Stack.
- The NodeJS runtime (version 4.2 LTS or later).
- Some version of Git for version control.
All these programs are very easy to install on most platforms.
Now we can build Koka itself:
-
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. -
Go to the newly created Koka directory:
> cd koka
-
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 thenpm
package manager has enough
permissions to install thejake
andmadoko
tools. -
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
-
Finally, build the compiler and run the Koka interactive environment:
> jake
jake
usesghc
to compile Haskell programs by default.
If you usestack
to build the project, set environment variablebuild_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
-
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.
-
Daan Leijen. “Koka: Programming with Row Polymorphic Effect Types.” In Mathematically Structured Functional Programming 2014. EPTCS. Mar. 2014. arXiv:1406.2061.
-
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].
-
Daan Leijen. “Type Directed Compilation of Row-Typed Algebraic Effects.” In Proceedings of Principles of Programming Languages (POPL’17). Paris, France. Jan. 2017.
-
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