6 ms·
The problem with bringing jQuery into the discussion is that it's not a monad. Don Stewart explains this more elegantly than I can [1] but the crux of the argum
by freyrs3 14y ago
The problem with bringing jQuery into the discussion is that it's not a monad. Don Stewart explains this more elegantly than I can [1] but the crux of the argument is that chaining together computations is not enough to be a monad, one needs to prove that the monad laws hold.
[1] http://stackoverflow.com/questions/10496932/is-jquery-a-monad/10499668#10499668 http://stackoverflow.com/questions/10496932/is-jquery-a-mona...
- sordina 14y agoThe majority of the 'proofs' I've seen that jQuery isn't a monad seem to either be probabalistic, or use the "Burden of Proof" argument to show that since nobody has proved that jQuery is a monad, we should assume that it isn't. I agree that jQuery probably isn't a monad, but it rubs me the wrong way that people aren't willing to say "It has yet to be determined if jQuery is a monad". Obviously it is trivial to show that it isn't a monad by the fact that javascript permits any action in pure code, but by the same token, unsafePerformIO proves that Haskell has no monads. I think it's fair to operate under a reasonable subset of jQuery functionality when asking the question.
- xyzzyz 14y agoThe point is that the concept of monad is a very concrete thing. Monad is not "chaining operations". Monad is not "returning actions instead of values". Monad is not some vague concept. If JQuery is a monad, what are the monadic operations that make it a monad? What are its equivalents of return and bind (or any other equivalent definitions of a monad)? It's not a tricky question. Saying that JQuery is a monad without saying what its return and bind are is equivalent to saying that whistling is Turing complete without saying how you can compute stuff with whistling. The problem here is not whether whistling is Turing complete or not, the problem is what it would even mean for whistling to be Turing complete. This means that as of now, as long as you don't tell us what are the monadic operations of JQuery, it cannot be determined whether JQuery is a monad, because this question does not make sense yet.
- jes5199 14y agoJQuery is an implementation of the List monad.
- tome 14y agoJQuery is an enormous API, so it is implausible that it is an implementation of the list monad. Some aspect of it might be the list monad. If so, which?
- jes5199 14y agoMost jQuery methods are functions that are mapped over a collection of elements. Many verbs return collections themselves, which are concatted together into the resulting collection. That's analogous to how List works. So $(element) implements return as a -> [a] And bind is mapcat, which is called by most of the jQuery methods. Yes, that's a subset of the API, but it's the subset that distinguishes jQuery from any other collection of helper functions.
- nbouscal 14y ago$(element) does not implement return. $($(element)) gives $(element), which means that it does not work on an arbitrary datatype, which means that it does not implement return. jQuery may be analogous to a monad. That doesn't make it a monad.
- jes5199 14y agoThere's a lot of stuff in jQuery that's not monadic, but that doesn't disqualify it. There's no law that return(return(x)) has to be defined - a Haskell List of Integers is still monadic, even though return(return(1)) would be a type violation. And undefined code can do whatever you want and not be a problem for the definition.
- nbouscal 14y ago
- arianvanp 14y agowell how'd we define return :: a -> m a in the jQuery monad? First I thought return = jQuery but $(".yo") :: [HTMLElement] isnt return ".yo" :: JQuery String I'd probably model it like this: (|>) = flip ($) jQuery "selector" |> css "color" "red" |> filter someSelector |> css "color" "blue" which would turn into: css "color" "blue" (filter someSelector (css "color "red" (jQuery "selector"))) class Selector a where instance Selector Text where instance Selector HtmlElement where jQuery :: Selector a -> JQuery css :: Text -> Text -> JQuery -> JQuery filter :: Selector a -> JQuery -> JQuery
- deleted 14y ago[deleted]
- nbouscal 14y agoA monad must have a `return` function that works on an arbitrary datatype. That includes a datatype that is already 'wrapped' in the monad. For example, `return $ Just 5` yielding `Just (Just 5)`. jQuery does not have such a function. The `jQuery` function can 'wrap' strings, but it cannot wrap, for example, other jQuery objects. If you try, you simply get the original jQuery object back. Accordingly jQuery does not satisfy the monad laws, and is therefore not a monad.
- jes5199 14y agoI deny that the return function is required to act on arbitrary data types. In Haskell, is the type [Integer] no longer a monad because it only can contain integers?
- nbouscal 14y agoYes, in Haskell the type [Integer] is not a monad. For a type to be an instance of the Monad type class, it must be of kind * -> * . As [Integer] is of kind * , it cannot possibly be an instance of Monad.
- jes5199 14y agoI didn't mean to suggest that [Integer] was of type Monad, I meant that when you write code in terms of the List monad, that code still works and is still valid and is still "monadic" even when the input to that code is a list of a non-polymorphic type.
- nbouscal 14y agoI really don't follow what you're trying to say here. >>= has signature m a -> (a -> m b) -> m b, so clearly you are working with a concrete type m a. The point is that the concrete type isn't what instantiates the Monad type class, the constructor m is.
- frio 14y agoThe distinction is: return [Integer] will give you [[Integer]] (if you're inside the list Monad). $($("#some-id")) will give you the same result as $("#some-id"), because $ of a $ doesn't nest (or make sense, really). Therefore jQuery doesn't satisfy the Monad laws :).