Haskell: Explicit type declarations in GHCI
On a few occasions I’ve wanted to be able to explicitly define the type of something when trying things out in the Haskell REPL (GHCI) but I didn’t actually realise this was possible until a couple of days ago.
For example say we want to use the http://zvon.org/other/haskell/Outputprelude/read_f.html function to parse an input string into an integer.
We could do this:
> read "1" :: Int
1
But if we just evaluate the function alone and try and assign the result without casting to a type we get an exception:
> let x = read "1"
<interactive>:1:10:
Ambiguous type variable `a0' in the constraint:
(Read a0) arising from a use of `read'
Probable fix: add a type signature that fixes these type variable(s)
In the expression: read "1"
In an equation for `x': x = read "1"
> let x::Int; x = read "1"
> x
1
We can also use it when creating a list of integers to ensure they are of type 'Int' rather than 'Integer' for example:
> let y = [1,2,3]
> :t y
y :: [Integer]
> let y::[Int]; y = [1,2,3]
> :t y
y :: [Int]
It’s a pretty simple thing but I didn’t know it was even possible!
About the author
I'm currently working on short form content at ClickHouse. I publish short 5 minute videos showing how to solve data problems on YouTube @LearnDataWithMark. I previously worked on graph analytics at Neo4j, where I also co-authored the O'Reilly Graph Algorithms Book with Amy Hodler.