
#13946: 自動選擇實例 -------------------------------------+------------------------------------- Reporter: zaoqi | Owner: (none) Type: feature request | Status: closed Priority: normal | Milestone: Component: Compiler | Version: 8.0.2 Resolution: invalid | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by RyanGlScott): * status: new => closed * resolution: => invalid Comment: This is behaving as expected. The issue is that when you type `(b undefined) :: Char`, GHC attempts to look for a `B a Char` instance. But `a` is underdetermined, so GHC cannot successfully find an instance. What you //can// do is use `FunctionalDependencies` to inform GHC that `b` uniquely determines `a`. That is, you can do this: {{{#!hs {-# LANGUAGE FunctionalDependencies #-} {-# LANGUAGE MultiParamTypeClasses #-} class B a b | b -> a where b :: a -> b instance B Int Char where b = error "B Int Char" instance B Int () where b = error "B Int ()" }}} Note the use of `| b -> a` in the class declaration for `B`. This is the notation used to express that `b` uniquely determines `a`. Now, if you try this: {{{#!hs
:t (b undefined) :: Char (b undefined) :: Char :: Char }}}
It works, since when GHC tries to find a `B a Char` instance, it knows that `Char` uniquely determines what `a` should be (in this case, `Int`), so it finds the `B Int Char` and proceeds. I'm going to close this as invalid - please reopen if this didn't answer your question in a satisfactory way. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/13946#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler