LoginSignup
3
1

More than 1 year has passed since last update.

Haskell の関数定義を検索するスクリプトを作ってみました

Last updated at Posted at 2022-06-10

Haskell 入門中です。関数定義を検索する h コマンドを作ってみました。

ダウンロード

使い方

h コマンドで関数定義を検索すると、

$ h zipWith

ソース (ソース閲覧用 html) をダウンロードして bat で開いてくれます:

h-zipWith.png

本当はローカルの Haskell レポジトリから検索した方が良いとは思います。

動機

hoogle では関数やデータ型の検索ができます:

$ hoogle print -n1
Prelude print :: Show a => a -> IO ()
-- plus more results not shown, pass --count=11 to see more

-i オプション (info) を渡すと、より詳細な解説やコード例を出力してくれます:

$ hoogle -i print
print :: Show a => a -> IO ()
base Prelude
The print function outputs a value of any printable type to the
standard output device. Printable types are those that are instances

~~ 省略 ~~

しかし print に関しては関数定義を出してくれた方がストレートで嬉しいです:

print           :: Show a => a -> IO ()
print x         =  putStrLn (show x)

そこで強引に関数定義を探す小さなコマンドを作ってみました。

作り方

まず hoogle コマンドで hackage へのリンクを探します:

$ hoogle search --json -n1 "print"
[{"docs":"The <a>print</a> function outputs a value of any printable type to the\nstandard output device. Printable types are those that are instances\nof class <a>Show</a>; <a>print</a> converts values to strings for\noutput using the <a>show</a> operation and adds a newline.\n\nFor example, a program to print the first 20 integers and their powers\nof 2 could be written as:\n\n<pre>\nmain = print ([(n, 2^n) | n &lt;- [0..19]])\n</pre>\n","item":"print :: Show a => a -> IO ()","module":{"name":"Prelude","url":"https://hackage.haskell.org/package/base/docs/Prelude.html"},"package":{"name":"base","url":"https://hackage.haskell.org/package/base"},"type":"","url":"https://hackage.haskell.org/package/base/docs/Prelude.html#v:print"}]

$ hoogle search --json -n1 "print" | jq .[].url
"https://hackage.haskell.org/package/base/docs/Prelude.html#v:print"

$ hoogle search --json -n1 "print" | jq .[].url | sed 's;";;g'
https://hackage.haskell.org/package/base/docs/Prelude.html#v:print

このリンクを開くと Hackage の関連ページに来ることができます 1:

hackage-print.png

右上の Source ボタンを押すと 、ソース閲覧用ページに飛べます:

print-def.png

この html ページをダウンロードして bat に渡します:

printf '%s' "$source" | bat -l haskell --pager "less -RF --pattern=$word.*::"

h-zipWith.png

  1. Slack で教えていただきました。ありがとうございました。

3
1
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
3
1