0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Kokaの環境構築

Last updated at Posted at 2025-11-23

Kokaについて

Kokaは関数型言語で、副作用を型システムで管理するアプローチを取っています。
理論的にはAlgebric Effect(代数的エフェクト)に基づく実装ということらしいです。

簡単な例

簡単にHaskellとの対比を書いておきますが、以下のような感じになります。

  • 引数を受け取ってIO出力する例

Haskell

greet :: String -> IO ()
greet name = do
    print("Hello, " ++ name)

Koka

fun greet(name : string) : console ()
  println("Hello, " ++ name)
  • 分母が0の場合に例外を返す割り算

Haskell

safeDivide :: Int -> Int -> IO Int
safeDivide x y = do
    if y == 0 then
              ioError $ userError "Divisin by zero"
    else
      return $ x `div` y

Koka

fun safe-divide(x : int, y : int) : exn int
  if y == 0 then
    throw("Division by zero")
  else
    x / y
  • 複数の副作用がある例

Haskell

process :: Int -> IO Int
process x = do
    print "Processing"
    if x < 0  then ioError $ userError "Negative number"
              else return $ x * 2

Koka

fun process(x : int) : <console,exn> int
  println("Processing: " ++ x.show)
  if x < 0 then
    throw("Negative number")
  else
    x * 2

環境構築

Kokaの環境構築を行っていきます。

インストール

インストールは以下の「Installing the compiler」の項目を参照して下さい。

vim

公式がプラグインを提供しているので利用します。

NeoVimを利用している場合は、以下の様に書きます。

  Plug 'https://github.com/koka-lang/koka', { 'rtp': 'support/vim' }

NeoBundleを利用している場合は、以下の様に書きます。

  NeoBundle 'koka-lang/koka', {'rtp': 'support/vim/'}

動作確認を行います。
以下の様に記述したファイルをtest.kkとして作成します。
上記のプラグインが正常に適用されていればシンタックスハイライトが有効になります。

fun main() {
  println("Hello World")
}

ファイルを実行する場合は、以下の様にします。
-o 実行ファイル名オプションをつけておかないと、ビルドしたファイルが~/.kokaの下に作成されてしまうので注意して下さい。

koka -e test.kk -o test

VSCode

VSCodeで利用する場合は、インストール手順書にある以下の拡張機能を導入して下さい。

  • Koka Language (koka)

VSCode上でコマンド実行したい場合は、以下の launch.jsontasks.jsonのどちらかで対応することになります。
ただし、launch.jsonの方は、実行後にデバッグ状態が完了しないので、タスク実行の方がオススメです。
特に、2025/11/23現在、VSCode上でのステップ実行などは未対応なので、launch.jsonの方で実行するメリットはほぼありません。

launch.jsonの設定
  • F5を押下するとパネルが表示されるので、「create a launch.json file」を選択します
  • launch.jsonのベースファイルが作成されるので、以下を追記して下さい

作成したプログラムを開いてF5で実行可能です。

  "configurations": [
    {
      "name": "Koka: Run",
      "type": "node",
      "request": "launch",
      "runtimeExecutable": "koka",
      "args": ["-e", "${file}"],
      "console": "integratedTerminal",
      "cwd": "${workspaceFolder}"
    },
    {
      "name": "Koka: Run (Debug)",
      "type": "node",
      "request": "launch",
      "runtimeExecutable": "koka",
      "args": ["--debug", "-e", "${file}"],
      "console": "integratedTerminal",
      "cwd": "${workspaceFolder}"
    }
  ]
tasks.jsonの設定
  • Command + Shift + Pでコマンドパレットを開きます
  • 「タスク: タスクの構成」を選択します
  • 「テンプレートからtasks.jsonを構成する」を選択します (下の方)
  • 「Others」を選択します
  • ベースファイルが作成されるので、以下を追記します

作成したプログラムを開いてCommand + Shift + Bで実行可能です。

  "tasks": [
    {
      "label": "Koka: Run",
      "type": "shell",
      "command": "koka",
      "args": ["-e", "${file}"],
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "presentation": {
        "reveal": "always",
        "panel": "dedicated"
      },
      "problemMatcher": []
    },
    {
      "label": "Koka: Run (Debug)",
      "type": "shell",
      "command": "koka",
      "args": ["--debug", "-e", "${file}"],
      "group": "build",
      "presentation": {
        "reveal": "always",
        "panel": "dedicated"
      },
      "problemMatcher": []
    }
  ]

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?