[GHC] #14836: GHC fails to infer implied superclass constraint

#14836: GHC fails to infer implied superclass constraint -------------------------------------+------------------------------------- Reporter: crockeea | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.2.2 Keywords: | Operating System: Unknown/Multiple Architecture: | Type of failure: GHC rejects Unknown/Multiple | valid program Test Case: | Blocked By: Blocking: | Related Tickets: Differential Rev(s): | Wiki Page: -------------------------------------+------------------------------------- The following code should compile, but does not. {{{ {-# LANGUAGE FlexibleInstances, UndecidableInstances #-} module Bug where class (Monad m) => RequiresMonad m where class (Monad m) => ImpliesMonad m where instance (ImpliesMonad m) => RequiresMonad m where }}} The basic idea is that I put the constraint `ImpliesMonad m` on the `RequiresMonad` instance, which does in fact imply `Monad m`. However, GHC complains: {{{ • Could not deduce (Monad m) arising from the superclasses of an instance declaration from the context: ImpliesMonad m bound by the instance declaration at Bug.hs:9:10-44 Possible fix: add (Monad m) to the context of the instance declaration • In the instance declaration for ‘RequiresMonad m’ }}} Of course, GHC is perfectly happy if I replace the instance constraint `ImpliesMonad m` with `Monad m`. Possibly related to #10338 or #11948. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14836 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14836: GHC fails to infer implied superclass constraint -------------------------------------+------------------------------------- Reporter: crockeea | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.2.2 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Type of failure: GHC rejects | Unknown/Multiple valid program | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by crockeea): Oddly, if I involve `MultiParamTypeClasses`, I can make this work in some cases, but GHC refuses others: {{{ {-# LANGUAGE FlexibleContexts, FlexibleInstances, MultiParamTypeClasses, UndecidableInstances #-} module Bug where import Control.Monad.Reader import Control.Monad.IO.Class class (Monad m) => Message msg m where class (Monad m) => ImpliesMonad m where data Foo --instance (ImpliesMonad m) => Message Foo m where -- compiles --instance (MonadIO m) => Message Foo m where -- compiles --instance (MonadReader Bool m) => Message Foo m where -- does NOT compile }}} I've defined three similar instances above. The first two instances are accepted by GHC (as, in my opinion, they should be), but the third instance is reject (`could not deduce (Monad m)`). -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14836#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

The compiler is now a bit more conservative in solving constraints
#14836: GHC fails to infer implied superclass constraint -------------------------------------+------------------------------------- Reporter: crockeea | Owner: (none) Type: bug | Status: closed Priority: normal | Milestone: Component: Compiler | Version: 8.2.2 Resolution: duplicate | Keywords: Operating System: Unknown/Multiple | Architecture: Type of failure: GHC rejects | Unknown/Multiple valid program | Test Case: Blocked By: | Blocking: Related Tickets: #14417 | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by RyanGlScott): * status: new => closed * resolution: => duplicate * related: => #14417 Comment: This is expected behavior. From the [https://downloads.haskell.org/~ghc/8.0.1/docs/html/users_guide/8.0.1-notes.h... GHC 8.0.1 release notes]: previously provided by superclasses (see Trac #11762). For instance, consider this program,:
{{{#!hs {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE UndecidableInstances #-}
class Super a class (Super a) => Left a class (Super a) => Right a instance (Left a) => Right a -- this is now an error }}}
GHC now rejects this instance, claiming it cannot deduce the `Super a`
superclass constraint of the `Right` typeclass. This stands in contrast to previous releases, which would accept this declaration, using the `Super a` constraint implied by the `Left a` constraint. To fix this simply add the needed superclass constraint explicitly,
{{{#!hs instance (Left a, Super a) => Right a }}}
In other words, it's a limitation that was imposed after the introduction of `UndecidableSuperClasses`. So from GHC's perspective, the correct thing to do here is to use this instance declaration instead: {{{#!hs instance (Monad m) => RequiresMonad m where }}} Closing as a duplicate of #14417. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14836#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC