考える:理解州モナドの `GET`
*Main> let s = state $ \x -> ("foo", x)
*Main> :t s
s :: MonadState s m => m [Char]
を私はget s
を呼び出そうとしましたが、以下のコンパイル時にエラーました:
*Main> :t get
get :: MonadState s m => m s
*Main> let x = get s
<interactive>:95:5: error:
• Non type-variable argument
in the constraint: MonadState t ((->) (m [Char]))
(Use FlexibleContexts to permit this)
• When checking the inferred type
x :: forall s (m :: * -> *) t.
(MonadState s m, MonadState t ((->) (m [Char]))) =>
t
を、私は、その拡張子を追加しました:
*Main> :set -XFlexibleContexts
しかし、まだコンパイルされません:
*Main> let result = get s
<interactive>:9:5: error:
• Could not deduce (MonadState s0 m0)
from the context: (MonadState s m, MonadState t ((->) (m [Char])))
bound by the inferred type for ‘result’:
(MonadState s m, MonadState t ((->) (m [Char]))) => t
at <interactive>:9:5-18
The type variables ‘s0’, ‘m0’ are ambiguous
• In the ambiguity check for the inferred type for ‘result’
To defer the ambiguity check to use sites, enable AllowAmbiguousTypes
When checking the inferred type
result :: forall s (m :: * -> *) t.
(MonadState s m, MonadState t ((->) (m [Char]))) =>
t
なぜコンパイルされていないのか説明してください。