[GHC] #15119: Program involving existentials and type class constraint gets rejected

GHC ghc-devs at haskell.org
Fri May 4 12:08:28 UTC 2018


#15119: Program involving existentials and type class constraint gets rejected
-------------------------------------+-------------------------------------
        Reporter:  sgraf             |                Owner:  (none)
            Type:  bug               |               Status:  new
        Priority:  normal            |            Milestone:  8.6.1
       Component:  Compiler (Type    |              Version:  8.2.2
  checker)                           |
      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 RyanGlScott):

 This is indeed expected. `a` is completely ambiguous in your program. The
 only type it ever appears in is `C a`, which is to the left of `=>`, and
 such types are always resolved through instance lookup, so GHC has trouble
 figuring out which particular instance you meant. The only type to the
 right of `=>`, `Int`, does not determine `a` either, so GHC throws up its
 hands and gives up.

 One way to make this program compile is to introduce an additional
 argument which determines `a`, such as in:

 {{{#!hs
 {-# LANGUAGE AllowAmbiguousTypes    #-}
 {-# LANGUAGE RankNTypes             #-}
 {-# LANGUAGE ScopedTypeVariables    #-}
 {-# LANGUAGE TypeApplications       #-}

 module Foo where

 import Data.Proxy

 class C a where
    getInt :: Int

 instance C Char where
    getInt = 42

 f :: (forall a. C a => Proxy a -> Int) -> Bool
 f x = even $ x $ Proxy @Char

 g :: (forall a. C a => Proxy a -> Int) -> Bool
 g = f
 }}}

-- 
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/15119#comment:1>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler


More information about the ghc-tickets mailing list