Skip to content

Possible regression in GHC 9.0.1-rc1: Compilation error when using mtl

While testing Agda with GHC 9.0.1-rc1 I found a compilation error when using mtl. The following MVE

{-# LANGUAGE GADTs      #-}
{-# LANGUAGE RankNTypes #-}

module Test where

import Control.Monad.Reader
import Control.Monad.State

data Env n where
  Env :: Ord n =>
    { f :: n -> [n]
    } -> Env n

type DotM n a = Ord n => ReaderT (Env n) (State Int) a

run :: Env n -> DotM n x -> Int
run env@Env{} = flip execState 42 . flip runReaderT env

fails with the following error:

Test.hs:27:5: error:
    • Couldn't match type ‘a’ with ‘()’
      Expected: ReaderT (Env n) IO a
        Actual: ReaderT (Env n) IO ()
      ‘a’ is a rigid type variable bound by
        the type signature for:
          printReaderContent :: forall n a. Show n => DotM n a
        at Test.hs:24:1-40
    • In a stmt of a 'do' block:
        liftIO $ putStrLn ("The Reader Content: " ++ show content)
      In the expression:
        do content <- ask
           liftIO $ putStrLn ("The Reader Content: " ++ show content)
      In an equation for ‘printReaderContent’:
          printReaderContent
            = do content <- ask
                 liftIO $ putStrLn ("The Reader Content: " ++ show content)
    • Relevant bindings include
        printReaderContent :: ReaderT (Env n) IO a (bound at Test.hs:25:1)
   |
27 |     liftIO $ putStrLn ("The Reader Content: " ++ show content)
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Is it the expected behaviour?

In this MVE I can fix the problem by removing the constraint Ord n from the definition of type DotM n a. In the original file, I also need to add the removed constraint to the type of some functions.

The MVE compiles with previous versions of GHC including 8.10.3.

Edited by Andrés Sicard-Ramírez
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information