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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:58:35+00:00 2026-06-14T17:58:35+00:00

I was just starting having fun with haskell when I got stuck. I’m trying

  • 0

I was just starting having fun with haskell when I got stuck.

I’m trying to make my new data type (let’s call it MyType) instance of the Read class. Mytype is a type constructor, so it takes another type as parameter. I wanted to write this kind of code

    instance (Read a) => Read (MyType a) where
        readsPrec _ r = [foo (read r :: a ), r]

but it gives me the following error

Could not deduce (Read a2) arising from a use of `read' from the context (Read a).

I thought that since a is Readable I could just infer it, but apparently I’m wrong. Any ideas?

EDIT:
I have changed the previous code to

readsPrec _ r = [foo (read r :: a ), ""]

so if I type: read "myString" :: MyType a it works perfectly fine.
Now I was hoping that if I would use read "myString" within a context, I shouldn’t have to specify the type to read. But the problem is that with

bar (read myString) a

where bar:: MyType a -> a -> MyType a, I got
Ambiguos variable type.

Is it possible to do something like that without getting that kind of error?

I hope it’s clearer now, I’m trying to simplify the code but I hope I didn’t omit anything crucial.

  • 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-06-14T17:58:37+00:00Added an answer on June 14, 2026 at 5:58 pm

    The code actually typechecks if written as

    instance (Read a) => Read (MyType a) where
            readsPrec _ r = [(foo (read r),r)]
    

    if foo has type a -> MyType a. The compiler can figure out from the expected type signature of readsPrec that the call to foo ought to return a MyType a, and hence (by the type of foo), the expression read r ought to have type a.

    But why does it fail when you annotate that with :: a? Because type variables are local to the type signature they appear in. So the a there is totally unrelated to the a in the instance heade and with read r :: a you are actually saying: The expression read r can have any arbitrary type. But an arbitrary type has no Read instance, hence the error message. In the message, the compiler renamed the inner a to a2 to avoid a name clash.

    Your code can work as expected, though, if you add {-# LANGUAGE ScopedTypeVariables #-} to the module header. Now, the a in read r :: a refers to the type of a in the instance header and all goes well.

    Note that you are not using readsPrec correctly, but I guess that is not part of the question.

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

Sidebar

Related Questions

I'm just starting with having fun with Spring Roo... Everything is nice so far.
I'm having a problem getting some data since I'm just starting to use LINQToEntities,
Just starting with Modelica and having trouble understanding how it works. In the below
I am just starting to learn a little about PHP; having trouble with the
I'm just starting my research on a system-wide keyboard replacement app, but I'm having
Just starting out to program in python and I'm having the following issue. I
I am just starting out with network programming and I am having trouble keeping
I'm just starting out with NHibernate and I'm having trouble with running more complex
I'm just starting to work with Silverlight (no WPF experience either) and am having
I'm just starting with jQuery, and having trouble doing something embarrassingly simple: using .load()

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.