r/functionalprogramming • • 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

function Component() {
   return jsx(Layout, jsx(Page));
}

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?

14 Upvotes

25 comments sorted by

View all comments

Show parent comment

3

u/ibrahimbensalah Feb 28 '23

The exact method does not exist, but also it does, the pattern is implemented ah-hoc-ely because I wasnt fully aware of what I am dealing with. I had e.g. a lot of duplication of the pattern matching logic which motivated this exploration.

Template can have strings, int, Date, or in fact any object. Template encapsulate the type provided, a string template is represented as follows:

type TextTemplate<T> = {
   type: 'text',
   data: T
}

Template<int> can be constructed with: textNode(5) or equaly tMap(textNode("xania"), length) === textNode(5)

If we peek into textNode(5) then it would look like this:

{ 
   type: 'text',
   data: 5
}

So Template<T> is not a string, but rather a discriminated union of variety of types, one of is the TextTemplate mentioned above.

There is even support for Promises and Observables and there is a constructor for each that produces a Template. but I didnt want to complicate the discussion.

Transforming Template to HTML happens only in side effect land, after we call render at the end.

5

u/gabedamien Feb 28 '23 edited Feb 28 '23

Template can have strings, int, Date, or in fact any object

Ok, so with Template<T>, it actually does have a T value within it; it doesn't demand a T as input, in the sense of a function param. In that case, Template is indeed a functor, and your map is fine.

So now we come back to: is JSX a monad?

// bind :: JSX t -> (f : t -> JSX u) -> JSX u
function bind<T, U>(child: JSX<T>, fn: (t:T) => JSX<U>): JSX<U> {

(First, I think there is a small typo in your // comment where you wrote f : x -> JSX u but I think you meant f : 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:

// bind :: JSX t -> (f : t -> JSX u) -> JSX u
function bind<T, U>(child: JSX<T>, fn: (t:T) => JSX<U>): JSX<U> {
    const parent = fn(child.template);
    return {
      template: parent.template,
      hydrate: [ 
         ...parent.hydrate, 
         MoveToChild, 
         ...child.hydrate,
         PopChild
      ]
    }
}

If I understand the intent correctly, the idea is that we can generate some more JSX from the value embedded in the current Template, 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 into JSX.

But sure, at this point I at least understand why you're asking if JSX is / can be a monad!

I should emphasize that I don't know how much your JSX type 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 to React.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:

Left identity: pure a >>= f ≡ f a

Presumably we would define pure : a -> JSX a as a JSX value that wraps a Template with a as its value, and has an empty hydrate array.

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 >>= f and f a would NOT be equivalent, because >>= would, according to your proposed implementation, add in new hydration ops MoveToChild and PopChild. 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, JSX is not a monad, strictly speaking. It might still be arguably a monad "in spirit", just as Promise is.

3

u/ibrahimbensalah Feb 28 '23

I need a moment to understand the last part 🤪

3

u/ibrahimbensalah Mar 01 '23

Got it, there a mistake in proposed bind, that’s why it doesn’t obey left id. To be continued 👍😬

3

u/ibrahimbensalah Mar 02 '23

Hi again, I added a reply as a top level reply to have more room for the code snippets