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

  • Home
  • SEARCH
  • 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 3395500
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:17:50+00:00 2026-05-18T04:17:50+00:00

I’d like to read some data which itself specifies the data type to use.

  • 0

I’d like to read some data which itself specifies the data type to use.

For example, let’s assume there may be user inputs like these:

integer pair 1 2
integer triple 1 2 3
real pair 1 2
real triple 1 2 3

and there is a data type to represent it:

data (MValue a) => T a = TP (Pair a) | TT (Triple a)
  deriving (Show, Eq)

data Pair a = Pair a a deriving (Show, Eq)
data Triple a = Triple a a a deriving (Show, Eq)

where the allowed value types have to belong to MValue class:

class (Num a, Read a) => MValue a where
  typename :: a -> String
  readval  :: [String] -> Maybe a

instance MValue Int where
  typename _ = "integer"
  readval [s] = maybeRead s
  readval _   = Nothing

instance MValue Double where
  typename _ = "real"
  readval [s] = maybeRead s
  readval _   = Nothing

maybeRead s =
  case reads s of
    [(x,_)] -> Just x
    _       -> Nothing

I can easily write readers for Pairs and Triples:

readPair (w1:w2:[]) = Pair <$> maybeRead w1 <*> maybeRead w2
readTriple (w1:w2:w3:[]) = Triple <$> maybeRead w1 <*> maybeRead w2 <*> maybeRead w3

The problem is how do I write a polymorphic reader for the entire T a type?:

readT :: (MValue a, Read a) => String -> Maybe (T a)

I want:

  1. The type a is chosen by the caller.
  2. readT should produce Nothing if the user’s input is incompatible with a.
  3. readT should produce Just (T a) if the input is valid.
  4. Numbers should be read as integers or as doubles depending on the input.

A naive implementation

readT :: (MValue a, Read a) => String -> Maybe (T a)
readT s =
  case words s of
    (tp:frm:rest) ->
        if tp /= typename (undefined :: a)
           then Nothing
           else case frm of
             "pair" -> TP <$> readPair rest
             "triple" -> TT <$> readTriple rest
             _ -> Nothing
    _ -> Nothing

gives an error in the line if tp /= typename (undefined :: a):

rd.hs:45:17:
    Ambiguous type variable `a' in the constraint:
      `MValue a' arising from a use of `typename' at rd.hs:45:17-41
    Probable fix: add a type signature that fixes these type variable(s)
Failed, modules loaded: none.

The error goes away if I remove this check, but how can I verify if the user input is compatible with the data type chosen by the caller? A solution might be to have separate readTInt and readTDouble, but I’d like the same readT to work polymorphically the same way as read does.

  • 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-18T04:17:50+00:00Added an answer on May 18, 2026 at 4:17 am

    The problem is that the a in undefined :: a is not the same a as the ones in readT‘s signature. There is a language extension available in GHC that enables that, called “ScopedTypeVariables”. A more portable fix would be to introduce a little extra code to explicitly tie the types together, for example:

    readT :: (MValue a, Read a) => String -> Maybe (T a)
    readT s = result
      where
        result = 
          case words s of
            (tp:frm:rest) ->
                if tp /= typename ((const :: a -> Maybe (T a) -> a) undefined result)
                   then Nothing
                   else case frm of
                     "pair" -> TP <$> readPair rest
                     "triple" -> TT <$> readTriple rest
                     _ -> Nothing
            _ -> Nothing
    

    This is a very quick and dirty modification of your code, and I’m the changes could be made more elegantly, but that should work.

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

Sidebar

Related Questions

I have some data like this: 1 2 3 4 5 9 2 6
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I used javascript for loading a picture on my website depending on which small
I have a JSP page retrieving data and when single or double quotes are
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have text I am displaying in SIlverlight that is coming from a CMS
I want to count how many characters a certain string has in PHP, but
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

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.