Is general case of monad expressible in Java 6? Note the words "general case" — it may be possible that general case of monad is not expressible, although many particular cases of monad (i.e. many specific monads) are expressible.
The problem here is (lack of) Higher-kinded generics in Java ; however, I saw that sample Haskell code was really ported to Java using the approach like https://stackoverflow.com/a/877036/1123502 (that is, public class Fix<F extends Fix<F>>
).
Of course, non-type-safe implementations (like using Object and downcasts) are not interesting.
Update: there are 2 common monad definitions: join-fmap and bind-return. Although they are (mathematically) equivalent, they may be not equivalent in the sense that one definition is expressible in Java, whereas other is not (however, it seems to me, that non-equivalence is unlikely). So my question concern both definitions.
The bottom line: did anyone overcome all obstacles and wrote "general case" monad in Java 6? Or, alternatively, please point out a paper, or a thorough blog post, or thoroughly explain why it is not possible.
bind
isMonad<M,T>
- notM<T>
, which wouldn't be possible in Java. Consequently if you have a List class that inherits from Monad, there is no guarantee that callingbind
on such a list will produce another list, so it wouldn't be legal to write something likeMonadicList<T> strings = myMonadicList.bind(...)
without a cast. ... - sepp2knew M
whereM
is a type parameter. So this makes it impossible to create instances of an arbitrary monadic type in a generic method (without reflection tricks like passingClass
objects around). - sepp2k