[GHC] #8477: Allow inferring ambiguous types

#8477: Allow inferring ambiguous types ------------------------------------+------------------------------------- Reporter: aavogt | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.6.3 Keywords: | Operating System: Unknown/Multiple Architecture: Unknown/Multiple | Type of failure: None/Unknown Difficulty: Unknown | Test Case: Blocked By: | Blocking: Related Tickets: 8390 | ------------------------------------+------------------------------------- Previous versions of ghc could infer types that are potentially ambiguous. This allowed things like http://okmij.org/ftp/Haskell/TypeLambdaVal.hs or assigning keyword args functions to variables http://code.haskell.org/~aavogt/HList/docs/HList/Data-HList-Keyword.html. Presently (7.6 and 7.7), you need to write a type signature yourself to allow such definitions, which in these situations doubles the amount of code to write. For the keyword case, the workaround is to use the kw function to apply the keyword function instead of ordinary function application. More related discussion http://www.haskell.org/pipermail/glasgow-haskell- users/2013-October/024404.html -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8477 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8477: Allow inferring ambiguous types -------------------------------------+------------------------------------ Reporter: aavogt | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.6.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Unknown/Multiple Type of failure: None/Unknown | Difficulty: Unknown Test Case: | Blocked By: Blocking: | Related Tickets: 8390 -------------------------------------+------------------------------------ Comment (by simonpj): Are you asking for `-XAllowAmbiguousTypes`? HEAD implements such a flag. Does it do what you want? -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8477#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8477: Allow inferring ambiguous types -------------------------------------+------------------------------------ Reporter: aavogt | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.6.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Unknown/Multiple Type of failure: None/Unknown | Difficulty: Unknown Test Case: | Blocked By: Blocking: | Related Tickets: 8390 -------------------------------------+------------------------------------ Comment (by aavogt): I am asking for more than -XAllowAmbiguousTypes. I think it could be possible to have ghc accept the request.hs without having to write a type signature (which in turn needs -XAllowAmbiguousTypes and -XScopedTypeVariables). -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8477#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8477: Allow inferring ambiguous types -------------------------------------+------------------------------------ Reporter: aavogt | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.6.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Unknown/Multiple Type of failure: None/Unknown | Difficulty: Unknown Test Case: | Blocked By: Blocking: | Related Tickets: 8390 -------------------------------------+------------------------------------ Comment (by simonpj): I'm dubious about this * Accepting ''inferred'' ambiguous types is a recipe for planting land- mines that will only show up later. Requiring a type signature in such cases seems quite reasonable to me. * I tried [http://okmij.org/ftp/Haskell/TypeLambdaVal.hs]. The instance on line 199 seems problematic; it fails both the ambiguity check and the coverage condition. However I realise that the ambiguity check on the instance declaration should really subsume the (liberal) coverage condition. I'll fix that. * Then the `TypeLambdaVal` example gives {{{ T8477.hs:218:1: Could not deduce (Apply (HIF' (b0 -> HNothing) (HList (a0 :* xs0) -> HJust a0)) (HBool b, x) res) arising from the ambiguity check for ‛hsafe_head’ from the context (Apply HNull x (HBool b), Apply (HIF' (b1 -> HNothing) (HList (a :* xs) -> HJust a)) (HBool b, x) res) bound by the inferred type for ‛hsafe_head’: (Apply HNull x (HBool b), Apply (HIF' (b1 -> HNothing) (HList (a :* xs) -> HJust a)) (HBool b, x) res) => x -> res at T8477.hs:(218,1)-(221,24) The type variables ‛b0’, ‛a0’, ‛xs0’ are ambiguous When checking that ‛hsafe_head’ has the inferred type ‛forall x res b b1 a xs. (Apply HNull x (HBool b), Apply (HIF' (b1 -> HNothing) (HList (a :* xs) -> HJust a)) (HBool b, x) res) => x -> res’ Probable cause: the inferred type is ambiguous }}} and one other. Indeed, writing out that type may be a bit onerous, but it really does look ambiguous to me! So I'm inclined to "won't fix" for now. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8477#comment:3 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8477: Allow inferring ambiguous types -------------------------------------+------------------------------------ Reporter: aavogt | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.6.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Unknown/Multiple Type of failure: None/Unknown | Difficulty: Unknown Test Case: | Blocked By: Blocking: | Related Tickets: 8390 -------------------------------------+------------------------------------ Comment (by simonpj): PS: my second bullet was wrong. The ambiguity check for instance declarations doesn't subsume the coverage check. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8477#comment:4 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

showRead = show . read }}} (with `-XAllowAmbiguousTypes`) we are told that term is ill-typed because of a failed constraint due to an ambiguous type variable. But, there is a
#8477: Allow inferring ambiguous types -------------------------------------+------------------------------------ Reporter: aavogt | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.6.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Unknown/Multiple Type of failure: None/Unknown | Difficulty: Unknown Test Case: | Blocked By: Blocking: | Related Tickets: 8390 -------------------------------------+------------------------------------ Changes (by simonpj): * cc: goldfire, diatchki (added) Comment: Richard Eisenberg (goldfire) writes: I'm here at MSR Redmond talking with Nik and Daan about type inference, among other things. We're wondering about ambiguity checks. When we type check {{{ perfectly good type for this: `(Show a, Read a) => String -> String`. This type is, of course, ambiguous, but I thought we wanted to ''allow'' ambiguous types. Our only theory is that GHC has to check the RHS ''twice'' -- once to infer a type and then once to check that it has the inferred type. It is during this second pass that we discover that the inferred type is no good. What has intrigued us is that {{{
showRead :: forall a. (Show a, Read a) => String -> String showRead = show . (read :: String -> a) }}} works just fine (with `-XAllowAmbiguousTypes` and `-XScopedTypeVariables`). So, the analysis is ''not'' just looking at the types; it has to be examining the RHS also.
-- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8477#comment:5 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8477: Allow inferring ambiguous types -------------------------------------+------------------------------------ Reporter: aavogt | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.6.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Unknown/Multiple Type of failure: None/Unknown | Difficulty: Unknown Test Case: | Blocked By: Blocking: | Related Tickets: 8390 -------------------------------------+------------------------------------ Comment (by simonpj): Like I say, I'm doubtful about allowing functions to get ambiguous ''inferred'' types, because that plants hidden land mines for those who call the function. If you give a signature, you are explicitly asking for this particular land mine. If you want it to have an ambiguous type, say so! It's like a per-declaration pragma, if you like. It would not be impossible to make `-XAllowAmbiguousTypes` work for inferred types, but actually it would not be entirely straightforward; see `Note [Impedence matching]` in `TcBinds`. Simon -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8477#comment:6 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8477: Allow inferring ambiguous types -------------------------------------+------------------------------------- Reporter: aavogt | Owner: Type: feature | Status: new request | Milestone: Priority: normal | Version: 7.6.3 Component: Compiler | Keywords: (Type checker) | Architecture: Unknown/Multiple Resolution: | Difficulty: Unknown Operating System: | Blocked By: Unknown/Multiple | Related Tickets: 8390 Type of failure: | None/Unknown | Test Case: | Blocking: | Differential Revisions: | -------------------------------------+------------------------------------- Changes (by thomie): * component: Compiler => Compiler (Type checker) -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8477#comment:7 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC