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 7604649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:58:02+00:00 2026-05-30T23:58:02+00:00

The following program type checks if I specify it on the command line (e.g.

  • 0

The following program type checks if I specify it on the command line (e.g. ghci file.hs):

import Data.Ratio
foo = let x = [1..]
          y = (1%2) + (head x)
       in y

However, if I enter it interactively I’ll get a type error:

Prelude> import Data.Ratio
Prelude Data.Ratio> let x = [1..]
Prelude Data.Ratio> let y = (1%2) + (head x)
<interactive>:1:23:
    Couldn't match expected type `Ratio a0' with actual type `Integer'

It seems that x is being eagerly typed as [Integer] as opposed to the more general (Num t, Enum t) => [t].

Is there anything I can do about that? Are there other situations where interactive mode will differ from batch mode?

  • 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-30T23:58:03+00:00Added an answer on May 30, 2026 at 11:58 pm

    Bindings without arguments, i.e those of the form x = ... are subject to the monomorphism restriction, which means that GHC will try to make it non-polymorphic using whatever type information is available, and falling back on type defaulting to resolve any ambiguities. (Actually, GHCi uses a slightly more permissive set of defaulting rules, but that doesn’t really matter for this question).

    Since there is no other type information available when you write let x = [1..], type defaulting causes the type to be inferred as [Integer], since Integer is the default numeric type.

    There are several ways to solve this problem:

    1. Use a type signature. This always works, but it can sometimes be tedious when working with complex types.

      > let x = [1..] :: [Rational]
      
    2. Write the binding with arguments. This doesn’t apply in your case, but you sometimes see this problem when writing point-free function definitions.

      > let f = (+)
      > :t f
      f :: Integer -> Integer -> Integer
      > let f x y = x + y
      > :t f
      f :: Num a => a -> a -> a
      
    3. Give the type checker more information. In your case we could avoid the problem by writing both bindings in one let statement. GHC can then use the type information from the second binding to correctly infer that x should have the type [Rational].

      > let x = [1..]; y = 1%2 + head x
      > :t x
      x :: [Ratio Integer]
      
    4. Disable the monomorphism restriction. This can have serious performance implications if you were expecting the type of something to be e.g. an Integer, while it is actually a Num a => a, since the latter must be recomputed each time while the former can be shared. This is the main reason why the restriction exists in the first place.

      However, in the interpreter this is typically less of an issue, so the convenience is often worth it.

      > :set -XNoMonomorphismRestriction
      > let x = [1..]
      > :t x
      x :: (Num t, Enum t) => [t]
      

      If you want this on by default, you can add it to your .ghci file.

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

Sidebar

Related Questions

I'm using GHCi 7.0.3 with the following program that implements type-level list: {-# LANGUAGE
Given the following program: import java.io.*; import java.util.*; public class GCTest { public static
I am executing following sample program of httpclient of GET method. import org.apache.commons.httpclient.HttpClient; import
I have a Windows program with the following flow: generated config file -> user
I'm writing a small command-line program that asks the user for polynomials in the
Consider the following short program: #include <type_traits> #include <iostream> using namespace std; template <typename
Consider following program: static void Main (string[] args) { int i; uint ui; i
In following program . I have one doubt. I have declared one global variable
The following program is a basic linked list divided in 3 classes. In the
The following program doesn't build in VS11 beta, gcc 4.5, or clang 3.1 #include

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.