Differences in type inferences in ghci for two functions
-
19-10-2022 - |
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 valuelistToUse
is inferred to be of typelistToUse :: [Integer] = _
- In
func2
the valueslistToUse
andval
are inferred to be typeslistToUse :: [a] = _
andval :: [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