Question

I am having trouble understanding the differences in type inferences ghci makes in the example given below. Namely it seems that

  • In func1 the value listToUse is inferred to be of type listToUse :: [Integer] = _
  • In func2 the values listToUse and val are inferred to be types listToUse :: [a] = _ and val :: [a] = _ respectively.

The code is :

module Main where

mylist = 2:[3..]

func1 lst = 
  let listToUse = lst
  in listToUse          -- line 7

func2 lst = 
  let listToUse = lst
      val = 1:listToUse
  in val                -- line 12

main = do
  print $ take 4 $ func1 mylist
  print $ take 4 $ func2 mylist

We can see the inferences by putting breakpoints and stepping through the debugger in the manner shown below :

*Main> :t mylist
mylist :: [Integer]
*Main> :break 7
Breakpoint 0 activated at Test.hs:7:6-14
*Main> :break 12
Breakpoint 1 activated at Test.hs:12:6-8
*Main> main
Stopped at Test.hs:7:6-14
_result :: [Integer] = _
listToUse :: [Integer] = _
[Test.hs:7:6-14] *Main> :continue 
[2,3,4,5]
Stopped at Test.hs:12:6-8
_result :: [a] = _
val :: [a] = _
[Test.hs:12:6-8] *Main> :step
Stopped at Test.hs:11:13-23
_result :: [a] = _
listToUse :: [a] = _

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top