[GHC] #8463: Inferred types problem

#8463: Inferred types problem ------------------------------------+------------------------------------- Reporter: danilo2 | Owner: Type: bug | 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: | ------------------------------------+------------------------------------- I've talked about this issue on Haskell irc and nobody knows why types are not automatically inferred in this example. Lets consider following code: {{{ data X a = X { method1 :: a } deriving(Show) cons_X = X { method1 = (\a b -> (a,b)) } f x = method1 x :: a -> b -> (a, b) main = do let x = cons_X let a = f x return () }}} It works if we do not write the explicit type signature. What is interesting, If we write it wrong, like {{{ f x = method1 x :: Int }}} compiler tells us: {{{ Couldn't match type `t0 -> t1 -> (t0, t1)' with `Int' }}} It does not work if we even specify explicit f type: {{{ f :: X (a -> b -> (a, b)) -> (a -> b -> (a, b)) }}} -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8463 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8463: Inferred types problem -------------------------------------+------------------------------------ Reporter: danilo2 | Owner: Type: bug | Status: closed Priority: normal | Milestone: Component: Compiler | Version: 7.6.3 Resolution: invalid | Keywords: Operating System: Unknown/Multiple | Architecture: Unknown/Multiple Type of failure: None/Unknown | Difficulty: Unknown Test Case: | Blocked By: Blocking: | Related Tickets: -------------------------------------+------------------------------------ Changes (by kosmikus): * status: new => closed * resolution: => invalid Comment: I don't see incorrect behaviour here. Your type annotation is interpreted as being universally quantified: {{{ f x = method1 x :: a -> b -> (a, b) }}} This says that the result of this function is of type `forall a. a -> b -> (a, b)` independently of what the input is, but this isn't true. You need to refer to the type variables introduced by `x`. You can resolve this in various ways: 1. Just use a type signature: {{{ f :: X (a -> b -> (a, b)) -> a -> b -> (a, b) f x = method1 x }}} 2. Enable `ScopedTypeVariables` and annotate the pattern: {{{ f (x :: X (a -> b -> (a, b))) = method1 x :: a -> b -> (a, b) }}} 3. Enable `ScopedTypeVariables` and reuse the type variables from the type signature: {{{ f :: forall a b. X (a -> b -> (a, b)) -> a -> b -> (a, b) f x = method1 x :: a -> b -> (a, b) }}} -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8463#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC