readLn fails in haskell playground?

I tried to run in playground my original code where the values of year, month and day entered in terminal are read by readLn.

It works in playground only using fixed values of year, month and day.

As I added this to read a year value

main = do
         print    "Year: "
         year :: Int <- readLn

It claimed this

Main.hs:25:10: error:
    Illegal type signature: ‘Int’
      Type signatures are only allowed in patterns with ScopedTypeVariables
25 |          year :: Int <- readLn
   |          ^^^^^^^^^^^

Removing signature does not help anything.

This works, typewise:

main = do
x ← readLn :: IO Int
print x

However, since programs in the haskell playground are not run interactively, just in “one shot” execution, naturally the result is rather disappointing:

Main: <stdin>: hGetLine: end of file


Ok, maybe side effects like the unknown values of variables are not allowed in playground.

Anyway, playground is fun and good for temporary use on any browser like testing small pieces of code if you happen to miss access to real ghci.

To address your specific issue:

The error you get here is correct; it’s not “standard Haskell” to allow things like (year :: Int) on the left hand side of a bind. That’s not a Haskell Playground thing, you’ll find the same on any computer that doesn’t have certain extensions turned on by default.

That said, the ScopedTypeVariables extension is good and you can feel comfortable enabling it. You can do that for an individual file by adding a language pragma at the top of the file:

{-# LANGUAGE ScopedTypeVariables #-}

main = do
  year :: Int <- readLn

It is strange that the playground doesn’t seem to use the default GHC2021 extension set (which includes ScopedTypeVariables) for compilers that support it (GHC 9.2 and later). @tomsmeding is that intended?


This may be a good feature request. @polarit do you want to submit one on their GitHub (the link is on the playground page)?

Intended no, but the cause is clearly this line here. If I’m not mistaken, just removing that line will be sufficient.

Not currently at a PC, but will fix soon — if not today then tomorrow.

EDIT: done


Yes, I would, if I could. I mean, what should I write to the issue? Is it possible to shear this discussion thread to the author or maintainer of play.haskell ?

Should we just wait until @jaror, @tomsmeding or who ever has made the fix he mentioned?
As I tried to run yesterday again the siplest possible readLn case in play, it didn’t work yet at least with selected GHC 9.25.

It has been fixed now.

1 Like

I have run once again tonight this code in playground.
It stopped with runtime error:

Command exited with code 1.

Main: <stdin>: hGetLine: end of file


"Year: "

Having the language pragma on the top of code has no effect either in ghci or in playground, (i.e. no errors in ghci with or without it.)

Here is an example of the output results of running in ghci without errors.

$ ghci
GHCi, version 9.2.5:  :? for help
ghci> :l exampleReadLines.hs 
[1 of 1] Compiling Main             ( exampleReadLines.hs, interpreted )
Ok, one module loaded.
ghci> main
"Year: "
"Month: "
"Day: "
2023-2-24, "Friday"  Julian date number (JDN) = 2460000

Does the web browser or platform affect how playground works?
I was using the latest Firefox on Linux and macOS 10.14.6

No, that is unfortunately the only thing it can do since you cannot give any input to the program in the playground. The programs you run are just given an empty stdin, as if you had run echo "" | ./my-program locally.

1 Like

Ok, this is clear now.
Any way, play.haskell is an interesting project.
Corresponding Ellie for Elm users,