r/functionalprogramming • u/ibrahimbensalah • Feb 28 '23
Question Is JSX a hidden monad bind?
Say we have this example
function Component() {
return <Layout><Page /></Layout>
}
And lets assume this is compiled to
where type of jsx is (fn, JSX.Element) => JSX.Element. For simplicity ignore that we can have multiple elements in the second argument.
jsx is not in the form of monadic bind (JSX.Element, (fn) => JSX.Element) => JSX.Element. If we ignore the laws, can we say jsx is a special case of the monadic bind function nevertheless?
15
Upvotes
View all comments
Show parent comment
5
u/gabedamien Feb 28 '23 edited Feb 28 '23
Ok, so with
Template<T>, it actually does have aTvalue within it; it doesn't demand aTas input, in the sense of a function param. In that case,Templateis indeed a functor, and yourmapis fine.So now we come back to: is
JSXa monad?(First, I think there is a small typo in your
//comment where you wrotef : x -> JSX ubut I think you meantf : t -> JSX u. I fixed it in the quote above.)This function signature you provided is indeed correct for monadic bind — so far so good! But of course, bind is not defined purely in terms of type, but also in terms of the logic of what is actually happening at the value level (which is implied by the monad laws).
So, the fundamental question here is, does this "bind" allow for transformation in a way that follows the spirit (or letter) of what it means to be a monad?
Looking at your proposed implementation:
If I understand the intent correctly, the idea is that we can generate some more
JSXfrom the value embedded in the currentTemplate, and then the existing JSX and the new generated JSX from the transformation are fused by the logic of "throw away the child template, use the new template, and combine the hydration of both by interleaving some additional ops".This might be a monad, both in spirit and in letter (i.e. lawfully). I am not totally sure of either since I don't entirely know the implications of this logic. But it's certainly prima facie possible. The next step would be to examine to what extent this does or doesn't obey the monad laws. The laws are sometimes seen as bureaucratic or anal-retentive, but IMHO even if you have an unlawful instance of a monad, it helps to judge its "monad-ness" to know how and why it is unlawful.
Also, to complete the API you'd need a
pure : T -> JSX<T>function to allow embedding arbitrary values intoJSX.But sure, at this point I at least understand why you're asking if
JSXis / can be a monad!I should emphasize that I don't know how much your
JSXtype relates to what most people think of when they talk about JSX as a sort of preprocessor framework for React. Vanilla JSX to me is a tree of calls toReact.CreateElement. But that's getting beyond what I can intelligently try to untangle right now.EDIT: ah screw it, we've come this far, let's see if it obeys the laws:
Presumably we would define
pure : a -> JSX aas a JSX value that wraps a Template withaas its value, and has an emptyhydratearray.In that case, a transformation
f : x -> JSX<u>might, for example, convert an Int to a String and make a new JSX with an empty hydrate array.But in that case,
pure a >>= fandf awould NOT be equivalent, because>>=would, according to your proposed implementation, add in new hydration opsMoveToChildandPopChild. We'd have one version with these new ops, and one version without, depending on which side of the≡we choose. So this definitely doesn't obey the laws.So,
JSXis not a monad, strictly speaking. It might still be arguably a monad "in spirit", just asPromiseis.