まず、これは でのみ機能しghci
ます。このプログラムをコンパイルしようとするとghc
、型エラーが発生します。
Test.hs:3:19:
Ambiguous type variable `a0' in the constraint:
(Show a0) arising from a use of `show'
Probable fix: add a type signature that fixes these type variable(s)
In the second argument of `($)', namely `show []'
In the expression: putStrLn $ show []
In an equation for `main': main = putStrLn $ show []
型シグネチャを追加すると、エラーがなくなります。
module Main where
main = putStrLn $ show ([]::[Int])
しかし、なぜそれが機能したのghci
ですか?答えは拡張タイプのデフォルトですghci
: のタイプa
はデフォルトで()
(ユニットタイプ) に設定されています。
この動作の動機は、ユーザーがインタープリターで作業するときに常に型を指定するのは少し面倒だからです。コメントで Vitus が指摘しているように、で実行ghci
する-Wall
(または に追加:set -Wall
する~/.ghci
) と、デフォルト設定を簡単に見つけることができます。
<interactive>:2:12:
Warning: Defaulting the following constraint(s) to type `()'
(Show a0) arising from a use of `show'
In the second argument of `($)', namely `show []'
In a stmt of an interactive GHCi command: it <- putStrLn $ show []