OCamlにおけるプレースホルダ式によるアドホック多相の実現

書誌事項

タイトル別名
  • Ad Hoc Polymorphism with Placeholders in OCaml

この論文をさがす

抄録

本発表はプログラミング言語OCamlにおけるアドホック多相のための言語機能を提案する.アドホック多相はデータ型に応じて関数の呼び出し先を変更する言語機能である.従来,OCamlにおいてアドホック多相は高階関数による明示的なディスパッチ先の指定により代替されてきた.しかしながら,そのようなディスパッチの多くは型に応じて一意に決まるため冗長であり,プログラミングが煩雑に感じられることが多い.典型的な例は,printf “%a” myprinter objのように,フォーマット文字列 %aに対して型に応じたプリティプリンタmyprinterにおけるディスパッチだが,ほとんどの場合objの型に対してプリティプリンタは一意に定まる.この状況をふまえ本発表は,静的に推論された型に応じて自動的にディスパッチ先を決定するプレースホルダ式を提案する.その基本的な機能は,任意の型をとりうるプレースホルダ式 __ が,型に応じて一意に定まるインスタンスに置き換えられるというものである.本発表では,プレースホルダ式に基づくアドホック多相がOCamlの様々なプログラミングイディオムに適合し,煩雑さを軽減することを述べる.実装として,OCamlのプリプロセッサ拡張PPXを利用したツールppx_fillupを示す.本拡張の評価として,既存のOCamlプロジェクトに対する適用例を示し,本ツール適用前後でのコンパイル時間や実行時間を比較する.

We propose a language feature in OCaml for ad hoc polymorphism, which makes a dispatch depending on the inferred data type. Conventionally, ad hoc polymorphism in OCaml has been mimicked by explicitly passing a dispatch destination via a higher-order function. However, many such dispatches are redundant and cumbersome to program because they are uniquely determined based on type. A typical example is the type-based pretty-printer used for the format string %a, such as (printf “%a” myprinter obj), but in most cases, the pretty-printer is uniquely determined for the type of obj. Given this situation, we propose a placeholder expression that automatically determines the dispatch destination based on the inferred type. The key functionality is that a placeholder expression __, which can be of any type itself, is replaced by an instance expression that is uniquely determined according to the type. We show that ad hoc polymorphism with placeholder expressions fits various programming idioms in OCaml and reduces the burden of manual dispatches in OCaml. As an implementation, we present ppx_fillup, a tool that utilizes OCaml's PPX preprocessor extension. As an evaluation, we show an example of its application to an existing OCaml project and compare the compilation and execution times with and without this tool.

収録刊行物

詳細情報 詳細情報について

問題の指摘

ページトップへ