15

でネストされた変数の推定型を出力する方法はありますghciか? コードを考えてみましょう。

let f = g where
    g (x :: Int) = x

次に、 のタイプを照会するとよいでしょう。gたとえば、:t f.gが出力されInt -> Intます。

4

2 に答える 2

11

適切に間違った型の注釈を付けて、エラー メッセージを確認することで、この情報を引き出すことができます。

*Main> let f = g where g::a; g (x::Int) = x

<interactive>:1:23:
    Couldn't match type `a1' with `Int -> Int'
      `a1' is a rigid type variable bound by...
于 2011-07-20T07:38:56.860 に答える
10

ghci デバッガーは、適切に配置されたブレークポイントを使用して出力できます (ただし、モジュール内に定義をロードする必要があります)。

{-# LANGUAGE ScopedTypeVariables #-} 

f a = g a where
    g (x :: Int) = x

次にghciで:

Prelude> :l tmp2.hs
[1 of 1] Compiling Main             ( tmp2.hs, interpreted )
Ok, modules loaded: Main.
*Main> :b 3 9
Breakpoint 0 activated at tmp2.hs:3:7-9
*Main> f undefined
Stopped at tmp2.hs:3:7-9
_result :: Int = _
a :: Int = _
g :: Int -> Int = _
[tmp2.hs:3:7-9] *Main>
于 2011-07-20T15:37:13.103 に答える