When does Haskell complain about incorrect typing in functions?Getting started with HaskellHaskell types frustrating a simple 'average' functionIs functional GUI programming possible?Large-scale design in Haskell?What does “exists” mean in Haskell type system?What is Weak Head Normal Form?Haskell Typeclass Type Constraints and DeductionHaskell function argument type inference with typeclassesTransitivity of Auto-Specialization in GHCHaskell defined types
Is a fighting a fallen friend with the help of a redeemed villain story too much for one book
Correlation length anisotropy in the 2D Ising model
How can religions be structured in ways that allow inter-faith councils to work?
Polyhedra, Polyhedron, Polytopes and Polygon
Why force the nose of 737 Max down in the first place?
If a 2019 UA artificer has the Repeating Shot infusion on two hand crossbows, can they use two-weapon fighting?
How did the Axis intend to hold the Caucasus?
What is the difference between 1/3, 1/2, and full casters?
How to judge a Ph.D. applicant that arrives "out of thin air"
(2 of 11: Moon-or-Sun) What is Pyramid Cult's Favorite Camera?
Trapped in an ocean Temple in Minecraft?
Why do all my history books divide Chinese history after the Han dynasty?
Request for a Latin phrase as motto "God is highest/supreme"
Converting 8V AC to 8V DC - bridge rectifier gets very hot while idling
Writing a clean implementation of rock–paper–scissors game in C++
What does "see" in "the Holy See" mean?
What are the different qualities of the intervals?
What is the use of で in this sentence?
Where to find an interactive PDF or HTML version of the tex.web documentation?
Unethical behavior : should I report it?
Why is the number of local variables used in a Java bytecode method not the most economical?
How do I stop my characters falling in love?
sfdx force:org:list --all doesn't show all scratch orgs
Is there a wealth gap in Boston where the median net worth of white households is $247,500 while the median net worth for black families was $8?
When does Haskell complain about incorrect typing in functions?
Getting started with HaskellHaskell types frustrating a simple 'average' functionIs functional GUI programming possible?Large-scale design in Haskell?What does “exists” mean in Haskell type system?What is Weak Head Normal Form?Haskell Typeclass Type Constraints and DeductionHaskell function argument type inference with typeclassesTransitivity of Auto-Specialization in GHCHaskell defined types
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am a Haskell newbie trying to wrap my head around type binding in functions and how Haskell enforces it. For example, even though the type for the fst function is fst :: (a, b) -> a, the compiler does not complain for the function fst'. But the compiler complains about type bindings for the function elem'.
fst' :: (a,a) -> a
fst' s = fst s
elem' :: (Eq a, Eq b) => a -> [b] -> Bool
elem' x xs = elem x xs
haskell types typeclass type-mismatch typechecking
New contributor
user4132 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
I am a Haskell newbie trying to wrap my head around type binding in functions and how Haskell enforces it. For example, even though the type for the fst function is fst :: (a, b) -> a, the compiler does not complain for the function fst'. But the compiler complains about type bindings for the function elem'.
fst' :: (a,a) -> a
fst' s = fst s
elem' :: (Eq a, Eq b) => a -> [b] -> Bool
elem' x xs = elem x xs
haskell types typeclass type-mismatch typechecking
New contributor
user4132 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
When you use a function with incorrect types. The reason why it does not complain about the first one is because you here define a "subset" type signature, that is fine. Whereas in the latter, that is not the case.
– Willem Van Onsem
9 hours ago
1
The nicest way I've found to think about this is that you can always make a type more specific than it needs to be, but you can't always make it more general. A spoon can be used for many things, but my spoon I choose to use only on ice-cream ;)
– AJFarmar
6 hours ago
add a comment |
I am a Haskell newbie trying to wrap my head around type binding in functions and how Haskell enforces it. For example, even though the type for the fst function is fst :: (a, b) -> a, the compiler does not complain for the function fst'. But the compiler complains about type bindings for the function elem'.
fst' :: (a,a) -> a
fst' s = fst s
elem' :: (Eq a, Eq b) => a -> [b] -> Bool
elem' x xs = elem x xs
haskell types typeclass type-mismatch typechecking
New contributor
user4132 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I am a Haskell newbie trying to wrap my head around type binding in functions and how Haskell enforces it. For example, even though the type for the fst function is fst :: (a, b) -> a, the compiler does not complain for the function fst'. But the compiler complains about type bindings for the function elem'.
fst' :: (a,a) -> a
fst' s = fst s
elem' :: (Eq a, Eq b) => a -> [b] -> Bool
elem' x xs = elem x xs
haskell types typeclass type-mismatch typechecking
haskell types typeclass type-mismatch typechecking
New contributor
user4132 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
user4132 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 8 hours ago
Will Ness
49.1k4 gold badges72 silver badges134 bronze badges
49.1k4 gold badges72 silver badges134 bronze badges
New contributor
user4132 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 9 hours ago
user4132user4132
432 bronze badges
432 bronze badges
New contributor
user4132 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
user4132 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
When you use a function with incorrect types. The reason why it does not complain about the first one is because you here define a "subset" type signature, that is fine. Whereas in the latter, that is not the case.
– Willem Van Onsem
9 hours ago
1
The nicest way I've found to think about this is that you can always make a type more specific than it needs to be, but you can't always make it more general. A spoon can be used for many things, but my spoon I choose to use only on ice-cream ;)
– AJFarmar
6 hours ago
add a comment |
2
When you use a function with incorrect types. The reason why it does not complain about the first one is because you here define a "subset" type signature, that is fine. Whereas in the latter, that is not the case.
– Willem Van Onsem
9 hours ago
1
The nicest way I've found to think about this is that you can always make a type more specific than it needs to be, but you can't always make it more general. A spoon can be used for many things, but my spoon I choose to use only on ice-cream ;)
– AJFarmar
6 hours ago
2
2
When you use a function with incorrect types. The reason why it does not complain about the first one is because you here define a "subset" type signature, that is fine. Whereas in the latter, that is not the case.
– Willem Van Onsem
9 hours ago
When you use a function with incorrect types. The reason why it does not complain about the first one is because you here define a "subset" type signature, that is fine. Whereas in the latter, that is not the case.
– Willem Van Onsem
9 hours ago
1
1
The nicest way I've found to think about this is that you can always make a type more specific than it needs to be, but you can't always make it more general. A spoon can be used for many things, but my spoon I choose to use only on ice-cream ;)
– AJFarmar
6 hours ago
The nicest way I've found to think about this is that you can always make a type more specific than it needs to be, but you can't always make it more general. A spoon can be used for many things, but my spoon I choose to use only on ice-cream ;)
– AJFarmar
6 hours ago
add a comment |
1 Answer
1
active
oldest
votes
fst has as type fst :: (a, b) -> a so that means it is fine to define a function:
fst' :: (a, a) -> a
fst' = fst
Your fst' function is more restrictive than the fst function. Regardless with what you replace a in your fst' function that is fine for fst. If for example a ~ Bool holds, then you call fst with signature fst :: (Bool, Bool) -> Bool. But since fst can deal with all as and b, it is fine that both elements of the tuple are Bool, so given fst can handle tuples for all possible types for both the first and the second item of the 2-tuple, it is defintely ok if the two items have the same type.
The latter is not ok, here you define:
elem' :: (Eq a, Eq b) => a -> [b] -> Bool
elem' = elem
but elem has type elem :: Eq a => a -> [a] -> Bool. The signatures you can make with the elem' function are not a subset of the ones of the elem function, since you can set a ~ Int and b ~ Bool. In that case you expect elem :: Int -> [Bool] -> Bool, but evidently that does not hold, since the Int and Bool type are two different types, and in the elem signature, these are both as.
Thanks for the explanation.
– user4132
8 hours ago
1
Covers everything, nothing to add here, I wanted to answer adding something. But usually your answers are very good and complete.
– Damian Lattenero
6 hours ago
@DamianLattenero: thank you :)
– Willem Van Onsem
4 hours ago
add a comment |
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
);
);
user4132 is a new contributor. Be nice, and check out our Code of Conduct.
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%2f57242142%2fwhen-does-haskell-complain-about-incorrect-typing-in-functions%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
fst has as type fst :: (a, b) -> a so that means it is fine to define a function:
fst' :: (a, a) -> a
fst' = fst
Your fst' function is more restrictive than the fst function. Regardless with what you replace a in your fst' function that is fine for fst. If for example a ~ Bool holds, then you call fst with signature fst :: (Bool, Bool) -> Bool. But since fst can deal with all as and b, it is fine that both elements of the tuple are Bool, so given fst can handle tuples for all possible types for both the first and the second item of the 2-tuple, it is defintely ok if the two items have the same type.
The latter is not ok, here you define:
elem' :: (Eq a, Eq b) => a -> [b] -> Bool
elem' = elem
but elem has type elem :: Eq a => a -> [a] -> Bool. The signatures you can make with the elem' function are not a subset of the ones of the elem function, since you can set a ~ Int and b ~ Bool. In that case you expect elem :: Int -> [Bool] -> Bool, but evidently that does not hold, since the Int and Bool type are two different types, and in the elem signature, these are both as.
Thanks for the explanation.
– user4132
8 hours ago
1
Covers everything, nothing to add here, I wanted to answer adding something. But usually your answers are very good and complete.
– Damian Lattenero
6 hours ago
@DamianLattenero: thank you :)
– Willem Van Onsem
4 hours ago
add a comment |
fst has as type fst :: (a, b) -> a so that means it is fine to define a function:
fst' :: (a, a) -> a
fst' = fst
Your fst' function is more restrictive than the fst function. Regardless with what you replace a in your fst' function that is fine for fst. If for example a ~ Bool holds, then you call fst with signature fst :: (Bool, Bool) -> Bool. But since fst can deal with all as and b, it is fine that both elements of the tuple are Bool, so given fst can handle tuples for all possible types for both the first and the second item of the 2-tuple, it is defintely ok if the two items have the same type.
The latter is not ok, here you define:
elem' :: (Eq a, Eq b) => a -> [b] -> Bool
elem' = elem
but elem has type elem :: Eq a => a -> [a] -> Bool. The signatures you can make with the elem' function are not a subset of the ones of the elem function, since you can set a ~ Int and b ~ Bool. In that case you expect elem :: Int -> [Bool] -> Bool, but evidently that does not hold, since the Int and Bool type are two different types, and in the elem signature, these are both as.
Thanks for the explanation.
– user4132
8 hours ago
1
Covers everything, nothing to add here, I wanted to answer adding something. But usually your answers are very good and complete.
– Damian Lattenero
6 hours ago
@DamianLattenero: thank you :)
– Willem Van Onsem
4 hours ago
add a comment |
fst has as type fst :: (a, b) -> a so that means it is fine to define a function:
fst' :: (a, a) -> a
fst' = fst
Your fst' function is more restrictive than the fst function. Regardless with what you replace a in your fst' function that is fine for fst. If for example a ~ Bool holds, then you call fst with signature fst :: (Bool, Bool) -> Bool. But since fst can deal with all as and b, it is fine that both elements of the tuple are Bool, so given fst can handle tuples for all possible types for both the first and the second item of the 2-tuple, it is defintely ok if the two items have the same type.
The latter is not ok, here you define:
elem' :: (Eq a, Eq b) => a -> [b] -> Bool
elem' = elem
but elem has type elem :: Eq a => a -> [a] -> Bool. The signatures you can make with the elem' function are not a subset of the ones of the elem function, since you can set a ~ Int and b ~ Bool. In that case you expect elem :: Int -> [Bool] -> Bool, but evidently that does not hold, since the Int and Bool type are two different types, and in the elem signature, these are both as.
fst has as type fst :: (a, b) -> a so that means it is fine to define a function:
fst' :: (a, a) -> a
fst' = fst
Your fst' function is more restrictive than the fst function. Regardless with what you replace a in your fst' function that is fine for fst. If for example a ~ Bool holds, then you call fst with signature fst :: (Bool, Bool) -> Bool. But since fst can deal with all as and b, it is fine that both elements of the tuple are Bool, so given fst can handle tuples for all possible types for both the first and the second item of the 2-tuple, it is defintely ok if the two items have the same type.
The latter is not ok, here you define:
elem' :: (Eq a, Eq b) => a -> [b] -> Bool
elem' = elem
but elem has type elem :: Eq a => a -> [a] -> Bool. The signatures you can make with the elem' function are not a subset of the ones of the elem function, since you can set a ~ Int and b ~ Bool. In that case you expect elem :: Int -> [Bool] -> Bool, but evidently that does not hold, since the Int and Bool type are two different types, and in the elem signature, these are both as.
answered 8 hours ago
Willem Van OnsemWillem Van Onsem
174k18 gold badges175 silver badges263 bronze badges
174k18 gold badges175 silver badges263 bronze badges
Thanks for the explanation.
– user4132
8 hours ago
1
Covers everything, nothing to add here, I wanted to answer adding something. But usually your answers are very good and complete.
– Damian Lattenero
6 hours ago
@DamianLattenero: thank you :)
– Willem Van Onsem
4 hours ago
add a comment |
Thanks for the explanation.
– user4132
8 hours ago
1
Covers everything, nothing to add here, I wanted to answer adding something. But usually your answers are very good and complete.
– Damian Lattenero
6 hours ago
@DamianLattenero: thank you :)
– Willem Van Onsem
4 hours ago
Thanks for the explanation.
– user4132
8 hours ago
Thanks for the explanation.
– user4132
8 hours ago
1
1
Covers everything, nothing to add here, I wanted to answer adding something. But usually your answers are very good and complete.
– Damian Lattenero
6 hours ago
Covers everything, nothing to add here, I wanted to answer adding something. But usually your answers are very good and complete.
– Damian Lattenero
6 hours ago
@DamianLattenero: thank you :)
– Willem Van Onsem
4 hours ago
@DamianLattenero: thank you :)
– Willem Van Onsem
4 hours ago
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
user4132 is a new contributor. Be nice, and check out our Code of Conduct.
user4132 is a new contributor. Be nice, and check out our Code of Conduct.
user4132 is a new contributor. Be nice, and check out our Code of Conduct.
user4132 is a new contributor. Be nice, and check out our Code of Conduct.
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%2f57242142%2fwhen-does-haskell-complain-about-incorrect-typing-in-functions%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
2
When you use a function with incorrect types. The reason why it does not complain about the first one is because you here define a "subset" type signature, that is fine. Whereas in the latter, that is not the case.
– Willem Van Onsem
9 hours ago
1
The nicest way I've found to think about this is that you can always make a type more specific than it needs to be, but you can't always make it more general. A spoon can be used for many things, but my spoon I choose to use only on ice-cream ;)
– AJFarmar
6 hours ago