Skip to content

ghci unexpectedly instantiates type variables when using the do-notation at the prompt

Summary

For unknown reasons, it seems to be impossible to create a polymorphic value using the x <- e pattern in ghci. It will either

  • turn a variable into Any, if it is without any constraint
  • turn a variable into a type specified by a default-declaration
  • throw an Ambiguous type variable error

Steps to reproduce

GHCi, version 9.1.20201219: https://www.haskell.org/ghc/  :? for help
ghci> :set +t
ghci> x <- return id
x :: GHC.Types.Any -> GHC.Types.Any
ghci> x <- return 0.0
x :: Double
ghci> x <- return 0
x :: Integer
ghci> x <- return mempty

<interactive>:5:13: error:
    * Ambiguous type variable `a0' arising from a use of `mempty'
      prevents the constraint `(Monoid a0)' from being solved.
      Probable fix: use a type annotation to specify what `a0' should be.
      These potential instances exist:
        instance Monoid a => Monoid (IO a) -- Defined in `GHC.Base'
        instance Monoid Ordering -- Defined in `GHC.Base'
        instance Semigroup a => Monoid (Maybe a) -- Defined in `GHC.Base'
        ...plus 7 others
        (use -fprint-potential-instances to see them all)
    * In the first argument of `return', namely `mempty'
      In the first argument of `GHC.GHCi.ghciStepIO ::
                                  IO a -> IO a', namely
        `(return mempty)'
      In a stmt of an interactive GHCi command:
        x <- GHC.GHCi.ghciStepIO :: IO a -> IO a (return mempty)
ghci> x = id
x :: a -> a
ghci> :q
Leaving GHCi.

Expected behavior

I expect the result similar to that when using x = e.

Environment

  • GHC version used: A recent build artifact and ghc 8.10.1
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information