Hello, for anyone interested, type of fmap (const '2') Just is:
ghci> :t fmap (const '2') Just
fmap (const '2') Just :: a -> Char
And for every input answear will be '2'. Now, I want to know how to derive it. At the beginning I though that Char is just functor, and fmap would just applied (const '2') Just like this:
fmap (const '2') Just $ '3' =
fmap ( (const '2') Just $ 3) =
-- fmap would do nothing?
(const '2') Just 3 =
'2'
But I think everything I wrote is wrong and just blindly guessing.
My question is, is there better and mechanical method to always correctly derive type? If that have something with lambda calcules or other subject, Iβm willing to learn.
The type of (.) is (b β c) β (a β b) β (a β c)
Then we observe that βJustβ is a value constructor, i.e a function β:: a β Maybe aβ
We plug the definitions in and get
fmap (const βcβ ) Just =
const βcβ . Just =
(\a β βcβ) . Just ::
(a β Char) . (u β Maybe u)
so Maybe u unifies with a and is discarded by the first argument of (const βcβ) , and we are left with a function β:: u β Charβ, which ignores the argument and will always return βcβ.