Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6695897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:16:38+00:00 2026-05-26T06:16:38+00:00

This code does not compile in GHC 7.0.3: import System.IO main = do z

  • 0

This code does not compile in GHC 7.0.3:

import System.IO

main = do
    z <- readLn
    print z

My intention is to read one line from stdin and store it in z, to do more advanced stuff with it later on. Error message looks like:

test.hs:5:9:
    Ambiguous type variable `a0' in the constraints:
      (Show a0) arising from a use of `print' at test.hs:5:9-13
      (Read a0) arising from a use of `readLn' at test.hs:4:14-19
    Probable fix: add a type signature that fixes these type variable(s)
    In a stmt of a 'do' expression: print z
    In the expression:
      do { z <- readLn;
           print z;
           return () }
    In an equation for `main':
        main
          = do { z <- readLn;
                 print z;
                 return () }

Obviously there is something fundamental I haven’t understood yet; please explain to me why it doesn’t work and how to fix it.

EDIT1: I fixed the compile error by changing print z to putStrLn z, so GHC understands that I want to read a string. But when I run the program, I get a runtime error which I can’t understand:

$ ./test
hello!
test: user error (Prelude.readIO: no parse)
$

I just typed “hello!” and then enter. Note that I’m running x86_64 GHC on OS X, which is considered unstable.

EDIT2: I changed readLn to getLine and it magically works for no reason. I would like to know why, but I’m happy it works.

Final code:

import System.IO

main = do
    z <- getLine
    print z
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:16:39+00:00Added an answer on May 26, 2026 at 6:16 am

    readLn as the type: Read a => IO a. It reads a line from the user, and then parses the string into type a. What is type a? It is whatever you want (as long as it is an instance of Read). For example:

    readAInt :: IO Int
    readAInt = readLn
    
    readABool :: IO Bool
    readABool = readLn
    

    print has the type Show a => a -> IO (). It takes a type that is an instance of Show, and prints it out. For example, to print True, you can use print True. To print the Int 42, you can use print 42.


    In your example, you are using print and readLn together. This doesn’t work, as haskell can’t figure out what type readLn should return. print can take any type that is showable, so it doesn’t restrict to one what type would be returned. This makes the return type of readLn ambiguous as haskell can’t figure out the type. This is what the error message is saying.


    What you probably what it to store just the string being entered by the user, rather than reading it into your own type. You can do this with getLine, which has the type getLine :: IO String. Similarily you can use putStrLn instead of print to just print a String. putStrLn has the type String -> IO ().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code does not compile for me on GCC version 4.3.2 (Debian 4.3.2-1.1) main()
Why this code does not compile (Cygwin)? #include <vector> template <class Ttile> class Tilemap
This code does not seem to compile, I just need to write something to
I just found my code like this does not compile right? Is there any
I cannot understand why this piece of code does not compile: namespace A {
Can one of you explain why the following piece of code does not compile?
The following code does not compile ... any idea why? Is this illegal C++?
This code does not compile. public class Diamond { public static void diamondOfAsterisks(String *
I'm struggling a bit to understand why this code snippet does not compile. #include
This code does not compile: public T Get<T>() { T result = default(T); if(typeof(T)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.