Opened 9 years ago

Closed 9 years ago

#3660 closed bug (fixed)

"Var.tcTyVarDetails" exception w/ Assoc. Datatypes and Monad Transformers

Reported by: jfredett Owned by:
Priority: normal Milestone:
Component: Compiler Version: 6.10.4
Keywords: Monad Transformers, Associated Datatypes Cc:
Operating System: Linux Architecture: x86
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description

Summary:

When trying to build a monad stack which involves a state transformer with a constrained state, the newtype which constrains said state (below, it is the Filter newtype) cannot seem to derive something within the list of things it's intended to derive. After some testing I found that changing the MonadState Bool part of the deriving clause to MonadState t (in line with the actual state parameter) made everything work. The Type of Email doesn't matter, I've tested to this effect (replacing Email with data Email = Email and not importing the real version, it still compiles fine in the fixed case, and does not compile in the broken case).

I have not tested on GHC 6.12 yet, I'm still trying to install it. All other system information at the bottom.

By the looks of it, this is a case of GHC not noticing I'm doing something silly, and not reporting something along the lines of the "This isn't polymorphic enough" error.

Broken Code:

type Context = ReaderT Email
type Match t = StateT t IO
type ContextMatch t a = Context (Match t) a

newtype FilterState t => Filter t a = Filter (ContextMatch t a)
      deriving (Functor, Monad, MonadReader Email, MonadState Bool, MonadIO)

class FilterState t where
       data FState t
       deliver :: FState t -> IO ()

Error:

[1 of 3] Compiling Network.HackMail.Email.ParseEmail ( Network/HackMail/Email/ParseEmail.hs, interpreted )
[2 of 3] Compiling Network.HackMail.Email.Email ( Network/HackMail/Email/Email.hs, interpreted )
[3 of 3] Compiling Network.HackMail.Filter.Filter ( Network/HackMail/Filter/Filter.hs, interpreted )
*** Exception: No match in record selector Var.tcTyVarDetails

Fixed Code:

type Context = ReaderT Email
type Match t = StateT t IO
type ContextMatch t a = Context (Match t) a

-- changed `Bool` to `t`.
newtype FilterState t => Filter t a = Filter (ContextMatch t a)
      deriving (Functor, Monad, MonadReader Email, MonadState t, MonadIO)

class FilterState t where
       data FState t
       deliver :: FState t -> IO ()

System Info:

[jfredett@Erdos]$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 6.10.4
[jfredett@Erdos]$ uname -a
Linux Erdos 2.6.31-ARCH #1 SMP PREEMPT Fri Oct 23 11:12:58 CEST 2009 i686 Intel(R) Celeron(R) CPU 3.06GHz GenuineIntel GNU/Linux

(Possibly) Related Tickets include: 3621, 3422 and 2714

Change History (1)

comment:1 Changed 9 years ago by simonpj

difficulty: Unknown
Resolution: fixed
Status: newclosed

Thanks. I had to add some imports thus:

{-# LANGUAGE GeneralizedNewtypeDeriving, TypeFamilies #-}
module Foo where
import Control.Monad.State
import Control.Monad.Reader

data Email = Email
type Context = ReaderT Email
type Match t = StateT t IO
type ContextMatch t a = Context (Match t) a

newtype FilterState t => Filter t a = Filter (ContextMatch t a)
      deriving (Functor, Monad, MonadReader Email, MonadState Bool,  MonadIO)

class FilterState t where
        data FState t
        deliver :: FState t -> IO ()

Happily this works ok in 6.12, and HEAD, saying

Foo.hs:12:51:
    Couldn't match expected type `Bool' against inferred type `t'
      `t' is a rigid type variable bound by
          the instance declaration at Foo.hs:11:32
    When using functional dependencies to combine
      MonadState s (StateT s m),
        arising from the dependency `m -> s'
        in the instance declaration at <no location info>
      MonadState Bool (StateT t IO),
        arising from the instance declaration at Foo.hs:12:51-65
    When checking the super-classes of an instance declaration
    In the instance declaration for `MonadState Bool (Filter t)'

So I'll close the bug as fixed. Thank you for boiling it down.

Simon

Note: See TracTickets for help on using tickets.