Create new bounded number data types in Haskell
I need to create a data type, that represents only the real numbers between 0 and 1. How would i go about that? Expected Behavior:
0 :: DataType ===> DataType 0
(-1) :: DataType ===> Error
haskell types numbers
|
show 1 more comment
I need to create a data type, that represents only the real numbers between 0 and 1. How would i go about that? Expected Behavior:
0 :: DataType ===> DataType 0
(-1) :: DataType ===> Error
haskell types numbers
6
You could write aninstance Num DataType
and definefromInteger
so that it errors out on invalid values. I'm not sure if that's a good idea, but it's the only way to achieve your goal.
– chi
Nov 22 '18 at 14:38
@chi I actually suspect a transcription error there, given that it's ascribing function types to literals. Not impossible, but I suspect that it's actually about creating a function to behave in such a way.
– Carl
Nov 22 '18 at 14:50
@Carl I'm pretty sure the above->
represents "evaluates to", not function arrow. My comment assumed that interpretation, otherwise the question makes no sense. I'm now replacing them with==>
to avoid confusion.
– chi
Nov 22 '18 at 15:12
1
Beyondinstance Num DataType
, for real numbers we would also needinstance Fractional DataType
, and define a partialfromRational
to reject invalid numbers.
– chi
Nov 22 '18 at 15:16
1
Have you considered using smart constructors. Might by a good idea here. wiki.haskell.org/Smart_constructors
– Luis Morillo
Nov 23 '18 at 8:49
|
show 1 more comment
I need to create a data type, that represents only the real numbers between 0 and 1. How would i go about that? Expected Behavior:
0 :: DataType ===> DataType 0
(-1) :: DataType ===> Error
haskell types numbers
I need to create a data type, that represents only the real numbers between 0 and 1. How would i go about that? Expected Behavior:
0 :: DataType ===> DataType 0
(-1) :: DataType ===> Error
haskell types numbers
haskell types numbers
edited Nov 22 '18 at 15:13
chi
74.1k284140
74.1k284140
asked Nov 22 '18 at 14:27
AgilixAgilix
11711
11711
6
You could write aninstance Num DataType
and definefromInteger
so that it errors out on invalid values. I'm not sure if that's a good idea, but it's the only way to achieve your goal.
– chi
Nov 22 '18 at 14:38
@chi I actually suspect a transcription error there, given that it's ascribing function types to literals. Not impossible, but I suspect that it's actually about creating a function to behave in such a way.
– Carl
Nov 22 '18 at 14:50
@Carl I'm pretty sure the above->
represents "evaluates to", not function arrow. My comment assumed that interpretation, otherwise the question makes no sense. I'm now replacing them with==>
to avoid confusion.
– chi
Nov 22 '18 at 15:12
1
Beyondinstance Num DataType
, for real numbers we would also needinstance Fractional DataType
, and define a partialfromRational
to reject invalid numbers.
– chi
Nov 22 '18 at 15:16
1
Have you considered using smart constructors. Might by a good idea here. wiki.haskell.org/Smart_constructors
– Luis Morillo
Nov 23 '18 at 8:49
|
show 1 more comment
6
You could write aninstance Num DataType
and definefromInteger
so that it errors out on invalid values. I'm not sure if that's a good idea, but it's the only way to achieve your goal.
– chi
Nov 22 '18 at 14:38
@chi I actually suspect a transcription error there, given that it's ascribing function types to literals. Not impossible, but I suspect that it's actually about creating a function to behave in such a way.
– Carl
Nov 22 '18 at 14:50
@Carl I'm pretty sure the above->
represents "evaluates to", not function arrow. My comment assumed that interpretation, otherwise the question makes no sense. I'm now replacing them with==>
to avoid confusion.
– chi
Nov 22 '18 at 15:12
1
Beyondinstance Num DataType
, for real numbers we would also needinstance Fractional DataType
, and define a partialfromRational
to reject invalid numbers.
– chi
Nov 22 '18 at 15:16
1
Have you considered using smart constructors. Might by a good idea here. wiki.haskell.org/Smart_constructors
– Luis Morillo
Nov 23 '18 at 8:49
6
6
You could write an
instance Num DataType
and define fromInteger
so that it errors out on invalid values. I'm not sure if that's a good idea, but it's the only way to achieve your goal.– chi
Nov 22 '18 at 14:38
You could write an
instance Num DataType
and define fromInteger
so that it errors out on invalid values. I'm not sure if that's a good idea, but it's the only way to achieve your goal.– chi
Nov 22 '18 at 14:38
@chi I actually suspect a transcription error there, given that it's ascribing function types to literals. Not impossible, but I suspect that it's actually about creating a function to behave in such a way.
– Carl
Nov 22 '18 at 14:50
@chi I actually suspect a transcription error there, given that it's ascribing function types to literals. Not impossible, but I suspect that it's actually about creating a function to behave in such a way.
– Carl
Nov 22 '18 at 14:50
@Carl I'm pretty sure the above
->
represents "evaluates to", not function arrow. My comment assumed that interpretation, otherwise the question makes no sense. I'm now replacing them with ==>
to avoid confusion.– chi
Nov 22 '18 at 15:12
@Carl I'm pretty sure the above
->
represents "evaluates to", not function arrow. My comment assumed that interpretation, otherwise the question makes no sense. I'm now replacing them with ==>
to avoid confusion.– chi
Nov 22 '18 at 15:12
1
1
Beyond
instance Num DataType
, for real numbers we would also need instance Fractional DataType
, and define a partial fromRational
to reject invalid numbers.– chi
Nov 22 '18 at 15:16
Beyond
instance Num DataType
, for real numbers we would also need instance Fractional DataType
, and define a partial fromRational
to reject invalid numbers.– chi
Nov 22 '18 at 15:16
1
1
Have you considered using smart constructors. Might by a good idea here. wiki.haskell.org/Smart_constructors
– Luis Morillo
Nov 23 '18 at 8:49
Have you considered using smart constructors. Might by a good idea here. wiki.haskell.org/Smart_constructors
– Luis Morillo
Nov 23 '18 at 8:49
|
show 1 more comment
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53433088%2fcreate-new-bounded-number-data-types-in-haskell%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53433088%2fcreate-new-bounded-number-data-types-in-haskell%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
6
You could write an
instance Num DataType
and definefromInteger
so that it errors out on invalid values. I'm not sure if that's a good idea, but it's the only way to achieve your goal.– chi
Nov 22 '18 at 14:38
@chi I actually suspect a transcription error there, given that it's ascribing function types to literals. Not impossible, but I suspect that it's actually about creating a function to behave in such a way.
– Carl
Nov 22 '18 at 14:50
@Carl I'm pretty sure the above
->
represents "evaluates to", not function arrow. My comment assumed that interpretation, otherwise the question makes no sense. I'm now replacing them with==>
to avoid confusion.– chi
Nov 22 '18 at 15:12
1
Beyond
instance Num DataType
, for real numbers we would also needinstance Fractional DataType
, and define a partialfromRational
to reject invalid numbers.– chi
Nov 22 '18 at 15:16
1
Have you considered using smart constructors. Might by a good idea here. wiki.haskell.org/Smart_constructors
– Luis Morillo
Nov 23 '18 at 8:49