[GHC] #14824: automatically select instance

#14824: automatically select instance -------------------------------------+------------------------------------- Reporter: zaoqi | Owner: (none) Type: feature | Status: new request | Priority: normal | Milestone: Component: Compiler | Version: 8.2.2 Keywords: | Operating System: Unknown/Multiple Architecture: | Type of failure: None/Unknown Unknown/Multiple | Test Case: | Blocked By: Blocking: | Related Tickets: Differential Rev(s): | Wiki Page: -------------------------------------+------------------------------------- There is {{{#!hs class C a b where m :: [a] -> b instance C x0 x0 instance C x1 x2 instance C x0 x3 }}} x0 x1 x2 x3 is Types. FunctionalDependencies can't work. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14824 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14824: automatically select instance -------------------------------------+------------------------------------- Reporter: zaoqi | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.2.2 Resolution: | 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: | -------------------------------------+------------------------------------- Description changed by zaoqi: Old description:
There is {{{#!hs class C a b where m :: [a] -> b instance C x0 x0 instance C x1 x2 instance C x0 x3 }}} x0 x1 x2 x3 is Types. FunctionalDependencies can't work.
New description: There is {{{#!hs class C a b where m :: [a] -> b instance C x0 x0 instance C x1 x2 instance C x0 x3 }}} x0 x1 x2 x3 are Types. FunctionalDependencies can't work. -- -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14824#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14824: automatically select instance -------------------------------------+------------------------------------- Reporter: zaoqi | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.2.2 Resolution: | 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: | -------------------------------------+------------------------------------- Comment (by AntC): I'm afraid we'll need a bit of elaboration to know what this means. What do you expect to see? Your instance declarations do not give implementations for method `m`. There are no `FunctionalDependencies` so all of those instances are valid. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14824#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14824: automatically select instance -------------------------------------+------------------------------------- Reporter: zaoqi | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.2.2 Resolution: | 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: | -------------------------------------+------------------------------------- Comment (by zaoqi): Replying to [comment:2 AntC]:
I'm afraid we'll need a bit of elaboration to know what this means.
What do you expect to see? Your instance declarations do not give implementations for method `m`.
There are no `FunctionalDependencies` so all of those instances are valid. If I write `b -> a` in that, `Type inference` can't work.
I can't write `a -> b`. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14824#comment:3 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14824: automatically select instance -------------------------------------+------------------------------------- Reporter: zaoqi | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.2.2 Resolution: | 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: | -------------------------------------+------------------------------------- Comment (by zaoqi): Replying to [comment:3 zaoqi]:
Replying to [comment:2 AntC]:
I'm afraid we'll need a bit of elaboration to know what this means.
What do you expect to see? Your instance declarations do not give implementations for method `m`.
There are no `FunctionalDependencies` so all of those instances are valid. If I write `b -> a` in that, `Type inference` can't work correctly.
I can't write `a -> b`.
-- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14824#comment:4 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14824: automatically select instance -------------------------------------+------------------------------------- Reporter: zaoqi | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.2.2 Resolution: | 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: | -------------------------------------+------------------------------------- Comment (by AntC): Perhaps (I'm just guessing) when you say
x0 x1 x2 x3 are Types.
You mean not those are Types, but those are type ''variables''? Are you getting error messages? What are they? What flag settings do you have? (Have you set `OverlappingInstances`?) When you say
I can't write `a -> b`.
''Where'' can't you write that? Please give the full code; and the error messages. So far I can't see any "new feature" you're requesting. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14824#comment:5 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14824: automatically select instance -------------------------------------+------------------------------------- Reporter: zaoqi | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.2.2 Resolution: | 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: | -------------------------------------+------------------------------------- Description changed by zaoqi: Old description:
There is {{{#!hs class C a b where m :: [a] -> b instance C x0 x0 instance C x1 x2 instance C x0 x3 }}} x0 x1 x2 x3 are Types. FunctionalDependencies can't work.
New description: There is {{{#!hs class C a b where m :: [a] -> b instance C X0 X0 instance C X1 X2 instance C X0 X3 }}} FunctionalDependencies can't work. -- -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14824#comment:6 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14824: automatically select instance -------------------------------------+------------------------------------- Reporter: zaoqi | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.2.2 Resolution: | 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: | -------------------------------------+------------------------------------- Description changed by zaoqi: Old description:
There is {{{#!hs class C a b where m :: [a] -> b instance C X0 X0 instance C X1 X2 instance C X0 X3 }}} FunctionalDependencies can't work.
New description: There is {{{#!hs {-# LANGUAGE OverlappingInstances, MultiParamTypeClasses #-} data X0 = X0 deriving Show data X1 = X1 deriving Show data X2 = X2 deriving Show data X3 = X3 deriving Show class C a b where m :: [a] -> b instance C X0 X0 where m _ = X0 instance C X1 X2 where m _ = X2 instance C X0 X3 where m _ = X3 }}} X0 X1 X2 X3 is 4 types in real program. FunctionalDependencies can't work. {{{ *Main> m [X1] <interactive>:6:1: error: • Ambiguous type variable ‘a0’ arising from a use of ‘print’ prevents the constraint ‘(Show a0)’ from being solved. Probable fix: use a type annotation to specify what ‘a0’ should be. These potential instances exist: instance Show Ordering -- Defined in ‘GHC.Show’ instance Show Integer -- Defined in ‘GHC.Show’ instance [overlap ok] [safe] Show X0 -- Defined at a.hs:2:23 ...plus 26 others ...plus 11 instances involving out-of-scope types (use -fprint-potential-instances to see them all) • In a stmt of an interactive GHCi command: print it }}} -- -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14824#comment:7 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14824: automatically select instance -------------------------------------+------------------------------------- Reporter: zaoqi | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.2.2 Resolution: | 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: | -------------------------------------+------------------------------------- Comment (by zaoqi): Replying to [comment:5 AntC]:
Perhaps (I'm just guessing) when you say
x0 x1 x2 x3 are Types.
You mean not those are Types, but those are type ''variables''?
Are you getting error messages? What are they? What flag settings do you have? (Have you set `OverlappingInstances`?)
When you say
I can't write `a -> b`.
''Where'' can't you write that? Please give the full code; and the error messages.
So far I can't see any "new feature" you're requesting. I edited.
-- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14824#comment:8 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14824: automatically select instance -------------------------------------+------------------------------------- Reporter: zaoqi | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.2.2 Resolution: | 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: | -------------------------------------+------------------------------------- Comment (by AntC): Thank you zaoqi. The ticket is now very different to your first post. GHC is behaving as I would expect. It doesn't know which instance of `C` to select for the invocation of `m`. That's because it doesn't know the type of the result for `m [X1]`. You could go: {{{ *Main> m [X1] :: X2 }}} And get result `X2`, because GHC can select `instance C X1 X2`. That's similar to `read "5" :: Int` vs `read "5" :: Float` to disambiguate number formats. GHC does not automatically select an instance just because it seems to be the only one that fits: maybe there is another instance declared in another module that fits just as well. I could tell you about `FunctionalDependencies` combined with `OverlappingInstances` and `UndecidableInstances`. But first consider whether Closed `TypeFamilies` will provide a better approach. I can't see any fault. I can't see any "new feature" needed. Please do not use Trac tickets for help requests. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14824#comment:9 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14824: automatically select instance -------------------------------------+------------------------------------- Reporter: zaoqi | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.2.2 Resolution: | 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: | -------------------------------------+------------------------------------- Comment (by zaoqi): Replying to [comment:9 AntC]:
Thank you zaoqi. The ticket is now very different to your first post.
GHC is behaving as I would expect. It doesn't know which instance of `C` to select for the invocation of `m`. That's because it doesn't know the type of the result for `m [X1]`. You could go:
{{{ *Main> m [X1] :: X2 }}}
And get result `X2`, because GHC can select `instance C X1 X2`.
That's similar to `read "5" :: Int` vs `read "5" :: Float` to disambiguate number formats.
GHC does not automatically select an instance just because it seems to be the only one that fits: maybe there is another instance declared in another module that fits just as well.
I could tell you about `FunctionalDependencies` combined with `OverlappingInstances` and `UndecidableInstances`. But first consider whether Closed `TypeFamilies` will provide a better approach.
I can't see any fault. I can't see any "new feature" needed. Please do not use Trac tickets for help requests. I think they are "new feature" or "bug", I never use Trac tickets for help requests.
-- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14824#comment:10 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14824: automatically select instance -------------------------------------+------------------------------------- Reporter: zaoqi | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.2.2 Resolution: | 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: | -------------------------------------+------------------------------------- Comment (by AntC): See the Haskell 2010 Report section 4.3.4 **Ambiguous types, ...** The type for your expression `m [X1] :: forall a0. C X1 a0 => a0`. You've put the expression at the GHCi `*Main>` prompt, so there's an implicit `print` around it, which gives type {{{ forall a0. (C X1 a0, Show a0) => String }}} Then type var `a0` is ambiguous in the sense in section 4.3.4. That's what the error message is saying. What type do you expect GHC to infer for `a0`? What are the rules of inference that will give that type? Please reference the Haskell 2010 Report and/or GHC User Guide. Explain how the rules will work for expression `m [X0]` -- given the instances for class `C` as above. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14824#comment:11 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC