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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:58:20+00:00 2026-05-27T13:58:20+00:00

Possible Duplicate: How to create a type bounded within a certain range I have

  • 0

Possible Duplicate:
How to create a type bounded within a certain range

I have the data type:

data Expr = Num Int
          | Expression Expr Operator Expr

In the context of the problem, the numbers that (Num Int) will represent are single digit only. Is there a way to ensure that restriction within the type declaration?

Of course we could define a function to test whether the Expr is valid, but it would be nice to have the type system handle it.

  • 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-27T13:58:21+00:00Added an answer on May 27, 2026 at 1:58 pm

    You can use an abstract data type with a smart constructor:

    newtype Digit = Digit { digitVal :: Int }
      deriving (Eq, Ord, Show)
    
    mkDigit :: Int -> Maybe Digit
    mkDigit n
      | n >= 0 && n < 10 = Just (Digit n)
      | otherwise = Nothing
    

    If you put this in another module and don’t export the Digit constructor, then client code can’t construct values of type Digit outside of the range [0,9], but you have to manually wrap and unwrap it to use it. You could define a Num instance that does modular arithmetic, if that would be helpful; that would also let you use numeric literals to construct Digits. (Similarly for Enum and Bounded.)

    However, this doesn’t ensure that you never try to create an invalid Digit, just that you never do. If you want more assurance, then the manual solution Jan offers is better, at the cost of being less convenient. (And if you define a Num instance for that Digit type, it will end up just as “unsafe”, because you’d be able to write 42 :: Digit thanks to the numeric literal support you’d get.)

    (If you don’t know what newtype is, it’s basically data for data-types with a single, strict field; a newtype wrapper around T will have the same runtime representation as T. It’s basically just an optimisation, so you can pretend it says data for the purpose of understanding this.)

    Edit: For the more theory-oriented, 100% solution, see the rather cramped comment section of this answer.

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

Sidebar

Related Questions

Possible duplicate : comparing-two-arrays I have two NSArray and I'd like to create a
Possible Duplicate: Scala map containing mix type values I have a situation where in
Possible Duplicate: How to determine the class of a generic type? I have a
Possible Duplicate: Is it possible to create a new operator in c#? I was
Possible Duplicate: return type in c++ #include<iostream> int& fun(); int main() { int p=fun();
Possible Duplicate: How to create a file in android? I have a game that
Possible Duplicate: Operator Overloading in PHP I am trying to create a library that
Possible Duplicate: Create a variable in .CSS file for use within that .CSS file
Possible Duplicate: Solution for overloaded operator constraint in .NET generics I have a problem
Possible Duplicate: Create a temporary directory in Java Duplicate: stackoverflow.com/questions/375910 Is there a way

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.