3 ms·
You're absolutely right; that is confusing. Swift's explanation in response to your comment is good, so hopefully that helps. There are a few reasons I wrote "
by wyager 12y ago
You're absolutely right; that is confusing. Swift's explanation in response to your comment is good, so hopefully that helps.
There are a few reasons I wrote "m a".
When you use a lowercase letter in a type definition (like "m" or "a"), that's a type variable. The type isn't anything in particular. In a function like
id :: a -> a
that "a" can become anything when you use the function later. So you can use "id" on integers, or floats, or strings, or whatever. And with functions like
add3numbers :: Num a => a -> a -> a -> a
that "a" can become anything that's an instance of the "Num" typeclass (in Java: anything that implements the "Num" interface). "Num" is a restriction on the value of "a", but "a" is still variable.
If I were to write a function that had an uppercase word in the type, like
doFooThing :: Foo -> Foo
an uppercase type indicates that this isn't a type variable, but rather a specific type. In this case, the function only takes and returns the type called "Foo". So that's why I used a lowercase word in my definition of ">>=", as it works on any Monad, not just Foo. Here's how you actually write out the definition of the Monad typeclass:
class Monad m where
(>>=) :: m a -> (a -> m b) -> m b
return :: a -> m a
In the definition of >>=, we don't define it in terms of Foo or any other specific Monad; we define it in terms of a type variable (which is idiomatically "m", for "Monad"). "m" can then be filled in with any Monad (like Foo, or Maybe, or IO) when the time comes to actually use ">>=".
You could also define Monad with
class Monad qwerty where
(>>=) :: qwerty ping -> (ping -> qwerty pong) -> qwerty pong
return :: dazzle -> qwerty dazzle
As you can see, this is not as nice as e.g. "a -> m a"
So it would have been more correct for me to have said "here's the type signature for >>=, and when you use >>= with something called Foo that implements the Monad interface, it <explain >>='s type in terms of Foo>."