ghci
GHCのインタプリタ
以下の:hogeのようなコマンドは、一文字で:hのように省略可能
$ stack ghci
$ stack ghci --no-load
docs
めちゃくちゃコマンドある
関数の情報を見る
:info hoge
以下のようなものを調べることができる
型
どのModuleで定義されているか
演算子の場合は、記号の優先順位
ex. infixr 9
数値が大きいほど結合度が強い
どの型クラスに属するか
code:ex.prelude
:i $
($) :: (a -> b) -> a -> b -- Defined in ‘GHC.Base’
infixr 0 $
code:ex.prelude
:i <*>
class Functor f => Applicative (f :: * -> *) where
...
(<*>) :: f (a -> b) -> f a -> f b
...
-- Defined in ‘GHC.Base’
infixl 4 <*>
型の確認
:type
code:ex.prelude
:t length
length :: Foldable t => t a -> Int
:type +d
わかりやすく
code:ex.prelude
:t +d length
length :: a -> Int
:type +v
より詳しく
code:ex.prelude
:t +v length
length :: Foldable t => forall a. t a -> Int
と、ここに書いてたが、出ねえな。何もナシと同じになる。なんでだmrsekut.icon
カインド
:kind
ファイルを読み込む
:load hoge.hs
リロード
:reload
browse
:browse Hoge.Hoge
moduleを引数を指定する
そのmodule内で定義されている識別子を表示する
つまり、そのmodule内で定義されている型や関数の型定義を表示する
Template Haskellを使っている場合、展開されたあとの識別子一覧が表示される
ref 『Haskell入門』.icon p.341~
終了
:quit
ヘルプ
:help
importしているモジュールの一覧
:show imports
ghciの:sprint
itで前回の結果を参照する
code:ghci
f x = x + 10
f 3
13
f it -- it == 13
23
ghciで複数行入力する
プロンプトの変更
:set prompt "> "
実行時間の計測, 使用メモリ量の計測
:set +sをしたあとに関数を実行
GHC拡張を使う
:set -XNoImplicitPrelude -XStrict
-X+拡張名
式のどこに括弧が付くのか手軽に調べる ref
code:例
$ ghci -ddump-splices -XTemplateHaskell
ghci> $(readMaybe <$> getLine >>= print |)
<interactive>:1:3-39: Splicing expression
readMaybe <$> getLine >>= print |
======>
((readMaybe <$> getLine) >>= print)
#??
moduleをimportしたときに、exportしていない関数もghci上で使いたい
ghciの起動時に拡張読み込むコマンドなかったっけ?
起動してから毎回:set prompt "> "をやるのが面倒
あるなら起動時にstack ghci -s prompt "> "みたいにやりたい
https://haskell.jp/blog/posts/2017/12-ghc-show-info.html#はじめに