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.

1 Like

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

4 Likes

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.

1 Like

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
  ...
1 Like

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?

3 Likes

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

1 Like

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

3 Likes

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.

1 Like

It has been fixed now.

2 Likes

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


Command exited with code 1.
Errors

Main: <stdin>: hGetLine: end of file

Output

"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: https://www.haskell.org/ghc/  :? for help
ghci> :l exampleReadLines.hs 
[1 of 1] Compiling Main             ( exampleReadLines.hs, interpreted )
Ok, one module loaded.
ghci> main
"Year: "
2023
"Month: "
2
"Day: "
24
2023-2-24, "Friday"  Julian date number (JDN) = 2460000
ghci>

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

1 Like

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.

2 Likes

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

1 Like