GHCi セッションからの次の例を考えてみましょう:
Prelude> :set -XRankNTypes
Prelude> let bar :: (forall a.[a]->[a]) -> [Int]; bar f = f [1,2,3]
これは、bar
ランク 2 型の関数を定義します。したがって、型推論は次の正しい型を推論できないはずです。
Prelude> let foo f = bar f
本当に、
<interactive>:7:17:
Couldn't match type `t' with `[a] -> [a]'
`t' is a rigid type variable bound by
the inferred type of foo :: t -> [Int] at <interactive>:7:5
In the first argument of `bar', namely `f'
In the expression: bar f
In an equation for `foo': foo f = bar f
驚くべきことに、同じものをポイントフリースタイルで書くとうまくいきます:
Prelude> let baz = bar
Prelude> :t baz
baz :: (forall a. [a] -> [a]) -> [Int]
ここで型推論が上位の型を推論できるのはどうしてですか? これがGHCで特別に扱われていることを確認したり、私が間違っている場所を指摘したりできますか.