3

As far as I know, haskell infers the most general possible type, being

integers      = [1,2,3]
integersOrNot = [1,2,3]

routine :: IO ()
routine = do
   let x = map (\i -> i / 2) integersOrNot
   return ()

When I test type of integersOrNot, I get

*Main> :t integersOrNot
integers_ :: [Double]

Given

*Main> :t 1
1 :: Num a => a

Haskell didn't have much options. But how come

*Main> :t integers
integers :: [Integer]

not integers :: (Num a) => [a]? Does that mean Integer is a "default" real type for Num kind?

Ilya Smagin
  • 5,992
  • 11
  • 40
  • 57
  • 9
    https://wiki.haskell.org/MonomorphismRestriction. BTW, Haskell doesn't really “infer types in a lazy manner”, rather it always infers _the most general possible_ type. – leftaroundabout Mar 28 '16 at 21:17
  • @leftaroundabout, only if there is a most general type. I seem to recall that there are some weird cases where it infers something less general even when there is a most general type, but I may be confused or that may only be true with certain GHC extensions. – dfeuer Mar 28 '16 at 21:26
  • 4
    @dfeuer: yeah, it won't infer a more general rank-N type, but it will give the most general rank-1 type (provided you turn off the monomorphism restriction). – leftaroundabout Mar 28 '16 at 21:29
  • 1
    @dfeuer There are some cases where inference fails shown here: http://stackoverflow.com/questions/27067905/when-are-type-signatures-necessary-in-haskell – chi Mar 28 '16 at 22:46

1 Answers1

0

http://wiki.haskell.org/MonomorphismRestriction

as suggested by @leftaroundabout

Ilya Smagin
  • 5,992
  • 11
  • 40
  • 57