Kompilując ten program w GHC:Nie można wywnioskować (m ~ M1)
import Control.Monad
f x = let
g y = let
h z = liftM not x
in h 0
in g 0
otrzymuję błąd:
test.hs:5:21:
Could not deduce (m ~ m1)
from the context (Monad m)
bound by the inferred type of f :: Monad m => m Bool -> m Bool
at test.hs:(3,1)-(7,8)
or from (m Bool ~ m1 Bool, Monad m1)
bound by the inferred type of
h :: (m Bool ~ m1 Bool, Monad m1) => t1 -> m1 Bool
at test.hs:5:5-21
`m' is a rigid type variable bound by
the inferred type of f :: Monad m => m Bool -> m Bool
at test.hs:3:1
`m1' is a rigid type variable bound by
the inferred type of
h :: (m Bool ~ m1 Bool, Monad m1) => t1 -> m1 Bool
at test.hs:5:5
Expected type: m1 Bool
Actual type: m Bool
In the second argument of `liftM', namely `x'
In the expression: liftM not x
In an equation for `h': h z = liftM not x
Dlaczego? Ponadto podanie jawnego podpisu typu dla f
(f :: Monad m => m Bool -> m Bool
) powoduje, że błąd znika. Ale jest to dokładnie ten sam typ, co Haskell automatycznie odpowiada za f
, zgodnie z komunikatem o błędzie!
Ograniczenie monomorfizmu? – MathematicalOrchid
Ograniczenie monomorfizmu dotyczy tylko prostych powiązań wzorów i tutaj ich nie ma. W każdym razie dodanie '-XNoMonomorphismRestriction' nie ma żadnego efektu. – abacabadabacaba
Myślę, że jest to związane z let-uogólnieniem, ponieważ błąd znika z '-XMonoLocalBinds' –