私は比較的新しいHaskellです。今私はリーダーモナドをより完全に理解しようとしています。それはその目的と使用方法がより明確になっているようです。しかし、ハスケルで:t reader
のタイプを見ている間に私はreader :: MonadReader r m => (r -> a) -> m a
を参照してください。この型制約は何を意味しますか? 私はエラーReader Monadタイプの動作を明確にしてください
<interactive>:21:11:
No instance for (MonadReader a0 m0) arising from a use of `reader'
The type variables `m0', `a0' are ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Note: there are several potential instances:
instance MonadReader r' m =>
MonadReader r' (Control.Monad.Trans.Cont.ContT r m)
-- Defined in `Control.Monad.Reader.Class'
instance MonadReader r ((->) r)
-- Defined in `Control.Monad.Reader.Class'
instance (Control.Monad.Trans.Error.Error e, MonadReader r m) =>
MonadReader r (Control.Monad.Trans.Error.ErrorT e m)
-- Defined in `Control.Monad.Reader.Class'
...plus 10 others
In the expression: reader (\ x -> x + 10)
In an equation for `myR': myR = reader (\ x -> x + 10)
<interactive>:21:27:
No instance for (Num a0) arising from a use of `+'
The type variable `a0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Note: there are several potential instances:
instance Num Double -- Defined in `GHC.Float'
instance Num Float -- Defined in `GHC.Float'
instance Integral a => Num (GHC.Real.Ratio a)
-- Defined in `GHC.Real'
...plus three others
In the expression: x + 10
In the first argument of `reader', namely `(\ x -> x + 10)'
In the expression: reader (\ x -> x + 10)
を参照してください例えば
let myR = reader (\x -> x + 10)
、リーダーを構築しようとしていたときに、私は型シグネチャを追加すべきであることは明らかである:
let myR = reader (\x -> x + 10) :: Reader Int Int
これは動作しますが、どのように私ができます読者reader :: MonadReader r m => (r -> a) -> m a
のこの定義から、私はそれをReader Int Int
と定義するべきであることを暗示していますか?
(たぶんの場合は、例えばlet m5 = return 5 :: Maybe Int
それはたぶん、1種類のパラメータを持っている、おそらくのため、私のために明確であるように思わ、私はわからない)
コンテキストとは何ですか?私は 'myR ::(Num a、MonadReader a m)=> m a'というGHCiプロンプトで' let myR =(\ x - > x + 10) 'を書くことができ、' myR 5'は15と評価されます。 – chepner