[Haskell-cafe] Suggestions For An Intro To Monads Talk.
Tillmann Rendel
rendel at informatik.uni-marburg.de
Wed Aug 4 07:48:49 EDT 2010
Hi,
aditya siram wrote:
> For example in the beginning it was useful for me to think of monads
> (and typeclasses really) as approximating Java interfaces.
Type classes are somewhat parallel to Java interfaces, but Monad is a
*specific* type class, so it should be somewhat parallel to a *specific*
Java interface, if at all.
Type classes are somewhat parallel to Java interfaces because a Java
interface
interface Foo {
Result method (Argument argument);
}
declares that there is a set of types so that every type T in that set
has an operation (T, Argument) -> Result, with these operations all
implemented specifically to particular type T. In Haskell, the type class
class Foo t where
method : t -> Argument -> Result
expresses a similar concept. There are a number of differences though:
Firstly, in Java, calls to the method are late bound, while in Haskell,
they are early bound. However, a kind of late bound behavior can be
achieved using existentials.
Secondly, in Java, the receiver of the method has to be of type T, and T
may not appear at other positions in the type of the method, while in
Haskell, T may appear anywhere in the type of the method, even more then
once.
Finally, in Java, T has to be a proper type (of kind *), while in
Haskell, it may be an improper type (of a kind involving ->).
Already for the type class Functor, these differences become relevant.
class Functor f where
fmap :: (a -> b) -> f a -> f b
f has kind (* -> *), and it is mentioned twice in the type of fmap.
Conclusion: While Haskell type classes have some similarities to Java
interfaces, the type class Functor (or Monad, if you like) is not that
similar to any Java interface, because it uses features specific to
Haskell type classes which are not available in Java interfaces.
Nevertheless, it may be helpful for a Java developer to understand that
Haskell type classes are more similar to Java interfaces than to Java
classes.
Tillmann
More information about the Haskell-Cafe
mailing list