4 ms·
class Monad def foo return Monad.new end end is a monad by the article, but is certainly not a monad. EDIT: s/unit/foo/, to remove dis
by steveklabnik 14y ago
class Monad
def foo
return Monad.new
end
end
is a monad by the article, but is certainly not a monad.
EDIT: s/unit/foo/, to remove discussion about comparisons to the 'unit' that monads actually have.
- sophacles 14y agoI don't fully understand monads, but this is disingenuous. The article clearly compares unit to constructors, in a way that is far more like this: class Monad: def __init__(self, val): self.val = val unit = __init__ def bind(self, fun): return fun(self.val) Now, in the case of our ruby and python examples respectively, there is no sane way of enforcing typing, but if it works out, it looks to me like that at least implements the monad interface. Not so sure how all the laws work out... But interestingly we could implement lift as a Monad method by: def lift(self, fun=lambda x: x*2): self.val = fun(self.val) return self unless I really don't get all this.
- steveklabnik 14y agoThe article says: > A Monad is an object whose methods return monads. Further down, it explicitly states that this is not an analogy, that it is 'equivalent': > That is, that the following definition is equivalent > to the type theoretical definition of monads: My Monad object has one (actually... let's not go there yet) method, and it returns a Monad. I happened to name it 'unit' but it could have been 'foo' and it would still fall under the definition given in the article. I've edited my above post to change the name. Okay, now let's go there: another reason that this is even more silly is that my object has way more than one method: irb(main):005:0> Monad.new.methods.count => 57 Many of these do not return a monad. Even if I inherited from BasicObject, there would be methods that do not return a Monad, and therefore, according to the above definition, it is impossible to have a monad in Ruby. However, you can have a monad in Ruby, so this further demonstrates that the above definition is incorrect.
- Peaker 14y agoYour class is not representing the "Monad" abstraction, but the "Identity" value wrapper that happens to have a Monad instance.