以下のfのような、モナドを返す関数があるとしましょう。しかし、あなたが見るところではInt
、それが本当に複雑なタイプであるふりをしてください。
f :: (Monad m) => m Int -- Pretend this isn't Int but something complicated
f = return 42
Maybe
これをモナドに強制したいとしましょう。これを行うために完全なタイプを記述する必要はありませんf
。次のことを行うことができます。
g :: Maybe a -> Maybe a
g = id
main = print $ (g f)
ダミー関数g
は強制的に。f
になりMaybe
ます。
上記はかなり厄介だと思います。私が書きたいのはこれです:
main = print $ (f :: Maybe a)
ただし、次のエラーで失敗します。
Couldn't match expected type `a' against inferred type `Int'
`a' is a rigid type variable bound by
the polymorphic type `forall a. Maybe a' at prog.hs:7:16
Expected type: Maybe a
Inferred type: Maybe Int
In the second argument of `($)', namely `(f :: Maybe a)'
In the expression: print $ (f :: Maybe a)
g
新しい関数の作成を伴わない、より面倒な方法で上記のことを行う方法はありますか?f :: Maybe Int
返品タイプが変わるとメンテナンスの問題になりますので、書きたくありません。GHC拡張機能は答えに問題がありません。