3 ms·
The 'm' is a type variable. This is a consequence of a difference in how Java interfaces and Haskell typeclasses are defined. (I'm continuing wyager's analogy b
by swift 12y ago
The 'm' is a type variable. This is a consequence of a difference in how Java interfaces and Haskell typeclasses are defined. (I'm continuing wyager's analogy between Java interfaces and Haskell typeclasses here.)
Here's a sample Java interface:
interface Pushable {
void push(bool shouldShove);
}
Here's a similar Haskell typeclass:
class Pushable p where
push :: p -> Bool -> ()
In type signatures, an identifier starting with a lowercase letter is a type variable. So 'p' is a type variable here, like the 'm' type variable used in the Monad typeclass. It refers to the type which, in Java terms, implements Pushable. (In Haskell you'd say the type "is an instance of Pushable".)
So why this difference? Java, being a C++-style OO language, privileges the implicit first argument. When class Button implements Pushable, you can think of its push method as having a real signature of "void push(Button this, bool shouldShove)". This particular pattern is special in Java, so you don't have to write the "Button this" parameter explicitly. But it's not a special pattern in Haskell, so you do need to explicitly write the 'p' in "push :: p -> Bool -> ()".
To drive the point home, maybe one more comparison is useful. In Java, when an interface method needs to take a parameter that has the type of the implementing class, you need to do something that looks a bit more like Haskell. Here's an example:
interface Comparable<T> {
Ordering compare(T other);
}
If you're not familiar with this idiom (which is the same as the CRTP in C++), here's how you'd implement Comparable<T>:
class Foo : Comparable<Foo> {
Ordering compare(Foo other) { ... }
}
Now compare the type parameter 'T' in our Java Comparable<T> interface and the type parameter 't' in this Haskell Comparable typeclass:
class Comparable t where
compare :: t -> t -> Ordering
They serve the same role - they refer to any type that may implement that interface (in Java) or be an instance of that typeclass (in Haskell).
Hopefully this clarifies things a bit.