🌕

elmのurl parserを見ながらMoonBitでurl parserを作ってみてわかった中置演算子やパイプらへんこと

に公開

この記事は https://qiita.com/advent-calendar/2025/moonbit の18日目です。


elmのUrl.Parserが好き

自分はelmのUrlパーサのライブラリがだいぶ好きです。
MoonBitでも真似して実装してみました。
ライブラリ作り中に気づいたMoonBitの書きっぷりについて紹介したいと思います。

https://package.elm-lang.org/packages/elm/url/1.0.0/Url-Parser

elmのurl parserは以下のように使われるライブラリです。

import Html exposing (text)
import Url.Parser exposing (s, int, (</>), parse, string, map)
import Url

main =
  let
    -- パーサ定義
    parser = s "users" </> string </> s "posts" </> int 
      |> map ((\userId -> \name -> (userId, name)))
    urlM = Url.fromString "https://localhost/users/alice/posts/123"
  in
  urlM 
    |> Maybe.andThen (parse parser) 
    |> Debug.toString 
    |> text

urlのパス構造をスラッシュ区切りで定義し、sで一致すべき文字列を定義しstring, intでパスにあった文字列をキャプチャしmapでparserから取り出したい値へ変換できるような感じです。

で、作ってみたところ下のような使い心地になるライブラリができました。

https://github.com/ryota0624/moonbit_elmlish_url

fn main {
  let u = @lib.top()
    |> @lib.slash(@lib.s("users"))
    |> @lib.slash(@lib.string())
    |> @lib.slash(@lib.s("posts"))
    |> @lib.slash(@lib.int())
    |> @lib.map(userId => postId => (userId, postId), _)
  let st = @lib.parse(u, "users/alice/posts/123")
  println(st)
}

作ってみた中で中置演算子やパイプ、部分適用についてわかってきました。

infix operator: 中置演算子

elmでは/は特別扱いされていて中置演算子として定義されています。
https://github.com/elm/url/blob/1.0.0/src/Url/Parser.elm#L49

MoonBitではDivトレイトが実装されていることで呼び出せる演算子とされています。
https://docs.moonbitlang.com/en/latest/language/methods.html#operator-overloading

url parserとしては/はparserの合成を行うslash関数のエイリアスとして扱いたいところでしたが、それはできませんでした。

pub fn[A, B, C] slash(
  parser1 : Parser[A, B],
  parser2 : Parser[B, C],
) -> Parser[A, C] {
 ...
}

というのもDivはSelfを取ってSelfを返すのを要求しているので、
slashで定義したような合成して型が変わるような実装はできませんでした。

pub(open) trait Div {
  div(Self, Self) -> Self = _
}

pipelines

https://docs.moonbitlang.com/en/latest/language/fundamentals.html#pipelines

/が合成用のオペレーターとして使えなかったのでpipeでそれっぽい宣言をしてみています。

@lib.top()
    |> @lib.slash(@lib.s("users"))
    |> @lib.slash(@lib.string())

ドキュメント中にあるように、MoonBitのpipelinesはパイプで渡された値を関数の第一引数に与えられたものとします。

The MoonBit code follows the data-first style, meaning the function places its "subject" as the first argument.

Elmの場合、第一引数とか第二引数という概念がなく、pipelinesで値を渡す先の関数の唯一の引数として渡すので下のようなコードと出力になります。

import Html exposing (text)

concat a b = a ++ " " ++ b 

main =
  "world" |> concat "hello" |> text
> hello world

一方MoonBitでは以下のようになります。
concatには先に"Hello"と与えているのに第二引数になるのはチョット不思議でした。

fn concat(a : String, b : String) -> String {
  a + " " + b
}

fn main {
  println("World" |> concat("Hello"))
}
> World Hello

では、MoonBitでelmのように関数で関数を返すように実装したらどうなるのか?というとpipeliesに値を渡して実行できなくなります。
value1 |> fnc(value2)という宣言の時点でfncが引数を二つ取る関数とるという想定をしてそうです。
pipelinesを使う想定をする関数はMoonBitでは関数を返す関数より、引数を並べたほうが良さそうですね。

fn concat2(a : String) -> (String) -> String {
  fn(b : String) -> String {
    a + " " + b
  }
}

fn main {
  println("World" |> concat2("Hello"))
}

This function has type (String) -> (String) -> String, which requires 1 arguments, but is given 2 arguments.moonE4080

partial application: 部分適用

https://docs.moonbitlang.com/en/latest/language/fundamentals.html#partial-applications

pipelinesではパイプに与える値は第一引数に行くというわけでしたが、partial applicationを使うとどこに渡されるか制御できます。

fn concat(a : String, b : String) -> String {
  a + " " + b
}

fn concat3(a : String, b : String, c : String) -> String {
  a + " " + b + " " + c
}


fn main {
  println("World" |> concat("Hello", _))
  println("World" |> concat3("Hello", _, "!"))
}
Hello World
Hello World !

関数を返す関数の場合もコレで解決できるのか?と思っていましたがコンパイルエラーになります。

fn main {
  println("World" |> concat2("Hello")(_))
}

Unsupported expression after the pipe operator.moon E4101

https://docs.moonbitlang.com/en/latest/language/error_codes/E4101.html

pipelinesで呼び出しできるのはシンプルな関数だけと考えると良いところなんでしょう。

最後に

ファンクショナルな言語でパイプもあるぜ!って感じでもelmとMoonBitでpipelinesの挙動に違いがあるとに驚き、MoonBit以外にもこういう方針で第一引数に渡すぜ。って言語が他にあるのか気になったのでした。

The MoonBit code follows the data-first style, meaning the function places its "subject" as the first argument.

Discussion