Hello,
I'm going through Haskellbook and while doing some exercises I'm stack with trying to explain myself how types matches in this examples: meh :: Monad m => [a] -> (a -> m b) -> m [b] meh [] _ = pure [] meh (x:xs) f = (:) <$> f x <*> meh xs f flipType :: (Monad m) => [m a] -> m [a] flipType xs = meh xs id What puzzles me is how id function which is of type (a->a) can fit here where meh function is requesting function of type (a->m b)? The function (a -> m b) is function that says give me anything and I will give back anything wrapped up into some monad structure; id ( a -> a) on other hand says give me anything and I will return it back to you. So, to me, the first function is somehow more restricted than id function because it puts limitation on what output of that function can be and I'm struggling to understand how id function can fit here. I hope someone can help me how to reason about these functions here? thanks _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
The (a) and (b) are different in each case. Let's rewrite it and
rename the variables to emphasise the difference. flipType :: (Monad k) => [k x] -> k [x] This means that id can accept any argument type, as long as it
returns the same argument type. For example: On 09/08/18 18:32, [hidden email]
wrote:
_______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners signature.asc (499 bytes) Download Attachment |
You can also put another function as the argument to the id :
ghci > x :: a -> m b;x = undefined ghci > :t (id x) (id x) :: a -> m b
_______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
In reply to this post by simkestuff@gmail.com
Thanks, it is still a bit fuzzy to me ... I understand what you did but what confuses me is that when i look at function with signature like f :: Monad m => c -> m d I always think that return type is somehow restricted in comparison to input because it demands that output type is wraped inside something (monad in this case). For such signature to fit id signature (a -> a) , c type shoud be also wraped inside monad but it is not case here... Anyhow, I still have to figure it out thanks _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
You're right that the return type is more restricted than the argument, but it's in an *absolute* sense, not a relative one. It's not possible to relax `m d` to make it the same type as `c`, but it IS possible to constrain `c` to be the same as `m d`! And that's how `id` works here: the input in this case is known to be the same type as the output. You need something wrapped in a monad, and you already have that, so you just use it as-is.
_______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
Yes, thinking it the terms that something which is absolutely free to be whatever, can be restricted and reduced into something else while other way around is not possible is usefull; thanks On Fri, Aug 10, 2018 at 12:06 PM Theodore Lief Gannon <[hidden email]> wrote:
_______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
Free forum by Nabble | Edit this page |