2013-06-27 6 views
5

tym przypadku MonadState, kopiowane z http://book.realworldhaskell.org/read/monad-transformers.html, daje mi błąd z GHC 7.4.2MonadState wystąpienie z Real World Haskell nie kompiluje

instance (MonadState s m) => MonadState s (MaybeT m) where 
    get = lift get 
    put k = lift (put k) 

daje

Illegal instance declaration for `MonadState s (MaybeT m)' 
    (All instance types must be of the form (T a1 ... an) 
    where a1 ... an are *distinct type variables*, 
    and each type variable appears at most once in the instance head. 
    Use -XFlexibleInstances if you want to disable this.) 
In the instance declaration for `MonadState s (MaybeT m)' 

Jeśli dodać XFlexibleInstances, Następnie polecono mi dodać XUndecidableInstances - nie sądzę, żebym potrzebował tych rozszerzeń tutaj. W jaki sposób można skompilować tę instancję?

Odpowiedz

5

Kiedy patrzysz na http://hackage.haskell.org/packages/archive/mtl/latest/doc/html/src/Control-Monad-State-Class.html#MonadState, widzisz, że jest on również używany w "oficjalnej" implementacji, więc myślę, że jest to potrzebne. Komentarz mówi, że ma do czynienia ze stanem pokrycia, co jest wyjaśnione w tych StackOverflow pytania:

W tym przypadku zmienna s nie jest obecny na prawo po stronie, a zależność funktalna idzie od prawej do lewej, więc twoja instancja jest nieważna. (Bez UndecidableInstances)