2016-08-30 21 views
1

私は比較的新しい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種類のパラメータを持っている、おそらくのため、私のために明確であるように思わ、私はわからない)

+0

コンテキストとは何ですか?私は 'myR ::(Num a、MonadReader a m)=> m a'というGHCiプロンプトで' let myR =(\ x - > x + 10) 'を書くことができ、' myR 5'は15と評価されます。 – chepner

答えて

1

doesnのInt Haskellのデフォルトすることができるだろうが」既定値はMonadReaderです。

let myR = reader (\x -> x + 10) :: Num a, MonadReader a m => m a 

は、型チェッカーが見つかっ何大まかです、それはIntNumのためのデフォルト設定ルールを適用して取得します。それはMonadReaderのために定義されたデフォルトはありませんが

let myR = reader (\x -> x + 10) :: MonadReader Int m => m Int 

ので、この時点で、あいまいなエラーを返すことがあります。

myRに注釈を付けたくない場合は、プログラムの他の部分に注釈を付けることができます。タイプチェッカーには、最終的にはどちらかを伝える必要があります。MonadReader

関連する問題