Opened 12 months ago

Last modified 12 months ago

#15661 new bug

Nullary constraint in GHCi breaks `:t` command

Reported by: taktoa Owned by:
Priority: normal Milestone: 8.6.1
Component: GHCi Version: 8.4.3
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description

If you create a value whose type has a nullary constraint (i.e.: a constraint that does not reference any of the type variables in scope) and then try to run :t on it, GHCi attempts to run instance resolution and fails before printing the type.

Expected output:

GHCi, version 8.4.3: http://www.haskell.org/ghc/  :? for help
Prelude> :set -XFlexibleContexts
Prelude> data Foo = Foo
Prelude> let x :: (Show Foo) => () ; x = ()
Prelude> :t x
x :: Show Foo => ()

Actual output:

GHCi, version 8.4.3: http://www.haskell.org/ghc/  :? for help
Prelude> :set -XFlexibleContexts
Prelude> data Foo = Foo
Prelude> let x :: (Show Foo) => () ; x = ()
Prelude> :t x

<interactive>:1:1: error:
    No instance for (Show Foo) arising from a use of ‘x’

Change History (2)

comment:1 Changed 12 months ago by simonpj

This behaviour is just what you'd expect. Remember: :type <expression> takes an arbitrary expression (not just a variable), finds its most general type, and shows that type. Here the expression happens to be just a single variable x. Fine, so we instantiate x, which gives rise to a "wanted" (Show Foo). Then we attempt to solve and generalise, but (Show Foo) is not soluble. Hence the error.

If you just want to see x's type, use :info, thus

ghci> :info x
x :: Show Foo => () 	-- Defined at <interactive>:3:26

I think everything seems fine here. But it clearly wasn't what you expected. Does my explanation help?

comment:2 Changed 12 months ago by RyanGlScott

Alternatively, you can use :type +v x, which finds the type signature without solving and generalizing.

Note: See TracTickets for help on using tickets.