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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:35:13+00:00 2026-05-26T12:35:13+00:00

For example I want to make a type MyType of integer triples. But not

  • 0

For example I want to make a type MyType of integer triples. But not just Cartesian product of three Integer, I want the type to represent all (x, y, z) such that x + y + z = 5.

How do I do that? Except of using just (x, y) since z = 5 - x - y.

And the same question if I have three constructors A, B, C and the type should be all (A x, B y, C z) such that x + y + z = 5.

  • 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-26T12:35:14+00:00Added an answer on May 26, 2026 at 12:35 pm

    I think the trick here is that you don’t enforce it on the type-level, you use “smart constructors”: i.e. only allow creation of such “tuples” via a function that generates such values:

    module Test(MyType,x,y,z,createMyType) where
    
    data MyType = MT { x :: Int, y :: Int, z :: Int }
    
    createMyType :: Int -> Int -> MyType
    createMyType myX myY = MT { x = myX, y = myY, z = 5 - myX - myY }
    

    If you want to generate all possible such values, then you can write a function to do so, either with provided or specified bounds.

    It may very well be possible to use type-level Church Numerals or some such so as to enforce creation of these, but it’s almost definitely too much work for what you probably want/need.

    This might not be what you want (i.e. “Except of using just (x, y) since z = 5 – x – y”) but it makes more sense than trying to have some kind of enforced restriction on the type level for allowing valid values.

    Types can ensure the correct “type” of value (no pun intended); to ensure validity of values you hide the constructor and only allow creation via approved functions that guarantee any invariants you require.

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

Sidebar

Related Questions

How can I make an enumeration of a complex type? For example, I want
I want to make the textbox allow only three digits and three decimals.For example
For example, I may want to make create a div.Style1 block where all <img
For example I want to make my own Boolean type and call it Bool.
I have a virtual path (example: ~/Images/Banner.jpg) and I want to make that an
for example : I want to remove all highlighted tags alt text http://shup.com/Shup/299976/110220132930-My-Desktop.png
Example: Suppose in the following example I want to match strings that do not
Using the SQLAlchemy ORM, I want to make sure values are the right type
I want to make it that for a certain file type, the icon is
I have an object of the type System.Drawing.Image and want to make every pixel

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.